refactor(dl): check dl options once in a run plan shared by the CLI and the REST download job

This commit is contained in:
imSp4rky
2026-11-25 16:16:43 -07:00
parent 7838226590
commit 4d3df8e74f
23 changed files with 501 additions and 524 deletions
+17 -55
View File
@@ -2,7 +2,7 @@
Covers serialize_title's conditional `part` key, the list-tracks wanted filter
routing through Episode.matches_wanted, the discrete season/episode/part request
fields, and download_manager's already-internal wanted sniff.
fields, and SeasonRange.convert passing already-parsed wanted keys through.
"""
from __future__ import annotations
@@ -17,6 +17,7 @@ import pytest
from unshackle.core.api import handlers
from unshackle.core.api.errors import APIError
from unshackle.core.titles.episode import Episode
from unshackle.core.utils.click_types import SEASON_RANGE
from unshackle.core.wire import build_title, serialize_title
pytestmark = pytest.mark.unit
@@ -202,68 +203,29 @@ def test_rest_multi_episode_order_is_stable_across_parts(monkeypatch) -> None:
assert selected(result) == ["1x1.1", "1x1.2", "1x1.3", "1x2"]
SNIFF = r"^!?\d+x\d+(\.\d+)?$"
@pytest.mark.parametrize(
"wanted, internal",
("wanted", "parsed"),
[
(["1x1"], True), # today's pre-parsed form
(["1x1", "1x2"], True),
(["1x1.2"], True), # part key
(["!1x1.2"], True), # negative part key
(["1x1", "!1x1.2"], True),
(["S01E01"], False),
(["s1e1.2"], False),
(["S01"], False),
(["1x1", "S01E02"], False),
(["1x1"], ["1x1"]),
(["1x1.2"], ["1x1.2"]),
(["!1x1.2"], ["!1x1.2"]),
(["1x1", "!1x1.2"], ["1x1", "!1x1.2"]),
("1x1, 1x2", ["1x1", "1x2"]),
(["1x01"], ["1x1"]),
],
)
def test_download_manager_sniff_regex(wanted: List[str], internal: bool) -> None:
needs_conversion = any(not re.match(SNIFF, w) for w in wanted)
assert needs_conversion is (not internal)
def test_parsed_keys_pass_unchanged(wanted: Any, parsed: List[str]) -> None:
"""A pre-parsed part or negative key never reaches parse_tokens, which cannot read it."""
assert SEASON_RANGE.convert(wanted) == parsed
def test_sniff_regex_matches_the_one_in_download_manager() -> None:
"""Guard the copy above against drift from the production regex."""
from pathlib import Path
import unshackle.core.api.download_manager as dm
source = Path(dm.__file__).read_text(encoding="utf8")
assert f'needs_conversion = any(not re.match(r"{SNIFF}", w) for w in wanted_raw)' in source
@pytest.mark.parametrize("wanted", [["S01E01"], "S01E01", ["s1e1"], "s1e1; S01E01"])
def test_tokens_parse_from_a_list_or_a_separated_string(wanted: Any) -> None:
assert SEASON_RANGE.convert(wanted) == ["1x1"]
def test_perform_download_skips_reconversion_for_preparsed_part_keys(monkeypatch) -> None:
"""A pre-parsed list with part and negative keys must not reach parse_tokens."""
from unshackle.core.api.download_manager import perform_download
from unshackle.core.utils.click_types import SeasonRange
calls = []
monkeypatch.setattr(SeasonRange, "parse_tokens", lambda self, *t: calls.append(t) or [])
params: Dict[str, Any] = {"wanted": ["1x1", "1x1.2", "!1x1.3"]}
with pytest.raises(BaseException):
# blows up later on the unknown service; the wanted block runs first
perform_download("job-1", "NoSuchServiceXYZ", "t1", params)
assert calls == []
assert params["wanted"] == ["1x1", "1x1.2", "!1x1.3"]
def test_perform_download_still_converts_cli_style_tokens(monkeypatch) -> None:
from unshackle.core.api.download_manager import perform_download
from unshackle.core.utils.click_types import SeasonRange
calls = []
monkeypatch.setattr(SeasonRange, "parse_tokens", lambda self, *t: calls.append(t) or ["1x1"])
params: Dict[str, Any] = {"wanted": "S01E01"}
with pytest.raises(BaseException):
perform_download("job-2", "NoSuchServiceXYZ", "t1", params)
assert calls == [("S01E01",)]
assert params["wanted"] == ["1x1"]
def test_a_mixed_list_parses_every_token() -> None:
assert sorted(SEASON_RANGE.convert(["1x1", "S01E02"])) == ["1x1", "1x2"]
def test_dl_and_handlers_share_the_one_match_helper() -> None: