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, "Boards of Canada") album := seedAlbum(t, pool, artist.ID, "Geogaddi", 2002) gone := seedTrack(t, pool, album.ID, artist.ID, "Music Is Math", 0, 300_000) removed := seedTrack(t, pool, album.ID, artist.ID, "Dawn Chorus", 2, 240_000) seedTrack(t, pool, album.ID, artist.ID, "Julie and Candy", 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 Math"]; 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["Dawn 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, "Autechre") album := seedAlbum(t, pool, artist.ID, "Amber", 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") } }