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>
225 lines
7.8 KiB
Go
225 lines
7.8 KiB
Go
package notifications_test
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/jackc/pgx/v5/pgtype"
|
||
"github.com/jackc/pgx/v5/pgxpool"
|
||
"github.com/stretchr/testify/require"
|
||
|
||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||
"git.fabledsword.com/bvandeusen/minstrel/internal/mailer"
|
||
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
||
)
|
||
|
||
// emailUser is a user with an address, so the digest can reach them.
|
||
func emailUser(t *testing.T, pool *pgxpool.Pool, name string, admin bool) pgtype.UUID {
|
||
t.Helper()
|
||
id := mkUser(t, dbq.New(pool), name, admin)
|
||
_, err := pool.Exec(context.Background(), "UPDATE users SET email = $2 WHERE id = $1", id, name+"@example.com")
|
||
require.NoError(t, err)
|
||
return id
|
||
}
|
||
|
||
func digestWith(pool *pgxpool.Pool) (*notifications.Digest, *mailer.FakeSender) {
|
||
fake := &mailer.FakeSender{}
|
||
return notifications.NewDigest(pool, fake, nil), fake
|
||
}
|
||
|
||
func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
alice := emailUser(t, pool, "alice", true)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice),
|
||
notifications.Payload{Name: "Moe Shop – WWW"}.Map()))
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice),
|
||
notifications.Payload{Name: "Tycho – Awake", Reason: "already have it"}.Map()))
|
||
|
||
d.Tick(ctx, start.Add(30*time.Minute))
|
||
require.Empty(t, fake.Sent, "inside the window nothing goes")
|
||
|
||
d.Tick(ctx, start.Add(61*time.Minute))
|
||
require.Len(t, fake.Sent, 1, "both items, one email")
|
||
require.Equal(t, "alice@example.com", fake.Sent[0].To)
|
||
require.Equal(t, "Minstrel: 2 updates", fake.Sent[0].Subject)
|
||
require.Contains(t, fake.Sent[0].TextBody, "Someone flagged Moe Shop – WWW.")
|
||
require.Contains(t, fake.Sent[0].TextBody, "Someone flagged Tycho – Awake: already have it")
|
||
|
||
d.Tick(ctx, start.Add(3*time.Hour))
|
||
require.Len(t, fake.Sent, 1, "never sent twice")
|
||
}
|
||
|
||
func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
q := dbq.New(pool)
|
||
alice := emailUser(t, pool, "alice", true)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
|
||
_, err := q.MarkAllNotificationsRead(ctx, dbq.MarkAllNotificationsReadParams{UserID: alice})
|
||
require.NoError(t, err)
|
||
|
||
d.Tick(ctx, start.Add(2*time.Hour))
|
||
require.Empty(t, fake.Sent, "everything was read: nothing to send")
|
||
}
|
||
|
||
func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
q := dbq.New(pool)
|
||
alice := emailUser(t, pool, "alice", true)
|
||
off := false
|
||
_, err := notifications.SaveSettings(ctx, q, alice, true, []notifications.SettingChange{
|
||
{Kind: notifications.KindQuarantineFlagged, Email: &off},
|
||
})
|
||
require.NoError(t, err)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
|
||
on := true
|
||
_, err = notifications.SaveSettings(ctx, q, alice, true, []notifications.SettingChange{
|
||
{Kind: notifications.KindQuarantineFlagged, Email: &on},
|
||
})
|
||
require.NoError(t, err)
|
||
|
||
d.Tick(ctx, start.Add(2*time.Hour))
|
||
require.Empty(t, fake.Sent, "turning email on later does not send what came before")
|
||
}
|
||
|
||
func TestDigest_NoAddressNoEmail(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
bob := mkUser(t, dbq.New(pool), "bob", true)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(bob), nil))
|
||
d.Tick(ctx, time.Now().Add(2*time.Hour))
|
||
require.Empty(t, fake.Sent)
|
||
}
|
||
|
||
func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
q := dbq.New(pool)
|
||
root := emailUser(t, pool, "root", true)
|
||
on := true
|
||
_, err := notifications.SaveSettings(ctx, q, root, true, []notifications.SettingChange{
|
||
{Kind: notifications.KindTracksMissing, Email: &on},
|
||
})
|
||
require.NoError(t, err)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
for _, c := range []int64{3, 2} {
|
||
require.NoError(t, n.Notify(ctx, notifications.KindTracksMissing,
|
||
notifications.ToAdmins(pgtype.UUID{}), notifications.Payload{Count: c}.Map()))
|
||
}
|
||
d.Tick(ctx, start.Add(2*time.Hour))
|
||
require.Len(t, fake.Sent, 1)
|
||
require.Equal(t, "Minstrel: 5 tracks went missing", fake.Sent[0].Subject)
|
||
}
|
||
|
||
func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
alice := emailUser(t, pool, "alice", true)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
|
||
|
||
fake.FailNext = errors.New("smtp: 421 try later")
|
||
d.Tick(ctx, start.Add(61*time.Minute))
|
||
require.Empty(t, fake.Sent)
|
||
|
||
d.Tick(ctx, start.Add(63*time.Minute))
|
||
require.Empty(t, fake.Sent, "waits out the first retry gap")
|
||
|
||
d.Tick(ctx, start.Add(67*time.Minute))
|
||
require.Len(t, fake.Sent, 1, "then goes")
|
||
|
||
d.Tick(ctx, start.Add(80*time.Minute))
|
||
require.Len(t, fake.Sent, 1, "and is not sent again")
|
||
}
|
||
|
||
func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
alice := emailUser(t, pool, "alice", true) // timezone defaults to UTC
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
|
||
now := time.Now().UTC()
|
||
slot := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.UTC)
|
||
if !slot.After(now) {
|
||
slot = slot.AddDate(0, 0, 1)
|
||
}
|
||
|
||
for _, a := range []notifications.Arrival{
|
||
{Artist: "Moe Shop", Title: "WWW"},
|
||
{Artist: "Moe Shop", Title: "Pure"},
|
||
} {
|
||
p := notifications.Payload{Count: 1, Albums: []notifications.Arrival{a}}
|
||
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, notifications.ToUser(alice), p.Map()))
|
||
}
|
||
|
||
d.Tick(ctx, slot.Add(-time.Minute))
|
||
require.Empty(t, fake.Sent, "new music never goes out in an hourly batch")
|
||
|
||
d.Tick(ctx, slot)
|
||
require.Len(t, fake.Sent, 1)
|
||
require.Equal(t, "Minstrel: new music in your library", fake.Sent[0].Subject)
|
||
require.Contains(t, fake.Sent[0].TextBody, "Moe Shop\n - WWW")
|
||
require.Contains(t, fake.Sent[0].TextBody, " - Pure")
|
||
|
||
d.Tick(ctx, slot.Add(5*time.Hour))
|
||
require.Len(t, fake.Sent, 1, "one summary a day")
|
||
}
|
||
|
||
func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
q := dbq.New(pool)
|
||
|
||
got, err := notifications.LoadEmailSettings(ctx, q)
|
||
require.NoError(t, err)
|
||
require.Equal(t, notifications.DefaultEmailSettings, got)
|
||
|
||
saved, err := notifications.SaveEmailSettings(ctx, q, notifications.EmailSettings{SummaryHour: 7, BatchWindowMinutes: 30})
|
||
require.NoError(t, err)
|
||
require.Equal(t, notifications.EmailSettings{SummaryHour: 7, BatchWindowMinutes: 30}, saved)
|
||
got, err = notifications.LoadEmailSettings(ctx, q)
|
||
require.NoError(t, err)
|
||
require.Equal(t, saved, got)
|
||
}
|
||
|
||
func TestDigest_UsesTheConfiguredBatchWindow(t *testing.T) {
|
||
pool := testPool(t)
|
||
ctx := context.Background()
|
||
alice := emailUser(t, pool, "alice", true)
|
||
_, err := notifications.SaveEmailSettings(ctx, dbq.New(pool), notifications.EmailSettings{SummaryHour: 9, BatchWindowMinutes: 15})
|
||
require.NoError(t, err)
|
||
n := notifications.New(pool, nil, nil)
|
||
d, fake := digestWith(pool)
|
||
start := time.Now()
|
||
|
||
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
|
||
d.Tick(ctx, start.Add(16*time.Minute))
|
||
require.Len(t, fake.Sent, 1)
|
||
}
|