fix(subsonic): one listen is one play, and a daily mix played in a Subsonic app is credited to it
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

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>
This commit is contained in:
2026-11-25 21:48:17 -05:00
co-authored by Claude Opus 5.5
parent 7f7e946bad
commit 333523b30e
8 changed files with 477 additions and 88 deletions
+101
View File
@@ -0,0 +1,101 @@
package subsonic
import (
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
)
// An app that has opened a daily mix is credited with it only for a while,
// so a mix opened yesterday doesn't claim today's listening.
func TestMixRecall_ForgetsAfterAWhile(t *testing.T) {
clock := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
m := newMixRecall()
m.now = func() time.Time { return clock }
user := pgtype.UUID{Bytes: [16]byte{1}, Valid: true}
m.remember(user, "play:Sub", "for_you")
if got := m.variant(user, "play:Sub"); got != "for_you" {
t.Fatalf("just opened: %q, want for_you", got)
}
if got := m.variant(user, "Amperfy"); got != "" {
t.Errorf("another app: %q, want nothing", got)
}
clock = clock.Add(mixRecallFor + time.Minute)
if got := m.variant(user, "play:Sub"); got != "" {
t.Errorf("after %v: %q, want nothing", mixRecallFor, got)
}
var none *mixRecall
none.remember(user, "play:Sub", "for_you")
if got := none.variant(user, "play:Sub"); got != "" {
t.Errorf("nil recall: %q, want nothing", got)
}
}
// A Subsonic app that opens For You and then reports songs played has those
// plays credited to For You, as the web and Android apps do, so the mix's
// metrics count them and its rotation moves on. A song that isn't in the
// mix, or a report from a different app, is not credited.
func TestScrobble_CreditsTheDailyMixTheAppOpened(t *testing.T) {
f := seedPicks(t)
ctx := context.Background()
svc := playlists.NewService(f.pool, nil, "")
mix, err := svc.Create(ctx, f.user.ID, "For you", "", false)
if err != nil {
t.Fatalf("create mix: %v", err)
}
if err := svc.AppendTracks(ctx, f.user.ID, mix.ID, []pgtype.UUID{f.tracks["Gale"].ID, f.tracks["Harbour"].ID}); err != nil {
t.Fatalf("fill mix: %v", err)
}
f.exec(t, `UPDATE playlists SET kind = 'system', system_variant = 'for_you' WHERE id = $1`, mix.ID)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
recall := newMixRecall()
picks := f.handlers()
picks.recall = recall
media := newMediaHandlers(f.pool, playevents.NewWriter(f.pool, logger, 30*time.Minute, 0.5, 30000), logger)
media.recall = recall
if code := call(t, picks.getPlaylist, f.user, "c=play:Sub&id="+uuidToID(mix.ID), nil); code != 0 {
t.Fatalf("getPlaylist: error %d", code)
}
scrobble := func(song, client string) {
t.Helper()
if code := call(t, media.handleScrobble, f.user, "submission=true&c="+client+"&id="+uuidToID(f.tracks[song].ID), nil); code != 0 {
t.Fatalf("scrobble %s: error %d", song, code)
}
}
scrobble("Gale", "play:Sub")
scrobble("Dusk", "play:Sub")
scrobble("Harbour", "Amperfy")
for song, want := range map[string]string{"Gale": "for_you", "Dusk": "", "Harbour": ""} {
var source *string
if err := f.pool.QueryRow(ctx, `SELECT source FROM play_events WHERE user_id = $1 AND track_id = $2`,
f.user.ID, f.tracks[song].ID).Scan(&source); err != nil {
t.Fatalf("play of %s: %v", song, err)
}
got := ""
if source != nil {
got = *source
}
if got != want {
t.Errorf("%s credited to %q, want %q", song, got, want)
}
}
var rotation []pgtype.UUID
if err := f.pool.QueryRow(ctx, `SELECT played_track_ids FROM system_playlist_rotation_state
WHERE user_id = $1 AND playlist_kind = 'for_you'`, f.user.ID).Scan(&rotation); err != nil {
t.Fatalf("for_you rotation: %v", err)
}
if len(rotation) != 1 || rotation[0] != f.tracks["Gale"].ID {
t.Errorf("for_you rotation = %v, want Gale only", rotation)
}
}