subagentSnapshotWait.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { execFile } from "node:child_process";
  2. import { appendFile, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { fileURLToPath } from "node:url";
  6. import { promisify } from "node:util";
  7. import { afterEach, describe, expect, it, vi } from "vitest";
  8. import { stringifyHostLogArg } from "../src/host/hostLog.js";
  9. import { subagentUpdateCursor } from "./e2e/helpers/subagent-snapshot-wait.js";
  10. const runNode = promisify(execFile);
  11. const roots: string[] = [];
  12. afterEach(async () => {
  13. vi.unstubAllGlobals();
  14. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  15. });
  16. async function fixture() {
  17. const root = await mkdtemp(join(tmpdir(), "subagent-log-path-"));
  18. roots.push(root);
  19. return root;
  20. }
  21. const paths = [
  22. "/workspace/.zcode/agents/reviewer.md",
  23. String.raw`D:\workspace\.zcode\agents\reviewer.md`,
  24. String.raw`\\server\share\workspace\.zcode\agents\reviewer.md`,
  25. ];
  26. const message = (path: string, pid = 42) =>
  27. [
  28. `[subagentRuntimeConfig][pid:${pid}]`,
  29. "Subagent 配置文件更新完成",
  30. { path, changedPaths: [path], fallback: false },
  31. ]
  32. .map(stringifyHostLogArg)
  33. .join(" ");
  34. describe("subagent E2E log path matching", () => {
  35. it.each(paths)("waits for a new matching Host update: %s", async (path) => {
  36. const root = await fixture();
  37. const logPath = join(root, "host.log");
  38. await writeFile(logPath, message(path, 1) + "\n");
  39. let matched = false;
  40. vi.stubGlobal("browser", {
  41. electron: { execute: async () => root },
  42. waitUntil: async (predicate: () => Promise<boolean>) => {
  43. matched = await predicate();
  44. },
  45. });
  46. const updated = await subagentUpdateCursor();
  47. await updated([1], [path]);
  48. expect(matched).toBe(false);
  49. await appendFile(logPath, message(path) + "\n");
  50. await updated([43], [path]);
  51. expect(matched).toBe(false);
  52. await updated([42], [path]);
  53. expect(matched).toBe(true);
  54. await updated([42], ["reviewer.md"]);
  55. expect(matched).toBe(true);
  56. });
  57. it.each(paths)("records the preload observer's matching update: %s", async (path) => {
  58. const root = await fixture();
  59. await writeFile(join(root, "host-fault-control.json"), JSON.stringify({ tracePaths: [path] }));
  60. const entry = join(root, "entry.mjs");
  61. await writeFile(entry, "export {};\n");
  62. const preload = fileURLToPath(
  63. new URL("./e2e/helpers/subagent-host-faults.cjs", import.meta.url),
  64. );
  65. // 在独立 Node 子进程执行真实测试 preload,避免其文件边界替换污染单测进程。
  66. const { stdout } = await runNode(process.execPath, [
  67. "-e",
  68. `process.env.ZCODE_E2E_HOST_FAULT_ROOT = ${JSON.stringify(root)};
  69. process.parentPort = { postMessage() {} };
  70. process.argv[2] = ${JSON.stringify(entry)};
  71. require(${JSON.stringify(preload)});
  72. process.parentPort.postMessage(${JSON.stringify({ source: "host", message: message(path) })});
  73. process.stdout.write(String(process.pid));`,
  74. ]);
  75. const events = (await readFile(join(root, `host-boundary-${stdout}.jsonl`), "utf8"))
  76. .trim()
  77. .split("\n")
  78. .map((line) => JSON.parse(line));
  79. expect(events.filter((event) => event.kind === "snapshot-published")).toEqual([
  80. { pid: Number(stdout), kind: "snapshot-published", paths: [path], readCounts: {} },
  81. ]);
  82. });
  83. });