From 7830ee9355892b7e31babb2d3ef3e967138b76e1 Mon Sep 17 00:00:00 2001 From: space Date: Thu, 4 Jun 2026 16:58:21 +0200 Subject: [PATCH] Add cleanup and artifact test coverage --- backend/tests/test_server.py | 35 ++++++++++++++++++++++++++++++- cli/tests/test_main.py | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/backend/tests/test_server.py b/backend/tests/test_server.py index 59baf5d..b9e41b4 100644 --- a/backend/tests/test_server.py +++ b/backend/tests/test_server.py @@ -1,4 +1,5 @@ from pathlib import Path +import shutil import sys sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) @@ -11,6 +12,20 @@ import server client = TestClient(server.app) +class FakeTemporaryDirectory: + def __init__(self, root: Path, prefix: str, created_paths: list[Path]) -> None: + self.path = root / f"{prefix}{len(created_paths)}" + self.created_paths = created_paths + + def __enter__(self) -> str: + self.path.mkdir(parents=True, exist_ok=False) + self.created_paths.append(self.path) + return str(self.path) + + def __exit__(self, exc_type, exc, tb) -> None: + shutil.rmtree(self.path, ignore_errors=True) + + def test_validate_output_format_rejects_unknown() -> None: try: server.validate_output_format("docx") @@ -21,6 +36,13 @@ def test_validate_output_format_rejects_unknown() -> None: def test_transcriptions_returns_generated_artifact(monkeypatch, tmp_path: Path) -> None: + created_paths: list[Path] = [] + monkeypatch.setattr( + server, + "TemporaryDirectory", + lambda prefix="": FakeTemporaryDirectory(tmp_path, prefix, created_paths), + ) + def fake_run(command: list[str], check: bool, capture_output: bool, text: bool): output_dir = Path(command[command.index("--output_dir") + 1]) (output_dir / "clip.txt").write_text("hello world", encoding="utf-8") @@ -43,9 +65,18 @@ def test_transcriptions_returns_generated_artifact(monkeypatch, tmp_path: Path) assert response.status_code == 200 assert response.text == "hello world" assert response.headers["x-whisper-output-format"] == "txt" + assert created_paths + assert all(not path.exists() for path in created_paths) -def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None: +def test_transcriptions_maps_subprocess_failure(monkeypatch, tmp_path: Path) -> None: + created_paths: list[Path] = [] + monkeypatch.setattr( + server, + "TemporaryDirectory", + lambda prefix="": FakeTemporaryDirectory(tmp_path, prefix, created_paths), + ) + def fake_run(command: list[str], check: bool, capture_output: bool, text: bool): class Result: returncode = 1 @@ -64,3 +95,5 @@ def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None: assert response.status_code == 502 assert response.json()["detail"] == "bad whisper day" + assert created_paths + assert all(not path.exists() for path in created_paths) diff --git a/cli/tests/test_main.py b/cli/tests/test_main.py index a77e9ab..8c51cc1 100644 --- a/cli/tests/test_main.py +++ b/cli/tests/test_main.py @@ -2,6 +2,7 @@ import os from argparse import Namespace from pathlib import Path import sys +from dataclasses import dataclass import pytest @@ -29,3 +30,42 @@ def test_infer_output_path_for_directory(tmp_path: Path) -> None: def test_infer_output_path_for_explicit_file(tmp_path: Path) -> None: destination = main.infer_output_path(tmp_path / "custom-name.txt", Path("clip.wav"), "txt") assert destination == tmp_path / "custom-name.txt" + + +@dataclass +class FakeResponse: + content: bytes + text: str + + def raise_for_status(self) -> None: + return None + + +class FakeClient: + def __init__(self, response: FakeResponse) -> None: + self.response = response + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, tb) -> None: + return None + + def post(self, endpoint: str, data: dict[str, str], files: dict[str, tuple[str, object, str]]): + return self.response + + +def test_main_writes_transcript_to_file(monkeypatch, tmp_path: Path, capsys) -> None: + input_file = tmp_path / "clip.wav" + input_file.write_bytes(b"audio") + destination = tmp_path / "saved" / "clip.txt" + + monkeypatch.setenv("WHISPER_REMOTE", "http://localhost:8000") + monkeypatch.setattr(main.httpx, "Client", lambda timeout: FakeClient(FakeResponse(b"hello world", "hello world"))) + monkeypatch.setattr(sys, "argv", ["whisper-remote", str(input_file), "--model", "base", "--to-file", str(destination)]) + + exit_code = main.main() + + assert exit_code == 0 + assert destination.read_bytes() == b"hello world" + assert capsys.readouterr().out == f"{destination}\n"