Files
yiyezhqiu-refactored/tests/ui/test_update_launch.py
T

140 lines
6.0 KiB
Python

import hashlib
import json
from pathlib import Path
import subprocess
import sys
import zipfile
from dx_productivity.ui import app as app_module
from dx_productivity.ui.main_window import MainWindow
from dx_productivity.ui.settings_controller import SettingsController
from dx_productivity.update.models import UpdateManifest
def _ready_update(tmp_path):
package = tmp_path / "release.zip"
with zipfile.ZipFile(package, "w") as archive:
archive.writestr("DxDesignerTools.exe", b"new-version")
archive.writestr("_runtime/marker.dll", b"new-runtime")
manifest = UpdateManifest.from_mapping({
"product": "yiyezhqiu-refactored", "version": "1.1.0", "channel": "development",
"min_supported_version": "1.0.0", "package_url": package.as_uri(),
"sha256": hashlib.sha256(package.read_bytes()).hexdigest(),
"release_notes": "local regression", "published_at": "2026-10-11",
})
controller = SettingsController(settings_path=tmp_path / "settings.json")
# This fixture starts at the already verified package boundary; staging
# itself is covered by real local-file tests in tests/update.
controller._staged_paths[str(manifest.version)] = package
return controller, manifest
def _portable(tmp_path, name):
root = tmp_path / name
(root / "_runtime").mkdir(parents=True)
(root / "DxDesignerTools.exe").write_bytes(b"old-version")
(root / "_runtime/marker.dll").write_bytes(b"old-runtime")
return root
def _launch_fixture(qtbot, tmp_path, monkeypatch):
window = MainWindow()
qtbot.addWidget(window)
controller, manifest = _ready_update(tmp_path)
monkeypatch.setenv("LOCALAPPDATA", str(tmp_path / "local"))
monkeypatch.setattr(sys, "frozen", True, raising=False)
# Prevent the test's shared QApplication from quitting after handoff.
monkeypatch.setattr(app_module.QApplication, "instance", lambda: None)
commands = []
def capture_spawn(command, **kwargs):
commands.append((list(command), kwargs))
return object()
monkeypatch.setattr(app_module.subprocess, "Popen", capture_spawn)
return window, controller, manifest, commands
def test_two_update_handoffs_keep_independent_install_plans(qtbot, tmp_path, monkeypatch):
window, controller, manifest, commands = _launch_fixture(qtbot, tmp_path, monkeypatch)
first = _portable(tmp_path, "portable-first")
second = _portable(tmp_path, "portable-second")
monkeypatch.setattr(sys, "executable", str(first / "DxDesignerTools.exe"))
app_module._launch_update(window, controller, manifest)
monkeypatch.setattr(sys, "executable", str(second / "DxDesignerTools.exe"))
app_module._launch_update(window, controller, manifest)
first_plan = Path(commands[0][0][-1])
second_plan = Path(commands[1][0][-1])
assert first_plan != second_plan
assert json.loads(first_plan.read_text(encoding="utf-8"))["current_root"] == str(first)
assert json.loads(second_plan.read_text(encoding="utf-8"))["current_root"] == str(second)
assert not Path(commands[0][0][0]).is_relative_to(first)
assert not Path(commands[1][0][0]).is_relative_to(second)
def test_ui_update_starts_persistent_application_after_successful_smoke(qtbot, tmp_path, monkeypatch):
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
window, controller, manifest, commands = _launch_fixture(qtbot, tmp_path, monkeypatch)
current = _portable(tmp_path, "portable")
executable = str(current / "DxDesignerTools.exe")
monkeypatch.setattr(sys, "executable", executable)
app_module._launch_update(window, controller, manifest)
mapping = json.loads(Path(commands[0][0][-1]).read_text(encoding="utf-8"))
assert mapping["restart_command"] == [executable, "ui", "--smoke-test-ms", "1000"]
assert mapping.get("launch_command") == [executable, "ui"]
plan = InstallPlan.from_mapping(mapping)
events = []
def successful_smoke(command, **kwargs):
assert (current / "DxDesignerTools.exe").read_bytes() == b"new-version"
events.append(("smoke", tuple(command)))
return subprocess.CompletedProcess(command, 0)
def persistent_launch(command, **kwargs):
assert events == [("smoke", (executable, "ui", "--smoke-test-ms", "1000"))]
assert Path(kwargs["cwd"]) == current
events.append(("launch", tuple(command)))
return object()
monkeypatch.setattr(subprocess, "run", successful_smoke)
monkeypatch.setattr(subprocess, "Popen", persistent_launch)
result = UpdateInstaller().install(plan)
assert result.state == "installed", result.message
assert events == [
("smoke", (executable, "ui", "--smoke-test-ms", "1000")),
("launch", (executable, "ui")),
]
def test_update_handoff_exits_both_windows_and_shutdowns_controllers(qtbot, tmp_path, monkeypatch):
from types import SimpleNamespace
from PySide6.QtCore import QTimer
window, controller, manifest, commands = _launch_fixture(qtbot, tmp_path, monkeypatch)
current = _portable(tmp_path, 'portable-exit')
monkeypatch.setattr(sys, 'executable', str(current / 'DxDesignerTools.exe'))
shutdowns = []
window.settings_controller = SimpleNamespace(shutdown=lambda: shutdowns.append('settings'))
timer = QTimer(window)
timer.start(100)
window.show_panel(0)
with qtbot.waitSignal(window.window_closed):
app_module._launch_update(window, controller, manifest)
assert commands and shutdowns == ['settings']
assert window._closing and not window.isVisible() and not window.toolbar.isVisible()
assert not timer.isActive()
def test_update_does_not_spawn_updater_while_menu_registration_is_running(qtbot, tmp_path, monkeypatch):
from types import SimpleNamespace
window, controller, manifest, commands = _launch_fixture(qtbot, tmp_path, monkeypatch)
window.startup_controller = SimpleNamespace(is_running=lambda: True)
app_module._launch_update(window, controller, manifest)
assert not commands and not window._closing
assert '菜单注册' in window.operation_status.text()
window.startup_controller = None