Files
minstrel/internal/playlists/cover_albums_db_test.go
T
bvandeusenandClaude Opus 5.5 ec017838a2
release / go (push) Successful in 2m2s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 4m11s
release / android (push) Successful in 4m10s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m48s
feat(playlists): playlists list their cover albums, and system mixes say what they hold
Each playlist on the wire (list and get) carries cover_album_ids: up to
six distinct albums with art, in the order they first appear. A shelf of
playlist tiles takes each tile's first album that no tile before it
shows, so no cover repeats along the shelf.

System mixes are stored with a one-line description of what they are
built from, written in one place so the web and Android apps show the
same words. Songs like and genre mixes have none; their names say it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 15:59:44 -05:00

112 lines
3.4 KiB
Go

package playlists_test
import (
"context"
"path/filepath"
"slices"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
)
// A playlist's cover albums are its distinct albums that have art, in the
// order they first appear, at most six: a shelf of tiles picks from them so
// no two tiles show the same cover.
func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
q := dbq.New(pool)
user := seedUser(t, pool, "covers")
svc := playlists.NewService(pool, nil, t.TempDir())
// Eight albums, one track each; album B has no art. A second track on
// album A comes third, so A must be counted once.
tracks := map[string]dbq.Track{}
for _, name := range []string{"A", "B", "C", "D", "E", "F", "G", "H"} {
tracks[name] = seedTrack(t, pool, "song "+name, "artist "+name)
if name != "B" {
if _, err := pool.Exec(ctx, `UPDATE albums SET cover_art_path = $2 WHERE id = $1`,
tracks[name].AlbumID, name+".jpg"); err != nil {
t.Fatalf("set cover %s: %v", name, err)
}
}
}
againOnA, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "song A2", AlbumID: tracks["A"].AlbumID, ArtistID: tracks["A"].ArtistID,
DurationMs: 1000, FilePath: filepath.Join(t.TempDir(), "a2.mp3"),
FileSize: 100, FileFormat: "mp3",
})
if err != nil {
t.Fatalf("seed second track on A: %v", err)
}
pl, err := svc.Create(ctx, user.ID, "Shelf test", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
order := []pgtype.UUID{tracks["A"].ID, tracks["B"].ID, againOnA.ID}
for _, name := range []string{"C", "D", "E", "F", "G", "H"} {
order = append(order, tracks[name].ID)
}
if err := svc.AppendTracks(ctx, user.ID, pl.ID, order); err != nil {
t.Fatalf("AppendTracks: %v", err)
}
var want []string
for _, name := range []string{"A", "C", "D", "E", "F", "G"} {
want = append(want, uuidString(tracks[name].AlbumID))
}
ids := func(in []pgtype.UUID) []string {
out := make([]string, 0, len(in))
for _, id := range in {
out = append(out, uuidString(id))
}
return out
}
detail, err := svc.Get(ctx, user.ID, pl.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got := ids(detail.CoverAlbumIDs); !slices.Equal(got, want) {
t.Errorf("Get cover albums = %v, want %v", got, want)
}
rows, err := svc.List(ctx, user.ID)
if err != nil {
t.Fatalf("List: %v", err)
}
i := slices.IndexFunc(rows, func(r playlists.PlaylistRow) bool { return r.ID == pl.ID })
if i < 0 {
t.Fatal("List did not return the playlist")
}
if got := ids(rows[i].CoverAlbumIDs); !slices.Equal(got, want) {
t.Errorf("List cover albums = %v, want %v", got, want)
}
}
// A playlist with no covered albums lists none, rather than failing the
// List call that carries every other playlist.
func TestCoverAlbums_EmptyPlaylistHasNone(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
user := seedUser(t, pool, "nocovers")
svc := playlists.NewService(pool, nil, t.TempDir())
pl, err := svc.Create(ctx, user.ID, "Empty", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
rows, err := svc.List(ctx, user.ID)
if err != nil {
t.Fatalf("List: %v", err)
}
for _, r := range rows {
if r.ID == pl.ID && len(r.CoverAlbumIDs) != 0 {
t.Errorf("cover albums = %v, want none", r.CoverAlbumIDs)
}
}
}