174 lines
5.0 KiB
Go
174 lines
5.0 KiB
Go
package matcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"applesync/internal/model"
|
|
)
|
|
|
|
type fakeQobuz struct {
|
|
results []model.QobuzAlbum
|
|
albums map[string]model.QobuzAlbum
|
|
}
|
|
|
|
func (f *fakeQobuz) SearchAlbums(context.Context, string, int) ([]model.QobuzAlbum, error) {
|
|
return f.results, nil
|
|
}
|
|
|
|
func (f *fakeQobuz) GetAlbum(_ context.Context, id string) (*model.QobuzAlbum, error) {
|
|
album, ok := f.albums[id]
|
|
if !ok {
|
|
return nil, errors.New("not found")
|
|
}
|
|
return &album, nil
|
|
}
|
|
|
|
func TestMatchUPC(t *testing.T) {
|
|
album := &model.Album{
|
|
AppleID: "apple-1",
|
|
Title: "Popfly",
|
|
Artist: "Michael Nau",
|
|
UPC: "617308063523",
|
|
TrackCount: 10,
|
|
Year: 2026,
|
|
Tracks: []*model.Track{{ISRC: "US33X2624501"}},
|
|
}
|
|
q := &fakeQobuz{
|
|
results: []model.QobuzAlbum{{ID: "q1", Title: "Popfly", Artist: "Michael Nau", UPC: "0617308063523", TrackCount: 10, Year: 2026}},
|
|
albums: map[string]model.QobuzAlbum{"q1": {ID: "q1", Title: "Popfly", Artist: "Michael Nau", TrackCount: 10}},
|
|
}
|
|
m := New(q, nil, 1)
|
|
matches, err := m.MatchAll(context.Background(), []*model.Album{album})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
match := matches["apple-1"]
|
|
if match == nil || match.QobuzAlbumID != "q1" || match.Method != "upc" {
|
|
t.Fatalf("unexpected match: %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestMatchFuzzyStrongMetadataWithDifferentISRCs(t *testing.T) {
|
|
album := &model.Album{
|
|
AppleID: "apple-1",
|
|
Title: "Train on the Island",
|
|
Artist: "Aldous Harding",
|
|
UPC: "191400087378",
|
|
TrackCount: 10,
|
|
Year: 2026,
|
|
Tracks: []*model.Track{{ISRC: "GBAFL2500561"}},
|
|
}
|
|
q := &fakeQobuz{
|
|
results: []model.QobuzAlbum{{ID: "q1", Title: "Train on the Island", Artist: "Aldous Harding", UPC: "0191400087361", TrackCount: 10, Year: 2026}},
|
|
albums: map[string]model.QobuzAlbum{"q1": {
|
|
ID: "q1", Title: "Train on the Island", Artist: "Aldous Harding", TrackCount: 10, Year: 2026,
|
|
Tracks: []model.QobuzTrack{{ISRC: "GBAFL2500549"}},
|
|
}},
|
|
}
|
|
m := New(q, nil, 1)
|
|
matches, err := m.MatchAll(context.Background(), []*model.Album{album})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
match := matches["apple-1"]
|
|
if match == nil || match.Method != "fuzzy" {
|
|
t.Fatalf("unexpected match: %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestMatchShortEditionPartialISRC(t *testing.T) {
|
|
album := &model.Album{
|
|
AppleID: "apple-1",
|
|
Title: "The X-panded Universe - Single",
|
|
Artist: "Father John Misty",
|
|
UPC: "098787965360",
|
|
TrackCount: 3,
|
|
Year: 2026,
|
|
Tracks: []*model.Track{{ISRC: "USSUB2572602"}},
|
|
}
|
|
q := &fakeQobuz{
|
|
results: []model.QobuzAlbum{{ID: "q1", Title: "The X-panded Universe", Artist: "Father John Misty", UPC: "0098787170566", TrackCount: 1, Year: 2026}},
|
|
albums: map[string]model.QobuzAlbum{"q1": {
|
|
ID: "q1", Title: "The X-panded Universe", Artist: "Father John Misty", TrackCount: 1, Year: 2026,
|
|
Tracks: []model.QobuzTrack{{ISRC: "USSUB2572602"}},
|
|
}},
|
|
}
|
|
m := New(q, nil, 1)
|
|
matches, err := m.MatchAll(context.Background(), []*model.Album{album})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if match := matches["apple-1"]; match == nil || match.Method != "isrc" {
|
|
t.Fatalf("unexpected match: %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestMatchRejectsShortEditionForMultiTrackPlaylist(t *testing.T) {
|
|
album := &model.Album{
|
|
AppleID: "apple-1",
|
|
Title: "Album",
|
|
Artist: "Artist",
|
|
UPC: "111",
|
|
TrackCount: 5,
|
|
Year: 2026,
|
|
Tracks: []*model.Track{
|
|
{ISRC: "AAA"},
|
|
{ISRC: "BBB"},
|
|
},
|
|
}
|
|
q := &fakeQobuz{
|
|
results: []model.QobuzAlbum{{ID: "q1", Title: "Album", Artist: "Artist", UPC: "222", TrackCount: 1, Year: 2026}},
|
|
albums: map[string]model.QobuzAlbum{"q1": {
|
|
ID: "q1", Title: "Album", Artist: "Artist", TrackCount: 1, Year: 2026,
|
|
Tracks: []model.QobuzTrack{{ISRC: "AAA"}},
|
|
}},
|
|
}
|
|
m := New(q, nil, 1)
|
|
matches, err := m.MatchAll(context.Background(), []*model.Album{album})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if match := matches["apple-1"]; match != nil {
|
|
t.Fatalf("expected no match, got %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestMatchRejectsDifferentArtist(t *testing.T) {
|
|
album := &model.Album{
|
|
AppleID: "apple-1",
|
|
Title: "Sinking",
|
|
Artist: "Big Jesus Trash Can",
|
|
UPC: "111",
|
|
TrackCount: 1,
|
|
Year: 2026,
|
|
Tracks: []*model.Track{{ISRC: "AAA"}},
|
|
}
|
|
q := &fakeQobuz{
|
|
results: []model.QobuzAlbum{{ID: "q1", Title: "Sinking", Artist: "Beatrice M.", UPC: "222", TrackCount: 1, Year: 2026}},
|
|
albums: map[string]model.QobuzAlbum{"q1": {
|
|
ID: "q1", Title: "Sinking", Artist: "Beatrice M.", TrackCount: 1, Year: 2026,
|
|
Tracks: []model.QobuzTrack{{ISRC: "BBB"}},
|
|
}},
|
|
}
|
|
m := New(q, nil, 1)
|
|
matches, err := m.MatchAll(context.Background(), []*model.Album{album})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if match := matches["apple-1"]; match != nil {
|
|
t.Fatalf("expected no match, got %+v", match)
|
|
}
|
|
}
|
|
|
|
func TestUPCNormalization(t *testing.T) {
|
|
if got := normalizeUPC("0617308063523"); got != "617308063523" {
|
|
t.Fatalf("normalizeUPC = %q", got)
|
|
}
|
|
variants := upcVariants("617308063523")
|
|
if len(variants) == 0 || variants[0] != "0617308063523" {
|
|
t.Fatalf("variants = %#v", variants)
|
|
}
|
|
}
|