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