Files
minstrel/internal/api/library_fixtures_test.go
T
bvandeusenandClaude Opus 5.5 492eb2fb9a
release / go (push) Successful in 1m49s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m29s
release / android (push) Successful in 4m43s
release / Build signed APK (push) Successful in 5m1s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(library): Missing files says why each track waits, and can forget or export it
- The missing list carries each track's disc and track number, its
  recording and release-group MBIDs, the Collection queue its file was
  removed from, and the admin who removed it.
- POST /api/admin/library/missing/forget deletes waiting tracks for good,
  tidying away an album or artist they leave empty, and writes a
  track_forget audit row naming each. It is all or nothing: a track that
  has its file again is refused with 409 track_has_file.
- GET /api/admin/library/missing.csv exports every waiting track (artist,
  album, disc, track, title, MBIDs, why, since) to hand to whatever finds
  music.
- DELETE /api/admin/tracks/{id} is retired. No client called it; its
  service becomes tracks.Service.ForgetTracks.

Grimoire #7744 (milestone 635).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:34:01 -05:00

237 lines
7.4 KiB
Go

package api
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// truncateLibrary clears the music tables between tests. Sessions/users are
// cleared by testHandlers. CASCADE covers tracks→albums→artists FK chain.
func truncateLibrary(t *testing.T, pool *pgxpool.Pool) {
t.Helper()
if _, err := pool.Exec(context.Background(),
"TRUNCATE tracks, albums, artists, scrobble_queue RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
}
// seedArtist inserts an artist with deterministic sort_name = name.
func seedArtist(t *testing.T, pool *pgxpool.Pool, name string) dbq.Artist {
t.Helper()
a, err := dbq.New(pool).UpsertArtist(context.Background(), dbq.UpsertArtistParams{
Name: name,
SortName: name,
Mbid: nil,
})
if err != nil {
t.Fatalf("UpsertArtist(%s): %v", name, err)
}
return a
}
// seedAlbum inserts an album belonging to artistID. releaseYear=0 leaves
// release_date unset.
func seedAlbum(t *testing.T, pool *pgxpool.Pool, artistID pgtype.UUID, title string, releaseYear int) dbq.Album {
t.Helper()
var rel pgtype.Date
if releaseYear > 0 {
rel = pgtype.Date{Time: time.Date(releaseYear, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true}
}
a, err := dbq.New(pool).UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{
Title: title,
SortTitle: title,
ArtistID: artistID,
ReleaseDate: rel,
Mbid: nil,
CoverArtPath: nil,
})
if err != nil {
t.Fatalf("UpsertAlbum(%s): %v", title, err)
}
return a
}
// seedTrack inserts a track. trackNumber<=0 leaves it NULL. filePath is
// synthesized from title to stay unique across seeds.
func seedTrack(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string, trackNumber int, durationMs int32) dbq.Track {
t.Helper()
var tn *int32
if trackNumber > 0 {
v := int32(trackNumber)
tn = &v
}
tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: title,
AlbumID: albumID,
ArtistID: artistID,
TrackNumber: tn,
DiscNumber: nil,
DurationMs: durationMs,
FilePath: "/seed/" + title + ".flac",
FileSize: 1024,
FileFormat: "flac",
Bitrate: nil,
Mbid: nil,
Genre: nil,
})
if err != nil {
t.Fatalf("UpsertTrack(%s): %v", title, err)
}
return tr
}
// seedTrackWithGenre is seedTrack but lets the caller set Genre. Used by
// tests that exercise the contextual recommendation path, since session
// vectors collect tags from tracks.genre.
func seedTrackWithGenre(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string, trackNumber int, durationMs int32, genre string) dbq.Track {
t.Helper()
var tn *int32
if trackNumber > 0 {
v := int32(trackNumber)
tn = &v
}
tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: title,
AlbumID: albumID,
ArtistID: artistID,
TrackNumber: tn,
DiscNumber: nil,
DurationMs: durationMs,
FilePath: "/seed/" + title + ".flac",
FileSize: 1024,
FileFormat: "flac",
Bitrate: nil,
Mbid: nil,
Genre: &genre,
})
if err != nil {
t.Fatalf("UpsertTrack(%s): %v", title, err)
}
return tr
}
// insertOpenSessionWithVector creates a play_session with ended_at NULL and
// inserts a play_event in it whose session_vector_at_play is the given JSON.
// Used to simulate "user is mid-listen" for the radio handler's current-vector
// lookup. The play_event references a placeholder track inserted on the fly
// so the FK is valid; the placeholder is not used for radio scoring.
func insertOpenSessionWithVector(t *testing.T, pool *pgxpool.Pool, userID, anyArtistID pgtype.UUID, vectorJSON []byte) {
t.Helper()
q := dbq.New(pool)
al, err := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{
Title: "PlaceholderAlbum", SortTitle: "PlaceholderAlbum", ArtistID: anyArtistID,
})
if err != nil {
t.Fatalf("placeholder album: %v", err)
}
ph, err := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "Placeholder", AlbumID: al.ID, ArtistID: anyArtistID,
FilePath: "/seed/placeholder.flac", DurationMs: 100_000, FileSize: 1024, FileFormat: "flac",
})
if err != nil {
t.Fatalf("placeholder track: %v", err)
}
var sessionID pgtype.UUID
if err := pool.QueryRow(context.Background(),
`INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id)
VALUES ($1, now() - interval '5 minutes', now(), 'test') RETURNING id`,
userID).Scan(&sessionID); err != nil {
t.Fatalf("insert session: %v", err)
}
if _, err := pool.Exec(context.Background(),
`INSERT INTO play_events (user_id, track_id, session_id, started_at, session_vector_at_play)
VALUES ($1, $2, $3, now() - interval '1 minute', $4)`,
userID, ph.ID, sessionID, vectorJSON); err != nil {
t.Fatalf("insert play_event: %v", err)
}
}
// seedTrackWithFile creates a fresh temp directory, writes fileBody to
// <dir>/<title>.<ext>, and inserts a Track row whose file_path points at it.
// Returns the inserted Track and the directory (callers drop sidecar covers
// into this dir when a test needs them). FileFormat is ext lowercased.
func seedTrackWithFile(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string, fileBody []byte, ext string) (dbq.Track, string) {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, title+"."+ext)
if err := os.WriteFile(path, fileBody, 0o644); err != nil {
t.Fatalf("WriteFile(%s): %v", path, err)
}
one := int32(1)
tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: title,
AlbumID: albumID,
ArtistID: artistID,
TrackNumber: &one,
DiscNumber: nil,
// DurationMs: arbitrary non-zero; tests don't assert on it.
DurationMs: int32(len(fileBody)),
FilePath: path,
FileSize: int64(len(fileBody)),
FileFormat: strings.ToLower(ext),
Bitrate: nil,
Mbid: nil,
Genre: nil,
})
if err != nil {
t.Fatalf("UpsertTrack(%s): %v", title, err)
}
return tr, dir
}
// seedTrackOnDiskWithMBIDs creates a fresh artist+album+track on disk and
// returns the track row. The mbids are set on the track and album when
// non-empty.
func seedTrackOnDiskWithMBIDs(t *testing.T, h *handlers, unique, trackMbid, albumMbid string) (dbq.Track, string) {
t.Helper()
artist := seedArtist(t, h.pool, "RemoveTest Artist "+unique)
dir := t.TempDir()
path := filepath.Join(dir, "track-"+unique+".mp3")
if err := os.WriteFile(path, []byte("audio"), 0o644); err != nil {
t.Fatalf("write file: %v", err)
}
q := dbq.New(h.pool)
var albumMbidPtr *string
if albumMbid != "" {
albumMbidPtr = &albumMbid
}
album, err := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{
Title: "RemoveTest Album " + unique,
SortTitle: "RemoveTest Album " + unique,
ArtistID: artist.ID,
Mbid: albumMbidPtr,
})
if err != nil {
t.Fatalf("UpsertAlbum: %v", err)
}
one := int32(1)
var trackMbidPtr *string
if trackMbid != "" {
trackMbidPtr = &trackMbid
}
tr, err := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "RemoveTest Track " + unique,
AlbumID: album.ID,
ArtistID: artist.ID,
TrackNumber: &one,
DurationMs: 30000,
FilePath: path,
FileSize: 5,
FileFormat: "mp3",
Mbid: trackMbidPtr,
})
if err != nil {
t.Fatalf("UpsertTrack: %v", err)
}
return tr, path
}