feat(tracks): resolve a track url on use, locally and over remote

This commit is contained in:
imSp4rky
2026-11-27 01:45:49 -07:00
parent 736f1cc731
commit 9901b62347
9 changed files with 198 additions and 8 deletions
@@ -326,6 +326,46 @@ def test_a_cipher_the_server_does_not_license_raises(grants, answer, message):
assert stopped.value.error_code == "DRM_ERROR"
def test_track_url_asks_the_segments_route_for_the_url_of_one_track():
"""A track the server sent with resolve_url gets its URL from the server when it is downloaded."""
svc = remote_service()
svc.drain_server_logs = lambda: None
sent = []
svc.client = SimpleNamespace(
post=lambda ep, data, **kw: sent.append((ep, data)) or {"tracks": {"a-1": {"url": "https://cdn.example/a"}}}
)
assert svc.track_url(SimpleNamespace(id="a-1")) == "https://cdn.example/a"
assert [data for _, data in sent] == [{"track_ids": ["a-1"]}]
assert sent[0][0].endswith("/segments")
@pytest.mark.parametrize(
"answer",
[
{"tracks": {"a-1": {"url": None}}},
{"tracks": {}},
RemoteError("Track not found in session: a-1", "TRACK_NOT_FOUND"),
],
ids=["no-url", "no-track", "refused"],
)
def test_a_track_the_server_gives_no_url_raises(answer):
"""A track with no URL from the server stops at its download with one line."""
svc = remote_service()
svc.drain_server_logs = lambda: None
def post(ep, data, **kw):
if isinstance(answer, BaseException):
raise answer
return answer
svc.client = SimpleNamespace(post=post)
with pytest.raises(click.ClickException, match="no URL for track a-1|Track not found"):
svc.track_url(SimpleNamespace(id="a-1"))
def resolve(manifest_drm, drm_type="widevine"):
"""Run resolve_server_keys for one track the server answers with its own key and a sibling."""
svc = remote_service()
+80
View File
@@ -0,0 +1,80 @@
from __future__ import annotations
import logging
import pytest
from unshackle.core.tracks import Audio
from unshackle.core.wire import build_tracks, serialize_audio_track, served_url
def audio(url: str = "") -> Audio:
return Audio(url=url, id_="a1", language="en", codec=Audio.Codec.AAC, bitrate=24000, channels=2.0)
def counted(calls: list[int]) -> str:
calls.append(1)
return "https://cdn.example/a"
def test_ensure_url_resolves_once() -> None:
track, calls = audio(), list[int]()
track.resolve_url = lambda: counted(calls)
track.ensure_url()
track.ensure_url()
assert track.url == "https://cdn.example/a"
assert calls == [1]
def test_ensure_url_keeps_a_given_url() -> None:
track = audio("https://cdn.example/given")
track.resolve_url = lambda: pytest.fail("a track with a URL must not resolve one")
track.ensure_url()
assert track.url == "https://cdn.example/given"
def test_a_track_with_no_url_and_no_resolver_stays_empty() -> None:
track = audio()
track.ensure_url()
assert track.url == ""
assert served_url(track) is None
def test_the_track_list_marks_a_track_that_resolves_its_url_on_use() -> None:
track, calls = audio(), list[int]()
track.resolve_url = lambda: counted(calls)
sent = serialize_audio_track(track, include_url=True)
assert sent["resolve_url"] is True and "url" not in sent
assert calls == []
assert serialize_audio_track(audio("https://cdn.example/b"), include_url=True)["url"] == "https://cdn.example/b"
def test_a_marked_track_arrives_with_no_url_and_others_keep_theirs() -> None:
tracks = build_tracks(
{
"audio": [
{"id": "a1", "codec": "AAC", "resolve_url": True},
{"id": "a2", "codec": "AAC", "url": "https://x/2"},
]
}
)
assert [track.url for track in tracks.audio] == ["", "https://x/2"]
def test_the_segments_route_resolves_the_url() -> None:
track, calls = audio(), list[int]()
track.resolve_url = lambda: counted(calls)
assert served_url(track) == "https://cdn.example/a"
assert calls == [1]
def test_a_failed_resolve_gives_no_url(caplog: pytest.LogCaptureFixture) -> None:
track = audio()
def refuse() -> str:
raise RuntimeError("HTTP 404")
track.resolve_url = refuse
with caplog.at_level(logging.WARNING, logger="api"):
assert served_url(track) is None
assert "Could not resolve the URL of track a1: HTTP 404" in caplog.text