e2eRuntimePaths.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { readFileSync, readdirSync } from "node:fs";
  2. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join, relative, resolve } from "node:path";
  5. import { describe, expect, it } from "vitest";
  6. import {
  7. findE2EBashOutputPath,
  8. E2E_RUNTIME_RELATIVE_ROOT,
  9. E2E_RUNTIME_ROOT_REPLAY_TOKEN,
  10. createE2EReplayFixtureVariables,
  11. isE2EHomeDirName,
  12. isIsolatedE2EHomeDir,
  13. resolveE2EHomeDir,
  14. resolveE2EStorageRoot,
  15. resolveE2ERuntimePath,
  16. resolveE2ERuntimeRoot,
  17. resolveE2EShellPath,
  18. resolveE2EToolPath,
  19. } from "./e2e/helpers/e2e-runtime-paths.js";
  20. const DESKTOP_TEST_DIR = import.meta.dirname;
  21. const E2E_DIR = join(DESKTOP_TEST_DIR, "e2e");
  22. const SOURCE_EXTENSIONS = new Set([".js", ".json", ".mjs", ".ts", ".tsx"]);
  23. // 这条守卫约束的是「Desktop E2E 自己的 runtime 文件不许落在本机系统 /tmp」。
  24. // R3A-BLD-09 的被测对象恰好是远端主机的 /tmp(8MB noexec tmpfs),用例必须断言
  25. // 远端 mount 属性以及安装后 /tmp 无 zcode-server 残留,这里的 /tmp 不是本地 runtime 路径。
  26. // 新增豁免必须像这样显式登记并说明理由,避免守卫被整体放宽。
  27. const REMOTE_TMP_ASSERTION_SOURCES = new Set([
  28. // 用例转正会移动文件;精确路径豁免必须随迁移更新,仍只放行下面列出的场景。
  29. "conversation-session/remote/installation/conversation-session-r3-install-environment.test.ts",
  30. // 数字 UID 的 HOME/workdir 位于被测 Docker 容器;不是 Desktop 宿主上的 runtime 文件。
  31. "conversation-session/remote/connection/conversation-session-r3-docker-transport.test.ts",
  32. // 修复原因:以下用例在 10-27 / 11-03 合入时未登记豁免,守卫在 staging 上已失败。
  33. // 被测 SSH/Docker 容器内的 /tmp:私有 tmp 挂载、探针夹具、采样/kill 日志、鉴权计数与安装包。
  34. "conversation-session/remote/installation/conversation-session-r3-daemon-service-fallback.test.ts",
  35. "conversation-session/remote/installation/conversation-session-ssh-probe-validation.test.ts",
  36. "conversation-session/remote/installation/conversation-session-ssh-remote-daemon-starting-reuse.test.ts",
  37. "conversation-session/remote/installation/conversation-session-ssh-remote-install-killed.test.ts",
  38. "conversation-session/remote/installation/conversation-session-ssh-remote-legacy-install-handoff.test.ts",
  39. "helpers/remote-restart-stale-daemon.ts",
  40. // 本机 Unix socket 需要短绝对路径(sun_path 约 104 字符上限),放进隔离 HOME 会超长;
  41. // 文件只是监听地址,不承载 E2E runtime 状态,用例结束即删除。
  42. "conversation-session/remote/installation/conversation-session-r3-docker-daemon-unavailable.test.ts",
  43. ]);
  44. // 只匹配作为绝对路径起点的系统 /tmp:前面不能紧跟路径字符,后面不能继续是单词字符。
  45. // 缺陷原因:此前用 includes("/tmp") 子串匹配,把远端 Source 的私有 staging 目录
  46. // "$HOME/.zcode/tmp/prompt-attachments" 也当成了系统 /tmp(R3 attachments 用例被误报)。
  47. const SYSTEM_TMP_PATH_PATTERN = /(?<![\w.\-~}$])\/tmp(?![\w-])/u;
  48. describe("Desktop E2E runtime paths", () => {
  49. it("keeps runtime files below the isolated Agent execution workspace", () => {
  50. const e2eHomeDir = resolve("root", "isolated-e2e-home");
  51. expect(resolveE2ERuntimeRoot(e2eHomeDir)).toBe(join(e2eHomeDir, "ZCodeProject", ".zcode-e2e"));
  52. expect(resolveE2ERuntimePath("bg25", "release-child-bash")).toBe(
  53. join(resolveE2ERuntimeRoot(), "bg25", "release-child-bash"),
  54. );
  55. });
  56. it("uses portable absolute tool paths and workspace-relative shell paths", () => {
  57. expect(resolveE2EToolPath("bg25", "continued-work.txt")).toBe(
  58. resolveE2ERuntimePath("bg25", "continued-work.txt").replaceAll("\\", "/"),
  59. );
  60. expect(resolveE2EShellPath("bg25", "release-child-bash")).toBe(
  61. ".zcode-e2e/bg25/release-child-bash",
  62. );
  63. expect(E2E_RUNTIME_RELATIVE_ROOT).toBe(".zcode-e2e");
  64. expect(E2E_RUNTIME_ROOT_REPLAY_TOKEN).toBe("{{e2eRuntimeRoot}}");
  65. expect(createE2EReplayFixtureVariables(resolve("root", "fixture-home"))).toEqual({
  66. e2eRuntimeRoot: resolveE2ERuntimeRoot(resolve("root", "fixture-home")).replaceAll("\\", "/"),
  67. });
  68. });
  69. it("uses the existing E2E HOME precedence", () => {
  70. expect(
  71. resolveE2EHomeDir({
  72. cwd: resolve("root", "desktop"),
  73. env: { ZCODE_DATA_BASE_DIR: resolve("root", "data-base") },
  74. }),
  75. ).toBe(resolve("root", "data-base"));
  76. expect(
  77. resolveE2EHomeDir({
  78. cwd: resolve("root", "desktop"),
  79. env: {
  80. ZCODE_DATA_BASE_DIR: resolve("root", "data-base"),
  81. ZCODE_E2E_HOME_DIR: resolve("root", "explicit-home"),
  82. },
  83. }),
  84. ).toBe(resolve("root", "explicit-home"));
  85. });
  86. it("keeps the E2E CLI storage under the legacy .zcode directory", () => {
  87. const homeDir = resolve("root", "isolated-e2e-home");
  88. expect(resolveE2EStorageRoot(homeDir)).toBe(join(homeDir, ".zcode"));
  89. });
  90. it("accepts only runner-isolated E2E HOME directories for fixture-writing guards", () => {
  91. expect(isIsolatedE2EHomeDir(resolve("repo", "desktop", ".e2e-home"))).toBe(true);
  92. expect(isIsolatedE2EHomeDir(resolve("repo", "desktop", ".e2e-home-run1"))).toBe(true);
  93. expect(isIsolatedE2EHomeDir(resolve("repo", "desktop", ".e2e-home-run1-shard-2"))).toBe(true);
  94. // 显式根目录名带 .e2e-home 时,分片子目录同样放行。
  95. expect(isIsolatedE2EHomeDir(resolve("srv", ".e2e-home", "run1", "shard-1-of-2"))).toBe(true);
  96. // 显式根目录名不带 .e2e-home 时无法与真实目录区分,守卫保守拒绝。
  97. expect(isIsolatedE2EHomeDir(resolve("srv", "e2e-root", "run1", "shard-1-of-2"))).toBe(false);
  98. expect(isIsolatedE2EHomeDir(resolve("Users", "dev"))).toBe(false);
  99. expect(isIsolatedE2EHomeDir(undefined)).toBe(false);
  100. expect(isIsolatedE2EHomeDir(" ")).toBe(false);
  101. // 前缀相同但不是 runner 布局的目录(误配的备份目录等)不能当作隔离 HOME。
  102. expect(isIsolatedE2EHomeDir(resolve("repo", "desktop", ".e2e-homebackup"))).toBe(false);
  103. });
  104. it("matches only the exact .e2e-home name or a dash-suffixed run directory", () => {
  105. expect(isE2EHomeDirName(".e2e-home")).toBe(true);
  106. expect(isE2EHomeDirName(".e2e-home-run1")).toBe(true);
  107. expect(isE2EHomeDirName(".e2e-homebackup")).toBe(false);
  108. expect(isE2EHomeDirName(".e2e-home2")).toBe(false);
  109. expect(isE2EHomeDirName("e2e-home")).toBe(false);
  110. });
  111. it("does not allow raw system /tmp paths in Desktop E2E runtime sources", () => {
  112. const offenders = collectSourceFiles(E2E_DIR)
  113. .filter((path) => SYSTEM_TMP_PATH_PATTERN.test(readFileSync(path, "utf-8")))
  114. .map((path) => relative(E2E_DIR, path).replaceAll("\\", "/"))
  115. .filter((path) => !REMOTE_TMP_ASSERTION_SOURCES.has(path));
  116. expect(offenders).toEqual([]);
  117. });
  118. });
  119. function collectSourceFiles(root: string): string[] {
  120. return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
  121. const path = join(root, entry.name);
  122. if (entry.isDirectory()) {
  123. return collectSourceFiles(path);
  124. }
  125. const extension = entry.name.slice(entry.name.lastIndexOf("."));
  126. return SOURCE_EXTENSIONS.has(extension) ? [path] : [];
  127. });
  128. }
  129. it("Bash 输出查找按 session/tool 隔离,区分清理完成与多个实际文件", async () => {
  130. const root = await mkdtemp(join(tmpdir(), "e2e-output-path-"));
  131. const storageRoot = join(root, "storage");
  132. const sessionId = "session_current";
  133. const toolCallId = "tool_current";
  134. const create = async (dir: string, session = sessionId) => {
  135. const path = join(root, dir, session, `${toolCallId}-stdout.log`);
  136. await mkdir(join(root, dir, session), { recursive: true });
  137. await writeFile(path, "output");
  138. return path;
  139. };
  140. try {
  141. await create("zcode-bash-other", "session_other");
  142. const options = { tempRoot: root, storageRoot };
  143. expect(await findE2EBashOutputPath(sessionId, toolCallId, options)).toBeNull();
  144. const path = await create("zcode-bash-current");
  145. expect(await findE2EBashOutputPath(sessionId, toolCallId, options)).toBe(path);
  146. await create("zcode-bash-duplicate");
  147. await expect(findE2EBashOutputPath(sessionId, toolCallId, options)).rejects.toThrow("没有唯一");
  148. } finally {
  149. await rm(root, { recursive: true, force: true });
  150. }
  151. });