| 1 |
import io |
| 2 |
import json |
| 3 |
import zipfile |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from beets import ui |
| 9 |
from beets.test.helper import ConfigMixin |
| 10 |
from beetsplug.listenbrainz import ListenBrainzPlugin |
| 11 |
|
| 12 |
|
| 13 |
def _make_zip(files: dict[str, str]) -> bytes: |
| 14 |
"""Build an in-memory zip with the given {name: content} entries.""" |
| 15 |
buf = io.BytesIO() |
| 16 |
with zipfile.ZipFile(buf, "w") as zf: |
| 17 |
for name, content in files.items(): |
| 18 |
zf.writestr(name, content) |
| 19 |
return buf.getvalue() |
| 20 |
|
| 21 |
|
| 22 |
class TestListenBrainzPlugin(ConfigMixin): |
| 23 |
@pytest.fixture(scope="class") |
| 24 |
def plugin(self) -> ListenBrainzPlugin: |
| 25 |
self.config["listenbrainz"]["token"] = "test_token" |
| 26 |
self.config["listenbrainz"]["username"] = "test_user" |
| 27 |
return ListenBrainzPlugin() |
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
"search_response, expected_id", |
| 31 |
[([{"id": "id1"}], "id1"), ([], None)], |
| 32 |
ids=["found", "not_found"], |
| 33 |
) |
| 34 |
def test_get_mb_recording_id( |
| 35 |
self, plugin, requests_mock, search_response, expected_id |
| 36 |
): |
| 37 |
requests_mock.get( |
| 38 |
"/ws/2/recording", json={"recordings": search_response} |
| 39 |
) |
| 40 |
track = {"track_metadata": {"track_name": "S", "release_name": "A"}} |
| 41 |
|
| 42 |
assert plugin.get_mb_recording_id(track) == expected_id |
| 43 |
|
| 44 |
def test_get_track_info(self, plugin, requests_mock): |
| 45 |
requests_mock.get( |
| 46 |
"/ws/2/recording/id1?inc=releases", |
| 47 |
json={ |
| 48 |
"title": "T", |
| 49 |
"artist-credit": [], |
| 50 |
"releases": [{"title": "Al", "date": "2023-01"}], |
| 51 |
}, |
| 52 |
) |
| 53 |
|
| 54 |
assert plugin.get_track_info([{"identifier": "id1"}]) == [ |
| 55 |
{ |
| 56 |
"identifier": "id1", |
| 57 |
"title": "T", |
| 58 |
"artist": None, |
| 59 |
"album": "Al", |
| 60 |
"year": "2023", |
| 61 |
} |
| 62 |
] |
| 63 |
|
| 64 |
def test_aggregate_listens_counts_by_mbid(self, plugin): |
| 65 |
tracks = [ |
| 66 |
{ |
| 67 |
"mbid": "m1", |
| 68 |
"artist": "A", |
| 69 |
"name": "S", |
| 70 |
"album": "Al", |
| 71 |
"playcount": 1, |
| 72 |
}, |
| 73 |
{ |
| 74 |
"mbid": "m1", |
| 75 |
"artist": "A", |
| 76 |
"name": "S", |
| 77 |
"album": "Al", |
| 78 |
"playcount": 1, |
| 79 |
}, |
| 80 |
{ |
| 81 |
"mbid": "m1", |
| 82 |
"artist": "A", |
| 83 |
"name": "S", |
| 84 |
"album": "Al", |
| 85 |
"playcount": 1, |
| 86 |
}, |
| 87 |
{ |
| 88 |
"mbid": "m2", |
| 89 |
"artist": "B", |
| 90 |
"name": "T", |
| 91 |
"album": "Bl", |
| 92 |
"playcount": 1, |
| 93 |
}, |
| 94 |
] |
| 95 |
result = plugin._aggregate_listens(tracks) |
| 96 |
by_mbid = {t["mbid"]: t["playcount"] for t in result} |
| 97 |
assert by_mbid == {"m1": 3, "m2": 1} |
| 98 |
|
| 99 |
def test_aggregate_listens_falls_back_to_artist_title_album(self, plugin): |
| 100 |
tracks = [ |
| 101 |
{ |
| 102 |
"mbid": None, |
| 103 |
"artist": "A", |
| 104 |
"name": "S", |
| 105 |
"album": "Al", |
| 106 |
"playcount": 1, |
| 107 |
}, |
| 108 |
{ |
| 109 |
"mbid": "", |
| 110 |
"artist": "A", |
| 111 |
"name": "S", |
| 112 |
"album": "Al", |
| 113 |
"playcount": 1, |
| 114 |
}, |
| 115 |
{ |
| 116 |
"mbid": None, |
| 117 |
"artist": "A", |
| 118 |
"name": "S", |
| 119 |
"album": "Al", |
| 120 |
"playcount": 1, |
| 121 |
}, |
| 122 |
] |
| 123 |
result = plugin._aggregate_listens(tracks) |
| 124 |
assert len(result) == 1 |
| 125 |
assert result[0]["playcount"] == 3 |
| 126 |
|
| 127 |
def test_get_listens_paginates(self, plugin): |
| 128 |
page1 = [ |
| 129 |
{ |
| 130 |
"listened_at": 100 - i, |
| 131 |
"track_metadata": { |
| 132 |
"track_name": f"T{i}", |
| 133 |
"artist_name": "A", |
| 134 |
"release_name": "R", |
| 135 |
}, |
| 136 |
} |
| 137 |
for i in range(5) |
| 138 |
] |
| 139 |
page2 = [ |
| 140 |
{ |
| 141 |
"listened_at": 50 - i, |
| 142 |
"track_metadata": { |
| 143 |
"track_name": f"T{5 + i}", |
| 144 |
"artist_name": "A", |
| 145 |
"release_name": "R", |
| 146 |
}, |
| 147 |
} |
| 148 |
for i in range(3) |
| 149 |
] |
| 150 |
|
| 151 |
call_count = 0 |
| 152 |
|
| 153 |
def mock_request(url, params=None): |
| 154 |
nonlocal call_count |
| 155 |
call_count += 1 |
| 156 |
if call_count == 1: |
| 157 |
return {"payload": {"listens": page1}} |
| 158 |
return {"payload": {"listens": page2}} |
| 159 |
|
| 160 |
with patch.object(plugin, "_make_request", side_effect=mock_request): |
| 161 |
result = plugin.get_listens(count=5) |
| 162 |
|
| 163 |
assert len(result) == 8 |
| 164 |
assert call_count == 2 |
| 165 |
|
| 166 |
def test_get_listens_stops_on_empty_page(self, plugin): |
| 167 |
def mock_request(url, params=None): |
| 168 |
return {"payload": {"listens": []}} |
| 169 |
|
| 170 |
with patch.object(plugin, "_make_request", side_effect=mock_request): |
| 171 |
result = plugin.get_listens() |
| 172 |
|
| 173 |
assert result == [] |
| 174 |
|
| 175 |
def test_get_listens_returns_none_on_api_error(self, plugin): |
| 176 |
def mock_request(url, params=None): |
| 177 |
return None |
| 178 |
|
| 179 |
with patch.object(plugin, "_make_request", side_effect=mock_request): |
| 180 |
result = plugin.get_listens() |
| 181 |
|
| 182 |
assert result is None |
| 183 |
|
| 184 |
def test_get_listens_rejects_both_min_and_max_ts(self, plugin): |
| 185 |
with pytest.raises(ValueError, match="mutually exclusive"): |
| 186 |
plugin.get_listens(min_ts=1, max_ts=2) |
| 187 |
|
| 188 |
def test_get_listens_clamps_count_to_1000(self, plugin): |
| 189 |
calls = [] |
| 190 |
|
| 191 |
def mock_request(url, params=None): |
| 192 |
calls.append(params) |
| 193 |
return {"payload": {"listens": []}} |
| 194 |
|
| 195 |
with patch.object(plugin, "_make_request", side_effect=mock_request): |
| 196 |
plugin.get_listens(count=5000) |
| 197 |
|
| 198 |
assert calls[0]["count"] == 1000 |
| 199 |
|
| 200 |
def test_get_tracks_from_listens_uses_recording_mbid(self, plugin): |
| 201 |
listens = [ |
| 202 |
{ |
| 203 |
"listened_at": 1000, |
| 204 |
"track_metadata": { |
| 205 |
"track_name": "Song", |
| 206 |
"artist_name": "Artist", |
| 207 |
"release_name": "Album", |
| 208 |
"mbid_mapping": { |
| 209 |
"recording_mbid": "rec-mbid-123", |
| 210 |
"release_mbid": "rel-mbid", |
| 211 |
}, |
| 212 |
}, |
| 213 |
} |
| 214 |
] |
| 215 |
tracks = plugin.get_tracks_from_listens(listens) |
| 216 |
assert tracks[0]["mbid"] == "rec-mbid-123" |
| 217 |
assert tracks[0]["playcount"] == 1 |
| 218 |
|
| 219 |
def test_get_tracks_from_listens_no_mbid_mapping(self, plugin): |
| 220 |
listens = [ |
| 221 |
{ |
| 222 |
"listened_at": 1000, |
| 223 |
"track_metadata": { |
| 224 |
"track_name": "Song", |
| 225 |
"artist_name": "Artist", |
| 226 |
"release_name": "Album", |
| 227 |
}, |
| 228 |
} |
| 229 |
] |
| 230 |
tracks = plugin.get_tracks_from_listens(listens) |
| 231 |
assert tracks[0]["mbid"] is None |
| 232 |
|
| 233 |
def test_get_listens_respects_max_total(self, plugin): |
| 234 |
def mock_request(url, params=None): |
| 235 |
count = params.get("count", 5) |
| 236 |
return { |
| 237 |
"payload": { |
| 238 |
"listens": [ |
| 239 |
{"listened_at": 100 - i, "track_metadata": {}} |
| 240 |
for i in range(count) |
| 241 |
] |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
with patch.object(plugin, "_make_request", side_effect=mock_request): |
| 246 |
result = plugin.get_listens(max_total=7) |
| 247 |
|
| 248 |
assert len(result) == 7 |
| 249 |
|
| 250 |
def test_get_tracks_from_listens_flat_structure(self, plugin): |
| 251 |
listens = [ |
| 252 |
{ |
| 253 |
"listened_at": 1000, |
| 254 |
"track_metadata": { |
| 255 |
"track_name": "Song", |
| 256 |
"artist_name": "Artist", |
| 257 |
"release_name": "Album", |
| 258 |
"mbid_mapping": {"recording_mbid": "m1"}, |
| 259 |
}, |
| 260 |
} |
| 261 |
] |
| 262 |
tracks = plugin.get_tracks_from_listens(listens) |
| 263 |
t = tracks[0] |
| 264 |
assert isinstance(t["artist"], str) |
| 265 |
assert isinstance(t["name"], str) |
| 266 |
assert t["album"] == "Album" |
| 267 |
|
| 268 |
def test_get_tracks_from_listens_null_mbid_mapping(self, plugin): |
| 269 |
listens = [ |
| 270 |
{ |
| 271 |
"listened_at": 1000, |
| 272 |
"track_metadata": { |
| 273 |
"track_name": "Song", |
| 274 |
"artist_name": "Artist", |
| 275 |
"release_name": "Album", |
| 276 |
"mbid_mapping": None, |
| 277 |
}, |
| 278 |
} |
| 279 |
] |
| 280 |
tracks = plugin.get_tracks_from_listens(listens) # should not raise |
| 281 |
assert tracks[0]["mbid"] is None |
| 282 |
|
| 283 |
def test_get_tracks_from_listens_prefers_additional_info_recording_mbid( |
| 284 |
self, plugin |
| 285 |
): |
| 286 |
listens = [ |
| 287 |
{ |
| 288 |
"listened_at": 1000, |
| 289 |
"track_metadata": { |
| 290 |
"track_name": "Song", |
| 291 |
"artist_name": "Artist", |
| 292 |
"release_name": "Album", |
| 293 |
"additional_info": {"recording_mbid": "rec-mbid-123"}, |
| 294 |
"mbid_mapping": { |
| 295 |
"recording_mbid": "rec-mbid-456", |
| 296 |
"release_mbid": "rel-mbid", |
| 297 |
}, |
| 298 |
}, |
| 299 |
} |
| 300 |
] |
| 301 |
tracks = plugin.get_tracks_from_listens(listens) |
| 302 |
assert tracks[0]["mbid"] == "rec-mbid-123" |
| 303 |
assert tracks[0]["playcount"] == 1 |
| 304 |
|
| 305 |
def test_import_file_returns_listens(self, plugin, tmp_path): |
| 306 |
listen = { |
| 307 |
"listened_at": 1778946700, |
| 308 |
"track_metadata": {"track_name": "S"}, |
| 309 |
} |
| 310 |
zip_path = tmp_path / "export.zip" |
| 311 |
zip_path.write_bytes( |
| 312 |
_make_zip({"listens/2026.jsonl": json.dumps(listen) + "\n"}) |
| 313 |
) |
| 314 |
|
| 315 |
result = plugin.import_listenbrainz_data_export(zip_path) |
| 316 |
|
| 317 |
assert result == [listen] |
| 318 |
|
| 319 |
def test_import_file_ignores_non_listens_entries(self, plugin, tmp_path): |
| 320 |
listen = {"listened_at": 1778946700, "track_metadata": {}} |
| 321 |
zip_path = tmp_path / "export.zip" |
| 322 |
zip_path.write_bytes( |
| 323 |
_make_zip( |
| 324 |
{ |
| 325 |
"listens/2026.jsonl": json.dumps(listen) + "\n", |
| 326 |
"other/data.jsonl": '{"should": "be ignored"}\n', |
| 327 |
"listens/readme.txt": "not a jsonl file\n", |
| 328 |
} |
| 329 |
) |
| 330 |
) |
| 331 |
|
| 332 |
result = plugin.import_listenbrainz_data_export(zip_path) |
| 333 |
|
| 334 |
assert result == [listen] |
| 335 |
|
| 336 |
def test_import_file_skips_empty_lines(self, plugin, tmp_path): |
| 337 |
listen = {"listened_at": 1778946700, "track_metadata": {}} |
| 338 |
content = "\n" + json.dumps(listen) + "\n\n" |
| 339 |
zip_path = tmp_path / "export.zip" |
| 340 |
zip_path.write_bytes(_make_zip({"listens/2026.jsonl": content})) |
| 341 |
|
| 342 |
result = plugin.import_listenbrainz_data_export(zip_path) |
| 343 |
|
| 344 |
assert result == [listen] |
| 345 |
|
| 346 |
def test_import_file_skips_invalid_json(self, plugin, tmp_path): |
| 347 |
listen = {"listened_at": 1778946700, "track_metadata": {}} |
| 348 |
content = "not valid json\n" + json.dumps(listen) + "\n" |
| 349 |
zip_path = tmp_path / "export.zip" |
| 350 |
zip_path.write_bytes(_make_zip({"listens/2026.jsonl": content})) |
| 351 |
|
| 352 |
result = plugin.import_listenbrainz_data_export(zip_path) |
| 353 |
|
| 354 |
assert result == [listen] |
| 355 |
|
| 356 |
def test_import_file_raises_on_missing_file(self, plugin, tmp_path): |
| 357 |
with pytest.raises(ui.UserError): |
| 358 |
plugin.import_listenbrainz_data_export(tmp_path / "nonexistent.zip") |