Files
minstrel/internal/api/me_notifications_test.go
T
bvandeusenandClaude Opus 5.5 561c1d5eae
release / web (push) Failing after 1m29s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m22s
release / android (push) Successful in 4m57s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 4m23s
feat: new-music notices come from the scanner; request notices and the original specs go
Request notifications (approved, declined, completed, pending) are removed
along with the request flow that produced them. A new_music kind takes the
"new music arrived" slot: when a scan or a watcher batch finishes, the
scanner claims every album not yet announced (albums.announced_at) and tells
every user about the ones that are new: an album with a track on disk and
no track older than itself. The first import into an empty library, and an
album formed only by retagging tracks already held, announce nothing. One
notice per scan, carrying up to 50 albums and the total; it is never
coalesced, so a notice already emailed can't quietly absorb later arrivals.
It feeds the daily summary email, grouped by artist. Defaults: inbox and
email on, phone off.

Migration 0103 deletes request-kind notification rows and prefs, swaps the
kind CHECKs to new_music, and adds albums.announced_at with every album
held marked announced, so the first scan after it announces nothing old.

Web and Android: labels, the Android "Requests" settings section becomes
"New music", the shade channel your_requests becomes new_music (the old one
is deleted at startup), and "/" opens Home in the app. TrackLabel moves to
the notifications package. docs/smart-music-{server,client}-spec.md are
deleted per the operator; an Android comment loses the word "exchanges".

Grimoire #7666, #7671.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 23:29:13 -05:00

263 lines
9.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
)
func notificationsRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Get("/api/me/notifications", h.handleListMyNotifications)
r.Get("/api/me/notifications/unread-count", h.handleMyUnreadNotificationCount)
r.Post("/api/me/notifications/read-all", h.handleMarkAllMyNotificationsRead)
r.Post("/api/me/notifications/{id}/read", h.handleMarkMyNotificationRead)
r.Get("/api/me/notification-settings", h.handleGetMyNotificationSettings)
r.Put("/api/me/notification-settings", h.handlePutMyNotificationSettings)
return r
}
func callAs(t *testing.T, r chi.Router, user dbq.User, method, path, body string, out any) int {
t.Helper()
req := withUser(httptest.NewRequest(method, path, bytes.NewBufferString(body)), user)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if out != nil && rec.Code == http.StatusOK {
if err := json.Unmarshal(rec.Body.Bytes(), out); err != nil {
t.Fatalf("decode %s: %v", rec.Body.String(), err)
}
}
return rec.Code
}
func notifyN(t *testing.T, pool *pgxpool.Pool, user dbq.User, n int) {
t.Helper()
nt := notifications.New(pool, nil, nil)
for i := 0; i < n; i++ {
p := notifications.Payload{Count: 1, Albums: []notifications.Arrival{{Artist: "Artist", Title: "Album", AlbumID: "al-1"}}}
if err := nt.Notify(context.Background(), notifications.KindNewMusic,
notifications.ToUser(user.ID), p.Map()); err != nil {
t.Fatalf("notify: %v", err)
}
}
}
func TestMyNotifications_ListRendersPagesAndCounts(t *testing.T) {
h, pool := testHandlers(t)
alice := seedUser(t, pool, "notif-alice", "pw", false)
bob := seedUser(t, pool, "notif-bob", "pw", false)
notifyN(t, pool, alice, 3)
notifyN(t, pool, bob, 1)
r := notificationsRouter(h)
var page notificationsPageResp
if code := callAs(t, r, alice, http.MethodGet, "/api/me/notifications?limit=2", "", &page); code != http.StatusOK {
t.Fatalf("list = %d", code)
}
if len(page.Items) != 2 || page.UnreadCount != 3 || page.NextBefore == "" {
t.Fatalf("first page = %d items, unread %d, next %q; want 2, 3, a cursor", len(page.Items), page.UnreadCount, page.NextBefore)
}
first := page.Items[0]
if first.Title != "New album" || first.Body != "Artist – Album is in the library." || first.Link != "/albums/al-1" || first.ReadAt != nil {
t.Errorf("rendered item = %+v", first)
}
var rest notificationsPageResp
callAs(t, r, alice, http.MethodGet, "/api/me/notifications?limit=2&before="+page.NextBefore, "", &rest)
if len(rest.Items) != 1 || rest.NextBefore != "" {
t.Errorf("second page = %d items, next %q; want 1 and no cursor", len(rest.Items), rest.NextBefore)
}
seen := map[string]bool{}
for _, it := range append(page.Items, rest.Items...) {
if seen[it.ID] {
t.Errorf("item %s on two pages", it.ID)
}
seen[it.ID] = true
}
for _, bad := range []string{"?limit=0", "?limit=x", "?before=nonsense"} {
if code := callAs(t, r, alice, http.MethodGet, "/api/me/notifications"+bad, "", nil); code != http.StatusBadRequest {
t.Errorf("GET %s = %d, want 400", bad, code)
}
}
}
func TestMyNotifications_MarkReadIsOwnerScopedAndCountsDown(t *testing.T) {
h, pool := testHandlers(t)
alice := seedUser(t, pool, "notif-owner", "pw", false)
mallory := seedUser(t, pool, "notif-mallory", "pw", false)
notifyN(t, pool, alice, 2)
r := notificationsRouter(h)
var page notificationsPageResp
callAs(t, r, alice, http.MethodGet, "/api/me/notifications", "", &page)
id := page.Items[0].ID
if code := callAs(t, r, mallory, http.MethodPost, "/api/me/notifications/"+id+"/read", "", nil); code != http.StatusNotFound {
t.Errorf("another user's mark-read = %d, want 404", code)
}
for i := 0; i < 2; i++ {
if code := callAs(t, r, alice, http.MethodPost, "/api/me/notifications/"+id+"/read", "", nil); code != http.StatusNoContent {
t.Errorf("mark-read #%d = %d, want 204 (repeat is harmless)", i+1, code)
}
}
var count unreadCountResp
callAs(t, r, alice, http.MethodGet, "/api/me/notifications/unread-count", "", &count)
if count.UnreadCount != 1 {
t.Errorf("unread = %d, want 1", count.UnreadCount)
}
if code := callAs(t, r, alice, http.MethodPost, "/api/me/notifications/read-all", "", nil); code != http.StatusNoContent {
t.Fatalf("read-all = %d", code)
}
callAs(t, r, alice, http.MethodGet, "/api/me/notifications/unread-count", "", &count)
if count.UnreadCount != 0 {
t.Errorf("unread after read-all = %d, want 0", count.UnreadCount)
}
if code := callAs(t, r, alice, http.MethodPost, "/api/me/notifications/not-a-uuid/read", "", nil); code != http.StatusNotFound {
t.Errorf("malformed id = %d, want 404", code)
}
}
// A "mark all read" replayed from an offline queue carries the moment the
// user asked; what arrived after it stays unread.
func TestMyNotifications_ReadAllUpToLeavesLaterOnesUnread(t *testing.T) {
h, pool := testHandlers(t)
alice := seedUser(t, pool, "notif-upto", "pw", false)
r := notificationsRouter(h)
notifyN(t, pool, alice, 1)
var page notificationsPageResp
callAs(t, r, alice, http.MethodGet, "/api/me/notifications", "", &page)
seen := page.Items[0].CreatedAt
notifyN(t, pool, alice, 1)
body := `{"up_to":"` + seen.Format(time.RFC3339Nano) + `"}`
if code := callAs(t, r, alice, http.MethodPost, "/api/me/notifications/read-all", body, nil); code != http.StatusNoContent {
t.Fatalf("read-all up_to = %d", code)
}
var count unreadCountResp
callAs(t, r, alice, http.MethodGet, "/api/me/notifications/unread-count", "", &count)
if count.UnreadCount != 1 {
t.Errorf("unread = %d, want 1 (the one that arrived later)", count.UnreadCount)
}
if code := callAs(t, r, alice, http.MethodPost, "/api/me/notifications/read-all", `{"up_to":"yesterday"}`, nil); code != http.StatusBadRequest {
t.Errorf("malformed up_to = %d, want 400", code)
}
}
func strPtr(s string) *string { return &s }
func setSMTP(t *testing.T, pool *pgxpool.Pool, enabled bool) {
t.Helper()
ctx := context.Background()
q := dbq.New(pool)
prev, err := q.GetSMTPConfig(ctx)
if err != nil {
t.Fatalf("read smtp: %v", err)
}
t.Cleanup(func() {
_ = q.UpdateSMTPConfig(context.Background(), dbq.UpdateSMTPConfigParams{
Enabled: prev.Enabled, Host: prev.Host, Port: prev.Port, Username: prev.Username,
Password: prev.Password, FromAddress: prev.FromAddress, FromName: prev.FromName, UseTls: prev.UseTls,
})
})
if err := q.UpdateSMTPConfig(ctx, dbq.UpdateSMTPConfigParams{
Enabled: enabled, Host: "smtp.example.com", Port: 587, FromAddress: "minstrel@example.com", FromName: "Minstrel", UseTls: true,
}); err != nil {
t.Fatalf("set smtp: %v", err)
}
}
func TestMyNotificationSettings_DefaultsRoundTripAndAdminKinds(t *testing.T) {
h, pool := testHandlers(t)
setSMTP(t, pool, true)
user := seedUser(t, pool, "notif-settings", "pw", false)
admin := seedUser(t, pool, "notif-settings-admin", "pw", true)
r := notificationsRouter(h)
var s notificationSettingsResp
if code := callAs(t, r, user, http.MethodGet, "/api/me/notification-settings", "", &s); code != http.StatusOK {
t.Fatalf("get = %d", code)
}
for _, k := range s.Kinds {
if k.AdminOnly {
t.Errorf("non-admin offered admin kind %s", k.Kind)
}
}
if s.EmailAvailable || s.EmailUnavailableReason != "no_address" {
t.Errorf("no address on file: email_available=%v reason=%q", s.EmailAvailable, s.EmailUnavailableReason)
}
// A partial change touches only what it names.
body := `{"kinds":[{"kind":"new_music","phone":true}]}`
if code := callAs(t, r, user, http.MethodPut, "/api/me/notification-settings", body, &s); code != http.StatusOK {
t.Fatalf("put = %d", code)
}
for _, k := range s.Kinds {
if k.Kind == notifications.KindNewMusic && (!k.Phone || !k.Inbox || !k.Email) {
t.Errorf("after PUT new_music = %+v, want phone on and inbox, email left on", k)
}
}
// A non-admin can't set an admin kind; nothing in the batch is applied.
bad := `{"kinds":[{"kind":"new_music","inbox":false},{"kind":"tracks_missing","inbox":false}]}`
if code := callAs(t, r, user, http.MethodPut, "/api/me/notification-settings", bad, nil); code != http.StatusBadRequest {
t.Errorf("non-admin setting an admin kind = %d, want 400", code)
}
callAs(t, r, user, http.MethodGet, "/api/me/notification-settings", "", &s)
for _, k := range s.Kinds {
if k.Kind == notifications.KindNewMusic && !k.Inbox {
t.Error("a refused batch was partly applied")
}
}
if code := callAs(t, r, user, http.MethodPut, "/api/me/notification-settings", `{"kinds":[{"kind":"bogus"}]}`, nil); code != http.StatusBadRequest {
t.Errorf("unknown kind = %d, want 400", code)
}
// An admin with an address and SMTP on sees the admin kinds and can email.
if _, err := pool.Exec(context.Background(), `UPDATE users SET email = 'admin@example.com' WHERE id = $1`, admin.ID); err != nil {
t.Fatal(err)
}
admin.Email = strPtr("admin@example.com")
callAs(t, r, admin, http.MethodGet, "/api/me/notification-settings", "", &s)
if !s.EmailAvailable {
t.Errorf("admin with address + SMTP: email unavailable (%q)", s.EmailUnavailableReason)
}
var adminKinds int
for _, k := range s.Kinds {
if k.AdminOnly {
adminKinds++
if k.Kind == notifications.KindTracksMissing && k.Email {
t.Error("tracks_missing should default to email off")
}
}
}
if adminKinds == 0 {
t.Error("admin was offered no admin kinds")
}
}
func TestMyNotificationSettings_EmailUnavailableWhenSMTPIsOff(t *testing.T) {
h, pool := testHandlers(t)
setSMTP(t, pool, false)
user := seedUser(t, pool, "notif-nosmtp", "pw", false)
user.Email = strPtr("someone@example.com")
var s notificationSettingsResp
callAs(t, notificationsRouter(h), user, http.MethodGet, "/api/me/notification-settings", "", &s)
if s.EmailAvailable || s.EmailUnavailableReason != "smtp_not_configured" {
t.Errorf("SMTP off: email_available=%v reason=%q", s.EmailAvailable, s.EmailUnavailableReason)
}
}