1) Uses textuals CSS_PATH and an external tcss file to bundle all DEFAULT_CSS into one file
2) Added keyboard shortcut bindings for switching the language (Ctrl-T) and showing the help (Ctrl-A) The help should use a markdown viewer 3) Include a test folder containing all the tests
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"""Shared pytest fixtures for the yt-dlp download manager tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
FIXTURES_DIR = Path(__file__).resolve().parent / "fixtures"
|
||||
|
||||
# Make both the app package and the bundled i18n library importable, exactly
|
||||
# as main.py does for the real application.
|
||||
sys.path.insert(0, str(PROJECT_ROOT / "lib"))
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from i18n import I18n # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def i18n_singleton() -> I18n:
|
||||
"""Configure the process-wide I18n singleton once for the whole run.
|
||||
|
||||
Mirrors what main.py does before starting the real application.
|
||||
"""
|
||||
return I18n(
|
||||
default_language="en",
|
||||
available_languages={"en": "English", "de": "Deutsch"},
|
||||
project_root=PROJECT_ROOT,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def split_format_json() -> dict:
|
||||
"""The "separate audio/video parts" example from the requirements doc."""
|
||||
with (FIXTURES_DIR / "example_split.json").open(encoding="utf-8") as fh:
|
||||
return json.load(fh)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def combined_format_json() -> dict:
|
||||
"""The "audio+video in one part" example from the requirements doc."""
|
||||
with (FIXTURES_DIR / "example_combined.json").open(encoding="utf-8") as fh:
|
||||
return json.load(fh)
|
||||
Vendored
+64
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"formats": [
|
||||
{
|
||||
"format_id": "http-main-6",
|
||||
"language": "deu",
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"vcodec": "H.264",
|
||||
"protocol": "https",
|
||||
"ext": "mp4",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1280x720",
|
||||
"dynamic_range": "SDR",
|
||||
"format": "http-main-6 - 1280x720 (HD Ready)"
|
||||
},
|
||||
{
|
||||
"format_id": "hls-main-3309-1",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"vcodec": "avc1.640020",
|
||||
"acodec": "mp4a.40.2",
|
||||
"dynamic_range": "SDR",
|
||||
"language": "deu",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1280x720",
|
||||
"format": "hls-main-3309-1 - 1280x720"
|
||||
},
|
||||
{
|
||||
"format_id": "http-main-7",
|
||||
"language": "deu",
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
"vcodec": "H.264",
|
||||
"protocol": "https",
|
||||
"ext": "mp4",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1920x1080",
|
||||
"dynamic_range": "SDR",
|
||||
"format": "http-main-7 - 1920x1080 (Full HD)"
|
||||
},
|
||||
{
|
||||
"format_id": "hls-main-5147-1",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
"vcodec": "avc1.64002a",
|
||||
"acodec": "mp4a.40.2",
|
||||
"dynamic_range": "SDR",
|
||||
"language": "deu",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1920x1080",
|
||||
"format": "hls-main-5147-1 - 1920x1080"
|
||||
}
|
||||
],
|
||||
"fulltitle": "Unterwegs nach Cold Mountain",
|
||||
"_type": "video"
|
||||
}
|
||||
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
{
|
||||
"formats": [
|
||||
{
|
||||
"format_id": "VA-audio_0-Deutsch__Audiodeskription_",
|
||||
"format_note": "Deutsch [DE]",
|
||||
"language": "de",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"vcodec": "none",
|
||||
"audio_ext": "mp4",
|
||||
"video_ext": "none",
|
||||
"resolution": "audio only",
|
||||
"format": "VA-audio_0-Deutsch__Audiodeskription_ - audio only (Deutsch [DE])"
|
||||
},
|
||||
{
|
||||
"format_id": "VA-1293",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"vcodec": "avc1.64001F",
|
||||
"acodec": "none",
|
||||
"dynamic_range": "SDR",
|
||||
"format_note": "Deutsch [DE]",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1280x720",
|
||||
"format": "VA-1293 - 1280x720 (Deutsch [DE])"
|
||||
},
|
||||
{
|
||||
"format_id": "VA-1069",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"vcodec": "hev1.1.6.L93.B0",
|
||||
"acodec": "none",
|
||||
"dynamic_range": "SDR",
|
||||
"format_note": "Deutsch [DE]",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1280x720",
|
||||
"format": "VA-1069 - 1280x720 (Deutsch [DE])"
|
||||
},
|
||||
{
|
||||
"format_id": "VA-2198",
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
"vcodec": "avc1.640028",
|
||||
"acodec": "none",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"abr": 0,
|
||||
"vbr": 2198.272,
|
||||
"resolution": "1920x1080",
|
||||
"aspect_ratio": 1.78,
|
||||
"format": "VA-2198 - 1920x1080 (Deutsch [DE])"
|
||||
},
|
||||
{
|
||||
"format_id": "VA-1978",
|
||||
"format_index": null,
|
||||
"ext": "mp4",
|
||||
"protocol": "m3u8_native",
|
||||
"width": 1920,
|
||||
"height": 1080,
|
||||
"vcodec": "hev1.1.6.L120.B0",
|
||||
"acodec": "none",
|
||||
"dynamic_range": "SDR",
|
||||
"format_note": "Deutsch [DE]",
|
||||
"video_ext": "mp4",
|
||||
"audio_ext": "none",
|
||||
"resolution": "1920x1080",
|
||||
"format": "VA-1978 - 1920x1080 (Deutsch [DE])"
|
||||
}
|
||||
],
|
||||
"fulltitle": "Der Pate",
|
||||
"_type": "video"
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
"""End-to-end tests driving the actual Textual UI via Pilot.
|
||||
|
||||
yt-dlp / ffmpeg subprocess calls are monkeypatched so these tests need
|
||||
neither network access nor the real binaries installed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from textual.widgets import Button, Label, MarkdownViewer, Static
|
||||
|
||||
from app import ffmpeg_client, ytdlp_client
|
||||
from app.tui_app import DownloadManagerApp
|
||||
|
||||
|
||||
async def _wait_until(pilot, predicate, attempts: int = 50) -> None:
|
||||
for _ in range(attempts):
|
||||
await pilot.pause()
|
||||
if predicate():
|
||||
return
|
||||
raise AssertionError("condition not met in time")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_split_selection_and_download_merges_and_cleans_up(
|
||||
monkeypatch: pytest.MonkeyPatch, split_format_json: dict, tmp_path: Path
|
||||
) -> None:
|
||||
async def fake_fetch_info(url: str) -> dict:
|
||||
return split_format_json
|
||||
|
||||
async def fake_download(url, destination, format_id, on_progress=None) -> None:
|
||||
if on_progress is not None:
|
||||
await on_progress(0)
|
||||
await on_progress(100)
|
||||
Path(destination).write_text("fake stream data")
|
||||
|
||||
merge_calls: list[tuple[str, str, str]] = []
|
||||
|
||||
async def fake_merge(video_path: str, audio_path: str, destination: str) -> None:
|
||||
merge_calls.append((video_path, audio_path, destination))
|
||||
Path(destination).write_text("merged")
|
||||
|
||||
monkeypatch.setattr(ytdlp_client, "fetch_info", fake_fetch_info)
|
||||
monkeypatch.setattr(ytdlp_client, "download", fake_download)
|
||||
monkeypatch.setattr(ffmpeg_client, "merge", fake_merge)
|
||||
|
||||
destination = tmp_path / "der_pate.mp4"
|
||||
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
app.query_one("#url-input").value = "https://example.com/der-pate"
|
||||
app.query_one("#name-input").value = str(destination)
|
||||
|
||||
await pilot.click("#select-parts-btn")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
|
||||
# First modal: audio selection (split example has one audio format).
|
||||
await pilot.click("#select-ok")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
|
||||
# Second modal: video selection.
|
||||
await pilot.click("#select-ok")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
audio_label = app.query_one("#audio-label", Static)
|
||||
video_label = app.query_one("#video-label", Static)
|
||||
assert "VA-audio 0-Deutsch Audiodeskription" in str(audio_label.content)
|
||||
assert "VA-1293" in str(video_label.content)
|
||||
|
||||
download_btn = app.query_one("#start-download-btn", Button)
|
||||
assert not download_btn.disabled
|
||||
|
||||
await pilot.click("#start-download-btn")
|
||||
await _wait_until(pilot, lambda: destination.exists())
|
||||
|
||||
# Both temp streams were merged in the required order (video, then
|
||||
# audio) and the temp files were cleaned up afterwards.
|
||||
assert len(merge_calls) == 1
|
||||
video_path, audio_path, dest_path = merge_calls[0]
|
||||
assert "video.tmp" in video_path
|
||||
assert "audio.tmp" in audio_path
|
||||
assert dest_path == str(destination)
|
||||
assert not any(tmp_path.glob("*.tmp.*"))
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_url_shows_error_dialog() -> None:
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
await pilot.click("#select-parts-btn")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
|
||||
error_message = app.screen.query_one("#error-message", Static)
|
||||
assert "URL" in str(error_message.content)
|
||||
|
||||
await pilot.click("#ok")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_combined_mode_shows_single_dialog_and_cancel_disables_download(
|
||||
monkeypatch: pytest.MonkeyPatch, combined_format_json: dict, tmp_path: Path
|
||||
) -> None:
|
||||
async def fake_fetch_info(url: str) -> dict:
|
||||
return combined_format_json
|
||||
|
||||
monkeypatch.setattr(ytdlp_client, "fetch_info", fake_fetch_info)
|
||||
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
app.query_one("#url-input").value = "https://example.com/cold-mountain"
|
||||
app.query_one("#name-input").value = str(tmp_path / "combined.mp4")
|
||||
|
||||
await pilot.click("#select-parts-btn")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
|
||||
# Cancel the (only) selection dialog -> should raise an error and
|
||||
# leave the download button disabled.
|
||||
await pilot.click("#select-cancel")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2) # error dialog
|
||||
await pilot.click("#ok")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
download_btn = app.query_one("#start-download-btn", Button)
|
||||
assert download_btn.disabled
|
||||
|
||||
# Redo it properly this time.
|
||||
await pilot.click("#select-parts-btn")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
await pilot.click("#select-ok")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
assert not download_btn.disabled
|
||||
audio_label = app.query_one("#audio-label", Static)
|
||||
assert str(audio_label.content).endswith("-") # no audio-only part
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ctrl_t_switches_language_live() -> None:
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
select_btn = app.query_one("#select-parts-btn", Button)
|
||||
assert str(select_btn.label) == "Select parts"
|
||||
|
||||
await pilot.press("ctrl+t")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
await pilot.click("#lang-de")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
assert str(select_btn.label) == "Teile auswählen"
|
||||
assert app.title == "yt-dlp Download-Manager"
|
||||
|
||||
# Switch back so later tests in the same session see English again.
|
||||
await pilot.press("ctrl+t")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
await pilot.click("#lang-en")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
assert str(select_btn.label) == "Select parts"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ctrl_a_shows_markdown_help() -> None:
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
await pilot.press("ctrl+a")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
|
||||
viewer = app.screen.query_one(MarkdownViewer)
|
||||
assert viewer is not None
|
||||
title = app.screen.query_one("#help-title", Label)
|
||||
assert str(title.content) == "Help"
|
||||
|
||||
await pilot.click("#help-close")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ctrl_a_works_even_while_url_input_is_focused() -> None:
|
||||
"""Input's own ctrl+a ("select all") binding must not shadow the app's."""
|
||||
app = DownloadManagerApp()
|
||||
async with app.run_test() as pilot:
|
||||
app.query_one("#url-input").focus()
|
||||
await pilot.pause()
|
||||
|
||||
await pilot.press("ctrl+a")
|
||||
await _wait_until(pilot, lambda: len(app.screen_stack) == 2)
|
||||
assert app.screen.query(MarkdownViewer)
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Tests for the i18n integration: singleton, language switching, fallback."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from i18n import I18n
|
||||
|
||||
|
||||
def test_singleton_returns_same_instance(i18n_singleton: I18n) -> None:
|
||||
assert I18n() is i18n_singleton
|
||||
|
||||
|
||||
def test_english_translation_present(i18n_singleton: I18n) -> None:
|
||||
assert i18n_singleton.t("button.select_parts") == "Select parts"
|
||||
|
||||
|
||||
def test_unknown_key_falls_back_to_key_itself(i18n_singleton: I18n) -> None:
|
||||
assert i18n_singleton.t("no.such.key") == "no.such.key"
|
||||
|
||||
|
||||
def test_unknown_key_uses_supplied_default(i18n_singleton: I18n) -> None:
|
||||
assert i18n_singleton.t("no.such.key", default="fallback text") == "fallback text"
|
||||
|
||||
|
||||
def test_status_progress_formatting(i18n_singleton: I18n) -> None:
|
||||
text = i18n_singleton.t(
|
||||
"status.progress", title="Der Pate", kind="Video", percent=42.0
|
||||
)
|
||||
assert text == "Der Pate - Video - 42%"
|
||||
|
||||
|
||||
def test_language_switch_changes_translations(i18n_singleton: I18n) -> None:
|
||||
assert i18n_singleton.get_language() == "en"
|
||||
try:
|
||||
i18n_singleton.set_language("de")
|
||||
assert i18n_singleton.t("button.select_parts") == "Teile auswählen"
|
||||
assert i18n_singleton.t("app.title") == "yt-dlp Download-Manager"
|
||||
finally:
|
||||
i18n_singleton.set_language("en")
|
||||
|
||||
assert i18n_singleton.t("button.select_parts") == "Select parts"
|
||||
|
||||
|
||||
def test_language_change_listener_is_notified(i18n_singleton: I18n) -> None:
|
||||
received: list[str] = []
|
||||
|
||||
def listener(code: str) -> None:
|
||||
received.append(code)
|
||||
|
||||
i18n_singleton.add_language_change_listener(listener)
|
||||
try:
|
||||
i18n_singleton.set_language("de")
|
||||
i18n_singleton.set_language("en")
|
||||
finally:
|
||||
i18n_singleton.remove_language_change_listener(listener)
|
||||
|
||||
assert received == ["de", "en"]
|
||||
|
||||
|
||||
def test_get_help_returns_language_specific_text(i18n_singleton: I18n) -> None:
|
||||
try:
|
||||
i18n_singleton.set_language("de")
|
||||
help_text = i18n_singleton.get_help()
|
||||
assert help_text.startswith("# yt-dlp Download-Manager - Hilfe")
|
||||
finally:
|
||||
i18n_singleton.set_language("en")
|
||||
|
||||
help_text_en = i18n_singleton.get_help()
|
||||
assert help_text_en.startswith("# yt-dlp Download Manager - Help")
|
||||
|
||||
|
||||
def test_available_languages_include_en_and_de(i18n_singleton: I18n) -> None:
|
||||
languages = i18n_singleton.get_available_languages()
|
||||
assert languages == {"en": "English", "de": "Deutsch"}
|
||||
@@ -0,0 +1,121 @@
|
||||
"""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"
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Tests for the async subprocess wrappers, using fake subprocesses.
|
||||
|
||||
These avoid depending on a real yt-dlp/ffmpeg installation or network
|
||||
access by monkeypatching asyncio.create_subprocess_exec with a small stand-in
|
||||
that plays back canned stdout and a fixed return code.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from app import ffmpeg_client, ytdlp_client
|
||||
|
||||
|
||||
class _FakeStdout:
|
||||
def __init__(self, lines: list[bytes]) -> None:
|
||||
self._lines = list(lines)
|
||||
|
||||
async def readline(self) -> bytes:
|
||||
if not self._lines:
|
||||
return b""
|
||||
return self._lines.pop(0)
|
||||
|
||||
|
||||
class _FakeProcess:
|
||||
def __init__(self, stdout_lines: list[bytes], return_code: int = 0) -> None:
|
||||
self.stdout = _FakeStdout(stdout_lines)
|
||||
self._return_code = return_code
|
||||
self._communicate_payload = b"".join(stdout_lines)
|
||||
|
||||
async def wait(self) -> int:
|
||||
return self._return_code
|
||||
|
||||
async def communicate(self) -> tuple[bytes, bytes]:
|
||||
return self._communicate_payload, b""
|
||||
|
||||
|
||||
# -- fetch_info -----------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_info_parses_json_document(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
document = {"fulltitle": "Test Film", "formats": []}
|
||||
payload = (json.dumps(document) + "\n").encode("utf-8")
|
||||
|
||||
async def fake_exec(*args, **kwargs):
|
||||
return _FakeProcess([payload])
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
result = await ytdlp_client.fetch_info("https://example.com/film")
|
||||
assert result == document
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_info_raises_on_no_json(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
async def fake_exec(*args, **kwargs):
|
||||
return _FakeProcess([b"ERROR: unsupported URL\n"])
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
with pytest.raises(ytdlp_client.YtDlpError):
|
||||
await ytdlp_client.fetch_info("https://example.com/bad-url")
|
||||
|
||||
|
||||
# -- download ---------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_download_reports_progress_on_whole_percent_changes(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
lines = [
|
||||
b"[download] 0.0% of 10.00MiB\n",
|
||||
b"[download] 0.4% of 10.00MiB\n", # same whole percent (0) -> no callback
|
||||
b"[download] 50.0% of 10.00MiB\n",
|
||||
b"[download] 50.6% of 10.00MiB\n", # same whole percent (50) -> no callback
|
||||
b"[download] 100.0% of 10.00MiB\n",
|
||||
]
|
||||
|
||||
async def fake_exec(*args, **kwargs):
|
||||
return _FakeProcess(lines)
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
|
||||
seen: list[float] = []
|
||||
|
||||
async def on_progress(percent: float) -> None:
|
||||
seen.append(percent)
|
||||
|
||||
await ytdlp_client.download("https://example.com/film", "out.mp4", "137", on_progress)
|
||||
assert seen == [0.0, 50.0, 100.0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_download_raises_on_nonzero_exit(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
async def fake_exec(*args, **kwargs):
|
||||
return _FakeProcess([b"ERROR: network failure\n"], return_code=1)
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
with pytest.raises(ytdlp_client.YtDlpError):
|
||||
await ytdlp_client.download("https://example.com/film", "out.mp4", "137")
|
||||
|
||||
|
||||
# -- ffmpeg merge -------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_succeeds_on_zero_exit(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
captured_args: list[str] = []
|
||||
|
||||
async def fake_exec(*args, **kwargs):
|
||||
captured_args.extend(args)
|
||||
return _FakeProcess([b""], return_code=0)
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
await ffmpeg_client.merge("video.mp4", "audio.mp4", "dest.mp4")
|
||||
|
||||
# Requirements specify: ffmpeg -i video.mp4 -i audio.mp4 -c copy dest
|
||||
assert "-i" in captured_args
|
||||
video_index = captured_args.index("video.mp4")
|
||||
audio_index = captured_args.index("audio.mp4")
|
||||
assert video_index < audio_index
|
||||
assert captured_args[-1] == "dest.mp4"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_raises_on_nonzero_exit(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
async def fake_exec(*args, **kwargs):
|
||||
return _FakeProcess([b"ffmpeg error output\n"], return_code=1)
|
||||
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec)
|
||||
with pytest.raises(ffmpeg_client.FfmpegError):
|
||||
await ffmpeg_client.merge("video.mp4", "audio.mp4", "dest.mp4")
|
||||
Reference in New Issue
Block a user