e2eRuntimePaths.test.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { readFileSync, readdirSync } from "node:fs";
  2. import { join, relative, resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. E2E_RUNTIME_RELATIVE_ROOT,
  6. E2E_RUNTIME_ROOT_REPLAY_TOKEN,
  7. createE2EReplayFixtureVariables,
  8. resolveE2EHomeDir,
  9. resolveE2EStorageRoot,
  10. resolveE2ERuntimePath,
  11. resolveE2ERuntimeRoot,
  12. resolveE2EShellPath,
  13. resolveE2EToolPath,
  14. } from "./e2e/helpers/e2e-runtime-paths.js";
  15. const DESKTOP_TEST_DIR = import.meta.dirname;
  16. const E2E_DIR = join(DESKTOP_TEST_DIR, "e2e");
  17. const SOURCE_EXTENSIONS = new Set([".js", ".json", ".mjs", ".ts", ".tsx"]);
  18. // 这条守卫约束的是「Desktop E2E 自己的 runtime 文件不许落在本机系统 /tmp」。
  19. // R3A-BLD-09 的被测对象恰好是远端主机的 /tmp(8MB noexec tmpfs),用例必须断言
  20. // 远端 mount 属性以及安装后 /tmp 无 zcode-server 残留,这里的 /tmp 不是本地 runtime 路径。
  21. // 新增豁免必须像这样显式登记并说明理由,避免守卫被整体放宽。
  22. const REMOTE_TMP_ASSERTION_SOURCES = new Set([
  23. "conversation-session/manual-review/pending/conversation-session-r3-install-environment.test.ts",
  24. ]);
  25. // 只匹配作为绝对路径起点的系统 /tmp:前面不能紧跟路径字符,后面不能继续是单词字符。
  26. // 缺陷原因:此前用 includes("/tmp") 子串匹配,把远端 Source 的私有 staging 目录
  27. // "$HOME/.zcode/tmp/prompt-attachments" 也当成了系统 /tmp(R3 attachments 用例被误报)。
  28. const SYSTEM_TMP_PATH_PATTERN = /(?<![\w.\-~}$])\/tmp(?![\w-])/u;
  29. describe("Desktop E2E runtime paths", () => {
  30. it("keeps runtime files below the isolated Agent execution workspace", () => {
  31. const e2eHomeDir = resolve("root", "isolated-e2e-home");
  32. expect(resolveE2ERuntimeRoot(e2eHomeDir)).toBe(join(e2eHomeDir, "ZCodeProject", ".zcode-e2e"));
  33. expect(resolveE2ERuntimePath("bg25", "release-child-bash")).toBe(
  34. join(resolveE2ERuntimeRoot(), "bg25", "release-child-bash"),
  35. );
  36. });
  37. it("uses portable absolute tool paths and workspace-relative shell paths", () => {
  38. expect(resolveE2EToolPath("bg25", "continued-work.txt")).toBe(
  39. resolveE2ERuntimePath("bg25", "continued-work.txt").replaceAll("\\", "/"),
  40. );
  41. expect(resolveE2EShellPath("bg25", "release-child-bash")).toBe(
  42. ".zcode-e2e/bg25/release-child-bash",
  43. );
  44. expect(E2E_RUNTIME_RELATIVE_ROOT).toBe(".zcode-e2e");
  45. expect(E2E_RUNTIME_ROOT_REPLAY_TOKEN).toBe("{{e2eRuntimeRoot}}");
  46. expect(createE2EReplayFixtureVariables(resolve("root", "fixture-home"))).toEqual({
  47. e2eRuntimeRoot: resolveE2ERuntimeRoot(resolve("root", "fixture-home")).replaceAll("\\", "/"),
  48. });
  49. });
  50. it("uses the existing E2E HOME precedence", () => {
  51. expect(
  52. resolveE2EHomeDir({
  53. cwd: resolve("root", "desktop"),
  54. env: { ZCODE_DATA_BASE_DIR: resolve("root", "data-base") },
  55. }),
  56. ).toBe(resolve("root", "data-base"));
  57. expect(
  58. resolveE2EHomeDir({
  59. cwd: resolve("root", "desktop"),
  60. env: {
  61. ZCODE_DATA_BASE_DIR: resolve("root", "data-base"),
  62. ZCODE_E2E_HOME_DIR: resolve("root", "explicit-home"),
  63. },
  64. }),
  65. ).toBe(resolve("root", "explicit-home"));
  66. });
  67. it("keeps the E2E CLI storage under the legacy .zcode directory", () => {
  68. const homeDir = resolve("root", "isolated-e2e-home");
  69. expect(resolveE2EStorageRoot(homeDir)).toBe(join(homeDir, ".zcode"));
  70. });
  71. it("does not allow raw system /tmp paths in Desktop E2E runtime sources", () => {
  72. const offenders = collectSourceFiles(E2E_DIR)
  73. .filter((path) => SYSTEM_TMP_PATH_PATTERN.test(readFileSync(path, "utf-8")))
  74. .map((path) => relative(E2E_DIR, path).replaceAll("\\", "/"))
  75. .filter((path) => !REMOTE_TMP_ASSERTION_SOURCES.has(path));
  76. expect(offenders).toEqual([]);
  77. });
  78. });
  79. function collectSourceFiles(root: string): string[] {
  80. return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
  81. const path = join(root, entry.name);
  82. if (entry.isDirectory()) {
  83. return collectSourceFiles(path);
  84. }
  85. const extension = entry.name.slice(entry.name.lastIndexOf("."));
  86. return SOURCE_EXTENSIONS.has(extension) ? [path] : [];
  87. });
  88. }