feat(discover): derive newest + most-popular album per artist

_derive_albums now also picks the artist's most-played album (Last.fm,
matched into the browsed discography by MBID then normalized title),
alongside the newest. Same release -> one row tagged newest,popular.
albumReason persisted; run_discovery threads an optional popularity provider.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-14 23:39:18 +02:00
parent efdd592761
commit 194b4911c7
2 changed files with 106 additions and 10 deletions
+66
View File
@@ -12,6 +12,21 @@ def _album_rows(conn):
return cur.fetchall()
def _reasons(conn):
with conn.cursor() as cur:
cur.execute('SELECT "rgMbid", "albumReason" FROM "DiscoverySuggestion" '
'WHERE kind = \'album\' ORDER BY "rgMbid"')
return dict(cur.fetchall())
class FakePopularity:
"""Maps artist NAME -> [[album_name, mbid_or_None, playcount], …] (Last.fm shape)."""
def __init__(self, data):
self._d = data
def top_albums(self, conn, name):
return self._d.get(name, [])
def test_derives_most_recent_core_album_for_surfaced_artist(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
@@ -37,6 +52,57 @@ def test_album_skips_release_groups_already_in_library(conn):
assert _album_rows(conn) == [("c1", "rg-fresh", "Fresh", "Album")] # owned rg excluded
def test_derives_newest_and_most_popular_albums(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
browser = FakeMbBrowser(releases={"c1": [
ReleaseGroupInfo("rg-hit", "Big Hit", "Album", (), "2005-01-01"),
ReleaseGroupInfo("rg-new", "Latest", "Album", (), "2022-01-01"),
]})
pop = FakePopularity({"Cand": [["Big Hit", "rg-hit", 90000], ["Latest", "rg-new", 100]]})
result = run_discovery(conn, [src], browser, DiscoveryConfig(albums_per_artist=1), popularity=pop)
assert result.albums == 2
assert _reasons(conn) == {"rg-new": "newest", "rg-hit": "popular"}
def test_newest_and_popular_the_same_album_is_one_row(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
browser = FakeMbBrowser(releases={"c1": [
ReleaseGroupInfo("rg-only", "Only", "Album", (), "2022-01-01"),
]})
pop = FakePopularity({"Cand": [["Only", "rg-only", 90000]]})
result = run_discovery(conn, [src], browser, DiscoveryConfig(albums_per_artist=1), popularity=pop)
assert result.albums == 1
assert _reasons(conn) == {"rg-only": "newest,popular"}
def test_popular_matches_by_title_when_lastfm_mbid_missing_and_skips_owned(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
insert_monitored_release(conn, artist_mbid="c1", rg_mbid="rg-owned", album="Owned Hit")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
browser = FakeMbBrowser(releases={"c1": [
ReleaseGroupInfo("rg-owned", "Owned Hit", "Album", (), "2010-01-01"), # excluded (owned)
ReleaseGroupInfo("rg-match", "Second Hit", "Album", (), "2012-01-01"),
ReleaseGroupInfo("rg-new", "Latest", "Album", (), "2022-01-01"),
]})
# top album is owned (skipped), next has no mbid -> matched by title into core
pop = FakePopularity({"Cand": [["Owned Hit", "rg-owned", 99999], ["Second Hit", None, 80000]]})
run_discovery(conn, [src], browser, DiscoveryConfig(albums_per_artist=1), popularity=pop)
assert _reasons(conn) == {"rg-new": "newest", "rg-match": "popular"}
def test_no_popularity_provider_yields_newest_only(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
browser = FakeMbBrowser(releases={"c1": [
ReleaseGroupInfo("rg-old", "Old", "Album", (), "2005-01-01"),
ReleaseGroupInfo("rg-new", "New", "Album", (), "2022-01-01"),
]})
run_discovery(conn, [src], browser, DiscoveryConfig(albums_per_artist=1)) # popularity=None
assert _reasons(conn) == {"rg-new": "newest"}
def test_albums_only_excludes_singles_and_eps_by_default(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})