e2eRuntimePaths.test.ts 7.7 KB

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