package httpapi import ( "fmt" "net/http" "net/url" "slices" "testing" ) func TestParseMentions(t *testing.T) { t.Parallel() tests := []struct { in string want []string }{ {"hallo @Bob, und @carol!", []string{"bob", "carol"}}, {"@bob @BOB @bob", []string{"bob"}}, {"mail@bob.org x@bob", nil}, {"@ab @" + "abcdefghijklmnopqrstu", nil}, // zu kurz, zu lang {"(@bob) #tag @_x_", []string{"bob", "_x_"}}, } for _, tt := range tests { if got := parseMentions(tt.in); !slices.Equal(got, tt.want) { t.Errorf("parseMentions(%q) = %q, erwartet %q", tt.in, got, tt.want) } } many := "" for i := range 15 { many += fmt.Sprintf(" @user%02d", i) } if n := len(parseMentions(many)); n != maxMentions { t.Errorf("%d Erwähnungen, erwartet höchstens %d", n, maxMentions) } } // mentionsOf liest die Erwähnungen eines Beitrags aus dem Thread. func mentionsOf(t *testing.T, c *http.Client, srv *testServer, pid int64) map[string]string { t.Helper() var th struct { Entry struct { Mentions map[string]string `json:"mentions"` } `json:"entry"` } getAs(t, c, fmt.Sprintf("%s/api/entry/%d/thread", srv.URL, pid), &th) return th.Entry.Mentions } func notificationsOf(t *testing.T, c *http.Client, srv *testServer) []notificationJSON { t.Helper() var list notificationList getAs(t, c, srv.URL+"/api/notifications", &list) return list.Notifications } func TestMentions(t *testing.T) { t.Parallel() srv := newTestServer(t) alice := registerAndLogin(t, srv, "alice") bob := registerAndLogin(t, srv, "bob") carol := registerAndLogin(t, srv, "carol") root := createEntry(t, alice, srv, post("hallo @Bob und @carol, @alice, mail@bob.org, @niemand")) if got := mentionsOf(t, alice, srv, root); len(got) != 3 || got["bob"] != "bob" || got["alice"] != "alice" { t.Errorf("mentions: %v", got) } for _, c := range []*http.Client{bob, carol} { n := notificationsOf(t, c, srv) if len(n) != 1 || n[0].Kind != "mention" || n[0].Message != "@alice hat dich erwähnt." || n[0].Link != fmt.Sprintf("/e/%d", root) { t.Errorf("erwähnt: %+v", n) } } if n := notificationsOf(t, alice, srv); len(n) != 0 { t.Errorf("selbst erwähnt: %+v", n) } // Antwort samt Erwähnung des Elternautors: nur die Antwort-Benachrichtigung. createEntry(t, bob, srv, reply(root, "danke @alice")) if n := notificationsOf(t, alice, srv); len(n) != 1 || n[0].Kind != "reply" { t.Errorf("antwort mit erwähnung: %+v", n) } // Umbenennen: der alte Link führt zur selben Person, nicht zum neuen // Träger des Handles. for _, r := range []struct { c *http.Client handle string }{{bob, "robert"}, {carol, "bob"}} { resp := postForm(t, r.c, srv.URL+"/api/user/handle", url.Values{"handle": {r.handle}}) resp.Body.Close() } if got := mentionsOf(t, alice, srv, root); got["bob"] != "robert" || got["carol"] != "bob" { t.Errorf("nach umbenennen: %v", got) } // Bearbeiten: weggefallene Erwähnung verschwindet samt Benachrichtigung. resp := postForm(t, alice, fmt.Sprintf("%s/api/entry/%d/edit", srv.URL, root), url.Values{"content": {"hallo @robert"}}) resp.Body.Close() if got := mentionsOf(t, alice, srv, root); len(got) != 1 || got["robert"] != "robert" { t.Errorf("nach bearbeiten: %v", got) } if n := notificationsOf(t, carol, srv); len(n) != 0 { t.Errorf("carol nach bearbeiten: %+v", n) } // robert war schon als @bob erwähnt: die Benachrichtigung bleibt die alte // (schon gesehene), keine zweite. if n := notificationsOf(t, bob, srv); len(n) != 1 || !n[0].Seen { t.Errorf("robert nach bearbeiten: %+v", n) } } func TestUserSuggest(t *testing.T) { t.Parallel() srv := newTestServer(t) alice := registerAndLogin(t, srv, "alice") registerAndLogin(t, srv, "joerg") registerAndLogin(t, srv, "jo_ann") registerAndLogin(t, srv, "maxi") resp := postForm(t, alice, srv.URL+"/api/user/display_name", url.Values{"display_name": {"Jörg Joseph"}}) resp.Body.Close() suggest := func(q string) []string { var out struct { Users []suggestionJSON `json:"users"` } if code := getAs(t, alice, srv.URL+"/api/users/suggest?q="+url.QueryEscape(q), &out); code != http.StatusOK { t.Fatalf("suggest %q: status %d", q, code) } var hs []string for _, u := range out.Users { hs = append(hs, u.Handle) } return hs } for _, tt := range []struct { q string want []string }{ {"@Jo", []string{"jo_ann", "joerg", "alice"}}, // Handles zuerst, dann Anzeigename {"jö", []string{"alice"}}, {"jo_", []string{"jo_ann"}}, // _ ist kein LIKE-Platzhalter {"", nil}, {"xyz", nil}, } { if got := suggest(tt.q); !slices.Equal(got, tt.want) { t.Errorf("suggest(%q) = %v, erwartet %v", tt.q, got, tt.want) } } if code := getAny(t, srv.URL+"/api/users/suggest?q=jo", &struct{}{}); code != http.StatusUnauthorized { t.Errorf("ohne Login: %d", code) } }