feat(playlists): track operations + cover-collage generator

AppendTracks / RemoveTrack / Reorder run in single transactions and
update the denormalized rollups (track_count, duration_sec). Reorder
uses a +10000 offset to bump every row out of the position range
before writing the new positions, sidestepping PK conflicts during
rewrite.

GenerateCollage composes a 600x600 JPEG from the first 4 album
covers, with a slate-tinted fallback for missing cells. Synchronous,
called inline after every mutating operation. SVG-rasterized fallback
is a follow-up — slice 1 ships with a solid placeholder.
This commit is contained in:
2026-05-03 10:25:20 -04:00
parent 5c61c10b63
commit 79bab14b30
5 changed files with 634 additions and 7 deletions
@@ -3,9 +3,12 @@ package playlists_test
import (
"context"
"crypto/rand"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"sync/atomic"
"testing"
"github.com/jackc/pgx/v5/pgtype"
@@ -16,6 +19,12 @@ import (
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// seedTrackCounter ensures every seedTrack call gets a unique file_path
// even when title + artist repeat. Tracks dedupe by file_path so we
// must vary it; using t.TempDir() alone isn't enough because some
// tests seed multiple tracks within one test function.
var seedTrackCounter uint64
// newPool migrates a fresh schema against MINSTREL_TEST_DATABASE_URL
// and returns a connection pool with all data tables wiped (test
// users only — see internal/dbtest/reset.go for the why). Skips when
@@ -58,6 +67,40 @@ func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User {
return u
}
// seedTrack creates an artist + album + track triple suitable for
// playlist track-list mutation tests. Each call produces a fresh
// track row with a unique file_path; artist and album are not
// deduplicated across calls (mbid-less upsert), but that's fine for
// these tests — playlist snapshots just capture the strings.
func seedTrack(t *testing.T, pool *pgxpool.Pool, title, artist string) dbq.Track {
t.Helper()
q := dbq.New(pool)
a, err := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{
Name: artist, SortName: artist,
})
if err != nil {
t.Fatalf("seed artist: %v", err)
}
al, err := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{
Title: artist + " - Album", SortTitle: artist + " - Album",
ArtistID: a.ID,
})
if err != nil {
t.Fatalf("seed album: %v", err)
}
n := atomic.AddUint64(&seedTrackCounter, 1)
track, err := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: title, AlbumID: al.ID, ArtistID: a.ID,
DurationMs: 1000,
FilePath: filepath.Join(t.TempDir(), fmt.Sprintf("%s-%d.mp3", title, n)),
FileSize: 100, FileFormat: "mp3",
})
if err != nil {
t.Fatalf("seed track: %v", err)
}
return track
}
// randomUUID returns a fresh valid pgtype.UUID. Used in tests that
// need an id guaranteed-not-to-exist in the database.
func randomUUID() pgtype.UUID {