Files
minstrel/internal/playevents/writer_test.go
T
bvandeusenandClaude Opus 5.5 cfdbfa30e3
release / web (push) Successful in 1m41s
release / go (push) Successful in 2m4s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 3m5s
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 / android (push) Canceled after 4m35s
release / Build signed APK (push) Canceled after 3m53s
test(backup, scrobble, playevents, recsettings, apierror, auth, providers): merge repeated cases (#8299)
- backup: schema coverage's three tests and the three AWS signing
  examples, one table each.
- scrobble: Qualifies' six boundary tests; the ListenBrainz client's
  refusal mapping, twelve tests of the same rule across three calls, one
  table over call x status; the two similarity lookups' success and URL
  tests, one table.
- playevents: the skip rule's three plays share one fixture. Dropped
  TestRecordPlayEnded_SubThreshold_NoEnqueue: the writer only calls
  scrobble.MaybeEnqueue, whose own test and Qualifies' table hold that
  case; the qualifying-play test still proves the writer calls it.
- recsettings: the three tag-scale tests share one database reset.
- apierror: Error() and From, one test each; auth: RequireAdmin's three.
- providers: GetJSON's not-found and transient answers, one table. Its
  two 401 tests expected the same answer with and without
  TreatAuthAsTransient, which changes only the error's text; the table
  says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 15:16:20 -05:00

566 lines
19 KiB
Go

package playevents
import (
"context"
"encoding/json"
"io"
"log/slog"
"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"
)
type fixture struct {
pool *pgxpool.Pool
q *dbq.Queries
w *Writer
user pgtype.UUID
track pgtype.UUID
}
func newFixture(t *testing.T, durationMs int32) fixture {
t.Helper()
pool := dbtest.Pool(t)
q := dbq.New(pool)
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
})
if err != nil {
t.Fatalf("user: %v", err)
}
a, err := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "Artist", SortName: "Artist"})
if err != nil {
t.Fatalf("artist: %v", err)
}
al, err := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "Album", SortTitle: "Album", ArtistID: a.ID})
if err != nil {
t.Fatalf("album: %v", err)
}
tr, err := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "Track", AlbumID: al.ID, ArtistID: a.ID,
FilePath: "/tmp/track-fixture.flac",
DurationMs: durationMs,
})
if err != nil {
t.Fatalf("track: %v", err)
}
return fixture{
pool: pool,
q: q,
w: NewWriter(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), 30*time.Minute, 0.5, 30000),
user: u.ID,
track: tr.ID,
}
}
func TestRecordPlayStarted_InsertsRow(t *testing.T) {
f := newFixture(t, 200_000)
now := time.Now().UTC()
res, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", now)
if err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
if !res.PlayEventID.Valid {
t.Fatalf("play_event_id invalid")
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.EndedAt.Valid {
t.Errorf("ended_at should be NULL right after start")
}
if got.WasSkipped {
t.Errorf("was_skipped should default false")
}
}
func TestRecordPlayStarted_AutoClosesPriorOpenRow(t *testing.T) {
f := newFixture(t, 200_000)
t1 := time.Now().UTC()
first, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t1)
if err != nil {
t.Fatalf("first: %v", err)
}
t2 := t1.Add(45 * time.Second)
_, err = f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t2)
if err != nil {
t.Fatalf("second: %v", err)
}
prior, err := f.q.GetPlayEventByID(context.Background(), first.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if !prior.EndedAt.Valid {
t.Errorf("prior should be closed")
}
// 45s elapsed on a 200s track clears the 30s duration threshold, so the
// skip rule's AND fails -> NOT a skip. An orphan that played long enough
// to count must still land in history rather than being force-hidden.
if prior.WasSkipped {
t.Errorf("auto-close at 45s should apply the skip rule -> was_skipped=false")
}
if prior.DurationPlayedMs == nil || *prior.DurationPlayedMs != 45_000 {
t.Errorf("duration_played_ms = %v, want 45000", prior.DurationPlayedMs)
}
}
func TestRecordPlayStarted_AutoCloseShortPlayIsSkip(t *testing.T) {
f := newFixture(t, 200_000)
t1 := time.Now().UTC()
first, _ := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t1)
// 5s elapsed: ratio 0.025 < 0.5 AND 5000 < 30000 -> both fail -> a skip.
t2 := t1.Add(5 * time.Second)
if _, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t2); err != nil {
t.Fatalf("second: %v", err)
}
prior, _ := f.q.GetPlayEventByID(context.Background(), first.PlayEventID)
if !prior.WasSkipped {
t.Errorf("auto-close at 5s should be was_skipped=true")
}
}
func TestRecordPlayStarted_AutoCloseCapsAtTrackDuration(t *testing.T) {
f := newFixture(t, 60_000) // 60s track
t1 := time.Now().UTC()
first, _ := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t1)
// Start a second play 5 minutes later — elapsed > track duration.
t2 := t1.Add(5 * time.Minute)
_, _ = f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t2)
prior, _ := f.q.GetPlayEventByID(context.Background(), first.PlayEventID)
if prior.DurationPlayedMs == nil || *prior.DurationPlayedMs != 60_000 {
t.Errorf("duration_played_ms = %v, want capped at 60000", prior.DurationPlayedMs)
}
}
// A play that ends is a skip only when it played under half the track AND
// under 30 seconds (the writer's thresholds here, 0.5 and 30000 ms), and
// only a skip writes a skip_events row. Either condition alone is a play.
func TestRecordPlayEnded_AppliesTheSkipRule(t *testing.T) {
f := newFixture(t, 200_000) // a 200 s track
ctx := context.Background()
t0 := time.Now().UTC().Add(-time.Hour)
for i, tc := range []struct {
name string
playedMs int32
want bool
}{
{"10 s, 5%: both under", 10_000, true},
{"30 s, 15%: the duration reaches 30 s", 30_000, false},
{"100 s, 50%: the completion reaches half", 100_000, false},
} {
start := t0.Add(time.Duration(i) * 10 * time.Minute)
res, err := f.w.RecordPlayStarted(ctx, f.user, f.track, "c", start)
if err != nil {
t.Fatalf("%s: start: %v", tc.name, err)
}
if err := f.w.RecordPlayEnded(ctx, res.PlayEventID, tc.playedMs, start.Add(time.Duration(tc.playedMs)*time.Millisecond)); err != nil {
t.Fatalf("%s: end: %v", tc.name, err)
}
got, _ := f.q.GetPlayEventByID(ctx, res.PlayEventID)
if got.WasSkipped != tc.want {
t.Errorf("%s: was_skipped = %v, want %v", tc.name, got.WasSkipped, tc.want)
}
}
var skips int
if err := f.pool.QueryRow(ctx, `SELECT count(*) FROM skip_events WHERE user_id = $1`, f.user).Scan(&skips); err != nil {
t.Fatal(err)
}
if skips != 1 {
t.Errorf("skip_events rows = %d, want 1, for the one skip", skips)
}
}
// A lost-response retry of an offline play (same user/track/started_at)
// must not create a second history row: the offline replay is
// at-least-once.
func TestRecordOfflinePlay_DedupsByUserTrackStartedAt(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
at := time.Now().UTC()
if err := f.w.RecordOfflinePlay(ctx, f.user, f.track, "c", "", "", at, 120_000); err != nil {
t.Fatalf("first offline: %v", err)
}
if err := f.w.RecordOfflinePlay(ctx, f.user, f.track, "c", "", "", at, 120_000); err != nil {
t.Fatalf("replay offline: %v", err)
}
var count int
if err := f.pool.QueryRow(ctx,
"SELECT COUNT(*) FROM play_events WHERE user_id=$1 AND track_id=$2 AND started_at=$3",
f.user, f.track, pgtype.Timestamptz{Time: at, Valid: true}).Scan(&count); err != nil {
t.Fatalf("count: %v", err)
}
if count != 1 {
t.Errorf("play_events count = %d, want 1 (deduped)", count)
}
}
// Re-closing an already-ended row (the client's durable close-by-id replay)
// updates the row but must NOT insert a second skip_events row.
func TestRecordPlayEnded_IdempotentClose_NoDuplicateSkipEvent(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
t1 := time.Now().UTC()
res, _ := f.w.RecordPlayStarted(ctx, f.user, f.track, "c", t1)
t2 := t1.Add(10 * time.Second) // 5% / 10s -> skip
if err := f.w.RecordPlayEnded(ctx, res.PlayEventID, 10_000, t2); err != nil {
t.Fatalf("first end: %v", err)
}
if err := f.w.RecordPlayEnded(ctx, res.PlayEventID, 10_000, t2); err != nil {
t.Fatalf("replay end: %v", err)
}
var count int
if err := f.pool.QueryRow(ctx,
"SELECT COUNT(*) FROM skip_events WHERE user_id=$1 AND track_id=$2",
f.user, f.track).Scan(&count); err != nil {
t.Fatalf("count: %v", err)
}
if count != 1 {
t.Errorf("skip_events count = %d, want 1 (idempotent close)", count)
}
}
func TestRecordPlaySkipped_AlwaysSkipFlagAndSkipEventRow(t *testing.T) {
f := newFixture(t, 200_000)
t1 := time.Now().UTC()
res, _ := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t1)
// User-initiated skip at 90% — overrides the rule, always treated as skip.
t2 := t1.Add(180 * time.Second)
if err := f.w.RecordPlaySkipped(context.Background(), res.PlayEventID, 180_000, t2); err != nil {
t.Fatalf("RecordPlaySkipped: %v", err)
}
got, _ := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if !got.WasSkipped {
t.Errorf("RecordPlaySkipped must always set was_skipped=true")
}
}
func TestRecordSyntheticCompletedPlay_WritesBothRows(t *testing.T) {
f := newFixture(t, 200_000)
now := time.Now().UTC()
if err := f.w.RecordSyntheticCompletedPlay(context.Background(), f.user, f.track, "feishin", "", now); err != nil {
t.Fatalf("synthetic: %v", err)
}
rows, _ := f.pool.Query(context.Background(),
"SELECT id FROM play_events WHERE user_id=$1 AND ended_at IS NOT NULL", f.user)
defer rows.Close()
count := 0
for rows.Next() {
count++
}
if count != 1 {
t.Errorf("closed play_events count = %d, want 1", count)
}
}
func TestRecordPlayStarted_PersistsSessionVector_Seed(t *testing.T) {
f := newFixture(t, 200_000)
now := time.Now().UTC()
res, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", now)
if err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.SessionVectorAtPlay == nil {
t.Fatalf("session_vector_at_play is NULL")
}
var vec map[string]any
if err := json.Unmarshal(got.SessionVectorAtPlay, &vec); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if seed, _ := vec["seed"].(bool); !seed {
t.Errorf("seed = %v, want true (first play in session)", vec["seed"])
}
if artists, _ := vec["artists"].([]any); len(artists) != 0 {
t.Errorf("artists not empty: %v", artists)
}
}
func TestRecordPlayStarted_PersistsSessionVector_Populated(t *testing.T) {
f := newFixture(t, 200_000)
// Seed 4 prior plays with the same track (reusing the fixture's single
// track is fine — vector dedup means we'll see 1 artist regardless).
t0 := time.Now().UTC().Add(-1 * time.Hour)
for i := 0; i < 4; i++ {
at := t0.Add(time.Duration(i) * time.Minute)
_, _ = f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", at)
}
// 5th play — should see vector with 4 prior tracks.
at := t0.Add(10 * time.Minute)
res, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", at)
if err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
got, _ := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if got.SessionVectorAtPlay == nil {
t.Fatal("session_vector_at_play is NULL")
}
var vec map[string]any
_ = json.Unmarshal(got.SessionVectorAtPlay, &vec)
if seed, _ := vec["seed"].(bool); seed {
t.Errorf("seed = true after 4 prior tracks, want false")
}
recentIDs, _ := vec["recent_track_ids"].([]any)
if len(recentIDs) != 4 {
t.Errorf("recent_track_ids len = %d, want 4", len(recentIDs))
}
}
func TestRecordPlayStarted_VectorScopedToSession(t *testing.T) {
f := newFixture(t, 200_000)
// Play 1: at t=0.
t0 := time.Now().UTC().Add(-2 * time.Hour)
_, _ = f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t0)
// Play 2: t = t0 + 31 minutes (exceeds 30-min session timeout).
t1 := t0.Add(31 * time.Minute)
res, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", t1)
if err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
got, _ := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if got.SessionVectorAtPlay == nil {
t.Fatal("vector NULL")
}
var vec map[string]any
_ = json.Unmarshal(got.SessionVectorAtPlay, &vec)
if seed, _ := vec["seed"].(bool); !seed {
t.Errorf("seed = false in fresh session, want true")
}
recentIDs, _ := vec["recent_track_ids"].([]any)
if len(recentIDs) != 0 {
t.Errorf("recent_track_ids has %d entries from prior session, want 0", len(recentIDs))
}
}
func TestRecordPlayEnded_QualifyingPlay_EnqueuesScrobble(t *testing.T) {
f := newFixture(t, 300_000) // 300s track
ctx := context.Background()
// Enable LB for the user.
tk := "tk"
if err := f.q.SetListenBrainzToken(ctx, dbq.SetListenBrainzTokenParams{ID: f.user, ListenbrainzToken: &tk}); err != nil {
t.Fatalf("token: %v", err)
}
if err := f.q.SetListenBrainzEnabled(ctx, dbq.SetListenBrainzEnabledParams{ID: f.user, ListenbrainzEnabled: true}); err != nil {
t.Fatalf("enable: %v", err)
}
res, err := f.w.RecordPlayStarted(ctx, f.user, f.track, "test", time.Now().Add(-1*time.Minute))
if err != nil {
t.Fatalf("start: %v", err)
}
// 250s played out of 300s = ~83% — well above LB threshold.
if err := f.w.RecordPlayEnded(ctx, res.PlayEventID, 250_000, time.Now()); err != nil {
t.Fatalf("end: %v", err)
}
var n int
if err := f.pool.QueryRow(ctx,
`SELECT count(*) FROM scrobble_queue WHERE play_event_id = $1`, res.PlayEventID).Scan(&n); err != nil {
t.Fatalf("count: %v", err)
}
if n != 1 {
t.Errorf("scrobble_queue rows = %d, want 1", n)
}
}
func TestRecordPlayStartedWithSource_AppendsRotation(t *testing.T) {
f := newFixture(t, 200_000)
now := time.Now().UTC()
if _, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "for_you", "", now,
); err != nil {
t.Fatalf("RecordPlayStartedWithSource: %v", err)
}
st, err := f.q.GetRotationState(context.Background(), dbq.GetRotationStateParams{
UserID: f.user, PlaylistKind: "for_you",
})
if err != nil {
t.Fatalf("GetRotationState: %v", err)
}
if len(st.PlayedTrackIds) != 1 || st.PlayedTrackIds[0] != f.track {
t.Errorf("played_track_ids = %v, want [%v]", st.PlayedTrackIds, f.track)
}
// Re-playing the same track stays a set (no duplicate append).
if _, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "for_you", "", now.Add(time.Minute),
); err != nil {
t.Fatalf("RecordPlayStartedWithSource 2: %v", err)
}
st, err = f.q.GetRotationState(context.Background(), dbq.GetRotationStateParams{
UserID: f.user, PlaylistKind: "for_you",
})
if err != nil {
t.Fatalf("GetRotationState 2: %v", err)
}
if len(st.PlayedTrackIds) != 1 {
t.Errorf("played_track_ids should dedupe, got %v", st.PlayedTrackIds)
}
}
func TestRecordPlayStarted_NoRotationWithoutSource(t *testing.T) {
f := newFixture(t, 200_000)
now := time.Now().UTC()
if _, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", now); err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
_, err := f.q.GetRotationState(context.Background(), dbq.GetRotationStateParams{
UserID: f.user, PlaylistKind: "for_you",
})
if err == nil {
t.Errorf("expected no rotation row for a source-less play")
}
}
// seedSystemSnapshot creates a system playlist of the given variant
// containing the fixture track with the given pick_kind, mimicking
// what the builder persists.
func seedSystemSnapshot(t *testing.T, f fixture, variant, pickKind string) {
t.Helper()
pl, err := f.q.CreateSystemPlaylist(context.Background(), dbq.CreateSystemPlaylistParams{
UserID: f.user, Name: variant, SystemVariant: &variant,
})
if err != nil {
t.Fatalf("CreateSystemPlaylist: %v", err)
}
if _, err := f.q.AppendPlaylistTrack(context.Background(), dbq.AppendPlaylistTrackParams{
PlaylistID: pl.ID, TrackID: f.track, PickKind: &pickKind,
}); err != nil {
t.Fatalf("AppendPlaylistTrack: %v", err)
}
}
func TestRecordPlayStartedWithSource_StampsForYouPickKind(t *testing.T) {
f := newFixture(t, 200_000)
seedSystemSnapshot(t, f, "for_you", "fresh")
res, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "for_you", "", time.Now().UTC(),
)
if err != nil {
t.Fatalf("RecordPlayStartedWithSource: %v", err)
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.PickKind == nil || *got.PickKind != "fresh" {
t.Errorf("pick_kind = %v, want fresh (frozen from live snapshot)", got.PickKind)
}
}
func TestRecordPlayStartedWithSource_NoSnapshotMatch_PickKindNull(t *testing.T) {
// A for_you play whose track isn't in the current snapshot (rotated
// out, or no snapshot exists) stays unattributed rather than guessing.
f := newFixture(t, 200_000)
res, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "for_you", "", time.Now().UTC(),
)
if err != nil {
t.Fatalf("RecordPlayStartedWithSource: %v", err)
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.PickKind != nil {
t.Errorf("pick_kind = %q, want NULL (track not in snapshot)", *got.PickKind)
}
}
func TestRecordPlayStartedWithSource_CrossVariant_PickKindNull(t *testing.T) {
// Attribution is variant-scoped: even with a For You snapshot
// containing the track, a play from a different surface must not
// pick up For You's stamp — it reads its OWN variant's snapshot,
// which here doesn't exist.
f := newFixture(t, 200_000)
seedSystemSnapshot(t, f, "for_you", "taste")
res, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "discover", "", time.Now().UTC(),
)
if err != nil {
t.Fatalf("RecordPlayStartedWithSource: %v", err)
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.PickKind != nil {
t.Errorf("pick_kind = %q, want NULL for a cross-variant play", *got.PickKind)
}
}
func TestRecordPlayStartedWithSource_StampsDiscoverBucket(t *testing.T) {
// Discover stamps its candidate bucket; a discover play whose
// track is in the live snapshot freezes that bucket onto the play.
f := newFixture(t, 200_000)
seedSystemSnapshot(t, f, "discover", "dormant")
res, err := f.w.RecordPlayStartedWithSource(
context.Background(), f.user, f.track, "c", "discover", "", time.Now().UTC(),
)
if err != nil {
t.Fatalf("RecordPlayStartedWithSource: %v", err)
}
got, err := f.q.GetPlayEventByID(context.Background(), res.PlayEventID)
if err != nil {
t.Fatalf("get: %v", err)
}
if got.PickKind == nil || *got.PickKind != "dormant" {
t.Errorf("pick_kind = %v, want dormant (frozen from live snapshot)", got.PickKind)
}
}
func TestRecordOfflinePlay_StampsForYouPickKind(t *testing.T) {
// The offline replay path threads source the same way; a replayed
// for_you play whose track is still in the snapshot gets attributed.
f := newFixture(t, 200_000)
seedSystemSnapshot(t, f, "for_you", "taste")
at := time.Now().UTC().Add(-time.Hour)
if err := f.w.RecordOfflinePlay(
context.Background(), f.user, f.track, "c", "for_you", "", at, 180_000,
); err != nil {
t.Fatalf("RecordOfflinePlay: %v", err)
}
var pickKind *string
if err := f.pool.QueryRow(context.Background(),
`SELECT pick_kind FROM play_events WHERE user_id = $1 AND track_id = $2`,
f.user, f.track,
).Scan(&pickKind); err != nil {
t.Fatalf("select: %v", err)
}
if pickKind == nil || *pickKind != "taste" {
t.Errorf("pick_kind = %v, want taste", pickKind)
}
}
// An offline play replayed while another song is playing live leaves the
// live play open: closing it at the replay's older time would end it before
// it began, as a zero-length skip.
func TestRecordOfflinePlay_LeavesALaterLivePlayOpen(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
now := time.Now().UTC()
live, err := f.w.RecordPlayStarted(ctx, f.user, f.track, "c", now)
if err != nil {
t.Fatalf("live start: %v", err)
}
if err := f.w.RecordOfflinePlay(ctx, f.user, f.track, "c", "", "", now.Add(-2*time.Hour), 120_000); err != nil {
t.Fatalf("offline replay: %v", err)
}
var ended pgtype.Timestamptz
if err := f.pool.QueryRow(ctx, "SELECT ended_at FROM play_events WHERE id = $1", live.PlayEventID).Scan(&ended); err != nil {
t.Fatalf("read live play: %v", err)
}
if ended.Valid {
t.Fatalf("the live play was closed at %v by an older replay", ended.Time)
}
}