refactor(api): give every served service its dl options from the run plan

This commit is contained in:
imSp4rky
2026-11-28 14:02:44 -07:00
parent 63dd037ebd
commit 053d83874d
18 changed files with 285 additions and 205 deletions
+58
View File
@@ -146,3 +146,61 @@ def test_each_builder_sets_its_launch_keys(
) -> None:
params = build(monkeypatch, tmp_path).params
assert {key: params[key] for key in LaunchKeys.__annotations__ if key in params} == expected
def served_paths(monkeypatch: pytest.MonkeyPatch) -> dict[str, Callable[[dict], click.Context]]:
"""Each served builder, as a function of the request body to the parent context its service gets."""
from unshackle.core import remote_service
handlers, seen = rest_handlers(monkeypatch)
monkeypatch.setattr(remote_service, "make_remote_service", lambda ctx, *a, **k: ctx)
def instantiate(parent_ctx: click.Context, *a: Any) -> Any:
seen["ctx"] = parent_ctx
return SimpleNamespace(authenticate=lambda cookies, credential: None, search=lambda: iter(()))
monkeypatch.setattr(handlers, "instantiate_service", instantiate)
def captured(call: Callable[[], Any]) -> click.Context:
call()
return seen["ctx"]
return {
"relay": lambda data: handlers.relay_service(data, "server", None, "t").parent,
"list": lambda data: captured(lambda: handlers.setup_list_service(data, "EXAMPLE", None, "t")),
"search": lambda data: captured(lambda: handlers.run_service_search(data, "EXAMPLE", "query")),
"session": lambda data: captured(
lambda: handlers.create_service_instance("EXAMPLE", "t", data, None, [], None)
),
}
@pytest.mark.parametrize("path", ["relay", "list", "search", "session"])
def test_every_served_path_takes_its_dl_options_from_the_served_plan(
monkeypatch: pytest.MonkeyPatch, path: str
) -> None:
from unshackle.core.tracks import Video
data = {"service": "EXAMPLE", "service_params": {}, "range_": ["DV"], "dl_params": {"a_lang": ["en"]}}
params = served_paths(monkeypatch)[path](data).params
assert (params["range_"], params["a_lang"], params["served"]) == ([Video.Range.DV], ["en"], True)
@pytest.mark.parametrize("path", ["relay", "list", "search", "session"])
def test_every_served_path_refuses_an_unknown_range(monkeypatch: pytest.MonkeyPatch, path: str) -> None:
from unshackle.core.api.errors import APIError
from unshackle.core.remote_protocol import APIErrorCode
with pytest.raises(APIError) as exc:
served_paths(monkeypatch)[path]({"service": "EXAMPLE", "service_params": {}, "range_": ["NOPE"]})
assert exc.value.error_code is APIErrorCode.INVALID_PARAMETERS
def test_a_relay_session_is_pooled_per_dl_selection() -> None:
from unshackle.core.api.handlers import remote_pool_key
data = {"service": "EXAMPLE", "dl_params": {"a_lang": ["en"]}}
keys = {
remote_pool_key(d, "server", {}, None, "t", None) for d in (data, {**data, "dl_params": {"a_lang": ["fr"]}})
}
assert len(keys) == 2