Files
minstrel/internal/api/admin_library_missing_db_test.go
T
bvandeusenandClaude Opus 5.5 4e1807cbba
release / web (push) Failing after 1m16s
release / go (push) Successful in 1m48s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m34s
release / Build signed APK (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
test: invented artists, albums and MusicBrainz ids in place of real ones
Test data and example comments named real artists, records and songs, and
carried real MusicBrainz ids. One mapping swaps them for invented ones
throughout (Fields of Ontario, Plastic Apes, Marlo Davies, ...), keeping
what each case tests: sort order, index letter, escaping, title keys,
artist-prefix stripping. A test username is no longer the operator's name.

The cover-art and Last.fm connection checks still query real releases;
they reach live services and need something that exists there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:21:34 -05:00

109 lines
3.9 KiB
Go

package api
import (
"bytes"
"context"
"encoding/csv"
"net/http"
"net/http/httptest"
"testing"
)
// The export names every waiting track and why it waits, with an empty cell
// for an MBID or a position the tags never carried.
func TestHandleExportMissingTracks_Integration(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)
admin := seedUser(t, pool, "export-admin", "pw", true)
artist := seedArtist(t, pool, "Fields of Ontario")
album := seedAlbum(t, pool, artist.ID, "Geomantic", 2002)
gone := seedTrack(t, pool, album.ID, artist.ID, "Music Is Maps", 0, 300_000)
removed := seedTrack(t, pool, album.ID, artist.ID, "Dusk Chorus", 2, 240_000)
seedTrack(t, pool, album.ID, artist.ID, "Julia and Cane", 3, 200_000)
ctx := context.Background()
if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now() WHERE id = $1`, gone.ID); err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now(), removed_reason = 'report', removed_by = $2,
mbid = 'rec-dawn', mbid_source = 'tag' WHERE id = $1`, removed.ID, admin.ID); err != nil {
t.Fatal(err)
}
req := withUser(httptest.NewRequest(http.MethodGet, "/api/admin/library/missing.csv", nil), admin)
w := httptest.NewRecorder()
h.handleExportMissingTracks(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
rows, err := csv.NewReader(bytes.NewReader(w.Body.Bytes())).ReadAll()
if err != nil {
t.Fatalf("parse csv: %v", err)
}
if len(rows) != 3 {
t.Fatalf("rows = %v, want a header and the two waiting tracks", rows)
}
byTitle := map[string][]string{}
for _, r := range rows[1:] {
byTitle[r[4]] = r
}
if r := byTitle["Music Is Maps"]; r == nil || r[3] != "" || r[5] != "" || r[7] != "missing" {
t.Errorf("untagged missing row = %v, want no track number, no mbid, why missing", r)
}
if r := byTitle["Dusk Chorus"]; r == nil || r[3] != "2" || r[5] != "rec-dawn" || r[7] != "report" {
t.Errorf("removed row = %v, want track 2, its mbid, why report", r)
}
}
// Forget deletes waiting tracks, all or nothing: a track that has its file
// is refused, and nothing in that request goes.
func TestHandleForgetMissingTracks_Integration(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)
admin := seedUser(t, pool, "forget-admin", "pw", true)
artist := seedArtist(t, pool, "Autoglyph")
album := seedAlbum(t, pool, artist.ID, "Ochre", 1994)
waiting := seedTrack(t, pool, album.ID, artist.ID, "Foil", 1, 300_000)
present := seedTrack(t, pool, album.ID, artist.ID, "Montreal", 2, 300_000)
ctx := context.Background()
if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now() WHERE id = $1`, waiting.ID); err != nil {
t.Fatal(err)
}
forget := func(ids ...string) int {
body := `{"track_ids":[`
for i, id := range ids {
if i > 0 {
body += ","
}
body += `"` + id + `"`
}
body += `]}`
req := withUser(httptest.NewRequest(http.MethodPost, "/api/admin/library/missing/forget", bytes.NewBufferString(body)), admin)
w := httptest.NewRecorder()
h.handleForgetMissingTracks(w, req)
return w.Code
}
exists := func(id string) bool {
var n int
if err := pool.QueryRow(ctx, `SELECT count(*) FROM tracks WHERE id = $1`, id).Scan(&n); err != nil {
t.Fatal(err)
}
return n == 1
}
if code := forget(uuidToString(waiting.ID), uuidToString(present.ID)); code != http.StatusConflict {
t.Fatalf("forgetting a track with its file: status %d, want 409", code)
}
if !exists(uuidToString(waiting.ID)) || !exists(uuidToString(present.ID)) {
t.Fatalf("a refused forget removed a track")
}
if code := forget(uuidToString(waiting.ID)); code != http.StatusOK {
t.Fatalf("forgetting the waiting track: status %d, want 200", code)
}
if exists(uuidToString(waiting.ID)) {
t.Errorf("the forgotten track is still there")
}
if !exists(uuidToString(present.ID)) {
t.Errorf("the album's other track went too")
}
}