|
| 1 | +"""Tests for the summary (slim) media item models.""" |
| 2 | + |
| 3 | +from music_assistant_models.enums import ImageType, MediaType |
| 4 | +from music_assistant_models.media_items import ( |
| 5 | + AlbumSummary, |
| 6 | + ArtistSummary, |
| 7 | + AudiobookSummary, |
| 8 | + GenreSummary, |
| 9 | + ItemMappingSummary, |
| 10 | + MediaItemImage, |
| 11 | + MediaItemMetadataSummary, |
| 12 | + Playlist, |
| 13 | + PlaylistSummary, |
| 14 | + PodcastSummary, |
| 15 | + Radio, |
| 16 | + RadioSummary, |
| 17 | + Track, |
| 18 | + TrackSummary, |
| 19 | +) |
| 20 | +from music_assistant_models.media_items.metadata import IMAGE_PROXY_ID_RESOLVER |
| 21 | +from music_assistant_models.unique_list import UniqueList |
| 22 | + |
| 23 | + |
| 24 | +def _track_summary() -> TrackSummary: |
| 25 | + return TrackSummary( |
| 26 | + item_id="123", |
| 27 | + provider="library", |
| 28 | + name="Test Track", |
| 29 | + duration=185, |
| 30 | + favorite=True, |
| 31 | + artists=UniqueList( |
| 32 | + [ |
| 33 | + ItemMappingSummary( |
| 34 | + media_type=MediaType.ARTIST, |
| 35 | + item_id="1", |
| 36 | + provider="library", |
| 37 | + name="Test Artist", |
| 38 | + ) |
| 39 | + ] |
| 40 | + ), |
| 41 | + album=ItemMappingSummary( |
| 42 | + media_type=MediaType.ALBUM, |
| 43 | + item_id="2", |
| 44 | + provider="library", |
| 45 | + name="Test Album", |
| 46 | + year=2001, |
| 47 | + ), |
| 48 | + metadata=MediaItemMetadataSummary( |
| 49 | + images=UniqueList( |
| 50 | + [MediaItemImage(type=ImageType.THUMB, path="/cover.jpg", provider="library")] |
| 51 | + ), |
| 52 | + explicit=True, |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_summary_serialization_omits_none_fields() -> None: |
| 58 | + """Serialized summary items must not contain any None-valued keys, at any level.""" |
| 59 | + d = _track_summary().to_dict() |
| 60 | + assert not [k for k, v in d.items() if v is None] |
| 61 | + assert not [k for k, v in d["metadata"].items() if v is None] |
| 62 | + assert not [k for k, v in d["artists"][0].items() if v is None] |
| 63 | + assert not [k for k, v in d["album"].items() if v is None] |
| 64 | + |
| 65 | + |
| 66 | +def test_summary_keeps_client_facing_fields() -> None: |
| 67 | + """Fields clients switch on must survive serialization even when default-valued.""" |
| 68 | + d = _track_summary().to_dict() |
| 69 | + assert d["media_type"] == "track" |
| 70 | + assert d["available"] is True |
| 71 | + assert d["provider_mappings"] == [] |
| 72 | + assert d["metadata"] == { |
| 73 | + "images": [ |
| 74 | + { |
| 75 | + "type": "thumb", |
| 76 | + "path": "/cover.jpg", |
| 77 | + "provider": "library", |
| 78 | + "remotely_accessible": False, |
| 79 | + "proxy_id": None, |
| 80 | + } |
| 81 | + ], |
| 82 | + "explicit": True, |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +def test_summary_roundtrips_through_regular_model() -> None: |
| 87 | + """A serialized summary item must deserialize cleanly with the regular class.""" |
| 88 | + summary = _track_summary() |
| 89 | + track = Track.from_dict(summary.to_dict()) |
| 90 | + assert track.name == summary.name |
| 91 | + assert track.duration == summary.duration |
| 92 | + assert track.favorite is True |
| 93 | + assert track.artists[0].name == "Test Artist" |
| 94 | + assert track.album is not None |
| 95 | + assert track.album.name == "Test Album" |
| 96 | + assert track.provider_mappings == set() |
| 97 | + |
| 98 | + |
| 99 | +def test_summary_is_instance_of_regular_model() -> None: |
| 100 | + """Summary items are subtypes of their regular counterparts.""" |
| 101 | + assert isinstance(_track_summary(), Track) |
| 102 | + assert isinstance( |
| 103 | + PlaylistSummary(item_id="1", provider="library", name="pl"), |
| 104 | + Playlist, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def test_summary_equality_matches_regular_semantics() -> None: |
| 109 | + """Summary items compare by identity (uri/external ids), like regular items.""" |
| 110 | + one = TrackSummary(item_id="123", provider="library", name="A") |
| 111 | + other = TrackSummary(item_id="123", provider="library", name="B") |
| 112 | + assert one == other |
| 113 | + assert hash(one) == hash(other) |
| 114 | + |
| 115 | + |
| 116 | +def test_summary_image_proxy_id_injection() -> None: |
| 117 | + """The image proxy_id resolver hook must fire for images nested in summary items.""" |
| 118 | + token = IMAGE_PROXY_ID_RESOLVER.set(lambda provider, _path: f"{provider}-proxy") |
| 119 | + try: |
| 120 | + d = _track_summary().to_dict() |
| 121 | + finally: |
| 122 | + IMAGE_PROXY_ID_RESOLVER.reset(token) |
| 123 | + assert d["metadata"]["images"][0]["proxy_id"] == "library-proxy" |
| 124 | + |
| 125 | + |
| 126 | +def test_radio_summary_duration_backcompat() -> None: |
| 127 | + """Radio's None->0 duration shim must not break on the omitted duration key.""" |
| 128 | + d = RadioSummary(item_id="1", provider="library", name="radio").to_dict() |
| 129 | + assert d["duration"] == 0 |
| 130 | + # regular Radio keeps the same behavior |
| 131 | + radio = Radio(item_id="1", provider="tunein", name="radio", provider_mappings=set()) |
| 132 | + assert radio.to_dict()["duration"] == 0 |
| 133 | + |
| 134 | + |
| 135 | +def test_per_type_summaries_serialize_sparse() -> None: |
| 136 | + """Every summary type serializes without None-valued keys.""" |
| 137 | + items = [ |
| 138 | + ArtistSummary(item_id="1", provider="library", name="artist"), |
| 139 | + AlbumSummary(item_id="1", provider="library", name="album", year=2001), |
| 140 | + PlaylistSummary(item_id="1", provider="library", name="playlist", owner="MA"), |
| 141 | + RadioSummary(item_id="1", provider="library", name="radio"), |
| 142 | + AudiobookSummary(item_id="1", provider="library", name="book", publisher="pub"), |
| 143 | + PodcastSummary(item_id="1", provider="library", name="podcast"), |
| 144 | + GenreSummary(item_id="1", provider="library", name="genre"), |
| 145 | + ] |
| 146 | + for item in items: |
| 147 | + d = item.to_dict() |
| 148 | + assert not [k for k, v in d.items() if v is None], type(item).__name__ |
| 149 | + assert d["media_type"] == item.media_type.value |
0 commit comments