Files
minstrel/internal/subsonic/playqueue_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

145 lines
5.1 KiB
Go

package subsonic
import (
"context"
"slices"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
// play records plays of a track by a listener, skipped or played through.
func (f picksFixture) play(t *testing.T, user dbq.User, track string, times int, skipped bool) {
t.Helper()
var session pgtype.UUID
if err := f.pool.QueryRow(context.Background(), `INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id)
VALUES ($1, now() - interval '2 days', now() - interval '2 days', 'top-songs-test') RETURNING id`,
user.ID).Scan(&session); err != nil {
t.Fatalf("play session: %v", err)
}
for range times {
f.exec(t, `INSERT INTO play_events (user_id, track_id, session_id, started_at, was_skipped)
VALUES ($1, $2, $3, now() - interval '2 days', $4)`, user.ID, f.tracks[track].ID, session, skipped)
}
}
func (f picksFixture) topSongs(t *testing.T, query string) ([]string, int) {
t.Helper()
var got struct {
TopSongs struct {
Songs *[]SongRef `json:"song"`
} `json:"topSongs"`
}
if code := call(t, f.browse().getTopSongs, f.user, query, &got); code != 0 {
return nil, code
}
if got.TopSongs.Songs == nil {
t.Fatalf("getTopSongs %s: song is null or absent, want a list", query)
}
return titles(*got.TopSongs.Songs), 0
}
// An artist's top songs are the ones played through most on this server, by
// anyone, then the most liked, then the rest in album order. Skips don't
// count, and the listener's quarantined song and the missing file are left
// out.
func TestGetTopSongs_RanksByPlaysThenLikesThenAlbumOrder(t *testing.T) {
f := seedPicks(t)
// The fixture's listener played Flint once.
f.play(t, f.other, "Harbour", 2, false)
f.play(t, f.other, "Inlet", 3, true)
f.exec(t, `INSERT INTO general_likes (user_id, track_id) VALUES ($1, $2)`, f.other.ID, f.tracks["Gale"].ID)
cases := []struct {
query string
want []string
}{
{"artist=Tin+Orchard", []string{"Harbour", "Flint", "Gale", "Dusk", "Inlet"}},
{"artist=tin+orchard&count=2", []string{"Harbour", "Flint"}},
{"artist=Nobody+Here", []string{}},
}
for _, c := range cases {
got, code := f.topSongs(t, c.query)
if code != 0 {
t.Fatalf("%s: error %d", c.query, code)
}
if !slices.Equal(got, c.want) {
t.Errorf("%s = %v, want %v", c.query, got, c.want)
}
}
if _, code := f.topSongs(t, ""); code != ErrMissingParameter {
t.Errorf("no artist: code %d, want %d", code, ErrMissingParameter)
}
}
func (f picksFixture) playQueue(t *testing.T, user dbq.User) *PlayQueue {
t.Helper()
var got struct {
PlayQueue *PlayQueue `json:"playQueue"`
}
if code := call(t, f.browse().getPlayQueue, user, "", &got); code != 0 {
t.Fatalf("getPlayQueue: error %d", code)
}
return got.PlayQueue
}
func (f picksFixture) saveQueue(t *testing.T, query string) int {
t.Helper()
return call(t, f.browse().savePlayQueue, f.user, query, nil)
}
// A saved queue comes back to the same listener, in order with repeats, with
// the current song and position; nobody else sees it; saving no songs clears
// it.
func TestPlayQueue_SavedByOneAppIsWhatTheNextOneGets(t *testing.T) {
f := seedPicks(t)
id := func(title string) string { return uuidToID(f.tracks[title].ID) }
if q := f.playQueue(t, f.user); q != nil {
t.Fatalf("before any save: queue %+v, want none", q)
}
if code := f.saveQueue(t, "id="+id("Gale")+"&id="+id("Harbour")+"&id="+id("Gale")+
"&current="+id("Harbour")+"&position=61000&c=DSub"); code != 0 {
t.Fatalf("savePlayQueue: error %d", code)
}
q := f.playQueue(t, f.user)
if q == nil {
t.Fatal("after a save: no queue")
}
if got, want := titles(q.Entries), []string{"Gale", "Harbour", "Gale"}; !slices.Equal(got, want) {
t.Errorf("entries = %v, want %v", got, want)
}
if q.Current != id("Harbour") || q.Position != 61000 {
t.Errorf("current %q at %d, want Harbour (%s) at 61000", q.Current, q.Position, id("Harbour"))
}
if q.ChangedBy != "DSub" || q.Username != f.user.Username || q.Changed == "" {
t.Errorf("changed by %q, user %q, changed %q; want DSub, %q and a time", q.ChangedBy, q.Username, q.Changed, f.user.Username)
}
if other := f.playQueue(t, f.other); other != nil {
t.Errorf("another listener sees queue %+v, want none", other)
}
// A song that is no longer in the library drops out, and a current song
// that went with it leaves the queue with none.
gone := "00000000-0000-4000-8000-000000000001"
if code := f.saveQueue(t, "id="+id("Gale")+"&id="+gone+"&current="+gone+"&position=5000"); code != 0 {
t.Fatalf("savePlayQueue: error %d", code)
}
q = f.playQueue(t, f.user)
if got := titles(q.Entries); !slices.Equal(got, []string{"Gale"}) || q.Current != "" || q.Position != 0 {
t.Errorf("queue = %v current %q at %d, want [Gale] with no current song", got, q.Current, q.Position)
}
if code := f.saveQueue(t, "id=not-a-song"); code != ErrDataNotFound {
t.Errorf("an id that is not a song: code %d, want %d", code, ErrDataNotFound)
}
if code := f.saveQueue(t, ""); code != 0 {
t.Fatalf("savePlayQueue with no songs: error %d", code)
}
if q := f.playQueue(t, f.user); q != nil {
t.Errorf("after saving no songs: queue %+v, want none", q)
}
}