import json from pathlib import Path import subprocess import sys import zipfile def make_plan(tmp_path: Path, restart_probe_exit_code: int): from dx_productivity.update.installer import InstallPlan current = tmp_path / "portable" current.mkdir() (current / "DxDesignerTools.exe").write_bytes(b"old") staged = tmp_path / "staged.zip" with zipfile.ZipFile(staged, "w") as archive: archive.writestr("DxDesignerTools.exe", b"new") backup = tmp_path / "backups" return InstallPlan( current_root=current, staged_zip=staged, backup_root=backup, restart_command=( sys.executable, "-c", f"raise SystemExit({restart_probe_exit_code})", ), ) def test_install_plan_restores_previous_root_when_restart_probe_fails(tmp_path: Path): from dx_productivity.update.installer import UpdateInstaller plan = make_plan(tmp_path, restart_probe_exit_code=1) result = UpdateInstaller().install(plan) assert result.state == "rolled-back" assert (plan.current_root / "DxDesignerTools.exe").read_bytes() == b"old" assert any(plan.backup_root.iterdir()) def test_install_plan_switches_to_verified_root_on_success(tmp_path: Path): from dx_productivity.update.installer import UpdateInstaller plan = make_plan(tmp_path, restart_probe_exit_code=0) result = UpdateInstaller().install(plan) assert result.state == "installed" assert (plan.current_root / "DxDesignerTools.exe").read_bytes() == b"new" def test_install_plan_rejects_missing_staged_zip(tmp_path: Path): from dx_productivity.update.installer import InstallPlan, UpdateInstaller plan = InstallPlan( current_root=tmp_path / "portable", staged_zip=tmp_path / "missing.zip", backup_root=tmp_path / "backups", restart_command=(sys.executable, "-c", "raise SystemExit(0)"), ) plan.current_root.mkdir() (plan.current_root / "DxDesignerTools.exe").write_bytes(b"old") result = UpdateInstaller().install(plan) assert result.state == "failed"