feat(music): list and select playlist songs by their position

This commit is contained in:
imSp4rky
2026-11-30 12:39:51 -07:00
parent 40114fff19
commit 70ace41201
16 changed files with 222 additions and 38 deletions
@@ -177,6 +177,20 @@ def test_list_tracks_stamps_the_service_flags(monkeypatch: pytest.MonkeyPatch, w
assert all((t["anime"], t["daily"]) == (True, True) for t in titles)
def test_list_tracks_selects_a_playlist_song_by_position(monkeypatch: pytest.MonkeyPatch) -> None:
songs = [
Song(f"song-{album}", _FakeSvc, "Track", "Artist", album, track, disc, position=position)
for album, track, disc, position in (("Z", 1, 1, 1), ("B", 6, 2, 2), ("A", 2, 1, 3))
]
monkeypatch.setattr(handlers, "validate_service", lambda tag, request=None: "FAKE")
monkeypatch.setattr(handlers, "launch_served", lambda *a, **kw: handlers.Launched(_FakeService(songs), None, None))
response = asyncio.run(handlers.list_tracks_handler({"service": "FAKE", "title_id": "t1", "wanted": "2"}))
title = json.loads(response.body)["title"]
assert (title["id"], title["position"], title["track"]) == ("song-B", 2, 6)
def test_list_titles_stamps_the_service_flags(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(handlers, "validate_service", lambda tag, request=None: "FAKE")
monkeypatch.setattr(handlers, "launch_served", lambda *a, **kw: handlers.Launched(_FakeService([ep()]), None, None))
+58
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import json
import logging
from threading import Lock
from typing import Any
import click
import pytest
@@ -87,6 +88,63 @@ def test_a_lost_session_reopens_and_replays_the_call_under_the_new_ids(monkeypat
assert service._title_alias == {"ep3-aaaa": "ep3-bbbb"}
def rest_song(id_: str, album: str, position: Any = None, track: int = 1) -> dict:
return {"type": "song", "id": id_, "name": "N", "artist": "A", "album": album, "track": track, "position": position}
@pytest.mark.parametrize(
("before", "after", "alias"),
[
pytest.param(
[rest_song("song-z", "Z", 1), rest_song("song-b", "B", 2)],
[rest_song("song-b", "B", 1), rest_song("song-z", "Z", 2)],
{},
id="reordered-playlist",
),
pytest.param(
[rest_song("song-clean", "Z", 1), rest_song("song-explicit", "Z", 2)],
[rest_song("song-explicit", "Z", 1)],
{},
id="two-versions-of-one-single",
),
pytest.param(
[rest_song("song-z", "Z"), rest_song("song-b", "B")],
[rest_song("song-z", "Z", 1), rest_song("song-b", "B", 2)],
{},
id="server-updated",
),
pytest.param(
[rest_song("song-x-old", "X"), rest_song("song-y-old", "X", track=2)],
[rest_song("song-x-new", "X"), rest_song("song-y-new", "X", track=2)],
{"song-x-old": "song-x-new", "song-y-old": "song-y-new"},
id="album-without-positions",
),
],
)
@responses.activate
def test_a_reopened_session_never_takes_another_song_for_a_playlist_song(monkeypatch, before, after, alias):
service = make_service()
service._titles = [build_title(info, "TEST", "x") for info in before]
monkeypatch.setattr(service, "authenticate", lambda credential=None: setattr(service, "_session_id", "s2"))
responses.add(responses.GET, f"{SERVER}/api/health", json={"status": "ok", "draining": False})
responses.add(responses.GET, f"{SERVER}/api/session/s2/titles", json={"titles": after})
assert service.reopen()
assert service._title_alias == alias
@responses.activate
def test_a_malformed_song_position_on_reopen_is_one_error_line(monkeypatch):
service = make_service()
monkeypatch.setattr(service, "authenticate", lambda credential=None: setattr(service, "_session_id", "s2"))
responses.add(responses.GET, f"{SERVER}/api/health", json={"status": "ok", "draining": False})
listing = {"titles": [rest_song("song-z", "Z", "x")]}
responses.add(responses.GET, f"{SERVER}/api/session/s2/titles", json=listing)
with pytest.raises(RemoteError, match="The remote server sent malformed titles: Expected position"):
service.reopen()
@pytest.mark.parametrize(
"broken",
[