release / go (push) Successful in 2m28s
release / web (push) Successful in 1m42s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 5m9s
release / android (push) Successful in 5m28s
release / Build signed APK (push) Successful in 5m54s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 34s
release / Verify release artifacts (tag releases only) (push) Skipped
Other programs file music into the library now, and a startup pass
deleted a whole album a download manager had just filed, keeping older
loose copies of the same songs. The resolver assumed Minstrel was the
only program writing the library; it is not.
An automatic pass now changes only Minstrel's database:
- copies of one song, on one album or across releases, are linked as one
song, so likes, plays and mixes count it once, and any group an admin
may want to thin stays pending for review (and is announced);
- distinct versions are still dismissed, and rips still held back;
- a one-album wrong slot gets a note naming the song MusicBrainz or
AcoustID says the recording is, for the admin who settles it.
Removing a copy is an admin's merge or wrong-slot settle, as before. When
an admin merges, a copy in a folder named for its album ("Album (Year)",
or its "CD nn" folder) now outranks a loose copy of the same audio.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
5.3 KiB
Go
143 lines
5.3 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
|
syncpkg "github.com/Fabled-Sword/minstrel/internal/sync"
|
|
)
|
|
|
|
// undecidedSlot is one recording filed as Void and as Reach, two songs
|
|
// MusicBrainz lists at lengths that cannot tell them apart: the resolver
|
|
// leaves it pending for an admin.
|
|
func undecidedSlot(t *testing.T) (oneAlbumFixture, dbq.User) {
|
|
t.Helper()
|
|
f := newOneAlbumFixture(t, "acoustic", "Void", "Reach")
|
|
ctx := context.Background()
|
|
seedTrackList(t, f.pool, f.album, map[string]int{"Void": 220500, "Reach": 221000})
|
|
if res, err := ResolveDuplicates(ctx, f.pool, nil, true); err != nil || res.SlotsNamed != 0 {
|
|
t.Fatalf("resolve = %+v, %v; want the group left for the operator", res, err)
|
|
}
|
|
name := strings.NewReplacer("/", "-", " ", "-").Replace(t.Name())
|
|
u, err := dbq.New(f.pool).CreateUser(ctx, dbq.CreateUserParams{
|
|
Username: dbtest.TestUserPrefix + name, PasswordHash: "x", ApiTokenHash: name + "-token",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return f, u
|
|
}
|
|
|
|
// emptiedTrack says whether a copy's track is marked missing and its slot
|
|
// emptied.
|
|
func emptiedTrack(t *testing.T, f oneAlbumFixture, id pgtype.UUID) bool {
|
|
t.Helper()
|
|
var emptied bool
|
|
if err := f.pool.QueryRow(context.Background(),
|
|
`SELECT missing_since IS NOT NULL AND emptied_at IS NOT NULL FROM tracks WHERE id = $1`, id).Scan(&emptied); err != nil {
|
|
t.Fatalf("track %s is gone: %v", syncpkg.FormatUUID(id), err)
|
|
}
|
|
return emptied
|
|
}
|
|
|
|
// settledByHand checks the group closed as the admin's and the audit row
|
|
// names them.
|
|
func settledByHand(t *testing.T, f oneAlbumFixture, actor dbq.User) {
|
|
t.Helper()
|
|
if status, _, _, auto := (resolveFixture{pool: f.pool, groupID: f.groupID}).group(t); status != "merged" || auto {
|
|
t.Errorf("group = (%s, auto=%v), want merged by the operator", status, auto)
|
|
}
|
|
var n int
|
|
if err := f.pool.QueryRow(context.Background(),
|
|
`SELECT count(*) FROM audit_log WHERE action = 'duplicate_wrong_file' AND actor_id = $1
|
|
AND metadata->>'group_id' = $2 AND NOT (metadata->>'automatic')::boolean`,
|
|
actor.ID, syncpkg.FormatUUID(f.groupID)).Scan(&n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("operator's wrong-file audit rows = %d, want 1", n)
|
|
}
|
|
}
|
|
|
|
// The admin cannot say which song the recording is, so every copy goes and
|
|
// both slots show as missing. A merge would have kept one title.
|
|
func TestSettleWrongSlot_GetItAgainRemovesEveryCopy_Integration(t *testing.T) {
|
|
f, op := undecidedSlot(t)
|
|
ctx := context.Background()
|
|
res, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, pgtype.UUID{}, op.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Kept != nil || len(res.Removed) != 2 || exists(f.aPath) || exists(f.bPath) {
|
|
t.Fatalf("result %+v, void exists %v, reach exists %v; want both copies removed",
|
|
res, exists(f.aPath), exists(f.bPath))
|
|
}
|
|
for _, id := range []pgtype.UUID{f.a.ID, f.b.ID} {
|
|
if !emptiedTrack(t, f, id) {
|
|
t.Errorf("track %s is not marked missing with its slot emptied", syncpkg.FormatUUID(id))
|
|
}
|
|
got, err := dbq.New(f.pool).GetTrackByID(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.RemovedReason == nil || *got.RemovedReason != string(RemovedForDuplicate) || got.RemovedBy != op.ID {
|
|
t.Errorf("track %s waits with reason %v by %v, want duplicate by the admin",
|
|
syncpkg.FormatUUID(id), got.RemovedReason, got.RemovedBy)
|
|
}
|
|
}
|
|
settledByHand(t, f, op)
|
|
|
|
if _, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, pgtype.UUID{}, op.ID); !errors.Is(err, ErrDuplicateGroupNotPending) {
|
|
t.Errorf("settling it twice: err = %v, want ErrDuplicateGroupNotPending", err)
|
|
}
|
|
}
|
|
|
|
// "This one is Reach": the copy the admin names stays, and only Void's slot
|
|
// is emptied.
|
|
func TestSettleWrongSlot_KeepsTheCopyTheOperatorNames_Integration(t *testing.T) {
|
|
f, op := undecidedSlot(t)
|
|
res, err := SettleWrongSlot(context.Background(), f.pool, nil, f.groupID, f.b.ID, op.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Kept == nil || res.Kept.TrackID != f.b.ID || len(res.Removed) != 1 || exists(f.aPath) || !exists(f.bPath) {
|
|
t.Fatalf("result %+v, void exists %v, reach exists %v; want only Void's copy removed",
|
|
res, exists(f.aPath), exists(f.bPath))
|
|
}
|
|
if !emptiedTrack(t, f, f.a.ID) {
|
|
t.Error("Void's track is not marked missing with its slot emptied")
|
|
}
|
|
settledByHand(t, f, op)
|
|
}
|
|
|
|
// Only a group of one recording in several slots is settled this way, and only
|
|
// with a copy from it.
|
|
func TestSettleWrongSlot_RefusesWhatItCannotSettle_Integration(t *testing.T) {
|
|
ctx := context.Background()
|
|
t.Run("two versions of a song", func(t *testing.T) {
|
|
f := newOneAlbumFixture(t, "acoustic", "Homesick", "Homesick (Instrumental)")
|
|
if _, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, pgtype.UUID{}, pgtype.UUID{}); !errors.Is(err, ErrNotWrongSlot) {
|
|
t.Errorf("err = %v, want ErrNotWrongSlot", err)
|
|
}
|
|
if !exists(f.aPath) || !exists(f.bPath) {
|
|
t.Error("a file was removed")
|
|
}
|
|
})
|
|
t.Run("a copy from another group", func(t *testing.T) {
|
|
f, op := undecidedSlot(t)
|
|
other := pgtype.UUID{Bytes: [16]byte{1}, Valid: true}
|
|
if _, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, other, op.ID); !errors.Is(err, ErrSurvivorNotInGroup) {
|
|
t.Errorf("err = %v, want ErrSurvivorNotInGroup", err)
|
|
}
|
|
if !exists(f.aPath) || !exists(f.bPath) {
|
|
t.Error("something changed")
|
|
}
|
|
})
|
|
}
|