mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-06 07:55:10 +00:00
test: delete slow, timing-based and redundant tests
This commit is contained in:
@@ -4,16 +4,14 @@ from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import zlib
|
||||
from enum import Enum
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.remote.sessions import hold_session, session_entry, stub_service_build
|
||||
from tests.remote.sessions import hold_session, session_entry
|
||||
from unshackle.core.manifests.snapshot import match_track
|
||||
from unshackle.core.remote_service import RemoteService
|
||||
from unshackle.core.run_plan import session_request
|
||||
from unshackle.core.titles.episode import Episode
|
||||
from unshackle.core.titles.movie import Movie
|
||||
from unshackle.core.tracks import Audio, Subtitle, Video
|
||||
from unshackle.core.wire import (
|
||||
build_title,
|
||||
@@ -21,7 +19,6 @@ from unshackle.core.wire import (
|
||||
deserialize_audio,
|
||||
deserialize_subtitle,
|
||||
deserialize_video,
|
||||
enum_get,
|
||||
reconstruct_drm,
|
||||
serialize_audio_track,
|
||||
serialize_subtitle_track,
|
||||
@@ -37,23 +34,6 @@ def blob(text: str) -> str:
|
||||
return base64.b64encode(zlib.compress(text.encode())).decode("ascii")
|
||||
|
||||
|
||||
class _Color(Enum):
|
||||
RED = 1
|
||||
BLUE = 2
|
||||
|
||||
|
||||
def test_enum_get_known() -> None:
|
||||
assert enum_get(_Color, "RED") is _Color.RED
|
||||
|
||||
|
||||
def test_enum_get_unknown_returns_default() -> None:
|
||||
assert enum_get(_Color, "PURPLE", default=_Color.BLUE) is _Color.BLUE
|
||||
|
||||
|
||||
def test_enum_get_none_returns_default() -> None:
|
||||
assert enum_get(_Color, None, default=_Color.RED) is _Color.RED
|
||||
|
||||
|
||||
def test_deserialize_video_minimal() -> None:
|
||||
v = deserialize_video({"id": "video-1", "codec": "AVC", "width": 1920, "height": 1080, "bitrate": 5000})
|
||||
assert isinstance(v, Video)
|
||||
@@ -87,10 +67,6 @@ def test_a_track_note_shows_over_remote_and_export_and_is_not_an_edition() -> No
|
||||
assert copy.edition == []
|
||||
|
||||
|
||||
def test_a_note_that_is_not_text_is_dropped_not_fatal() -> None:
|
||||
assert deserialize_video({"id": "v1", "codec": "AVC", "width": 1, "height": 1, "note": {"x": 1}}).note is None
|
||||
|
||||
|
||||
def test_deserialize_video_unknown_codec_falls_back_to_none() -> None:
|
||||
v = deserialize_video({"id": "v2", "codec": "MADE_UP", "width": 0, "height": 0})
|
||||
assert v.codec is None
|
||||
@@ -105,36 +81,6 @@ def test_deserialize_audio_atmos_flag_without_a_count() -> None:
|
||||
assert a.bitrate == 256_000
|
||||
|
||||
|
||||
def test_deserialize_audio_atmos_keeps_a_real_joc_count() -> None:
|
||||
a = deserialize_audio({"id": "a3", "codec": "EC3", "atmos": True, "joc": 16, "channels": 6})
|
||||
assert a.atmos is True
|
||||
assert a.joc == 16
|
||||
|
||||
|
||||
def test_deserialize_audio_no_atmos() -> None:
|
||||
a = deserialize_audio({"id": "a2", "codec": "AAC", "channels": 2})
|
||||
assert a.atmos is False
|
||||
assert not a.joc
|
||||
|
||||
|
||||
def test_deserialize_subtitle_forced_flag() -> None:
|
||||
s = deserialize_subtitle({"id": "s1", "codec": "WebVTT", "language": "en", "forced": True})
|
||||
assert isinstance(s, Subtitle)
|
||||
assert s.forced is True
|
||||
assert s.sdh is False
|
||||
|
||||
|
||||
def test_deserialize_subtitle_sdh_flag() -> None:
|
||||
s = deserialize_subtitle({"id": "s2", "codec": "WebVTT", "language": "en", "sdh": True})
|
||||
assert s.sdh is True
|
||||
assert s.forced is False
|
||||
|
||||
|
||||
def test_reconstruct_drm_empty() -> None:
|
||||
assert reconstruct_drm(None) == []
|
||||
assert reconstruct_drm([]) == []
|
||||
|
||||
|
||||
def test_reconstruct_drm_skips_entries_without_pssh() -> None:
|
||||
assert reconstruct_drm([{"type": "widevine"}]) == []
|
||||
|
||||
@@ -143,19 +89,6 @@ def test_reconstruct_drm_invalid_pssh_silently_dropped() -> None:
|
||||
assert reconstruct_drm([{"type": "widevine", "pssh": "not-real-pssh"}]) == []
|
||||
|
||||
|
||||
def test_build_tracks_aggregates() -> None:
|
||||
data = {
|
||||
"video": [{"id": "v", "codec": "AVC", "width": 1280, "height": 720, "bitrate": 2500}],
|
||||
"audio": [{"id": "a", "codec": "AAC", "channels": 2, "bitrate": 128}],
|
||||
"subtitles": [{"id": "s", "codec": "WebVTT", "language": "en"}],
|
||||
"attachments": [],
|
||||
}
|
||||
t = build_tracks(data)
|
||||
assert len(t.videos) == 1
|
||||
assert len(t.audio) == 1
|
||||
assert len(t.subtitles) == 1
|
||||
|
||||
|
||||
def test_build_tracks_restores_drm_preference() -> None:
|
||||
data = {
|
||||
"video": [{"id": "v", "codec": "AVC", "width": 1280, "height": 720, "drm_preference": "playready"}],
|
||||
@@ -180,12 +113,6 @@ def test_match_track_by_id() -> None:
|
||||
assert match_track(remote, [a, b]) is b
|
||||
|
||||
|
||||
def test_match_track_by_attributes_when_id_missing() -> None:
|
||||
local = deserialize_video({"id": "X", "codec": "AVC", "width": 1920, "height": 1080, "language": "en"})
|
||||
remote = deserialize_video({"id": "Y", "codec": "AVC", "width": 1920, "height": 1080, "language": "en"})
|
||||
assert match_track(remote, [local]) is local
|
||||
|
||||
|
||||
def test_match_track_by_attributes_keeps_range_and_bitrate_apart() -> None:
|
||||
sdr = deserialize_video(
|
||||
{"id": "a", "codec": "HEVC", "width": 1920, "height": 960, "language": "en", "range": "SDR", "bitrate": 8000000}
|
||||
@@ -223,19 +150,6 @@ def test_match_track_by_attributes_matches_probed_hdr10p_to_local_hdr10() -> Non
|
||||
assert match_track(remote, [sdr, hdr10]) is hdr10
|
||||
|
||||
|
||||
def test_match_track_no_candidates_returns_none() -> None:
|
||||
remote = deserialize_video({"id": "X", "codec": "AVC", "width": 1, "height": 1})
|
||||
assert match_track(remote, []) is None
|
||||
|
||||
|
||||
def test_build_title_movie() -> None:
|
||||
info = {"type": "movie", "id": "movie-0001", "name": "Foo", "year": 2024, "language": "en"}
|
||||
title = build_title(info, "EXAMPLE", "fallback")
|
||||
assert isinstance(title, Movie)
|
||||
assert title.id == "movie-0001"
|
||||
assert title.name == "Foo"
|
||||
|
||||
|
||||
def test_build_title_episode() -> None:
|
||||
info = {
|
||||
"type": "episode",
|
||||
@@ -254,11 +168,6 @@ def test_build_title_episode() -> None:
|
||||
assert title.name == "Pilot"
|
||||
|
||||
|
||||
def test_build_title_falls_back_to_id_when_missing() -> None:
|
||||
title = build_title({"type": "movie", "name": "x"}, "EXAMPLE", "fallback-id")
|
||||
assert title.id == "fallback-id"
|
||||
|
||||
|
||||
def test_credential_cache_digests_cover_common_key_schemes() -> None:
|
||||
import hashlib
|
||||
|
||||
@@ -366,51 +275,6 @@ def test_load_cache_files_wiring(tmp_path, monkeypatch) -> None:
|
||||
assert set(sent) == {"tokens_default", "session_guid"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extra, warned", [(0, False), (1, True)])
|
||||
def test_load_cache_files_warns_above_the_server_limit(tmp_path, monkeypatch, caplog, extra, warned) -> None:
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
|
||||
from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_service import RemoteService
|
||||
|
||||
cache_dir = tmp_path / "TESTSVC"
|
||||
cache_dir.mkdir()
|
||||
count = MAX_SESSION_CACHE_KEYS + extra
|
||||
for i in range(count):
|
||||
(cache_dir / f"state{i}.json").write_text("{}")
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(config, "credentials", {})
|
||||
stub = SimpleNamespace(service_tag="TESTSVC", log=logging.getLogger("test"))
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="test"):
|
||||
sent = RemoteService.load_cache_files(stub, None)
|
||||
|
||||
assert len(sent) == count
|
||||
assert (f"has {count} files to send" in caplog.text) is warned
|
||||
|
||||
|
||||
@pytest.mark.parametrize("size, warned", [(10, False), (11, True)])
|
||||
def test_load_cache_files_warns_above_the_server_byte_limit(tmp_path, monkeypatch, caplog, size, warned) -> None:
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
|
||||
from unshackle.core.config import config
|
||||
|
||||
(tmp_path / "TESTSVC").mkdir()
|
||||
(tmp_path / "TESTSVC" / "state.json").write_text("a" * size)
|
||||
monkeypatch.setattr("unshackle.core.api.sanitize.MAX_SESSION_CACHE_BYTES", 10)
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(config, "credentials", {})
|
||||
stub = SimpleNamespace(service_tag="TESTSVC", log=logging.getLogger("test"))
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="test"):
|
||||
RemoteService.load_cache_files(stub, None)
|
||||
|
||||
assert (f"has {size} bytes to send" in caplog.text) is warned
|
||||
|
||||
|
||||
def test_load_cache_files_forwards_nested_keys(tmp_path, monkeypatch) -> None:
|
||||
"""A service that keeps its cache in subfolders (session_web/<sha1>.json) must have
|
||||
those files forwarded under the Cacher key, and the credential filter must still apply
|
||||
@@ -653,53 +517,6 @@ def test_write_client_cache_refuses_a_bad_map_before_it_writes_a_file(tmp_path,
|
||||
assert not any(tmp_path.iterdir())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stop_at", ["write", "store", "after_insert"])
|
||||
def test_a_session_create_that_fails_after_the_cache_write_leaves_no_cache_dir(tmp_path, monkeypatch, stop_at) -> None:
|
||||
import asyncio
|
||||
import logging
|
||||
import uuid
|
||||
from types import SimpleNamespace
|
||||
|
||||
from unshackle.core.api import handlers
|
||||
from unshackle.core.api.session_store import SessionStore
|
||||
|
||||
class Stop(Exception):
|
||||
pass
|
||||
|
||||
real_write = handlers.write_client_cache
|
||||
written: list[str] = []
|
||||
|
||||
def write_then_maybe_fail(cache_data, cache_tag):
|
||||
real_write(cache_data, cache_tag)
|
||||
written.extend(p.name for p in tmp_path.rglob("*.json"))
|
||||
if stop_at == "write":
|
||||
raise Stop()
|
||||
|
||||
monkeypatch.setattr(handlers.config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(handlers, "write_client_cache", write_then_maybe_fail)
|
||||
monkeypatch.setattr(handlers, "validate_service", lambda service, request=None: service)
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *args: (None, []))
|
||||
monkeypatch.setattr(handlers, "server_account_for", lambda *args: False)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: type("Svc", (), {}))
|
||||
stub_service_build(monkeypatch, lambda: SimpleNamespace(log=logging.getLogger("Svc")))
|
||||
monkeypatch.setattr("unshackle.core.api.session_store.refuse_while_draining", lambda: None)
|
||||
|
||||
class RefusingStore(SessionStore):
|
||||
async def create(self, *args, **kwargs):
|
||||
if stop_at == "after_insert":
|
||||
await super().create(*args, **kwargs)
|
||||
raise Stop()
|
||||
|
||||
store = RefusingStore()
|
||||
monkeypatch.setattr("unshackle.core.api.session_store.get_session_store", lambda: store)
|
||||
|
||||
data = {"service": "TESTSVC", "title_id": "t", "session_id": str(uuid.uuid4()), "cache": {"token": blob('"t"')}}
|
||||
response = asyncio.run(handlers.session_create_handler(data))
|
||||
assert response.status == 422
|
||||
assert written == ["token.json"]
|
||||
assert bool(list(tmp_path.rglob("*.json"))) is (stop_at == "after_insert")
|
||||
|
||||
|
||||
class _SentCreate(Exception):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user