112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
import os
|
|
from argparse import Namespace
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
import httpx
|
|
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"
|
|
|
|
|
|
def test_format_http_error_with_body() -> None:
|
|
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
|
response = httpx.Response(500, text="Internal Server Error", request=request)
|
|
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
|
|
assert message == "HTTP 500 from http://localhost:8000/transcriptions: Internal Server Error"
|
|
|
|
|
|
def test_format_http_error_with_empty_body() -> None:
|
|
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
|
response = httpx.Response(500, text="", request=request)
|
|
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
|
|
assert message == "HTTP 500 from http://localhost:8000/transcriptions: <empty response body>"
|
|
|
|
|
|
def test_format_request_error_timeout() -> None:
|
|
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
|
exc = httpx.ReadTimeout("read timed out", request=request)
|
|
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
|
assert message == "Request to http://localhost:8000/transcriptions timed out."
|
|
|
|
|
|
def test_format_request_error_network_failure() -> None:
|
|
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
|
exc = httpx.ConnectError("connection refused", request=request)
|
|
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
|
assert (
|
|
message
|
|
== "Request to http://localhost:8000/transcriptions failed: connection refused"
|
|
)
|
|
|
|
|
|
@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"
|