"""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。"\\\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。"\\\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()