/
/
/
1"""Tests for the Snapcast Unix-socket server payload serialization."""
2
3from __future__ import annotations
4
5from pathlib import Path
6from unittest.mock import MagicMock
7
8from music_assistant_models.enums import ImageType
9from music_assistant_models.media_items import MediaItemImage
10from music_assistant_models.media_items.metadata import IMAGE_PROXY_ID_RESOLVER
11
12from music_assistant.providers.snapcast.socket_server import SnapcastSocketServer
13
14
15def _fake_compute_image_id(provider: str, path: str) -> str:
16 """Return a deterministic marker id so assertions can pin the (provider, path)."""
17 return f"id::{provider}::{path}"
18
19
20def _make_server(socket_path: Path) -> SnapcastSocketServer:
21 """Build a socket server whose mass resolves image ids via the fake above."""
22 mass = MagicMock()
23 mass.metadata.compute_image_id = _fake_compute_image_id
24 return SnapcastSocketServer(
25 mass=mass,
26 queue_id="queue-1",
27 socket_path=str(socket_path),
28 streamserver_ip="127.0.0.1",
29 streamserver_port=8097,
30 )
31
32
33def test_serialize_injects_proxy_id_on_images(tmp_path: Path) -> None:
34 """A serialized MediaItemImage carries the proxy_id the control script builds its URL from."""
35 server = _make_server(tmp_path / "control.sock")
36 image = MediaItemImage(type=ImageType.THUMB, path="/covers/a.jpg", provider="filesystem")
37
38 result = server._serialize(image)
39
40 assert result["proxy_id"] == "id::filesystem::/covers/a.jpg"
41
42
43def test_serialize_passes_through_non_models(tmp_path: Path) -> None:
44 """Plain data without a to_dict is returned unchanged."""
45 server = _make_server(tmp_path / "control.sock")
46 assert server._serialize("ok") == "ok"
47 assert server._serialize({"a": 1}) == {"a": 1}
48
49
50def test_serialize_resets_resolver(tmp_path: Path) -> None:
51 """The imageproxy resolver must not leak onto the context after serialization."""
52 server = _make_server(tmp_path / "control.sock")
53 server._serialize(MediaItemImage(type=ImageType.THUMB, path="/x.jpg", provider="filesystem"))
54 assert IMAGE_PROXY_ID_RESOLVER.get() is None
55