Files
Kontrollverlust/internal/httpapi/notifications_test.go
T
irrlichtandClaude Opus 5.5 fe0e1ba5a3 Benachrichtigungen: Typ fett, Auszug und Datum darunter
Eine Zeile mit Meldung, Auszug in Klammern und Datum war schwer zu
überfliegen. Jetzt steht fett, was passiert ist, darunter in normaler Schrift
Auszug und Datum; dafür liefert die API den Auszug getrennt (excerpt). Mehr
Abstand zwischen den Einträgen, keine Unterstreichung. Da der Typ nun immer
fett ist, markiert "neu" hinter dem Datum, was ungesehen war.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 21:39:07 +01:00

120 lines
3.9 KiB
Go

package httpapi
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
)
type notificationList struct {
Notifications []notificationJSON `json:"notifications"`
}
func unreadNotifications(t *testing.T, c *http.Client, srv *testServer) int {
t.Helper()
var out struct {
Unread int `json:"unread"`
}
if code := getAs(t, c, srv.URL+"/api/notifications/unread", &out); code != http.StatusOK {
t.Fatalf("/notifications/unread: status %d", code)
}
return out.Unread
}
func TestNotifyReply(t *testing.T) {
t.Parallel()
srv := newTestServer(t)
alice := registerAndLogin(t, srv, "alice")
bob := registerAndLogin(t, srv, "bob")
root := createEntry(t, alice, srv, post("frage"))
createEntry(t, alice, srv, reply(root, "nachtrag")) // an sich selbst: nichts
long := strings.Repeat("wort ", 20)
answer := createEntry(t, bob, srv, reply(root, long))
if n := unreadNotifications(t, alice, srv); n != 1 {
t.Fatalf("alice ungesehen: %d, erwartet 1", n)
}
if n := unreadNotifications(t, bob, srv); n != 0 {
t.Errorf("bob ungesehen: %d, erwartet 0", n)
}
var list notificationList
getAs(t, alice, srv.URL+"/api/notifications", &list)
if len(list.Notifications) != 1 {
t.Fatalf("liste: %+v", list)
}
n := list.Notifications[0]
if n.Kind != "reply" || n.Link != fmt.Sprintf("/e/%d", answer) || n.Seen ||
n.Message != "@bob hat auf deinen Beitrag geantwortet." || n.Excerpt != strings.TrimSpace(long[:50])+"…" {
t.Errorf("benachrichtigung: %+v", n)
}
// Das Ansehen der Liste markiert alles als gesehen.
if c := unreadNotifications(t, alice, srv); c != 0 {
t.Errorf("nach dem Ansehen ungesehen: %d", c)
}
getAs(t, alice, srv.URL+"/api/notifications", &list)
if !list.Notifications[0].Seen {
t.Error("zweiter Abruf: nicht als gesehen markiert")
}
// Bearbeiten zieht den Auszug nach, ein neuer Handle erscheint sofort,
// Löschen nimmt die Benachrichtigung mit.
editURL := fmt.Sprintf("%s/api/entry/%d/edit", srv.URL, answer)
resp := postForm(t, bob, editURL, url.Values{"content": {"neu"}})
resp.Body.Close()
resp = postForm(t, bob, srv.URL+"/api/user/handle", url.Values{"handle": {"robert"}})
resp.Body.Close()
getAs(t, alice, srv.URL+"/api/notifications", &list)
if n := list.Notifications[0]; n.Message != "@robert hat auf deinen Beitrag geantwortet." || n.Excerpt != "neu" {
t.Errorf("nach Bearbeiten und Umbenennen: %+v", n)
}
resp = postForm(t, bob, fmt.Sprintf("%s/api/entry/%d/delete", srv.URL, answer), nil)
resp.Body.Close()
getAs(t, alice, srv.URL+"/api/notifications", &list)
if len(list.Notifications) != 0 {
t.Errorf("nach dem Löschen: %+v", list.Notifications)
}
}
func TestNotifyBump(t *testing.T) {
t.Parallel()
srv := newTestServer(t)
alice := registerAndLogin(t, srv, "alice")
bob := registerAndLogin(t, srv, "bob")
mine := createEntry(t, alice, srv, post("mein erster\nbeitrag"))
own := createEntry(t, alice, srv, post("selbst repostet"))
for _, b := range []struct {
c *http.Client
pid int64
}{{bob, mine}, {alice, own}} {
resp := postForm(t, b.c, fmt.Sprintf("%s/api/entry/%d/bump", srv.URL, b.pid), nil)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("bump %d: status %d", b.pid, resp.StatusCode)
}
}
var list notificationList
getAs(t, alice, srv.URL+"/api/notifications", &list)
if len(list.Notifications) != 1 {
t.Fatalf("liste: %+v", list.Notifications)
}
n := list.Notifications[0]
// Wer repostet, wird nicht gespeichert: "Jemand", auch wenn es bob war.
if n.Kind != "bump" || n.Link != fmt.Sprintf("/e/%d", mine) ||
n.Message != "Jemand hat deinen Beitrag repostet." || n.Excerpt != "mein erster beitrag" {
t.Errorf("benachrichtigung: %+v", n)
}
}
func TestNotificationsNeedLogin(t *testing.T) {
t.Parallel()
srv := newTestServer(t)
if code := getAny(t, srv.URL+"/api/notifications/unread", &struct{}{}); code != http.StatusUnauthorized {
t.Errorf("ohne Login: status %d, erwartet 401", code)
}
}