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

252 lines
11 KiB
Python

from __future__ import annotations
import os
from contextlib import contextmanager
from dataclasses import replace
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
import pytest
from PySide6.QtCore import QEvent, QPoint, QSettings, Qt
from PySide6.QtGui import QContextMenuEvent
from PySide6.QtTest import QSignalSpy, QTest
from PySide6.QtWidgets import QApplication, QMenu, QWidget
from doctor_workstation.ui import shell, shell_spatial
AI = "ai_assistant"
DEFAULT = ["reception", "appointments", "patients", AI]
PERMISSIONS = {
"doctor.appointment/lists",
"firstvisit.myPatient/lists",
"tcm.diagnosis/aiAssistant",
}
class _Page(QWidget):
def __init__(self, repository, *, permissions, current_user, parent):
super().__init__(parent)
@pytest.fixture
def open_shell(monkeypatch, tmp_path):
app = QApplication.instance() or QApplication([])
monkeypatch.setattr(
shell, "NAVIGATION", [replace(item, page_type=_Page) for item in shell.NAVIGATION]
)
monkeypatch.setattr(
shell_spatial,
"QSettings",
lambda *_: QSettings(str(tmp_path / "settings.ini"), QSettings.Format.IniFormat),
)
monkeypatch.setattr(
shell_spatial.SpatialShellWindow, "_prewarm_wing_surfaces", lambda self: None
)
@contextmanager
def open_window(user_id=1, permissions=PERMISSIONS):
user = {"id": user_id, "account": f"test-{user_id}"}
window = shell_spatial.SpatialShellWindow(
object(), {"user": user, "demo_mode": True}, permissions
)
window.resize(1440, 900)
window.show()
window.activateWindow()
app.processEvents()
try:
yield window
finally:
window.close()
window.deleteLater()
app.sendPostedEvents(None, QEvent.Type.DeferredDelete)
app.processEvents()
return open_window
def _visual_order(window):
keys = {button: key for key, button in window.nav_buttons.items()}
if hasattr(window, "ai_nav_button"):
keys[window.ai_nav_button] = AI
return [keys[window.nav_layout.itemAt(i).widget()] for i in range(window.nav_layout.count())]
def _drag(source, end):
QTest.mousePress(source, Qt.MouseButton.LeftButton, pos=source.rect().center())
QTest.mouseMove(source, end)
QTest.mouseRelease(source, Qt.MouseButton.LeftButton, pos=end)
QApplication.processEvents()
def _visible_feedback(name):
return [
widget for widget in QApplication.allWidgets()
if widget.objectName() == name and widget.isVisible()
]
@pytest.mark.parametrize("key", ["patients", AI])
@pytest.mark.parametrize("hold", [False, True], ids=["short-click", "hold-and-release"])
def test_press_without_drag_activates_once_and_leaves_no_preview(open_shell, key, hold):
with open_shell() as window:
button = window.ai_nav_button if key == AI else window.nav_buttons[key]
clicked = QSignalSpy(button.clicked)
pos = button.rect().center()
QTest.mousePress(button, Qt.MouseButton.LeftButton, pos=pos)
if hold:
QTest.qWait(300)
preview, = _visible_feedback("NavDragPreview")
assert preview.width() > 0 and preview.height() > 0
else:
assert not _visible_feedback("NavDragPreview")
assert clicked.count() == 0
assert window.stack.currentWidget() is window.pages["reception"]
assert not window.ai_bottom_sheet.is_expanded
QTest.mouseRelease(button, Qt.MouseButton.LeftButton, pos=pos)
QTest.qWait(300)
assert clicked.count() == 1
assert _visual_order(window) == DEFAULT
assert not _visible_feedback("NavDragPreview")
assert not _visible_feedback("NavDragOrigin")
if key == AI:
assert window.ai_bottom_sheet.is_expanded
else:
assert window.stack.currentWidget() is window.pages[key]
@pytest.mark.parametrize("key", ["reception", AI], ids=["selected-page", "ai"])
@pytest.mark.parametrize("cancel", ["outside", "escape"])
def test_preview_tracks_drag_origin_stays_put_and_cancel_discards_move(open_shell, key, cancel):
with open_shell() as window:
button = window.ai_nav_button if key == AI else window.nav_buttons[key]
clicked = QSignalSpy(button.clicked)
press = button.rect().center()
original_center = button.mapToGlobal(press)
cursor_before = button.cursor().shape()
QTest.mousePress(button, Qt.MouseButton.LeftButton, pos=press)
if cancel == "outside":
QTest.qWait(300)
assert _visible_feedback("NavDragPreview")
targets = [window.nav_buttons[target_key] for target_key in ("appointments", "patients")]
if key == AI:
targets.reverse()
first, second = [target.mapToGlobal(target.rect().center()) for target in targets]
QTest.mouseMove(button, button.mapFromGlobal(first))
preview, = _visible_feedback("NavDragPreview")
origin, = _visible_feedback("NavDragOrigin")
assert preview.rect().contains(preview.mapFromGlobal(first))
assert origin.rect().contains(origin.mapFromGlobal(original_center))
preview_start = preview.mapToGlobal(QPoint())
origin_start = origin.mapToGlobal(QPoint())
QTest.mouseMove(button, button.mapFromGlobal(second))
assert preview.mapToGlobal(QPoint()) - preview_start == second - first
assert origin.mapToGlobal(QPoint()) == origin_start
if cancel == "escape":
QTest.keyClick(window.focusWidget() or button, Qt.Key.Key_Escape)
assert not _visible_feedback("NavDragPreview")
assert not _visible_feedback("NavDragOrigin")
release = second
else:
bar = window.nav_island
release = bar.mapToGlobal(QPoint(bar.rect().center().x(), bar.height() + 20))
QTest.mouseMove(button, button.mapFromGlobal(release))
QTest.mouseRelease(button, Qt.MouseButton.LeftButton, pos=button.mapFromGlobal(release))
QTest.qWait(300)
assert not _visible_feedback("NavDragPreview")
assert not _visible_feedback("NavDragOrigin")
assert button.isVisible() and not button.isDown()
assert button.cursor().shape() == cursor_before
assert _visual_order(window) == DEFAULT
assert window.nav_buttons["reception"].isChecked()
assert window.stack.currentWidget() is window.pages["reception"]
assert not window.ai_bottom_sheet.is_expanded
assert clicked.count() == 0
QTest.mouseClick(button, Qt.MouseButton.LeftButton)
assert clicked.count() == 1
def test_drag_reorders_without_activating_and_preserves_buttons_and_shortcuts(open_shell):
with open_shell() as window:
assert _visual_order(window) == DEFAULT
ai = window.ai_nav_button
first = window.nav_buttons["reception"]
before_first = first.mapToGlobal(QPoint(2, first.height() // 2))
_drag(ai, ai.mapFromGlobal(before_first))
assert _visual_order(window) == [AI, "reception", "appointments", "patients"]
assert not _visible_feedback("NavDragPreview")
assert not _visible_feedback("NavDragOrigin")
assert window.nav_island.order == _visual_order(window)
assert window.stack.currentWidget() is window.pages["reception"]
assert not window.ai_bottom_sheet.is_expanded
patients = window.nav_buttons["patients"]
outside = window.nav_island.mapToGlobal(QPoint(2, window.nav_island.height() + 22))
_drag(patients, patients.mapFromGlobal(outside))
assert _visual_order(window) == [AI, "reception", "appointments", "patients"]
assert window.stack.currentWidget() is window.pages["reception"]
QTest.mouseClick(patients, Qt.MouseButton.LeftButton)
assert window.stack.currentWidget() is window.pages["patients"]
QTest.keyClick(window, Qt.Key.Key_2, Qt.KeyboardModifier.AltModifier)
assert window.stack.currentWidget() is window.pages["reception"]
assert first.isChecked()
QTest.mouseClick(ai, Qt.MouseButton.LeftButton)
assert window.ai_bottom_sheet.is_expanded
QTest.keyClick(window, Qt.Key.Key_BracketRight, Qt.KeyboardModifier.ControlModifier)
assert not window.ai_bottom_sheet.is_expanded
QTest.keyClick(window, Qt.Key.Key_1, Qt.KeyboardModifier.AltModifier)
assert window.ai_bottom_sheet.is_expanded
assert window.stack.currentWidget() is window.pages["reception"]
def test_context_menu_moves_the_button_and_restores_order(open_shell):
with open_shell() as window:
def context_actions():
button = window.ai_nav_button
pos = button.rect().center()
event = QContextMenuEvent(QContextMenuEvent.Reason.Mouse, pos, button.mapToGlobal(pos))
QApplication.sendEvent(button, event)
menu = QApplication.activePopupWidget()
assert isinstance(menu, QMenu)
return menu, {action.text(): action for action in menu.actions()}
menu, actions = context_actions()
assert not actions["向右移动"].isEnabled()
QTest.mouseClick(
menu, Qt.MouseButton.LeftButton, pos=menu.actionGeometry(actions["向左移动"]).center()
)
assert _visual_order(window) == ["reception", "appointments", AI, "patients"]
menu, actions = context_actions()
QTest.mouseClick(
menu,
Qt.MouseButton.LeftButton,
pos=menu.actionGeometry(actions["恢复默认顺序"]).center(),
)
assert _visual_order(window) == DEFAULT
assert not window.ai_bottom_sheet.is_expanded
def test_order_persists_per_user_and_respects_permissions(open_shell):
reordered = ["patients", AI, "reception", "appointments"]
with open_shell() as window:
window.nav_island.move_button("patients", 0)
window.nav_island.move_button(AI, 1)
assert _visual_order(window) == reordered
with open_shell(2) as other_user:
assert _visual_order(other_user) == DEFAULT
with open_shell(permissions={"doctor.appointment/lists"}) as limited:
assert _visual_order(limited) == ["reception", "appointments"]
assert not hasattr(limited, "ai_nav_button")
limited.nav_island.move_button("appointments", 0)
assert _visual_order(limited) == ["appointments", "reception"]
with open_shell() as restored:
assert _visual_order(restored) == ["patients", AI, "appointments", "reception"]
restored.nav_island.reset_order()
assert _visual_order(restored) == DEFAULT
with open_shell() as reset:
assert _visual_order(reset) == DEFAULT