Files
minstrel/internal/subsonic/mixrecall_test.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

102 lines
3.5 KiB
Go

package subsonic
import (
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/playevents"
"github.com/Fabled-Sword/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)
}
}