Files
minstrel/internal/reacquisition/sweeper_test.go
T
bvandeusenandClaude Opus 5.5 2c677b3184
release / govulncheck (push) Successful in 16s
release / web (push) Successful in 1m24s
release / go (push) Successful in 1m44s
release / integration (push) Successful in 5m19s
release / android (push) Successful in 5m31s
release / Build signed APK (releases and dev) (push) Successful in 5m35s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped
feat: the library's albums go on the wanted list as held, once; after that the list is the truth (M502 #5567)
A one-time job in the wanted worker lists every album in the library as a
held want, one per release group, with a record that it happened
(acquisition_settings.library_listed_at). Albums whose tags name only a
release are named on MusicBrainz first, 120 a pass; what nothing names is
counted and shown on the Held tab.

Re-acquisition now wants back only albums on the list. Taking a held album
off the list leaves its files, and it is never fetched back; albums put in
the library folder by hand afterwards are not added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:13:46 -04:00

192 lines
6.7 KiB
Go

package reacquisition
import (
"context"
"io"
"log/slog"
"os"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
"git.fabledsword.com/bvandeusen/minstrel/internal/tags"
)
func newSweepPool(t *testing.T) *pgxpool.Pool {
t.Helper()
if testing.Short() {
t.Skip("skipping integration test in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
dbtest.ResetDB(t, pool)
return pool
}
// lostAlbum seeds an album whose only track has been missing for two days,
// named by MusicBrainz release id and (when group is set) release group.
func lostAlbum(t *testing.T, pool *pgxpool.Pool, title, release, group string) pgtype.UUID {
t.Helper()
ctx := context.Background()
q := dbq.New(pool)
artistMBID := "artist-" + release
ar, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: title + " Artist", SortName: title, Mbid: &artistMBID})
if err != nil {
t.Fatal(err)
}
params := dbq.UpsertAlbumParams{Title: title, SortTitle: title, ArtistID: ar.ID, Mbid: &release}
if group != "" {
params.ReleaseGroupMbid = &group
}
al, err := q.UpsertAlbum(ctx, params)
if err != nil {
t.Fatal(err)
}
tr, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Lost", AlbumID: al.ID, ArtistID: ar.ID, DurationMs: 180000,
FilePath: "/music/" + release + "/01.flac", FileSize: 1, FileFormat: "flac",
})
if err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, "UPDATE tracks SET missing_since = now() - interval '2 days' WHERE id = $1", tr.ID); err != nil {
t.Fatal(err)
}
return al.ID
}
// heldWant puts an album on the wanted list, held since ago.
func heldWant(t *testing.T, pool *pgxpool.Pool, group string, album pgtype.UUID, ago string) {
t.Helper()
if _, err := pool.Exec(context.Background(), `
INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, fulfilled_at)
VALUES ($1, 'a', 'A', 'T', 'library', $2, now() - $3::interval)`, group, album, ago); err != nil {
t.Fatal(err)
}
}
// Only an album on the wanted list is fetched back (#5567): one taken off
// it, or dropped into the library by hand, is not. An album on the list is
// wanted by its release group, never its release id (#5241): a tag-supplied
// group is used as is; otherwise MusicBrainz names it and the answer is
// cached onto the album; an album MusicBrainz cannot name is skipped and
// tried again next pass.
func TestSweep_WantsBackOnlyAlbumsOnTheList_Integration(t *testing.T) {
pool := newSweepPool(t)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
q := dbq.New(pool)
settings, err := NewSettingsService(ctx, pool, logger)
if err != nil {
t.Fatal(err)
}
tagged := lostAlbum(t, pool, "Tagged", "release-tagged", "group-tagged")
resolved := lostAlbum(t, pool, "Resolved", "release-resolved", "")
unknown := lostAlbum(t, pool, "Unknown", "release-unknown", "")
_ = lostAlbum(t, pool, "Off The List", "release-off", "group-off")
heldWant(t, pool, "group-tagged", tagged, "3 days")
heldWant(t, pool, "group-resolved", resolved, "3 days")
heldWant(t, pool, "group-unknown", unknown, "3 days")
s := NewSweeper(pool, settings, logger)
asked := map[string]int{}
s.releaseGroup = func(_ context.Context, release string) (string, error) {
asked[release]++
if release == "release-resolved" {
return "group-resolved", nil
}
return "", tags.ErrNotFound
}
if err := s.SweepOnce(ctx); err != nil {
t.Fatalf("sweep: %v", err)
}
wantFor := func(group string, album pgtype.UUID) {
t.Helper()
w, err := q.GetWantByReleaseGroup(ctx, group)
if err != nil {
t.Fatalf("no want for %s: %v", group, err)
}
if w.Reason != "missing" || w.AlbumID != album || w.FulfilledAt.Valid {
t.Errorf("want %s = %+v: an open missing want about the album", group, w)
}
}
wantFor("group-tagged", tagged)
wantFor("group-resolved", resolved)
if w, _ := q.GetWantByReleaseGroup(ctx, "group-unknown"); !w.FulfilledAt.Valid {
t.Error("an album MusicBrainz cannot name was reopened under the wrong group")
}
if _, err := q.GetWantByReleaseGroup(ctx, "group-off"); err == nil {
t.Error("an album not on the list was wanted back")
}
if asked["release-tagged"] != 0 || asked["release-off"] != 0 {
t.Errorf("MusicBrainz asked %v: not about a tagged album, nor one off the list", asked)
}
var cached *string
if err := pool.QueryRow(ctx, "SELECT release_group_mbid FROM albums WHERE id = $1", resolved).Scan(&cached); err != nil {
t.Fatal(err)
}
if cached == nil || *cached != "group-resolved" {
t.Errorf("resolved group not cached on the album: %v", cached)
}
// The next pass finds both already wanted and asks nothing new.
asked = map[string]int{}
if err := s.SweepOnce(ctx); err != nil {
t.Fatal(err)
}
if asked["release-resolved"] != 0 || asked["release-unknown"] != 1 {
t.Errorf("second pass asked MusicBrainz %v: only the unresolved album again", asked)
}
}
// A want held after the files went missing means the library has a full copy
// despite the missing rows (#5245): the album is not wanted again. One held
// before they went missing is reopened.
func TestSweep_HeldWantsAreReopenedOnlyForANewLoss_Integration(t *testing.T) {
pool := newSweepPool(t)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
settings, err := NewSettingsService(ctx, pool, logger)
if err != nil {
t.Fatal(err)
}
stale := lostAlbum(t, pool, "Stale", "release-stale", "group-stale")
lost := lostAlbum(t, pool, "Lost Again", "release-lost", "group-lost")
for _, c := range []struct {
group, heldAgo string
album pgtype.UUID
}{{"group-stale", "1 hour", stale}, {"group-lost", "3 days", lost}} {
if _, err := pool.Exec(ctx, `
INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, fulfilled_at)
VALUES ($1, 'a', 'A', 'T', 'request', $2, now() - $3::interval)`, c.group, c.album, c.heldAgo); err != nil {
t.Fatal(err)
}
}
if err := NewSweeper(pool, settings, logger).SweepOnce(ctx); err != nil {
t.Fatal(err)
}
q := dbq.New(pool)
if w, _ := q.GetWantByReleaseGroup(ctx, "group-stale"); !w.FulfilledAt.Valid {
t.Error("a want held since the loss was reopened")
}
if w, _ := q.GetWantByReleaseGroup(ctx, "group-lost"); w.FulfilledAt.Valid || w.Reason != "missing" {
t.Errorf("a want held before the loss should reopen as missing: %+v", w)
}
}