From b4d0004bbe43d3ff5d51c51d38c26cdfa07b32bd Mon Sep 17 00:00:00 2001 From: Thanvish Date: Thu, 19 Nov 2026 18:13:44 +0530 Subject: [PATCH] fix(tidal): honour search_limit in album and track query searches The tidal plugin's search_albums_by_query and search_tracks_by_query called api.search_results() without respecting the search_limit configured by the MetadataSourcePlugin base class. This caused Tidal to return 20+ candidates even when the user set e.g. `tidal: search_limit: 1`. Fix: read self.config["search_limit"].get(int) in both methods and slice the relationship list before resolving album/track IDs. Add focused tests for both album and track search with search_limit: 1 to guard against regressions. Closes: #6770 Co-Authored-By: Claude Sonnet 5 --- beetsplug/tidal/__init__.py | 6 +++-- test/plugins/test_tidal.py | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/beetsplug/tidal/__init__.py b/beetsplug/tidal/__init__.py index d98dea2650..b434e4f9df 100644 --- a/beetsplug/tidal/__init__.py +++ b/beetsplug/tidal/__init__.py @@ -211,9 +211,10 @@ def search_tracks_by_query(self, query: str) -> Iterable[TrackInfo]: for item in search_doc.get("included", []) if item["type"] == "artists" } + limit = self.config["search_limit"].get(int) for track_rel in search_doc["data"][0]["relationships"]["tracks"][ "data" - ]: + ][:limit]: if track := track_by_id.get(track_rel["id"]): yield self._get_track_info(track, artist_by_id=artist_by_id) else: @@ -233,11 +234,12 @@ def search_albums_by_query(self, query: str) -> Iterable[AlbumInfo]: if not search_doc["data"]: return + limit = self.config["search_limit"].get(int) album_ids = [ album_rel["id"] for album_rel in search_doc["data"][0]["relationships"]["albums"][ "data" - ] + ][:limit] ] yield from filter(None, self.search_albums_by_ids(tidal_ids=album_ids)) diff --git a/test/plugins/test_tidal.py b/test/plugins/test_tidal.py index 9ad86039ce..142ec02e02 100644 --- a/test/plugins/test_tidal.py +++ b/test/plugins/test_tidal.py @@ -709,6 +709,36 @@ def test_candidates_with_query_fallback(self): assert len(candidates) == 1 assert candidates[0].album == "Query Album" + def test_search_albums_respects_search_limit(self): + """search_albums_by_query must honour the configured search_limit.""" + # Return three album IDs from the API; config limit is 1. + self.tidal.config["search_limit"] = 1 + self.tidal.api.search_results = Mock( + return_value=_make_search_doc("albums", ["1", "2", "3"]) + ) + + track = _make_track("101", "Album Track", "PT3M", "ISRC001", ["1001"]) + album, track_lookup, artist_lookup = _make_album( + "1", "Limited Album", [track], ["1001"] + ) + self.tidal.api.get_albums = Mock( + return_value={ + "data": [album], + "included": [*artist_lookup.values(), *track_lookup.values()], + } + ) + + candidates = list(self.tidal.search_albums_by_query("some query")) + + # Only album "1" should have been requested despite three IDs returned + call_args = self.tidal.api.get_albums.call_args + requested_ids = call_args.kwargs.get( + "ids", call_args.args[0] if call_args.args else [] + ) + assert requested_ids == ["1"] + assert len(candidates) == 1 + assert candidates[0].album == "Limited Album" + def test_album_search_handles_empty_response(self): self.tidal.api.search_results = Mock(return_value={"data": []}) self.tidal.api.get_albums = Mock() @@ -771,6 +801,26 @@ def test_track_search_handles_empty_response(self): assert candidates == [] + def test_search_tracks_respects_search_limit(self): + """search_tracks_by_query must honour the configured search_limit.""" + self.tidal.config["search_limit"] = 1 + + # Return two tracks from the search doc; limit should allow only one. + track1 = _make_track("501", "Track One", "PT3M", "ISRC501", ["1001"]) + track2 = _make_track("502", "Track Two", "PT3M", "ISRC502", ["1001"]) + artist = _make_artist("1001", "Some Artist") + self.tidal.api.search_results = Mock( + return_value={ + **_make_search_doc("tracks", ["501", "502"]), + "included": [track1, track2, artist], + } + ) + + results = list(self.tidal.search_tracks_by_query("some query")) + + assert len(results) == 1 + assert results[0].title == "Track One" + class TestStaticHelpers: """Tests for static helper methods."""