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)
}
}