Files
AppleSync/internal/library/library_test.go
T
2026-11-26 15:11:41 +01:00

206 lines
7.3 KiB
Go

package library
import (
"context"
"os"
"path/filepath"
"testing"
"applesync/internal/model"
)
func TestReadTagsFilenameFallback(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "03. Some Artist - Some Title.flac")
if err := os.WriteFile(path, nil, 0o644); err != nil {
t.Fatal(err)
}
file, err := readTags(path)
if err != nil {
t.Fatal(err)
}
if file.Artist != "Some Artist" || file.Title != "Some Title" || file.TrackNumber != 3 {
t.Fatalf("unexpected file: %+v", file)
}
}
func TestFindAlbumDirs(t *testing.T) {
album := &model.Album{AppleID: "a1", Title: "Popfly", Artist: "Michael Nau"}
match := &model.AlbumMatch{QobuzTitle: "Popfly", QobuzArtist: "Michael Nau"}
dirs := []dirInfo{
{path: "/music/Michael Nau - Popfly (2026) [FLAC] [24B-44.1kHz]", norm: "michael nau popfly 2026 flac 24b 44 1khz"},
{path: "/music/Other - Album", norm: "other album"},
}
got := findAlbumDirs(album, match, dirs)
if len(got) != 1 || got[0] != dirs[0].path {
t.Fatalf("got %#v", got)
}
}
func TestFindAlbumDirsLongTitlePrefix(t *testing.T) {
album := &model.Album{AppleID: "a1", Title: "A very long album title that will certainly exceed the truncation limit used by the ripper for folder names", Artist: "Artist"}
dirs := []dirInfo{
{path: "/music/Artist - A very long album title that will certainly exceed the truncatio", norm: "artist a very long album title that will certainly exceed the truncatio"},
}
got := findAlbumDirs(album, nil, dirs)
if len(got) != 1 {
t.Fatalf("got %#v", got)
}
}
func newIndex(files ...*model.FileEntry) *Index {
idx := &Index{
byISRC: map[string][]*model.FileEntry{},
byKey: map[string][]*model.FileEntry{},
byTitle: map[string][]*model.FileEntry{},
seenPaths: map[string]bool{},
missingDir: map[string]bool{},
}
for _, file := range files {
idx.add(file)
}
return idx
}
func TestResolveByISRC(t *testing.T) {
file := &model.FileEntry{Path: "/music/a.flac", ISRC: "ABC123", Title: "One", Artist: "A", Album: "Album"}
idx := newIndex(file)
got, ok := idx.Resolve(&model.Track{Title: "Wrong", ArtistName: "B", ISRC: "ABC123"}, &model.Album{Title: "Album", Artist: "A"}, nil)
if !ok || got.Path != file.Path {
t.Fatalf("got %+v ok=%v", got, ok)
}
}
func TestResolveByArtistTitle(t *testing.T) {
file := &model.FileEntry{Path: "/music/a.flac", Title: "One", Artist: "A", Album: "Album"}
idx := newIndex(file)
got, ok := idx.Resolve(&model.Track{Title: "One", ArtistName: "A"}, &model.Album{Title: "Album", Artist: "A"}, nil)
if !ok || got.Path != file.Path {
t.Fatalf("got %+v ok=%v", got, ok)
}
}
func TestResolveRejectsWrongArtistSameTitle(t *testing.T) {
file := &model.FileEntry{Path: "/music/sinking.flac", Title: "Sinking", Artist: "Beatrice M.", Album: "Sinking"}
idx := newIndex(file)
got, ok := idx.Resolve(
&model.Track{Title: "Sinking", ArtistName: "Big Jesus Trash Can", AlbumName: "Sinking - Single"},
&model.Album{Title: "Sinking - Single", Artist: "Big Jesus Trash Can"},
nil,
)
if ok {
t.Fatalf("expected no match, got %+v", got)
}
}
func TestResolvePrefersImportedLibraryCopy(t *testing.T) {
root := t.TempDir()
downloads := filepath.Join(root, "staging")
library := filepath.Join(root, "library")
stagedDir := filepath.Join(downloads, "Michael Nau - Popfly (2026) [FLAC]")
importedDir := filepath.Join(library, "Michael Nau", "Popfly")
if err := os.MkdirAll(stagedDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(importedDir, 0o755); err != nil {
t.Fatal(err)
}
staged := filepath.Join(stagedDir, "01. Michael Nau - Lost Station Hideaway.flac")
imported := filepath.Join(importedDir, "01. Michael Nau - Lost Station Hideaway.flac")
if err := os.WriteFile(staged, nil, 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(imported, nil, 0o644); err != nil {
t.Fatal(err)
}
albums := []*model.Album{{AppleID: "a1", Title: "Popfly", Artist: "Michael Nau", TrackCount: 10}}
matches := map[string]*model.AlbumMatch{"a1": {QobuzTitle: "Popfly", QobuzArtist: "Michael Nau"}}
idx, err := Build(context.Background(), downloads, library, albums, matches, "albums", nil)
if err != nil {
t.Fatal(err)
}
track := &model.Track{Title: "Lost Station Hideaway", ArtistName: "Michael Nau", AlbumName: "Popfly"}
got, ok := idx.Resolve(track, albums[0], matches["a1"])
if !ok {
t.Fatal("no match")
}
if got.Path != imported {
t.Fatalf("resolved %s, want imported copy %s", got.Path, imported)
}
}
func TestScanRootsNested(t *testing.T) {
roots := scanRoots("/music", "/music/staging")
if len(roots) != 1 || roots[0] != "/music" {
t.Fatalf("roots = %#v", roots)
}
roots = scanRoots("/library", "/staging")
if len(roots) != 2 {
t.Fatalf("roots = %#v", roots)
}
}
func TestBuildWithBeetsPrefersLibrary(t *testing.T) {
root := t.TempDir()
downloads := filepath.Join(root, "staging")
libraryRoot := filepath.Join(root, "library")
stagedDir := filepath.Join(downloads, "Michael Nau - Popfly (2026) [FLAC]")
if err := os.MkdirAll(stagedDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(libraryRoot, "Michael Nau", "Popfly"), 0o755); err != nil {
t.Fatal(err)
}
staged := filepath.Join(stagedDir, "01. Michael Nau - Lost Station Hideaway.flac")
if err := os.WriteFile(staged, nil, 0o644); err != nil {
t.Fatal(err)
}
imported := filepath.Join(libraryRoot, "Michael Nau", "Popfly", "01 Lost Station Hideaway.flac")
beetsFiles := []*model.FileEntry{{
Path: imported, Title: "Lost Station Hideaway", Artist: "Michael Nau", Album: "Popfly", ISRC: "ABC123",
}}
albums := []*model.Album{{AppleID: "a1", Title: "Popfly", Artist: "Michael Nau", TrackCount: 10}}
matches := map[string]*model.AlbumMatch{"a1": {QobuzTitle: "Popfly", QobuzArtist: "Michael Nau"}}
idx, err := BuildWithBeets(context.Background(), downloads, libraryRoot, beetsFiles, albums, matches, "albums", nil)
if err != nil {
t.Fatal(err)
}
// ISRC match resolves to the beets library copy.
track := &model.Track{Title: "Lost Station Hideaway", ArtistName: "Michael Nau", AlbumName: "Popfly", ISRC: "ABC123"}
got, ok := idx.Resolve(track, albums[0], matches["a1"])
if !ok || got.Path != imported {
t.Fatalf("got %+v ok=%v, want %s", got, ok, imported)
}
// Artist/title match also prefers the library copy.
track.ISRC = ""
got, ok = idx.Resolve(track, albums[0], matches["a1"])
if !ok || got.Path != imported {
t.Fatalf("got %+v ok=%v, want %s", got, ok, imported)
}
}
func TestBuildAndResolve(t *testing.T) {
root := t.TempDir()
albumDir := filepath.Join(root, "Michael Nau - Popfly (2026) [FLAC] [24B-44.1kHz]")
if err := os.MkdirAll(albumDir, 0o755); err != nil {
t.Fatal(err)
}
file := filepath.Join(albumDir, "01. Michael Nau - Lost Station Hideaway.flac")
if err := os.WriteFile(file, nil, 0o644); err != nil {
t.Fatal(err)
}
albums := []*model.Album{{AppleID: "a1", Title: "Popfly", Artist: "Michael Nau", TrackCount: 10}}
matches := map[string]*model.AlbumMatch{"a1": {QobuzTitle: "Popfly", QobuzArtist: "Michael Nau"}}
idx, err := Build(context.Background(), root, root, albums, matches, "albums", nil)
if err != nil {
t.Fatal(err)
}
track := &model.Track{Title: "Lost Station Hideaway", ArtistName: "Michael Nau", AlbumName: "Popfly"}
got, ok := idx.Resolve(track, albums[0], matches["a1"])
if !ok || filepath.Base(got.Path) != filepath.Base(file) {
t.Fatalf("got %+v ok=%v", got, ok)
}
}