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
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>
178 lines
7.9 KiB
Go
178 lines
7.9 KiB
Go
package notifications
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
digestCfg = EmailSettings{SummaryHour: 9, BatchWindowMinutes: 60}
|
|
t0 = time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
|
)
|
|
|
|
func item(k Kind, at time.Time) pendingItem {
|
|
return pendingItem{ID: pgtype.UUID{Bytes: [16]byte{byte(at.Minute()), byte(len(k))}, Valid: true}, Kind: k, CreatedAt: at}
|
|
}
|
|
|
|
func allEmail() map[Kind]bool {
|
|
m := map[Kind]bool{}
|
|
for _, k := range Kinds() {
|
|
m[k] = true
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestPlanUser_BatchWindow(t *testing.T) {
|
|
first := item(KindQuarantineFlagged, t0)
|
|
later := item(KindTracksMissing, t0.Add(40*time.Minute))
|
|
cases := []struct {
|
|
name string
|
|
now time.Time
|
|
state groupState
|
|
items []pendingItem
|
|
wantDue bool
|
|
wantOpen time.Time
|
|
}{
|
|
{"opens at the first item and is not due inside the window", t0.Add(59 * time.Minute), groupState{}, []pendingItem{first, later}, false, t0},
|
|
{"due a window after the first item, carrying everything since", t0.Add(60 * time.Minute), groupState{}, []pendingItem{first, later}, true, t0},
|
|
{"a recorded start wins over an item that moved later", t0.Add(61 * time.Minute), groupState{BatchOpenedAt: t0}, []pendingItem{later}, true, t0},
|
|
{"a failed send waits out its retry", t0.Add(90 * time.Minute), groupState{BatchOpenedAt: t0, RetryAfter: t0.Add(95 * time.Minute)}, []pendingItem{first}, false, t0},
|
|
{"and goes once the retry has passed", t0.Add(96 * time.Minute), groupState{BatchOpenedAt: t0, RetryAfter: t0.Add(95 * time.Minute)}, []pendingItem{first}, true, t0},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
p := planUser(c.now, digestCfg, time.UTC, c.items, allEmail(), c.state, groupState{})
|
|
require.Equal(t, c.wantDue, p.BatchDue)
|
|
require.Equal(t, c.wantOpen, p.BatchOpenedAt)
|
|
require.Len(t, p.Batch, len(c.items))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanUser_QuietBatchSendsNothing(t *testing.T) {
|
|
p := planUser(t0.Add(3*time.Hour), digestCfg, time.UTC, nil, allEmail(), groupState{BatchOpenedAt: t0}, groupState{})
|
|
require.False(t, p.BatchDue)
|
|
require.True(t, p.BatchOpenedAt.IsZero(), "nothing pending leaves no batch open")
|
|
}
|
|
|
|
func TestPlanUser_SummaryHourInUserTimezone(t *testing.T) {
|
|
ny, err := time.LoadLocation("America/New_York")
|
|
require.NoError(t, err)
|
|
arrived := []pendingItem{item(KindNewMusic, time.Date(2026, 7, 1, 3, 0, 0, 0, time.UTC))}
|
|
// 09:00 in New York in July is 13:00 UTC (EDT, UTC-4).
|
|
cases := []struct {
|
|
name string
|
|
now time.Time
|
|
lastSent time.Time
|
|
want bool
|
|
}{
|
|
{"before the local hour", time.Date(2026, 7, 1, 12, 59, 0, 0, time.UTC), time.Time{}, false},
|
|
{"at the local hour", time.Date(2026, 7, 1, 13, 0, 0, 0, time.UTC), time.Time{}, true},
|
|
{"once a day: already sent after today's hour", time.Date(2026, 7, 1, 18, 0, 0, 0, time.UTC), time.Date(2026, 7, 1, 13, 1, 0, 0, time.UTC), false},
|
|
{"yesterday's summary does not hold today's", time.Date(2026, 7, 2, 13, 5, 0, 0, time.UTC), time.Date(2026, 7, 1, 13, 1, 0, 0, time.UTC), true},
|
|
// 03:30 UTC on 2 July is 23:30 on 1 July in New York: still the 1st there.
|
|
{"the local day, not the UTC day", time.Date(2026, 7, 2, 3, 30, 0, 0, time.UTC), time.Date(2026, 7, 1, 13, 1, 0, 0, time.UTC), false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
p := planUser(c.now, digestCfg, ny, arrived, allEmail(), groupState{}, groupState{LastSentAt: c.lastSent})
|
|
require.Equal(t, c.want, p.SummaryDue)
|
|
require.False(t, p.BatchDue, "new music never goes out in a batch")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanUser_SummaryAcrossDST(t *testing.T) {
|
|
ny, err := time.LoadLocation("America/New_York")
|
|
require.NoError(t, err)
|
|
arrived := []pendingItem{item(KindNewMusic, time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC))}
|
|
// Clocks go back on 1 November 2026: 09:00 the day before is 13:00 UTC
|
|
// (EDT), and 09:00 that day is 14:00 UTC (EST).
|
|
before := groupState{LastSentAt: time.Date(2026, 10, 31, 13, 0, 0, 0, time.UTC)}
|
|
p := planUser(time.Date(2026, 11, 1, 13, 30, 0, 0, time.UTC), digestCfg, ny, arrived, allEmail(), groupState{}, before)
|
|
require.False(t, p.SummaryDue, "08:30 local after the change is not yet the hour")
|
|
p = planUser(time.Date(2026, 11, 1, 14, 0, 0, 0, time.UTC), digestCfg, ny, arrived, allEmail(), groupState{}, before)
|
|
require.True(t, p.SummaryDue, "09:00 local after the change")
|
|
|
|
// Spring forward, 8 March 2026: 02:00 does not exist. A summary hour of
|
|
// 2 still goes out that day, once.
|
|
gap := EmailSettings{SummaryHour: 2, BatchWindowMinutes: 60}
|
|
spring := []pendingItem{item(KindNewMusic, time.Date(2026, 3, 8, 0, 0, 0, 0, time.UTC))}
|
|
at := time.Date(2026, 3, 8, 9, 0, 0, 0, time.UTC) // 05:00 EDT
|
|
p = planUser(at, gap, ny, spring, allEmail(), groupState{}, groupState{LastSentAt: time.Date(2026, 3, 7, 7, 0, 0, 0, time.UTC)})
|
|
require.True(t, p.SummaryDue)
|
|
p = planUser(at.Add(time.Hour), gap, ny, spring, allEmail(), groupState{}, groupState{LastSentAt: at})
|
|
require.False(t, p.SummaryDue)
|
|
}
|
|
|
|
func TestPlanUser_EmailOffAndStaleItemsAreSkippedNotSent(t *testing.T) {
|
|
off := allEmail()
|
|
off[KindTracksMissing] = false
|
|
items := []pendingItem{
|
|
item(KindTracksMissing, t0),
|
|
item(KindQuarantineFlagged, t0.Add(-8*24*time.Hour)),
|
|
}
|
|
p := planUser(t0.Add(2*time.Hour), digestCfg, time.UTC, items, off, groupState{}, groupState{})
|
|
require.Len(t, p.Skip, 2)
|
|
require.Empty(t, p.Batch)
|
|
require.False(t, p.BatchDue, "nothing left after filtering: no email")
|
|
require.False(t, p.SummaryDue)
|
|
}
|
|
|
|
func TestRetryDelay_DoublesToACap(t *testing.T) {
|
|
require.Equal(t, 5*time.Minute, retryDelay(1))
|
|
require.Equal(t, 10*time.Minute, retryDelay(2))
|
|
require.Equal(t, 40*time.Minute, retryDelay(4))
|
|
require.Equal(t, 6*time.Hour, retryDelay(20))
|
|
}
|
|
|
|
func TestRenderDigest_SummaryGroupsByArtist(t *testing.T) {
|
|
scan := func(albums ...Arrival) pendingItem {
|
|
b, _ := json.Marshal(Payload{Count: int64(len(albums)), Albums: albums}.Map())
|
|
return pendingItem{Kind: KindNewMusic, Payload: b}
|
|
}
|
|
items := []pendingItem{
|
|
scan(Arrival{Artist: "Moe Shop", Title: "WWW", ArtistID: "ar-1", AlbumID: "al-1"},
|
|
Arrival{Artist: "Boards of Canada", Title: "Geogaddi", AlbumID: "al-2"}),
|
|
scan(Arrival{Artist: "Moe Shop", Title: "Pure", ArtistID: "ar-1", AlbumID: "al-3"}),
|
|
}
|
|
e, err := renderDigest(EmailSummary, "alice", items, "https://music.example/")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "Minstrel: new music in your library", e.Subject)
|
|
|
|
moe := strings.Index(e.Text, "Moe Shop")
|
|
require.GreaterOrEqual(t, moe, 0)
|
|
require.Equal(t, moe, strings.LastIndex(e.Text, "Moe Shop\n"), "one heading per artist, across scans")
|
|
require.Less(t, moe, strings.Index(e.Text, "Boards of Canada"), "artists in the order they arrived")
|
|
require.Contains(t, e.Text, "Moe Shop\n https://music.example/artists/ar-1")
|
|
require.Contains(t, e.Text, " - WWW\n https://music.example/albums/al-1")
|
|
require.Contains(t, e.Text, " - Pure\n https://music.example/albums/al-3")
|
|
require.Contains(t, e.Text, " - Geogaddi\n https://music.example/albums/al-2")
|
|
require.Contains(t, e.Text, "https://music.example/settings/notifications")
|
|
require.Contains(t, e.HTML, `<a href="https://music.example/albums/al-1"`)
|
|
}
|
|
|
|
func TestRenderDigest_BatchListsEachItemAndEscapesHTML(t *testing.T) {
|
|
b, _ := json.Marshal(Payload{Name: "<script>x</script>", Reason: "dup"}.Map())
|
|
items := []pendingItem{
|
|
{Kind: KindQuarantineFlagged, Payload: []byte(`{"name":"WWW","actor":"Ann"}`)},
|
|
{Kind: KindQuarantineFlagged, Payload: b},
|
|
}
|
|
e, err := renderDigest(EmailBatch, "alice", items, "")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "Minstrel: 2 updates", e.Subject)
|
|
require.Contains(t, e.Text, "- Track flagged\n Ann flagged WWW.")
|
|
require.NotContains(t, e.Text, "http", "no public address: no links")
|
|
require.Contains(t, e.Text, "Settings → Notifications")
|
|
require.NotContains(t, e.HTML, "<script>")
|
|
|
|
one, err := renderDigest(EmailBatch, "alice", items[:1], "")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "Minstrel: Track flagged", one.Subject)
|
|
}
|