更新
This commit is contained in:
@@ -0,0 +1,362 @@
|
||||
"""Cloud ASR reaches UI and live AI only after trusted, normalized persistence."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from concurrent.futures import Future
|
||||
from types import MethodType, SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.video import window
|
||||
from doctor_workstation.video.security import TrustedDocumentPolicy
|
||||
|
||||
pytestmark = pytest.mark.skipif(not window.WEBENGINE_AVAILABLE, reason="optional QtWebEngine")
|
||||
TRUSTED = "https://video.example.test/call/index.html"
|
||||
SESSION = "tr-cloud-test"
|
||||
SOURCE = "沒有過敏,沒有吃藥。0.5 mg,不是 5 mg。血壓 120/80 mmHg。\n唔係,冇食藥。"
|
||||
SIMPLIFIED = "没有过敏,没有吃药。0.5 mg,不是 5 mg。血压 120/80 mmHg。\n唔系,冇食药。"
|
||||
TIMESTAMP = 1793332800000
|
||||
|
||||
|
||||
def host(**changes):
|
||||
item = SimpleNamespace(
|
||||
_closing=False, _released=False, _shutdown_requested=False, _injected=True,
|
||||
_start_requested=True, _call_cycle_closed=False,
|
||||
_policy=TrustedDocumentPolicy.from_url(TRUSTED, is_local=False),
|
||||
_page=Mock(), _local_transcription=Mock(), _live_ai=Mock(),
|
||||
_asr_session_id=SESSION, _asr_ready=False, _asr_partial=False,
|
||||
_transcription_final_status="completed", _transcription_completed=Mock(),
|
||||
request=SimpleNamespace(user_id="doctor_test", target_user_id="patient_test"),
|
||||
lifecycle=SimpleNamespace(
|
||||
call_record_id=71, transcription_session_id=SESSION,
|
||||
start_transcription=Mock(return_value=Future()),
|
||||
save_transcript_segment=Mock(return_value=Future()),
|
||||
finish_transcription=Mock(return_value=Future()),
|
||||
),
|
||||
_start_local_audio_recording=Mock(), _append_local_audio_chunk=Mock(),
|
||||
_finish_local_audio_recording=Mock(), _abort_local_audio_recording=Mock(),
|
||||
)
|
||||
item._page.url.return_value.toString.return_value = TRUSTED
|
||||
for name in (
|
||||
"_handle_bridge_message", "_handle_video_assistance_message",
|
||||
"_local_transcription_context_is_trusted", "_start_transcription",
|
||||
"_save_transcript_segment", "_finish_transcription",
|
||||
"_notify_transcription_completed", "_on_transcription_completed",
|
||||
):
|
||||
setattr(item, name, MethodType(getattr(window._EmbeddedVideoWindow, name), item))
|
||||
item._transcription_completed.emit.side_effect = item._on_transcription_completed
|
||||
for name, value in changes.items():
|
||||
setattr(item, name, value)
|
||||
return item
|
||||
|
||||
|
||||
def segment_message(**changes):
|
||||
return {
|
||||
"source": "doctor-call", "event": "transcription-segment", "sessionId": SESSION,
|
||||
"segment": {
|
||||
"segment_id": "cloud-segment-1", "speaker_user_id": "patient_test",
|
||||
"speaker_role": "doctor", "timestamp": TIMESTAMP, "text": SOURCE,
|
||||
},
|
||||
**changes,
|
||||
}
|
||||
|
||||
|
||||
def transcription_result(item):
|
||||
script = item._page.runJavaScript.call_args.args[0]
|
||||
assert script.startswith("window.doctorConsultation?.transcriptionResult?.(")
|
||||
assert script.endswith(");")
|
||||
return json.loads("[" + script.split("?.(", 1)[1][:-2] + "]")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("speaker_id", "expected_role"),
|
||||
[("doctor_test", "doctor"), ("patient_test", "patient"), ("other_test", "unknown")],
|
||||
)
|
||||
def test_trusted_cloud_segment_is_normalized_and_role_is_host_derived(speaker_id, expected_role):
|
||||
item = host()
|
||||
message = segment_message()
|
||||
message["segment"]["speaker_user_id"] = speaker_id
|
||||
item._handle_bridge_message(message)
|
||||
|
||||
item.lifecycle.save_transcript_segment.assert_called_once()
|
||||
saved = item.lifecycle.save_transcript_segment.call_args.args[0]
|
||||
assert saved["text"] == SIMPLIFIED
|
||||
assert saved["speaker_role"] == expected_role
|
||||
assert saved["speaker_user_id"] == speaker_id
|
||||
assert saved["segment_id"] == "cloud-segment-1"
|
||||
assert saved["timestamp"] == TIMESTAMP
|
||||
assert message["segment"]["text"] == SOURCE
|
||||
item._live_ai.changed.assert_not_called()
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
item.lifecycle.save_transcript_segment.return_value.set_result(True)
|
||||
|
||||
result = transcription_result(item)
|
||||
assert result == [
|
||||
"segment", SESSION, "cloud-segment-1", True, "",
|
||||
{"segmentId": "cloud-segment-1", "speakerUserId": speaker_id,
|
||||
"sourceText": SIMPLIFIED, "timestamp": TIMESTAMP, "isCompleted": True},
|
||||
]
|
||||
assert len(item._transcription_completed.emit.call_args.args) == 6
|
||||
item._live_ai.changed.assert_called_once_with(SESSION)
|
||||
item._local_transcription.start.assert_not_called()
|
||||
item._local_transcription.submit.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("failure", [False, RuntimeError("storage unavailable")])
|
||||
def test_failed_storage_never_returns_normalized_text_or_dirties_ai(failure):
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message())
|
||||
future = item.lifecycle.save_transcript_segment.return_value
|
||||
if isinstance(failure, Exception):
|
||||
future.set_exception(failure)
|
||||
else:
|
||||
future.set_result(failure)
|
||||
|
||||
result = transcription_result(item)
|
||||
assert result[:4] == ["segment", SESSION, "cloud-segment-1", False]
|
||||
assert len(result) == 6 and result[5] is None
|
||||
assert item._asr_partial
|
||||
item._live_ai.changed.assert_not_called()
|
||||
item._live_ai.transcription_error.assert_called_once()
|
||||
|
||||
|
||||
def test_synchronous_storage_failure_has_no_normalized_ack():
|
||||
item = host()
|
||||
item.lifecycle.save_transcript_segment.side_effect = RuntimeError("storage unavailable")
|
||||
item._handle_bridge_message(segment_message())
|
||||
result = transcription_result(item)
|
||||
assert result[:4] == ["segment", SESSION, "cloud-segment-1", False]
|
||||
assert result[5] is None
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
def test_camel_case_cloud_segment_uses_the_same_normalized_contract():
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message(segment={
|
||||
"segmentId": "cloud-segment-2", "speakerUserId": " doctor_test ",
|
||||
"speakerRole": "patient", "sourceText": SOURCE, "timestamp": TIMESTAMP,
|
||||
}))
|
||||
saved = item.lifecycle.save_transcript_segment.call_args.args[0]
|
||||
assert saved["speaker_role"] == "doctor"
|
||||
assert saved["speaker_user_id"] == "doctor_test"
|
||||
assert saved["text"] == SIMPLIFIED
|
||||
item.lifecycle.save_transcript_segment.return_value.set_result(True)
|
||||
assert transcription_result(item)[5] == {
|
||||
"segmentId": "cloud-segment-2", "speakerUserId": "doctor_test",
|
||||
"sourceText": SIMPLIFIED, "timestamp": TIMESTAMP, "isCompleted": True,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("segment", [
|
||||
None, {}, {"segment_id": "s1", "text": " "},
|
||||
{"segment_id": "s1", "text": "字" * 4001},
|
||||
])
|
||||
def test_malformed_segment_is_rejected_before_storage(segment):
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message(segment=segment))
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
result = transcription_result(item)
|
||||
assert result[0] == "segment" and result[3] is False and result[5] is None
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
def test_conversion_failure_does_not_store_or_ack_unnormalized_text(monkeypatch):
|
||||
def unavailable_dictionary(_text):
|
||||
raise RuntimeError("conversion dictionary unavailable")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"doctor_workstation.video.lifecycle.simplified_transcript", unavailable_dictionary,
|
||||
)
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message())
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
result = transcription_result(item)
|
||||
assert result[:4] == ["segment", SESSION, "cloud-segment-1", False]
|
||||
assert result[5] is None
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
def test_json_ack_preserves_text_without_inserting_executable_source():
|
||||
item = host()
|
||||
message = segment_message()
|
||||
message["segment"]["text"] = '沒有;0.5 mg,不是 5 mg。</script>"\\\u2028\u2029結尾'
|
||||
item._handle_bridge_message(message)
|
||||
item.lifecycle.save_transcript_segment.return_value.set_result(True)
|
||||
result = transcription_result(item)
|
||||
assert result[5]["sourceText"] == '没有;0.5 mg,不是 5 mg。</script>"\\\u2028\u2029结尾'
|
||||
script = item._page.runJavaScript.call_args.args[0]
|
||||
assert "\u2028" not in script and "\u2029" not in script
|
||||
|
||||
|
||||
@pytest.mark.parametrize("session_id", ["tr-old-call", ""])
|
||||
def test_segment_for_another_or_empty_session_is_not_saved(session_id):
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message(sessionId=session_id))
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
def test_late_persistence_completion_does_not_dirty_new_call():
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message())
|
||||
original_future = item.lifecycle.save_transcript_segment.return_value
|
||||
item.lifecycle = SimpleNamespace(call_record_id=72, transcription_session_id="tr-new-call")
|
||||
item._asr_session_id = "tr-new-call"
|
||||
original_future.set_result(True)
|
||||
item._live_ai.changed.assert_not_called()
|
||||
item._live_ai.bind.assert_not_called()
|
||||
item._live_ai.finish.assert_not_called()
|
||||
|
||||
|
||||
def test_navigation_before_persistence_ack_blocks_page_delivery_and_ai_change():
|
||||
item = host()
|
||||
item._handle_bridge_message(segment_message())
|
||||
item._page.url.return_value.toString.return_value = "https://another.example.test/"
|
||||
item.lifecycle.save_transcript_segment.return_value.set_result(True)
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("succeeded", [True, False])
|
||||
def test_start_marks_starting_and_binds_only_after_successful_storage_ack(succeeded):
|
||||
item = host()
|
||||
item._handle_bridge_message({
|
||||
"event": "transcription-start-request", "sessionId": SESSION, "language": "zh-CN",
|
||||
})
|
||||
item._live_ai.transcription_starting.assert_called_once()
|
||||
item.lifecycle.start_transcription.assert_called_once_with(SESSION, language="zh-CN")
|
||||
item._live_ai.bind.assert_not_called()
|
||||
item._local_transcription.start.assert_not_called()
|
||||
|
||||
item.lifecycle.start_transcription.return_value.set_result(succeeded)
|
||||
if succeeded:
|
||||
item._live_ai.bind.assert_called_once_with(71, SESSION)
|
||||
else:
|
||||
item._live_ai.bind.assert_not_called()
|
||||
assert transcription_result(item)[:4] == ["start", SESSION, "", succeeded]
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
def test_cloud_error_marks_failed_and_stop_keeps_partial_storage_status():
|
||||
item = host()
|
||||
item._handle_bridge_message({
|
||||
"event": "transcription-error", "sessionId": SESSION, "message": "云转写连接中断",
|
||||
})
|
||||
item._live_ai.transcription_error.assert_called_once_with("云转写连接中断", status="failed")
|
||||
assert item._asr_partial
|
||||
|
||||
item._handle_bridge_message({
|
||||
"event": "transcription-stop", "sessionId": SESSION, "status": "completed",
|
||||
})
|
||||
item.lifecycle.finish_transcription.assert_called_once_with(status="partial")
|
||||
item._live_ai.finish.assert_not_called()
|
||||
item.lifecycle.finish_transcription.return_value.set_result(True)
|
||||
item._live_ai.finish.assert_called_once_with(SESSION, "partial")
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("session,shutdown,ready", [(SESSION, False, True), ("old", False, False), (SESSION, True, False), ("", False, False)])
|
||||
def test_cloud_ready_after_retry_is_scoped_to_active_call(session, shutdown, ready):
|
||||
item = host(_shutdown_requested=shutdown)
|
||||
item._handle_bridge_message({"event": "transcription-ready", "sessionId": session})
|
||||
assert item._live_ai.transcription_running.called is ready
|
||||
if ready:
|
||||
item._live_ai.transcription_running.assert_called_once_with(SESSION)
|
||||
|
||||
|
||||
def test_error_or_stop_from_old_session_does_not_change_current_call():
|
||||
item = host()
|
||||
item._handle_bridge_message({
|
||||
"event": "transcription-error", "sessionId": "tr-old-call", "message": "old failure",
|
||||
})
|
||||
item._handle_bridge_message({
|
||||
"event": "transcription-stop", "sessionId": "tr-old-call", "status": "partial",
|
||||
})
|
||||
assert not item._asr_partial
|
||||
item.lifecycle.finish_transcription.assert_not_called()
|
||||
item._live_ai.transcription_error.assert_not_called()
|
||||
item._live_ai.finish.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("event", [
|
||||
"transcription-start-request", "transcription-segment", "transcription-stop", "transcription-error", "transcription-ready",
|
||||
])
|
||||
@pytest.mark.parametrize("untrusted", ["closing", "released", "uninjected", "navigated"])
|
||||
def test_cloud_commands_require_injected_trusted_live_document(event, untrusted):
|
||||
item = host()
|
||||
if untrusted == "navigated":
|
||||
item._page.url.return_value.toString.return_value = "https://another.example.test/"
|
||||
else:
|
||||
setattr(item, {"closing": "_closing", "released": "_released", "uninjected": "_injected"}[untrusted],
|
||||
untrusted != "uninjected")
|
||||
message = segment_message(event=event, message="cloud failure", language="zh-CN", status="completed")
|
||||
item._handle_bridge_message(message)
|
||||
item.lifecycle.start_transcription.assert_not_called()
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
item.lifecycle.finish_transcription.assert_not_called()
|
||||
item._live_ai.transcription_starting.assert_not_called()
|
||||
item._live_ai.transcription_error.assert_not_called()
|
||||
item._live_ai.transcription_running.assert_not_called()
|
||||
item._live_ai.changed.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("operation", ["start", "segment", "stop"])
|
||||
def test_direct_transcription_methods_also_require_a_trusted_document(operation):
|
||||
item = host(_injected=False)
|
||||
if operation == "start":
|
||||
item._start_transcription(SESSION, "zh-CN")
|
||||
elif operation == "segment":
|
||||
item._save_transcript_segment(segment_message())
|
||||
else:
|
||||
item._finish_transcription(SESSION, "completed")
|
||||
item.lifecycle.start_transcription.assert_not_called()
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
item.lifecycle.finish_transcription.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("event", [
|
||||
"local-transcription-start", "local-transcription-chunk", "local-transcription-stop",
|
||||
])
|
||||
def test_old_local_asr_dispatch_cannot_run_recognition(event):
|
||||
item = host()
|
||||
item._handle_bridge_message({
|
||||
"event": event, "sessionId": SESSION, "role": "patient", "sequence": 0,
|
||||
"timestamp": TIMESTAMP, "data": "AQACAA==",
|
||||
})
|
||||
item._local_transcription.start.assert_not_called()
|
||||
item._local_transcription.submit.assert_not_called()
|
||||
item._local_transcription.stop.assert_not_called()
|
||||
item.lifecycle.start_transcription.assert_not_called()
|
||||
item.lifecycle.save_transcript_segment.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("event", "handler", "arguments"),
|
||||
[
|
||||
("local-audio-start", "_start_local_audio_recording", (SESSION, "audio/webm")),
|
||||
("local-audio-chunk", "_append_local_audio_chunk", (SESSION, 0, "AQACAA==")),
|
||||
("local-audio-finish", "_finish_local_audio_recording", (SESSION, 4)),
|
||||
("local-audio-abort", "_abort_local_audio_recording", (SESSION,)),
|
||||
],
|
||||
)
|
||||
def test_local_audio_recording_dispatch_remains_available(event, handler, arguments):
|
||||
item = host()
|
||||
item._handle_bridge_message({
|
||||
"event": event, "sessionId": SESSION, "mimeType": "audio/webm",
|
||||
"sequence": 0, "data": "AQACAA==", "totalBytes": 4,
|
||||
})
|
||||
getattr(item, handler).assert_called_once_with(*arguments)
|
||||
|
||||
|
||||
def test_pending_shutdown_drains_cloud_segments_and_stop_without_new_start():
|
||||
item = host(_shutdown_requested=True)
|
||||
item._handle_bridge_message({"event": "transcription-start-request", "sessionId": "tr-next"})
|
||||
item._handle_bridge_message(segment_message())
|
||||
item._handle_bridge_message({"event": "transcription-stop", "sessionId": SESSION})
|
||||
item.lifecycle.start_transcription.assert_not_called()
|
||||
item.lifecycle.save_transcript_segment.assert_called_once()
|
||||
item.lifecycle.finish_transcription.assert_called_once_with(status="completed")
|
||||
item._local_transcription.start.assert_not_called()
|
||||
@@ -0,0 +1,512 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.core.errors import ApiBusinessError, ApiHttpError, ApiTimeoutError
|
||||
from doctor_workstation.video.live_ai import LiveAiController
|
||||
|
||||
|
||||
def result(call=71, session="tr-local", **changes):
|
||||
value = {
|
||||
"event": "done", "diagnosis_id": 9,
|
||||
"live_context": {
|
||||
"call_record_id": call, "transcription_session_id": session,
|
||||
"revision": "a" * 64, "segment_count": 151,
|
||||
"through_timestamp_ms": 1793332800000, "transcription_status": "running",
|
||||
"sources": [{"id": "patient-1", "role": "patient", "timestamp_ms": 1793332800000,
|
||||
"text": "这是用于软件测试的合成对话。"}],
|
||||
},
|
||||
"live_assistance": {
|
||||
"summary": [{"text": "合成测试要点", "evidence_ids": ["patient-1"]}],
|
||||
"questions": [], "checks": [], "review": [],
|
||||
},
|
||||
}
|
||||
value.update(changes)
|
||||
return value
|
||||
|
||||
|
||||
class Repo:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
self.cancels = []
|
||||
self.response = result()
|
||||
self.started = Event()
|
||||
self.release = None
|
||||
self.fail = False
|
||||
|
||||
def stream_video_ai(self, diagnosis_id, **kwargs):
|
||||
self.calls.append((diagnosis_id, kwargs))
|
||||
self.started.set()
|
||||
if self.release is not None:
|
||||
assert self.release.wait(3)
|
||||
if self.fail:
|
||||
raise RuntimeError("private connection details must not leak")
|
||||
yield deepcopy(self.response)
|
||||
|
||||
def cancel_diagnosis_ai(self, diagnosis_id, request_id):
|
||||
self.cancels.append((diagnosis_id, request_id))
|
||||
return {"accepted": True}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configured():
|
||||
repo = Repo()
|
||||
clock = [100.0]
|
||||
controller = LiveAiController(repo, diagnosis_id=9, enabled=True, clock=lambda: clock[0])
|
||||
controller.bind(71, "tr-local")
|
||||
yield controller, repo, clock
|
||||
if repo.release is not None:
|
||||
repo.release.set()
|
||||
controller.close()
|
||||
assert controller.wait(3)
|
||||
|
||||
|
||||
def run(controller, clock):
|
||||
clock[0] += 10
|
||||
controller.tick()
|
||||
assert controller.wait(3)
|
||||
return controller.take_state()
|
||||
|
||||
|
||||
def test_only_persisted_current_call_changes_trigger(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("another-call")
|
||||
run(controller, clock)
|
||||
assert not repo.calls
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["summary"][0]["evidence_ids"] == ["patient-1"]
|
||||
assert state["segmentCount"] == 151
|
||||
assert "transcript" not in repo.calls[0][1]
|
||||
assert repo.calls[0][1]["call_record_id"] == 71
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
|
||||
|
||||
def test_cloud_retry_clears_failure_without_losing_saved_evidence(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
before = run(controller, clock)
|
||||
controller.transcription_error("云转写中断", status="failed")
|
||||
controller.transcription_running("stale-session")
|
||||
assert controller.take_state()["transcriptionStatus"] == "failed"
|
||||
controller.transcription_running("tr-local")
|
||||
after = controller.take_state()
|
||||
assert after["transcriptionStatus"] == "running"
|
||||
assert after["error"] == ""
|
||||
assert after["summary"] == before["summary"]
|
||||
assert after["revision"] == before["revision"]
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
|
||||
|
||||
def test_one_inflight_and_many_changes_coalesce(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.release = Event()
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 2
|
||||
controller.tick()
|
||||
assert repo.started.wait(2)
|
||||
for _ in range(50):
|
||||
controller.changed("tr-local")
|
||||
controller.tick()
|
||||
assert len(repo.calls) == 1
|
||||
repo.release.set()
|
||||
assert controller.wait(3)
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 2
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 2
|
||||
|
||||
|
||||
def test_pause_stops_auto_but_explicit_refresh_works(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.handle("toggle", False)
|
||||
controller.changed("tr-local")
|
||||
run(controller, clock)
|
||||
assert not repo.calls
|
||||
controller.handle("refresh")
|
||||
state = run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
assert state["enabled"] is False
|
||||
|
||||
|
||||
def test_no_retry_loop_on_failure(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.fail = True
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error"
|
||||
assert "private" not in str(state)
|
||||
for _ in range(5):
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
repo.fail = False
|
||||
controller.handle("refresh")
|
||||
assert run(controller, clock)["phase"] == "ready"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mutation", [
|
||||
lambda r: r.update(diagnosis_id=10),
|
||||
lambda r: r["live_context"].update(call_record_id=72),
|
||||
lambda r: r["live_context"].update(transcription_session_id="other"),
|
||||
lambda r: r["live_context"].update(revision="invalid"),
|
||||
lambda r: r["live_context"].update(segment_count=-1),
|
||||
lambda r: r["live_context"].update(through_timestamp_ms=float("nan")),
|
||||
lambda r: r["live_context"]["sources"][0].update(role="guessed"),
|
||||
lambda r: r["live_context"]["sources"][0].update(timestamp_ms=True),
|
||||
lambda r: r["live_assistance"]["summary"][0].update(evidence_ids=["made-up"]),
|
||||
lambda r: r["live_assistance"]["summary"][0].update(evidence_ids=[]),
|
||||
])
|
||||
def test_invalid_identity_or_citations_never_published(configured, mutation):
|
||||
controller, repo, clock = configured
|
||||
mutation(repo.response)
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error"
|
||||
assert state["summary"] == []
|
||||
|
||||
|
||||
def test_new_call_discards_late_result(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.release = Event()
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 2
|
||||
controller.tick()
|
||||
assert repo.started.wait(2)
|
||||
controller.bind(72, "tr-next")
|
||||
repo.release.set()
|
||||
assert controller.wait(3)
|
||||
state = controller.take_state()
|
||||
assert state["summary"] == []
|
||||
assert state["revision"] == ""
|
||||
|
||||
|
||||
def test_dialing_clears_previous_call_before_new_transcription_exists(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["summary"]
|
||||
controller.reset()
|
||||
state = controller.take_state()
|
||||
assert state["summary"] == [] and state["sources"] == []
|
||||
assert state["transcriptionStatus"] == "idle"
|
||||
controller.changed("tr-local")
|
||||
controller.handle("review")
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
|
||||
|
||||
def test_complete_long_source_quote_is_not_truncated(configured):
|
||||
controller, repo, clock = configured
|
||||
source = "合成测试原文。" * 400
|
||||
repo.response["live_context"]["sources"][0]["text"] = source
|
||||
repo.response["live_assistance"]["summary"][0]["text"] = "患者原话:" + source
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["summary"][0]["text"] == "患者原话:" + source
|
||||
|
||||
|
||||
def test_previous_good_result_retained_during_update_and_failure(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["summary"]
|
||||
repo.release = Event()
|
||||
repo.fail = True
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 10
|
||||
controller.tick()
|
||||
state = controller.take_state()
|
||||
assert state["busy"] and state["summary"]
|
||||
repo.release.set()
|
||||
assert controller.wait(3)
|
||||
assert controller.take_state()["summary"]
|
||||
|
||||
|
||||
def test_review_only_after_transcript_finish(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
run(controller, clock)
|
||||
controller.handle("review")
|
||||
run(controller, clock)
|
||||
assert len(repo.calls) == 1
|
||||
controller.finish("tr-local", "partial")
|
||||
controller.handle("review")
|
||||
state = run(controller, clock)
|
||||
assert repo.calls[-1][1]["task"] == "live_review"
|
||||
assert state["transcriptionStatus"] == "partial"
|
||||
|
||||
|
||||
def test_no_permission_no_work(configured):
|
||||
_, repo, clock = configured
|
||||
disabled = LiveAiController(repo, diagnosis_id=9, enabled=False)
|
||||
disabled.bind(71, "tr-local")
|
||||
disabled.changed("tr-local")
|
||||
disabled.handle("refresh")
|
||||
disabled.tick()
|
||||
assert not repo.calls
|
||||
assert disabled.take_state()["available"] is False
|
||||
disabled.close()
|
||||
|
||||
|
||||
def test_first_and_subsequent_updates_have_short_bounded_coalescing(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 0.79
|
||||
controller.tick()
|
||||
assert not repo.calls
|
||||
# More speech does not indefinitely postpone the first request.
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 0.02
|
||||
controller.tick()
|
||||
assert controller.wait(3)
|
||||
assert len(repo.calls) == 1
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 1.99
|
||||
controller.tick()
|
||||
assert len(repo.calls) == 1
|
||||
clock[0] += 0.02
|
||||
controller.tick()
|
||||
assert controller.wait(3)
|
||||
assert len(repo.calls) == 2
|
||||
|
||||
|
||||
def test_validated_suggestions_are_visible_before_stream_finishes(configured):
|
||||
controller, repo, clock = configured
|
||||
delivered = Event()
|
||||
repo.release = Event()
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
yield result(event="live_update")
|
||||
delivered.set()
|
||||
assert repo.release.wait(3)
|
||||
yield result()
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 1
|
||||
controller.tick()
|
||||
assert delivered.wait(2)
|
||||
state = controller.take_state()
|
||||
assert state["busy"] is True and state["phase"] == "analyzing"
|
||||
assert state["firstResultMs"] == 0
|
||||
assert state["summary"] and state["revision"] == "a" * 64
|
||||
assert state["pendingSegments"] == 0
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 3
|
||||
controller.tick()
|
||||
state = controller.take_state()
|
||||
assert state["elapsedMs"] == 3000 and state["pendingSegments"] == 1
|
||||
repo.release.set()
|
||||
assert controller.wait(3)
|
||||
state = controller.take_state()
|
||||
assert state["busy"] is False and state["lastDurationMs"] == 3000
|
||||
assert state["firstResultMs"] == 0
|
||||
assert state["pendingSegments"] == 1
|
||||
|
||||
|
||||
def test_slow_request_does_not_add_another_wait_for_queued_speech(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.release = Event()
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 1
|
||||
controller.tick()
|
||||
assert repo.started.wait(2)
|
||||
controller.changed("tr-local")
|
||||
clock[0] += 20
|
||||
controller.tick()
|
||||
assert len(repo.calls) == 1
|
||||
repo.release.set()
|
||||
assert controller.wait(3)
|
||||
# The queued snapshot is already due; no new sleep after the slow request ends.
|
||||
controller.tick()
|
||||
assert controller.wait(3)
|
||||
assert len(repo.calls) == 2
|
||||
assert controller.take_state()["pendingSegments"] == 0
|
||||
|
||||
|
||||
def test_failed_stream_keeps_only_validated_progress(configured):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
yield result(event="live_update")
|
||||
raise ApiTimeoutError("secret raw upstream content")
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error" and state["summary"]
|
||||
assert state["errorCode"] == "UPSTREAM_TIMEOUT"
|
||||
assert "尚未全部完成" in state["message"]
|
||||
assert "secret" not in str(state)
|
||||
|
||||
|
||||
def test_progress_cannot_change_snapshot_mid_request(configured):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
yield result(event="live_update")
|
||||
different = result()
|
||||
different["live_context"]["revision"] = "b" * 64
|
||||
yield different
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error"
|
||||
assert state["revision"] == "a" * 64
|
||||
|
||||
|
||||
@pytest.mark.parametrize("scope", ["current_call_only", "authorized_case"])
|
||||
def test_displayed_scope_comes_from_validated_server_result(configured, scope):
|
||||
controller, repo, clock = configured
|
||||
repo.response["analysis_scope"] = scope
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["analysisScope"] == scope
|
||||
controller.bind(72, "new-session")
|
||||
assert controller.take_state()["analysisScope"] == ""
|
||||
|
||||
|
||||
def test_legacy_server_does_not_claim_dialogue_only(configured):
|
||||
controller, repo, clock = configured
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["analysisScope"] == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("scope", [None, [], "invented"])
|
||||
def test_unrecognized_result_scope_is_rejected(configured, scope):
|
||||
controller, repo, clock = configured
|
||||
repo.response["analysis_scope"] = scope
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error" and not state["summary"]
|
||||
assert state["analysisScope"] == ""
|
||||
|
||||
|
||||
def test_scope_cannot_change_within_one_stream(configured):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
yield result(event="live_update", analysis_scope="current_call_only")
|
||||
yield result(analysis_scope="authorized_case")
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error" and state["summary"]
|
||||
assert state["analysisScope"] == "current_call_only"
|
||||
|
||||
|
||||
def test_scope_of_previous_results_survives_review_start_or_failure(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.response["analysis_scope"] = "current_call_only"
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["analysisScope"] == "current_call_only"
|
||||
controller.finish("tr-local", "completed")
|
||||
observed = []
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
assert kwargs["task"] == "live_review"
|
||||
yield {"event": "start", "analysis_scope": "authorized_case"}
|
||||
observed.append(controller.take_state())
|
||||
raise ApiTimeoutError("synthetic timeout")
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.handle("review")
|
||||
state = run(controller, clock)
|
||||
assert observed[0]["busy"] and observed[0]["analysisScope"] == "current_call_only"
|
||||
assert state["phase"] == "error" and state["analysisScope"] == "current_call_only"
|
||||
|
||||
|
||||
def test_successful_review_and_refresh_replace_displayed_scope(configured):
|
||||
controller, repo, clock = configured
|
||||
repo.response["analysis_scope"] = "current_call_only"
|
||||
controller.changed("tr-local")
|
||||
assert run(controller, clock)["analysisScope"] == "current_call_only"
|
||||
controller.finish("tr-local", "completed")
|
||||
repo.response["analysis_scope"] = "authorized_case"
|
||||
controller.handle("review")
|
||||
assert run(controller, clock)["analysisScope"] == "authorized_case"
|
||||
assert repo.calls[-1][1]["task"] == "live_review"
|
||||
repo.response["analysis_scope"] = "current_call_only"
|
||||
controller.handle("refresh")
|
||||
assert run(controller, clock)["analysisScope"] == "current_call_only"
|
||||
assert repo.calls[-1][1]["task"] == "live_consultation"
|
||||
|
||||
|
||||
def test_items_in_the_same_group_are_published_cumulatively(configured):
|
||||
controller, repo, clock = configured
|
||||
observed = []
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
response = result(event="live_update")
|
||||
for index in range(3):
|
||||
response["live_assistance"]["questions"].append({
|
||||
"text": f"合成测试追问 {index + 1}?", "evidence_ids": ["patient-1"],
|
||||
})
|
||||
yield deepcopy(response)
|
||||
observed.append(controller.take_state())
|
||||
yield {**response, "event": "done"}
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert [len(item["questions"]) for item in observed] == [1, 2, 3]
|
||||
assert all(item["busy"] and item["revision"] == "a" * 64 for item in observed)
|
||||
assert len(state["questions"]) == 3 and not state["busy"]
|
||||
|
||||
|
||||
def test_invalid_progress_never_publishes_or_accepts_a_later_done(configured):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
bad = result(event="live_update")
|
||||
bad["live_assistance"]["summary"][0]["evidence_ids"] = ["invented"]
|
||||
yield bad
|
||||
yield result()
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["phase"] == "error" and not state["summary"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error, expected", [
|
||||
(ApiTimeoutError("private payload"), "UPSTREAM_TIMEOUT"),
|
||||
(ApiHttpError("private payload", status_code=403), "PERMISSION_DENIED"),
|
||||
(ApiHttpError("private payload", status_code=404), "SERVICE_UPDATE_REQUIRED"),
|
||||
(ApiBusinessError("private payload", data={"error_code": "LIVE_RESULT_INVALID"}), "LIVE_RESULT_INVALID"),
|
||||
(ApiBusinessError("private payload", data={"error_code": "UPSTREAM_TIMEOUT"}), "UPSTREAM_TIMEOUT"),
|
||||
(ApiBusinessError("private payload", data={"error_code": {"secret": "payload"}}), "UPSTREAM_UNAVAILABLE"),
|
||||
])
|
||||
def test_failure_categories_do_not_leak_payloads(configured, error, expected, caplog):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
raise error
|
||||
yield
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert state["errorCode"] == expected
|
||||
assert "private payload" not in str(state) + caplog.text
|
||||
assert expected in caplog.text
|
||||
|
||||
|
||||
def test_validated_error_reason_distinguishes_citations_from_format(configured):
|
||||
controller, repo, clock = configured
|
||||
|
||||
def stream(*args, **kwargs):
|
||||
raise ApiBusinessError("private source text", data={
|
||||
"error_code": "LIVE_RESULT_INVALID", "live_validation_reason": "CITATION_INVALID",
|
||||
})
|
||||
yield
|
||||
|
||||
repo.stream_video_ai = stream
|
||||
controller.changed("tr-local")
|
||||
state = run(controller, clock)
|
||||
assert "引用无法核对" in state["error"]
|
||||
assert "private" not in str(state)
|
||||
@@ -0,0 +1,82 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.core.errors import ApiProtocolError
|
||||
from doctor_workstation.services.repository import RemoteDoctorRepository
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, events=None):
|
||||
self.calls = []
|
||||
self.events = events if events is not None else [{"event": "done", "data": {"diagnosis_id": 9, "live_context": {"revision": "a" * 64}}}]
|
||||
|
||||
def post_event_stream(self, endpoint, body, **kwargs):
|
||||
self.calls.append((endpoint, body, kwargs))
|
||||
yield from self.events
|
||||
|
||||
|
||||
def invoke(client, **changes):
|
||||
args = {"call_record_id": 71, "transcription_session_id": "tr-local", "request_id": "a" * 36}
|
||||
args.update(changes)
|
||||
return list(RemoteDoctorRepository(client).stream_video_ai(9, **args))
|
||||
|
||||
|
||||
def test_scope_is_server_bound_and_structured_done_is_preserved():
|
||||
client = Client()
|
||||
result = invoke(client)
|
||||
assert result[0]["live_context"]["revision"] == "a" * 64
|
||||
endpoint, body, options = client.calls[0]
|
||||
assert endpoint == "tcm.diagnosis/aiAssistantStream"
|
||||
assert body["call_record_id"] == 71 and body["transcription_session_id"] == "tr-local"
|
||||
assert body["task"] == "live_consultation"
|
||||
assert "audio" not in body and "transcript" not in body
|
||||
assert options["timeout"] == 105
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [{"call_record_id": True}, {"call_record_id": 0},
|
||||
{"transcription_session_id": ""}, {"transcription_session_id": "../other"},
|
||||
{"task": "prescription_generate"}])
|
||||
def test_bad_identity_or_task_never_sends(changes):
|
||||
client = Client()
|
||||
with pytest.raises(ValueError):
|
||||
invoke(client, **changes)
|
||||
assert client.calls == []
|
||||
|
||||
|
||||
def test_early_disconnect_does_not_fallback_to_unbound_general_assistant():
|
||||
client = Client([])
|
||||
with pytest.raises(ApiProtocolError):
|
||||
invoke(client)
|
||||
assert len(client.calls) == 1
|
||||
|
||||
|
||||
def test_cancelled_stream_does_not_publish_late_done():
|
||||
client = Client()
|
||||
assert invoke(client, cancelled=lambda: True) == []
|
||||
|
||||
|
||||
def test_validated_progress_is_forwarded_before_done_without_ending_stream():
|
||||
client = Client([
|
||||
{"event": "live_update", "data": {"diagnosis_id": 9, "live_assistance": {"questions": []}}},
|
||||
{"event": "done", "data": {"diagnosis_id": 9, "live_context": {"revision": "a" * 64}}},
|
||||
])
|
||||
events = invoke(client)
|
||||
assert [event["event"] for event in events] == ["live_update", "done"]
|
||||
assert events[0]["live_assistance"] == {"questions": []}
|
||||
|
||||
|
||||
def test_progress_does_not_substitute_for_final_done():
|
||||
client = Client([{"event": "live_update", "data": {"diagnosis_id": 9}}])
|
||||
with pytest.raises(ApiProtocolError):
|
||||
invoke(client)
|
||||
|
||||
|
||||
def test_server_analysis_scope_is_preserved_in_stream_metadata():
|
||||
client = Client([
|
||||
{"event": kind, "data": {"analysis_scope": "current_call_only"}}
|
||||
for kind in ("start", "live_update", "done")
|
||||
])
|
||||
events = invoke(client)
|
||||
assert [event["analysis_scope"] for event in events] == ["current_call_only"] * 3
|
||||
assert "analysis_scope" not in client.calls[0][1]
|
||||
@@ -0,0 +1,571 @@
|
||||
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())
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Trusted native PCM ingestion and exact transcript/AI identity handoff."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.video import window
|
||||
from doctor_workstation.video.security import TrustedDocumentPolicy
|
||||
|
||||
pytestmark = pytest.mark.skipif(not window.WEBENGINE_AVAILABLE, reason="optional QtWebEngine")
|
||||
TRUSTED = "https://video.example.test/call/index.html"
|
||||
|
||||
|
||||
def host(**changes):
|
||||
item = SimpleNamespace(
|
||||
_closing=False, _released=False, _shutdown_requested=False, _injected=True,
|
||||
_start_requested=True, _call_cycle_closed=False,
|
||||
_policy=TrustedDocumentPolicy.from_url(TRUSTED, is_local=False),
|
||||
_page=Mock(), _local_transcription=Mock(), _live_ai=Mock(),
|
||||
_video_ai_timer=Mock(), _asr_session_id="tr-local", _asr_ready=True,
|
||||
_asr_partial=False, _transcription_final_status="completed",
|
||||
lifecycle=SimpleNamespace(call_record_id=71, transcription_session_id="tr-local"),
|
||||
request=SimpleNamespace(user_id="doctor_1", target_user_id="patient_9"),
|
||||
_save_transcript_segment=Mock(),
|
||||
)
|
||||
item._page.url.return_value.toString.return_value = TRUSTED
|
||||
for key, value in changes.items():
|
||||
setattr(item, key, value)
|
||||
item._local_transcription_context_is_trusted = lambda: window._EmbeddedVideoWindow._local_transcription_context_is_trusted(item)
|
||||
item._on_local_transcription = lambda event: window._EmbeddedVideoWindow._on_local_transcription(item, event)
|
||||
return item
|
||||
|
||||
|
||||
def chunk(**changes):
|
||||
return {"event": "local-transcription-chunk", "sessionId": "tr-local", "role": "patient",
|
||||
"sequence": 3, "timestamp": 1793332800000.0, "data": "AQACAA==", **changes}
|
||||
|
||||
|
||||
def test_pcm_uses_bound_session_and_double_timestamp_is_converted_safely():
|
||||
item = host()
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, chunk())
|
||||
item._local_transcription.submit.assert_called_once_with("tr-local", "patient", 3, 1793332800000, "AQACAA==")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [{"sessionId": "other"}, {"timestamp": float("nan")},
|
||||
{"timestamp": float("inf")}, {"timestamp": -1}, {"timestamp": True}])
|
||||
def test_wrong_session_or_unbounded_pcm_metadata_rejected(changes):
|
||||
item = host()
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, chunk(**changes))
|
||||
item._local_transcription.submit.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [{"_closing": True}, {"_released": True}, {"_injected": False}])
|
||||
def test_untrusted_lifecycle_cannot_ingest_audio(changes):
|
||||
item = host(**changes)
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, chunk())
|
||||
item._local_transcription.submit.assert_not_called()
|
||||
|
||||
|
||||
def test_navigation_blocks_pcm_and_analysis_commands():
|
||||
item = host()
|
||||
item._page.url.return_value.toString.return_value = "https://another.example.test/"
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, chunk())
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, {"event": "video-ai-action", "action": "refresh"})
|
||||
item._local_transcription.submit.assert_not_called()
|
||||
item._live_ai.handle.assert_not_called()
|
||||
|
||||
|
||||
def test_pending_shutdown_drains_pcm_but_cannot_start_new_inference_session():
|
||||
item = host(_shutdown_requested=True)
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, chunk())
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, {"event": "local-transcription-start", "sessionId": "tr-next"})
|
||||
window._EmbeddedVideoWindow._handle_video_assistance_message(item, {"event": "video-ai-action", "action": "refresh"})
|
||||
item._local_transcription.submit.assert_called_once()
|
||||
item._local_transcription.start.assert_not_called()
|
||||
item._live_ai.handle.assert_not_called()
|
||||
|
||||
|
||||
def test_asr_segment_is_persisted_with_host_speaker_before_json_display():
|
||||
item = host()
|
||||
event = {"type": "segment", "sessionId": "tr-local", "role": "patient", "segmentId": "tr-local:patient:3",
|
||||
"text": '没有;0.5 mg,不是 5 mg。</script>\u2028', "timestamp": 1793332800000, "isCompleted": True}
|
||||
item._on_local_transcription(event)
|
||||
item._save_transcript_segment.assert_called_once()
|
||||
saved = item._save_transcript_segment.call_args.args[0]
|
||||
assert saved["segment"]["text"] == event["text"]
|
||||
assert saved["segment"]["speaker_user_id"] == "patient_9"
|
||||
script = item._page.runJavaScript.call_args.args[0]
|
||||
assert json.loads(script.split("?.(", 1)[1][:-2]) == event
|
||||
item._live_ai.changed.assert_not_called() # waits for persistence ACK
|
||||
|
||||
|
||||
def test_late_asr_segment_cannot_enter_new_call():
|
||||
item = host(_asr_session_id="tr-next")
|
||||
item._on_local_transcription({"type": "segment", "sessionId": "tr-local", "role": "patient"})
|
||||
item._save_transcript_segment.assert_not_called()
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_persistence_ack_is_the_only_ai_dirty_trigger():
|
||||
item = host()
|
||||
window._EmbeddedVideoWindow._on_transcription_completed(item, "segment", "other", "s1", True, "")
|
||||
item._live_ai.changed.assert_not_called()
|
||||
window._EmbeddedVideoWindow._on_transcription_completed(item, "segment", "tr-local", "s1", True, "")
|
||||
item._live_ai.changed.assert_called_once_with("tr-local")
|
||||
window._EmbeddedVideoWindow._on_transcription_completed(item, "segment", "tr-local", "s2", False, "")
|
||||
assert item._asr_partial
|
||||
assert item._live_ai.changed.call_count == 1
|
||||
|
||||
|
||||
def test_model_missing_is_failed_and_partial_chunk_is_not_false_ready():
|
||||
item = host(_asr_ready=False)
|
||||
item._on_local_transcription({"type": "error", "sessionId": "tr-local", "message": "本地模型未安装"})
|
||||
item._live_ai.transcription_error.assert_called_once_with("本地模型未安装", status="failed")
|
||||
|
||||
|
||||
def test_shutdown_keeps_asr_for_drain_then_closes_it():
|
||||
item = host()
|
||||
window._EmbeddedVideoWindow._close_video_assistance(item, close_asr=False)
|
||||
item._live_ai.close.assert_called_once()
|
||||
item._local_transcription.close.assert_not_called()
|
||||
window._EmbeddedVideoWindow._close_video_assistance(item)
|
||||
item._local_transcription.close.assert_called_once()
|
||||
|
||||
|
||||
def test_special_pcm_bridge_bounds_data_and_preserves_epoch():
|
||||
callback = Mock()
|
||||
bridge = window._QtVideoBridge(callback, Mock())
|
||||
bridge.appendLocalTranscriptionChunk("tr-local", "doctor", 0, 1793332800000.0, "x" * 512001)
|
||||
callback.assert_not_called()
|
||||
bridge.appendLocalTranscriptionChunk("tr-local", "doctor", 0, 1793332800000.0, "AQACAA==")
|
||||
assert callback.call_args.args[0]["timestamp"] == 1793332800000.0
|
||||
@@ -0,0 +1,58 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import socket
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
from PySide6.QtWidgets import QApplication, QDialog, QPlainTextEdit, QWidget
|
||||
|
||||
from doctor_workstation.core.transcript_text import simplified_transcript
|
||||
from doctor_workstation.ui.dialogs.diagnosis import DiagnosisDialog
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("source", "expected"),
|
||||
[
|
||||
("你會說老家話嗎?很會,很會。", "你会说老家话吗?很会,很会。"),
|
||||
("沒有過敏,沒有吃藥。0.5 mg,不是 5 mg。", "没有过敏,没有吃药。0.5 mg,不是 5 mg。"),
|
||||
("血壓 120/80 mmHg,血糖 2.00–30.00 mmol/L,HbA1c 6.5%。",
|
||||
"血压 120/80 mmHg,血糖 2.00–30.00 mmol/L,HbA1c 6.5%。"),
|
||||
("唔係,冇食藥,咩事?", "唔系,冇食药,咩事?"),
|
||||
("患者:口乾。\n醫生:不?\n患者:不。", "患者:口干。\n医生:不?\n患者:不。"),
|
||||
("简体,不! \n0.5 mg\tB12", "简体,不! \n0.5 mg\tB12"),
|
||||
("", ""),
|
||||
],
|
||||
)
|
||||
def test_script_conversion_is_offline_and_preserves_clinical_tokens(monkeypatch, source, expected):
|
||||
def no_network(*args, **kwargs):
|
||||
pytest.fail("Script conversion must use bundled dictionaries only")
|
||||
|
||||
monkeypatch.setattr(socket.socket, "connect", no_network)
|
||||
# A fresh thread loads its own dictionaries, even when a prior test ran.
|
||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||
assert pool.submit(simplified_transcript, source).result() == expected
|
||||
assert simplified_transcript(expected) == expected
|
||||
|
||||
|
||||
def test_historical_transcript_dialog_displays_simplified_without_changing_record(monkeypatch):
|
||||
application = QApplication.instance() or QApplication([])
|
||||
parent = QWidget()
|
||||
record = {"transcript_text": "患者:你會說老家話嗎?\n患者:沒有吃藥,0.5 mg。"}
|
||||
source = DiagnosisDialog._call_transcript_text(record)
|
||||
displayed = []
|
||||
|
||||
def inspect(dialog):
|
||||
displayed.append(dialog.findChild(QPlainTextEdit, "DiagnosisCallTranscriptText").toPlainText())
|
||||
return 0
|
||||
|
||||
monkeypatch.setattr(QDialog, "exec", inspect)
|
||||
try:
|
||||
DiagnosisDialog._view_call_transcript(parent, 12, source)
|
||||
assert displayed == ["患者:你会说老家话吗?\n患者:没有吃药,0.5 mg。"]
|
||||
assert record["transcript_text"] == source
|
||||
finally:
|
||||
parent.close()
|
||||
application.processEvents()
|
||||
@@ -0,0 +1,234 @@
|
||||
"""Retry a failed storage start without duplicate requests or reopening a call."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.video.launcher import VideoCallRequest
|
||||
from doctor_workstation.video.lifecycle import OrderedCallLifecycle
|
||||
|
||||
|
||||
class Repository:
|
||||
def __init__(self, *, fail_first: bool = False, block_attempt: int = 0) -> None:
|
||||
self.fail_first = fail_first
|
||||
self.block_attempt = block_attempt
|
||||
self.attempts: list[dict] = []
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def start_call(self, **_payload):
|
||||
return {"call_record_id": 71}
|
||||
|
||||
def start_call_transcription(self, **payload):
|
||||
self.attempts.append(payload)
|
||||
attempt = len(self.attempts)
|
||||
if attempt == self.block_attempt:
|
||||
self.entered.set()
|
||||
assert self.release.wait(3), "test did not release storage request"
|
||||
if self.fail_first and attempt == 1:
|
||||
raise RuntimeError("temporary storage failure")
|
||||
|
||||
def finish_call_transcription(self, **_payload):
|
||||
return True
|
||||
|
||||
def end_call(self, **_payload):
|
||||
return True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_lifecycle():
|
||||
lifecycles = []
|
||||
|
||||
def make(**options):
|
||||
repository = Repository(**options)
|
||||
request = VideoCallRequest(
|
||||
sdk_app_id=1400123456,
|
||||
user_id="doctor-test",
|
||||
user_sig="test-ticket",
|
||||
target_user_id="patient-test",
|
||||
diagnosis_id=12,
|
||||
)
|
||||
lifecycle = OrderedCallLifecycle(request, repository, logging.getLogger(__name__))
|
||||
lifecycles.append((lifecycle, repository))
|
||||
assert lifecycle.start().result(timeout=2) is True
|
||||
return lifecycle, repository
|
||||
|
||||
yield make
|
||||
for lifecycle, repository in lifecycles:
|
||||
repository.release.set()
|
||||
lifecycle.end("test-cleanup").result(timeout=3)
|
||||
assert lifecycle.wait(3)
|
||||
|
||||
|
||||
def test_failed_storage_start_retries_same_session_then_reuses_success(make_lifecycle):
|
||||
lifecycle, repository = make_lifecycle(fail_first=True)
|
||||
failed = lifecycle.start_transcription("tr-retry", language="zh")
|
||||
with pytest.raises(RuntimeError, match="temporary storage failure"):
|
||||
failed.result(timeout=2)
|
||||
|
||||
retry = lifecycle.start_transcription("tr-retry", language="zh")
|
||||
assert retry is not failed
|
||||
assert retry.result(timeout=2) is True
|
||||
assert lifecycle.start_transcription("tr-retry") is retry
|
||||
assert lifecycle.transcription_session_id == "tr-retry"
|
||||
assert len(repository.attempts) == 2
|
||||
assert (
|
||||
repository.attempts[0]
|
||||
== repository.attempts[1]
|
||||
== {
|
||||
"diagnosis_id": 12,
|
||||
"call_record_id": 71,
|
||||
"transcription_session_id": "tr-retry",
|
||||
"language": "zh",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_inflight_start_is_reused_without_blocking_caller(make_lifecycle):
|
||||
lifecycle, repository = make_lifecycle(block_attempt=1)
|
||||
first = lifecycle.start_transcription("tr-running")
|
||||
try:
|
||||
assert repository.entered.wait(2)
|
||||
assert not first.done()
|
||||
assert lifecycle.start_transcription("tr-running") is first
|
||||
assert len(repository.attempts) == 1
|
||||
finally:
|
||||
repository.release.set()
|
||||
assert first.result(timeout=2) is True
|
||||
|
||||
|
||||
def test_concurrent_retry_callers_share_one_new_request(make_lifecycle):
|
||||
lifecycle, repository = make_lifecycle(fail_first=True, block_attempt=2)
|
||||
with pytest.raises(RuntimeError, match="temporary storage failure"):
|
||||
lifecycle.start_transcription("tr-concurrent").result(timeout=2)
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=4) as callers:
|
||||
futures = list(
|
||||
callers.map(lambda _: lifecycle.start_transcription("tr-concurrent"), range(4))
|
||||
)
|
||||
assert repository.entered.wait(2)
|
||||
assert all(future is futures[0] for future in futures)
|
||||
assert not futures[0].done()
|
||||
assert len(repository.attempts) == 2
|
||||
finally:
|
||||
repository.release.set()
|
||||
assert futures[0].result(timeout=2) is True
|
||||
|
||||
|
||||
def test_false_completion_can_retry_without_changing_identity(make_lifecycle):
|
||||
lifecycle, repository = make_lifecycle()
|
||||
# Seed the documented False completion (e.g. call-start dependency had not
|
||||
# succeeded), then exercise retry against a now-available actual lifecycle.
|
||||
previous: Future[bool] = Future()
|
||||
previous.set_result(False)
|
||||
lifecycle._transcription_session_id = "tr-false"
|
||||
lifecycle._transcription_start_future = previous
|
||||
|
||||
retry = lifecycle.start_transcription("tr-false")
|
||||
assert retry is not previous
|
||||
assert retry.result(timeout=2) is True
|
||||
assert lifecycle.start_transcription("tr-false") is retry
|
||||
assert len(repository.attempts) == 1
|
||||
assert repository.attempts[0]["transcription_session_id"] == "tr-false"
|
||||
|
||||
|
||||
def test_cancelled_queued_start_can_retry_same_session(make_lifecycle):
|
||||
lifecycle, repository = make_lifecycle()
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
|
||||
def occupy_worker():
|
||||
entered.set()
|
||||
assert release.wait(3)
|
||||
|
||||
lifecycle._worker.submit("test-block", occupy_worker)
|
||||
try:
|
||||
assert entered.wait(2)
|
||||
cancelled = lifecycle.start_transcription("tr-cancelled")
|
||||
assert cancelled.cancel()
|
||||
retry = lifecycle.start_transcription("tr-cancelled")
|
||||
assert retry is not cancelled
|
||||
finally:
|
||||
release.set()
|
||||
assert retry.result(timeout=2) is True
|
||||
assert len(repository.attempts) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("state", ["running", "succeeded", "failed", "false"])
|
||||
def test_session_identity_cannot_change_even_after_failure(make_lifecycle, state):
|
||||
lifecycle, repository = make_lifecycle(
|
||||
fail_first=state == "failed", block_attempt=1 if state == "running" else 0
|
||||
)
|
||||
if state == "false":
|
||||
first: Future[bool] = Future()
|
||||
first.set_result(False)
|
||||
lifecycle._transcription_session_id = "tr-original"
|
||||
lifecycle._transcription_start_future = first
|
||||
else:
|
||||
first = lifecycle.start_transcription("tr-original")
|
||||
if state == "running":
|
||||
assert repository.entered.wait(2)
|
||||
elif state == "failed":
|
||||
with pytest.raises(RuntimeError, match="temporary storage failure"):
|
||||
first.result(timeout=2)
|
||||
else:
|
||||
assert first.result(timeout=2)
|
||||
try:
|
||||
with pytest.raises(RuntimeError, match="another transcription session"):
|
||||
lifecycle.start_transcription("tr-different")
|
||||
assert lifecycle.transcription_session_id == "tr-original"
|
||||
finally:
|
||||
repository.release.set()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("terminal", ["finish", "end"])
|
||||
@pytest.mark.parametrize("failed", [False, True])
|
||||
def test_finish_or_end_blocks_restart_before_and_after_completion(make_lifecycle, terminal, failed):
|
||||
lifecycle, repository = make_lifecycle(fail_first=failed)
|
||||
first = lifecycle.start_transcription("tr-terminal")
|
||||
if failed:
|
||||
with pytest.raises(RuntimeError, match="temporary storage failure"):
|
||||
first.result(timeout=2)
|
||||
else:
|
||||
assert first.result(timeout=2)
|
||||
|
||||
ending = (
|
||||
lifecycle.finish_transcription(status="partial")
|
||||
if terminal == "finish"
|
||||
else lifecycle.end("test-terminal")
|
||||
)
|
||||
error = "already stopped" if terminal == "finish" else "already ended"
|
||||
with pytest.raises(RuntimeError, match=error):
|
||||
lifecycle.start_transcription("tr-terminal")
|
||||
ending.result(timeout=2)
|
||||
with pytest.raises(RuntimeError, match=error):
|
||||
lifecycle.start_transcription("tr-terminal")
|
||||
assert len(repository.attempts) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("terminal", ["finish", "end"])
|
||||
def test_terminal_request_blocks_start_even_while_original_start_is_running(
|
||||
make_lifecycle, terminal
|
||||
):
|
||||
lifecycle, repository = make_lifecycle(block_attempt=1)
|
||||
first = lifecycle.start_transcription("tr-draining")
|
||||
try:
|
||||
assert repository.entered.wait(2)
|
||||
ending = (
|
||||
lifecycle.finish_transcription()
|
||||
if terminal == "finish"
|
||||
else lifecycle.end("test-draining")
|
||||
)
|
||||
assert not first.done()
|
||||
assert not ending.done()
|
||||
error = "already stopped" if terminal == "finish" else "already ended"
|
||||
with pytest.raises(RuntimeError, match=error):
|
||||
lifecycle.start_transcription("tr-draining")
|
||||
finally:
|
||||
repository.release.set()
|
||||
assert first.result(timeout=2) is True
|
||||
assert ending.result(timeout=2) is True
|
||||
assert len(repository.attempts) == 1
|
||||
@@ -62,7 +62,7 @@ def test_companion_archives_cloud_video_local_mixed_audio_and_transcript() -> No
|
||||
assert "operations.push(stopLocalRecording())" in source
|
||||
assert "operations.push(stopTranscription('completed'))" in source
|
||||
assert "Promise.allSettled(operations)" in source
|
||||
assert "腾讯云混流视频、本机语音录音和实时转写均已启动" in source
|
||||
assert "本机录音已启动" in source
|
||||
|
||||
|
||||
def test_companion_watches_room_id_for_the_entire_call_cycle() -> None:
|
||||
@@ -83,7 +83,7 @@ def test_companion_watches_room_id_for_the_entire_call_cycle() -> None:
|
||||
assert "diagnosisId: activeConfig.diagnosisId" in room_source
|
||||
|
||||
|
||||
def test_room_binding_is_acknowledged_and_transcriber_room_is_a_fallback() -> None:
|
||||
def test_room_binding_is_acknowledged_independently_of_cloud_transcription() -> None:
|
||||
companion_source = (
|
||||
PROJECT_ROOT / "video_companion" / "src" / "main.ts"
|
||||
).read_text(encoding="utf-8")
|
||||
@@ -91,8 +91,11 @@ def test_room_binding_is_acknowledged_and_transcriber_room_is_a_fallback() -> No
|
||||
PROJECT_ROOT / "src" / "doctor_workstation" / "video" / "window.py"
|
||||
).read_text(encoding="utf-8")
|
||||
|
||||
assert "observeRoomId(roomId)" in companion_source
|
||||
assert "onRealtimeTranscriberStarted: (roomId)" in companion_source
|
||||
assert "observeRoomId(readRoomId())" in companion_source
|
||||
assert "getAITranscriberManager" in companion_source
|
||||
assert "startRealtimeTranscriber" in companion_source
|
||||
assert "sourceLanguage: 'zh'" in companion_source
|
||||
assert "LocalPcmCapture" not in companion_source
|
||||
assert "function roomBindingResult(" in companion_source
|
||||
assert "if (boundRoomId) return" in companion_source
|
||||
assert "roomBindingResult?.(" in window_source
|
||||
@@ -186,7 +189,7 @@ def test_local_audio_capture_keeps_and_persists_its_own_call_room_identity() ->
|
||||
assert "store.bind_identity(" in source
|
||||
|
||||
|
||||
def test_companion_shows_incremental_subtitles_but_only_persists_final_segments() -> None:
|
||||
def test_companion_displays_only_native_normalized_cloud_transcript_acks() -> None:
|
||||
main_source = (
|
||||
PROJECT_ROOT / "video_companion" / "src" / "main.ts"
|
||||
).read_text(encoding="utf-8")
|
||||
@@ -196,13 +199,18 @@ def test_companion_shows_incremental_subtitles_but_only_persists_final_segments(
|
||||
handler = main_source.split("function handleTranscriberMessage", 1)[1].split(
|
||||
"function subscribeTranscriber", 1
|
||||
)[0]
|
||||
acknowledgement = main_source.split("function transcriptionResult", 1)[1].split(
|
||||
"function videoAiAction", 1
|
||||
)[0]
|
||||
|
||||
assert "showLiveCaption(message)" in handler
|
||||
assert "if (message.isCompleted !== true) return" in handler
|
||||
assert handler.index("showLiveCaption(message)") < handler.index(
|
||||
"if (message.isCompleted !== true) return"
|
||||
)
|
||||
assert 'aria-label="实时语音字幕"' in component_source
|
||||
assert "transcription-segment" in handler
|
||||
assert "showLiveCaption(" not in handler
|
||||
assert "message.isCompleted !== true" in handler
|
||||
assert "sessionId === transcriptionSessionId" in acknowledgement
|
||||
assert "normalizedSegment?.segmentId === segmentId" in acknowledgement
|
||||
assert "normalizedSegment.isCompleted === true" in acknowledgement
|
||||
assert "showLiveCaption(normalizedSegment)" in acknowledgement
|
||||
assert 'aria-label="腾讯云实时转写的对话原文"' in component_source
|
||||
assert "liveCaptions.value" in component_source
|
||||
assert "caption.speaker" in component_source
|
||||
assert "caption.text" in component_source
|
||||
@@ -219,7 +227,7 @@ def test_companion_keeps_transcript_and_patient_case_visible_in_a_side_rail() ->
|
||||
|
||||
assert "liveCaptions.value = [...previous, caption].slice(-120)" in main_source
|
||||
assert "liveCaptionClearTimer" not in main_source
|
||||
assert 'aria-label="患者病例与实时对话"' in component_source
|
||||
assert 'aria-label="AI辅助、对话原文与患者资料"' in component_source
|
||||
assert 'id="patient-case-title"' in component_source
|
||||
assert 'id="live-transcript-title"' in component_source
|
||||
assert 'aria-label="打开完整诊单"' in component_source
|
||||
@@ -559,7 +567,7 @@ def test_built_video_companion_contains_the_patient_case_rail() -> None:
|
||||
assert ".consultation-rail" in styles
|
||||
assert ".video-layer--with-rail" in styles
|
||||
assert "patientCase" in scripts
|
||||
assert "患者病例与实时对话" in scripts
|
||||
assert "AI辅助、对话原文与患者资料" in scripts
|
||||
|
||||
|
||||
def test_companion_screenshot_requires_doctor_confirmation_before_upload() -> None:
|
||||
|
||||
Reference in New Issue
Block a user