572 lines
20 KiB
Python
572 lines
20 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
import builtins
|
|
import socket
|
|
import struct
|
|
import sys
|
|
import threading
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from doctor_workstation.video.local_transcription import (
|
|
MAX_BASE64_CHARS,
|
|
MAX_PCM_BYTES,
|
|
MAX_SEQUENCE,
|
|
FasterWhisperEngine,
|
|
LocalTranscriptionController,
|
|
LocalTranscriptionError,
|
|
default_model_path,
|
|
local_transcription_readiness,
|
|
)
|
|
|
|
TIMESTAMP = 1_795_000_000_000
|
|
PCM = struct.pack("<hhhh", 0, 100, -100, 200)
|
|
AUDIO = base64.b64encode(PCM).decode("ascii")
|
|
|
|
|
|
class Events:
|
|
def __init__(self) -> None:
|
|
self.items: list[dict[str, Any]] = []
|
|
self.thread_ids: list[int] = []
|
|
self.condition = threading.Condition()
|
|
|
|
def __call__(self, event: dict[str, Any]) -> None:
|
|
with self.condition:
|
|
self.items.append(event)
|
|
self.thread_ids.append(threading.get_ident())
|
|
self.condition.notify_all()
|
|
|
|
def wait(self, kind: str, session: str = "call-1") -> dict[str, Any]:
|
|
with self.condition:
|
|
|
|
def match() -> dict[str, Any] | None:
|
|
return next(
|
|
(e for e in self.items if e["type"] == kind and e["sessionId"] == session),
|
|
None,
|
|
)
|
|
|
|
assert self.condition.wait_for(lambda: match() is not None, timeout=3)
|
|
event = match()
|
|
assert event is not None
|
|
return event
|
|
|
|
|
|
class Engine:
|
|
def __init__(self, text: str = "无过敏史,服用 0.5 mg,不是 5 mg。") -> None:
|
|
self.text = text
|
|
self.inputs: list[bytes] = []
|
|
|
|
def transcribe(self, pcm: bytes) -> str:
|
|
self.inputs.append(pcm)
|
|
return self.text
|
|
|
|
|
|
@pytest.fixture
|
|
def make_controller():
|
|
controllers = []
|
|
|
|
def make(events: Events, factory: Callable | None = None, **kwargs):
|
|
controller = LocalTranscriptionController(events, factory or Engine, **kwargs)
|
|
controllers.append(controller)
|
|
return controller
|
|
|
|
yield make
|
|
for controller in controllers:
|
|
controller.close()
|
|
if controller._worker:
|
|
controller._worker.join(3)
|
|
assert not controller._worker.is_alive()
|
|
|
|
|
|
def test_lazy_daemon_load_async_events_and_exact_numeric_negation_text(make_controller) -> None:
|
|
events = Events()
|
|
engine = Engine()
|
|
factory_threads = []
|
|
|
|
def factory():
|
|
factory_threads.append(threading.get_ident())
|
|
return engine
|
|
|
|
controller = make_controller(events, factory)
|
|
assert not factory_threads
|
|
assert controller.start("call-1")
|
|
assert events.wait("ready") == {"sessionId": "call-1", "type": "ready"}
|
|
assert controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
assert controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "completed"
|
|
assert events.wait("segment") == {
|
|
"sessionId": "call-1",
|
|
"type": "segment",
|
|
"segmentId": "call-1:patient:0",
|
|
"role": "patient",
|
|
"text": engine.text,
|
|
"timestamp": TIMESTAMP,
|
|
"isCompleted": True,
|
|
}
|
|
assert engine.inputs == [PCM]
|
|
assert controller._worker.daemon
|
|
assert factory_threads == [events.thread_ids[0]]
|
|
assert all(i != threading.get_ident() for i in events.thread_ids)
|
|
|
|
|
|
def test_controller_converts_before_caption_and_persistence_events(make_controller) -> None:
|
|
events = Events()
|
|
controller = make_controller(events, lambda: Engine("沒有吃藥,0.5 mg,不是 5 mg。"))
|
|
controller.start("call-1")
|
|
controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("segment")["text"] == "没有吃药,0.5 mg,不是 5 mg。"
|
|
assert events.wait("stopped")["status"] == "completed"
|
|
|
|
|
|
def test_conversion_failure_does_not_emit_unconverted_text(make_controller, monkeypatch) -> None:
|
|
def broken(text):
|
|
raise OSError("private dictionary path")
|
|
|
|
monkeypatch.setattr("doctor_workstation.video.local_transcription.simplified_transcript", broken)
|
|
events = Events()
|
|
controller = make_controller(events, lambda: Engine("沒有吃藥"))
|
|
controller.start("call-1")
|
|
controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("error")["code"] == "transcription_failed"
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert not any(e["type"] == "segment" for e in events.items)
|
|
|
|
|
|
def test_roles_have_independent_monotonic_sequences_and_drain_is_ordered(make_controller) -> None:
|
|
events = Events()
|
|
entered, release = threading.Event(), threading.Event()
|
|
engine = Engine()
|
|
|
|
def transcribe(pcm):
|
|
entered.set()
|
|
assert release.wait(3)
|
|
return engine.text
|
|
|
|
engine.transcribe = transcribe
|
|
controller = make_controller(events, lambda: engine)
|
|
assert controller.start("call-1")
|
|
events.wait("ready")
|
|
try:
|
|
assert controller.submit("call-1", "doctor", 0, TIMESTAMP, AUDIO)
|
|
assert entered.wait(3)
|
|
assert controller.submit("call-1", "patient", 0, TIMESTAMP + 100, AUDIO)
|
|
assert controller.submit("call-1", "doctor", 1, TIMESTAMP + 200, AUDIO)
|
|
assert controller.stop("call-1")
|
|
assert not controller.submit("call-1", "doctor", 2, TIMESTAMP + 300, AUDIO)
|
|
assert not any(e["type"] == "stopped" for e in events.items)
|
|
finally:
|
|
release.set()
|
|
assert events.wait("stopped")["status"] == "completed"
|
|
assert [e["type"] for e in events.items] == [
|
|
"ready",
|
|
"segment",
|
|
"segment",
|
|
"segment",
|
|
"stopped",
|
|
]
|
|
assert [e["segmentId"] for e in events.items if e["type"] == "segment"] == [
|
|
"call-1:doctor:0",
|
|
"call-1:patient:0",
|
|
"call-1:doctor:1",
|
|
]
|
|
assert controller.stop("call-1")
|
|
assert not controller.start("call-1")
|
|
|
|
|
|
@pytest.mark.parametrize("sequence", [0, -1, True, 1.5, MAX_SEQUENCE + 1, "2"])
|
|
def test_rejects_duplicate_invalid_or_unbounded_sequence(make_controller, sequence) -> None:
|
|
events = Events()
|
|
controller = make_controller(events)
|
|
assert controller.start("call-1")
|
|
assert controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
assert not controller.submit("call-1", "patient", sequence, TIMESTAMP, AUDIO)
|
|
assert controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert len([e for e in events.items if e["type"] == "segment"]) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("role", "timestamp", "audio"),
|
|
[
|
|
("unknown", TIMESTAMP, AUDIO),
|
|
("patient", 0, AUDIO),
|
|
("patient", True, AUDIO),
|
|
("patient", float("nan"), AUDIO),
|
|
("patient", 253_402_300_800_000, AUDIO),
|
|
("patient", TIMESTAMP, "!!!"),
|
|
("patient", TIMESTAMP, ""),
|
|
("patient", TIMESTAMP, "é"),
|
|
("patient", TIMESTAMP, "AA=="),
|
|
("patient", TIMESTAMP, "A" * (MAX_BASE64_CHARS + 1)),
|
|
],
|
|
ids=[
|
|
"role",
|
|
"zero-time",
|
|
"bool-time",
|
|
"nan-time",
|
|
"large-time",
|
|
"base64",
|
|
"empty",
|
|
"non-ascii",
|
|
"odd-pcm",
|
|
"oversized",
|
|
],
|
|
)
|
|
def test_input_validation_rejects_bad_metadata_and_audio(
|
|
make_controller, role, timestamp, audio
|
|
) -> None:
|
|
events = Events()
|
|
engine = Engine()
|
|
controller = make_controller(events, lambda: engine)
|
|
controller.start("call-1")
|
|
assert not controller.submit("call-1", role, 0, timestamp, audio)
|
|
controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert events.wait("error")["code"] in {"invalid_audio", "invalid_metadata"}
|
|
assert not engine.inputs
|
|
|
|
|
|
def test_accepts_twelve_seconds_rejects_extra_sample_and_reports_sequence_gap(
|
|
make_controller,
|
|
) -> None:
|
|
events = Events()
|
|
controller = make_controller(events)
|
|
controller.start("call-1")
|
|
assert controller.submit(
|
|
"call-1", "doctor", 0, TIMESTAMP, base64.b64encode(bytes(MAX_PCM_BYTES)).decode()
|
|
)
|
|
assert not controller.submit(
|
|
"call-1", "doctor", 1, TIMESTAMP, base64.b64encode(bytes(MAX_PCM_BYTES + 2)).decode()
|
|
)
|
|
assert controller.submit("call-1", "doctor", 2, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert {e["code"] for e in events.items if e["type"] == "error"} == {
|
|
"invalid_audio",
|
|
"sequence_gap",
|
|
}
|
|
|
|
|
|
def test_silence_and_vad_empty_result_produce_no_fabricated_segment(make_controller) -> None:
|
|
events = Events()
|
|
engine = Engine("")
|
|
controller = make_controller(events, lambda: engine)
|
|
controller.start("call-1")
|
|
assert controller.submit(
|
|
"call-1", "patient", 0, TIMESTAMP, base64.b64encode(bytes(3200)).decode()
|
|
)
|
|
assert controller.submit("call-1", "patient", 1, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "completed"
|
|
assert engine.inputs == [PCM]
|
|
assert not any(e["type"] == "segment" for e in events.items)
|
|
|
|
|
|
def test_queue_overload_is_bounded_coalesced_and_stop_still_drains(make_controller) -> None:
|
|
events = Events()
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
class SlowEngine(Engine):
|
|
def transcribe(self, pcm):
|
|
entered.set()
|
|
assert release.wait(3)
|
|
return super().transcribe(pcm)
|
|
|
|
engine = SlowEngine()
|
|
controller = make_controller(events, lambda: engine, max_pending_chunks=2)
|
|
controller.start("call-1")
|
|
events.wait("ready")
|
|
try:
|
|
assert controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
assert entered.wait(3)
|
|
assert controller.submit("call-1", "patient", 1, TIMESTAMP, AUDIO)
|
|
assert controller.submit("call-1", "patient", 2, TIMESTAMP, AUDIO)
|
|
for sequence in range(3, 100):
|
|
assert not controller.submit("call-1", "patient", sequence, TIMESTAMP, AUDIO)
|
|
assert controller.stop("call-1")
|
|
finally:
|
|
release.set()
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert len(engine.inputs) == 3
|
|
assert len([e for e in events.items if e.get("code") == "overloaded"]) == 1
|
|
assert events.items[-1]["type"] == "stopped"
|
|
|
|
|
|
@pytest.mark.parametrize("during_load", [False, True])
|
|
def test_new_session_cancels_old_inflight_result_and_queued_audio(
|
|
make_controller, during_load
|
|
) -> None:
|
|
events = Events()
|
|
entered, release = threading.Event(), threading.Event()
|
|
engine = Engine()
|
|
loads = []
|
|
|
|
def block():
|
|
entered.set()
|
|
assert release.wait(3)
|
|
|
|
def factory():
|
|
loads.append(1)
|
|
if during_load:
|
|
block()
|
|
return engine
|
|
|
|
if not during_load:
|
|
original = engine.transcribe
|
|
|
|
def transcribe(pcm):
|
|
block()
|
|
return original(pcm)
|
|
|
|
engine.transcribe = transcribe
|
|
controller = make_controller(events, factory)
|
|
controller.start("call-1")
|
|
try:
|
|
if not during_load:
|
|
events.wait("ready")
|
|
controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
assert entered.wait(3)
|
|
controller.submit("call-1", "patient", 1, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert controller.start("call-2")
|
|
cutoff = len(events.items)
|
|
assert not controller.submit("call-1", "patient", 2, TIMESTAMP, AUDIO)
|
|
assert not controller.stop("call-1")
|
|
finally:
|
|
release.set()
|
|
events.wait("ready", "call-2")
|
|
assert controller.submit("call-2", "doctor", 0, TIMESTAMP, AUDIO)
|
|
controller.stop("call-2")
|
|
events.wait("stopped", "call-2")
|
|
assert all(e["sessionId"] == "call-2" for e in events.items[cutoff:])
|
|
assert len(loads) == 1
|
|
assert len(engine.inputs) == (1 if during_load else 2)
|
|
|
|
|
|
def test_close_suppresses_callbacks_without_waiting_for_native_inference(make_controller) -> None:
|
|
events = Events()
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
class SlowEngine(Engine):
|
|
def transcribe(self, pcm):
|
|
entered.set()
|
|
assert release.wait(3)
|
|
return "不"
|
|
|
|
controller = make_controller(events, SlowEngine)
|
|
controller.start("call-1")
|
|
events.wait("ready")
|
|
try:
|
|
controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
assert entered.wait(3)
|
|
controller.close()
|
|
count = len(events.items)
|
|
assert not controller.start("call-2")
|
|
assert not controller.stop("call-1")
|
|
finally:
|
|
release.set()
|
|
controller._worker.join(3)
|
|
assert not controller._worker.is_alive()
|
|
assert len(events.items) == count
|
|
|
|
|
|
def test_missing_model_failure_is_explicit_and_stops_partial(make_controller) -> None:
|
|
events = Events()
|
|
|
|
def missing_model():
|
|
raise LocalTranscriptionError("model_missing")
|
|
|
|
controller = make_controller(events, missing_model)
|
|
controller.start("call-1")
|
|
assert events.wait("error")["code"] == "model_missing"
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert not any(e["type"] == "ready" for e in events.items)
|
|
assert not controller.submit("call-1", "doctor", 0, TIMESTAMP, AUDIO)
|
|
|
|
|
|
def test_inference_failure_does_not_leak_data_or_abort_later_chunks(make_controller) -> None:
|
|
events = Events()
|
|
|
|
class FailingEngine(Engine):
|
|
def transcribe(self, pcm):
|
|
if not self.inputs:
|
|
self.inputs.append(pcm)
|
|
raise RuntimeError("SECRET_PATIENT_DETAILS")
|
|
return "不"
|
|
|
|
controller = make_controller(events, FailingEngine)
|
|
controller.start("call-1")
|
|
controller.submit("call-1", "patient", 0, TIMESTAMP, AUDIO)
|
|
controller.submit("call-1", "patient", 1, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "partial"
|
|
assert events.wait("error")["code"] == "transcription_failed"
|
|
assert events.wait("segment")["text"] == "不"
|
|
assert "SECRET_PATIENT_DETAILS" not in repr(events.items)
|
|
|
|
|
|
def test_callback_failure_does_not_kill_worker(make_controller) -> None:
|
|
events = Events()
|
|
|
|
def callback(event):
|
|
if event["type"] == "ready":
|
|
raise RuntimeError("detached UI")
|
|
events(event)
|
|
|
|
controller = make_controller(callback)
|
|
controller.start("call-1")
|
|
controller.submit("call-1", "doctor", 0, TIMESTAMP, AUDIO)
|
|
controller.stop("call-1")
|
|
assert events.wait("stopped")["status"] == "completed"
|
|
|
|
|
|
@pytest.mark.parametrize("session", ["", " ", "a" * 129, "x\n", "患者", None, 123])
|
|
def test_invalid_session_cannot_create_worker(make_controller, session) -> None:
|
|
events = Events()
|
|
controller = make_controller(events)
|
|
assert not controller.start(session)
|
|
assert controller._worker is None
|
|
assert not events.items
|
|
|
|
|
|
def write_model_files(path: Path) -> None:
|
|
for name in ("model.bin", "config.json", "tokenizer.json", "vocabulary.txt"):
|
|
(path / name).write_text("test", encoding="utf-8")
|
|
|
|
|
|
def test_missing_tokenizer_never_imports_model_or_accesses_network(tmp_path, monkeypatch) -> None:
|
|
write_model_files(tmp_path)
|
|
(tmp_path / "tokenizer.json").unlink()
|
|
original_import = builtins.__import__
|
|
|
|
def guarded_import(name, *args, **kwargs):
|
|
assert name != "faster_whisper"
|
|
return original_import(name, *args, **kwargs)
|
|
|
|
def no_network(*args, **kwargs):
|
|
pytest.fail("Runtime ASR must not access the network")
|
|
|
|
monkeypatch.setattr(builtins, "__import__", guarded_import)
|
|
monkeypatch.setattr(socket.socket, "connect", no_network)
|
|
with pytest.raises(LocalTranscriptionError) as raised:
|
|
FasterWhisperEngine(tmp_path)
|
|
assert raised.value.code == "model_missing"
|
|
|
|
|
|
def test_missing_dependency_is_explicit_without_exposing_exception(tmp_path, monkeypatch) -> None:
|
|
write_model_files(tmp_path)
|
|
original_import = builtins.__import__
|
|
|
|
def guarded_import(name, *args, **kwargs):
|
|
if name == "numpy":
|
|
raise ModuleNotFoundError("internal path / private details")
|
|
return original_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(builtins, "__import__", guarded_import)
|
|
with pytest.raises(LocalTranscriptionError) as raised:
|
|
FasterWhisperEngine(tmp_path)
|
|
assert raised.value.code == "dependency_missing"
|
|
assert "private" not in str(raised.value)
|
|
|
|
|
|
def test_missing_conversion_dictionary_fails_before_model_ready(tmp_path, monkeypatch) -> None:
|
|
write_model_files(tmp_path)
|
|
|
|
def broken_dictionary(text):
|
|
raise FileNotFoundError("private path to missing t2s.json")
|
|
|
|
def model_must_not_load(*args, **kwargs):
|
|
pytest.fail("Missing conversion resources must fail before expensive model loading")
|
|
|
|
monkeypatch.setitem(sys.modules, "numpy", SimpleNamespace())
|
|
monkeypatch.setitem(sys.modules, "faster_whisper", SimpleNamespace(WhisperModel=model_must_not_load))
|
|
monkeypatch.setitem(sys.modules, "faster_whisper.vad", SimpleNamespace(get_vad_model=lambda: None))
|
|
monkeypatch.setattr(
|
|
"doctor_workstation.video.local_transcription.simplified_transcript", broken_dictionary
|
|
)
|
|
with pytest.raises(LocalTranscriptionError) as raised:
|
|
FasterWhisperEngine(tmp_path)
|
|
assert raised.value.code == "conversion_unavailable"
|
|
assert "private" not in str(raised.value)
|
|
|
|
|
|
def test_engine_uses_local_cpu_int8_vad_and_iterates_without_rewriting(
|
|
tmp_path, monkeypatch
|
|
) -> None:
|
|
write_model_files(tmp_path)
|
|
calls = {}
|
|
|
|
class Array:
|
|
def astype(self, dtype):
|
|
calls["float_dtype"] = dtype
|
|
return self
|
|
|
|
def __truediv__(self, divisor):
|
|
calls["divisor"] = divisor
|
|
return self
|
|
|
|
def frombuffer(pcm, dtype):
|
|
calls["pcm"] = pcm
|
|
calls["pcm_dtype"] = dtype
|
|
return Array()
|
|
|
|
class Model:
|
|
def __init__(self, path, **kwargs):
|
|
calls["path"] = path
|
|
calls["load"] = kwargs
|
|
|
|
def transcribe(self, audio, **kwargs):
|
|
calls["transcribe"] = kwargs
|
|
|
|
def segments():
|
|
calls["iterated"] = True
|
|
yield SimpleNamespace(text=" 没有。")
|
|
yield SimpleNamespace(text=" 0.5 mg,不是 5 mg。 ")
|
|
|
|
return segments(), None
|
|
|
|
def vad():
|
|
calls["vad_loaded"] = True
|
|
|
|
def no_network(*args, **kwargs):
|
|
pytest.fail("Runtime ASR must not access the network")
|
|
|
|
monkeypatch.setattr(socket.socket, "connect", no_network)
|
|
monkeypatch.setitem(
|
|
sys.modules, "numpy", SimpleNamespace(frombuffer=frombuffer, float32="float32")
|
|
)
|
|
monkeypatch.setitem(sys.modules, "faster_whisper", SimpleNamespace(WhisperModel=Model))
|
|
monkeypatch.setitem(sys.modules, "faster_whisper.vad", SimpleNamespace(get_vad_model=vad))
|
|
engine = FasterWhisperEngine(tmp_path)
|
|
assert engine.transcribe(PCM) == "没有。 0.5 mg,不是 5 mg。"
|
|
assert calls["path"] == str(tmp_path.resolve())
|
|
assert calls["load"]["local_files_only"] is True
|
|
assert calls["load"]["device"] == "cpu"
|
|
assert calls["load"]["compute_type"] == "int8"
|
|
assert calls["load"]["num_workers"] == 1
|
|
assert calls["vad_loaded"] and calls["iterated"]
|
|
assert calls["pcm"] == PCM and calls["pcm_dtype"] == "<i2"
|
|
assert calls["float_dtype"] == "float32" and calls["divisor"] == 32768.0
|
|
assert calls["transcribe"]["vad_filter"] is True
|
|
assert calls["transcribe"]["vad_parameters"]["min_speech_duration_ms"] == 0
|
|
assert calls["transcribe"]["condition_on_previous_text"] is False
|
|
assert calls["transcribe"]["task"] == "transcribe"
|
|
assert engine.transcribe(bytes(3200)) == ""
|
|
|
|
|
|
def test_preflight_reports_model_path_override_without_downloading(tmp_path, monkeypatch) -> None:
|
|
monkeypatch.setenv("DOCTOR_ASR_MODEL_PATH", str(tmp_path))
|
|
assert default_model_path() == tmp_path.resolve()
|
|
info = local_transcription_readiness()
|
|
assert not info["available"]
|
|
assert info["modelPath"] == str(tmp_path.resolve())
|
|
assert "tokenizer.json" in info["missingFiles"]
|
|
assert not list(tmp_path.iterdir())
|