package library import ( "bytes" "testing" "github.com/dhowden/tag" ) func TestWithoutGuests(t *testing.T) { tests := []struct{ credit, want string }{ {"Low Tide Choir feat. Ruby Calloway", "Low Tide Choir"}, {"Low Tide Choir ft. Ruby Calloway", "Low Tide Choir"}, {"Low Tide Choir featuring Ruby Calloway", "Low Tide Choir"}, {"Low Tide Choir (feat. Ruby Calloway)", "Low Tide Choir"}, {"Low Tide Choir [ft Ruby Calloway]", "Low Tide Choir"}, {"Low Tide Choir FEAT. Ruby Calloway & Mae", "Low Tide Choir"}, // Names that only look like a credit stay whole. {"Little Feat", "Little Feat"}, {"Little Feat featuring Bonnie Raitt", "Little Feat"}, {"Daft Punk", "Daft Punk"}, {"Soft Cell", "Soft Cell"}, {"Featuring Fools", "Featuring Fools"}, {"Simon & Garfunkel", "Simon & Garfunkel"}, {"St. Paul & The Broken Bones", "St. Paul & The Broken Bones"}, } for _, tc := range tests { if got := withoutGuests(tc.credit); got != tc.want { t.Errorf("withoutGuests(%q) = %q, want %q", tc.credit, got, tc.want) } } } // id3Meta is an ID3v2 file's tags as dhowden/tag presents them. type id3Meta struct { tag.Metadata artist string raw map[string]interface{} } func (m id3Meta) Format() tag.Format { return tag.ID3v2_4 } func (m id3Meta) FileType() tag.FileType { return tag.MP3 } func (m id3Meta) Artist() string { return m.artist } func (m id3Meta) Raw() map[string]interface{} { return m.raw } func TestReadArtistCredit_ID3(t *testing.T) { tests := []struct { name string artist string raw map[string]interface{} mbid string want artistCredit }{ { name: "a guest is cut from the credit", artist: "Low Tide Choir feat. Ruby Calloway", mbid: "id-choir", want: artistCredit{Name: "Low Tide Choir", MBID: "id-choir", Credit: "Low Tide Choir feat. Ruby Calloway"}, }, { name: "a collaboration the file lists is filed under its first artist", artist: "Paper Lanterns & Ruby Calloway", raw: map[string]interface{}{"TXXX": &tag.Comm{Description: "ARTISTS", Text: "Paper Lanterns\x00Ruby Calloway"}}, want: artistCredit{Name: "Paper Lanterns", Credit: "Paper Lanterns & Ruby Calloway"}, }, { name: "an & the file does not list is one artist", artist: "St. Paul & The Broken Bones", raw: map[string]interface{}{"TXXX": &tag.Comm{Description: "ARTISTS", Text: "St. Paul & The Broken Bones"}}, want: artistCredit{Name: "St. Paul & The Broken Bones"}, }, { name: "a v2.3 list has no separator, so a slash stays in the name", artist: "AC/DC", raw: map[string]interface{}{"TXXX_0": &tag.Comm{Description: "Artists", Text: "AC/DC"}}, want: artistCredit{Name: "AC/DC"}, }, { name: "no artist at all", want: artistCredit{Name: "Unknown Artist"}, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := readArtistCredit(id3Meta{artist: tc.artist, raw: tc.raw}, nil, TagMBIDs{Artist: tc.mbid}) if got != tc.want { t.Errorf("readArtistCredit = %+v, want %+v", got, tc.want) } }) } } // A FLAC credited to two artists, as Picard writes it: ARTIST is the credit, // and ARTISTS and MUSICBRAINZ_ARTISTID list the artists in credit order. // dhowden/tag keeps the last of each repeated field, so it alone would file // the track under the second artist's id. func TestReadArtistCredit_FLACListsItsArtists(t *testing.T) { data := buildFLAC(false, "TITLE=Undertow", "ARTIST=Paper Lanterns & Ruby Calloway", "ARTISTS=Paper Lanterns", "ARTISTS=Ruby Calloway", "MUSICBRAINZ_ARTISTID=id-lanterns", "MUSICBRAINZ_ARTISTID=id-ruby", ) rs := bytes.NewReader(data) meta, err := tag.ReadFrom(rs) if err != nil { t.Fatalf("tag.ReadFrom: %v", err) } got := readArtistCredit(meta, rs, ReadTagMBIDs(meta)) want := artistCredit{Name: "Paper Lanterns", MBID: "id-lanterns", Credit: "Paper Lanterns & Ruby Calloway"} if got != want { t.Errorf("readArtistCredit = %+v, want %+v", got, want) } } func TestReadArtistCredit_FLACWithOneArtist(t *testing.T) { data := buildFLAC(false, "ARTIST=Marina & the Kats", "ARTISTS=Marina & the Kats", "MUSICBRAINZ_ARTISTID=id-kats") rs := bytes.NewReader(data) meta, err := tag.ReadFrom(rs) if err != nil { t.Fatalf("tag.ReadFrom: %v", err) } got := readArtistCredit(meta, rs, ReadTagMBIDs(meta)) if want := (artistCredit{Name: "Marina & the Kats", MBID: "id-kats"}); got != want { t.Errorf("readArtistCredit = %+v, want %+v", got, want) } } func TestReadMP4FreeformValues(t *testing.T) { freeform := func(name string, values ...string) []byte { children := [][]byte{ mp4Box("mean", []byte{0, 0, 0, 0}, []byte("com.apple.iTunes")), mp4Box("name", []byte{0, 0, 0, 0}, []byte(name)), } for _, v := range values { children = append(children, mp4Text(v)) } return mp4Box("----", children...) } data := buildMP4(true, freeform("MusicBrainz Artist Id", "id-lanterns"), freeform("ARTISTS", "Paper Lanterns", "Ruby Calloway"), ) got, err := readMP4FreeformValues(bytes.NewReader(data), "artists") if err != nil { t.Fatalf("readMP4FreeformValues: %v", err) } if want := []string{"Paper Lanterns", "Ruby Calloway"}; !equalStrings(got, want) { t.Errorf("ARTISTS = %q, want %q", got, want) } if got, err := readMP4FreeformValues(bytes.NewReader(buildMP4(true)), "ARTISTS"); err != nil || got != nil { t.Errorf("no ARTISTS item: got %q, %v; want nil, nil", got, err) } }