import os from argparse import Namespace from pathlib import Path import sys from dataclasses import dataclass import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) import main def test_resolve_server_from_env(monkeypatch) -> None: monkeypatch.setenv("WHISPER_REMOTE", "http://localhost:8000/") assert main.resolve_server(Namespace(server=None)) == "http://localhost:8000" def test_resolve_server_requires_value(monkeypatch) -> None: monkeypatch.delenv("WHISPER_REMOTE", raising=False) with pytest.raises(SystemExit): main.resolve_server(Namespace(server=None)) def test_infer_output_path_for_directory(tmp_path: Path) -> None: destination = main.infer_output_path(tmp_path, Path("clip.wav"), "srt") assert destination == tmp_path / "clip.srt" 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"