Files
zyt/app/tests/test_cockpit_layout_v5.py
T
2026-12-03 09:54:46 +08:00

260 lines
10 KiB
Python

"""Automated verification of Clinical OS 5.0 Modular Cockpit (Remove, 4 Layouts, Reorder & Persist)."""
from __future__ import annotations
import os
from pathlib import Path
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.services.demo_repository import DemoDoctorRepository
from doctor_workstation.ui.shell_spatial import SpatialShellWindow
from doctor_workstation.ui.theme import apply_theme
from doctor_workstation.ui.visuals.cockpit_layout import (
ALL_CARD_KEYS,
COCKPIT_CARD_SPECS,
COCKPIT_PRESET_SUITES,
DEFAULT_CARD_ORDER,
)
from doctor_workstation.ui.visuals.reception_cockpit import ReceptionDigitalTwinCockpit
def _settle(app: QApplication, rounds: int = 12) -> None:
for _ in range(rounds):
app.processEvents()
QTest.qWait(6)
@pytest.fixture(scope="module")
def application() -> QApplication:
app = QApplication.instance() or QApplication([])
apply_theme(app)
return app
def test_cockpit_card_remove_restore_guard_and_persistence(
application: QApplication,
tmp_path: Path,
) -> None:
store_file = tmp_path / "cockpit_layout.json"
cockpit = ReceptionDigitalTwinCockpit(
layout_storage_path=store_file,
persist_layout=True,
)
cockpit.resize(1380, 760)
cockpit.show()
_settle(application, 6)
try:
assert cockpit.layout_state.visible_cards == list(DEFAULT_CARD_ORDER)
# 1. Remove 2 cards ('care_team' and 'fasting')
assert cockpit.remove_card("care_team") is True
assert cockpit.remove_card("fasting") is True
_settle(application, 4)
assert not cockpit._card_tiles["care_team"].isVisible()
assert not cockpit._card_tiles["fasting"].isVisible()
assert set(cockpit.layout_state.removed_cards) == {"care_team", "fasting"}
# 2. Removing down to 1 card succeeds, but removing the VERY LAST card is blocked!
assert cockpit.remove_card("diurnal") is True
assert cockpit.remove_card("queue") is True
assert cockpit.remove_card("journey") is True
assert cockpit.layout_state.visible_cards == ["twin_stage"]
assert cockpit.remove_card("twin_stage") is False
assert cockpit.layout_state.visible_cards == ["twin_stage"]
# 3. Restore cards and customize order + layout mode + span
assert cockpit.restore_card("queue") is True
assert cockpit.restore_card("fasting") is True
assert cockpit.restore_card("journey") is True
assert cockpit.reorder_card("queue", "fasting") is True
assert cockpit.set_layout_mode("bento_flow") is True
assert cockpit.set_card_span("queue", 6) is True
assert store_file.exists()
# 4. Reload a brand-new Cockpit from disk and verify exact state restoration
reloaded = ReceptionDigitalTwinCockpit(
layout_storage_path=store_file,
persist_layout=True,
)
try:
assert reloaded.layout_state.layout_mode == "bento_flow"
assert reloaded.layout_state.span_for("queue") == 6
assert reloaded.layout_state.visible_cards[0] == "queue"
assert set(reloaded.layout_state.removed_cards) == {"care_team", "diurnal"}
reloaded.reset_layout_defaults()
assert reloaded.layout_state.layout_mode == "classic"
assert reloaded.layout_state.visible_cards == list(DEFAULT_CARD_ORDER)
assert reloaded.layout_state.removed_cards == []
finally:
reloaded.close()
reloaded.deleteLater()
finally:
cockpit.close()
cockpit.deleteLater()
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
application.processEvents()
def test_spatial_shell_cockpit_four_layouts_and_reordering(application: QApplication) -> None:
repo = DemoDoctorRepository()
session = repo.login(repo.DEMO_ACCOUNT, repo.DEMO_PASSWORD)
shell = SpatialShellWindow(
repo,
{"session": session, "demo_mode": True},
session.permissions,
)
shell.resize(1536, 864)
shell.show()
_settle(application, 20)
try:
rec = shell.pages["reception"]
cockpit = rec.cockpit_view
# Sub-hero layout studio toggle button works cleanly
assert shell.cockpit_layout_button.isVisible()
shell.cockpit_layout_button.click()
_settle(application, 6)
assert cockpit.is_layout_edit_mode is True
assert cockpit.layout_studio_bar.isVisible()
assert all(cockpit._card_tiles[k].edit_bar.isVisible() for k in cockpit.layout_state.visible_cards)
# Reorder in Classic mode: move 'queue' to slot 0 and 'care_team' to slot 1 -> Queue moves to LEFT!
cockpit.reorder_card("queue", "fasting")
cockpit.reorder_card("care_team", "fasting")
_settle(application, 8)
q_tile = cockpit._card_tiles["queue"]
f_tile = cockpit._card_tiles["fasting"]
q_x = q_tile.mapTo(cockpit.grid_canvas, q_tile.rect().topLeft()).x()
f_x = f_tile.mapTo(cockpit.grid_canvas, f_tile.rect().topLeft()).x()
assert q_x < f_x, f"Expected queue ({q_x}) to sit left of fasting ({f_x}) after reordering!"
# Move 'journey' to slot 0 -> Journey calendar moves to TOP above columns!
cockpit.reorder_card("journey", "queue")
_settle(application, 8)
j_tile = cockpit._card_tiles["journey"]
t_tile = cockpit._card_tiles["twin_stage"]
j_y = j_tile.mapTo(cockpit.grid_canvas, j_tile.rect().topLeft()).y()
t_y = t_tile.mapTo(cockpit.grid_canvas, t_tile.rect().topLeft()).y()
assert j_y < t_y, f"Expected journey ({j_y}) above twin_stage ({t_y})!"
# Exercise all 4 layout modes and verify every visible card renders with valid geometry
for mode_key in ("stage_focus", "dual_grid", "bento_flow", "classic"):
assert cockpit.set_layout_mode(mode_key) is True
_settle(application, 6)
snap = shell.grab()
assert not snap.isNull()
for k in cockpit.layout_state.visible_cards:
tile = cockpit._card_tiles[k]
assert tile.isVisible()
assert tile.width() >= 180
assert tile.height() >= 120
# Remove 1 card -> Sub-Hero button displays hidden badge once exiting edit mode
cockpit.remove_card("care_team")
cockpit.set_layout_edit_mode(False)
_settle(application, 4)
assert "已隐1" in shell.cockpit_layout_button.text()
cockpit.reset_layout_defaults()
_settle(application, 4)
assert shell.cockpit_layout_button.text() == "布局编排"
finally:
for page in shell.pages.values():
if hasattr(page, "poll_timer"):
page.poll_timer.stop()
shell.chat_notifications.stop()
shell.close()
shell.deleteLater()
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
application.processEvents()
def test_four_curated_suites_14_card_library_swap_and_custom_save(
application: QApplication,
tmp_path: Path,
) -> None:
assert len(COCKPIT_CARD_SPECS) >= 14
assert len(COCKPIT_PRESET_SUITES) >= 4
# Verify the 4 preset suites have genuinely distinct card combinations
suite_card_sets = [frozenset(s.card_keys) for s in COCKPIT_PRESET_SUITES]
assert len(set(suite_card_sets)) == len(COCKPIT_PRESET_SUITES)
store_file = tmp_path / "cockpit_suites_v51.json"
repo = DemoDoctorRepository()
session = repo.login(repo.DEMO_ACCOUNT, repo.DEMO_PASSWORD)
shell = SpatialShellWindow(
repo,
{"session": session, "demo_mode": True},
session.permissions,
)
shell.resize(1536, 864)
shell.show()
_settle(application, 20)
try:
cockpit = shell.pages["reception"].cockpit_view
cockpit._layout_store._path = store_file
cockpit._layout_store._persist = True
# Sub-Hero 1-click Suite Island is visible with all 4 presets
assert shell.cockpit_suite_island.isVisible()
for spec in COCKPIT_PRESET_SUITES:
shell._subhero_suite_buttons[spec.key].click()
_settle(application, 8)
assert cockpit.layout_state.active_suite == spec.key
assert cockpit.layout_state.visible_cards == list(spec.card_keys)
for ck in spec.card_keys:
tile = cockpit._card_tiles[ck]
assert tile.isVisible()
assert tile.width() >= 180
# Free Card Library: add new cards, swap a card in-place, and save as Custom Suite
assert cockpit.apply_suite("tcm_rx") is True
assert cockpit.swap_card("notes_media", "metabolic_panel") is True
assert "metabolic_panel" in cockpit.layout_state.visible_cards
assert "notes_media" not in cockpit.layout_state.visible_cards
assert cockpit.add_card("ai_brief_card") is True
assert "ai_brief_card" in cockpit.layout_state.visible_cards
cockpit.save_custom_suite()
_settle(application, 4)
assert cockpit.layout_state.active_suite == "custom"
assert shell._subhero_custom_suite_btn.isVisible()
saved_custom_cards = list(cockpit.layout_state.visible_cards)
# Switch back to 'metabolic', then restore 'custom' in 1 click!
assert cockpit.apply_suite("metabolic") is True
assert cockpit.layout_state.visible_cards != saved_custom_cards
assert cockpit.apply_suite("custom") is True
assert cockpit.layout_state.visible_cards == saved_custom_cards
# Verify every single one of the 14 cards can be rendered without error
cockpit.set_layout_mode("bento_flow")
for ck in ALL_CARD_KEYS:
cockpit.add_card(ck)
_settle(application, 8)
assert len(cockpit.layout_state.visible_cards) == len(ALL_CARD_KEYS)
full_snap = shell.grab()
assert not full_snap.isNull()
finally:
for page in shell.pages.values():
if hasattr(page, "poll_timer"):
page.poll_timer.stop()
shell.chat_notifications.stop()
shell.close()
shell.deleteLater()
application.sendPostedEvents(None, QEvent.Type.DeferredDelete)
application.processEvents()