192 lines
7.5 KiB
Python
192 lines
7.5 KiB
Python
"""End-to-end verification of Clinical OS 3.0 Spatial Shell & Digital Twin Cockpit."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from datetime import date, timedelta
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
import pytest
|
|
from PySide6.QtCore import QEvent
|
|
from PySide6.QtTest import QTest
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
from doctor_workstation.core.permissions import PermissionSet
|
|
from doctor_workstation.services.demo_repository import DEMO_PERMISSIONS, DemoDoctorRepository
|
|
from doctor_workstation.ui.shell_spatial import SpatialShellWindow
|
|
from doctor_workstation.ui.state import PatientContextStore
|
|
from doctor_workstation.ui.theme import apply_theme
|
|
from doctor_workstation.ui.viewmodels import derive_cockpit_snapshot
|
|
|
|
|
|
def _settle(app: QApplication, rounds: int = 14) -> None:
|
|
for _ in range(rounds):
|
|
app.processEvents()
|
|
QTest.qWait(8)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def application() -> QApplication:
|
|
app = QApplication.instance() or QApplication([])
|
|
apply_theme(app)
|
|
return app
|
|
|
|
|
|
def test_patient_context_store_broadcasts_snapshots_and_modes() -> None:
|
|
store = PatientContextStore()
|
|
seen_names: list[str] = []
|
|
sheet_states: list[bool] = []
|
|
store.active_patient_changed.connect(lambda snap: seen_names.append(snap.patient_name))
|
|
store.bottom_sheet_mode_changed.connect(sheet_states.append)
|
|
|
|
snap = store.update_from_reception(
|
|
{"id": 101, "patient_name": "林晓岚", "status": 1, "appointment_time": "09:30"},
|
|
{"id": 201, "gender": 2, "age": 46, "phone": "13800138000"},
|
|
{"id": 501, "clinical_diagnosis": "2型糖尿病", "disease_course_text": "8年"},
|
|
)
|
|
assert snap.has_patient
|
|
assert snap.patient_name == "林晓岚"
|
|
assert snap.appointment_id == 101
|
|
assert snap.diagnosis_id == 501
|
|
assert "46岁" in snap.subtitle_line
|
|
assert seen_names == ["林晓岚"]
|
|
|
|
assert store.toggle_bottom_sheet() is True
|
|
assert store.toggle_bottom_sheet() is False
|
|
assert sheet_states == [True, False]
|
|
|
|
store.clear()
|
|
assert not store.current_patient.has_patient
|
|
assert seen_names[-1] == "未选择患者"
|
|
|
|
|
|
def test_cockpit_viewmodel_derives_three_jiao_journey_and_aurora_cards() -> None:
|
|
vm = derive_cockpit_snapshot(
|
|
{"id": 11, "patient_name": "张建国", "status": 1},
|
|
{"height": 172, "weight": 78},
|
|
{
|
|
"id": 511,
|
|
"hba1c": 7.6,
|
|
"fasting_blood_sugar": 7.8,
|
|
"chief_complaint": "晨起口渴多饮,伴乏力夜尿频",
|
|
"tongue": "舌红少津",
|
|
"pulse": "脉弦细",
|
|
"diagnosis_date": (date.today() - timedelta(days=11)).isoformat(),
|
|
},
|
|
ai_payload={
|
|
"diagnosis_advice": "气阴两虚夹肝郁燥热,宜益气生津疏肝。",
|
|
"treatment_advice": "玉女煎合生脉散加减",
|
|
"risk_assessment": [{"label": "黎明血糖抬升", "level": "medium"}],
|
|
},
|
|
active_ai_model="qwen",
|
|
)
|
|
assert vm.patient_name == "张建国"
|
|
assert len(vm.progression.bars) == 12
|
|
assert vm.progression.bars[-1].highlight is True
|
|
assert len(vm.diurnal.slots) == 16
|
|
assert any(s.state == "alert" for s in vm.diurnal.slots)
|
|
assert len(vm.diurnal.vitals) == 3
|
|
|
|
sanjiao = vm.twin_for_mode("sanjiao")
|
|
assert sanjiao.upper_heat_intensity > 0.5
|
|
assert sanjiao.middle_heat_intensity > 0.5
|
|
assert sanjiao.lower_heat_intensity > 0.5
|
|
assert len(vm.twin_for_mode("organ").nodes) == 2
|
|
assert len(vm.twin_for_mode("tcm").nodes) == 2
|
|
|
|
assert vm.journey.total_days == 21
|
|
assert vm.journey.current_day == 12
|
|
assert sum(1 for d in vm.journey.days if d.state == "today") == 1
|
|
assert len(vm.aurora.cards) == 3
|
|
assert "气阴两虚" in vm.aurora.ai_summary_line
|
|
|
|
|
|
def test_spatial_shell_zero_sidebar_cockpit_and_cross_page_flow(
|
|
application: QApplication,
|
|
) -> None:
|
|
repo = DemoDoctorRepository()
|
|
session = repo.login(repo.DEMO_ACCOUNT, repo.DEMO_PASSWORD)
|
|
shell = SpatialShellWindow(
|
|
repo,
|
|
{"session": session, "user": session.user, "demo_mode": True},
|
|
PermissionSet(DEMO_PERMISSIONS),
|
|
)
|
|
shell.resize(1600, 940)
|
|
shell.show()
|
|
_settle(application, 24)
|
|
try:
|
|
# 1. Zero-sidebar floating capsule nav defaults to reception Digital Twin Cockpit
|
|
assert not hasattr(shell, "sidebar")
|
|
assert shell._active_page_key == "reception"
|
|
assert shell.nav_buttons["reception"].isChecked()
|
|
|
|
rec_page = shell.pages["reception"]
|
|
assert rec_page._workspace_mode == "cockpit"
|
|
assert rec_page.view_mode_stack.currentWidget() is rec_page.cockpit_view
|
|
assert shell.patient_context_store.current_patient.has_patient
|
|
active_name = shell.patient_context_store.current_patient.patient_name
|
|
assert active_name in shell.context_patient_name.text()
|
|
|
|
# Paint-test all custom QPainter widgets via grab()
|
|
cockpit_img = shell.grab().toImage()
|
|
assert not cockpit_img.isNull()
|
|
assert cockpit_img.width() >= 1600
|
|
|
|
# 2. Switch Twin Stage modes (sanjiao -> organ -> tcm)
|
|
rec_page.cockpit_view.twin_stage.set_mode("organ")
|
|
rec_page.cockpit_view.twin_stage.set_mode("tcm")
|
|
rec_page.cockpit_view.twin_stage.set_mode("sanjiao")
|
|
_settle(application, 4)
|
|
|
|
# 3. Switch to 6-tab Structured Chart and back to Digital Twin Cockpit
|
|
rec_page._open_structured_chart_tab(4)
|
|
_settle(application, 4)
|
|
assert rec_page._workspace_mode == "chart"
|
|
assert rec_page._return_to_cockpit_bar.isVisible()
|
|
assert rec_page.detail_tabs.currentIndex() == 4
|
|
rec_page.set_workspace_mode("cockpit")
|
|
_settle(application, 4)
|
|
assert rec_page._workspace_mode == "cockpit"
|
|
|
|
# 4. Expand Serene.ai Obsidian Aurora-Glass Bottom Sheet
|
|
assert not shell.ai_bottom_sheet.is_expanded
|
|
shell.toggle_ai_bottom_sheet()
|
|
_settle(application, 6)
|
|
assert shell.ai_bottom_sheet.is_expanded
|
|
assert shell.ai_bottom_sheet.height() > 200
|
|
expanded_img = shell.grab().toImage()
|
|
assert not expanded_img.isNull()
|
|
shell.toggle_ai_bottom_sheet()
|
|
_settle(application, 4)
|
|
assert not shell.ai_bottom_sheet.is_expanded
|
|
|
|
# 5. Navigate to Schedule (AppointmentsPage) & verify 7-day WeekCalendarStripWidget
|
|
assert shell.navigate("appointments")
|
|
_settle(application, 16)
|
|
appt_page = shell.pages["appointments"]
|
|
assert appt_page.spatial_week_card.isVisible()
|
|
assert not appt_page.header.isVisible()
|
|
tomorrow_iso = (date.today() + timedelta(days=1)).isoformat()
|
|
appt_page.week_strip.set_selected_date(date.today() + timedelta(days=1), emit_signal=True)
|
|
_settle(application, 10)
|
|
assert appt_page._start_date == tomorrow_iso
|
|
|
|
# 6. Navigate across Patients, Prescription Library, Prescriptions, Consultations
|
|
for route_key in ("patients", "prescription_library", "prescriptions", "consultations"):
|
|
assert shell.navigate(route_key)
|
|
_settle(application, 12)
|
|
page = shell.pages[route_key]
|
|
assert hasattr(page, "spatial_bridge")
|
|
assert page.spatial_bridge.isVisible()
|
|
finally:
|
|
for page in shell.pages.values():
|
|
poll = getattr(page, "poll_timer", None)
|
|
if poll is not None:
|
|
poll.stop()
|
|
shell.chat_notifications.stop()
|
|
shell.close()
|
|
shell.deleteLater()
|
|
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
|
|
application.processEvents()
|