64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from whisper_remote_backend 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"
|