Files
minstrel/internal/playevents/synthetic_test.go
T
bvandeusenandClaude Opus 5.5 333523b30e
release / web (push) Successful in 1m23s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m50s
release / Build signed APK (push) Successful in 5m29s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
fix(subsonic): one listen is one play, and a daily mix played in a Subsonic app is credited to it
A Subsonic app reports "now playing" when a song starts and "played" when
it ends. The played report used to close the now-playing row as a play of
its own and write a second one, so every listen counted twice, and as a
skip when the app sent the song's start time. The report now completes the
app's own row for that song. A late report for an earlier song no longer
cuts short the song playing now.

Subsonic's scrobble doesn't say where a song was played from, so Minstrel
remembers, per user and app, the daily mix the app last opened (for six
hours). A play of a song in that mix is credited to it: the mix's metrics
count it and its rotation moves on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 21:48:17 -05:00

141 lines
4.8 KiB
Go

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