subagentConfigProxy.test.ts 3.1 KB

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