Files
Kontrollverlust/internal/httpapi/notifications_test.go
T
irrlichtandClaude Opus 5.5 d6b6baedc1 Benachrichtigungen: Antworten auf eigene Beiträge
Bisher erfuhr man nur durch Nachsehen, ob jemand geantwortet hat. Jetzt
legt jede Antwort eine Benachrichtigung für den Autor des Elternbeitrags an
(Zeit, Typ, Text mit Auszug, interner Link, gesehen-Flag). Die Glocke in
der Navigation zählt die ungesehenen; die Liste markiert beim Ansehen alles
als gesehen.

Der Auszug zitiert die Antwort, deshalb hängt die Benachrichtigung an deren
pid: Bearbeiten zieht den Text nach, Löschen (auch des Kontos) nimmt sie mit.

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

86 lines
2.7 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]
wantMsg := "Jemand hat auf deinen Beitrag geantwortet. (" + strings.TrimSpace(long[:50]) + "…)"
if n.Kind != "reply" || n.Link != fmt.Sprintf("/e/%d", answer) || n.Seen || n.Message != wantMsg {
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, 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()
getAs(t, alice, srv.URL+"/api/notifications", &list)
if got := list.Notifications[0].Message; got != "Jemand hat auf deinen Beitrag geantwortet. (neu)" {
t.Errorf("nach dem Bearbeiten: %q", got)
}
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 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)
}
}