86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
from pathlib import Path
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
import server
|
|
|
|
|
|
client = TestClient(server.app)
|
|
|
|
|
|
def test_validate_output_format_rejects_unknown() -> None:
|
|
try:
|
|
server.validate_output_format("docx")
|
|
except Exception as exc: # pragma: no cover - structure assertion below
|
|
assert getattr(exc, "status_code", None) == 400
|
|
else: # pragma: no cover
|
|
raise AssertionError("Expected HTTPException")
|
|
|
|
|
|
def test_transcriptions_returns_generated_artifact(monkeypatch, tmp_path: Path) -> None:
|
|
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")
|
|
|
|
class Result:
|
|
returncode = 0
|
|
stdout = ""
|
|
stderr = ""
|
|
|
|
return Result()
|
|
|
|
monkeypatch.setattr(server.subprocess, "run", fake_run)
|
|
|
|
response = client.post(
|
|
"/transcriptions",
|
|
data={"model": "base", "language": "en", "output_format": "txt"},
|
|
files={"file": ("clip.wav", b"audio", "audio/wav")},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.text == "hello world"
|
|
assert response.headers["x-whisper-output-format"] == "txt"
|
|
|
|
|
|
def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None:
|
|
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool):
|
|
class Result:
|
|
returncode = 1
|
|
stdout = ""
|
|
stderr = "bad whisper day"
|
|
|
|
return Result()
|
|
|
|
monkeypatch.setattr(server.subprocess, "run", fake_run)
|
|
|
|
response = client.post(
|
|
"/transcriptions",
|
|
data={"model": "base", "output_format": "txt"},
|
|
files={"file": ("clip.wav", b"audio", "audio/wav")},
|
|
)
|
|
|
|
assert response.status_code == 502
|
|
assert response.json()["detail"] == "bad whisper day"
|
|
|
|
|
|
def test_transcriptions_maps_subprocess_timeout(monkeypatch) -> None:
|
|
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool, timeout: int):
|
|
raise server.subprocess.TimeoutExpired(cmd=command, timeout=timeout)
|
|
|
|
monkeypatch.setattr(server.subprocess, "run", fake_run)
|
|
|
|
response = client.post(
|
|
"/transcriptions",
|
|
data={"model": "base", "output_format": "txt"},
|
|
files={"file": ("clip.wav", b"audio", "audio/wav")},
|
|
)
|
|
|
|
assert response.status_code == 504
|
|
assert (
|
|
response.json()["detail"]
|
|
== f"Whisper CLI timed out after {server.WHISPER_PROCESS_TIMEOUT_SECONDS}s and was terminated."
|
|
)
|