test(quarantine, playsessions): share one database reset per scenario (#8299)
release / go (push) Successful in 2m10s
release / integration (push) Successful in 3m2s
release / web (push) Successful in 1m30s
release / govulncheck (push) Successful in 20s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 42s
release / android (push) Canceled after 1m21s

- quarantine: the four refusals (a report on a missing track, an unknown
  reason, resolving or deleting a missing track) are one table.
- playsessions: the four window tests are one listener's plays at 0, 15,
  45 and 76 minutes against a 30-minute window: a new session, joining,
  joining at exactly the window, and starting anew.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 15:22:38 -05:00
co-authored by Claude Opus 5.5
parent cfdbfa30e3
commit 97cb2b2dd1
2 changed files with 48 additions and 127 deletions
+28 -78
View File
@@ -26,91 +26,41 @@ func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
return u.ID
}
func TestFindOrCreate_NoPriorSessionCreatesOne(t *testing.T) {
// A play joins the listener's latest session when it starts within the
// window of that session's last play (exactly the window still counts), and
// starts a new session otherwise. One listener's plays at 0, 15, 45 and 76
// minutes walk through each case against a 30-minute window.
func TestFindOrCreate_JoinsWithinTheWindowAndStartsAfterIt(t *testing.T) {
pool := dbtest.Pool(t)
user := seedTestUser(t, pool)
q := dbq.New(pool)
ctx := context.Background()
t0 := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
play := func(after time.Duration) pgtype.UUID {
t.Helper()
id, err := FindOrCreate(ctx, q, user, t0.Add(after), "client-a", 30*time.Minute)
if err != nil || !id.Valid {
t.Fatalf("play at +%s: %v, %v", after, id, err)
}
return id
}
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
id, err := FindOrCreate(context.Background(), q, user, now, "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("FindOrCreate: %v", err)
first := play(0)
row, err := q.GetMostRecentPlaySessionForUser(ctx, user)
if err != nil || row.ID != first || !row.StartedAt.Time.Equal(t0) {
t.Fatalf("the first play's session = %+v, %v; want %v started at %v", row, err, first, t0)
}
if !id.Valid {
t.Fatalf("session id not valid")
if got := play(15 * time.Minute); got != first {
t.Errorf("15 minutes later: session %v, want the same %v", got, first)
}
row, err := q.GetMostRecentPlaySessionForUser(context.Background(), user)
if err != nil {
t.Fatalf("get session: %v", err)
row, _ = q.GetMostRecentPlaySessionForUser(ctx, user)
if !row.LastEventAt.Time.Equal(t0.Add(15*time.Minute)) || row.TrackCount != 2 {
t.Errorf("after two plays: last_event_at %v, track_count %d; want %v, 2", row.LastEventAt.Time, row.TrackCount, t0.Add(15*time.Minute))
}
if row.ID != id {
t.Errorf("returned id %v, db has %v", id, row.ID)
if got := play(45 * time.Minute); got != first {
t.Errorf("exactly the window after the last play: session %v, want the same %v", got, first)
}
if !row.StartedAt.Time.Equal(now) {
t.Errorf("started_at = %v, want %v", row.StartedAt.Time, now)
}
}
func TestFindOrCreate_WithinWindowExtendsExisting(t *testing.T) {
pool := dbtest.Pool(t)
user := seedTestUser(t, pool)
q := dbq.New(pool)
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
first, err := FindOrCreate(context.Background(), q, user, now, "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("first: %v", err)
}
second, err := FindOrCreate(context.Background(), q, user, now.Add(15*time.Minute), "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("second: %v", err)
}
if first != second {
t.Errorf("session id changed: %v -> %v", first, second)
}
row, err := q.GetMostRecentPlaySessionForUser(context.Background(), user)
if err != nil {
t.Fatalf("get session: %v", err)
}
if !row.LastEventAt.Time.Equal(now.Add(15 * time.Minute)) {
t.Errorf("last_event_at = %v", row.LastEventAt.Time)
}
if row.TrackCount != 2 {
t.Errorf("track_count = %d, want 2", row.TrackCount)
}
}
func TestFindOrCreate_BeyondWindowStartsNew(t *testing.T) {
pool := dbtest.Pool(t)
user := seedTestUser(t, pool)
q := dbq.New(pool)
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
first, err := FindOrCreate(context.Background(), q, user, now, "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("first: %v", err)
}
second, err := FindOrCreate(context.Background(), q, user, now.Add(31*time.Minute), "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("second: %v", err)
}
if first == second {
t.Errorf("expected new session, got same id %v", first)
}
}
func TestFindOrCreate_AtExactBoundaryExtendsExisting(t *testing.T) {
pool := dbtest.Pool(t)
user := seedTestUser(t, pool)
q := dbq.New(pool)
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
first, _ := FindOrCreate(context.Background(), q, user, now, "client-a", 30*time.Minute)
second, err := FindOrCreate(context.Background(), q, user, now.Add(30*time.Minute), "client-a", 30*time.Minute)
if err != nil {
t.Fatalf("second: %v", err)
}
if first != second {
t.Errorf("expected same session at exact boundary, got new id %v", second)
if got := play(76 * time.Minute); got == first {
t.Errorf("31 minutes after the last play: still session %v, want a new one", got)
}
}
+20 -49
View File
@@ -93,33 +93,6 @@ func TestFlag_UpsertOnSecondFlag(t *testing.T) {
}
}
func TestFlag_NonexistentTrackReturnsErrTrackNotFound(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
var bogus pgtype.UUID
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
bogus.Valid = true
svc := NewService(pool, "")
_, err := svc.Flag(context.Background(), user.ID, bogus, "bad_rip", "")
if !errors.Is(err, ErrTrackNotFound) {
t.Errorf("err = %v, want ErrTrackNotFound", err)
}
}
func TestFlag_BadReasonRejected(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
track, _, _ := seedTrack(t, pool, "T", "x")
svc := NewService(pool, "")
_, err := svc.Flag(context.Background(), user.ID, track.ID, "garbage", "")
if !errors.Is(err, ErrBadReason) {
t.Errorf("err = %v, want ErrBadReason", err)
}
}
func TestUnflag_DeletesRow(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
@@ -250,20 +223,6 @@ func TestResolve_NoExistingRowsStillWritesAudit(t *testing.T) {
}
}
func TestResolve_TrackNotFound(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
var bogus pgtype.UUID
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
bogus.Valid = true
svc := NewService(pool, "")
_, err := svc.Resolve(context.Background(), bogus, user.ID)
if !errors.Is(err, ErrTrackNotFound) {
t.Errorf("err = %v, want ErrTrackNotFound", err)
}
}
func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
@@ -376,16 +335,28 @@ func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
}
}
func TestDeleteFile_TrackNotFound(t *testing.T) {
// A report needs a known reason and a track that exists, and an admin's
// resolve or delete needs a track that exists; each refusal has its own
// error, which the API answers as 400 or 404.
func TestService_Refusals(t *testing.T) {
pool := dbtest.Pool(t)
ctx := context.Background()
user := seedUser(t, pool, "alice")
var bogus pgtype.UUID
bogus.Bytes = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
bogus.Valid = true
track, _, _ := seedTrack(t, pool, "T", "x")
svc := NewService(pool, "")
_, err := svc.DeleteFile(context.Background(), bogus, user.ID)
if !errors.Is(err, ErrTrackNotFound) {
t.Errorf("err = %v, want ErrTrackNotFound", err)
missing := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Valid: true}
for _, tc := range []struct {
name string
do func() error
want error
}{
{"a report on a missing track", func() error { _, err := svc.Flag(ctx, user.ID, missing, "bad_rip", ""); return err }, ErrTrackNotFound},
{"a report with an unknown reason", func() error { _, err := svc.Flag(ctx, user.ID, track.ID, "garbage", ""); return err }, ErrBadReason},
{"resolving a missing track", func() error { _, err := svc.Resolve(ctx, missing, user.ID); return err }, ErrTrackNotFound},
{"deleting a missing track's file", func() error { _, err := svc.DeleteFile(ctx, missing, user.ID); return err }, ErrTrackNotFound},
} {
if err := tc.do(); !errors.Is(err, tc.want) {
t.Errorf("%s: err = %v, want %v", tc.name, err, tc.want)
}
}
}