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