From 20e06665a199eea599aef8218b3df1ec11bbe029 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Wed, 2 Dec 2026 17:58:02 -0700 Subject: [PATCH] fix(remote): send the off form of a service --x/--no-x flag over --remote --- docs/dev/rest-api/endpoints.md | 4 +- tests/remote/unit/test_remote_flag_options.py | 81 +++++++++++++++++++ unshackle/core/api/routes.py | 1 + unshackle/core/remote_service.py | 10 ++- unshackle/core/services.py | 1 + 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 tests/remote/unit/test_remote_flag_options.py diff --git a/docs/dev/rest-api/endpoints.md b/docs/dev/rest-api/endpoints.md index b69c8058..59b5cac9 100644 --- a/docs/dev/rest-api/endpoints.md +++ b/docs/dev/rest-api/endpoints.md @@ -95,6 +95,7 @@ Show the streaming services available on this server, filtered by your allowlist "name": "movie", "kind": "option", "opts": ["-m", "--movie"], + "secondary_opts": [], "is_flag": true, "default": false, "help": "Title is a movie.", @@ -124,7 +125,7 @@ Field notes: | `title_regex` | URL/ID pattern the service matches, as a string or list of strings (or `null`). | | `url` | Service URL, taken from the service's CLI short help. | | `help` | The service module's docstring. | -| `cli_params` | Array of parameter descriptors. Arguments have `{name, kind: "argument", required}`; options have `{name, kind: "option", opts, is_flag, default, help, type, multiple}` and, for choice options, a `choices` list. | +| `cli_params` | Array of parameter descriptors. Arguments have `{name, kind: "argument", required}`; options have `{name, kind: "option", opts, secondary_opts, is_flag, default, help, type, multiple}` and, for choice options, a `choices` list. `secondary_opts` holds the off names of a boolean flag such as `--x/--no-x`, and is empty for other options. | | `needs_auth` | Whether the service overrides `authenticate()`. | | `has_search` | Whether the service supports `POST /api/search`. | | `anonymous_search` | Whether `POST /api/search` operates with no login for this service. The server then ignores `credentials` and `cookies` on a search. | @@ -208,6 +209,7 @@ Get the list of titles behind a title ID, for example episodes or a movie, witho You can also pass a service's own CLI options, in a nested `service_params` object or as additional keys. When the body has `service_params`, the server reads service options only from it. +To turn off a flag such as `--x/--no-x`, send `false` under its name (`"x": false`), not under its off name. #### Logins that need the user diff --git a/tests/remote/unit/test_remote_flag_options.py b/tests/remote/unit/test_remote_flag_options.py new file mode 100644 index 00000000..7e1d36ab --- /dev/null +++ b/tests/remote/unit/test_remote_flag_options.py @@ -0,0 +1,81 @@ +"""A service --flag/--no-flag option over --remote: the listing, the client command and the params sent to the server.""" + +from __future__ import annotations + +import json +from typing import Any +from unittest.mock import patch + +import click +import pytest + +from unshackle.core.api import handlers, routes +from unshackle.core.remote_service import remote_service_params +from unshackle.core.service import Service +from unshackle.core.services import Services + + +class EXAMPLE(Service): + """Example service.""" + + @staticmethod + @click.command(name="EXAMPLE") + @click.argument("title", type=str) + @click.option("--flag/--no-flag", default=True) + @click.option("--loud", is_flag=True, default=False) + @click.option("--quiet/--no-quiet", " /-Q", default=lambda: True) + @click.pass_context + def cli(ctx: click.Context, **kwargs: Any) -> dict[str, Any]: + return kwargs + + +@pytest.fixture +def listing(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]: + monkeypatch.setattr(Services, "load", staticmethod(lambda tag: EXAMPLE)) + monkeypatch.setattr(routes, "SERVICE_DETAILS", {}) + return dict(json.loads(json.dumps(routes.service_details("EXAMPLE")))) + + +def sent(info: dict[str, Any], argv: list[str]) -> dict[str, Any]: + with patch.object(Services, "fetch_remote_services", return_value=[info]): + cmd = Services.make_remote_command("EXAMPLE", None) + params = cmd.make_context("EXAMPLE", [*argv, "an-id"]).params + return remote_service_params(info, {"service_params": {k: v for k, v in params.items() if k != "title"}}) + + +def served(params: dict[str, Any]) -> dict[str, Any]: + ctx = click.Context(click.Command("dl")) + return dict(handlers.instantiate_service(ctx, EXAMPLE, "an-id", {"service_params": params})) + + +def test_no_flag_reaches_the_server_service(listing: dict[str, Any]) -> None: + params = sent(listing, ["--no-flag"]) + assert params == {"flag": False} + assert served(params) == {"title": "an-id", "flag": False, "loud": False, "quiet": True} + + +def test_a_secondary_only_alias_with_a_callable_default_reaches_the_server(listing: dict[str, Any]) -> None: + params = sent(listing, ["-Q"]) + assert params == {"flag": True, "quiet": False} + assert served(params)["quiet"] is False + + +def test_a_default_value_is_sent_as_before(listing: dict[str, Any]) -> None: + assert sent(listing, []) == {"flag": True} + assert sent(listing, ["--loud"]) == {"flag": True, "loud": True} + + +def test_a_listing_without_secondary_names_keeps_the_primary_flag_only(listing: dict[str, Any]) -> None: + for p in listing["cli_params"]: + p.pop("secondary_opts", None) + assert sent(listing, ["--flag"]) == {"flag": True} + with pytest.raises(click.NoSuchOption): + sent(listing, ["--no-flag"]) + + +def test_a_rest_relay_sends_false_for_a_default_true_flag(listing: dict[str, Any]) -> None: + assert remote_service_params(listing, {"service_params": {"flag": False, "loud": False}}) == {"flag": False} + options, _ = handlers.requested_service_options( + {"service_params": {"flag": False}}, EXAMPLE.cli.params, handlers.DL_TRANSPORT_KEYS + ) + assert options == {"flag": False} diff --git a/unshackle/core/api/routes.py b/unshackle/core/api/routes.py index 46d783f7..e0c47ab6 100644 --- a/unshackle/core/api/routes.py +++ b/unshackle/core/api/routes.py @@ -406,6 +406,7 @@ def service_details(tag: str) -> dict[str, Any]: else: param_info["kind"] = "option" param_info["opts"] = list(param.opts) if hasattr(param, "opts") else [] + param_info["secondary_opts"] = list(getattr(param, "secondary_opts", [])) param_info["is_flag"] = getattr(param, "is_flag", False) default = param.default if callable(default) or type(default).__name__ == "Sentinel": diff --git a/unshackle/core/remote_service.py b/unshackle/core/remote_service.py index 755984f8..4ed1cb56 100644 --- a/unshackle/core/remote_service.py +++ b/unshackle/core/remote_service.py @@ -581,9 +581,13 @@ def find_remote_service(services: list[dict], name: str) -> dict | None: def remote_service_params(info: dict, data: dict[str, Any]) -> dict[str, Any]: - """The remote service's cli options in ``data``, read by ``service_options``.""" - names = {p.get("name") for p in info.get("cli_params") or [] if p.get("kind") == "option"} - return {k: v for k, v in service_options(data, names).items() if v is not None and v is not False} + """The remote service's cli options in ``data``, without None, and without False for an option off by default.""" + defaults = {p.get("name"): p.get("default") for p in info.get("cli_params") or [] if p.get("kind") == "option"} + return { + k: v + for k, v in service_options(data, defaults).items() + if v is not None and (v is not False or defaults[k] is not False) + } def make_remote_service( diff --git a/unshackle/core/services.py b/unshackle/core/services.py index 57e65cfb..9aabded7 100644 --- a/unshackle/core/services.py +++ b/unshackle/core/services.py @@ -561,6 +561,7 @@ class Services(click.Group): for param in cli_params: if param.get("kind") == "option": opts = param.get("opts", [f"--{param['name']}"]) + opts = [*opts, *(f" /{s}" for s in param.get("secondary_opts") or [])] kwargs: dict = {} if param.get("is_flag"): kwargs["is_flag"] = True