feat: add MusicBrainz artist-ID resolver

Add ResolveArtistMBID to query the MB artist search endpoint and return the
first matching artist ID, with httptest-backed tests for match, no-match,
HTTP error, and invalid-JSON paths.
This commit is contained in:
2026-07-19 22:16:43 +03:00
parent 85c42ec858
commit 40c4240693
3 changed files with 156 additions and 4 deletions

View File

@@ -0,0 +1,105 @@
package musicbrainz
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// newTestClient is defined in sync_test.go (signature: func newTestClient(serverURL string) *MusicBrainzClient).
// contains is defined in model_test.go (signature: func contains(s []string, want string) bool).
func TestResolveArtistMBID_MatchFound(t *testing.T) {
var gotPath string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.RequestURI()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(mbArtistSearchResult{
Artists: []struct {
ID string `json:"id"`
Name string `json:"name"`
Score int `json:"score"`
}{
{ID: "f27a7a7e-5a47-4cd5-afbe-6b7b01672b3b", Name: "Radiohead", Score: 100},
{ID: "another-id", Name: "Radiohead (Tribute)", Score: 80},
},
})
}))
defer server.Close()
client := newTestClient(server.URL)
mbid, err := client.ResolveArtistMBID(context.Background(), "Radiohead")
if err != nil {
t.Fatalf("ResolveArtistMBID() error = %v", err)
}
want := "f27a7a7e-5a47-4cd5-afbe-6b7b01672b3b"
if mbid != want {
t.Errorf("ResolveArtistMBID() = %q, want %q", mbid, want)
}
// Verify the request used the expected query path and JSON format.
if gotPath == "" || !strings.Contains(gotPath, "/artist?") {
t.Errorf("ResolveArtistMBID() requested path = %q, want /artist?...", gotPath)
}
if !strings.Contains(gotPath, "query=artist%3ARadiohead") {
t.Errorf("ResolveArtistMBID() query missing artist name, got %q", gotPath)
}
if !strings.Contains(gotPath, "fmt=json") {
t.Errorf("ResolveArtistMBID() query missing fmt=json, got %q", gotPath)
}
}
func TestResolveArtistMBID_NoMatch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(mbArtistSearchResult{Artists: []struct {
ID string `json:"id"`
Name string `json:"name"`
Score int `json:"score"`
}{}})
}))
defer server.Close()
client := newTestClient(server.URL)
_, err := client.ResolveArtistMBID(context.Background(), "Nonexistent Artist 12345")
if err == nil {
t.Fatal("ResolveArtistMBID() expected error for no match, got nil")
}
}
func TestResolveArtistMBID_HTTPError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Service Unavailable"))
}))
defer server.Close()
client := newTestClient(server.URL)
_, err := client.ResolveArtistMBID(context.Background(), "Radiohead")
if err == nil {
t.Fatal("ResolveArtistMBID() expected error on HTTP failure, got nil")
}
}
func TestResolveArtistMBID_InvalidJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`not valid json`))
}))
defer server.Close()
client := newTestClient(server.URL)
_, err := client.ResolveArtistMBID(context.Background(), "Radiohead")
if err == nil {
t.Fatal("ResolveArtistMBID() expected parse error, got nil")
}
}