workspace-removal-runtime-release.test.ts 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { execFile } from "node:child_process";
  2. import { promisify } from "node:util";
  3. import { rename, rm, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { clearAppData, getE2EAppDataPaths, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  6. import { openProjectMenuAction, waitForProjectRowGone } from "#e2e/helpers/project-sidebar";
  7. import { getPassiveAgentStartupWorkspacePaths } from "#e2e/helpers/passive-agent-startup-fixture";
  8. import {
  9. openWorkspaceDraftFromSidebar,
  10. waitForRestoredProjects,
  11. } from "#e2e/helpers/workspace-agent-warmup";
  12. import { selectUpstreamModel, selectUpstreamThoughtLevel } from "#e2e/helpers/upstream-provider";
  13. import {
  14. getV4PaneSnapshot,
  15. sendV4Prompt,
  16. waitForV4ComposerText,
  17. waitForV4AssistantMessageContaining,
  18. waitForV4ConversationState,
  19. } from "#e2e/helpers/v4-conversation";
  20. import {
  21. collectProcessTreeByRootPids,
  22. listSystemProcesses,
  23. } from "#e2e/helpers/e2e-process-cleanup";
  24. const MARKER = "E2E_WORKSPACE_REMOVAL_RUNTIME_RELEASE";
  25. const REPLY = "WRL_HISTORY_PRESERVED";
  26. describe("项目软删除与应用退出 E2E", () => {
  27. after(async () => {
  28. await clearAppData();
  29. });
  30. it("WRL01: 删除项目保留会话,退出后释放进程树和目录占用", async function () {
  31. this.timeout(150000);
  32. const { homeDir } = getE2EAppDataPaths();
  33. const workspacePaths = getPassiveAgentStartupWorkspacePaths(homeDir, 2);
  34. const removable = workspacePaths[0];
  35. const active = workspacePaths[1];
  36. if (!removable || !active) throw new Error("WRL01: 缺少 workspace fixture");
  37. await writeFile(join(removable, "wrl-lock-probe.txt"), "runtime release probe", "utf8");
  38. await waitForWorkspaceApp(active, 30000);
  39. await waitForRestoredProjects(workspacePaths, 30000);
  40. await openWorkspaceDraftFromSidebar(removable);
  41. // 本例使用两个专用目录,不能调用强制等待默认 ZCodeProject 的通用准备入口。
  42. await selectUpstreamModel();
  43. await selectUpstreamThoughtLevel();
  44. await sendV4Prompt(`${MARKER}: reply exactly ${REPLY}`);
  45. await waitForV4ComposerText("", "WRL01 首发未接受");
  46. await waitForV4AssistantMessageContaining(REPLY);
  47. await waitForV4ConversationState((state) => state.state === "idle", "WRL01 历史未完成");
  48. const taskId = (await getV4PaneSnapshot()).sessionId;
  49. if (!taskId || taskId === "draft") throw new Error("WRL01: 未创建真实会话");
  50. // 当前删除只软删项目容器;旧用例立即要求 Agent 归零,混淆了容器和会话生命周期。
  51. await openProjectMenuAction(removable, "delete", { timeout: 30000 });
  52. await waitForProjectRowGone(removable, { timeout: 30000 });
  53. // 已完成任务删除项目无需确认,当前打开的会话内容仍应保持可读。
  54. await waitForV4AssistantMessageContaining(REPLY);
  55. expect((await getV4PaneSnapshot()).sessionId).toBe(taskId);
  56. const mainPid = await browser.electron.execute(() => process.pid);
  57. const treePids = collectProcessTreeByRootPids(await listSystemProcesses(), [mainPid]).map(
  58. (item) => item.pid,
  59. );
  60. expect(treePids).toContain(mainPid);
  61. await browser.electron.execute((electron) => electron.app.quit());
  62. // 运行时证据:Main 已退出却暂留 Z/<defunct>,直到 ChromeDriver DELETE session 回收。
  63. // kill(pid, 0) 会把僵尸进程当存活;它已无目录句柄,按真实进程状态等待,不能强杀转绿。
  64. await browser.waitUntil(
  65. async () => {
  66. const remaining = (await listSystemProcesses()).filter(
  67. (item) => treePids.includes(item.pid) && !item.command.endsWith("<defunct>"),
  68. );
  69. return remaining.length === 0;
  70. },
  71. { timeout: 15000, interval: 100, timeoutMsg: "WRL01: 应用退出后仍有活跃进程" },
  72. );
  73. if (process.platform !== "win32") {
  74. const state = await promisify(execFile)("ps", [
  75. "-p",
  76. treePids.join(","),
  77. "-o",
  78. "pid=,ppid=,stat=,command=",
  79. ]).then(
  80. (result) => result.stdout,
  81. () => "所有跟踪进程已被回收",
  82. );
  83. await writeFile(join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "quit-process-state.txt"), state);
  84. }
  85. const renamed = `${removable}-renamed`;
  86. // 进程退出屏障之后只操作一次,禁止靠重命名重试掩盖资源未释放。
  87. await rename(removable, renamed);
  88. await rm(renamed, { recursive: true });
  89. });
  90. });