release / web (push) Successful in 1m30s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m26s
release / android (push) Successful in 4m40s
release / Build signed APK (push) Successful in 5m12s
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 28s
release / Verify release artifacts (tag releases only) (push) Skipped
- Moved-file adoption: 14 tests become one table. uniqueMatch, the row converters and pgtype's zero UUID are dropped: the adoption table covers the first two, and the third tests pgx. - Reconcile: marking and its change rows, and restoring and its change rows, are one test each; the five "a volume looks absent" refusals are one table. Dropped: an error-propagation test. - Genres: the ID3 reader cases, the normalisers and extractGenres across ID3, FLAC, Ogg and MP4 are one table each. - MusicBrainz tag ids: eleven per-format tests become one table. - Scan trigger: start / skip / reap is one table. Dropped: a closed-pool test. - Stage publisher: six tests become three. - Duplicate resolver: the cross-release cases (rip, renamed, kept as fine, two titles) asserted the same outcome once the resolver stopped removing files. They become one table that also checks the group's note, which is where rip and clean still differ. The auto-resolve-off song-link check moves into the existing auto-resolve-off test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
5.9 KiB
Go
145 lines
5.9 KiB
Go
package library
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// fakeAdopter answers the audio-hash lookup only for the hash it holds, the way
|
|
// the real query does. A fake that returned its rows for any hash could not
|
|
// tell adoption by identity apart from adoption by coincidence.
|
|
type fakeAdopter struct {
|
|
byMbid []dbq.FindMissingTrackByMbidRow
|
|
hash []byte
|
|
byHash []dbq.FindMissingTrackByAudioHashRow
|
|
mbidErr error
|
|
hashErr error
|
|
adoptErr error
|
|
adoptRows int64
|
|
|
|
mbidQueried []string
|
|
hashQueried [][]byte
|
|
adopted []dbq.AdoptTrackPathParams
|
|
}
|
|
|
|
func (f *fakeAdopter) FindMissingTrackByMbid(_ context.Context, mbid string) ([]dbq.FindMissingTrackByMbidRow, error) {
|
|
f.mbidQueried = append(f.mbidQueried, mbid)
|
|
return f.byMbid, f.mbidErr
|
|
}
|
|
|
|
func (f *fakeAdopter) FindMissingTrackByAudioHash(
|
|
_ context.Context, audioStreamSha256 []byte,
|
|
) ([]dbq.FindMissingTrackByAudioHashRow, error) {
|
|
f.hashQueried = append(f.hashQueried, audioStreamSha256)
|
|
if f.hashErr != nil {
|
|
return nil, f.hashErr
|
|
}
|
|
if !bytes.Equal(audioStreamSha256, f.hash) {
|
|
return nil, nil
|
|
}
|
|
return f.byHash, nil
|
|
}
|
|
|
|
func (f *fakeAdopter) AdoptTrackPath(_ context.Context, arg dbq.AdoptTrackPathParams) (int64, error) {
|
|
f.adopted = append(f.adopted, arg)
|
|
if f.adoptErr != nil {
|
|
return 0, f.adoptErr
|
|
}
|
|
return f.adoptRows, nil
|
|
}
|
|
|
|
// The narrowed interface must not drift from the real queries.
|
|
var _ trackAdopter = (*dbq.Queries)(nil)
|
|
|
|
func mbidRow(n byte, path string) dbq.FindMissingTrackByMbidRow {
|
|
return dbq.FindMissingTrackByMbidRow{ID: testUUID(n), FilePath: path}
|
|
}
|
|
|
|
func hashRow(n byte, path string) dbq.FindMissingTrackByAudioHashRow {
|
|
return dbq.FindMissingTrackByAudioHashRow{ID: testUUID(n), FilePath: path}
|
|
}
|
|
|
|
func audioHash(b byte) []byte { return bytes.Repeat([]byte{b}, 32) }
|
|
|
|
const (
|
|
oldPath = "/music/Lincoln Yard/Minutes to Noon/02 - Bleed It Out.mp3"
|
|
newPath = "/music/Lincoln Yard/Minutes to Noon/04 - Bleed It Out.mp3"
|
|
)
|
|
|
|
// A new file is adopted as a missing track that moved when its recording MBID
|
|
// names exactly one missing track, or failing that its audio hash does. Size
|
|
// and duration are not inputs: only identity can match. Anything ambiguous, or
|
|
// any failure, inserts the file fresh instead: adopting by coin flip would
|
|
// attach the wrong history, and failing to detect a move must never fail the
|
|
// file.
|
|
func TestAdoptMovedTrack(t *testing.T) {
|
|
sentinel := errors.New("db down")
|
|
twoMbid := []dbq.FindMissingTrackByMbidRow{mbidRow(1, "/music/a.mp3"), mbidRow(2, "/music/b.mp3")}
|
|
for _, tc := range []struct {
|
|
name string
|
|
q *fakeAdopter
|
|
hash []byte
|
|
mbid string
|
|
// want is the adopted row's id byte; 0 means nothing adopted.
|
|
want byte
|
|
// noHashLookup: the audio hash must not be looked up at all.
|
|
noHashLookup bool
|
|
}{
|
|
{name: "by MBID, without consulting the hash", hash: audioHash(1), mbid: "rec-mbid", want: 7, noHashLookup: true,
|
|
q: &fakeAdopter{byMbid: []dbq.FindMissingTrackByMbidRow{mbidRow(7, oldPath)}, adoptRows: 1}},
|
|
{name: "by audio hash when untagged", hash: audioHash(3), want: 3,
|
|
q: &fakeAdopter{hash: audioHash(3), byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(3, oldPath)}, adoptRows: 1}},
|
|
{name: "different audio", hash: audioHash(0xBB),
|
|
q: &fakeAdopter{hash: audioHash(0xAA), byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(1, oldPath)}, adoptRows: 1}},
|
|
{name: "two missing tracks share the MBID", mbid: "rec-mbid",
|
|
q: &fakeAdopter{byMbid: twoMbid, adoptRows: 1}},
|
|
{name: "an ambiguous MBID settled by the hash", hash: audioHash(2), mbid: "rec-mbid", want: 2,
|
|
q: &fakeAdopter{byMbid: twoMbid, hash: audioHash(2),
|
|
byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(2, "/music/b.mp3")}, adoptRows: 1}},
|
|
{name: "two missing tracks share the audio", hash: audioHash(5),
|
|
q: &fakeAdopter{hash: audioHash(5), byHash: []dbq.FindMissingTrackByAudioHashRow{
|
|
hashRow(1, "/music/a.mp3"), hashRow(2, "/music/b.mp3")}, adoptRows: 1}},
|
|
// Every unhashable file would pair with every other.
|
|
{name: "no hash taken (nil)", hash: nil, noHashLookup: true,
|
|
q: &fakeAdopter{hash: nil, byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(1, oldPath)}, adoptRows: 1}},
|
|
{name: "no hash taken (empty)", hash: []byte{}, noHashLookup: true,
|
|
q: &fakeAdopter{hash: []byte{}, byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(1, oldPath)}, adoptRows: 1}},
|
|
{name: "no candidates", hash: audioHash(9), mbid: "rec-mbid",
|
|
q: &fakeAdopter{adoptRows: 1}},
|
|
// Another file adopted the row between lookup and update; the
|
|
// missing_since predicate matches no row.
|
|
{name: "lost the race", hash: audioHash(5), mbid: "rec-mbid",
|
|
q: &fakeAdopter{byMbid: []dbq.FindMissingTrackByMbidRow{mbidRow(5, oldPath)}, adoptRows: 0}},
|
|
{name: "the hash lookup fails", hash: audioHash(1), mbid: "rec-mbid",
|
|
q: &fakeAdopter{hashErr: sentinel}},
|
|
{name: "the adopt fails", hash: audioHash(1), mbid: "rec-mbid",
|
|
q: &fakeAdopter{byMbid: []dbq.FindMissingTrackByMbidRow{mbidRow(1, oldPath)}, adoptErr: sentinel}},
|
|
{name: "an MBID lookup error still tries the hash", hash: audioHash(9), mbid: "rec-mbid", want: 9,
|
|
q: &fakeAdopter{mbidErr: sentinel, hash: audioHash(9),
|
|
byHash: []dbq.FindMissingTrackByAudioHashRow{hashRow(9, oldPath)}, adoptRows: 1}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := testScanner(t)
|
|
got := s.adoptMovedTrack(context.Background(), tc.q, newPath, tc.hash, tc.mbid)
|
|
if got != (tc.want != 0) {
|
|
t.Fatalf("adopted = %v, want %v", got, tc.want != 0)
|
|
}
|
|
if tc.want != 0 {
|
|
if n := len(tc.q.adopted); n == 0 || tc.q.adopted[n-1].ID != testUUID(tc.want) || tc.q.adopted[n-1].FilePath != newPath {
|
|
t.Errorf("adopted %+v, want row %d at %q", tc.q.adopted, tc.want, newPath)
|
|
}
|
|
}
|
|
if tc.mbid == "" && len(tc.q.mbidQueried) != 0 {
|
|
t.Error("queried by MBID with no MBID available")
|
|
}
|
|
if tc.noHashLookup && len(tc.q.hashQueried) != 0 {
|
|
t.Errorf("looked up the audio hash: %x", tc.q.hashQueried)
|
|
}
|
|
})
|
|
}
|
|
}
|