"""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)