subagentConfigProxy.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { spawn } from "node:child_process";
  2. import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { createInterface } from "node:readline";
  6. import { afterEach, expect, it } from "vitest";
  7. const roots: string[] = [];
  8. afterEach(async () => {
  9. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  10. });
  11. it.each([false, true])(
  12. "preloads the stdio proxy without replacing CLI startup (failure: %s)",
  13. async (failed) => {
  14. const root = await mkdtemp(join(tmpdir(), "subagent-proxy-"));
  15. roots.push(root);
  16. await writeFile(
  17. join(root, "subagent-config-control.json"),
  18. JSON.stringify({ failedSessionIds: failed ? ["parent"] : [] }),
  19. );
  20. const entry = join(root, "cli.mjs");
  21. await writeFile(
  22. entry,
  23. `
  24. import { createInterface } from "node:readline";
  25. console.log(JSON.stringify({ method: "storage/startup", params: { ready: true } }));
  26. console.log(JSON.stringify({ id: 1, method: "subagents/readRuntimeConfig", params: { sessionId: "parent" } }));
  27. createInterface({ input: process.stdin }).once("line", line => {
  28. console.log(JSON.stringify({ method: "done", response: JSON.parse(line), args: process.argv.slice(2) }));
  29. process.exit(0);
  30. });
  31. `,
  32. );
  33. // 与 WDIO 注入方式一致:Windows 的 ESM loader 不接受裸绝对路径,预加载必须是 file:// URL。
  34. const proxy = new URL("./e2e/helpers/subagent-config-proxy.mjs", import.meta.url).href;
  35. const child = spawn(process.execPath, [entry, "app-server", "--stdio"], {
  36. env: {
  37. ...process.env,
  38. NODE_OPTIONS: `--import=${JSON.stringify(proxy)}`,
  39. ZCODE_E2E_ARTIFACT_DIR: root,
  40. },
  41. stdio: ["pipe", "pipe", "pipe"],
  42. });
  43. const frames: Array<Record<string, any>> = [];
  44. let stderr = "";
  45. child.stderr.on("data", (chunk) => {
  46. stderr += chunk;
  47. });
  48. createInterface({ input: child.stdout }).on("line", (line) => {
  49. const frame = JSON.parse(line);
  50. frames.push(frame);
  51. if (frame.method === "subagents/readRuntimeConfig")
  52. child.stdin.write(
  53. JSON.stringify({ id: frame.id, result: { kind: "built-in-fallback" } }) + "\n",
  54. );
  55. });
  56. const code = await new Promise<number | null>((resolve, reject) => {
  57. child.once("error", reject);
  58. child.once("exit", resolve);
  59. });
  60. expect(code, stderr).toBe(0);
  61. expect(frames.filter((frame) => frame.method === "storage/startup")).toHaveLength(1);
  62. expect(frames.at(-1)).toMatchObject({
  63. method: "done",
  64. args: ["app-server", "--stdio"],
  65. response: failed
  66. ? { error: { message: "E2E_CONFIG_UNAVAILABLE" } }
  67. : { result: { kind: "built-in-fallback" } },
  68. });
  69. const logs = (await readdir(root)).filter((path) => path.startsWith("subagent-config-rpc-"));
  70. expect(logs).toHaveLength(1);
  71. const records = (await readFile(join(root, logs[0]!), "utf8"))
  72. .trim()
  73. .split("\n")
  74. .map((line) => JSON.parse(line));
  75. expect(records[0]).toMatchObject({ kind: "read", sessionId: "parent", failed });
  76. },
  77. );