/
/
/
1"""Test Tidal Page Parser."""
2
3import json
4import pathlib
5from unittest.mock import Mock
6
7import pytest
8from music_assistant_models.enums import MediaType
9
10from music_assistant.providers.tidal.tidal_page_parser import TidalPageParser
11
12FIXTURES_DIR = pathlib.Path(__file__).parent / "fixtures"
13PAGE_FIXTURES = list(FIXTURES_DIR.glob("pages/*.json"))
14
15
16@pytest.mark.parametrize("example", PAGE_FIXTURES, ids=lambda val: str(val.stem))
17def test_page_parser(example: pathlib.Path, provider_mock: Mock) -> None:
18 """Test page parser with fixtures."""
19 with open(example) as f:
20 data = json.load(f)
21
22 parser = TidalPageParser(provider_mock)
23 parser.parse_page_structure(data, "pages/home")
24
25 assert len(parser._module_map) == 3
26
27 # Test first module (Playlists)
28 module_info = parser._module_map[0]
29 items, content_type = parser.get_module_items(module_info)
30 assert content_type == MediaType.PLAYLIST
31 assert len(items) == 1
32 assert items[0].name == "Test Playlist"
33
34 # Test second module (Albums)
35 module_info = parser._module_map[1]
36 items, content_type = parser.get_module_items(module_info)
37 assert content_type == MediaType.ALBUM
38 assert len(items) == 1
39 assert items[0].name == "Test Album"
40
41 # Test third module (Mixes)
42 module_info = parser._module_map[2]
43 items, content_type = parser.get_module_items(module_info)
44 assert content_type == MediaType.PLAYLIST
45 assert len(items) == 1
46 assert items[0].name == "My Mix"
47
48
49def test_track_list_skips_unparsable_track(provider_mock: Mock) -> None:
50 """Test a malformed track does not prevent loading the rest of a page module."""
51 with open(FIXTURES_DIR / "tracks" / "track.json") as f:
52 malformed = json.load(f)
53 malformed["item"]["mediaMetadata"] = "invalid"
54 with open(FIXTURES_DIR / "tracks" / "track.json") as f:
55 valid = json.load(f)
56
57 parser = TidalPageParser(provider_mock)
58 module_info = {
59 "raw_data": {
60 "type": "TRACK_LIST",
61 "pagedList": {"items": [malformed, valid]},
62 }
63 }
64
65 items, content_type = parser.get_module_items(module_info)
66
67 assert content_type == MediaType.TRACK
68 assert len(items) == 1
69 provider_mock.logger.warning.assert_called_once()
70