Merge branch 'master' of http://git.zhenyangtang.com.cn/ZYT/zyt
This commit is contained in:
@@ -26,6 +26,7 @@ from PySide6.QtWidgets import (
|
||||
|
||||
from doctor_workstation.core import PermissionSet
|
||||
from doctor_workstation.services import ApiClient, DemoDoctorRepository, RemoteDoctorRepository
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.dialogs import ai_consult as ai_consult_module
|
||||
from doctor_workstation.ui.dialogs.ai_consult import (
|
||||
AiConsultDialog,
|
||||
@@ -2306,14 +2307,14 @@ def test_risk_block_uses_the_red_alert_palette() -> None:
|
||||
assert "#FEF3F2" in risk_card
|
||||
assert "#F1B35C" not in risk_card # 旧的橙色描边
|
||||
marker = qss.split("QLabel#AiConsultRiskMarker {", 1)[1].split("}", 1)[0]
|
||||
assert "#BE4B58" in marker
|
||||
assert PALETTE["danger"] in marker
|
||||
|
||||
|
||||
def test_clinical_bodies_are_no_longer_rendered_at_eleven_pixels() -> None:
|
||||
qss = ai_consult_module.AI_CONSULT_QSS
|
||||
|
||||
body = qss.rsplit("QLabel#AiConsultRiskBody {", 1)[1].split("}", 1)[0]
|
||||
assert "color: #1a1c1f" in body.lower()
|
||||
assert f'color: {PALETTE["text"]}' in body
|
||||
assert "font-size: 14px" in body
|
||||
block = qss.split("QLabel#AiConsultClinicalBody,", 1)[1].split("}", 1)[0]
|
||||
assert "font-size: 14px" in block
|
||||
|
||||
@@ -10,6 +10,7 @@ from PySide6.QtCore import QDate, QSize, Qt
|
||||
from PySide6.QtWidgets import QApplication, QStyle, QStyleOptionButton, QWidget
|
||||
|
||||
from doctor_workstation.ui.appointment_drawer import APPOINTMENT_DRAWER_QSS, AppointmentDrawer
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -565,8 +566,39 @@ def test_keyboard_focus_has_a_visible_state(
|
||||
assert focus_target.hasFocus()
|
||||
assert application.focusWidget() is focus_target
|
||||
assert 'QPushButton[appointmentDate="true"]:focus' in APPOINTMENT_DRAWER_QSS
|
||||
assert "border-color: #8B9AD9;" in APPOINTMENT_DRAWER_QSS
|
||||
assert f"border-color: {PALETTE['focus']};" in APPOINTMENT_DRAWER_QSS
|
||||
|
||||
drawer.close()
|
||||
host.close()
|
||||
application.processEvents()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("button_kind", ["date", "slot"])
|
||||
def test_appointment_hover_keeps_hit_target_stable_after_relayout(
|
||||
application: QApplication, button_kind: str,
|
||||
) -> None:
|
||||
"""Hovering must not move a time choice or restore a stale layout position."""
|
||||
from PySide6.QtCore import QEvent, QPoint, QPointF
|
||||
from PySide6.QtGui import QEnterEvent
|
||||
|
||||
host, drawer = _show_drawer(
|
||||
application, (1440, 900), repository=_VisualRepository(), autoload=True,
|
||||
)
|
||||
tomorrow = QDate.currentDate().addDays(1).toString("yyyy-MM-dd")
|
||||
button = drawer.date_buttons[tomorrow] if button_kind == "date" else drawer.slot_buttons["09:30-10:00"]
|
||||
original = button.geometry()
|
||||
try:
|
||||
for _ in range(3):
|
||||
application.sendEvent(button, QEnterEvent(QPointF(10, 10), QPointF(30, 34), QPointF(30, 34)))
|
||||
assert button.geometry() == original
|
||||
assert button.graphicsEffect() is None
|
||||
relocated = original.topLeft() + QPoint(12, 16)
|
||||
button.move(relocated) # A resize/reflow while the pointer stays inside.
|
||||
application.sendEvent(button, QEvent(QEvent.Type.Leave))
|
||||
assert button.pos() == relocated
|
||||
button.setGeometry(original)
|
||||
button.click()
|
||||
assert button.isChecked()
|
||||
finally:
|
||||
drawer.close()
|
||||
host.close()
|
||||
|
||||
@@ -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,343 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from collections.abc import Callable, Iterator
|
||||
from types import MethodType, SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtCore import QRect, QSize, Qt
|
||||
from PySide6.QtGui import QCloseEvent
|
||||
from PySide6.QtWidgets import QApplication, QDialog, QMainWindow
|
||||
|
||||
from doctor_workstation.app import ApplicationController
|
||||
from doctor_workstation.video import window as video_window
|
||||
|
||||
|
||||
class _CallWindow(QMainWindow):
|
||||
"""Exercise production Qt close handling without starting WebEngine or media."""
|
||||
|
||||
closeEvent = video_window._EmbeddedVideoWindow.closeEvent
|
||||
set_compact_close_handler = video_window._EmbeddedVideoWindow.set_compact_close_handler
|
||||
hangup = video_window._EmbeddedVideoWindow.hangup
|
||||
_request_companion_shutdown = video_window._EmbeddedVideoWindow._request_companion_shutdown
|
||||
_close_from_companion = video_window._EmbeddedVideoWindow._close_from_companion
|
||||
_force_requested_shutdown = video_window._EmbeddedVideoWindow._force_requested_shutdown
|
||||
_begin_shutdown = video_window._EmbeddedVideoWindow._begin_shutdown
|
||||
_handle_bridge_message = video_window._EmbeddedVideoWindow._handle_bridge_message
|
||||
_on_load_finished = video_window._EmbeddedVideoWindow._on_load_finished
|
||||
_after_injection = video_window._EmbeddedVideoWindow._after_injection
|
||||
|
||||
def __init__(self, **changes: Any) -> None:
|
||||
super().__init__()
|
||||
self._on_compact_close: Callable[[], None] | None = None
|
||||
self._closing = False
|
||||
self._released = False
|
||||
self._shutdown_requested = False
|
||||
self._companion_ended = False
|
||||
self._injected = True
|
||||
self._start_requested = True
|
||||
self._call_cycle_closed = False
|
||||
self._media_active = True
|
||||
self._close_reason = "window-closed"
|
||||
self.open_im = False
|
||||
self.lifecycle = SimpleNamespace(end=Mock())
|
||||
self._page = SimpleNamespace(runJavaScript=Mock())
|
||||
self._shutdown_timer = SimpleNamespace(start=Mock(), stop=Mock())
|
||||
self._abort_local_audio_recording = Mock()
|
||||
self._append_local_audio_chunk = Mock()
|
||||
self._release_webengine = Mock(side_effect=self._mark_released)
|
||||
self.restore_screenshot_preview = Mock()
|
||||
self.call_ended = SimpleNamespace(emit=Mock())
|
||||
self.call_error = SimpleNamespace(emit=Mock())
|
||||
self.logger = Mock()
|
||||
self.request = SimpleNamespace(safe_log_context=lambda: {})
|
||||
for key, value in changes.items():
|
||||
setattr(self, key, value)
|
||||
self.setProperty("doctorSkipOpenGeometry", True)
|
||||
self.setMinimumSize(620, 420)
|
||||
self.setGeometry(40, 50, 680, 480)
|
||||
|
||||
def _mark_released(self) -> None:
|
||||
self._released = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def qapp() -> QApplication:
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_window(qapp: QApplication) -> Iterator[Callable[..., _CallWindow]]:
|
||||
windows: list[_CallWindow] = []
|
||||
|
||||
def create(**changes: Any) -> _CallWindow:
|
||||
window = _CallWindow(**changes)
|
||||
windows.append(window)
|
||||
return window
|
||||
|
||||
yield create
|
||||
for window in windows:
|
||||
window.set_compact_close_handler(None)
|
||||
window._companion_ended = True
|
||||
window.close()
|
||||
window.deleteLater()
|
||||
qapp.processEvents()
|
||||
|
||||
|
||||
def _controller() -> SimpleNamespace:
|
||||
controller = SimpleNamespace(_video_preview_state=None, _video_preview_generation=0)
|
||||
controller._show_video_preview = MethodType(
|
||||
ApplicationController._show_video_preview, controller
|
||||
)
|
||||
controller._restore_video_preview = MethodType(
|
||||
ApplicationController._restore_video_preview, controller
|
||||
)
|
||||
return controller
|
||||
|
||||
|
||||
def test_native_compact_close_restores_visible_window_and_next_close_ends_call(
|
||||
qapp: QApplication, make_window: Callable[..., _CallWindow]
|
||||
) -> None:
|
||||
window = make_window()
|
||||
window.show()
|
||||
qapp.processEvents()
|
||||
original_geometry = QRect(window.geometry())
|
||||
original_minimum = QSize(window.minimumSize())
|
||||
controller = _controller()
|
||||
dialog = QDialog()
|
||||
|
||||
controller._show_video_preview(window, dialog)
|
||||
qapp.processEvents()
|
||||
|
||||
assert window.isVisible()
|
||||
assert window.minimumSize() == QSize(440, 300)
|
||||
assert window.geometry() != original_geometry
|
||||
assert window.windowFlags() & Qt.WindowType.WindowStaysOnTopHint
|
||||
assert window._on_compact_close is not None
|
||||
|
||||
assert window.close() is False
|
||||
qapp.processEvents()
|
||||
|
||||
assert window.isVisible()
|
||||
assert window.geometry() == original_geometry
|
||||
assert window.minimumSize() == original_minimum
|
||||
assert not window.windowFlags() & Qt.WindowType.WindowStaysOnTopHint
|
||||
assert window.property("doctorSkipOpenGeometry") is False
|
||||
assert controller._video_preview_state is None
|
||||
assert window._on_compact_close is None
|
||||
assert window._shutdown_requested is False
|
||||
window._page.runJavaScript.assert_not_called()
|
||||
window.lifecycle.end.assert_not_called()
|
||||
window._abort_local_audio_recording.assert_not_called()
|
||||
window._release_webengine.assert_not_called()
|
||||
window.restore_screenshot_preview.assert_called_once_with()
|
||||
|
||||
assert window.close() is False
|
||||
|
||||
assert window._shutdown_requested is True
|
||||
assert window.isVisible()
|
||||
assert window._closing is False
|
||||
window._page.runJavaScript.assert_called_once()
|
||||
window._release_webengine.assert_not_called()
|
||||
|
||||
window._handle_bridge_message({"event": "hangup", "status": "local-hangup"})
|
||||
qapp.processEvents()
|
||||
|
||||
assert not window.isVisible()
|
||||
assert window._closing is True
|
||||
window._release_webengine.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["hangup", "facade-close"])
|
||||
@pytest.mark.parametrize("wait_for_recording", [False, True], ids=["idle", "recording"])
|
||||
def test_explicit_shutdown_bypasses_compact_restore_and_drains_recording(
|
||||
make_window: Callable[..., _CallWindow], action: str, wait_for_recording: bool
|
||||
) -> None:
|
||||
window = make_window(_start_requested=wait_for_recording)
|
||||
restore = Mock()
|
||||
window.set_compact_close_handler(restore)
|
||||
window.show()
|
||||
|
||||
def end_call() -> None:
|
||||
if action == "hangup":
|
||||
window.hangup()
|
||||
else:
|
||||
video_window.VideoCallWindow.close(SimpleNamespace(_session=window))
|
||||
|
||||
end_call()
|
||||
|
||||
restore.assert_not_called()
|
||||
assert window._shutdown_requested is True
|
||||
if wait_for_recording:
|
||||
assert window.isVisible()
|
||||
assert window._closing is False
|
||||
window.lifecycle.end.assert_not_called()
|
||||
window._release_webengine.assert_not_called()
|
||||
window._abort_local_audio_recording.assert_not_called()
|
||||
window._shutdown_timer.start.assert_called_once_with(190_000)
|
||||
window._page.runJavaScript.assert_called_once()
|
||||
assert "doctorConsultation?.close?.()" in window._page.runJavaScript.call_args.args[0]
|
||||
|
||||
end_call()
|
||||
window.close()
|
||||
window._page.runJavaScript.assert_called_once()
|
||||
window._handle_bridge_message(
|
||||
{"event": "local-audio-chunk", "sessionId": "recording", "sequence": 7, "data": "YQ=="}
|
||||
)
|
||||
window._append_local_audio_chunk.assert_called_once_with("recording", 7, "YQ==")
|
||||
window._release_webengine.assert_not_called()
|
||||
window._handle_bridge_message({"event": "hangup", "status": "local-hangup"})
|
||||
else:
|
||||
window._page.runJavaScript.assert_not_called()
|
||||
window._shutdown_timer.start.assert_not_called()
|
||||
reason = "desktop-hangup" if action == "hangup" else "window-closed"
|
||||
window.lifecycle.end.assert_called_once_with(reason)
|
||||
|
||||
restore.assert_not_called()
|
||||
assert not window.isVisible()
|
||||
assert window._on_compact_close is None
|
||||
assert window._closing is True
|
||||
assert window._released is True
|
||||
assert window._media_active is False
|
||||
window._shutdown_timer.stop.assert_called_once_with()
|
||||
window._abort_local_audio_recording.assert_called_once_with("")
|
||||
window._release_webengine.assert_called_once_with()
|
||||
window._begin_shutdown()
|
||||
window._release_webengine.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"trigger",
|
||||
["remote-hangup", "remote-error", "timeout", "load-failed", "load-failed-active", "bridge-missing"],
|
||||
)
|
||||
def test_terminal_paths_close_compact_window_without_restoring(
|
||||
make_window: Callable[..., _CallWindow], trigger: str
|
||||
) -> None:
|
||||
window = make_window()
|
||||
restore = Mock()
|
||||
window.set_compact_close_handler(restore)
|
||||
window.show()
|
||||
|
||||
if trigger == "remote-hangup":
|
||||
window._handle_bridge_message({"event": "hangup", "status": "remote-hangup"})
|
||||
window.call_ended.emit.assert_called_once_with("remote-hangup")
|
||||
elif trigger == "remote-error":
|
||||
window._handle_bridge_message({"event": "error", "message": "connection lost"})
|
||||
window.call_error.emit.assert_called_once_with("connection lost")
|
||||
elif trigger == "timeout":
|
||||
window.hangup()
|
||||
window._force_requested_shutdown()
|
||||
window.lifecycle.end.assert_any_call("desktop-hangup-timeout")
|
||||
elif trigger.startswith("load-failed"):
|
||||
if trigger == "load-failed":
|
||||
window._injected = False
|
||||
window._start_requested = False
|
||||
window._on_load_finished(False)
|
||||
assert window._close_reason == "page-load-failed"
|
||||
if trigger == "load-failed-active":
|
||||
assert window._closing is False
|
||||
window._release_webengine.assert_not_called()
|
||||
restore.assert_not_called()
|
||||
window._force_requested_shutdown()
|
||||
window.lifecycle.end.assert_any_call("page-load-failed-timeout")
|
||||
else:
|
||||
window._after_injection(False)
|
||||
assert window._close_reason == "bridge-api-missing"
|
||||
|
||||
restore.assert_not_called()
|
||||
assert not window.isVisible()
|
||||
assert window._closing is True
|
||||
assert window._released is True
|
||||
assert window._on_compact_close is None
|
||||
window._release_webengine.assert_called_once_with()
|
||||
window._shutdown_timer.stop.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"state", ["_shutdown_requested", "_companion_ended", "_closing", "_released"]
|
||||
)
|
||||
def test_close_event_never_restores_a_stopping_or_released_call(
|
||||
make_window: Callable[..., _CallWindow], state: str
|
||||
) -> None:
|
||||
window = make_window(_start_requested=False, **{state: True})
|
||||
restore = Mock()
|
||||
window.set_compact_close_handler(restore)
|
||||
event = QCloseEvent()
|
||||
|
||||
window.closeEvent(event)
|
||||
|
||||
restore.assert_not_called()
|
||||
assert event.isAccepted()
|
||||
|
||||
|
||||
def test_repeated_preview_refreshes_handler_and_ignores_old_dialog_generation(
|
||||
make_window: Callable[..., _CallWindow],
|
||||
) -> None:
|
||||
window = make_window()
|
||||
controller = _controller()
|
||||
first_dialog, second_dialog, third_dialog = QDialog(), QDialog(), QDialog()
|
||||
controller._show_video_preview(window, first_dialog)
|
||||
first_handler = window._on_compact_close
|
||||
original_state = controller._video_preview_state
|
||||
|
||||
controller._show_video_preview(window, second_dialog)
|
||||
second_handler = window._on_compact_close
|
||||
assert first_handler is not None
|
||||
assert second_handler is not None
|
||||
assert first_handler is not second_handler
|
||||
assert controller._video_preview_state is original_state
|
||||
first_dialog.finished.emit(0)
|
||||
first_handler()
|
||||
assert controller._video_preview_state is original_state
|
||||
assert window._on_compact_close is second_handler
|
||||
|
||||
second_handler()
|
||||
assert controller._video_preview_state is None
|
||||
assert window._on_compact_close is None
|
||||
|
||||
controller._show_video_preview(window, third_dialog)
|
||||
current_handler = window._on_compact_close
|
||||
second_dialog.finished.emit(0)
|
||||
second_handler()
|
||||
assert controller._video_preview_state is not None
|
||||
assert window._on_compact_close is current_handler
|
||||
|
||||
third_dialog.finished.emit(0)
|
||||
assert controller._video_preview_state is None
|
||||
assert window._on_compact_close is None
|
||||
window.lifecycle.end.assert_not_called()
|
||||
window._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_switching_preview_restores_old_window_and_isolates_old_callbacks(
|
||||
make_window: Callable[..., _CallWindow],
|
||||
) -> None:
|
||||
first, second = make_window(), make_window()
|
||||
controller = _controller()
|
||||
first_dialog, second_dialog = QDialog(), QDialog()
|
||||
first_geometry = QRect(first.geometry())
|
||||
controller._show_video_preview(first, first_dialog)
|
||||
old_handler = first._on_compact_close
|
||||
|
||||
controller._show_video_preview(second, second_dialog)
|
||||
current_handler = second._on_compact_close
|
||||
|
||||
assert first._on_compact_close is None
|
||||
assert first.geometry() == first_geometry
|
||||
assert controller._video_preview_state["window"] is second
|
||||
assert old_handler is not None
|
||||
old_handler()
|
||||
first_dialog.finished.emit(0)
|
||||
assert controller._video_preview_state["window"] is second
|
||||
assert second._on_compact_close is current_handler
|
||||
|
||||
second_dialog.finished.emit(0)
|
||||
assert controller._video_preview_state is None
|
||||
assert second._on_compact_close is None
|
||||
first.lifecycle.end.assert_not_called()
|
||||
second.lifecycle.end.assert_not_called()
|
||||
@@ -22,6 +22,7 @@ from PySide6.QtWidgets import (
|
||||
)
|
||||
|
||||
from doctor_workstation.core import PermissionSet
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.diagnosis_drawer import (
|
||||
DiagnosisChoiceButtons,
|
||||
DiagnosisComboBox,
|
||||
@@ -1012,14 +1013,14 @@ def test_choice_chips_keep_visible_checked_style_when_readonly(
|
||||
application.processEvents()
|
||||
# Sample the pad (not glyph center) so white text does not hide the fill.
|
||||
enabled_color = selected.grab().toImage().pixelColor(6, selected.height() // 2)
|
||||
assert enabled_color.name().lower() == "#eef1fa"
|
||||
assert enabled_color.name().lower() == PALETTE["accent_soft"].lower()
|
||||
diet.setReadOnly(True)
|
||||
application.processEvents()
|
||||
disabled_color = selected.grab().toImage().pixelColor(6, selected.height() // 2)
|
||||
assert selected.isChecked()
|
||||
assert selected.isEnabled()
|
||||
assert diet.isReadOnly()
|
||||
assert disabled_color.name().lower() == "#eef1fa"
|
||||
assert disabled_color.name().lower() == PALETTE["accent_soft"].lower()
|
||||
dialog.close()
|
||||
application.processEvents()
|
||||
|
||||
@@ -1039,7 +1040,7 @@ def test_view_only_drawer_shows_selected_choice_chips(
|
||||
selected = next(button for button in diet._buttons if button.isChecked())
|
||||
application.processEvents()
|
||||
color = selected.grab().toImage().pixelColor(6, selected.height() // 2)
|
||||
assert color.name().lower() == "#eef1fa"
|
||||
assert color.name().lower() == PALETTE["accent_soft"].lower()
|
||||
assert diet.isReadOnly()
|
||||
assert not dialog.save_button.isVisibleTo(dialog)
|
||||
dialog.close()
|
||||
|
||||
@@ -23,6 +23,7 @@ from PySide6.QtWidgets import (
|
||||
)
|
||||
|
||||
from doctor_workstation.core import PermissionSet
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.diagnosis_index_widgets import (
|
||||
DIAGNOSIS_INDEX_QSS,
|
||||
DiagnosisItemDelegate,
|
||||
@@ -424,8 +425,8 @@ def test_empty_loading_and_compact_footer_keep_the_table_shell(
|
||||
@pytest.mark.parametrize(
|
||||
("record", "stripe", "channel"),
|
||||
[
|
||||
(_row(701, DiagnosisViewRecord=[{"is_confirmed": 0}]), "#a9691d", "warning"),
|
||||
(_row(702, has_appointment=0, appointments=[]), "#4f63d9", "info"),
|
||||
(_row(701, DiagnosisViewRecord=[{"is_confirmed": 0}]), PALETTE["warning"].lower(), "warning"),
|
||||
(_row(702, has_appointment=0, appointments=[]), PALETTE["accent"].lower(), "info"),
|
||||
],
|
||||
)
|
||||
def test_semantic_hover_preserves_three_pixel_stripe_on_neutral_background(
|
||||
@@ -452,9 +453,9 @@ def test_semantic_hover_preserves_three_pixel_stripe_on_neutral_background(
|
||||
assert image.pixelColor(0, 20).name() == stripe
|
||||
assert image.pixelColor(1, 20).name() == stripe
|
||||
assert image.pixelColor(2, 20).name() == stripe
|
||||
assert image.pixelColor(3, 20).name() == "#f7f7f7", f"{channel} stripe widened"
|
||||
assert image.pixelColor(6, 20).name() == "#f7f7f7"
|
||||
assert image.pixelColor(40, 20).name() == "#f7f7f7"
|
||||
assert image.pixelColor(3, 20).name() == PALETTE["surface_alt"].lower(), f"{channel} stripe widened"
|
||||
assert image.pixelColor(6, 20).name() == PALETTE["surface_alt"].lower()
|
||||
assert image.pixelColor(40, 20).name() == PALETTE["surface_alt"].lower()
|
||||
|
||||
|
||||
def test_page_hides_fixed_shadow_and_preserves_admin_token_contract(
|
||||
|
||||
@@ -9,10 +9,17 @@ from pathlib import Path
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import QPoint, Qt
|
||||
from PySide6.QtGui import QFont
|
||||
from PySide6.QtCore import QPoint, QRect, Qt
|
||||
from PySide6.QtGui import QFont, QImage, QPainter
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QApplication, QLabel, QToolButton, QVBoxLayout, QWidget
|
||||
from PySide6.QtWidgets import (
|
||||
QApplication,
|
||||
QLabel,
|
||||
QStyleOptionViewItem,
|
||||
QToolButton,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from doctor_workstation.ui.diagnosis_index_widgets import (
|
||||
DiagnosisTableHost,
|
||||
@@ -115,6 +122,79 @@ def test_blue_metrics_are_opt_in_and_expand_for_complete_content(application: QA
|
||||
legacy.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"size,column_width,row_height",
|
||||
[((1024, 640), 84, 75), ((1440, 900), 126, 72)],
|
||||
)
|
||||
def test_followup_paint_keeps_time_whole_and_preserves_density(
|
||||
application: QApplication, size: tuple[int, int], column_width: int, row_height: int,
|
||||
) -> None:
|
||||
class RecordingPainter(QPainter):
|
||||
def __init__(self, image: QImage) -> None:
|
||||
super().__init__(image)
|
||||
self.lines: list[tuple[QRect, int, str]] = []
|
||||
|
||||
def drawText(self, rect: QRect, flags: int, text: str) -> None: # noqa: N802
|
||||
self.lines.append((QRect(rect), flags, text))
|
||||
super().drawText(rect, flags, text)
|
||||
|
||||
row = {
|
||||
"id": 501, "has_prescription": 1,
|
||||
"followup_time_text": "2026-08-17 09:00", "followup_doctor_name": "陈医生",
|
||||
}
|
||||
original = copy.deepcopy(row)
|
||||
host = DiagnosisTableHost(tech_blue=True)
|
||||
host.resize(*size)
|
||||
host.set_rows([row])
|
||||
host.show()
|
||||
_settle(application)
|
||||
try:
|
||||
assert host.main.columnWidth(6) == column_width
|
||||
assert host.main.rowHeight(0) == row_height
|
||||
assert host.fixed.rowHeight(0) == row_height
|
||||
index = host.model.index(0, 6)
|
||||
full_text = "2026-08-17 09:00\n陈医生"
|
||||
assert index.data() == full_text
|
||||
assert index.data(Qt.ItemDataRole.ToolTipRole) == full_text
|
||||
option = QStyleOptionViewItem()
|
||||
option.rect = QRect(0, 0, column_width, row_height)
|
||||
image = QImage(column_width, row_height, QImage.Format.Format_ARGB32)
|
||||
painter = RecordingPainter(image)
|
||||
host.main.itemDelegate().paint(painter, option, index)
|
||||
painter.end()
|
||||
expected = ["2026-08-17", "09:00", "陈医生"] if size[0] == 1024 else [
|
||||
"2026-08-17 09:00", "陈医生",
|
||||
]
|
||||
assert [line[2] for line in painter.lines] == expected
|
||||
content = option.rect.adjusted(8, 8, -8, -8)
|
||||
for rect, flags, _text in painter.lines:
|
||||
assert content.contains(rect)
|
||||
assert flags & Qt.TextFlag.TextSingleLine
|
||||
assert not flags & Qt.TextFlag.TextWrapAnywhere
|
||||
assert row == original
|
||||
finally:
|
||||
host.close()
|
||||
_settle(application)
|
||||
|
||||
|
||||
def test_followup_long_metadata_has_complete_tooltip(application: QApplication) -> None:
|
||||
host = DiagnosisTableHost(tech_blue=True)
|
||||
row = {
|
||||
"id": 502, "has_prescription": 1,
|
||||
"followup_time_text": "2026-08-17 09:00-09:30",
|
||||
"followup_doctor_name": "陈医生(内分泌科专家门诊)", "followup_rx_voided": True,
|
||||
}
|
||||
original = copy.deepcopy(row)
|
||||
host.set_rows([row])
|
||||
index = host.model.index(0, 6)
|
||||
assert index.data(Qt.ItemDataRole.ToolTipRole) == (
|
||||
"2026-08-17 09:00-09:30\n陈医生(内分泌科专家门诊)\n处方已作废"
|
||||
)
|
||||
assert index.data() == index.data(Qt.ItemDataRole.ToolTipRole)
|
||||
assert row == original
|
||||
host.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tech_blue", [False, True])
|
||||
def test_geometry_has_no_minimum_height_feedback_and_tracks_both_scroll_axes(
|
||||
application: QApplication, tech_blue: bool,
|
||||
|
||||
@@ -8,6 +8,7 @@ from PySide6.QtCore import QPoint, Qt, QTimer
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.pages.prescriptions import DoctorMultiSelect, _DoctorSelectionDialog
|
||||
from doctor_workstation.ui.theme import apply_theme
|
||||
|
||||
@@ -74,7 +75,7 @@ def test_checked_row_blue_and_unchecked_white(dialog, application):
|
||||
rect = dialog.listing.visualItemRect(item_for(dialog, doctor_id))
|
||||
colors.append(image.pixelColor(int((rect.right() - 70) * scale),
|
||||
int(rect.center().y() * scale)).name())
|
||||
assert colors == ["#eaf2ff", "#ffffff"]
|
||||
assert colors == [PALETTE["accent_soft"].lower(), PALETTE["surface"].lower()]
|
||||
|
||||
|
||||
def test_accept_commits_cancel_preserves_and_reopen_restores(application):
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Current-peer archive paging preserves old callers and request identities."""
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.services.mock_repository import DemoDoctorRepository
|
||||
from doctor_workstation.services.repository import RemoteDoctorRepository
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
def post(self, endpoint, payload, *, timeout=None):
|
||||
self.calls.append((endpoint, payload, timeout))
|
||||
return {"completed": False, "sync_token": "a" * 48, "errors": []}
|
||||
|
||||
|
||||
def test_sync_optional_parameters_and_timeout_preserve_exact_legacy_payload():
|
||||
client = Client()
|
||||
repository = RemoteDoctorRepository(client)
|
||||
repository.sync_im_chat_messages(501)
|
||||
result = repository.sync_im_chat_messages(501, scope="current", sync_token="a" * 48)
|
||||
assert client.calls == [
|
||||
("tcm.diagnosis/triggerImChatSync", {"diagnosis_id": 501}, 15.0),
|
||||
("tcm.diagnosis/triggerImChatSync",
|
||||
{"diagnosis_id": 501, "scope": "current", "sync_token": "a" * 48}, 15.0),
|
||||
]
|
||||
assert result["completed"] is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kwargs", [{"scope": "other"}, {"sync_token": "bad"}, {"sync_token": None}])
|
||||
def test_sync_rejects_invalid_options_without_network(kwargs):
|
||||
client = Client()
|
||||
with pytest.raises(ValueError):
|
||||
RemoteDoctorRepository(client).sync_im_chat_messages(501, **kwargs)
|
||||
assert not client.calls
|
||||
|
||||
|
||||
def test_mock_sync_supports_paging_contract_and_legacy_receipt():
|
||||
repository = DemoDoctorRepository()
|
||||
assert repository.sync_im_chat_messages(501)["queued"] is True
|
||||
assert repository.sync_im_chat_messages(501, scope="current")["completed"] is True
|
||||
@@ -0,0 +1,270 @@
|
||||
"""Offline tests for native IM AI authorization, synchronization and races."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import deque
|
||||
from threading import BoundedSemaphore, Event
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from doctor_workstation.video import im_ai
|
||||
|
||||
|
||||
class Repository:
|
||||
def __init__(self, pages=None, events=None):
|
||||
self.pages = deque(pages or [{"completed": True, "errors": []}])
|
||||
self.events = events or [{"event": "done", "diagnosis_id": 501, "answer": "合成回复"}]
|
||||
self.sync_calls = []
|
||||
self.stream_calls = []
|
||||
self.cancel_calls = []
|
||||
self.cancel_called = Event()
|
||||
|
||||
def sync_im_chat_messages(self, diagnosis_id, **kwargs):
|
||||
self.sync_calls.append((diagnosis_id, kwargs))
|
||||
return self.pages.popleft()
|
||||
|
||||
def stream_diagnosis_ai(self, diagnosis_id, prompt, **kwargs):
|
||||
self.stream_calls.append((diagnosis_id, prompt, kwargs))
|
||||
yield from self.events
|
||||
|
||||
def cancel_diagnosis_ai(self, diagnosis_id, request_id):
|
||||
self.cancel_calls.append((diagnosis_id, request_id))
|
||||
self.cancel_called.set()
|
||||
return {"cancel_requested": True}
|
||||
|
||||
|
||||
def controller(repository=None, **kwargs):
|
||||
return im_ai.ImAiController(
|
||||
repository or Repository(), diagnosis_id=501, appointment_id=101,
|
||||
doctor_account="doctor_7", enabled=True, **kwargs,
|
||||
)
|
||||
|
||||
|
||||
def request(service, **changes):
|
||||
return {
|
||||
"source": "doctor-call", "event": "im-ai-request", "sessionId": service.session_id,
|
||||
"requestId": str(uuid4()), "contextKey": "view-1", "diagnosisId": "501",
|
||||
"appointmentId": 101, "action": "summary", **changes,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action,task", [("summary", "summary"), ("followup", "custom"), ("reply", "custom")])
|
||||
def test_current_peer_sync_completes_before_ai_and_keeps_identity(action, task):
|
||||
token = "a" * 48
|
||||
repository = Repository(pages=[
|
||||
{"completed": False, "phase": "checking_accounts", "sync_token": token, "errors": []},
|
||||
{"completed": False, "phase": "syncing", "sync_token": token, "errors": []},
|
||||
{"completed": True, "errors": []},
|
||||
], events=[
|
||||
{"event": "start"}, {"event": "delta", "text": "合成"},
|
||||
{"event": "done", "diagnosis_id": 501, "answer": "合成完整回复", "model_label": "模型",
|
||||
"clinical_review_warnings": [{"code": "PULSE_EXCLUDED_FROM_ASSESSMENT", "source_id": "report"}]},
|
||||
])
|
||||
service = controller(repository)
|
||||
message = request(service, action=action)
|
||||
assert service.handle(message)
|
||||
assert service.wait()
|
||||
result = service.take_result()
|
||||
assert result["status"] == "success"
|
||||
assert result["text"] == "合成完整回复"
|
||||
assert result["requestId"] == message["requestId"]
|
||||
assert result["modelLabel"] == "模型"
|
||||
assert "不采集脉象" in result["warnings"][0]
|
||||
assert len(repository.sync_calls) == 3
|
||||
assert repository.sync_calls[0] == (501, {"scope": "current", "sync_token": ""})
|
||||
assert repository.sync_calls[1][1]["sync_token"] == token
|
||||
assert len(repository.stream_calls) == 1
|
||||
diagnosis_id, prompt, options = repository.stream_calls[0]
|
||||
assert diagnosis_id == 501 and options["task"] == task
|
||||
assert options["request_id"] == message["requestId"]
|
||||
assert "doctor_7" in prompt and len(prompt) <= 500
|
||||
assert "不补问脉象" in prompt and "不自动确诊" in prompt
|
||||
assert not repository.cancel_calls
|
||||
assert service.take_result() is None
|
||||
assert not service.handle(message) # A replay cannot submit another billable request.
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [
|
||||
{"sessionId": "other"}, {"diagnosisId": "502"}, {"diagnosisId": True},
|
||||
{"diagnosisId": "0501"}, {"appointmentId": 102}, {"requestId": "invalid"},
|
||||
{"contextKey": ""}, {"contextKey": "x" * 129}, {"action": "prescription"},
|
||||
{"action": {}}, {"source": "another-page"}, {"event": "send-message"},
|
||||
])
|
||||
def test_invalid_identity_or_action_cannot_read_patient_context(changes):
|
||||
repository = Repository()
|
||||
service = controller(repository)
|
||||
assert not service.handle(request(service, **changes))
|
||||
assert not repository.sync_calls and not repository.stream_calls
|
||||
|
||||
|
||||
def test_permission_default_denied_and_each_host_has_unique_session():
|
||||
repository = Repository()
|
||||
disabled = im_ai.ImAiController(
|
||||
repository, diagnosis_id=501, appointment_id=101, doctor_account="doctor_7"
|
||||
)
|
||||
assert disabled.config["enabled"] is False
|
||||
assert not disabled.handle(request(disabled))
|
||||
assert controller().session_id != controller().session_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize("page", [
|
||||
{"queued": True}, {"completed": "true"}, {"completed": True, "errors": ["private detail"]},
|
||||
{"completed": False, "sync_token": "bad"}, None,
|
||||
])
|
||||
def test_incomplete_or_failed_sync_never_generates(page):
|
||||
repository = Repository(pages=[page])
|
||||
service = controller(repository)
|
||||
assert service.handle(request(service))
|
||||
assert service.wait()
|
||||
result = service.take_result()
|
||||
assert result["status"] == "error"
|
||||
assert "private detail" not in str(result)
|
||||
assert not repository.stream_calls and not repository.cancel_calls
|
||||
|
||||
|
||||
def test_sync_step_and_time_budget_are_bounded():
|
||||
page = {"completed": False, "sync_token": "b" * 48, "errors": []}
|
||||
repository = Repository(pages=[page] * 10)
|
||||
service = controller(repository, max_sync_steps=2)
|
||||
service.handle(request(service))
|
||||
assert service.wait()
|
||||
assert len(repository.sync_calls) == 2
|
||||
assert service.take_result()["status"] == "error"
|
||||
assert not repository.stream_calls
|
||||
|
||||
times = iter([0, 0, 46])
|
||||
repository = Repository()
|
||||
service = controller(repository, clock=lambda: next(times))
|
||||
service.handle(request(service))
|
||||
assert service.wait()
|
||||
assert "超时" in service.take_result()["message"]
|
||||
assert not repository.stream_calls
|
||||
|
||||
|
||||
def test_cancel_during_sync_stops_before_model_submission():
|
||||
entered, release = Event(), Event()
|
||||
|
||||
class SlowSync(Repository):
|
||||
def sync_im_chat_messages(self, diagnosis_id, **kwargs):
|
||||
entered.set()
|
||||
assert release.wait(2)
|
||||
return super().sync_im_chat_messages(diagnosis_id, **kwargs)
|
||||
|
||||
repository = SlowSync()
|
||||
service = controller(repository)
|
||||
message = request(service)
|
||||
service.handle(message)
|
||||
try:
|
||||
assert entered.wait(1)
|
||||
assert service.handle({**message, "event": "im-ai-cancel"})
|
||||
assert "尚未向 AI" in service.take_result()["message"]
|
||||
finally:
|
||||
release.set()
|
||||
assert service.wait()
|
||||
assert not repository.stream_calls and not repository.cancel_calls
|
||||
assert service.take_result() is None
|
||||
|
||||
|
||||
def test_cancel_is_independent_and_late_response_cannot_overwrite_next_request():
|
||||
entered, release, cancel_release = Event(), Event(), Event()
|
||||
|
||||
class SlowStream(Repository):
|
||||
def stream_diagnosis_ai(self, *args, **kwargs):
|
||||
self.stream_calls.append((args, kwargs))
|
||||
yield {"event": "delta", "text": "尚未完成"}
|
||||
entered.set()
|
||||
assert release.wait(2)
|
||||
yield {"event": "done", "answer": "迟到的旧结果"}
|
||||
|
||||
def cancel_diagnosis_ai(self, *args):
|
||||
result = super().cancel_diagnosis_ai(*args)
|
||||
assert cancel_release.wait(2)
|
||||
return result
|
||||
|
||||
repository = SlowStream()
|
||||
service = controller(repository)
|
||||
first = request(service)
|
||||
service.handle(first)
|
||||
try:
|
||||
assert entered.wait(1)
|
||||
assert service.take_result()["text"] == "尚未完成"
|
||||
service.handle({**first, "event": "im-ai-cancel"})
|
||||
assert repository.cancel_called.wait(1) # Independent HTTP starts while stream is blocked.
|
||||
assert service.take_result()["status"] == "cancelled"
|
||||
second = request(service, contextKey="view-2")
|
||||
assert service.handle(second)
|
||||
result = service.take_result()
|
||||
assert result["requestId"] == second["requestId"] and result["status"] == "error"
|
||||
assert len(repository.stream_calls) == 1
|
||||
finally:
|
||||
release.set()
|
||||
cancel_release.set()
|
||||
assert service.wait()
|
||||
assert service.take_result() is None
|
||||
|
||||
|
||||
def test_close_invalidates_mailbox_and_cancels_without_waiting():
|
||||
entered, release = Event(), Event()
|
||||
|
||||
class SlowStream(Repository):
|
||||
def stream_diagnosis_ai(self, *args, **kwargs):
|
||||
entered.set()
|
||||
assert release.wait(2)
|
||||
yield {"event": "done", "answer": "旧患者回复"}
|
||||
|
||||
repository = SlowStream()
|
||||
service = controller(repository)
|
||||
message = request(service)
|
||||
service.handle(message)
|
||||
try:
|
||||
assert entered.wait(1)
|
||||
service.close()
|
||||
assert repository.cancel_called.wait(1)
|
||||
assert service.take_result() is None
|
||||
assert not service.handle(request(service))
|
||||
finally:
|
||||
release.set()
|
||||
assert service.wait()
|
||||
assert service.take_result() is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("events", [
|
||||
[{"event": "delta", "text": "不完整"}],
|
||||
[{"event": "done", "answer": ""}],
|
||||
[{"event": "done", "diagnosis_id": 502, "answer": "其他患者"}],
|
||||
[{"event": "delta", "text": "x" * (im_ai.MAX_RESULT_CHARS + 1)}],
|
||||
[{"event": "done", "answer": "x" * (im_ai.MAX_RESULT_CHARS + 1)}],
|
||||
])
|
||||
def test_incomplete_mismatched_and_oversized_results_are_not_usable(events):
|
||||
repository = Repository(events=events)
|
||||
service = controller(repository)
|
||||
service.handle(request(service))
|
||||
assert service.wait()
|
||||
assert service.take_result()["status"] == "error"
|
||||
assert repository.cancel_called.wait(1)
|
||||
assert len(repository.stream_calls) == 1 # No transport/validation retry.
|
||||
|
||||
|
||||
def test_thousands_of_deltas_are_coalesced_into_one_mailbox_result():
|
||||
events = [{"event": "delta", "text": "字"}] * 4000 + [{"event": "done"}]
|
||||
service = controller(Repository(events=events))
|
||||
service.handle(request(service))
|
||||
assert service.wait()
|
||||
result = service.take_result()
|
||||
assert result["status"] == "success" and result["text"] == "字" * 4000
|
||||
assert service.take_result() is None
|
||||
|
||||
|
||||
def test_capacity_rejection_does_not_spawn_or_sync(monkeypatch):
|
||||
slots = BoundedSemaphore(1)
|
||||
slots.acquire()
|
||||
monkeypatch.setattr(im_ai, "_WORKER_SLOTS", slots)
|
||||
repository = Repository()
|
||||
service = controller(repository)
|
||||
try:
|
||||
service.handle(request(service))
|
||||
assert service.take_result()["status"] == "error"
|
||||
assert not repository.sync_calls
|
||||
finally:
|
||||
slots.release()
|
||||
@@ -0,0 +1,157 @@
|
||||
"""Exercise bridge trust and JSON delivery without starting WebEngine or HTTP."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
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/im/index.html"
|
||||
|
||||
|
||||
def host(**changes):
|
||||
item = SimpleNamespace(
|
||||
_closing=False, _released=False, _shutdown_requested=False, _injected=True,
|
||||
_policy=TrustedDocumentPolicy.from_url(TRUSTED, is_local=False),
|
||||
_page=Mock(), _im_ai=Mock(), _im_ai_timer=Mock(),
|
||||
)
|
||||
item._page.url.return_value.toString.return_value = TRUSTED
|
||||
item._im_ai_context_is_trusted = lambda: window._EmbeddedVideoWindow._im_ai_context_is_trusted(item)
|
||||
for key, value in changes.items():
|
||||
setattr(item, key, value)
|
||||
return item
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [
|
||||
{"_closing": True}, {"_released": True}, {"_shutdown_requested": True}, {"_injected": False},
|
||||
])
|
||||
def test_inactive_or_closing_page_cannot_submit_or_receive_ai(changes):
|
||||
item = host(**changes)
|
||||
window._EmbeddedVideoWindow._handle_im_ai_message(item, {"event": "im-ai-request"})
|
||||
window._EmbeddedVideoWindow._flush_im_ai(item)
|
||||
item._im_ai.handle.assert_not_called()
|
||||
item._im_ai.take_result.assert_not_called()
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("url", ["about:blank", "https://evil.example.test/im/index.html", TRUSTED + "?other=1"])
|
||||
def test_document_navigation_cannot_reuse_authenticated_ai_bridge(url):
|
||||
item = host()
|
||||
item._page.url.return_value.toString.return_value = url
|
||||
window._EmbeddedVideoWindow._handle_im_ai_message(item, {"event": "im-ai-request"})
|
||||
window._EmbeddedVideoWindow._flush_im_ai(item)
|
||||
item._im_ai.handle.assert_not_called()
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_trusted_bridge_routes_only_to_identity_checked_controller():
|
||||
item = host()
|
||||
message = {"source": "doctor-call", "event": "im-ai-request", "diagnosisId": 501}
|
||||
window._EmbeddedVideoWindow._handle_im_ai_message(item, message)
|
||||
item._im_ai.handle.assert_called_once_with(message)
|
||||
|
||||
|
||||
def test_result_content_is_json_encoded_and_no_javascript_is_executed():
|
||||
item = host()
|
||||
payload = {"status": "success", "text": '\");window.bad=true;//\n</script>\u2028', "warnings": ["合成提示"]}
|
||||
item._im_ai.take_result.return_value = payload
|
||||
window._EmbeddedVideoWindow._flush_im_ai(item)
|
||||
script = item._page.runJavaScript.call_args.args[0]
|
||||
prefix, suffix = "window.doctorConsultation?.imAiResult?.(", ");"
|
||||
assert script.startswith(prefix) and script.endswith(suffix)
|
||||
assert json.loads(script[len(prefix):-len(suffix)]) == payload
|
||||
assert "\\u2028" in script
|
||||
|
||||
|
||||
def test_host_acknowledges_initial_summary_only_after_request_gate_opens():
|
||||
item = host(_injected=False)
|
||||
item._im_ai.session_id = 'synthetic-session-\\"\u2028'
|
||||
|
||||
def verify_gate(script):
|
||||
assert item._im_ai_context_is_trusted() is True
|
||||
prefix, suffix = "window.doctorConsultation?.imAiReady?.(", ");"
|
||||
assert script.startswith(prefix) and script.endswith(suffix)
|
||||
assert json.loads(script[len(prefix):-len(suffix)]) == item._im_ai.session_id
|
||||
|
||||
item._page.runJavaScript.side_effect = verify_gate
|
||||
window._EmbeddedVideoWindow._after_injection(item, True)
|
||||
item._page.runJavaScript.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [
|
||||
{"_closing": True}, {"_released": True}, {"_shutdown_requested": True},
|
||||
])
|
||||
def test_late_injection_cannot_acknowledge_a_closed_ai_context(changes):
|
||||
item = host(_injected=False, **changes)
|
||||
window._EmbeddedVideoWindow._after_injection(item, True)
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_navigation_cannot_receive_ai_readiness_acknowledgement():
|
||||
item = host(_injected=False)
|
||||
item._page.url.return_value.toString.return_value = "https://evil.example.test/im/index.html"
|
||||
window._EmbeddedVideoWindow._after_injection(item, True)
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_failed_injection_closes_without_ai_readiness_acknowledgement():
|
||||
item = host(_injected=False, _request_companion_shutdown=Mock())
|
||||
window._EmbeddedVideoWindow._after_injection(item, False)
|
||||
item._request_companion_shutdown.assert_called_once_with("bridge-api-missing")
|
||||
item._page.runJavaScript.assert_not_called()
|
||||
|
||||
|
||||
def test_shutdown_cancels_ai_before_waiting_for_media_or_destroying_page():
|
||||
item = host(
|
||||
_start_requested=True, _call_cycle_closed=False, _companion_ended=False,
|
||||
_shutdown_timer=Mock(), close=Mock(),
|
||||
)
|
||||
window._EmbeddedVideoWindow._request_companion_shutdown(item, "window-closed")
|
||||
item._im_ai.close.assert_called_once()
|
||||
item._im_ai_timer.stop.assert_called_once()
|
||||
assert item._shutdown_requested is True
|
||||
item.close.assert_not_called() # Recording shutdown still gets its existing grace period.
|
||||
item._shutdown_timer.start.assert_called_once_with(190_000)
|
||||
|
||||
|
||||
def test_qt_notify_rejects_oversized_or_wrong_source_payloads():
|
||||
callback = Mock()
|
||||
bridge = window._QtVideoBridge(callback, Mock())
|
||||
bridge.notify("x" * 16_385)
|
||||
bridge.notify(json.dumps({"source": "evil", "event": "im-ai-request"}))
|
||||
bridge.notify("[]")
|
||||
callback.assert_not_called()
|
||||
message = {"source": "doctor-call", "event": "im-ai-cancel"}
|
||||
bridge.notify(json.dumps(message))
|
||||
callback.assert_called_once_with(message)
|
||||
|
||||
|
||||
def test_video_module_remains_importable_without_optional_qt():
|
||||
source = Path(__file__).resolve().parents[1] / "src"
|
||||
script = """
|
||||
import builtins
|
||||
original_import = builtins.__import__
|
||||
def without_qt(name, *args, **kwargs):
|
||||
if name == 'PySide6' or name.startswith('PySide6.'):
|
||||
raise ImportError('synthetic core-only installation')
|
||||
return original_import(name, *args, **kwargs)
|
||||
builtins.__import__ = without_qt
|
||||
from doctor_workstation.video.window import WEBENGINE_AVAILABLE, VideoCallWindow
|
||||
from doctor_workstation.video.im_ai import ImAiController
|
||||
assert WEBENGINE_AVAILABLE is False
|
||||
"""
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-c", script], env={**os.environ, "PYTHONPATH": str(source)},
|
||||
capture_output=True, text=True, timeout=10, check=False,
|
||||
)
|
||||
assert result.returncode == 0, result.stderr
|
||||
@@ -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
|
||||
+295
-12
@@ -12,17 +12,16 @@ from __future__ import annotations
|
||||
import os
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
# The module disables itself under the offscreen platform so widget grabs in the
|
||||
# other suites capture settled frames; these tests are about the animation, so
|
||||
# they opt back in.
|
||||
os.environ["DOCTOR_MOTION"] = "on"
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import QEvent, QPoint, QPointF, Qt
|
||||
from PySide6.QtGui import QWheelEvent
|
||||
from PySide6.QtCore import QAbstractAnimation, QEvent, QPoint, QPointF, QPropertyAnimation, Qt
|
||||
from PySide6.QtGui import QKeyEvent, QWheelEvent
|
||||
from PySide6.QtWidgets import (
|
||||
QAbstractItemView,
|
||||
QApplication,
|
||||
QGraphicsDropShadowEffect,
|
||||
QLabel,
|
||||
QListWidget,
|
||||
QPlainTextEdit,
|
||||
QScrollArea,
|
||||
QStackedWidget,
|
||||
QVBoxLayout,
|
||||
@@ -32,6 +31,13 @@ from PySide6.QtWidgets import (
|
||||
from doctor_workstation.ui import motion
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def enable_motion(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
# Opt in per test so collecting this module does not animate other suites'
|
||||
# screenshots or leak motion settings across the shared QApplication.
|
||||
monkeypatch.setenv("DOCTOR_MOTION", "on")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def application() -> QApplication:
|
||||
return QApplication.instance() or QApplication([])
|
||||
@@ -122,15 +128,22 @@ def _scroll_area(application: QApplication) -> QScrollArea:
|
||||
return area
|
||||
|
||||
|
||||
def _wheel(delta: int) -> QWheelEvent:
|
||||
def _wheel(
|
||||
delta: int,
|
||||
*,
|
||||
horizontal: int = 0,
|
||||
pixels: int = 0,
|
||||
modifiers: Qt.KeyboardModifier = Qt.KeyboardModifier.NoModifier,
|
||||
phase: Qt.ScrollPhase = Qt.ScrollPhase.NoScrollPhase,
|
||||
) -> QWheelEvent:
|
||||
return QWheelEvent(
|
||||
QPointF(50, 50),
|
||||
QPointF(50, 50),
|
||||
QPoint(0, 0),
|
||||
QPoint(0, delta),
|
||||
QPoint(0, pixels),
|
||||
QPoint(horizontal, delta),
|
||||
Qt.MouseButton.NoButton,
|
||||
Qt.KeyboardModifier.NoModifier,
|
||||
Qt.ScrollPhase.NoScrollPhase,
|
||||
modifiers,
|
||||
phase,
|
||||
False,
|
||||
)
|
||||
|
||||
@@ -226,3 +239,273 @@ def test_reading_surfaces_get_smooth_scrolling_from_the_theme(
|
||||
|
||||
assert getattr(area, "_doctor_smooth_scroll", None) is not None
|
||||
area.close()
|
||||
|
||||
|
||||
def test_repeated_fade_continues_from_current_opacity(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
motion.fade_in(widget)
|
||||
first = widget._doctor_motion["fade"]
|
||||
first.setCurrentTime(motion.BASE // 2)
|
||||
opacity = widget.graphicsEffect().opacity()
|
||||
|
||||
motion.fade_in(widget)
|
||||
|
||||
assert widget.graphicsEffect().opacity() == pytest.approx(opacity)
|
||||
assert widget._doctor_motion["fade"].startValue() == pytest.approx(opacity)
|
||||
assert first.state() == QAbstractAnimation.State.Stopped
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_fade_out_starts_at_full_opacity(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
widget.show()
|
||||
|
||||
motion.fade_out(widget)
|
||||
|
||||
assert widget.graphicsEffect().opacity() == pytest.approx(1.0)
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_reversing_fade_does_not_run_cancelled_hide_callback(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
widget.show()
|
||||
completed = []
|
||||
motion.fade_out(widget, on_finished=lambda: completed.append(True))
|
||||
outgoing = widget._doctor_motion["fade"]
|
||||
outgoing.setCurrentTime(motion.FAST // 2)
|
||||
opacity = widget.graphicsEffect().opacity()
|
||||
|
||||
motion.fade_in(widget)
|
||||
incoming = widget._doctor_motion["fade"]
|
||||
|
||||
assert incoming.startValue() == pytest.approx(opacity)
|
||||
incoming.setCurrentTime(incoming.duration())
|
||||
application.processEvents()
|
||||
assert widget.isVisible()
|
||||
assert not completed
|
||||
assert widget.graphicsEffect() is None
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_previous_fade_cleanup_does_not_delete_reentrant_fade(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
widget.show()
|
||||
motion.fade_out(widget, on_finished=lambda: motion.fade_in(widget))
|
||||
outgoing = widget._doctor_motion["fade"]
|
||||
|
||||
outgoing.setCurrentTime(outgoing.duration())
|
||||
incoming = widget._doctor_motion["fade"]
|
||||
effect = widget.graphicsEffect()
|
||||
application.processEvents()
|
||||
|
||||
assert widget.graphicsEffect() is effect
|
||||
assert incoming.state() == QAbstractAnimation.State.Running
|
||||
incoming.setCurrentTime(incoming.duration())
|
||||
application.processEvents()
|
||||
assert widget.graphicsEffect() is None
|
||||
assert widget.isVisible()
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_replaced_and_completed_animations_are_released(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
destroyed = []
|
||||
for _ in range(10):
|
||||
motion.fade_in(widget)
|
||||
widget._doctor_motion["fade"].destroyed.connect(lambda: destroyed.append(True))
|
||||
fade = widget._doctor_motion["fade"]
|
||||
fade.setCurrentTime(fade.duration())
|
||||
application.processEvents()
|
||||
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
|
||||
|
||||
assert not widget._doctor_motion
|
||||
assert not widget.findChildren(QPropertyAnimation)
|
||||
assert len(destroyed) == 10
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_fades_preserve_existing_graphics_effects(application: QApplication) -> None:
|
||||
widget = QWidget()
|
||||
shadow = QGraphicsDropShadowEffect(widget)
|
||||
widget.setGraphicsEffect(shadow)
|
||||
|
||||
motion.fade_in(widget)
|
||||
assert widget.graphicsEffect() is shadow
|
||||
motion.fade_out(widget)
|
||||
application.processEvents()
|
||||
|
||||
assert widget.graphicsEffect() is shadow
|
||||
assert widget.isHidden()
|
||||
widget.close()
|
||||
|
||||
|
||||
def test_repeated_enter_does_not_accumulate_position_offset(application: QApplication) -> None:
|
||||
host = QWidget()
|
||||
widget = QWidget(host)
|
||||
origin = QPoint(10, 20)
|
||||
widget.move(origin)
|
||||
motion.enter(widget)
|
||||
widget._doctor_motion["enter"].setCurrentTime(motion.BASE // 3)
|
||||
position = widget.pos()
|
||||
|
||||
motion.enter(widget)
|
||||
|
||||
assert widget.pos() == position
|
||||
arrival = widget._doctor_motion["enter"]
|
||||
assert arrival.endValue() == origin
|
||||
arrival.setCurrentTime(arrival.duration())
|
||||
assert widget.pos() == origin
|
||||
host.close()
|
||||
|
||||
|
||||
def test_disabling_motion_settles_an_active_entry(
|
||||
application: QApplication, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
host = QWidget()
|
||||
widget = QWidget(host)
|
||||
origin = QPoint(10, 20)
|
||||
widget.move(origin)
|
||||
motion.enter(widget)
|
||||
monkeypatch.setenv("DOCTOR_MOTION", "off")
|
||||
|
||||
motion.enter(widget)
|
||||
application.processEvents()
|
||||
|
||||
assert widget.pos() == origin
|
||||
assert widget.graphicsEffect() is None
|
||||
assert not widget._doctor_motion
|
||||
host.close()
|
||||
|
||||
|
||||
def test_wheel_reversal_discards_pending_travel(application: QApplication) -> None:
|
||||
area = _scroll_area(application)
|
||||
motion.install_smooth_scroll(area)
|
||||
scroller = area._doctor_smooth_scroll
|
||||
bar = area.verticalScrollBar()
|
||||
bar.setValue(500)
|
||||
for _ in range(4):
|
||||
scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
scroller._animation.setCurrentTime(30)
|
||||
position = bar.value()
|
||||
|
||||
scroller.eventFilter(area.viewport(), _wheel(120))
|
||||
|
||||
assert scroller._animation.startValue() == position
|
||||
assert scroller._animation.endValue() == position - motion.SCROLL_STEP
|
||||
scroller._animation.setCurrentTime(60)
|
||||
assert bar.value() < position
|
||||
area.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("interaction", ["set_value", "keyboard", "slider_press", "range_change"])
|
||||
def test_direct_scroll_interaction_cancels_pending_wheel(
|
||||
application: QApplication, interaction: str
|
||||
) -> None:
|
||||
area = _scroll_area(application)
|
||||
motion.install_smooth_scroll(area)
|
||||
scroller = area._doctor_smooth_scroll
|
||||
bar = area.verticalScrollBar()
|
||||
scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
scroller._animation.setCurrentTime(30)
|
||||
|
||||
if interaction == "set_value":
|
||||
bar.setValue(500)
|
||||
elif interaction == "keyboard":
|
||||
application.sendEvent(
|
||||
area,
|
||||
QKeyEvent(QEvent.Type.KeyPress, Qt.Key.Key_Down, Qt.KeyboardModifier.NoModifier),
|
||||
)
|
||||
elif interaction == "slider_press":
|
||||
bar.setSliderDown(True)
|
||||
else:
|
||||
bar.setRange(0, 50)
|
||||
|
||||
assert scroller._animation.state() == QAbstractAnimation.State.Stopped
|
||||
assert scroller._target == bar.value()
|
||||
if interaction == "set_value":
|
||||
scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
assert scroller._animation.endValue() == 500 + motion.SCROLL_STEP
|
||||
area.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"wheel_options",
|
||||
[
|
||||
{"pixels": -8},
|
||||
{"phase": Qt.ScrollPhase.ScrollUpdate},
|
||||
{"phase": Qt.ScrollPhase.ScrollMomentum},
|
||||
{"modifiers": Qt.KeyboardModifier.ControlModifier},
|
||||
{"modifiers": Qt.KeyboardModifier.ShiftModifier},
|
||||
{"horizontal": -120},
|
||||
],
|
||||
)
|
||||
def test_native_gestures_cancel_pending_wheel(
|
||||
application: QApplication, wheel_options: dict
|
||||
) -> None:
|
||||
area = _scroll_area(application)
|
||||
motion.install_smooth_scroll(area)
|
||||
scroller = area._doctor_smooth_scroll
|
||||
scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
scroller._animation.setCurrentTime(30)
|
||||
delta = 0 if "horizontal" in wheel_options else -120
|
||||
|
||||
assert not scroller.eventFilter(area.viewport(), _wheel(delta, **wheel_options))
|
||||
assert scroller._animation.state() == QAbstractAnimation.State.Stopped
|
||||
assert scroller._target == area.verticalScrollBar().value()
|
||||
area.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["items", "text_blocks"])
|
||||
def test_non_pixel_scrollbars_keep_native_wheel_steps(application: QApplication, kind: str) -> None:
|
||||
if kind == "items":
|
||||
area = QListWidget()
|
||||
area.addItems([f"row {index}" for index in range(300)])
|
||||
area.setVerticalScrollMode(QAbstractItemView.ScrollMode.ScrollPerItem)
|
||||
else:
|
||||
area = QPlainTextEdit("\n".join(f"line {index}" for index in range(300)))
|
||||
area.resize(300, 200)
|
||||
area.show()
|
||||
application.processEvents()
|
||||
motion.install_smooth_scroll(area)
|
||||
|
||||
assert not area._doctor_smooth_scroll.eventFilter(area.viewport(), _wheel(-120))
|
||||
application.sendEvent(area.viewport(), _wheel(-120))
|
||||
assert 0 < area.verticalScrollBar().value() < motion.SCROLL_STEP
|
||||
area.close()
|
||||
|
||||
|
||||
def test_pending_scroll_reaches_edge_before_handing_wheel_back(application: QApplication) -> None:
|
||||
area = _scroll_area(application)
|
||||
motion.install_smooth_scroll(area)
|
||||
scroller = area._doctor_smooth_scroll
|
||||
bar = area.verticalScrollBar()
|
||||
bar.setValue(bar.maximum() - 20)
|
||||
|
||||
assert scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
assert scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
scroller._animation.setCurrentTime(scroller._animation.duration())
|
||||
assert not scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
area.close()
|
||||
|
||||
|
||||
def test_horizontal_scroller_honors_horizontal_and_shift_wheel(application: QApplication) -> None:
|
||||
area = QScrollArea()
|
||||
content = QWidget()
|
||||
content.resize(2000, 1000)
|
||||
area.setWidget(content)
|
||||
area.resize(300, 200)
|
||||
motion.install_smooth_scroll(area, orientation=Qt.Orientation.Horizontal)
|
||||
area.show()
|
||||
application.processEvents()
|
||||
scroller = area._doctor_smooth_scroll
|
||||
|
||||
assert not scroller.eventFilter(area.viewport(), _wheel(-120))
|
||||
assert scroller.eventFilter(area.viewport(), _wheel(0, horizontal=-120))
|
||||
scroller._animation.setCurrentTime(scroller._animation.duration())
|
||||
assert area.horizontalScrollBar().value() == motion.SCROLL_STEP
|
||||
assert area.verticalScrollBar().value() == 0
|
||||
assert scroller.eventFilter(
|
||||
area.viewport(), _wheel(-120, modifiers=Qt.KeyboardModifier.ShiftModifier)
|
||||
)
|
||||
assert scroller._animation.endValue() == motion.SCROLL_STEP * 2
|
||||
area.close()
|
||||
|
||||
@@ -16,6 +16,7 @@ from PySide6.QtWidgets import QAbstractItemView, QApplication, QPushButton
|
||||
from doctor_workstation.core import PermissionSet
|
||||
from doctor_workstation.services import DemoDoctorRepository
|
||||
from doctor_workstation.ui import icons
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.pages import prescriptions as prescriptions_module
|
||||
from doctor_workstation.ui.shell import ShellWindow
|
||||
from doctor_workstation.ui.theme import apply_theme
|
||||
@@ -222,7 +223,7 @@ def test_sorted_native_row_view_button_targets_visible_record(
|
||||
|
||||
|
||||
def test_blue_icon_disabled_variant_does_not_modify_shared_icon_cache(application) -> None:
|
||||
kind, color, size = "pencil", "#1769E8", 19
|
||||
kind, color, size = "pencil", PALETTE["accent"], 19
|
||||
shared = icons.icon(kind, color, size)
|
||||
original_key = shared.cacheKey()
|
||||
original_normal = shared.pixmap(size, size, QIcon.Mode.Normal).toImage()
|
||||
@@ -230,13 +231,19 @@ def test_blue_icon_disabled_variant_does_not_modify_shared_icon_cache(applicatio
|
||||
|
||||
blue = prescriptions_module._blue_prescription_icon(kind, color, size)
|
||||
|
||||
assert blue is not shared
|
||||
assert icons.icon(kind, color, size) is shared
|
||||
assert shared.cacheKey() == original_key
|
||||
assert shared.pixmap(size, size, QIcon.Mode.Normal).toImage() == original_normal
|
||||
assert shared.pixmap(size, size, QIcon.Mode.Disabled).toImage() == original_disabled
|
||||
assert blue.pixmap(size, size, QIcon.Mode.Normal).toImage() == original_normal
|
||||
assert blue.pixmap(size, size, QIcon.Mode.Disabled).toImage() == icons.pixmap(kind, "#A4ADBA", size).toImage()
|
||||
assert blue.pixmap(size, size, QIcon.Mode.Disabled).toImage() != original_disabled
|
||||
assert blue.pixmap(size, size, QIcon.Mode.Disabled).toImage() == icons.pixmap(kind, PALETTE["disabled"], size).toImage()
|
||||
assert blue.pixmap(size, size, QIcon.Mode.Disabled).toImage() == original_disabled
|
||||
# Both surfaces share the disabled colour; changing the returned icon still
|
||||
# must not mutate the cached icon used by another page.
|
||||
blue.addPixmap(icons.pixmap(kind, PALETTE["danger"], size), QIcon.Mode.Disabled)
|
||||
assert shared.cacheKey() == original_key
|
||||
assert shared.pixmap(size, size, QIcon.Mode.Disabled).toImage() == original_disabled
|
||||
|
||||
|
||||
def test_long_doctor_button_paints_compactly_without_truncating_real_value(shell_factory, application) -> None:
|
||||
|
||||
@@ -6,6 +6,7 @@ from PySide6.QtGui import QPalette
|
||||
from PySide6.QtWidgets import QApplication, QFrame, QLabel, QPushButton
|
||||
|
||||
from doctor_workstation.core import PermissionSet
|
||||
from doctor_workstation.ui.design_system import PALETTE
|
||||
from doctor_workstation.ui.pages import reception as reception_module
|
||||
from doctor_workstation.ui.pages.reception import ReceptionPage
|
||||
from doctor_workstation.ui.theme import apply_theme
|
||||
@@ -43,12 +44,12 @@ def page(application, monkeypatch):
|
||||
def test_primary_action_and_reading_ink_use_separate_colors(page, application):
|
||||
page.video_button.setEnabled(True)
|
||||
settle(application)
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.Button).name() == "#1769e8"
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.ButtonText).name() == "#ffffff"
|
||||
assert page.case_labels["present"].palette().color(QPalette.ColorRole.WindowText).name() == "#273244"
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.Button).name() == PALETTE["accent"].lower()
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.ButtonText).name() == PALETTE["surface"].lower()
|
||||
assert page.case_labels["present"].palette().color(QPalette.ColorRole.WindowText).name() == PALETTE["text"].lower()
|
||||
page.video_button.setEnabled(False)
|
||||
settle(application)
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.ButtonText).name() == "#8e8f90"
|
||||
assert page.video_button.palette().color(QPalette.ColorRole.ButtonText).name() == PALETTE["disabled"].lower()
|
||||
|
||||
|
||||
def test_typography_preserves_regular_body_and_distinct_values(page):
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtCore import QRect, QSize, Qt, QUrl
|
||||
from PySide6.QtWidgets import QApplication, QMainWindow
|
||||
|
||||
from doctor_workstation.app import ApplicationController
|
||||
from doctor_workstation.ui import theme
|
||||
from doctor_workstation.video import window as video_window
|
||||
from doctor_workstation.video.screenshot import ScreenshotPreviewSize
|
||||
from doctor_workstation.video.security import TrustedDocumentPolicy
|
||||
|
||||
TRUSTED_URL = "https://video.example.test/companion/index.html?mode=call"
|
||||
|
||||
|
||||
class _Window:
|
||||
"""Supply geometry and state only, so preview sizing cannot alter other settings."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
available: QRect | None = None,
|
||||
geometry: QRect | None = None,
|
||||
) -> None:
|
||||
self.current_geometry = QRect(geometry) if geometry is not None else QRect(1362, 18, 540, 356)
|
||||
self.available = QRect(available) if available is not None else QRect(0, 0, 1920, 1040)
|
||||
self.compact_flag: bool | None = True
|
||||
self.maximized = False
|
||||
self.full_screen = False
|
||||
self.geometry_changes: list[QRect] = []
|
||||
|
||||
def screen(self) -> SimpleNamespace:
|
||||
return SimpleNamespace(availableGeometry=lambda: QRect(self.available))
|
||||
|
||||
def geometry(self) -> QRect:
|
||||
return QRect(self.current_geometry)
|
||||
|
||||
def setGeometry(self, geometry: QRect) -> None: # noqa: N802 - Qt-compatible double
|
||||
self.current_geometry = QRect(geometry)
|
||||
self.geometry_changes.append(QRect(geometry))
|
||||
|
||||
def property(self, name: str) -> bool | None:
|
||||
assert name == "doctorSkipOpenGeometry"
|
||||
return self.compact_flag
|
||||
|
||||
def isMaximized(self) -> bool: # noqa: N802 - Qt-compatible double
|
||||
return self.maximized
|
||||
|
||||
def isFullScreen(self) -> bool: # noqa: N802 - Qt-compatible double
|
||||
return self.full_screen
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def qapp() -> QApplication:
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("available", "target", "expected_size"),
|
||||
[
|
||||
(QRect(0, 0, 1920, 1040), None, QSize(1120, 760)),
|
||||
(QRect(-1920, -120, 1920, 1040), None, QSize(1120, 760)),
|
||||
(QRect(-1280, 180, 1280, 720), QRect(0, 0, 1400, 950), QSize(1232, 672)),
|
||||
(QRect(100, 50, 800, 600), None, QSize(752, 552)),
|
||||
(QRect(0, 0, 1920, 1040), QRect(40, 60, 1360, 850), QSize(1360, 850)),
|
||||
],
|
||||
ids=["default", "negative-monitor", "bounded-target", "small-screen", "normal-target"],
|
||||
)
|
||||
def test_preview_expands_within_current_screen_and_restores(
|
||||
available: QRect, target: QRect | None, expected_size: QSize
|
||||
) -> None:
|
||||
window = _Window(available, QRect(available.x() + 30, available.y() + 30, 440, 300))
|
||||
original = window.geometry()
|
||||
preview = ScreenshotPreviewSize()
|
||||
|
||||
preview.open(window, target)
|
||||
|
||||
expanded = window.geometry()
|
||||
assert expanded.size() == expected_size
|
||||
assert available.adjusted(24, 24, -24, -24).contains(expanded)
|
||||
assert abs(expanded.center().x() - available.center().x()) <= 1
|
||||
assert abs(expanded.center().y() - available.center().y()) <= 1
|
||||
assert window.compact_flag is True
|
||||
|
||||
preview.restore(window)
|
||||
|
||||
assert window.geometry() == original
|
||||
assert window.compact_flag is True
|
||||
|
||||
|
||||
def test_repeated_open_preserves_first_geometry_and_restore_is_idempotent() -> None:
|
||||
window = _Window()
|
||||
original = window.geometry()
|
||||
preview = ScreenshotPreviewSize()
|
||||
|
||||
preview.open(window)
|
||||
expanded = window.geometry()
|
||||
preview.open(window, QRect(0, 0, 1500, 900))
|
||||
|
||||
assert window.geometry() == expanded
|
||||
assert len(window.geometry_changes) == 1
|
||||
|
||||
preview.restore(window)
|
||||
preview.restore(window)
|
||||
|
||||
assert window.geometry() == original
|
||||
assert window.geometry_changes == [expanded, original]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("state", ["maximized", "full_screen", "large", "no-screen", "tiny-screen"])
|
||||
def test_preview_does_not_modify_windows_that_do_not_need_expansion(state: str) -> None:
|
||||
window = _Window()
|
||||
if state == "large":
|
||||
window.current_geometry = QRect(25, 30, 1300, 850)
|
||||
elif state == "no-screen":
|
||||
window.screen = lambda: None # type: ignore[method-assign, return-value]
|
||||
elif state == "tiny-screen":
|
||||
window.available = QRect(0, 0, 40, 40)
|
||||
else:
|
||||
setattr(window, state, True)
|
||||
original = window.geometry()
|
||||
preview = ScreenshotPreviewSize()
|
||||
|
||||
preview.open(window)
|
||||
preview.restore(window)
|
||||
|
||||
assert window.geometry() == original
|
||||
assert window.geometry_changes == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["move", "resize", "compact-flag", "maximize", "fullscreen"])
|
||||
def test_restore_preserves_external_window_changes_and_forgets_old_snapshot(change: str) -> None:
|
||||
window = _Window()
|
||||
preview = ScreenshotPreviewSize()
|
||||
preview.open(window)
|
||||
expanded = window.geometry()
|
||||
if change == "move":
|
||||
window.current_geometry.translate(40, 15)
|
||||
elif change == "resize":
|
||||
window.current_geometry.setWidth(window.current_geometry.width() + 50)
|
||||
elif change == "compact-flag":
|
||||
window.compact_flag = False
|
||||
elif change == "maximize":
|
||||
window.maximized = True
|
||||
else:
|
||||
window.full_screen = True
|
||||
external_geometry = window.geometry()
|
||||
writes_before_restore = len(window.geometry_changes)
|
||||
|
||||
preview.restore(window)
|
||||
|
||||
assert window.geometry() == external_geometry
|
||||
assert len(window.geometry_changes) == writes_before_restore
|
||||
|
||||
# Even when those external changes later disappear, a late close is harmless.
|
||||
window.current_geometry = expanded
|
||||
window.compact_flag = True
|
||||
window.maximized = False
|
||||
window.full_screen = False
|
||||
preview.restore(window)
|
||||
|
||||
assert window.geometry() == expanded
|
||||
assert len(window.geometry_changes) == writes_before_restore
|
||||
|
||||
|
||||
def test_real_window_preserves_theme_opt_out_minimum_and_flags(qapp: QApplication) -> None:
|
||||
theme._install_business_dialog_styling(qapp)
|
||||
main = QMainWindow()
|
||||
main.setProperty("doctorMainWindow", True)
|
||||
main.setGeometry(30, 30, 980, 720)
|
||||
main.show()
|
||||
window = QMainWindow()
|
||||
window.setProperty("doctorSkipOpenGeometry", True)
|
||||
window.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)
|
||||
window.setMinimumSize(440, 300)
|
||||
window.setGeometry(30, 30, 440, 300)
|
||||
window.show()
|
||||
qapp.processEvents()
|
||||
original = window.geometry()
|
||||
flags = window.windowFlags()
|
||||
minimum = window.minimumSize()
|
||||
preview = ScreenshotPreviewSize()
|
||||
try:
|
||||
preview.open(window, theme.main_window_target_rect(window))
|
||||
qapp.processEvents()
|
||||
|
||||
assert window.width() > original.width()
|
||||
assert window.height() > original.height()
|
||||
assert window.minimumSize() == minimum
|
||||
assert window.windowFlags() == flags
|
||||
assert window.property("doctorSkipOpenGeometry") is True
|
||||
|
||||
preview.restore(window)
|
||||
qapp.processEvents()
|
||||
|
||||
assert window.geometry() == original
|
||||
assert window.minimumSize() == minimum
|
||||
assert window.windowFlags() == flags
|
||||
assert window.property("doctorSkipOpenGeometry") is True
|
||||
finally:
|
||||
window.close()
|
||||
main.close()
|
||||
|
||||
|
||||
def test_diagnosis_restore_clears_screenshot_layer_before_restoring_large_window(
|
||||
qapp: QApplication,
|
||||
) -> None:
|
||||
theme._install_business_dialog_styling(qapp)
|
||||
window = QMainWindow()
|
||||
window.setProperty("doctorSkipOpenGeometry", True)
|
||||
window.setMinimumSize(440, 300)
|
||||
window.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)
|
||||
window.setGeometry(30, 30, 440, 300)
|
||||
window.show()
|
||||
qapp.processEvents()
|
||||
compact = window.geometry()
|
||||
normal = QRect(40, 50, 670, 500)
|
||||
preview = ScreenshotPreviewSize()
|
||||
restored_layers: list[QRect] = []
|
||||
|
||||
def restore_screenshot_preview() -> None:
|
||||
preview.restore(window)
|
||||
restored_layers.append(window.geometry())
|
||||
|
||||
window.restore_screenshot_preview = restore_screenshot_preview
|
||||
controller = SimpleNamespace(
|
||||
_video_preview_generation=4,
|
||||
_video_preview_state={
|
||||
"window": window,
|
||||
"geometry": normal,
|
||||
"minimum_size": QSize(500, 360),
|
||||
"maximized": False,
|
||||
"full_screen": False,
|
||||
"stays_on_top": False,
|
||||
},
|
||||
)
|
||||
try:
|
||||
preview.open(window, QRect(0, 0, 600, 450))
|
||||
|
||||
ApplicationController._restore_video_preview(controller, 4, activate=False)
|
||||
qapp.processEvents()
|
||||
|
||||
assert restored_layers == [compact]
|
||||
assert window.geometry() == normal
|
||||
assert window.minimumSize() == QSize(500, 360)
|
||||
assert not window.windowFlags() & Qt.WindowType.WindowStaysOnTopHint
|
||||
assert window.property("doctorSkipOpenGeometry") is False
|
||||
assert controller._video_preview_state is None
|
||||
assert controller._video_preview_generation == 5
|
||||
|
||||
restore_screenshot_preview()
|
||||
|
||||
assert window.geometry() == normal
|
||||
finally:
|
||||
window.close()
|
||||
|
||||
|
||||
def _host(**changes: Any) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
**{
|
||||
"_closing": False,
|
||||
"_released": False,
|
||||
"_shutdown_requested": False,
|
||||
"_media_active": True,
|
||||
"_policy": TrustedDocumentPolicy.from_url(TRUSTED_URL, is_local=False),
|
||||
"_page": SimpleNamespace(url=lambda: QUrl(TRUSTED_URL)),
|
||||
"_screenshot_preview_size": SimpleNamespace(open=Mock()),
|
||||
"restore_screenshot_preview": Mock(),
|
||||
**changes,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"changes",
|
||||
[
|
||||
{"_closing": True},
|
||||
{"_released": True},
|
||||
{"_shutdown_requested": True},
|
||||
{"_media_active": False},
|
||||
{"_page": SimpleNamespace(url=lambda: QUrl("about:blank"))},
|
||||
{"_page": SimpleNamespace(url=lambda: QUrl("https://untrusted.example.test/"))},
|
||||
],
|
||||
ids=["closing", "released", "shutdown", "inactive", "blank-document", "untrusted-document"],
|
||||
)
|
||||
def test_host_does_not_open_preview_when_inactive_or_untrusted(changes: dict[str, Any]) -> None:
|
||||
host = _host(**changes)
|
||||
|
||||
video_window._EmbeddedVideoWindow._set_screenshot_preview_visible(host, True)
|
||||
|
||||
host._screenshot_preview_size.open.assert_not_called()
|
||||
host.restore_screenshot_preview.assert_not_called()
|
||||
|
||||
|
||||
def test_host_uses_normal_window_target_for_trusted_preview(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
host = _host()
|
||||
target = QRect(30, 40, 1360, 850)
|
||||
target_for_window = Mock(return_value=target)
|
||||
monkeypatch.setattr(theme, "main_window_target_rect", target_for_window)
|
||||
|
||||
video_window._EmbeddedVideoWindow._set_screenshot_preview_visible(host, True)
|
||||
|
||||
target_for_window.assert_called_once_with(host)
|
||||
host._screenshot_preview_size.open.assert_called_once_with(host, target)
|
||||
host.restore_screenshot_preview.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [{}, {"_shutdown_requested": True}, {"_media_active": False}])
|
||||
def test_host_allows_preview_cleanup_after_shutdown_request(changes: dict[str, Any]) -> None:
|
||||
host = _host(**changes)
|
||||
|
||||
video_window._EmbeddedVideoWindow._set_screenshot_preview_visible(host, False)
|
||||
|
||||
host.restore_screenshot_preview.assert_called_once_with()
|
||||
host._screenshot_preview_size.open.assert_not_called()
|
||||
@@ -164,6 +164,59 @@ def test_diagnosis_callback_previews_exact_appointment_session(chat_controller):
|
||||
assert len(case.previews) == 1 # A closed session cannot borrow another appointment's video.
|
||||
|
||||
|
||||
@pytest.mark.parametrize("permissions, expected", [
|
||||
(None, False),
|
||||
([], False),
|
||||
(["tcm.diagnosis/read"], False),
|
||||
(["tcm.diagnosis.aiAssistant"], False),
|
||||
(["tcm.diagnosis/aiAssistant"], True),
|
||||
(["tcm.diagnosis/*"], True),
|
||||
(["*"], True),
|
||||
])
|
||||
def test_im_ai_capability_comes_from_current_shell_permissions(chat_controller, permissions, expected):
|
||||
case = chat_controller
|
||||
case.controller.shell_window.permissions = permissions
|
||||
case.controller._request_video({
|
||||
"patient_id": 8, "diagnosis_id": 123, "appointment_id": 456,
|
||||
"mode": "im", "can_ai_assistant": True,
|
||||
})
|
||||
case.complete(0)
|
||||
assert case.launched[0][1]["can_ai_assistant"] is expected
|
||||
|
||||
|
||||
def test_im_ai_permission_is_evaluated_after_ticket_load(chat_controller):
|
||||
case = chat_controller
|
||||
case.controller.shell_window.permissions = ["tcm.diagnosis/aiAssistant"]
|
||||
case.request(456)
|
||||
case.controller.shell_window.permissions = []
|
||||
case.complete(0)
|
||||
assert case.launched[0][1]["can_ai_assistant"] is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("capability, expected", [(True, True), (False, False), (None, False), (1, False), ("true", False)])
|
||||
def test_video_launcher_forwards_only_explicit_ai_capability(monkeypatch, capability, expected):
|
||||
from doctor_workstation.video import window as window_module
|
||||
from doctor_workstation.video.launcher import launch_video_call
|
||||
|
||||
launches = []
|
||||
sentinel = object()
|
||||
|
||||
def capture(request, **kwargs):
|
||||
launches.append((request, kwargs))
|
||||
return sentinel
|
||||
|
||||
monkeypatch.setattr(window_module, "open_video_call", capture)
|
||||
result = launch_video_call(
|
||||
ticket(), repository=object(), open_im=True, can_ai_assistant=capability,
|
||||
)
|
||||
assert result is sentinel
|
||||
request, options = launches[0]
|
||||
assert request.diagnosis_id == 123
|
||||
assert request.patient_id == 8
|
||||
assert request.appointment_id == 456
|
||||
assert options["can_ai_assistant"] is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("second_diagnosis", [123, 124])
|
||||
@pytest.mark.parametrize("completion_order", [(0, 1), (1, 0)])
|
||||
def test_latest_im_selection_retires_pending_callbacks(chat_controller, second_diagnosis, completion_order):
|
||||
|
||||
@@ -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
|
||||
@@ -295,6 +295,48 @@ def test_real_demo_login_reaches_success_without_widget_adapter(
|
||||
application.processEvents()
|
||||
|
||||
|
||||
def test_login_password_group_keeps_focus_when_reveal_is_keyboard_focused(tmp_path: Any) -> None:
|
||||
application = QApplication.instance() or QApplication([])
|
||||
apply_theme(application)
|
||||
window = LoginWindow(
|
||||
object(), settings=QSettings(str(tmp_path / "focus.ini"), QSettings.Format.IniFormat),
|
||||
)
|
||||
window.show()
|
||||
application.processEvents()
|
||||
group = window.password_edit.parentWidget()
|
||||
geometry = group.geometry()
|
||||
try:
|
||||
for control in (window.password_edit, window.reveal_button):
|
||||
control.setFocus()
|
||||
application.processEvents()
|
||||
assert group.property("focused") is True
|
||||
assert group.geometry() == geometry
|
||||
window.login_button.setFocus()
|
||||
application.processEvents()
|
||||
assert group.property("focused") is False
|
||||
assert group.geometry() == geometry
|
||||
finally:
|
||||
window.close()
|
||||
|
||||
|
||||
def test_compact_login_keeps_the_whole_form_visible(tmp_path: Any) -> None:
|
||||
application = QApplication.instance() or QApplication([])
|
||||
apply_theme(application)
|
||||
window = LoginWindow(
|
||||
object(), settings=QSettings(str(tmp_path / "compact.ini"), QSettings.Format.IniFormat),
|
||||
)
|
||||
window.resize(860, 640)
|
||||
window.show()
|
||||
application.processEvents()
|
||||
try:
|
||||
assert not window.brand_panel.isVisible()
|
||||
assert window.login_scroll.verticalScrollBar().maximum() == 0
|
||||
assert window.card.height() >= window.card.minimumSizeHint().height()
|
||||
assert window.login_button.isVisibleTo(window)
|
||||
finally:
|
||||
window.close()
|
||||
|
||||
|
||||
def test_remembered_password_survives_a_new_login_window(tmp_path: Any) -> None:
|
||||
application = QApplication.instance() or QApplication([])
|
||||
settings_path = tmp_path / "remember-password.ini"
|
||||
|
||||
@@ -38,6 +38,8 @@ def page(application, monkeypatch):
|
||||
|
||||
|
||||
def test_reception_prompts_remain_readable_in_a_narrow_workspace(page, application):
|
||||
# The responsive workspace exposes insights in its second tab.
|
||||
page.workbench.mode_tabs.setCurrentIndex(1)
|
||||
settle(application)
|
||||
for button in page.ai_prompt_buttons:
|
||||
assert button.width() >= button.minimumSizeHint().width()
|
||||
@@ -48,6 +50,8 @@ def test_reception_prompts_remain_readable_in_a_narrow_workspace(page, applicati
|
||||
|
||||
|
||||
def test_hidden_tall_consultation_does_not_expand_daily_records(page, application):
|
||||
# Force real overflow: a short case is allowed to fill the viewport.
|
||||
page.case_labels["present"].setText("Long clinical history\n" * 80)
|
||||
page.detail_tabs.setCurrentIndex(0)
|
||||
settle(application)
|
||||
consultation_height = page.clinical_pages.height()
|
||||
|
||||
@@ -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
|
||||
@@ -269,7 +277,7 @@ def test_video_diagnosis_entry_uses_existing_permission_scoped_drawer() -> None:
|
||||
assert "not modeless and not self._standalone_readonly" in diagnosis_source
|
||||
compact_styles = styles.split("@media (max-width: 700px)", 1)[1]
|
||||
assert ".consultation-rail { display: none; }" in compact_styles
|
||||
assert ".capture-button { display: none; }" in compact_styles
|
||||
assert ".capture-button { display: none; }" not in compact_styles
|
||||
assert "on_open_diagnosis=on_open_diagnosis" in launcher_source
|
||||
assert "event: 'open-diagnosis-request'" in main_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:
|
||||
@@ -573,7 +581,8 @@ def test_companion_screenshot_requires_doctor_confirmation_before_upload() -> No
|
||||
"watch(", 1
|
||||
)[0]
|
||||
|
||||
assert "screenshotPreview.value = canvas.toDataURL" in capture
|
||||
assert "await captureVideoStage(videoStage.value)" in capture
|
||||
assert "generation === screenshotGeneration" in capture
|
||||
assert "onSaveScreenshot" not in capture
|
||||
assert "await props.onSaveScreenshot(screenshotPreview.value)" in confirm
|
||||
assert "确认画面后再保存到患者资料" in source
|
||||
|
||||
@@ -0,0 +1,379 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from collections.abc import Mapping
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import QRect, QUrl
|
||||
from PySide6.QtGui import QColor, QImage, QPainter
|
||||
|
||||
from doctor_workstation.video import window
|
||||
from doctor_workstation.video.screenshot import capture_view_region
|
||||
from doctor_workstation.video.security import TrustedDocumentPolicy
|
||||
|
||||
TRUSTED_URL = "https://video.example.test/companion/index.html?mode=call"
|
||||
REGION_KEYS = ("x", "y", "width", "height", "viewportWidth", "viewportHeight")
|
||||
|
||||
|
||||
def _region(**changes: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"x": 120,
|
||||
"y": 80,
|
||||
"width": 480,
|
||||
"height": 320,
|
||||
"viewportWidth": 1000,
|
||||
"viewportHeight": 800,
|
||||
**changes,
|
||||
}
|
||||
|
||||
|
||||
class _View:
|
||||
"""Keep grab coordinates logical, as QWidget does, and its image physical."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
width: int = 1000,
|
||||
height: int = 800,
|
||||
*,
|
||||
zoom: float = 1,
|
||||
dpr: float = 1,
|
||||
source: QImage | None = None,
|
||||
) -> None:
|
||||
self._width = width
|
||||
self._height = height
|
||||
self._zoom = zoom
|
||||
self.dpr = dpr
|
||||
self.source = source
|
||||
self.grabbed: list[QRect] = []
|
||||
|
||||
def width(self) -> int:
|
||||
return self._width
|
||||
|
||||
def height(self) -> int:
|
||||
return self._height
|
||||
|
||||
def zoomFactor(self) -> float: # noqa: N802 - Qt API
|
||||
return self._zoom
|
||||
|
||||
def rect(self) -> QRect:
|
||||
return QRect(0, 0, self._width, self._height)
|
||||
|
||||
def grab(self, rect: QRect) -> SimpleNamespace:
|
||||
self.grabbed.append(QRect(rect))
|
||||
physical_rect = QRect(
|
||||
round(rect.x() * self.dpr),
|
||||
round(rect.y() * self.dpr),
|
||||
round(rect.width() * self.dpr),
|
||||
round(rect.height() * self.dpr),
|
||||
)
|
||||
if self.source is not None:
|
||||
image = self.source.copy(physical_rect)
|
||||
else:
|
||||
image = QImage(physical_rect.size(), QImage.Format.Format_RGB32)
|
||||
image.fill(QColor("#143b67"))
|
||||
image.setDevicePixelRatio(self.dpr)
|
||||
return SimpleNamespace(toImage=lambda: image)
|
||||
|
||||
|
||||
def _decode_jpeg(data_url: str) -> QImage:
|
||||
prefix = "data:image/jpeg;base64,"
|
||||
assert data_url.startswith(prefix)
|
||||
data = base64.b64decode(data_url[len(prefix) :], validate=True)
|
||||
assert data.startswith(b"\xff\xd8")
|
||||
image = QImage.fromData(data, "JPEG")
|
||||
assert not image.isNull()
|
||||
return image
|
||||
|
||||
|
||||
@pytest.mark.parametrize("zoom", [0.8, 1, 1.25, 1.5, 2])
|
||||
def test_css_coordinates_follow_webengine_zoom(zoom: float) -> None:
|
||||
view = _View(round(1000 * zoom), round(800 * zoom), zoom=zoom)
|
||||
|
||||
image = _decode_jpeg(capture_view_region(view, _region()))
|
||||
|
||||
expected = QRect(round(120 * zoom), round(80 * zoom), round(480 * zoom), round(320 * zoom))
|
||||
assert view.grabbed == [expected]
|
||||
assert image.size() == expected.size()
|
||||
|
||||
|
||||
def test_fractional_edges_round_inward_and_exclude_adjacent_patient_panel() -> None:
|
||||
source = QImage(1250, 1000, QImage.Format.Format_RGB32)
|
||||
source.fill(QColor("red"))
|
||||
painter = QPainter(source)
|
||||
# Only complete logical pixels inside the fractional video rectangle are safe.
|
||||
expected = QRect(17, 26, 374, 249)
|
||||
painter.fillRect(expected, QColor("#143b67"))
|
||||
painter.end()
|
||||
view = _View(1250, 1000, zoom=1.25, source=source)
|
||||
|
||||
image = _decode_jpeg(
|
||||
capture_view_region(view, _region(x=13.1, y=20.2, width=300.4, height=200.4))
|
||||
)
|
||||
|
||||
assert view.grabbed == [expected]
|
||||
for x, y in ((0, 0), (373, 0), (0, 248), (373, 248), (187, 124)):
|
||||
color = image.pixelColor(x, y)
|
||||
assert color.blue() > color.red() + 50
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("changes", "expected"),
|
||||
[
|
||||
({"x": -20, "y": -10, "width": 150, "height": 110}, QRect(0, 0, 130, 100)),
|
||||
({"x": 950, "y": 760, "width": 100, "height": 80}, QRect(950, 760, 50, 40)),
|
||||
({"x": -50, "y": -50, "width": 1200, "height": 1000}, QRect(0, 0, 1000, 800)),
|
||||
],
|
||||
)
|
||||
def test_capture_is_clipped_to_the_visible_viewport(
|
||||
changes: dict[str, int], expected: QRect
|
||||
) -> None:
|
||||
view = _View()
|
||||
|
||||
image = _decode_jpeg(capture_view_region(view, _region(**changes)))
|
||||
|
||||
assert view.grabbed == [expected]
|
||||
assert image.size() == expected.size()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", REGION_KEYS)
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
[float("nan"), float("inf"), float("-inf"), True, False, None, "12"],
|
||||
ids=["nan", "infinity", "negative-infinity", "true", "false", "null", "string"],
|
||||
)
|
||||
def test_invalid_coordinate_values_are_rejected_before_grab(key: str, value: Any) -> None:
|
||||
view = _View()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
capture_view_region(view, _region(**{key: value}))
|
||||
|
||||
assert view.grabbed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", REGION_KEYS)
|
||||
def test_missing_coordinates_are_rejected_before_grab(key: str) -> None:
|
||||
view = _View()
|
||||
region = _region()
|
||||
del region[key]
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
capture_view_region(view, region)
|
||||
|
||||
assert view.grabbed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["width", "height", "viewportWidth", "viewportHeight"])
|
||||
@pytest.mark.parametrize("value", [0, -1])
|
||||
def test_empty_or_negative_sizes_are_rejected_before_grab(key: str, value: int) -> None:
|
||||
view = _View()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
capture_view_region(view, _region(**{key: value}))
|
||||
|
||||
assert view.grabbed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"changes",
|
||||
[
|
||||
{"x": 1000},
|
||||
{"y": 800},
|
||||
{"x": -480},
|
||||
{"y": -320},
|
||||
{"x": 12.2, "width": 0.2},
|
||||
{"y": 12.2, "height": 0.2},
|
||||
],
|
||||
)
|
||||
def test_offscreen_or_subpixel_empty_regions_are_rejected(changes: dict[str, Any]) -> None:
|
||||
view = _View()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
capture_view_region(view, _region(**changes))
|
||||
|
||||
assert view.grabbed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("width,height,zoom", [(1200, 800, 1), (1000, 900, 1), (1000, 800, 1.25)])
|
||||
def test_resize_or_zoom_race_rejects_stale_viewport(width: int, height: int, zoom: float) -> None:
|
||||
view = _View(width, height, zoom=zoom)
|
||||
|
||||
with pytest.raises(ValueError, match="尺寸已变化"):
|
||||
capture_view_region(view, _region())
|
||||
|
||||
assert view.grabbed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dpr", [1, 1.25, 2])
|
||||
def test_dpr_changes_encoded_pixels_without_scaling_grab_coordinates(dpr: float) -> None:
|
||||
view = _View(1250, 1000, zoom=1.25, dpr=dpr)
|
||||
|
||||
image = _decode_jpeg(capture_view_region(view, _region()))
|
||||
|
||||
assert view.grabbed == [QRect(150, 100, 600, 400)]
|
||||
assert (image.width(), image.height()) == (round(600 * dpr), round(400 * dpr))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"width,height,dpr,expected",
|
||||
[
|
||||
(3000, 1500, 1, (1920, 960)),
|
||||
(1500, 3000, 1, (960, 1920)),
|
||||
(1600, 900, 2, (1920, 1080)),
|
||||
(320, 240, 1, (320, 240)),
|
||||
],
|
||||
)
|
||||
def test_jpeg_longest_edge_is_limited_to_1920_without_upscaling(
|
||||
width: int, height: int, dpr: float, expected: tuple[int, int]
|
||||
) -> None:
|
||||
view = _View(width, height, dpr=dpr)
|
||||
region = _region(
|
||||
x=0, y=0, width=width, height=height, viewportWidth=width, viewportHeight=height
|
||||
)
|
||||
|
||||
image = _decode_jpeg(capture_view_region(view, region))
|
||||
|
||||
assert (image.width(), image.height()) == expected
|
||||
assert view.grabbed == [QRect(0, 0, width, height)]
|
||||
|
||||
|
||||
def test_null_grab_and_jpeg_encoding_failure_are_reported() -> None:
|
||||
view = _View()
|
||||
view.grab = Mock(return_value=SimpleNamespace(toImage=lambda: QImage()))
|
||||
with pytest.raises(ValueError, match="截屏失败"):
|
||||
capture_view_region(view, _region())
|
||||
|
||||
image = SimpleNamespace(
|
||||
isNull=lambda: False, width=lambda: 480, height=lambda: 320, save=lambda *_: False
|
||||
)
|
||||
view.grab = Mock(return_value=SimpleNamespace(toImage=lambda: image))
|
||||
with pytest.raises(ValueError, match="图片生成失败"):
|
||||
capture_view_region(view, _region())
|
||||
|
||||
|
||||
def _host(view: _View, **changes: Any) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
**{
|
||||
"_closing": False,
|
||||
"_released": False,
|
||||
"_shutdown_requested": False,
|
||||
"_media_active": True,
|
||||
"_policy": TrustedDocumentPolicy.from_url(TRUSTED_URL, is_local=False),
|
||||
"_page": SimpleNamespace(url=lambda: QUrl(TRUSTED_URL)),
|
||||
"web_view": view,
|
||||
"lifecycle": SimpleNamespace(save_screenshot=Mock()),
|
||||
**changes,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_bridge_returns_preview_without_saving_until_explicit_save() -> None:
|
||||
view = _View()
|
||||
host = _host(view)
|
||||
events: list[Mapping[str, Any]] = []
|
||||
bridge = window._QtVideoBridge(
|
||||
events.append,
|
||||
lambda region: window._EmbeddedVideoWindow._capture_screenshot_preview(host, region),
|
||||
)
|
||||
|
||||
result = json.loads(bridge.captureScreenshot(json.dumps(_region())))
|
||||
|
||||
assert set(result) == {"dataUrl"}
|
||||
assert _decode_jpeg(result["dataUrl"]).size() == QRect(0, 0, 480, 320).size()
|
||||
assert events == []
|
||||
host.lifecycle.save_screenshot.assert_not_called()
|
||||
bridge.saveScreenshot(result["dataUrl"])
|
||||
assert events == [
|
||||
{"source": "doctor-call", "event": "screenshot", "dataUrl": result["dataUrl"]}
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("payload", [None, 12, "", "{", "null", "[]", '"region"', " " * 2049])
|
||||
def test_bridge_rejects_invalid_payloads_without_calling_capture(payload: Any) -> None:
|
||||
capture = Mock()
|
||||
notify = Mock()
|
||||
bridge = window._QtVideoBridge(notify, capture)
|
||||
|
||||
result = json.loads(bridge.captureScreenshot(payload))
|
||||
|
||||
assert set(result) == {"error"}
|
||||
assert result["error"]
|
||||
capture.assert_not_called()
|
||||
notify.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
[float("nan"), float("inf"), True, 0, -1, 10**400],
|
||||
ids=["nan", "infinity", "boolean", "empty", "negative", "oversized-integer"],
|
||||
)
|
||||
def test_bridge_returns_error_for_invalid_region_values(value: Any) -> None:
|
||||
view = _View()
|
||||
notify = Mock()
|
||||
bridge = window._QtVideoBridge(notify, lambda region: capture_view_region(view, region))
|
||||
|
||||
result = json.loads(bridge.captureScreenshot(json.dumps(_region(width=value))))
|
||||
|
||||
assert set(result) == {"error"}
|
||||
assert result["error"]
|
||||
assert view.grabbed == []
|
||||
notify.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error_type", [ValueError, TypeError, RuntimeError])
|
||||
def test_bridge_reports_capture_failure_without_exposing_error_details(
|
||||
error_type: type[Exception],
|
||||
) -> None:
|
||||
notify = Mock()
|
||||
bridge = window._QtVideoBridge(
|
||||
notify, Mock(side_effect=error_type("private path or patient information"))
|
||||
)
|
||||
|
||||
result = json.loads(bridge.captureScreenshot(json.dumps(_region())))
|
||||
|
||||
assert set(result) == {"error"}
|
||||
assert "private" not in result["error"]
|
||||
notify.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"changes",
|
||||
[
|
||||
{"_closing": True},
|
||||
{"_released": True},
|
||||
{"_shutdown_requested": True},
|
||||
{"_media_active": False},
|
||||
],
|
||||
)
|
||||
def test_host_rejects_capture_when_call_is_inactive_or_closing(changes: dict[str, bool]) -> None:
|
||||
view = _View()
|
||||
host = _host(view, **changes)
|
||||
|
||||
with pytest.raises(ValueError, match="已关闭"):
|
||||
window._EmbeddedVideoWindow._capture_screenshot_preview(host, _region())
|
||||
|
||||
assert view.grabbed == []
|
||||
host.lifecycle.save_screenshot.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"url",
|
||||
[
|
||||
"about:blank",
|
||||
"https://untrusted.example.test/companion/index.html?mode=call",
|
||||
"https://video.example.test/other",
|
||||
"https://video.example.test/companion/index.html?mode=other",
|
||||
],
|
||||
)
|
||||
def test_host_rejects_capture_after_navigation_away_from_trusted_document(url: str) -> None:
|
||||
view = _View()
|
||||
host = _host(view, _page=SimpleNamespace(url=lambda: QUrl(url)))
|
||||
|
||||
with pytest.raises(ValueError, match="页面不可用"):
|
||||
window._EmbeddedVideoWindow._capture_screenshot_preview(host, _region())
|
||||
|
||||
assert view.grabbed == []
|
||||
host.lifecycle.save_screenshot.assert_not_called()
|
||||
Reference in New Issue
Block a user