115 lines
3.9 KiB
Python
115 lines
3.9 KiB
Python
"""
|
|
사이드카 /diarize 엔드포인트 계약 테스트 (GAP-MTG-02).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import types
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
SIDECAR_DIR = Path(__file__).resolve().parent.parent
|
|
if str(SIDECAR_DIR) not in sys.path:
|
|
sys.path.insert(0, str(SIDECAR_DIR))
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import main # noqa: E402
|
|
|
|
|
|
class _FakeTurn:
|
|
def __init__(self, start: float, end: float) -> None:
|
|
self.start = start
|
|
self.end = end
|
|
|
|
|
|
class _FakeAnnotation:
|
|
def itertracks(self, yield_label: bool = False):
|
|
tracks = [
|
|
(_FakeTurn(0.2, 4.5), "A", "SPEAKER_00"),
|
|
(_FakeTurn(5.0, 9.8), "B", "SPEAKER_01"),
|
|
(_FakeTurn(10.1, 13.0), "C", "SPEAKER_00"),
|
|
]
|
|
if yield_label:
|
|
return tracks
|
|
return [(t[0], t[1]) for t in tracks]
|
|
|
|
|
|
class DiarizeEndpointTest(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.client = TestClient(main.app)
|
|
|
|
def test_diarize_rejects_empty_hf_token(self) -> None:
|
|
response = self.client.post(
|
|
"/diarize",
|
|
files={"audio": ("audio.wav", b"RIFF0000WAVEfmt ", "audio/wav")},
|
|
data={"hf_token": " "},
|
|
)
|
|
self.assertEqual(response.status_code, 400)
|
|
body = response.json()
|
|
self.assertEqual(body["code"], "missing_hf_token")
|
|
|
|
def test_diarize_returns_503_when_pyannote_not_installed(self) -> None:
|
|
with mock.patch.dict(sys.modules, {"pyannote": None, "pyannote.audio": None}):
|
|
response = self.client.post(
|
|
"/diarize",
|
|
files={"audio": ("audio.wav", b"RIFF0000WAVEfmt ", "audio/wav")},
|
|
data={"hf_token": "hf_test_token"},
|
|
)
|
|
self.assertEqual(response.status_code, 503)
|
|
body = response.json()
|
|
self.assertEqual(body["code"], "pyannote_not_installed")
|
|
|
|
def test_diarize_returns_segments_and_cleans_temp_file(self) -> None:
|
|
observed_paths: list[Path] = []
|
|
observed_kwargs: list[dict[str, object]] = []
|
|
|
|
class _FakePipeline:
|
|
@classmethod
|
|
def from_pretrained(cls, model_id: str, **kwargs: object) -> "_FakePipeline":
|
|
self_inst = cls()
|
|
self_inst.model_id = model_id
|
|
self_inst.init_kwargs = kwargs
|
|
return self_inst
|
|
|
|
def __call__(self, audio_path: str, **kwargs: object) -> _FakeAnnotation:
|
|
observed_paths.append(Path(audio_path))
|
|
observed_kwargs.append(dict(kwargs))
|
|
self.temp_existed_during_call = Path(audio_path).exists()
|
|
return _FakeAnnotation()
|
|
|
|
fake_pyannote = types.ModuleType("pyannote")
|
|
fake_pyannote_audio = types.ModuleType("pyannote.audio")
|
|
fake_pyannote_audio.Pipeline = _FakePipeline # type: ignore[attr-defined]
|
|
|
|
with mock.patch.dict(
|
|
sys.modules,
|
|
{"pyannote": fake_pyannote, "pyannote.audio": fake_pyannote_audio},
|
|
):
|
|
response = self.client.post(
|
|
"/diarize",
|
|
files={"audio": ("audio.wav", b"RIFF1234WAVEfmt ", "audio/wav")},
|
|
data={"hf_token": "hf_valid_token", "num_speakers": "2"},
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
body = response.json()
|
|
self.assertEqual(body["status"], "ok")
|
|
self.assertEqual(body["num_speakers"], 2)
|
|
self.assertEqual(
|
|
body["segments"],
|
|
[
|
|
{"speaker": "SPEAKER_00", "start": 0.2, "end": 4.5},
|
|
{"speaker": "SPEAKER_01", "start": 5.0, "end": 9.8},
|
|
{"speaker": "SPEAKER_00", "start": 10.1, "end": 13.0},
|
|
],
|
|
)
|
|
self.assertEqual(observed_kwargs, [{"num_speakers": 2}])
|
|
self.assertEqual(len(observed_paths), 1)
|
|
self.assertFalse(observed_paths[0].exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|