Files
minstrel/internal/playsessions/service_test.go
T
bvandeusenandClaude Opus 5.5 97cb2b2dd1
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
test(quarantine, playsessions): share one database reset per scenario (#8299)
- 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>
2026-12-02 15:22:38 -05:00

67 lines
2.2 KiB
Go

package playsessions
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
"github.com/Fabled-Sword/minstrel/internal/dbtest"
)
func seedTestUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
t.Helper()
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "tester",
PasswordHash: "x",
ApiTokenHash: "x",
IsAdmin: false,
})
if err != nil {
t.Fatalf("CreateUser: %v", err)
}
return u.ID
}
// 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
}
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 got := play(15 * time.Minute); got != first {
t.Errorf("15 minutes later: session %v, want the same %v", got, first)
}
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 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 got := play(76 * time.Minute); got == first {
t.Errorf("31 minutes after the last play: still session %v, want a new one", got)
}
}