Compare commits

..

2 Commits

Author SHA1 Message Date
Hazel Noack
ee4d476b44 testing some error handling 2025-10-07 14:51:38 +02:00
Hazel Noack
49cc689a89 testing native go plugins 2025-10-07 14:46:02 +02:00
3 changed files with 185 additions and 4 deletions

View File

@ -24,9 +24,13 @@ type Plugin interface {
var namePlugins = map[string]Plugin{}
var nameSourceType = map[string]data.SourceType{}
func RegisterPlugin(plugin Plugin) {
func RegisterPlugin(plugin Plugin) error {
name := plugin.Name()
if _, ok := namePlugins[name]; ok {
return errors.New("plugin " + name + " is already registered")
}
nameSourceType[name] = data.SourceType{
Name: name,
Regex: plugin.Regex(),
@ -36,10 +40,14 @@ func RegisterPlugin(plugin Plugin) {
}
namePlugins[name] = plugin
return nil
}
func compileSourceType(source data.Source) (data.Source, error) {
if source.SourceType != nil {
if _, ok := namePlugins[source.SourceType.Name]; !ok {
return source, errors.New("source type " + source.SourceType.Name + " not found")
}
return source, nil
}

View File

@ -0,0 +1,170 @@
package plugin
import (
"regexp"
"strings"
"testing"
"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) 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 string) ([]data.MusicObject, error) {
panic("unimplemented")
}
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`)
}
}

View File

@ -11,7 +11,10 @@ func main() {
fmt.Println("music kraken")
plugin.RegisterPlugin(plugin.Musify{})
fmt.Println(plugin.Fetch(data.Source{
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
}))
a, _ := plugin.Fetch(data.Source{
Url: "https://musify.club/artist/linkin-park-5",
})
artist := a.(data.Artist)
fmt.Println(artist.UnifiedName)
}