fix(api): cast download job options with the dl click types

cast_dl_params feeds ctx.params to dl() and dl.result() as on the CLI. A string lang value now returns 400 instead of splitting into letters, and -r accepts HDR10+.
This commit is contained in:
imSp4rky
2026-11-02 20:30:40 -07:00
parent dc188b7276
commit eeff83b820
18 changed files with 284 additions and 284 deletions
+132
View File
@@ -0,0 +1,132 @@
from __future__ import annotations
import click
import pytest
from unshackle.commands.dl import dl
from unshackle.core.api.handlers import (
API_SHAPED_DL_PARAMS,
DEFAULT_DOWNLOAD_PARAMS,
cast_dl_params,
validate_download_parameters,
)
from unshackle.core.tracks import Video
CLI_ONLY = {
"select_titles",
"list_",
"list_titles",
"import_file",
"cdm_name",
"remote",
"server",
"local_proxy",
"postscript",
"no_postscript",
"merge_video",
"real_video_bitrate",
"real_audio_bitrate",
"speed_limit",
}
def test_every_dl_option_is_an_api_param_or_cli_only():
"""A new dl option fails here until it is added to DEFAULT_DOWNLOAD_PARAMS or to CLI_ONLY."""
api_names = set(cast_dl_params(dl.cli, {}))
cli_names = {p.name for p in dl.cli.params}
assert api_names - cli_names == set()
assert cli_names - api_names == CLI_ONLY
def test_api_defaults_cast_to_the_dl_defaults():
"""An API-shaped option keeps its own empty default, which perform_download normalizes."""
ctx = click.Context(dl.cli)
expected = {
p.name: p.type_cast_value(ctx, p.get_default(ctx))
for p in dl.cli.params
if p.name not in CLI_ONLY | API_SHAPED_DL_PARAMS
}
cast = cast_dl_params(dl.cli, {})
assert {k: v for k, v in cast.items() if k not in API_SHAPED_DL_PARAMS} == expected
def test_cli_only_options_never_reach_dl():
cast = cast_dl_params(dl.cli, {"postscript": ["rm -rf /"], "speed_limit": "1M", "list_": True})
assert not CLI_ONLY & cast.keys()
@pytest.mark.parametrize(
"value,expected",
[
("HDR10+", [Video.Range.HDR10P]),
(["dv", "HDR10+"], [Video.Range.DV, Video.Range.HDR10P]),
([], [Video.Range.SDR]),
(None, [Video.Range.SDR]),
],
)
def test_range_takes_the_api_spellings(value, expected):
assert cast_dl_params(dl.cli, {"range": value})["range_"] == expected
def test_values_are_cast_by_the_dl_click_types():
cast = cast_dl_params(dl.cli, {"quality": "1080p,720", "vcodec": "H265", "acodec": ["EC3"], "sample": True})
assert cast["quality"] == [1080, 720]
assert cast["vcodec"] == [Video.Codec.HEVC]
assert [c.name for c in cast["acodec"]] == ["EC3"]
assert cast["sample"] is not None
@pytest.mark.parametrize("key", ["lang", "v_lang", "a_lang", "s_lang", "forced_s_lang"])
@pytest.mark.parametrize("value", ["en", "en,fr", [1], {"x": "en"}])
def test_api_rejects_a_non_array_language_value(key, value):
"""A string would reach dl.result and split into one-character language codes."""
assert validate_download_parameters({key: value}) == f"{key} must be an array of language strings"
@pytest.mark.parametrize(
"data,error",
[
({"tvdb_order": "nope"}, "Invalid tvdb_order"),
({"tag": ["a", "b"]}, "Invalid tag: expected a single value"),
({"proxy": {"host": "x"}}, "Invalid proxy: expected a single value"),
],
)
def test_api_refuses_a_value_the_dl_option_cannot_take(data, error):
assert (validate_download_parameters(data) or "").startswith(error)
def test_no_dl_option_needs_a_parse_step_the_cast_skips():
"""type_cast_value runs no callback and no required check, so neither may appear on a dl option."""
assert not [p.name for p in dl.cli.params if p.callback or p.required]
def test_null_takes_the_api_default():
assert cast_dl_params(dl.cli, {key: None for key in DEFAULT_DOWNLOAD_PARAMS}) == cast_dl_params(dl.cli, {})
@pytest.mark.parametrize("key", ["downloads", "workers", "channels", "latest_episodes"])
def test_a_boolean_is_not_a_number(key):
with pytest.raises(click.BadParameter, match=f"Invalid {key}: expected a number"):
cast_dl_params(dl.cli, {key: True})
@pytest.mark.parametrize("value", [[1], [None], {"a": 1}])
def test_a_non_string_range_is_a_validation_error(value):
assert (validate_download_parameters({"range": value}) or "").startswith("Invalid range")
def test_the_worker_keeps_the_code_of_an_api_error(tmp_path, monkeypatch):
import json
from unshackle.core.api import download_worker
from unshackle.core.api.errors import APIError, APIErrorCode
def refuse(*args, **kwargs):
raise APIError(APIErrorCode.INVALID_PARAMETERS, "Invalid workers: expected a number, not a boolean")
monkeypatch.setattr(download_worker, "perform_download", refuse)
monkeypatch.setattr(download_worker, "relay_prompts", lambda callback: None)
payload, result = tmp_path / "payload.json", tmp_path / "result.json"
payload.write_text(json.dumps({"job_id": "j", "service": "EXAMPLE", "title_id": "t", "parameters": {}}))
download_worker.main(["download_worker", str(payload), str(result)])
assert json.loads(result.read_text())["error_code"] == "INVALID_PARAMETERS"
+1 -1
View File
@@ -27,7 +27,7 @@ def test_the_api_accepts_the_quality_forms_the_cli_accepts(quality: Any) -> None
def test_a_rest_job_converts_quality_and_passes_cdm_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
class FakeDl:
cli = click.Command("dl")
cli = dl_module.dl.cli
def __init__(self, ctx: click.Context, **kwargs: object) -> None:
self.ctx = ctx
@@ -171,4 +171,4 @@ def test_range_validation_accepts_hdr10p_and_alias():
def test_range_validation_rejects_unknown_and_lists_hdr10p():
err = handlers.validate_download_parameters({"range": ["HDR99"]})
assert err and "HDR10P" in err and "HDR99" in err
assert err and "hdr10p" in err.lower() and "HDR99" in err
+48 -22
View File
@@ -135,27 +135,14 @@ def test_job_status_values() -> None:
}
def test_normalize_sub_format_preserves_original_sentinel() -> None:
"""An API job asking for sub_format="original" must keep the "original" sentinel.
dl.py treats "original" as "keep source format" (skip conversion). If it were normalized to
None (as to_enum(["original"]) yields, since there is no such enum member), dl.py would fall
into the default branch and re-encode TTML -> WebVTT, contradicting the CLI.
"""
from unshackle.core.api.download_manager import normalize_sub_format
def test_api_sub_format_keeps_the_original_sentinel() -> None:
"""sub_format "original" keeps the source format, so the API cast must not turn it into a codec or None."""
from unshackle.commands.dl import dl
from unshackle.core.api.handlers import cast_dl_params
from unshackle.core.tracks import Subtitle
from unshackle.core.utils.click_types import SubtitleCodecChoice
# API path keeps the sentinel, case-insensitively.
assert normalize_sub_format("original") == "original"
assert normalize_sub_format("ORIGINAL") == "original"
# The CLI produces the exact same sentinel value, so dl.py's `== "original"` branch handles both.
assert SubtitleCodecChoice(Subtitle.Codec).convert("original") == "original"
# A real codec still maps to its enum member; unknowns collapse to None.
assert normalize_sub_format("srt") is Subtitle.Codec.SubRip
assert normalize_sub_format("nonsense") is None
assert cast_dl_params(dl.cli, {"sub_format": "ORIGINAL"})["sub_format"] == "original"
assert cast_dl_params(dl.cli, {"sub_format": "srt"})["sub_format"] is Subtitle.Codec.SubRip
def test_worker_write_result_retries_denied_replace(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
@@ -218,7 +205,7 @@ def test_perform_download_puts_the_lang_selection_in_the_service_ctx(
from unshackle.core.services import Services
class FakeDl:
cli = click.Command("dl")
cli = dl_module.dl.cli
def __init__(self, ctx: click.Context, **kwargs: object) -> None:
raise _CapturedCtx(ctx)
@@ -245,7 +232,7 @@ def test_perform_download_passes_latest_episodes_to_dl_result(tmp_path: Path, mo
from unshackle.core.services import Services
class FakeDl:
cli = click.Command("dl")
cli = dl_module.dl.cli
def __init__(self, ctx: click.Context, **kwargs: object) -> None:
pass
@@ -268,6 +255,45 @@ def test_perform_download_passes_latest_episodes_to_dl_result(tmp_path: Path, mo
assert exc.value.args[0]["latest_episodes"] == 3
def test_perform_download_gives_dl_the_defaults_for_null_and_empty_values(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
import click
import unshackle.commands.dl as dl_module
from unshackle.core.api import download_manager, handlers
from unshackle.core.services import Services
seen: dict[str, dict] = {}
class FakeDl:
cli = dl_module.dl.cli
def __init__(self, ctx: click.Context, **kwargs: object) -> None:
seen["init"] = kwargs
def result(self, **kwargs: object) -> None:
seen["result"] = kwargs
raise RuntimeError("stop")
class FakeService:
def __init__(self, ctx: click.Context) -> None:
seen["parent"] = dict(ctx.parent.params)
monkeypatch.setattr(dl_module, "dl", FakeDl)
monkeypatch.setattr(Services, "get_path", staticmethod(lambda s: tmp_path))
monkeypatch.setattr(Services, "load", staticmethod(lambda s: FakeService))
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
params: dict[str, object] = {"quality": None, "vcodec": None, "acodec": None, "lang": None, "output_dir": ""}
with pytest.raises(RuntimeError, match="stop"):
download_manager.perform_download("job-1", "EXAMPLE", "t1", params)
assert seen["init"]["output_dir"] is None
for view in (seen["parent"], seen["result"]):
assert view["quality"] == [] and view["vcodec"] == [] and view["acodec"] == [] and view["lang"] == ["orig"]
def test_perform_download_casts_service_options(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
import click
@@ -276,7 +302,7 @@ def test_perform_download_casts_service_options(tmp_path: Path, monkeypatch: pyt
from unshackle.core.services import Services
class FakeDl:
cli = click.Command("dl")
cli = dl_module.dl.cli
def __init__(self, ctx: click.Context, **kwargs: object) -> None:
pass
@@ -305,7 +305,6 @@ class TestDownloadEnforcement:
@pytest.mark.parametrize(("params", "expected"), [({}, []), ({"server_proxy": True}, None)])
def test_perform_download_maps_stamp_to_dl_providers(self, monkeypatch, tmp_path, params, expected):
"""An unstamped or denied job hands dl an empty provider list; a stamped one loads config."""
import click
from unshackle.commands import dl as dl_module
from unshackle.core.api import download_manager as dm
@@ -316,7 +315,7 @@ class TestDownloadEnforcement:
pass
class FakeDl:
cli = click.Group("dl")
cli = dl_module.dl.cli
def __init__(self, **kwargs):
seen.update(kwargs)
+2 -2
View File
@@ -291,7 +291,7 @@ def test_validate_download_params_accepts_defaults() -> None:
"data, fragment",
[
({"vcodec": "WUT"}, "Invalid vcodec"),
({"vcodec": 123}, "vcodec must be a string or list"),
({"vcodec": 123}, "Invalid vcodec"),
({"acodec": "MP9"}, "Invalid acodec"),
({"sub_format": "doc"}, "Invalid sub_format"),
({"vbitrate": -1}, "vbitrate"),
@@ -310,7 +310,7 @@ def test_validate_download_params_accepts_defaults() -> None:
({"no_audio": True, "audio_only": True}, "no_audio and audio_only"),
({"require_subs": "en"}, "require_subs must be an array"),
({"range": "UHD"}, "Invalid range"),
({"range": ["SDR", "UHD"]}, "Invalid range value"),
({"range": ["SDR", "UHD"]}, "Invalid range"),
],
)
def test_validate_download_params_errors(data: dict, fragment) -> None: