e2eRuntimePaths.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. describe("Desktop E2E runtime paths", () => {
  19. it("keeps runtime files below the isolated Agent execution workspace", () => {
  20. const e2eHomeDir = resolve("root", "isolated-e2e-home");
  21. expect(resolveE2ERuntimeRoot(e2eHomeDir)).toBe(join(e2eHomeDir, "ZCodeProject", ".zcode-e2e"));
  22. expect(resolveE2ERuntimePath("bg25", "release-child-bash")).toBe(
  23. join(resolveE2ERuntimeRoot(), "bg25", "release-child-bash"),
  24. );
  25. });
  26. it("uses portable absolute tool paths and workspace-relative shell paths", () => {
  27. expect(resolveE2EToolPath("bg25", "continued-work.txt")).toBe(
  28. resolveE2ERuntimePath("bg25", "continued-work.txt").replaceAll("\\", "/"),
  29. );
  30. expect(resolveE2EShellPath("bg25", "release-child-bash")).toBe(
  31. ".zcode-e2e/bg25/release-child-bash",
  32. );
  33. expect(E2E_RUNTIME_RELATIVE_ROOT).toBe(".zcode-e2e");
  34. expect(E2E_RUNTIME_ROOT_REPLAY_TOKEN).toBe("{{e2eRuntimeRoot}}");
  35. expect(createE2EReplayFixtureVariables(resolve("root", "fixture-home"))).toEqual({
  36. e2eRuntimeRoot: resolveE2ERuntimeRoot(resolve("root", "fixture-home")).replaceAll("\\", "/"),
  37. });
  38. });
  39. it("uses the existing E2E HOME precedence", () => {
  40. expect(
  41. resolveE2EHomeDir({
  42. cwd: resolve("root", "desktop"),
  43. env: { ZCODE_DATA_BASE_DIR: resolve("root", "data-base") },
  44. }),
  45. ).toBe(resolve("root", "data-base"));
  46. expect(
  47. resolveE2EHomeDir({
  48. cwd: resolve("root", "desktop"),
  49. env: {
  50. ZCODE_DATA_BASE_DIR: resolve("root", "data-base"),
  51. ZCODE_E2E_HOME_DIR: resolve("root", "explicit-home"),
  52. },
  53. }),
  54. ).toBe(resolve("root", "explicit-home"));
  55. });
  56. it("keeps the E2E CLI storage under the legacy .zcode directory", () => {
  57. const homeDir = resolve("root", "isolated-e2e-home");
  58. expect(resolveE2EStorageRoot(homeDir)).toBe(join(homeDir, ".zcode"));
  59. });
  60. it("does not allow raw system /tmp paths in Desktop E2E runtime sources", () => {
  61. const offenders = collectSourceFiles(E2E_DIR)
  62. .filter((path) => readFileSync(path, "utf-8").includes("/tmp"))
  63. .map((path) => relative(E2E_DIR, path).replaceAll("\\", "/"));
  64. expect(offenders).toEqual([]);
  65. });
  66. });
  67. function collectSourceFiles(root: string): string[] {
  68. return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
  69. const path = join(root, entry.name);
  70. if (entry.isDirectory()) {
  71. return collectSourceFiles(path);
  72. }
  73. const extension = entry.name.slice(entry.name.lastIndexOf("."));
  74. return SOURCE_EXTENSIONS.has(extension) ? [path] : [];
  75. });
  76. }