package plugin import ( "regexp" "strings" "testing" "gitea.elara.ws/Hazel/music-kraken/internal/common" "gitea.elara.ws/Hazel/music-kraken/internal/data" ) func extractNameTest(s string) string { parts := strings.Split(s, "/") lastPart := parts[len(parts)-1] hyphenParts := strings.Split(lastPart, "-") result := strings.Join(hyphenParts[:len(hyphenParts)-1], " ") return result } type MusifyTest struct { } func (m MusifyTest) Name() string { return "MusifyTest" } func (m MusifyTest) Regex() *regexp.Regexp { return regexp.MustCompile(`(?i)https?://musify\.club/(artist|release|track)/[a-z\-0-9]+`) } func (m MusifyTest) RegexArtist() *regexp.Regexp { return regexp.MustCompile(`(?i)https?://musify\.club/artist/[a-z\-0-9]+`) } func (m MusifyTest) RegexAlbum() *regexp.Regexp { return regexp.MustCompile(`(?i)https?://musify\.club/release/[a-z\-0-9]+`) } func (m MusifyTest) RegexSong() *regexp.Regexp { return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`) } func (m *MusifyTest) Init() { } func (m MusifyTest) FetchSong(source data.Source) (data.Song, error) { return data.Song{ Name: extractNameTest(source.Url), }, nil } func (m MusifyTest) FetchAlbum(source data.Source) (data.Album, error) { return data.Album{ Name: extractNameTest(source.Url), }, nil } func (m MusifyTest) FetchArtist(source data.Source) (data.Artist, error) { return data.Artist{ Name: extractNameTest(source.Url), }, nil } func (m MusifyTest) Search(query common.Query) ([]data.MusicObject, error) { return []data.MusicObject{}, nil } func TestRegister(t *testing.T) { if RegisterPlugin(&MusifyTest{}) != nil { t.Errorf(`registering first plugin shouldn't return an error`) } if RegisterPlugin(&MusifyTest{}) == nil { t.Errorf(`registering same plugin twice should return an error`) } if len(namePlugins) != 1 { t.Errorf(`%d plugins where registered`, len(namePlugins)) } if len(NameSourceType) != 1 { t.Errorf(`%d source types were registered`, len(NameSourceType)) } } func TestFetchSong(t *testing.T) { RegisterPlugin(&MusifyTest{}) s, err := Fetch(data.Source{ Url: "https://musify.club/track/linkin-park-in-the-end-3058", }) if err != nil { t.Error(err) } song, ok := s.(data.Song) if !ok { t.Errorf(`fetch should have returned song`) } if song.UnifiedName != "linkin park in the end" { t.Errorf(`didn't correctly parse the name`) } } func TestFetchAlbum(t *testing.T) { RegisterPlugin(&MusifyTest{}) a, err := Fetch(data.Source{ Url: "https://musify.club/release/linkin-park-hybrid-theory-2000-188", }) if err != nil { t.Error(err) } album, ok := a.(data.Album) if !ok { t.Errorf(`fetch should have returned song`) } if album.UnifiedName != "linkin park hybrid theory 2000" { t.Errorf(`didn't correctly parse the name`) } } func TestFetchArtist(t *testing.T) { RegisterPlugin(&MusifyTest{}) a, err := Fetch(data.Source{ Url: "https://musify.club/artist/linkin-park-5", }) if err != nil { t.Error(err) } artist, ok := a.(data.Artist) if !ok { t.Errorf(`fetch should have returned song`) } if artist.UnifiedName != "linkin park" { t.Errorf(`didn't correctly parse the name`) } } func TestFetchWrongUrl(t *testing.T) { RegisterPlugin(&MusifyTest{}) _, err := Fetch(data.Source{ Url: "https://musify.club/", }) if err == nil { t.Errorf(`trying to fetch an unknown url should result in error`) } } func TestNonExistentSourceType(t *testing.T) { RegisterPlugin(&MusifyTest{}) _, err := Fetch(data.Source{ Url: "https://musify.club/", SourceType: &data.SourceType{ Name: "doesn't exist", }, }) if err == nil { t.Errorf(`trying to fetch a not recognized source type should result in error`) } }