"""API — FastAPI TestClient. 엔진은 monkeypatch(가짜)로 모델 로드 회피.""" from __future__ import annotations from types import SimpleNamespace import pytest from fastapi.testclient import TestClient import luke_scribe.api.routes.transcribe as route from luke_scribe.api.app import create_app from luke_scribe.config import settings class _FakeSeg: def __init__(self, start: float, end: float, text: str) -> None: self.start = start self.end = end self.text = text class _FakeEngine: def transcribe(self, _audio, **_kw): return [_FakeSeg(0.0, 1.0, "안녕 vLLM"), _FakeSeg(1.0, 2.0, "두번째")], SimpleNamespace( language="ko" ) @pytest.fixture def client(monkeypatch): monkeypatch.setattr(route, "get_engine", lambda *a, **k: _FakeEngine()) monkeypatch.setattr( route, "probe_media", lambda p: SimpleNamespace(path=p, duration_s=2.0, size_bytes=1234) ) monkeypatch.setattr(settings, "api_keys", ["testkey"]) return TestClient(create_app()) def _files(): return {"file": ("a.wav", b"RIFF0000WAVE", "audio/wav")} def test_health(client): assert client.get("/health").json() == {"status": "ok"} def test_requires_key(client): assert client.post("/v1/transcribe", files=_files()).status_code == 401 def test_transcribe_ok(client): r = client.post( "/v1/transcribe", files=_files(), headers={"X-API-Key": "testkey"}, data={"language": "ko"} ) assert r.status_code == 200 body = r.json() assert body["segments"][0]["text"] == "안녕 vLLM" assert body["model_used"] assert body["corrected"] is False def test_413(client, monkeypatch): monkeypatch.setattr( route, "probe_media", lambda p: SimpleNamespace(path=p, duration_s=999999, size_bytes=1) ) r = client.post("/v1/transcribe", files=_files(), headers={"X-API-Key": "testkey"}) assert r.status_code == 413 def test_srt_format(client): r = client.post( "/v1/transcribe", files=_files(), headers={"X-API-Key": "testkey"}, data={"response_format": "srt"}, ) assert r.status_code == 200 assert "00:00:00,000 --> 00:00:01,000" in r.text def test_correct_path(client, monkeypatch): monkeypatch.setattr(route.llm_correct, "correct", lambda text, **k: text + " [보정]") r = client.post( "/v1/transcribe", files=_files(), headers={"X-API-Key": "testkey"}, data={"correct": "true"} ) assert r.status_code == 200 assert r.json()["corrected"] is True