Peach: Local-first video metadata scraper and media manager.
test(desktop): 验证制品托盘首启与正常退出
longmeidaoaf4093c62844
diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 91a2f4b11..5b12965b3 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -117,6 +117,13 @@ jobs:while ((Test-Path -LiteralPath $Installed) -and (Get-Date) -lt $Deadline) { Start-Sleep -Seconds 1 }if (Test-Path -LiteralPath $Installed) { throw '卸载后程序目录仍在' }if ((Test-Path -LiteralPath $Key) -or (Test-Path -LiteralPath $Menu)) { throw '卸载后仍留有登记项或开始菜单项' }+ - name: 保留制品验收失败日志+ if: failure()+ uses: actions/upload-artifact@v7+ with:+ name: desktop-smoke-failure+ path: smoke-evidence/+ if-no-files-found: ignorerelease:name: GitHub 预发布diff --git a/scripts/smoke_desktop.py b/scripts/smoke_desktop.pyindex 58799d384..6b8c5ef10 100644--- a/scripts/smoke_desktop.py+++ b/scripts/smoke_desktop.py@@ -2,28 +2,198 @@from __future__ import annotationsimport argparse+from contextlib import contextmanager+import ctypes+from ctypes import wintypesimport jsonimport http.cookiejarimport osfrom pathlib import Path+import shutilimport socketimport subprocess+import sysimport tempfileimport time+import tracebackimport urllib.errorimport urllib.requestimport urllib.parse+import uuid-def wait_health(opener, base: str, process: subprocess.Popen, log_path: Path) -> dict:+@contextmanager+def smoke_directory(evidence_dir: Path):+ """失败时只保留本次临时运行的日志,不打包账本与配置。"""+ with tempfile.TemporaryDirectory(prefix="peach-desktop-") as directory:+ root = Path(directory).resolve()+ try:+ yield root+ except BaseException:+ destination = evidence_dir / uuid.uuid4().hex+ destination.mkdir(parents=True)+ for path in root.rglob("*.log"):+ target = destination / path.relative_to(root)+ target.parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(path, target)+ (destination / "failure.txt").write_text(traceback.format_exc(), encoding="utf-8")+ print(f"制品验收失败,日志:{destination}", file=sys.stderr, flush=True)+ raise+++def artifact_processes(executable: Path) -> list[int]:+ """仅核对本次制品的精确映像路径,包含脱离启动器的托盘与监视器。"""+ powershell = Path(os.environ["SystemRoot"]) / "System32/WindowsPowerShell/v1.0/powershell.exe"+ environment = dict(os.environ, PEACH_SMOKE_EXECUTABLE=str(executable))+ result = subprocess.run([str(powershell), "-NoProfile", "-NonInteractive", "-Command",+ "@(Get-Process | Where-Object { $_.Path -eq $env:PEACH_SMOKE_EXECUTABLE } | "+ "ForEach-Object { $_.Id }) | ConvertTo-Json -Compress"],+ env=environment, capture_output=True, text=True, encoding="utf-8", timeout=15, check=True)+ value = json.loads(result.stdout.strip() or "[]")+ return value if isinstance(value, list) else [value]+++def tray_window_ready(process_ids: list[int]) -> bool:+ """验到 pystray 的真实 Windows 消息窗口,且消息循环可以应答。"""+ user32 = ctypes.WinDLL("user32", use_last_error=True)+ callback_type = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)+ user32.EnumWindows.argtypes = [callback_type, wintypes.LPARAM]+ user32.EnumWindows.restype = wintypes.BOOL+ user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]+ user32.GetClassNameW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]+ user32.SendMessageTimeoutW.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM,+ wintypes.LPARAM, wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)]+ user32.SendMessageTimeoutW.restype = ctypes.c_ssize_t+ found = []++ @callback_type+ def visit(window, _extra):+ process_id = wintypes.DWORD()+ user32.GetWindowThreadProcessId(window, ctypes.byref(process_id))+ if process_id.value in process_ids:+ name = ctypes.create_unicode_buffer(512)+ user32.GetClassNameW(window, name, len(name))+ if name.value.startswith("Peach") and name.value.endswith("SystemTrayIcon"):+ result = ctypes.c_size_t()+ if user32.SendMessageTimeoutW(window, 0, 0, 0, 2, 1000, ctypes.byref(result)):+ found.append(int(window))+ return True++ if not user32.EnumWindows(visit, 0):+ raise ctypes.WinError(ctypes.get_last_error())+ return bool(found)+++def lifecycle_events(data: Path) -> list[dict]:+ path = data / "logs/tray-lifecycle.log"+ if not path.exists():+ return []+ rows = []+ for line in path.read_text(encoding="utf-8").splitlines():+ try:+ rows.append(json.loads(line))+ except json.JSONDecodeError:+ pass # 写入中的末行留给下一轮读取。+ return rows+++def wait_for(check, description: str, *, timeout: float = 60):+ deadline = time.monotonic() + timeout+ while time.monotonic() < deadline:+ result = check()+ if result:+ return result+ time.sleep(.25)+ raise RuntimeError(f"等待超时:{description}")+++def smoke_environment(data: Path) -> dict[str, str]:+ environment = dict(os.environ, PEACH_DATA_ROOT=str(data), PYTHONIOENCODING="utf-8")+ environment.pop("PYTHONPATH", None)+ environment["PATH"] = os.environ.get("SystemRoot", "/usr") + ("/System32" if os.name == "nt" else "/bin")+ return environment+++def stop_tray(executable: Path, root: Path, environment: dict, log, launcher) -> None:+ try:+ stopped = subprocess.run([str(executable), "--installer-stop"], cwd=root,+ env=environment, stdout=log, stderr=log, timeout=90)+ assert stopped.returncode == 0, f"托盘退出失败:{stopped.returncode}"+ launcher.wait(timeout=15)+ wait_for(lambda: not artifact_processes(executable), "托盘、服务与监视器全部退出", timeout=20)+ except BaseException:+ # 正常退出契约失败也要回收本次实例;强制回收会保留失败结果和日志。+ watchdogs = root / "data/state/tray-watchdogs"+ for path in watchdogs.glob("*.json"):+ path.with_suffix(".stop").write_text("smoke-cleanup", encoding="utf-8")+ process_ids = artifact_processes(executable)+ guards = {int(path.read_text(encoding="utf-8")) for path in watchdogs.glob("*.ready")}+ taskkill = Path(os.environ["SystemRoot"]) / "System32/taskkill.exe"+ for pid in sorted(process_ids, key=lambda pid: pid not in guards):+ subprocess.run([str(taskkill), "/PID", str(pid), "/T", "/F"],+ stdout=log, stderr=log, timeout=15, check=False)+ launcher.wait(timeout=15)+ wait_for(lambda: not artifact_processes(executable), "失败实例全部退出", timeout=20)+ raise+++def smoke_tray(executable: Path, evidence_dir: Path) -> None:+ """真实首启托盘、完成设置、接管服务并通过安装器契约正常退出。"""+ assert os.name == "nt", "托盘制品验收需要 Windows"+ assert not artifact_processes(executable), "此制品已有进程运行,不能接管它"+ with smoke_directory(evidence_dir) as root:+ data, media = root / "data", root / "media"+ media.mkdir()+ # 全新数据根的默认端口必须空闲,避免把其他部署当作首启服务。+ with socket.socket() as reservation:+ reservation.bind(("127.0.0.1", 8900))+ with socket.socket() as reservation:+ reservation.bind(("127.0.0.1", 0))+ port = reservation.getsockname()[1]+ environment = smoke_environment(data)+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({}),+ urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))+ with (root / "runtime.log").open("wb") as log:+ launcher = subprocess.Popen([str(executable), "--silent"], cwd=root,+ env=environment, stdout=log, stderr=log)+ try:+ def tray_started():+ rows = lifecycle_events(data)+ pids = [row["tray_pid"] for row in rows if row["event"] == "watchdog_ready"]+ return pids and tray_window_ready(pids)+ wait_for(tray_started, "托盘窗口、消息循环与监视器就绪")+ wait_health(opener, "http://127.0.0.1:8900", None, root / "runtime.log")+ complete_first_run(opener, "http://127.0.0.1:8900", media, port,+ "standalone-tray-smoke-password")+ health = wait_health(opener, f"http://127.0.0.1:{port}", None, root / "runtime.log")+ assert health["ok"]+ assert (data / "database/ledger.db").is_file()+ wait_for(lambda: not (data / "state/configuration-reload.request").exists(),+ "托盘消费设置重载请求")+ assert_requires_login(opener, f"http://127.0.0.1:{port}")+ finally:+ stop_tray(executable, root, environment, log, launcher)+ rows = lifecycle_events(data)+ assert any(row["event"] == "stop_requested" and row.get("reason") == "external-stop"+ for row in rows), "未取得托盘正常退出意图"+ assert any(row["event"] == "exited" and row.get("exit_code") == 0 for row in rows), "未取得托盘正常退出记录"+ assert not any(row["event"] in {"unexpected_exit", "watchdog_error", "recovery_started"}+ for row in rows), "托盘发生异常退出或恢复"+ assert not list((data / "state/tray-watchdogs").glob("*.json")), "监视器记录仍在"+ assert not list((data / "state/tray-watchdogs").glob("*.ready")), "监视器握手仍在"+ print(json.dumps({"ok": True, "checks": ["tray-first-run", "tray-window",+ "tray-watchdog", "tray-service-handoff", "tray-graceful-stop", "tray-no-residue"]}))+++def wait_health(opener, base: str, process: subprocess.Popen | None, log_path: Path) -> dict:deadline = time.monotonic() + 40while True:try:with opener.open(base + "/healthz", timeout=1) as response:return json.load(response)except (OSError, urllib.error.URLError):- if process.poll() is not None or time.monotonic() > deadline:- raise RuntimeError(log_path.read_text(errors="replace"))+ if (process is not None and process.poll() is not None) or time.monotonic() > deadline:+ raise RuntimeError(f"服务未就绪:{base}/healthz\n" + log_path.read_text(encoding="utf-8", errors="replace"))time.sleep(.25)@@ -74,17 +244,17 @@ def complete_first_run(opener, base: str, media: Path, port: int, password: str)def main() -> int:parser = argparse.ArgumentParser()parser.add_argument("executable", type=Path)+ parser.add_argument("--evidence-dir", type=Path, default=Path("smoke-evidence"))args = parser.parse_args()executable = args.executable.resolve(strict=True)- with tempfile.TemporaryDirectory(prefix="peach-desktop-") as directory:- root = Path(directory).resolve()+ evidence_dir = args.evidence_dir.resolve()+ smoke_tray(executable, evidence_dir)+ with smoke_directory(evidence_dir) as root:data = root / "data"media = root / "media"media.mkdir()- environment = dict(os.environ, PEACH_DATA_ROOT=str(data), PYTHONIOENCODING="utf-8")- environment.pop("PYTHONPATH", None)# 制品不得借助用户安装的 Python、Git、Node、FFmpeg 或 OpenSSL。- environment["PATH"] = os.environ.get("SystemRoot", "/usr") + ("/System32" if os.name == "nt" else "/bin")+ environment = smoke_environment(data)with (root / "runtime.log").open("wb") as log:with socket.socket() as reservation:reservation.bind(("127.0.0.1", 0))diff --git a/tests/test_scripts.py b/tests/test_scripts.pyindex 9e22c8dab..f494c3635 100644--- a/tests/test_scripts.py+++ b/tests/test_scripts.py@@ -53,6 +53,109 @@ def load_script(name: str):return module+class DesktopSmokeTests(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.script = load_script("smoke_desktop")++ def test_failure_preserves_logs_without_database_or_configuration(self):+ with tempfile.TemporaryDirectory() as directory:+ evidence = Path(directory).resolve() / "evidence"+ with self.assertRaisesRegex(RuntimeError, "startup failed"), redirect_stderr(io.StringIO()):+ with self.script.smoke_directory(evidence) as root:+ (root / "data/logs").mkdir(parents=True)+ (root / "data/logs/tray.log").write_text("启动堆栈", encoding="utf-8")+ (root / "data/config.toml").write_text("private config", encoding="utf-8")+ (root / "data/ledger.db").write_bytes(b"private database")+ raise RuntimeError("startup failed")+ files = [path.relative_to(evidence).parts[1:] for path in evidence.rglob("*") if path.is_file()]+ self.assertCountEqual(files, [("data", "logs", "tray.log"), ("failure.txt",)])+ self.assertFalse(root.exists())++ def test_success_removes_temporary_data_without_creating_evidence(self):+ with tempfile.TemporaryDirectory() as directory:+ evidence = Path(directory).resolve() / "evidence"+ with self.script.smoke_directory(evidence) as root:+ (root / "runtime.log").write_text("ok", encoding="utf-8")+ self.assertFalse(root.exists())+ self.assertFalse(evidence.exists())++ def test_lifecycle_reader_tolerates_a_record_being_written(self):+ with tempfile.TemporaryDirectory() as directory:+ data = Path(directory).resolve()+ self.assertEqual(self.script.lifecycle_events(data), [])+ (data / "logs").mkdir()+ (data / "logs/tray-lifecycle.log").write_text(+ '{"event":"watchdog_ready","tray_pid":42}\n{"event":', encoding="utf-8")+ self.assertEqual(self.script.lifecycle_events(data), [{"event": "watchdog_ready", "tray_pid": 42}])++ def test_process_inventory_filters_by_exact_artifact_and_handles_single_pid(self):+ with mock.patch.dict(os.environ, {"SystemRoot": "C:/Windows"}), \+ mock.patch.object(self.script.subprocess, "run",+ return_value=SimpleNamespace(stdout="42")) as run:+ executable = Path("C:/isolated/Peach.exe")+ self.assertEqual(self.script.artifact_processes(executable), [42])+ self.assertEqual(run.call_args.kwargs["env"]["PEACH_SMOKE_EXECUTABLE"], str(executable))+ self.assertIn("-NonInteractive", run.call_args.args[0])++ def test_health_wait_accepts_a_detached_launcher(self):+ response = mock.MagicMock()+ response.__enter__.return_value.read.return_value = b'{"ok":true}'+ opener = mock.Mock()+ opener.open.return_value = response+ self.assertEqual(self.script.wait_health(opener, "http://127.0.0.1:1", None,+ Path("unused.log")), {"ok": True})++ def test_failed_normal_stop_disarms_watchdogs_and_reaps_only_artifact_pids(self):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory).resolve()+ watchdogs = root / "data/state/tray-watchdogs"+ watchdogs.mkdir(parents=True)+ (watchdogs / "session.json").write_text("{}", encoding="utf-8")+ (watchdogs / "session.ready").write_text("43", encoding="utf-8")+ executable = root / "Peach.exe"+ launcher = mock.Mock()+ with mock.patch.dict(os.environ, {"SystemRoot": "C:/Windows"}), \+ mock.patch.object(self.script, "artifact_processes", side_effect=[[42, 43], []]), \+ mock.patch.object(self.script, "wait_for", side_effect=lambda check, *_args, **_kwargs: check()), \+ mock.patch.object(self.script.subprocess, "run", side_effect=[+ SimpleNamespace(returncode=1), SimpleNamespace(returncode=0), SimpleNamespace(returncode=0)]) as run:+ with self.assertRaisesRegex(AssertionError, "托盘退出失败"):+ self.script.stop_tray(executable, root, {}, io.BytesIO(), launcher)+ commands = [call.args[0] for call in run.call_args_list]+ self.assertEqual(commands[0], [str(executable), "--installer-stop"])+ self.assertEqual([command[2] for command in commands[1:]], ["43", "42"])+ self.assertEqual((watchdogs / "session.stop").read_text(encoding="utf-8"), "smoke-cleanup")++ @unittest.skipUnless(os.name == "nt", "Windows 托盘验收")+ def test_setup_failure_still_stops_tray_and_checks_all_artifact_processes(self):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory).resolve()+ executable = root / "Peach.exe"+ process = mock.Mock()++ def wait_for(check, description, **_kwargs):+ if "全部退出" in description:+ self.assertTrue(check())+ return True++ with mock.patch.object(self.script, "artifact_processes", return_value=[]) as inventory, \+ mock.patch.object(self.script, "wait_for", side_effect=wait_for), \+ mock.patch.object(self.script, "wait_health", return_value={"ok": True}), \+ mock.patch.object(self.script, "complete_first_run", side_effect=RuntimeError("setup failed")), \+ mock.patch.object(self.script.subprocess, "Popen", return_value=process) as popen, \+ mock.patch.object(self.script.subprocess, "run", return_value=SimpleNamespace(returncode=0)) as stop, \+ mock.patch.object(self.script.socket, "socket"), redirect_stderr(io.StringIO()):+ with self.assertRaisesRegex(RuntimeError, "setup failed"):+ self.script.smoke_tray(executable, root / "evidence")+ self.assertEqual(popen.call_args.args[0], [str(executable), "--silent"])+ self.assertEqual(stop.call_args.args[0], [str(executable), "--installer-stop"])+ self.assertEqual(inventory.call_count, 2)+ process.wait.assert_called_once_with(timeout=15)+ failure = next((root / "evidence").rglob("failure.txt")).read_text(encoding="utf-8")+ self.assertIn("setup failed", failure)++class Sha1SyncOptionsTests(unittest.TestCase):@classmethoddef setUpClass(cls):