import json import httpx from tag_albums.library import MusicLibrary def test_search_albums_parses_music_assistant_response(): calls = [] def handler(request: httpx.Request) -> httpx.Response: calls.append(request) if request.url.path == "/api/config/config_entries/entry": return httpx.Response(200, json=[{"entry_id": "ma-entry"}]) body = json.loads(request.content) assert body == {"config_entry_id": "ma-entry", "name": "lateralus", "media_type": ["album"], "limit": 12} return httpx.Response(200, json={"service_response": {"albums": [ {"name": "Lateralus", "artists": [{"name": "Tool"}, {"name": "Guest"}], "image": "http://img/1"}, {"name": "No Artist", "artists": []}, ]}}) client = httpx.Client(base_url="http://ha", transport=httpx.MockTransport(handler)) library = MusicLibrary("http://ha", "token", client=client) albums = library.search_albums("lateralus") assert [(a.artist, a.album, a.image) for a in albums] == [ ("Tool", "Lateralus", "http://img/1"), ("", "No Artist", None), ] library.search_albums("lateralus") # The Music Assistant config entry is looked up once, then cached. assert sum(c.url.path == "/api/config/config_entries/entry" for c in calls) == 1