| 1 |
"""Tests for the 'tidal' plugin.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import logging |
| 6 |
import time |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
from unittest.mock import Mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from beets.library import Item |
| 13 |
from beets.test.helper import PluginTestHelper |
| 14 |
from beetsplug.tidal import TidalPlugin |
| 15 |
from beetsplug.tidal.api import API_BASE, TidalAPI |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from beetsplug.tidal.api_types import ( |
| 19 |
AlbumAttributes, |
| 20 |
RelationshipData, |
| 21 |
ResourceIdentifier, |
| 22 |
SearchDocument, |
| 23 |
TidalAlbum, |
| 24 |
TidalArtist, |
| 25 |
TidalArtwork, |
| 26 |
TidalTrack, |
| 27 |
TrackAttributes, |
| 28 |
) |
| 29 |
|
| 30 |
CURRENT_TS = 150000000 |
| 31 |
|
| 32 |
|
| 33 |
def _make_search_doc( |
| 34 |
resource_type: str, resource_ids: list[str] |
| 35 |
) -> SearchDocument: |
| 36 |
return { |
| 37 |
"data": [ |
| 38 |
{ |
| 39 |
"id": "search-result", |
| 40 |
"type": "searchResults", |
| 41 |
"attributes": {"trackingId": "tracking-id"}, |
| 42 |
"relationships": { |
| 43 |
resource_type: { |
| 44 |
"data": [ |
| 45 |
{"id": id_, "type": resource_type} |
| 46 |
for id_ in resource_ids |
| 47 |
], |
| 48 |
"links": {}, |
| 49 |
} |
| 50 |
}, |
| 51 |
} |
| 52 |
] |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
def _make_artwork(id_: str, href: str = "") -> TidalArtwork: |
| 57 |
return { |
| 58 |
"id": id_, |
| 59 |
"type": "artworks", |
| 60 |
"attributes": { |
| 61 |
"mediaType": "IMAGE", |
| 62 |
"files": [{"href": href, "meta": {"width": 1280, "height": 1280}}] |
| 63 |
if href |
| 64 |
else [], |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
def _make_artist(id_: str, name: str) -> TidalArtist: |
| 70 |
return { |
| 71 |
"id": id_, |
| 72 |
"type": "artists", |
| 73 |
"attributes": {"name": name, "popularity": 0.5}, |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
def _make_album( |
| 78 |
id_: str, |
| 79 |
title: str, |
| 80 |
tracks: list[TidalTrack], |
| 81 |
artist_ids: list[str], |
| 82 |
release_date: str = "2024-01-15", |
| 83 |
version: str | None = None, |
| 84 |
cover_art_id: str | None = None, |
| 85 |
) -> tuple[TidalAlbum, dict[str, TidalTrack], dict[str, TidalArtist]]: |
| 86 |
artist_lookup = { |
| 87 |
aid: _make_artist(aid, f"Artist {aid}") for aid in artist_ids |
| 88 |
} |
| 89 |
track_lookup = {t["id"]: t for t in tracks} |
| 90 |
|
| 91 |
attrs: AlbumAttributes = { |
| 92 |
"albumType": "ALBUM", |
| 93 |
"barcodeId": "123456", |
| 94 |
"duration": "PT45M", |
| 95 |
"explicit": False, |
| 96 |
"mediaTags": [], |
| 97 |
"numberOfItems": len(tracks), |
| 98 |
"numberOfVolumes": 1, |
| 99 |
"popularity": 0.5, |
| 100 |
"title": title, |
| 101 |
"releaseDate": release_date, |
| 102 |
} |
| 103 |
if version: |
| 104 |
attrs["version"] = version |
| 105 |
|
| 106 |
relationships: dict[str, RelationshipData] = { |
| 107 |
"artists": { |
| 108 |
"data": [{"id": aid, "type": "artists"} for aid in artist_ids], |
| 109 |
"links": {}, |
| 110 |
}, |
| 111 |
"items": { |
| 112 |
"data": [{"id": t["id"], "type": "tracks"} for t in tracks], |
| 113 |
"links": {}, |
| 114 |
}, |
| 115 |
} |
| 116 |
if cover_art_id: |
| 117 |
relationships["coverArt"] = { |
| 118 |
"data": [{"id": cover_art_id, "type": "artworks"}], |
| 119 |
"links": {}, |
| 120 |
} |
| 121 |
|
| 122 |
album: TidalAlbum = { |
| 123 |
"id": id_, |
| 124 |
"type": "albums", |
| 125 |
"attributes": attrs, |
| 126 |
"relationships": relationships, |
| 127 |
} |
| 128 |
return album, track_lookup, artist_lookup |
| 129 |
|
| 130 |
|
| 131 |
def _make_track( |
| 132 |
id_: str, |
| 133 |
title: str, |
| 134 |
duration: str = "PT3M30S", |
| 135 |
isrc: str = "ISRC123", |
| 136 |
artist_ids: list[str] | None = None, |
| 137 |
version: str | None = None, |
| 138 |
) -> TidalTrack: |
| 139 |
artist_relationships: list[ResourceIdentifier] = [ |
| 140 |
{"id": aid, "type": "artists"} for aid in (artist_ids or []) |
| 141 |
] |
| 142 |
attrs: TrackAttributes = { |
| 143 |
"title": title, |
| 144 |
"duration": duration, |
| 145 |
"explicit": False, |
| 146 |
"isrc": isrc, |
| 147 |
"key": "C", |
| 148 |
"keyScale": "MAJOR", |
| 149 |
"mediaTags": [], |
| 150 |
"popularity": 0.5, |
| 151 |
} |
| 152 |
if version: |
| 153 |
attrs["version"] = version |
| 154 |
return { |
| 155 |
"id": id_, |
| 156 |
"type": "tracks", |
| 157 |
"attributes": attrs, |
| 158 |
"relationships": { |
| 159 |
"artists": {"data": artist_relationships, "links": {}} |
| 160 |
}, |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
@pytest.fixture(autouse=True) |
| 165 |
def set_current_ts(monkeypatch): |
| 166 |
monkeypatch.setattr("beetsplug.tidal.time.time", lambda: CURRENT_TS) |
| 167 |
|
| 168 |
|
| 169 |
class TidalPluginTest(PluginTestHelper): |
| 170 |
plugin = "tidal" |
| 171 |
|
| 172 |
def setup_beets(self): |
| 173 |
super().setup_beets() |
| 174 |
self.tidal = TidalPlugin() |
| 175 |
|
| 176 |
|
| 177 |
class TestAPI: |
| 178 |
def test_search_results_uses_query_filter(self): |
| 179 |
api = TidalAPI("client-id", "token.json") |
| 180 |
response = {"data": []} |
| 181 |
api.get_json = Mock(return_value=response) |
| 182 |
|
| 183 |
result = api.search_results("My Artist My Album", include=["albums"]) |
| 184 |
|
| 185 |
assert result == response |
| 186 |
api.get_json.assert_called_once_with( |
| 187 |
f"{API_BASE}/searchResults", |
| 188 |
params={ |
| 189 |
"filter[query]": "My Artist My Album", |
| 190 |
"explicitFilter": "INCLUDE", |
| 191 |
"countryCode": "US", |
| 192 |
"include": ["albums"], |
| 193 |
}, |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
class TestParsing(TidalPluginTest): |
| 198 |
"""High-level tests for album parsing.""" |
| 199 |
|
| 200 |
def test_parse_album(self): |
| 201 |
tracks = [ |
| 202 |
_make_track("101", "Track One", "PT3M", "ISRC1", ["1001"]), |
| 203 |
_make_track( |
| 204 |
"102", |
| 205 |
"Track Two", |
| 206 |
"PT4M", |
| 207 |
"ISRC2", |
| 208 |
["1001"], |
| 209 |
version="Remastered", |
| 210 |
), |
| 211 |
] |
| 212 |
album, track_lookup, artist_lookup = _make_album( |
| 213 |
"1", "My Album", tracks, ["1001"], version="Deluxe Edition" |
| 214 |
) |
| 215 |
|
| 216 |
info = self.tidal._get_album_info( |
| 217 |
album, track_lookup, artist_lookup, {} |
| 218 |
) |
| 219 |
|
| 220 |
assert info.raw_data == { |
| 221 |
"album": "My Album (Deluxe Edition)", |
| 222 |
"album_id": "1", |
| 223 |
"albumdisambig": None, |
| 224 |
"albumstatus": None, |
| 225 |
"albumtype": "album", |
| 226 |
"albumtypes": ["album"], |
| 227 |
"artist": "Artist 1001", |
| 228 |
"artist_credit": None, |
| 229 |
"artist_id": "1001", |
| 230 |
"artist_sort": None, |
| 231 |
"artists": ["Artist 1001"], |
| 232 |
"artists_credit": None, |
| 233 |
"artists_ids": ["1001"], |
| 234 |
"artists_sort": None, |
| 235 |
"asin": None, |
| 236 |
"barcode": "123456", |
| 237 |
"catalognum": None, |
| 238 |
"country": None, |
| 239 |
"cover_art_url": None, |
| 240 |
"data_source": "Tidal", |
| 241 |
"data_url": None, |
| 242 |
"day": 15, |
| 243 |
"discogs_albumid": None, |
| 244 |
"discogs_artistid": None, |
| 245 |
"discogs_labelid": None, |
| 246 |
"duration": 2700, |
| 247 |
"genres": None, |
| 248 |
"label": None, |
| 249 |
"language": None, |
| 250 |
"media": None, |
| 251 |
"mediums": None, |
| 252 |
"month": 1, |
| 253 |
"original_day": None, |
| 254 |
"original_month": None, |
| 255 |
"original_year": None, |
| 256 |
"release_group_title": None, |
| 257 |
"releasegroup_id": None, |
| 258 |
"releasegroupdisambig": None, |
| 259 |
"script": None, |
| 260 |
"style": None, |
| 261 |
"tidal_album_id": "1", |
| 262 |
"tidal_album_popularity": 50, |
| 263 |
"tidal_artist_id": "1001", |
| 264 |
"tidal_updated": CURRENT_TS, |
| 265 |
"tracks": [ |
| 266 |
{ |
| 267 |
"album": None, |
| 268 |
"arrangers": None, |
| 269 |
"arrangers_ids": [], |
| 270 |
"artist": "Artist 1001", |
| 271 |
"artist_credit": None, |
| 272 |
"artist_id": None, |
| 273 |
"artist_sort": None, |
| 274 |
"artists": ["Artist 1001"], |
| 275 |
"artists_credit": None, |
| 276 |
"artists_ids": ["1001"], |
| 277 |
"artists_sort": None, |
| 278 |
"bpm": None, |
| 279 |
"composer_sort": None, |
| 280 |
"composers": None, |
| 281 |
"composers_ids": [], |
| 282 |
"data_source": "Tidal", |
| 283 |
"data_url": None, |
| 284 |
"disctitle": None, |
| 285 |
"genres": None, |
| 286 |
"index": 1, |
| 287 |
"initial_key": None, |
| 288 |
"isrc": "ISRC1", |
| 289 |
"label": None, |
| 290 |
"length": 180, |
| 291 |
"lyricists": None, |
| 292 |
"lyricists_ids": [], |
| 293 |
"mb_workid": None, |
| 294 |
"media": None, |
| 295 |
"medium": None, |
| 296 |
"medium_index": None, |
| 297 |
"medium_total": None, |
| 298 |
"release_track_id": None, |
| 299 |
"remixers": None, |
| 300 |
"remixers_ids": [], |
| 301 |
"tidal_artist_id": "1001", |
| 302 |
"tidal_track_id": "101", |
| 303 |
"tidal_track_popularity": 50, |
| 304 |
"tidal_updated": CURRENT_TS, |
| 305 |
"title": "Track One", |
| 306 |
"track_alt": None, |
| 307 |
"track_id": "101", |
| 308 |
"work": None, |
| 309 |
"work_disambig": None, |
| 310 |
}, |
| 311 |
{ |
| 312 |
"album": None, |
| 313 |
"arrangers": None, |
| 314 |
"arrangers_ids": [], |
| 315 |
"artist": "Artist 1001", |
| 316 |
"artist_credit": None, |
| 317 |
"artist_id": None, |
| 318 |
"artist_sort": None, |
| 319 |
"artists": ["Artist 1001"], |
| 320 |
"artists_credit": None, |
| 321 |
"artists_ids": ["1001"], |
| 322 |
"artists_sort": None, |
| 323 |
"bpm": None, |
| 324 |
"composer_sort": None, |
| 325 |
"composers": None, |
| 326 |
"composers_ids": [], |
| 327 |
"data_source": "Tidal", |
| 328 |
"data_url": None, |
| 329 |
"disctitle": None, |
| 330 |
"genres": None, |
| 331 |
"index": 2, |
| 332 |
"initial_key": None, |
| 333 |
"isrc": "ISRC2", |
| 334 |
"label": None, |
| 335 |
"length": 240, |
| 336 |
"lyricists": None, |
| 337 |
"lyricists_ids": [], |
| 338 |
"mb_workid": None, |
| 339 |
"media": None, |
| 340 |
"medium": None, |
| 341 |
"medium_index": None, |
| 342 |
"medium_total": None, |
| 343 |
"release_track_id": None, |
| 344 |
"remixers": None, |
| 345 |
"remixers_ids": [], |
| 346 |
"tidal_artist_id": "1001", |
| 347 |
"tidal_track_id": "102", |
| 348 |
"tidal_track_popularity": 50, |
| 349 |
"tidal_updated": CURRENT_TS, |
| 350 |
"title": "Track Two (Remastered)", |
| 351 |
"track_alt": None, |
| 352 |
"track_id": "102", |
| 353 |
"work": None, |
| 354 |
"work_disambig": None, |
| 355 |
}, |
| 356 |
], |
| 357 |
"va": False, |
| 358 |
"year": 2024, |
| 359 |
} |
| 360 |
|
| 361 |
def test_parse_album_with_version(self): |
| 362 |
"""Album title should have version appended.""" |
| 363 |
track = _make_track("101", "My Song", "PT3M", "ISRC001", ["1001"]) |
| 364 |
album, track_lookup, artist_lookup = _make_album( |
| 365 |
"3", "My Album", [track], ["1001"], version="Deluxe Edition" |
| 366 |
) |
| 367 |
|
| 368 |
info = self.tidal._get_album_info( |
| 369 |
album, track_lookup, artist_lookup, {} |
| 370 |
) |
| 371 |
|
| 372 |
assert info.album == "My Album (Deluxe Edition)" |
| 373 |
|
| 374 |
|
| 375 |
class TestArtworkParsing(TidalPluginTest): |
| 376 |
"""Tests for artwork URL parsing.""" |
| 377 |
|
| 378 |
def test_artwork_with_url(self): |
| 379 |
"""Cover art URL from included resources.""" |
| 380 |
track = _make_track("t1", "Song", "PT3M", "ISRC001", ["a1"]) |
| 381 |
album, track_lookup, artist_lookup = _make_album( |
| 382 |
"al1", "Album", [track], ["a1"], cover_art_id="ca1" |
| 383 |
) |
| 384 |
artwork_lookup = { |
| 385 |
"ca1": _make_artwork( |
| 386 |
"ca1", "https://resources.tidal.com/images/ca1/1280x1280.jpg" |
| 387 |
) |
| 388 |
} |
| 389 |
|
| 390 |
info = self.tidal._get_album_info( |
| 391 |
album, track_lookup, artist_lookup, artwork_lookup |
| 392 |
) |
| 393 |
|
| 394 |
assert ( |
| 395 |
info.cover_art_url |
| 396 |
== "https://resources.tidal.com/images/ca1/1280x1280.jpg" |
| 397 |
) |
| 398 |
|
| 399 |
def test_artwork_without_files_returns_none(self): |
| 400 |
"""Cover art returns None when files array is empty.""" |
| 401 |
track = _make_track("t1", "Song", "PT3M", "ISRC001", ["a1"]) |
| 402 |
album, track_lookup, artist_lookup = _make_album( |
| 403 |
"al1", "Album", [track], ["a1"], cover_art_id="ca1" |
| 404 |
) |
| 405 |
artwork_lookup = {"ca1": _make_artwork("ca1")} |
| 406 |
|
| 407 |
info = self.tidal._get_album_info( |
| 408 |
album, track_lookup, artist_lookup, artwork_lookup |
| 409 |
) |
| 410 |
|
| 411 |
assert info.cover_art_url is None |
| 412 |
|
| 413 |
def test_artwork_without_relationship_returns_none(self): |
| 414 |
"""No cover_art_url when album has no coverArt relationship.""" |
| 415 |
track = _make_track("t1", "Song", "PT3M", "ISRC001", ["a1"]) |
| 416 |
album, track_lookup, artist_lookup = _make_album( |
| 417 |
"al1", "Album", [track], ["a1"] |
| 418 |
) |
| 419 |
|
| 420 |
info = self.tidal._get_album_info( |
| 421 |
album, track_lookup, artist_lookup, {} |
| 422 |
) |
| 423 |
|
| 424 |
assert info.cover_art_url is None |
| 425 |
|
| 426 |
|
| 427 |
class TestTrackParsing(TidalPluginTest): |
| 428 |
"""High-level tests for track parsing.""" |
| 429 |
|
| 430 |
def test_parse_track(self): |
| 431 |
track = _make_track("101", "My Track", "PT4M", "ISRC456", ["1001"]) |
| 432 |
artist_lookup = {"1001": _make_artist("1001", "My Artist")} |
| 433 |
|
| 434 |
info = self.tidal._get_track_info(track, artist_lookup) |
| 435 |
|
| 436 |
assert info.title == "My Track" |
| 437 |
assert info.track_id == "101" |
| 438 |
assert info.length == 240 # PT4M = 240 seconds |
| 439 |
assert info.isrc == "ISRC456" |
| 440 |
assert info.artist == "My Artist" |
| 441 |
assert info.tidal_track_id == "101" |
| 442 |
assert info.tidal_artist_id == "1001" |
| 443 |
assert info.tidal_track_popularity == 50 |
| 444 |
|
| 445 |
def test_parse_track_with_version(self): |
| 446 |
"""Track title should have version appended.""" |
| 447 |
track = _make_track( |
| 448 |
"102", "My Song", "PT3M", "ISRC002", ["1001"], version="Remastered" |
| 449 |
) |
| 450 |
artist_lookup = {"1001": _make_artist("1001", "My Artist")} |
| 451 |
|
| 452 |
info = self.tidal._get_track_info(track, artist_lookup) |
| 453 |
|
| 454 |
assert info.title == "My Song (Remastered)" |
| 455 |
assert info.tidal_track_id == "102" |
| 456 |
|
| 457 |
|
| 458 |
class TestTrackForID(TidalPluginTest): |
| 459 |
"""Tests for track_for_id with mocked API.""" |
| 460 |
|
| 461 |
def test_track_for_id(self): |
| 462 |
"""Test fetching track by ID via API.""" |
| 463 |
track = _make_track( |
| 464 |
"490839595", "API Track", "PT3M", "ISRC001", ["1001"] |
| 465 |
) |
| 466 |
artist = _make_artist("1001", "API Artist") |
| 467 |
|
| 468 |
self.tidal.api.get_tracks = Mock( |
| 469 |
return_value={"data": [track], "included": [artist]} |
| 470 |
) |
| 471 |
|
| 472 |
info = self.tidal.track_for_id("https://tidal.com/track/490839595") |
| 473 |
self.tidal.api.get_tracks.assert_called_once() |
| 474 |
|
| 475 |
assert info is not None |
| 476 |
assert info.title == "API Track" |
| 477 |
assert info.track_id == "490839595" |
| 478 |
assert info.artist == "API Artist" |
| 479 |
|
| 480 |
def test_track_for_id_not_found(self): |
| 481 |
"""Test track_for_id returns None when not found.""" |
| 482 |
self.tidal.api.get_tracks = Mock(return_value={"data": []}) |
| 483 |
info = self.tidal.track_for_id("https://tidal.com/track/490839595") |
| 484 |
assert info is None |
| 485 |
|
| 486 |
info = self.tidal.track_for_id("does_not_exist") |
| 487 |
assert info is None |
| 488 |
|
| 489 |
|
| 490 |
class TestTracksForIDs(TidalPluginTest): |
| 491 |
"""Tests for tracks_for_ids with mocked API.""" |
| 492 |
|
| 493 |
def test_tracks_for_ids(self): |
| 494 |
"""Test fetching multiple tracks by IDs via API.""" |
| 495 |
track1 = _make_track( |
| 496 |
"490839595", "API Track 1", "PT3M", "ISRC001", ["1001"] |
| 497 |
) |
| 498 |
track2 = _make_track( |
| 499 |
"490839596", "API Track 2", "PT4M", "ISRC002", ["1001"] |
| 500 |
) |
| 501 |
artist = _make_artist("1001", "API Artist") |
| 502 |
|
| 503 |
self.tidal.api.get_tracks = Mock( |
| 504 |
return_value={"data": [track1, track2], "included": [artist]} |
| 505 |
) |
| 506 |
|
| 507 |
results = list( |
| 508 |
self.tidal.tracks_for_ids( |
| 509 |
[ |
| 510 |
"https://tidal.com/track/490839595", |
| 511 |
"https://tidal.com/track/490839596", |
| 512 |
] |
| 513 |
) |
| 514 |
) |
| 515 |
self.tidal.api.get_tracks.assert_called_once() |
| 516 |
assert len(results) == 2 |
| 517 |
assert results[0] is not None |
| 518 |
assert results[0].title == "API Track 1" |
| 519 |
assert results[1] is not None |
| 520 |
assert results[1].title == "API Track 2" |
| 521 |
|
| 522 |
def test_tracks_for_ids_with_missing(self): |
| 523 |
"""Test tracks_for_ids yields None for IDs not found.""" |
| 524 |
track = _make_track( |
| 525 |
"490839595", "API Track", "PT3M", "ISRC001", ["1001"] |
| 526 |
) |
| 527 |
artist = _make_artist("1001", "API Artist") |
| 528 |
|
| 529 |
self.tidal.api.get_tracks = Mock( |
| 530 |
return_value={"data": [track], "included": [artist]} |
| 531 |
) |
| 532 |
|
| 533 |
results = list( |
| 534 |
self.tidal.tracks_for_ids( |
| 535 |
["https://tidal.com/track/490839595", "does_not_exist"] |
| 536 |
) |
| 537 |
) |
| 538 |
|
| 539 |
assert len(results) == 2 |
| 540 |
assert results[0] is not None |
| 541 |
assert results[0].title == "API Track" |
| 542 |
assert results[1] is None |
| 543 |
|
| 544 |
|
| 545 |
class TestAlbumForID(TidalPluginTest): |
| 546 |
"""Tests for album_for_id with mocked API.""" |
| 547 |
|
| 548 |
def test_album_for_id(self): |
| 549 |
"""Test fetching album by ID via API.""" |
| 550 |
track = _make_track( |
| 551 |
"101", "Album Track", "PT3M30S", "ISRC001", ["1001"] |
| 552 |
) |
| 553 |
album, track_lookup, artist_lookup = _make_album( |
| 554 |
"226495055", "API Album", [track], ["1001"] |
| 555 |
) |
| 556 |
self.tidal.api.get_albums = Mock( |
| 557 |
return_value={ |
| 558 |
"data": [album], |
| 559 |
"included": [*artist_lookup.values(), *track_lookup.values()], |
| 560 |
} |
| 561 |
) |
| 562 |
|
| 563 |
info = self.tidal.album_for_id("https://tidal.com/album/226495055") |
| 564 |
|
| 565 |
assert info is not None |
| 566 |
assert info.album == "API Album" |
| 567 |
assert info.album_id == "226495055" |
| 568 |
assert len(info.tracks) == 1 |
| 569 |
assert info.tracks[0].title == "Album Track" |
| 570 |
|
| 571 |
def test_album_for_id_not_found(self): |
| 572 |
"""Test album_for_id returns None when not found.""" |
| 573 |
self.tidal.api.get_albums = Mock(return_value={"data": []}) |
| 574 |
info = self.tidal.album_for_id("https://tidal.com/album/226495055") |
| 575 |
assert info is None |
| 576 |
|
| 577 |
info = self.tidal.album_for_id("does_not_exist") |
| 578 |
assert info is None |
| 579 |
|
| 580 |
|
| 581 |
class TestAlbumsForIDs(TidalPluginTest): |
| 582 |
"""Tests for albums_for_ids with mocked API.""" |
| 583 |
|
| 584 |
def test_albums_for_ids(self): |
| 585 |
"""Test fetching multiple albums by IDs via API.""" |
| 586 |
track1 = _make_track( |
| 587 |
"101", "Album Track 1", "PT3M", "ISRC001", ["1001"] |
| 588 |
) |
| 589 |
track2 = _make_track( |
| 590 |
"102", "Album Track 2", "PT4M", "ISRC002", ["1001"] |
| 591 |
) |
| 592 |
album1, track_lookup1, artist_lookup1 = _make_album( |
| 593 |
"226495055", "API Album 1", [track1], ["1001"] |
| 594 |
) |
| 595 |
album2, track_lookup2, artist_lookup2 = _make_album( |
| 596 |
"226495056", "API Album 2", [track2], ["1001"] |
| 597 |
) |
| 598 |
|
| 599 |
# Combine lookups to simulate API response |
| 600 |
all_included = [ |
| 601 |
*artist_lookup1.values(), |
| 602 |
*artist_lookup2.values(), |
| 603 |
*track_lookup1.values(), |
| 604 |
*track_lookup2.values(), |
| 605 |
] |
| 606 |
|
| 607 |
self.tidal.api.get_albums = Mock( |
| 608 |
return_value={"data": [album1, album2], "included": all_included} |
| 609 |
) |
| 610 |
|
| 611 |
results = list( |
| 612 |
self.tidal.albums_for_ids( |
| 613 |
[ |
| 614 |
"https://tidal.com/album/226495055", |
| 615 |
"https://tidal.com/album/226495056", |
| 616 |
] |
| 617 |
) |
| 618 |
) |
| 619 |
|
| 620 |
self.tidal.api.get_albums.assert_called_once() |
| 621 |
# Note: yields album then None for each ID |
| 622 |
assert len(results) == 2 |
| 623 |
assert results[0] is not None |
| 624 |
assert results[0].album == "API Album 1" |
| 625 |
assert results[1] is not None |
| 626 |
assert results[1].album == "API Album 2" |
| 627 |
|
| 628 |
def test_albums_for_ids_with_missing(self): |
| 629 |
"""Test albums_for_ids yields None for IDs not found.""" |
| 630 |
track = _make_track("101", "Album Track", "PT3M", "ISRC001", ["1001"]) |
| 631 |
album, track_lookup, artist_lookup = _make_album( |
| 632 |
"226495055", "API Album", [track], ["1001"] |
| 633 |
) |
| 634 |
|
| 635 |
self.tidal.api.get_albums = Mock( |
| 636 |
return_value={ |
| 637 |
"data": [album], |
| 638 |
"included": [*artist_lookup.values(), *track_lookup.values()], |
| 639 |
} |
| 640 |
) |
| 641 |
|
| 642 |
results = list( |
| 643 |
self.tidal.albums_for_ids( |
| 644 |
["https://tidal.com/album/226495055", "does_not_exist"] |
| 645 |
) |
| 646 |
) |
| 647 |
|
| 648 |
# yields (album, None) for (found, not_found) |
| 649 |
assert len(results) == 2 |
| 650 |
assert results[0] is not None |
| 651 |
assert results[0].album == "API Album" |
| 652 |
assert results[1] is None |
| 653 |
|
| 654 |
|
| 655 |
class TestCandidates(TidalPluginTest): |
| 656 |
"""Tests for candidates method.""" |
| 657 |
|
| 658 |
def test_candidates_with_barcode(self): |
| 659 |
"""Test that candidates uses barcode lookup first.""" |
| 660 |
track = _make_track("101", "Album Track", "PT3M", "ISRC001", ["1001"]) |
| 661 |
album, track_lookup, artist_lookup = _make_album( |
| 662 |
"1", "Barcode Album", [track], ["1001"] |
| 663 |
) |
| 664 |
|
| 665 |
self.tidal.api.get_albums = Mock( |
| 666 |
return_value={ |
| 667 |
"data": [album], |
| 668 |
"included": [*artist_lookup.values(), *track_lookup.values()], |
| 669 |
} |
| 670 |
) |
| 671 |
|
| 672 |
items = [Item(barcode="123456")] |
| 673 |
|
| 674 |
candidates = list( |
| 675 |
self.tidal.candidates(items, "Artist", "Album", False) |
| 676 |
) |
| 677 |
|
| 678 |
self.tidal.api.get_albums.assert_called_once() |
| 679 |
assert len(candidates) == 1 |
| 680 |
assert candidates[0].album == "Barcode Album" |
| 681 |
|
| 682 |
def test_candidates_with_query_fallback(self): |
| 683 |
"""Test that candidates falls back to query search when no barcode.""" |
| 684 |
items = [Item(title="My Song", artist="My Artist", album="My Album")] |
| 685 |
|
| 686 |
# Mock search returning album IDs |
| 687 |
self.tidal.api.search_results = Mock( |
| 688 |
return_value=_make_search_doc("albums", ["1"]) |
| 689 |
) |
| 690 |
|
| 691 |
# Mock album lookup by ID |
| 692 |
track = _make_track("101", "Album Track", "PT3M", "ISRC001", ["1001"]) |
| 693 |
album, track_lookup, artist_lookup = _make_album( |
| 694 |
"1", "Query Album", [track], ["1001"] |
| 695 |
) |
| 696 |
self.tidal.api.get_albums = Mock( |
| 697 |
return_value={ |
| 698 |
"data": [album], |
| 699 |
"included": [*artist_lookup.values(), *track_lookup.values()], |
| 700 |
} |
| 701 |
) |
| 702 |
|
| 703 |
candidates = list( |
| 704 |
self.tidal.candidates(items, "My Artist", "My Album", False) |
| 705 |
) |
| 706 |
|
| 707 |
# Should have called search_results |
| 708 |
assert self.tidal.api.search_results.called |
| 709 |
assert len(candidates) == 1 |
| 710 |
assert candidates[0].album == "Query Album" |
| 711 |
|
| 712 |
def test_album_search_handles_empty_response(self): |
| 713 |
self.tidal.api.search_results = Mock(return_value={"data": []}) |
| 714 |
self.tidal.api.get_albums = Mock() |
| 715 |
|
| 716 |
candidates = list(self.tidal.search_albums_by_query("missing album")) |
| 717 |
|
| 718 |
assert candidates == [] |
| 719 |
self.tidal.api.get_albums.assert_not_called() |
| 720 |
|
| 721 |
|
| 722 |
class TestItemCandidates(TidalPluginTest): |
| 723 |
"""Tests for item_candidates method.""" |
| 724 |
|
| 725 |
def test_item_candidates_with_isrc(self): |
| 726 |
"""Test that item_candidates uses ISRC lookup first.""" |
| 727 |
track = _make_track( |
| 728 |
"490839595", "ISRC Track", "PT3M", "ISRC001", ["1001"] |
| 729 |
) |
| 730 |
artist = _make_artist("1001", "ISRC Artist") |
| 731 |
|
| 732 |
self.tidal.api.get_tracks = Mock( |
| 733 |
return_value={"data": [track], "included": [artist]} |
| 734 |
) |
| 735 |
|
| 736 |
item = Item(isrc="ISRC001") |
| 737 |
|
| 738 |
results = list(self.tidal.item_candidates(item, "Artist", "Title")) |
| 739 |
|
| 740 |
self.tidal.api.get_tracks.assert_called_once() |
| 741 |
assert len(results) == 1 |
| 742 |
assert results[0].title == "ISRC Track" |
| 743 |
|
| 744 |
def test_item_candidates_with_query_fallback(self): |
| 745 |
"""Test item_candidates falls back to query search when no ISRC.""" |
| 746 |
item = Item(title="Query Song", artist="Query Artist") |
| 747 |
|
| 748 |
self.tidal.api.search_results = Mock( |
| 749 |
return_value={ |
| 750 |
**_make_search_doc("tracks", ["490839595"]), |
| 751 |
"included": [ |
| 752 |
_make_track( |
| 753 |
"490839595", "Query Track", "PT3M", "ISRC002", ["1001"] |
| 754 |
), |
| 755 |
_make_artist("1001", "Query Artist"), |
| 756 |
], |
| 757 |
} |
| 758 |
) |
| 759 |
|
| 760 |
results = list( |
| 761 |
self.tidal.item_candidates(item, "Query Artist", "Query Song") |
| 762 |
) |
| 763 |
|
| 764 |
assert self.tidal.api.search_results.called |
| 765 |
assert results[0].title == "Query Track" |
| 766 |
|
| 767 |
def test_track_search_handles_empty_response(self): |
| 768 |
self.tidal.api.search_results = Mock(return_value={"data": []}) |
| 769 |
|
| 770 |
candidates = list(self.tidal.search_tracks_by_query("missing track")) |
| 771 |
|
| 772 |
assert candidates == [] |
| 773 |
|
| 774 |
|
| 775 |
class TestStaticHelpers: |
| 776 |
"""Tests for static helper methods.""" |
| 777 |
|
| 778 |
@pytest.mark.parametrize( |
| 779 |
"attrs, expected", |
| 780 |
[ |
| 781 |
( |
| 782 |
{ |
| 783 |
"externalLinks": [ |
| 784 |
{"href": "https://tidal.com/b/123", "meta": ""} |
| 785 |
] |
| 786 |
}, |
| 787 |
"https://tidal.com/b/123", |
| 788 |
), |
| 789 |
({}, None), |
| 790 |
], |
| 791 |
) |
| 792 |
def test_parse_data_url(self, attrs, expected): |
| 793 |
assert TidalPlugin._parse_data_url(attrs) == expected |
| 794 |
|
| 795 |
@pytest.mark.parametrize( |
| 796 |
"attrs, expected", |
| 797 |
[({"copyright": {"text": "(P) 2024 Tidal"}}, "Tidal"), ({}, None)], |
| 798 |
) |
| 799 |
def test_parse_label(self, attrs, expected): |
| 800 |
assert TidalPlugin._parse_label(attrs) == expected |
| 801 |
|
| 802 |
@pytest.mark.parametrize( |
| 803 |
"text, expected", |
| 804 |
[ |
| 805 |
# marker prefixes |
| 806 |
("© 2017 Blackened Recordings", "Blackened Recordings"), |
| 807 |
("(C) 2019 Ghosteen Ltd", "Ghosteen"), |
| 808 |
("(P) 2007 Century Media Records Ltd.", "Century Media Records"), |
| 809 |
("℗ 2024 Tidal", "Tidal"), |
| 810 |
# plain label name |
| 811 |
("Lightning Bolt", "Lightning Bolt"), |
| 812 |
# under exclusive license to |
| 813 |
( |
| 814 |
"Kim Gordon under exclusive license to Matador Records", |
| 815 |
"Matador Records", |
| 816 |
), |
| 817 |
# issue #6796's own examples, verbatim — cover the corporate-relationship |
| 818 |
# clause, the territorial split, and the bare trailing suffix |
| 819 |
( |
| 820 |
"© 2011 Motown Records, a Division of UMG Recordings, Inc.", |
| 821 |
"Motown Records", |
| 822 |
), |
| 823 |
( |
| 824 |
( |
| 825 |
"© 2019 Atlantic Recording Corporation for the United States and " |
| 826 |
"WEA International Inc. for the world outside of the United States" |
| 827 |
), |
| 828 |
"Atlantic Recording Corporation", |
| 829 |
), |
| 830 |
("(P) 1992 Zomba Recording LLC", "Zomba Recording"), |
| 831 |
], |
| 832 |
) |
| 833 |
def test_normalize_label(self, text, expected): |
| 834 |
assert TidalPlugin._normalize_label(text) == expected |
| 835 |
|
| 836 |
@pytest.mark.parametrize( |
| 837 |
"attrs, expected", |
| 838 |
[ |
| 839 |
({"releaseDate": "2024-01-15"}, (2024, 1, 15)), |
| 840 |
({}, None), |
| 841 |
({"releaseDate": "2024"}, None), |
| 842 |
], |
| 843 |
) |
| 844 |
def test_parse_release_date(self, attrs, expected): |
| 845 |
assert TidalPlugin._parse_release_date(attrs) == expected |
| 846 |
|
| 847 |
@pytest.mark.parametrize( |
| 848 |
"duration,expected", |
| 849 |
[ |
| 850 |
("PT30S", 30), |
| 851 |
("PT3M30S", 210), |
| 852 |
("PT4M", 240), |
| 853 |
("PT1H", 3600), |
| 854 |
("PT1H30M", 5400), |
| 855 |
], |
| 856 |
) |
| 857 |
def test_duration_conversions(self, duration, expected): |
| 858 |
assert TidalPlugin._duration_to_seconds(duration) == expected |
| 859 |
|
| 860 |
def test_duration_invalid_raises(self, caplog): |
| 861 |
with caplog.at_level(logging.WARNING): |
| 862 |
TidalPlugin._duration_to_seconds("invalid") |
| 863 |
assert "Invalid ISO 8601 duration: invalid" in caplog.text |
| 864 |
|
| 865 |
def test_popularity_with_float(self): |
| 866 |
assert TidalPlugin._parse_popularity({"popularity": 0.5}) == 50 |
| 867 |
assert TidalPlugin._parse_popularity({"popularity": 1.0}) == 100 |
| 868 |
assert TidalPlugin._parse_popularity({"popularity": 0.0}) == 0 |
| 869 |
|
| 870 |
@pytest.mark.parametrize( |
| 871 |
"artwork_data, artwork_by_id, expected", |
| 872 |
[ |
| 873 |
( |
| 874 |
[{"id": "ca1", "type": "artworks"}], |
| 875 |
{ |
| 876 |
"ca1": { |
| 877 |
"id": "ca1", |
| 878 |
"type": "artworks", |
| 879 |
"attributes": { |
| 880 |
"mediaType": "IMAGE", |
| 881 |
"files": [ |
| 882 |
{ |
| 883 |
"href": "https://example.com/cover.jpg", |
| 884 |
"meta": {"width": 1280, "height": 1280}, |
| 885 |
} |
| 886 |
], |
| 887 |
}, |
| 888 |
} |
| 889 |
}, |
| 890 |
"https://example.com/cover.jpg", |
| 891 |
), |
| 892 |
( |
| 893 |
[{"id": "ca1", "type": "artworks"}], |
| 894 |
{ |
| 895 |
"ca1": { |
| 896 |
"id": "ca1", |
| 897 |
"type": "artworks", |
| 898 |
"attributes": {"mediaType": "IMAGE", "files": []}, |
| 899 |
} |
| 900 |
}, |
| 901 |
None, |
| 902 |
), |
| 903 |
# No artworks in relationship data |
| 904 |
([{"id": "ca1", "type": "coverArts"}], {}, None), |
| 905 |
# No cover art lookup |
| 906 |
([{"id": "ca1", "type": "artworks"}], {}, None), |
| 907 |
# Empty relationships |
| 908 |
({}, {}, None), |
| 909 |
], |
| 910 |
) |
| 911 |
def test_parse_artwork_url(self, artwork_data, artwork_by_id, expected): |
| 912 |
album: TidalAlbum = { |
| 913 |
"id": "al1", |
| 914 |
"type": "albums", |
| 915 |
"attributes": { |
| 916 |
"albumType": "ALBUM", |
| 917 |
"barcodeId": "123", |
| 918 |
"duration": "PT45M", |
| 919 |
"explicit": False, |
| 920 |
"mediaTags": [], |
| 921 |
"numberOfItems": 1, |
| 922 |
"numberOfVolumes": 1, |
| 923 |
"popularity": 0.5, |
| 924 |
"title": "Album", |
| 925 |
}, |
| 926 |
"relationships": {"coverArt": {"data": artwork_data, "links": {}}} |
| 927 |
if artwork_data |
| 928 |
else {}, |
| 929 |
} |
| 930 |
assert TidalPlugin._parse_artwork_url(album, artwork_by_id) == expected |
| 931 |
|
| 932 |
|
| 933 |
class TestTidalsync(TidalPluginTest): |
| 934 |
"""Tests for the tidalsync command.""" |
| 935 |
|
| 936 |
def _run_tidalsync(self, *args: str) -> None: |
| 937 |
command = next( |
| 938 |
cmd for cmd in self.tidal.commands() if cmd.name == "tidalsync" |
| 939 |
) |
| 940 |
opts, subargs = command.parser.parse_args(list(args)) |
| 941 |
command.func(self.lib, opts, subargs) |
| 942 |
|
| 943 |
def test_sync_updates_popularity(self): |
| 944 |
"""Test sync_item_popularity fetches and stores popularity.""" |
| 945 |
item = self.add_item( |
| 946 |
tidal_track_id=490839595, title="Test Track", artist="Test Artist" |
| 947 |
) |
| 948 |
|
| 949 |
track = _make_track( |
| 950 |
"490839595", "Test Track", "PT3M", "ISRC001", ["1001"] |
| 951 |
) |
| 952 |
artist = _make_artist("1001", "Test Artist") |
| 953 |
|
| 954 |
self.tidal.api.get_tracks = Mock( |
| 955 |
return_value={"data": [track], "included": [artist]} |
| 956 |
) |
| 957 |
|
| 958 |
self.tidal.sync_item_popularity([item], write=False) |
| 959 |
|
| 960 |
assert item["tidal_track_popularity"] == 50 |
| 961 |
assert item["tidal_updated"] is not None |
| 962 |
assert isinstance(item["tidal_updated"], (int, float)) |
| 963 |
|
| 964 |
def test_sync_skips_existing(self): |
| 965 |
"""Test sync_item_popularity skips items with existing popularity.""" |
| 966 |
item = self.add_item( |
| 967 |
tidal_track_id=490839595, |
| 968 |
tidal_track_popularity=42, |
| 969 |
tidal_updated=time.time(), |
| 970 |
title="Test Track", |
| 971 |
artist="Test Artist", |
| 972 |
) |
| 973 |
|
| 974 |
self.tidal.sync_item_popularity([item], write=False) |
| 975 |
|
| 976 |
assert item["tidal_track_popularity"] == 42 |
| 977 |
|
| 978 |
def test_sync_force_updates_existing(self): |
| 979 |
"""Test sync_item_popularity with force re-fetches.""" |
| 980 |
item = self.add_item( |
| 981 |
tidal_track_id=490839595, |
| 982 |
tidal_track_popularity=42, |
| 983 |
tidal_updated=time.time(), |
| 984 |
title="Test Track", |
| 985 |
artist="Test Artist", |
| 986 |
) |
| 987 |
|
| 988 |
track = _make_track( |
| 989 |
"490839595", "Test Track", "PT3M", "ISRC001", ["1001"] |
| 990 |
) |
| 991 |
artist = _make_artist("1001", "Test Artist") |
| 992 |
|
| 993 |
self.tidal.api.get_tracks = Mock( |
| 994 |
return_value={"data": [track], "included": [artist]} |
| 995 |
) |
| 996 |
|
| 997 |
self.tidal.sync_item_popularity([item], write=False, force=True) |
| 998 |
|
| 999 |
assert item["tidal_track_popularity"] == 50 |
| 1000 |
|
| 1001 |
def test_sync_skips_items_without_track_id(self): |
| 1002 |
"""Test sync_item_popularity skips items without tidal_track_id.""" |
| 1003 |
item = self.add_item(title="No ID Track", artist="No ID Artist") |
| 1004 |
|
| 1005 |
self.tidal.sync_item_popularity([item], write=False) |
| 1006 |
|
| 1007 |
assert item.get("tidal_track_popularity") is None |
| 1008 |
|
| 1009 |
def test_sync_does_not_update_when_lookup_is_missing(self): |
| 1010 |
"""Test sync_item_popularity leaves stale data untouched on miss.""" |
| 1011 |
item = self.add_item( |
| 1012 |
tidal_track_id="490839595", |
| 1013 |
title="Missing Track", |
| 1014 |
artist="Missing Artist", |
| 1015 |
) |
| 1016 |
|
| 1017 |
self.tidal.api.get_tracks = Mock( |
| 1018 |
return_value={"data": [], "included": []} |
| 1019 |
) |
| 1020 |
|
| 1021 |
self.tidal.sync_item_popularity([item], write=False) |
| 1022 |
|
| 1023 |
assert item.get("tidal_track_popularity") is None |
| 1024 |
assert item.get("tidal_updated") is None |
| 1025 |
|
| 1026 |
def test_sync_updates_album_popularity(self): |
| 1027 |
"""Test sync_album_popularity fetches and stores album popularity.""" |
| 1028 |
item = self.add_item( |
| 1029 |
tidal_track_id="490839595", |
| 1030 |
tidal_album_id="251380836", |
| 1031 |
title="Test Track", |
| 1032 |
artist="Test Artist", |
| 1033 |
) |
| 1034 |
|
| 1035 |
track = _make_track( |
| 1036 |
"490839595", "Test Track", "PT3M", "ISRC001", ["1001"] |
| 1037 |
) |
| 1038 |
artist = _make_artist("1001", "Test Artist") |
| 1039 |
|
| 1040 |
album_track = _make_track( |
| 1041 |
"101", "Album Track", "PT3M", "ISRC001", ["1001"] |
| 1042 |
) |
| 1043 |
album_data, _, artist_lookup = _make_album( |
| 1044 |
"251380836", "Test Album", [album_track], ["1001"] |
| 1045 |
) |
| 1046 |
|
| 1047 |
album = self.lib.add_album([item]) |
| 1048 |
album["tidal_album_id"] = "251380836" |
| 1049 |
album.store() |
| 1050 |
|
| 1051 |
self.tidal.api.get_tracks = Mock( |
| 1052 |
return_value={"data": [track], "included": [artist]} |
| 1053 |
) |
| 1054 |
self.tidal.api.get_albums = Mock( |
| 1055 |
return_value={ |
| 1056 |
"data": [album_data], |
| 1057 |
"included": [*artist_lookup.values()], |
| 1058 |
} |
| 1059 |
) |
| 1060 |
|
| 1061 |
self.tidal.sync_item_popularity([item], write=False) |
| 1062 |
self.tidal.sync_album_popularity([album], write=False) |
| 1063 |
|
| 1064 |
assert item["tidal_track_popularity"] == 50 |
| 1065 |
assert album["tidal_album_popularity"] == 50 |
| 1066 |
assert item["tidal_updated"] is not None |
| 1067 |
|
| 1068 |
def test_tidalsync_uses_first_query_argument(self): |
| 1069 |
self.lib.items = Mock(return_value=["items"]) |
| 1070 |
self.tidal.sync_item_popularity = Mock() |
| 1071 |
|
| 1072 |
self._run_tidalsync("path::aaa") |
| 1073 |
|
| 1074 |
self.lib.items.assert_called_once_with( |
| 1075 |
["data_source:tidal", "path::aaa"] |
| 1076 |
) |
| 1077 |
self.tidal.sync_item_popularity.assert_called_once_with( |
| 1078 |
["items"], write=False, force=False |
| 1079 |
) |
| 1080 |
|
| 1081 |
def test_tidalsync_defaults_to_items(self): |
| 1082 |
self.lib.items = Mock(return_value=["items"]) |
| 1083 |
self.tidal.sync_item_popularity = Mock() |
| 1084 |
self.tidal.sync_album_popularity = Mock() |
| 1085 |
|
| 1086 |
self._run_tidalsync() |
| 1087 |
|
| 1088 |
self.lib.items.assert_called_once_with(["data_source:tidal"]) |
| 1089 |
self.tidal.sync_item_popularity.assert_called_once_with( |
| 1090 |
["items"], write=False, force=False |
| 1091 |
) |
| 1092 |
self.tidal.sync_album_popularity.assert_not_called() |
| 1093 |
|
| 1094 |
def test_tidalsync_album_mode_uses_album_query(self): |
| 1095 |
self.lib.albums = Mock(return_value=["albums"]) |
| 1096 |
self.tidal.sync_item_popularity = Mock() |
| 1097 |
self.tidal.sync_album_popularity = Mock() |
| 1098 |
|
| 1099 |
self._run_tidalsync("-a", "artist:Test") |
| 1100 |
|
| 1101 |
self.lib.albums.assert_called_once_with( |
| 1102 |
["data_source:tidal", "artist:Test"] |
| 1103 |
) |
| 1104 |
self.tidal.sync_album_popularity.assert_called_once_with( |
| 1105 |
["albums"], write=False, force=False |
| 1106 |
) |
| 1107 |
self.tidal.sync_item_popularity.assert_not_called() |