Files
minstrel/internal/playlists/collage_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

117 lines
3.3 KiB
Go

package playlists_test
import (
"context"
"image"
_ "image/jpeg"
"io"
"log/slog"
"os"
"path/filepath"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/dbtest"
"github.com/Fabled-Sword/minstrel/internal/playlists"
)
func TestGenerateCollage_EmptyPlaylist(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
dir := t.TempDir()
svc := playlists.NewService(pool, nil, dir)
pl, err := svc.Create(context.Background(), user.ID, "Empty", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
relPath, err := playlists.GenerateCollage(context.Background(), pool, pl.ID, dir)
if err != nil {
t.Fatalf("GenerateCollage: %v", err)
}
if relPath != "" {
t.Errorf("relPath = %q, want \"\" for empty playlist", relPath)
}
}
func TestGenerateCollage_OneTrack(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
tk := seedTrack(t, pool, "Prismatic", "FoO")
dir := t.TempDir()
svc := playlists.NewService(pool, nil, dir)
pl, err := svc.Create(context.Background(), user.ID, "One", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
if err := svc.AppendTracks(context.Background(), user.ID, pl.ID, []pgtype.UUID{tk.ID}); err != nil {
t.Fatalf("AppendTracks: %v", err)
}
got, err := svc.Get(context.Background(), user.ID, pl.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got.CoverPath == nil || *got.CoverPath == "" {
t.Fatalf("CoverPath empty after append")
}
full := filepath.Join(dir, *got.CoverPath)
f, err := os.Open(full)
if err != nil {
t.Fatalf("open collage: %v", err)
}
defer func() { _ = f.Close() }()
img, format, err := image.Decode(f)
if err != nil {
t.Fatalf("decode collage: %v", err)
}
if format != "jpeg" {
t.Errorf("format = %q, want jpeg", format)
}
if img.Bounds().Dx() != 600 || img.Bounds().Dy() != 600 {
t.Errorf("dimensions = %dx%d, want 600x600", img.Bounds().Dx(), img.Bounds().Dy())
}
}
// The nightly redraw draws a collage for a playlist that has none, as after
// a restore (which leaves cover_path out and carries no collage files).
func TestRedrawUserPlaylistCollages_DrawsAMissingCollage(t *testing.T) {
pool := dbtest.Pool(t)
ctx := context.Background()
user := seedUser(t, pool, "alice")
tk := seedTrack(t, pool, "Prismatic", "FoO")
dir := t.TempDir()
svc := playlists.NewService(pool, nil, dir)
pl, err := svc.Create(ctx, user.ID, "Restored", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
if err := svc.AppendTracks(ctx, user.ID, pl.ID, []pgtype.UUID{tk.ID}); err != nil {
t.Fatalf("AppendTracks: %v", err)
}
// As a restore leaves it: no cover_path, and no file.
if _, err := pool.Exec(ctx, `UPDATE playlists SET cover_path = NULL WHERE id = $1`, pl.ID); err != nil {
t.Fatalf("clear cover_path: %v", err)
}
if err := os.RemoveAll(filepath.Join(dir, "playlist_covers")); err != nil {
t.Fatalf("remove covers: %v", err)
}
playlists.RedrawUserPlaylistCollages(ctx, pool, slog.New(slog.NewTextHandler(io.Discard, nil)), user.ID, dir)
got, err := svc.Get(ctx, user.ID, pl.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got.CoverPath == nil || *got.CoverPath == "" {
t.Fatal("no collage after the redraw")
}
if _, err := os.Stat(filepath.Join(dir, *got.CoverPath)); err != nil {
t.Fatalf("collage file: %v", err)
}
}