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) } } }