"""Update dialog contract for optional and forced desktop upgrades.""" from __future__ import annotations import os os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") from PySide6.QtWidgets import QApplication from doctor_workstation.services.app_update import UpdateOffer, UpdatePackage from doctor_workstation.ui.dialogs.app_update import AppUpdateDialog from doctor_workstation.ui.theme import apply_theme def _offer(*, force: bool, can_install: bool = True) -> UpdateOffer: package = ( UpdatePackage( url="https://cdn.example.com/DoctorWorkstation.zip", sha256="a" * 64, size=1024, filename="DoctorWorkstation.zip", ) if can_install else None ) return UpdateOffer( has_update=True, force=force, enabled=True, current_version="0.1.0", latest_version="0.2.0", min_version="", title="医生工作站 0.2.0", notes="修复若干问题", platform="windows", arch="x64", package=package, can_install=can_install, ) def test_optional_update_dialog_allows_later(application: QApplication | None = None) -> None: app = application or QApplication.instance() or QApplication([]) apply_theme(app) dialog = AppUpdateDialog(_offer(force=False)) dialog.show() app.processEvents() assert dialog.later_button.isVisible() assert dialog.update_button.text() == "立即更新" assert dialog.notes.toPlainText() == "修复若干问题" dialog.close() def test_forced_update_dialog_hides_defer_and_blocks_escape( application: QApplication | None = None, ) -> None: app = application or QApplication.instance() or QApplication([]) apply_theme(app) dialog = AppUpdateDialog(_offer(force=True)) dialog.show() app.processEvents() assert not dialog.later_button.isVisible() assert "必须更新" in dialog.badge.text() dialog.close() app.processEvents() assert dialog.isVisible() dialog.offer = _offer(force=False) dialog._busy = False dialog.close()