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
-35
View File
@@ -1,7 +1,6 @@
from __future__ import annotations
import inspect
import logging
import click
import pytest
@@ -16,7 +15,6 @@ from unshackle.core.dl_options import (
DL_OPTIONS,
FORWARDED_DL_PARAMS,
cast_dl_params,
forwarded_dl_params,
remote_dl_params,
)
from unshackle.core.tracks import Audio, Subtitle, Video
@@ -39,19 +37,6 @@ def test_the_openapi_yaml_names_each_api_dl_option_and_no_other():
assert properties.keys() - DEFAULT_DOWNLOAD_PARAMS.keys() == {"service", "title_id", "remote", "client_name", "cdm"}
def test_the_server_reads_back_what_a_remote_client_forwards():
params = {
"lang": ["orig", "es-419"],
"v_lang": [],
"a_lang": ["en"],
"s_lang": ["all"],
"acodec": [Audio.Codec.EC3],
"forced_subs": True,
"exact_lang": True,
}
assert forwarded_dl_params({"dl_params": remote_dl_params(params)}) == params
def test_api_defaults_cast_to_the_dl_defaults():
"""``wanted`` keeps its API default of no keys, which dl reads as every title, like the CLI's None."""
ctx = click.Context(dl.cli)
@@ -121,26 +106,6 @@ def test_every_dl_option_reaches_dl_result():
}
def test_the_server_reads_the_forwarded_dl_params(caplog):
data = {
"dl_params": {"a_lang": "x", "acodec": ["ddp", "NOPE"], "exact_lang": 1, "forced_subs": True, "lang": ["en"]}
}
with caplog.at_level(logging.WARNING, logger="api"):
assert list(forwarded_dl_params(data).items()) == [
("lang", ["en"]),
("v_lang", []),
("a_lang", []),
("s_lang", ["all"]),
("acodec", [Audio.Codec.EC3]),
("forced_subs", True),
("exact_lang", False),
]
assert [(r.name, r.getMessage()) for r in caplog.records] == [
("api", "Ignoring dl_params.a_lang: it must be an array of strings"),
("api", "Ignoring unknown dl_params.acodec value: NOPE"),
]
@pytest.mark.parametrize(
"parent_params,sent",
[
+1 -1
View File
@@ -74,4 +74,4 @@ def test_a_remote_session_gets_quality_as_heights(monkeypatch: pytest.MonkeyPatc
def test_a_remote_session_refuses_a_malformed_quality(monkeypatch: pytest.MonkeyPatch, quality: str) -> None:
with pytest.raises(handlers.APIError) as exc:
session_params(monkeypatch, {"quality": quality})
assert exc.value.error_code == handlers.APIErrorCode.INVALID_INPUT
assert exc.value.error_code == handlers.APIErrorCode.INVALID_PARAMETERS
+3 -2
View File
@@ -21,6 +21,7 @@ from unshackle.core.api.input_bridge import InputBridge
from unshackle.core.cdm.client_relay import ClientDeviceCdm, answer_cdm_call
from unshackle.core.launch import served_context
from unshackle.core.remote_protocol import APIErrorCode, AuthStatus
from unshackle.core.run_plan import served_plan
from unshackle.core.utils.click_types import ContextData
pytestmark = pytest.mark.unit
@@ -174,7 +175,7 @@ def test_a_relaying_client_gets_a_stand_in_with_its_device_facts(monkeypatch: py
monkeypatch.setattr(
handlers,
"served_context",
lambda obj, params, keys: built.update(cdm=obj.cdm, config=obj.config, params={**params, **keys}),
lambda obj, plan, keys: built.update(cdm=obj.cdm, config=obj.config, params={**plan.params, **keys}),
)
monkeypatch.setattr(handlers.Services, "load", lambda tag: Declares)
monkeypatch.setattr(handlers, "instantiate_service", lambda *a, **k: "service")
@@ -502,7 +503,7 @@ def test_a_lent_device_without_the_vault_grant_asks_the_server_for_no_key() -> N
@pytest.mark.parametrize("params", [{}, {"quality": None}])
def test_every_service_context_defaults_quality_to_a_list(params: Any) -> None:
parent = served_context(ContextData(config={}, cdm=None, proxy_providers=[]), params, {})
parent = served_context(ContextData(config={}, cdm=None, proxy_providers=[]), served_plan(params), {})
assert parent.params["quality"] == []
@@ -365,17 +365,3 @@ async def test_search_unknown_service_raises_invalid_service() -> None:
with pytest.raises(APIError) as exc:
await search_handler({"service": "NOPE_THIS_IS_NOT_REAL_", "query": "x"}, None)
assert exc.value.error_code == APIErrorCode.INVALID_SERVICE
def test_resolve_vcodec_rejects_junk_as_invalid_input() -> None:
"""Session routes skip the download job admission, so a bad codec must be a 400, not a 500."""
from unshackle.core.api.handlers import resolve_vcodec
from unshackle.core.remote_protocol import APIErrorCode
from unshackle.core.tracks import Video
assert resolve_vcodec(["H265"]) == [Video.Codec.HEVC]
assert resolve_vcodec(None) is None
for junk in ("XVID", 5, [5]):
with pytest.raises(APIError) as exc:
resolve_vcodec(junk)
assert exc.value.error_code == APIErrorCode.INVALID_INPUT
+2 -2
View File
@@ -202,9 +202,9 @@ async def test_list_tracks_service_sees_dl_params(seen):
@pytest.mark.asyncio
async def test_search_service_gets_no_dl_params(seen):
async def test_search_service_sees_dl_params(seen):
await handlers.search_handler({"service": "EXAMPLE", "query": "q", "dl_params": {"a_lang": ["es-419"]}})
assert "a_lang" not in seen["parent_params"]
assert seen["parent_params"]["a_lang"] == ["es-419"]
@pytest.mark.asyncio
+12 -11
View File
@@ -178,7 +178,7 @@ def test_nested_dl_params_reach_the_parent_ctx(monkeypatch):
"v_lang": ["ja"],
"a_lang": ["es-419"],
"s_lang": ["ko"],
"acodec": ["EC3", "NOPE", "AAC"],
"acodec": ["EC3", "AAC"],
"forced_subs": True,
"exact_lang": True,
}
@@ -211,20 +211,21 @@ def test_a_service_lang_option_never_gets_the_dl_lang(monkeypatch):
assert "dl_params" in handlers.SESSION_TRANSPORT_KEYS
@pytest.mark.parametrize(
"dl_params",
[
"en",
{"lang": "en", "v_lang": "ja", "a_lang": {"x": 1}, "acodec": "EC3", "forced_subs": "yes", "exact_lang": "yes"},
{"lang": [1], "v_lang": [None], "a_lang": ["en", None], "acodec": [3], "forced_subs": 1, "exact_lang": 1},
],
)
def test_malformed_dl_params_are_ignored(monkeypatch, dl_params):
svc = _create_with_real_ctx(monkeypatch, {"dl_params": dl_params})
def test_dl_params_that_are_not_an_object_are_ignored(monkeypatch):
svc = _create_with_real_ctx(monkeypatch, {"dl_params": "en"})
selection = {k: svc.parent_params[k] for k in (*DL_DEFAULTS,)}
assert selection == DL_DEFAULTS
@pytest.mark.parametrize(
"dl_params", [{"a_lang": {"x": 1}}, {"acodec": ["EC3", "NOPE"]}, {"acodec": [3]}, {"forced_subs": "maybe"}]
)
def test_a_malformed_dl_param_is_refused(monkeypatch, dl_params):
with pytest.raises(handlers.APIError) as exc:
_create_with_real_ctx(monkeypatch, {"dl_params": dl_params})
assert exc.value.error_code is handlers.APIErrorCode.INVALID_PARAMETERS
def test_profile_reaches_the_parent_ctx_params(monkeypatch):
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
@@ -108,10 +108,10 @@ def test_above_the_cap_without_a_device_is_refused_at_create():
assert ei.value.details == {"reason": "server_cdm_max_height", "max_height": 1080}
def test_bad_quality_is_invalid_input():
def test_bad_quality_is_invalid_parameters():
with pytest.raises(APIError) as ei:
handlers.choose_session_cdm(KeyPolicy(USERS["flat"]), "EXAMPLE1", {"quality": ["best"]})
assert ei.value.error_code is APIErrorCode.INVALID_INPUT
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
@pytest.mark.parametrize(
@@ -249,10 +249,10 @@ def test_an_ultrawide_track_is_measured_as_the_smallest_quality_that_selects_it(
@pytest.mark.parametrize("quality", [[-1], [0], [1080, 0]])
def test_a_height_of_zero_or_less_is_invalid_input(quality):
def test_a_height_of_zero_or_less_is_invalid_parameters(quality):
with pytest.raises(APIError) as ei:
job_admission.enforce_download_gates({"service": "EXAMPLE1", "quality": quality}, request("flat"))
assert ei.value.error_code is APIErrorCode.INVALID_INPUT
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
def test_a_capped_download_job_cannot_set_best_available():