From 9693cbd39c8b957e60b1ec3d8d880cba9f01b12d Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Mon, 16 Nov 2026 14:44:47 -0700 Subject: [PATCH] fix(api): convert quality to heights for remote sessions A string or zero -q from a REST client reached the service as raw text; it now gets the same checked height list as dl. --- tests/remote/unit/test_api_quality_param.py | 28 +++++++++++++++++++++ unshackle/core/api/handlers.py | 16 +++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/tests/remote/unit/test_api_quality_param.py b/tests/remote/unit/test_api_quality_param.py index 0cf652ea..71b96d94 100644 --- a/tests/remote/unit/test_api_quality_param.py +++ b/tests/remote/unit/test_api_quality_param.py @@ -48,3 +48,31 @@ def test_a_rest_job_converts_quality_and_passes_cdm_only(tmp_path: Path, monkeyp download_manager.perform_download("job-1", "EXAMPLE1", "t1", {"quality": "1080p", "cdm_only": True}) assert exc.value.args == ([1080], [1080], True) + + +def session_params(monkeypatch: pytest.MonkeyPatch, data: dict) -> dict: + """The params a remote session's service reads from its parent ctx, with the service machinery stubbed.""" + seen: dict = {} + + def fake_instantiate(parent_ctx: click.Context, *args: object) -> object: + seen["params"] = parent_ctx.params + return object() + + monkeypatch.setattr(handlers, "load_service_yaml", lambda service: {}) + monkeypatch.setattr(handlers, "load_full_cdm", lambda *args: None) + monkeypatch.setattr(handlers.Services, "load", lambda service: None) + monkeypatch.setattr(handlers, "instantiate_service", fake_instantiate) + handlers.create_service_instance("EXAMPLE", "title", data, None, [], None) + return seen["params"] + + +@pytest.mark.parametrize(("quality", "heights"), [("1080p", [1080]), (["2160", "342"], [2160, 342]), (None, [])]) +def test_a_remote_session_gets_quality_as_heights(monkeypatch: pytest.MonkeyPatch, quality: Any, heights: list) -> None: + assert session_params(monkeypatch, {"quality": quality})["quality"] == heights + + +@pytest.mark.parametrize("quality", ["tall", "0"]) +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 diff --git a/unshackle/core/api/handlers.py b/unshackle/core/api/handlers.py index 9ab59140..d746c650 100644 --- a/unshackle/core/api/handlers.py +++ b/unshackle/core/api/handlers.py @@ -1251,16 +1251,20 @@ def capped_error(message: str, cap: Optional[int] = None) -> APIError: return APIError(APIErrorCode.SERVER_CDM_CAPPED, message, details=details) -def requested_height(quality: Any) -> Optional[int]: - """The tallest height a client asked for with ``-q``; None when it asked for the best there is.""" - from unshackle.core.utils.click_types import QUALITY_LIST - +def requested_heights(quality: Any) -> list[int]: + """The heights a client asked for with ``-q``, as the dl option gives them; empty for the best there is.""" try: - heights = QUALITY_LIST.convert(quality) + heights = QUALITY_LIST.convert(quality or []) except click.BadParameter as e: raise APIError(APIErrorCode.INVALID_INPUT, f"quality: {e.message}") if any(height <= 0 for height in heights): raise APIError(APIErrorCode.INVALID_INPUT, f"quality: every height must be above 0, not {quality!r}") + return heights + + +def requested_height(quality: Any) -> Optional[int]: + """The tallest height a client asked for with ``-q``; None when it asked for the best there is.""" + heights = requested_heights(quality) return max(heights) if heights else None @@ -3909,7 +3913,7 @@ def create_service_instance( extra_params = { "range_": range_values, "vcodec": vcodec_values, - "quality": data.get("quality"), + "quality": requested_heights(data.get("quality")), "best_available": data.get("best_available", False), "generate_dv": data.get("generate_dv", False), **forwarded_dl_params(data),