diff --git a/tests/test_playlist_command.py b/tests/test_playlist_command.py index 00046c6..635771a 100644 --- a/tests/test_playlist_command.py +++ b/tests/test_playlist_command.py @@ -561,8 +561,18 @@ async def test_retry_recovers_review_tracks_end_to_end( assert "2/2" in channel.sent[-1][0] +@pytest.mark.parametrize( + ("request_text", "lookup_term"), + [ + ("Halo", "Halo Depeche Mode"), + ("Halo | Violator Depeche Mode", "Violator Depeche Mode"), + ], +) async def test_match_assigns_the_chosen_album_end_to_end( - playlist_command: CommandCase, monkeypatch: pytest.MonkeyPatch + playlist_command: CommandCase, + monkeypatch: pytest.MonkeyPatch, + request_text: str, + lookup_term: str, ) -> None: """Catch an album choice that never reaches the import.""" from datetime import UTC, datetime @@ -605,8 +615,25 @@ async def test_match_assigns_the_chosen_album_end_to_end( return "mb-album" monkeypatch.setattr(cog, "_download_choice", choose) + original_call = cog._arr_call + + async def check_lookup( + kind: str, + path: str, + *, + body: dict[str, Any] | None = None, + params: dict[str, str] | None = None, + method: str | None = None, + ) -> object: + if (kind, path) == ("music", "album/lookup"): + assert params == {"term": lookup_term} + return await original_call( + kind, path, body=body, params=params, method=method + ) + + monkeypatch.setattr(cog, "_arr_call", check_lookup) edge.ready = set() - await Watch.playlist_match.callback(cog, ctx, song="Halo") + await Watch.playlist_match.callback(cog, ctx, song=request_text) await asyncio.gather(*cog._pollers) job = (await cog.config.pending_jobs())[key] assert job["tracks"][0]["album_id"] == 7 diff --git a/watch/watch.py b/watch/watch.py index 702ed02..cc34255 100644 --- a/watch/watch.py +++ b/watch/watch.py @@ -1639,15 +1639,22 @@ class Watch(commands.Cog): ) async def _lidarr_album( - self, ctx: commands.Context, track: dict[str, Any] + self, + ctx: commands.Context, + track: dict[str, Any], + album_query: str = "", ) -> dict[str, Any] | None: """Ask the requester which Lidarr album a song belongs to.""" title = discord.utils.escape_markdown( str(track.get("title", "song")) ) term = ( - f"{track.get('title', '')} {track.get('artist', '')}" - ).strip() + album_query + or ( + f"{track.get('title', '')} " + f"{track.get('artist', '')}" + ).strip() + ) try: candidates = await self._arr_call( "music", "album/lookup", params={"term": term} @@ -1690,18 +1697,20 @@ class Watch(commands.Cog): async def playlist_match( self, ctx: commands.Context, *, song: str = "" ) -> None: - """Choose the Lidarr album for a reviewed song.""" + """Choose a Lidarr album with an optional album search term.""" if not await self.can_download(ctx.author): await ctx.send( "Download permission is required; ask " "the owner to use `request allow`." ) return - query = song.strip().casefold() - if len(query) < 2: # noqa: PLR2004 (a useful search) + track_query, separator, album_query = song.partition(" | ") + query = track_query.strip().casefold() + album_query = album_query.strip() + if len(query) < 2 or (separator and not album_query): # noqa: PLR2004 await ctx.send( f"Use `{ctx.clean_prefix}importplaylist " - "match `." + "match [ | ]`." ) return jobs = self.background() @@ -1721,7 +1730,7 @@ class Watch(commands.Cog): "again in a minute." ) return - album = await self._lidarr_album(ctx, track) + album = await self._lidarr_album(ctx, track, album_query) if album is None: return title = discord.utils.escape_markdown(