From dbe0e79f54a8f94350bc17050662d30da6cc6e6d Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Thu, 30 Apr 2026 17:01:50 -0400 Subject: [PATCH] fix(lidarr): wrap MBID-lookup decode errors with ErrInvalidPayload + fill test gaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Decode errors in LookupArtist/AlbumByMBID now wrap ErrInvalidPayload so callers can errors.Is the same way they do against the M5a methods. - Tighten TestLookupAlbumByMBID_BadJSON to assert on ErrInvalidPayload. - Add empty-mbid rejection tests for both methods. - Add TestLookupArtistByMBID_EmptyArrayReturnsErrNotFound (only the album variant had the test before — symmetric coverage now). - Normalize the network-error test's api key to key123 for consistency with the rest of delete_test.go. --- internal/lidarr/delete_test.go | 42 +++++++++++++++++++++++++++++++--- internal/lidarr/lookup_mbid.go | 4 ++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/internal/lidarr/delete_test.go b/internal/lidarr/delete_test.go index 78d6789c..db8d3822 100644 --- a/internal/lidarr/delete_test.go +++ b/internal/lidarr/delete_test.go @@ -86,8 +86,44 @@ func TestLookupAlbumByMBID_BadJSON(t *testing.T) { defer srv.Close() _, err := c.LookupAlbumByMBID(context.Background(), "x") - if err == nil { - t.Fatal("err = nil, want decode error") + if !errors.Is(err, ErrInvalidPayload) { + t.Errorf("err = %v, want ErrInvalidPayload", err) + } +} + +func TestLookupAlbumByMBID_EmptyMBIDRejected(t *testing.T) { + c, srv := newTestClient(func(http.ResponseWriter, *http.Request) { + t.Error("server should not be called with empty mbid") + }) + defer srv.Close() + + if _, err := c.LookupAlbumByMBID(context.Background(), ""); err == nil { + t.Error("err = nil, want empty-mbid rejection") + } +} + +func TestLookupArtistByMBID_EmptyArrayReturnsErrNotFound(t *testing.T) { + c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("[]")) + }) + defer srv.Close() + + _, err := c.LookupArtistByMBID(context.Background(), "x") + if !errors.Is(err, ErrNotFound) { + t.Errorf("err = %v, want ErrNotFound", err) + } +} + +func TestLookupArtistByMBID_EmptyMBIDRejected(t *testing.T) { + c, srv := newTestClient(func(http.ResponseWriter, *http.Request) { + t.Error("server should not be called with empty mbid") + }) + defer srv.Close() + + if _, err := c.LookupArtistByMBID(context.Background(), ""); err == nil { + t.Error("err = nil, want empty-mbid rejection") } } @@ -158,7 +194,7 @@ func TestDeleteAlbum_5xxReturnsErrServerError(t *testing.T) { func TestDeleteAlbum_NetworkErrorReturnsErrUnreachable(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) srv.Close() // server is closed; client should fail to connect - c := NewClient(srv.URL, "test-key") + c := NewClient(srv.URL, "key123") err := c.DeleteAlbum(context.Background(), 42, true, true) if !errors.Is(err, ErrUnreachable) { diff --git a/internal/lidarr/lookup_mbid.go b/internal/lidarr/lookup_mbid.go index 89e839c8..ecfc0e08 100644 --- a/internal/lidarr/lookup_mbid.go +++ b/internal/lidarr/lookup_mbid.go @@ -22,7 +22,7 @@ func (c *Client) LookupArtistByMBID(ctx context.Context, mbid string) (LidarrArt var rows []LidarrArtist if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil { - return LidarrArtist{}, fmt.Errorf("lidarr: decode artist: %w", err) + return LidarrArtist{}, fmt.Errorf("%w: decode artist: %v", ErrInvalidPayload, err) } if len(rows) == 0 { return LidarrArtist{}, ErrNotFound @@ -45,7 +45,7 @@ func (c *Client) LookupAlbumByMBID(ctx context.Context, mbid string) (LidarrAlbu var rows []LidarrAlbum if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil { - return LidarrAlbum{}, fmt.Errorf("lidarr: decode album: %w", err) + return LidarrAlbum{}, fmt.Errorf("%w: decode album: %v", ErrInvalidPayload, err) } if len(rows) == 0 { return LidarrAlbum{}, ErrNotFound