"""Unit tests for app.models: protocol filtering and split/combined detection. Exercised against the two worked examples from the requirements document ("requirements for a textual download manager"): one film with separate audio/video parts ("Der Pate") and one with combined audio+video parts ("Unterwegs nach Cold Mountain"). """ from __future__ import annotations from app.models import AUDIO_ONLY_RESOLUTION, VALID_PROTOCOL, FormatEntry, VideoInfo def test_split_example_detected_as_split(split_format_json: dict) -> None: info = VideoInfo.from_json(split_format_json) assert info.is_split is True def test_split_example_fulltitle(split_format_json: dict) -> None: info = VideoInfo.from_json(split_format_json) assert info.fulltitle == "Der Pate" def test_split_example_audio_formats(split_format_json: dict) -> None: info = VideoInfo.from_json(split_format_json) assert [f.format_id for f in info.audio_formats] == [ "VA-audio_0-Deutsch__Audiodeskription_" ] assert info.audio_formats[0].resolution == AUDIO_ONLY_RESOLUTION assert info.audio_formats[0].vcodec == "none" def test_split_example_video_formats(split_format_json: dict) -> None: info = VideoInfo.from_json(split_format_json) assert [f.format_id for f in info.video_formats] == [ "VA-1293", "VA-1069", "VA-2198", "VA-1978", ] # All surviving formats used the only valid protocol. assert all(f.protocol == VALID_PROTOCOL for f in info.video_formats) # None of them are audio-only. assert all(f.resolution != AUDIO_ONLY_RESOLUTION for f in info.video_formats) def test_combined_example_detected_as_combined(combined_format_json: dict) -> None: info = VideoInfo.from_json(combined_format_json) assert info.is_split is False assert info.audio_formats == [] def test_combined_example_fulltitle(combined_format_json: dict) -> None: info = VideoInfo.from_json(combined_format_json) assert info.fulltitle == "Unterwegs nach Cold Mountain" def test_combined_example_https_formats_are_skipped(combined_format_json: dict) -> None: """http-main-6 / http-main-7 use protocol "https" and must be dropped.""" info = VideoInfo.from_json(combined_format_json) ids = [f.format_id for f in info.video_formats] assert "http-main-6" not in ids assert "http-main-7" not in ids def test_combined_example_m3u8_formats_survive(combined_format_json: dict) -> None: info = VideoInfo.from_json(combined_format_json) ids = [f.format_id for f in info.video_formats] assert ids == ["hls-main-3309-1", "hls-main-5147-1"] assert all(f.protocol == VALID_PROTOCOL for f in info.video_formats) def test_no_formats_survive_filtering_yields_empty_lists() -> None: raw = { "fulltitle": "Nothing downloadable", "formats": [ {"format_id": "a", "protocol": "https", "resolution": "1280x720"}, {"format_id": "b", "protocol": "http_dash_segments", "resolution": "1920x1080"}, ], } info = VideoInfo.from_json(raw) assert info.audio_formats == [] assert info.video_formats == [] assert info.is_split is False def test_missing_formats_key_yields_empty_lists() -> None: info = VideoInfo.from_json({"fulltitle": "No formats key at all"}) assert info.audio_formats == [] assert info.video_formats == [] class TestFormatEntry: def test_display_text_replaces_underscores_with_blanks(self) -> None: entry = FormatEntry.from_json( { "format_id": "VA-audio_0-Deutsch__Audiodeskription_", "resolution": "audio only", "acodec": None, "vcodec": "none", "format": "VA-audio_0-Deutsch__Audiodeskription_ - audio only (Deutsch [DE])", "protocol": "m3u8_native", } ) assert "_" not in entry.display_text assert entry.display_text == ( "VA-audio 0-Deutsch Audiodeskription - audio only (Deutsch [DE])" ) def test_is_audio_only_property(self) -> None: audio_entry = FormatEntry.from_json({"resolution": "audio only", "format_id": "a"}) video_entry = FormatEntry.from_json({"resolution": "1920x1080", "format_id": "b"}) assert audio_entry.is_audio_only is True assert video_entry.is_audio_only is False def test_missing_or_none_codec_normalised_to_none_string(self) -> None: entry = FormatEntry.from_json( {"format_id": "x", "resolution": "1280x720", "acodec": None, "vcodec": "none"} ) assert entry.acodec == "none" assert entry.vcodec == "none"