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