package playevents import ( "context" "testing" "time" "github.com/jackc/pgx/v5/pgtype" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" ) // userPlays is every play_events row of the fixture user, oldest first. func userPlays(t *testing.T, f fixture) []dbq.PlayEvent { t.Helper() rows, err := f.pool.Query(context.Background(), `SELECT id FROM play_events WHERE user_id = $1 ORDER BY started_at`, f.user) if err != nil { t.Fatalf("list plays: %v", err) } var ids []pgtype.UUID for rows.Next() { var id pgtype.UUID if err := rows.Scan(&id); err != nil { t.Fatalf("scan: %v", err) } ids = append(ids, id) } rows.Close() out := make([]dbq.PlayEvent, 0, len(ids)) for _, id := range ids { ev, err := f.q.GetPlayEventByID(context.Background(), id) if err != nil { t.Fatalf("get play: %v", err) } out = append(out, ev) } return out } // A Subsonic app reports "now playing" when a song starts and "played" when // it ends. The pair is one listen, a full one, whether the report's time is // the song's start (as several apps send it) or its end (or left out, so the // server's clock, which is the end). func TestSyntheticCompletedPlay_CompletesTheNowPlayingRow(t *testing.T) { for _, tc := range []struct { name string reportAt time.Duration // after the song started }{ {"report carries the start time", 0}, {"report carries the end time", 200 * time.Second}, } { t.Run(tc.name, func(t *testing.T) { f := newFixture(t, 200_000) ctx := context.Background() start := time.Now().UTC().Add(-10 * time.Minute).Truncate(time.Millisecond) if _, err := f.w.RecordPlayStarted(ctx, f.user, f.track, "dsub", start); err != nil { t.Fatalf("now playing: %v", err) } if err := f.w.RecordSyntheticCompletedPlay(ctx, f.user, f.track, "dsub", "", start.Add(tc.reportAt)); err != nil { t.Fatalf("played: %v", err) } plays := userPlays(t, f) if len(plays) != 1 { t.Fatalf("got %d play rows for one listen, want 1", len(plays)) } p := plays[0] if p.WasSkipped { t.Error("a song played to the end is recorded as a skip") } if p.CompletionRatio == nil || *p.CompletionRatio != 1 { t.Errorf("completion ratio = %v, want 1", p.CompletionRatio) } if !p.StartedAt.Time.Equal(start) { t.Errorf("started_at = %v, want the now-playing time %v", p.StartedAt.Time, start) } if want := start.Add(200 * time.Second); !p.EndedAt.Valid || !p.EndedAt.Time.Equal(want) { t.Errorf("ended_at = %v, want %v", p.EndedAt.Time, want) } }) } } // An app that reports a song as played after the next one has started must // not cut the next one short: it stays open, still playing. func TestSyntheticCompletedPlay_LeavesALaterSongPlaying(t *testing.T) { f := newFixture(t, 200_000) ctx := context.Background() track, err := f.q.GetTrackByID(ctx, f.track) if err != nil { t.Fatalf("track: %v", err) } next, err := f.q.UpsertTrack(ctx, dbq.UpsertTrackParams{ Title: "Next", AlbumID: track.AlbumID, ArtistID: track.ArtistID, FilePath: "/tmp/track-fixture-next.flac", DurationMs: 180_000, }) if err != nil { t.Fatalf("next track: %v", err) } songStart := time.Now().UTC().Add(-10 * time.Minute) nextStart := songStart.Add(200 * time.Second) if _, err := f.w.RecordPlayStarted(ctx, f.user, next.ID, "dsub", nextStart); err != nil { t.Fatalf("now playing next: %v", err) } if err := f.w.RecordSyntheticCompletedPlay(ctx, f.user, f.track, "dsub", "", songStart); err != nil { t.Fatalf("played: %v", err) } open, err := f.q.GetOpenPlayEventForUser(ctx, f.user) if err != nil { t.Fatalf("the next song is no longer playing: %v", err) } if open.TrackID != next.ID { t.Errorf("open play is %v, want the next song", open.TrackID) } if n := len(userPlays(t, f)); n != 2 { t.Errorf("got %d play rows, want 2 (the reported song and the one playing)", n) } } // A play reported as coming from a daily mix is credited to it: the row // carries the mix as its source and the mix's rotation counts the song. func TestSyntheticCompletedPlay_CreditsTheMix(t *testing.T) { f := newFixture(t, 200_000) seedSystemSnapshot(t, f, "for_you", "fresh") ctx := context.Background() if err := f.w.RecordSyntheticCompletedPlay(ctx, f.user, f.track, "dsub", "for_you", time.Now().UTC()); err != nil { t.Fatalf("played: %v", err) } plays := userPlays(t, f) if len(plays) != 1 || plays[0].Source == nil || *plays[0].Source != "for_you" { t.Fatalf("plays = %+v, want one play from for_you", plays) } if plays[0].PickKind == nil || *plays[0].PickKind != "fresh" { t.Errorf("pick_kind = %v, want fresh", plays[0].PickKind) } st, err := f.q.GetRotationState(ctx, dbq.GetRotationStateParams{UserID: f.user, PlaylistKind: "for_you"}) if err != nil || len(st.PlayedTrackIds) != 1 { t.Errorf("for_you rotation = %v (%v), want the song counted", st.PlayedTrackIds, err) } }