diff --git a/tests/test_music_release_preference.py b/tests/test_music_release_preference.py new file mode 100644 index 0000000..ee6e941 --- /dev/null +++ b/tests/test_music_release_preference.py @@ -0,0 +1,113 @@ +"""Prefer cacheable release groups when a track resolves to several albums.""" + +from typing import Any + + +def album( + album_id: int, + title: str, + kind: str | None, + secondary: list[str] | None = None, +) -> dict[str, Any]: + """Build a Lidarr album resource as album/lookup returns it.""" + return { + "id": album_id, + "foreignAlbumId": f"mbid-{album_id}", + "title": title, + "albumType": kind, + "secondaryTypes": secondary or [], + "artist": {"artistName": "Avicii"}, + } + + +def choose( + track_title: str, candidates: list[dict[str, Any]] +) -> dict[str, Any] | None: + """Select an album for a track by the artist above.""" + from watch.music import choose_album + + source = {"title": track_title, "artist": "Avicii"} + return choose_album(source, candidates) + + +def test_a_studio_album_wins_over_a_single_with_the_same_title() -> None: + """Uncached singles stall downloads; the album is the safe target.""" + candidates = [ + album(1, "Wake Me Up", "Single"), + album(2, "Wake Me Up", "Album"), + ] + selected = choose("Wake Me Up", candidates) + assert selected is not None + assert selected["id"] == 2 + + +def test_an_ep_wins_over_a_single_but_not_over_an_album() -> None: + """Rank album, then EP, then single.""" + candidates = [ + album(1, "Levels", "Single"), + album(2, "Levels", "EP"), + ] + assert choose("Levels", candidates)["id"] == 2 + + candidates.append(album(3, "Levels", "Album")) + assert choose("Levels", candidates)["id"] == 3 + + +def test_a_plain_album_wins_over_a_compilation_or_live_edition() -> None: + """A studio album imports more reliably than a compilation.""" + candidates = [ + album(1, "True", "Album", ["Compilation"]), + album(2, "True", "Album", ["Live"]), + album(3, "True", "Album"), + ] + assert choose("True", candidates)["id"] == 3 + + +def test_a_single_wins_over_a_remix_edition() -> None: + """A remix collection is worse than the plain single for one track.""" + candidates = [ + album(1, "Wake Me Up", "Single", ["Remix"]), + album(2, "Wake Me Up", "Single"), + ] + assert choose("Wake Me Up", candidates)["id"] == 2 + + +def test_equally_ranked_albums_stay_ambiguous() -> None: + """Never guess between two genuine candidates.""" + candidates = [ + album(1, "True", "Album"), + album(2, "True", "Album"), + ] + assert choose("True", candidates) is None + + +def test_albums_without_a_release_type_stay_ambiguous() -> None: + """Keep the old behaviour for lookups that carry no type.""" + candidates = [ + album(1, "Violator", None), + album(2, "Violator", None), + ] + assert choose("Violator", candidates) is None + + +def test_a_lone_single_is_still_selected() -> None: + """Preference is a tie-break, not a filter.""" + selected = choose("Wake Me Up", [album(1, "Wake Me Up", "Single")]) + assert selected is not None + assert selected["id"] == 1 + + +def test_release_preference_never_overrides_the_artist_check() -> None: + """A better-ranked wrong artist is still rejected.""" + candidates = [ + album(1, "True", "Album"), + { + "id": 2, + "foreignAlbumId": "mbid-2", + "title": "True", + "albumType": "Album", + "secondaryTypes": [], + "artist": {"artistName": "A Tribute Band"}, + }, + ] + assert choose("True", candidates)["id"] == 1 diff --git a/watch/music.py b/watch/music.py index 8716093..1f82128 100644 --- a/watch/music.py +++ b/watch/music.py @@ -68,18 +68,64 @@ def appears_on(artist: str, album: dict[str, Any]) -> bool: ) in artist_credits(artist) +# A track often resolves to several release groups: the single it was sold as +# and the album it also appears on. Uncached singles are the ones that stall in +# TorBox and push Lidarr into grabbing other releases, so a studio album is the +# safer target and ranks first. +RELEASE_TYPE_ORDER = {"album": 0, "ep": 1, "single": 2} +DERIVATIVE_RELEASE_TYPES = frozenset( + { + "compilation", + "live", + "remix", + "djmix", + "mixtapestreet", + "soundtrack", + "spokenword", + "interview", + "audiobook", + "audiodrama", + } +) + + +def release_rank(album: dict[str, Any]) -> tuple[int, int]: + """Rank release groups so a cacheable album beats a single.""" + kind = normalized(str(album.get("albumType") or "")) + secondary = { + normalized(str(value)) + for value in (album.get("secondaryTypes") or []) + } + derivative = 1 if secondary & DERIVATIVE_RELEASE_TYPES else 0 + order = RELEASE_TYPE_ORDER.get(kind, len(RELEASE_TYPE_ORDER)) + return (order, derivative) + + def choose_album( source: dict[str, str], candidates: list[dict[str, Any]] ) -> dict[str, Any] | None: - """Require one canonical release group by the credited artist.""" + """Pick one canonical release group by the credited artist. + + Title and credit must match exactly. When that still leaves several + release groups, prefer the most cacheable one: album before EP, EP + before single, and a plain edition before a compilation, live or remix + edition. A tie between equally good candidates stays ambiguous so the + requester chooses. + """ matches = [ a for a in candidates if appears_on(source["artist"], a) and album_title(a["title"]) == album_title(source["title"]) ] - ids = {a["foreignAlbumId"] for a in matches} - return matches[0] if len(ids) == 1 else None + unique = {a["foreignAlbumId"]: a for a in matches} + if not unique: + return None + if len(unique) == 1: + return matches[0] + best = min(release_rank(a) for a in unique.values()) + winners = [a for a in unique.values() if release_rank(a) == best] + return winners[0] if len(winners) == 1 else None def find_audio(