feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
115
apps/desktop/sidecar/tests/test_diarize_endpoint.py
Normal file
115
apps/desktop/sidecar/tests/test_diarize_endpoint.py
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
"""
|
||||
사이드카 /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()
|
||||
87
apps/desktop/sidecar/tests/test_transcribe_hygiene.py
Normal file
87
apps/desktop/sidecar/tests/test_transcribe_hygiene.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
"""Sidecar /transcribe & /shutdown hygiene regression tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import threading
|
||||
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 _FakeModel:
|
||||
pass
|
||||
|
||||
|
||||
class TranscribeAndShutdownHygieneTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._orig_model = main._model
|
||||
self._orig_model_id = main._model_id
|
||||
self._orig_thread = main._download_thread
|
||||
main._model = _FakeModel() # type: ignore[assignment]
|
||||
main._model_id = "tiny"
|
||||
self.client = TestClient(main.app)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
main._model = self._orig_model
|
||||
main._model_id = self._orig_model_id
|
||||
main._download_thread = self._orig_thread
|
||||
main._download_cancel.clear()
|
||||
|
||||
def test_transcribe_single_byte_pcm_returns_400_without_frombuffer_crash(self) -> None:
|
||||
res = self.client.post(
|
||||
"/transcribe",
|
||||
files={"audio": ("chunk.pcm", b"\x01", "application/octet-stream")},
|
||||
)
|
||||
self.assertEqual(res.status_code, 400)
|
||||
self.assertEqual(res.json()["status"], "error")
|
||||
|
||||
def test_transcribe_odd_byte_pcm_truncates_trailing_byte_and_succeeds(self) -> None:
|
||||
observed_samples: list[int] = []
|
||||
|
||||
def fake_transcribe(_model, audio_array, _kwargs, _resolved_id, _is_primary, _cancel):
|
||||
observed_samples.append(len(audio_array))
|
||||
return [{"text": "ok", "start": 0.0, "end": 0.1}], "ok", "ko"
|
||||
|
||||
with mock.patch.object(main, "_transcribe_blocking", side_effect=fake_transcribe):
|
||||
res = self.client.post(
|
||||
"/transcribe",
|
||||
files={"audio": ("chunk.pcm", b"\x00\x01\x02\x03\x99", "application/octet-stream")},
|
||||
)
|
||||
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(observed_samples, [2])
|
||||
self.assertEqual(res.json()["text"], "ok")
|
||||
|
||||
def test_shutdown_cancels_active_download_thread(self) -> None:
|
||||
stopped = threading.Event()
|
||||
|
||||
def fake_worker() -> None:
|
||||
main._download_cancel.wait(timeout=5.0)
|
||||
stopped.set()
|
||||
|
||||
main._download_cancel.clear()
|
||||
worker = threading.Thread(target=fake_worker, daemon=True)
|
||||
main._download_thread = worker
|
||||
worker.start()
|
||||
|
||||
with mock.patch.object(main, "_server", None):
|
||||
res = self.client.post("/shutdown")
|
||||
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.json()["status"], "shutting_down")
|
||||
self.assertTrue(main._download_cancel.is_set())
|
||||
self.assertTrue(stopped.is_set())
|
||||
self.assertFalse(worker.is_alive())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue