Files
minstrel/internal/reacquisition/sweeper_test.go
T
bvandeusenandClaude Opus 5.5 460e78c955
release / govulncheck (push) Successful in 17s
release / web (push) Successful in 1m24s
release / go (push) Successful in 1m32s
release / integration (push) Successful in 5m1s
release / android (push) Successful in 5m7s
release / Build signed APK (releases and dev) (push) Successful in 5m17s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 16s
release / Verify release artifacts (tag releases only) (push) Skipped
fix: an album with missing tracks is wanted back by its release group, not only when it carries a release MBID (#6167)
The release id was only ever used to find the group. 238 albums matched to a
group carried none, so their losses, five of the resolver's wrong-slot
removals among them, were never fetched again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 10:21:06 -04:00

201 lines
7.1 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 (when release is set) and release group
// (when group is set).
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-" + title
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}
if release != "" {
params.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/" + title + "/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")
// Matched to a group with no release id: 238 such albums on the operator's
// library were never wanted back (#6167).
groupOnly := lostAlbum(t, pool, "Group Only", "", "group-only")
heldWant(t, pool, "group-tagged", tagged, "3 days")
heldWant(t, pool, "group-only", groupOnly, "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)
wantFor("group-only", groupOnly)
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)
}
}