Files
minstrel/internal/library/mbids_test.go
T
bvandeusenandClaude Opus 5.5 ac8792323c
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
test(library): merge repeated cases into tables, drop tests of other code (#8296)
- 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>
2026-12-02 12:32:36 -05:00

112 lines
5.4 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 }
// ReadTagMBIDs reads each MusicBrainz id from each format's own Picard key,
// in dhowden/tag's Raw() shape for that format: Vorbis keys lowercased, ID3
// TXXX frames under "TXXX"/"TXXX_N" with the Picard name in the Description,
// MP4 freeform atoms under their bare name. The release and the release
// group, and the recording and the release track, are never confused.
func TestReadTagMBIDs(t *testing.T) {
all := TagMBIDs{Release: "rel-1", Artist: "ar-1", AlbumArtist: "aa-1", ReleaseGroup: "rg-1", Recording: "rec-1"}
txxx := func(name, text string) *tag.Comm { return &tag.Comm{Description: name, Text: text} }
for _, tc := range []struct {
name string
meta stubMeta
want TagMBIDs
}{
{"vorbis", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"musicbrainz_albumid": "rel-1", "musicbrainz_artistid": "ar-1", "musicbrainz_albumartistid": "aa-1",
"musicbrainz_releasegroupid": "rg-1",
// Picard calls the recording MUSICBRAINZ_TRACKID in Vorbis, the
// name mbz gives the release track.
"musicbrainz_trackid": "rec-1", "musicbrainz_releasetrackid": "reltrack-1",
}}, all},
// dhowden leaves ID3v2.4's terminating NUL on TXXX values, and
// Postgres rejects NULs in text (SQLSTATE 22021).
{"id3v2", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"TXXX": txxx("MusicBrainz Album Id", "rel-1\x00"),
"TXXX_0": txxx("MusicBrainz Artist Id", "ar-1"),
"TXXX_1": txxx("MusicBrainz Album Artist Id", "aa-1\x00"),
"TXXX_2": txxx("MusicBrainz Release Group Id", "rg-1\x00"),
"TXXX_3": txxx("MusicBrainz Release Track Id", "reltrack-1\x00"),
"UFID": &tag.UFID{Provider: "http://musicbrainz.org", Identifier: []byte("rec-1")},
}}, all},
{"mp4", stubMeta{format: tag.MP4, raw: map[string]interface{}{
"MusicBrainz Album Id": "rel-1", "MusicBrainz Artist Id": "ar-1", "MusicBrainz Album Artist Id": "aa-1",
"MusicBrainz Release Group Id": "rg-1", "MusicBrainz Track Id": "rec-1", "MusicBrainz Release Track Id": "reltrack-1",
}}, all},
{"untagged", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{}}, TagMBIDs{}},
// dhowden never produces uppercase Vorbis keys; a lookup by the
// uppercase name would find nothing in a real file.
{"uppercase vorbis keys", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"MUSICBRAINZ_ALBUMID": "wrong-case", "MUSICBRAINZ_ARTISTID": "wrong-case",
}}, TagMBIDs{}},
{"a vorbis release track is not a recording", stubMeta{format: tag.VORBIS, raw: map[string]interface{}{
"musicbrainz_releasetrackid": "reltrack-1",
}}, TagMBIDs{}},
// Some Picard configs write collaborations NUL-separated; Minstrel
// uses the primary artist, so the first value wins.
{"id3 multi-value takes the first", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"TXXX": txxx("MusicBrainz Artist Id", "primary-mbid\x00collab-mbid"),
}}, TagMBIDs{Artist: "primary-mbid"}},
// A value that is only NULs is absent, not a NUL string for Postgres.
{"id3 value of only NULs", stubMeta{format: tag.ID3v2_4, raw: map[string]interface{}{
"TXXX": txxx("MusicBrainz Album Id", "\x00\x00"),
}}, TagMBIDs{}},
} {
t.Run(tc.name, func(t *testing.T) {
if got := ReadTagMBIDs(tc.meta); got != tc.want {
t.Errorf("got %+v\nwant %+v", got, tc.want)
}
})
}
}
// 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)
}
}
}