Files
minstrel/internal/library/mbids_test.go
T
bvandeusenandClaude Opus 5.5 1a98a23d1f
release / go (push) Failing after 1m13s
release / web (push) Successful in 1m16s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m4s
release / android (push) Successful in 4m48s
release / Build signed APK (push) Successful in 5m13s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
feat(library): albums are filed under their album artist, and empty rows are tidied after a scan
The scanner read only the track artist, so a compilation tagged
Various Artists, or an album with one featured-artist track, became one
album per track artist. It now files the album under the album artist
the file names (ALBUMARTIST / TPE2 / aART, with its MusicBrainz id),
and each track keeps its own artist. Tag-read version 9 re-reads the
library so existing tracks move back onto one album.

A scan now ends by removing album rows no track points to, and artists
left with no albums or tracks, telling clients they are gone. Albums
whose tracks are only missing stay for Needs a file, an artist a Songs
like playlist is seeded from stays, and rows made in the last ten
minutes are left alone so a file being added is never caught half way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 19:08:21 -05:00

291 lines
9.8 KiB
Go

package library
import (
"testing"
"github.com/dhowden/tag"
)
// stubMeta is a minimal tag.Metadata used to feed the dhowden mbz extractor
// the same Raw()/Format() shape its real parsers produce. The other interface
// methods return zero values — the extractor only branches on Format and
// iterates Raw().
type stubMeta struct {
format tag.Format
raw map[string]interface{}
}
func (s stubMeta) Format() tag.Format { return s.format }
func (s stubMeta) FileType() tag.FileType { return tag.UnknownFileType }
func (s stubMeta) Title() string { return "" }
func (s stubMeta) Album() string { return "" }
func (s stubMeta) Artist() string { return "" }
func (s stubMeta) AlbumArtist() string { return "" }
func (s stubMeta) Composer() string { return "" }
func (s stubMeta) Year() int { return 0 }
func (s stubMeta) Genre() string { return "" }
func (s stubMeta) Track() (int, int) { return 0, 0 }
func (s stubMeta) Disc() (int, int) { return 0, 0 }
func (s stubMeta) Picture() *tag.Picture { return nil }
func (s stubMeta) Lyrics() string { return "" }
func (s stubMeta) Comment() string { return "" }
func (s stubMeta) Raw() map[string]interface{} { return s.raw }
func TestExtractMBIDs_Vorbis(t *testing.T) {
// dhowden lowercases all Vorbis comment keys at parse time.
m := stubMeta{
format: tag.VORBIS,
raw: map[string]interface{}{
"musicbrainz_albumid": "abc",
"musicbrainz_artistid": "def",
},
}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "abc" || ar != "def" {
t.Errorf("got (%q, %q), want (\"abc\", \"def\")", a, ar)
}
}
func TestExtractMBIDs_ID3v2(t *testing.T) {
// dhowden stores TXXX frames under bare "TXXX"/"TXXX_N" keys with
// *tag.Comm values whose Description holds the Picard tag name.
m := stubMeta{
format: tag.ID3v2_4,
raw: map[string]interface{}{
"TXXX": &tag.Comm{
Description: "MusicBrainz Album Id",
Text: "abc-123",
},
"TXXX_0": &tag.Comm{
Description: "MusicBrainz Artist Id",
Text: "def-456",
},
},
}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "abc-123" || ar != "def-456" {
t.Errorf("got (%q, %q), want (\"abc-123\", \"def-456\")", a, ar)
}
}
func TestExtractMBIDs_MP4(t *testing.T) {
// dhowden stores freeform iTunes atoms under their bare sub-name.
// mbz.Extract maps the human-readable form back to its lowercase key.
m := stubMeta{
format: tag.MP4,
raw: map[string]interface{}{
"MusicBrainz Album Id": "abc",
"MusicBrainz Artist Id": "def",
},
}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "abc" || ar != "def" {
t.Errorf("got (%q, %q), want (\"abc\", \"def\")", a, ar)
}
}
func TestExtractMBIDs_MissingTagsReturnEmpty(t *testing.T) {
m := stubMeta{format: tag.VORBIS, raw: map[string]interface{}{}}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "" || ar != "" {
t.Errorf("got (%q, %q), want empty", a, ar)
}
}
func TestExtractMBIDs_VorbisUppercaseKeysMiss(t *testing.T) {
// Real dhowden never produces uppercase Vorbis keys. Looking up
// "MUSICBRAINZ_ALBUMID" directly would find nothing, and every album
// would be skipped; this test keeps that lookup returning empty.
m := stubMeta{
format: tag.VORBIS,
raw: map[string]interface{}{
"MUSICBRAINZ_ALBUMID": "wrong-case",
"MUSICBRAINZ_ARTISTID": "wrong-case",
},
}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "" || ar != "" {
t.Errorf("uppercase Vorbis keys must not match dhowden output; got (%q, %q)", a, ar)
}
}
func TestExtractMBIDs_ID3v2StripsTrailingNUL(t *testing.T) {
// dhowden's readTextWithDescrFrame leaves the ID3v2.4 frame-
// terminator NUL on TXXX values. Postgres rejects NULs in text
// columns (SQLSTATE 22021), so we must strip them.
m := stubMeta{
format: tag.ID3v2_4,
raw: map[string]interface{}{
"TXXX": &tag.Comm{
Description: "MusicBrainz Album Id",
Text: "abc-123\x00",
},
"TXXX_0": &tag.Comm{
Description: "MusicBrainz Artist Id",
Text: "def-456\x00",
},
},
}
ids := ReadTagMBIDs(m)
a, ar := ids.Release, ids.Artist
if a != "abc-123" || ar != "def-456" {
t.Errorf("got (%q, %q), want trailing NUL stripped", a, ar)
}
}
func TestExtractMBIDs_ID3v2MultiValueTakesFirst(t *testing.T) {
// Some Picard configs emit multi-value TXXX:MusicBrainz Artist Id
// for collaborations, NUL-separated. Minstrel uses the primary
// release artist for MBCAA, so first wins.
m := stubMeta{
format: tag.ID3v2_4,
raw: map[string]interface{}{
"TXXX": &tag.Comm{
Description: "MusicBrainz Artist Id",
Text: "primary-mbid\x00collab-mbid",
},
},
}
ar := ReadTagMBIDs(m).Artist
if ar != "primary-mbid" {
t.Errorf("got %q, want %q", ar, "primary-mbid")
}
}
func TestCleanMBID_OnlyNULReturnsEmpty(t *testing.T) {
// Defensive: a TXXX value that is purely NULs (or empty after
// trimming) must round-trip to empty so the caller skips the row
// instead of writing a NUL-only string to Postgres.
if got := cleanMBID("\x00\x00"); got != "" {
t.Errorf("got %q, want empty", got)
}
if got := cleanMBID(""); got != "" {
t.Errorf("got %q, want empty", got)
}
}
// The release-group id is read beside the release id, from each
// format's own key, and the two are never confused.
func TestExtractReleaseGroupMBID(t *testing.T) {
cases := []struct {
name string
meta stubMeta
}{
{"vorbis", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"musicbrainz_albumid": "release-1",
"musicbrainz_releasegroupid": "group-1",
}}},
{"id3v2", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"TXXX": &tag.Comm{Description: "MusicBrainz Album Id", Text: "release-1"},
"TXXX_0": &tag.Comm{Description: "MusicBrainz Release Group Id", Text: "group-1\x00"},
}}},
{"mp4", stubMeta{format: tag.MP4, raw: map[string]interface{}{
"MusicBrainz Album Id": "release-1",
"MusicBrainz Release Group Id": "group-1",
}}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := ReadTagMBIDs(c.meta).ReleaseGroup; got != "group-1" {
t.Errorf("release group = %q, want group-1", got)
}
if album := ReadTagMBIDs(c.meta).Release; album != "release-1" {
t.Errorf("album (release) = %q, want release-1", album)
}
})
}
none := stubMeta{format: tag.VORBIS, raw: map[string]interface{}{"musicbrainz_albumid": "release-1"}}
if got := ReadTagMBIDs(none).ReleaseGroup; got != "" {
t.Errorf("untagged release group = %q, want empty", got)
}
}
// Each format's recording id lands in Recording, and the release-track
// id never does. Picard calls the recording MUSICBRAINZ_TRACKID in Vorbis
// comments, the name mbz gives the release track.
func TestReadTagMBIDs_Recording(t *testing.T) {
cases := []struct {
name string
meta stubMeta
}{
{"vorbis", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"musicbrainz_trackid": "rec-1",
"musicbrainz_releasetrackid": "reltrack-1",
}}},
{"id3v2 ufid", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"UFID": &tag.UFID{Provider: "http://musicbrainz.org", Identifier: []byte("rec-1")},
"TXXX_0": &tag.Comm{Description: "MusicBrainz Release Track Id", Text: "reltrack-1\x00"},
}}},
{"mp4", stubMeta{format: tag.MP4, raw: map[string]interface{}{
"MusicBrainz Track Id": "rec-1",
"MusicBrainz Release Track Id": "reltrack-1",
}}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := ReadTagMBIDs(c.meta).Recording; got != "rec-1" {
t.Errorf("recording = %q, want rec-1", got)
}
})
}
// A Vorbis file carrying only the release-track id has no recording id.
only := stubMeta{format: tag.VORBIS, raw: map[string]interface{}{"musicbrainz_releasetrackid": "reltrack-1"}}
if got := ReadTagMBIDs(only).Recording; got != "" {
t.Errorf("release track only: recording = %q, want empty", got)
}
}
// A Vorbis TRACKNUMBER of "3/20" is track 3: dhowden/tag reads it as 0.
func TestReadTrackAndDisc(t *testing.T) {
for _, tc := range []struct {
name string
m stubMeta
track, disc int
}{
{"vorbis with counts", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{"tracknumber": "3/20", "discnumber": "2/2"}}, 3, 2},
{"vorbis bare", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{"tracknumber": " 7 "}}, 7, 0},
{"vorbis junk", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{"tracknumber": "A1", "discnumber": "-1"}}, 0, 0},
{"id3 is the library's own reading", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{"tracknumber": "3/20"}}, 0, 0},
} {
if track, disc := ReadTrackAndDisc(tc.m); track != tc.track || disc != tc.disc {
t.Errorf("%s: got track %d disc %d, want %d and %d", tc.name, track, disc, tc.track, tc.disc)
}
}
}
// The album artist's id is read from each format's Picard name.
func TestReadTagMBIDs_AlbumArtist(t *testing.T) {
cases := []struct {
name string
meta stubMeta
}{
{"vorbis", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"musicbrainz_albumartistid": "aa-1",
"musicbrainz_artistid": "ar-1",
}}},
{"id3v2", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"TXXX": &tag.Comm{Description: "MusicBrainz Album Artist Id", Text: "aa-1\x00"},
"TXXX_0": &tag.Comm{Description: "MusicBrainz Artist Id", Text: "ar-1"},
}}},
{"mp4", stubMeta{format: tag.MP4, raw: map[string]interface{}{
"MusicBrainz Album Artist Id": "aa-1",
"MusicBrainz Artist Id": "ar-1",
}}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ids := ReadTagMBIDs(c.meta)
if ids.AlbumArtist != "aa-1" || ids.Artist != "ar-1" {
t.Errorf("album artist = %q, artist = %q; want aa-1 and ar-1", ids.AlbumArtist, ids.Artist)
}
})
}
}