release / go (push) Successful in 1m57s
release / web (push) Successful in 1m8s
release / integration (push) Successful in 2m42s
release / govulncheck (push) Successful in 20s
release / android (push) Successful in 1m53s
release / Build signed APK (push) Successful in 1m43s
release / Build + push container image (push) Successful in 24s
The Title Case sweep of the export found real song titles and artists in fixtures and comments: three songs from one real album, a real song in a release fixture, real artists in a tag test, a link test and a genre test, a real song as a notification example, and a count from a real folder. Each is now invented. 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 - Ring It Out.mp3"
|
|
newPath = "/music/Lincoln Yard/Minutes to Noon/04 - Ring 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)
|
|
}
|
|
})
|
|
}
|
|
}
|