agentWiring.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { createReleaseAgentWiring, resolveBundledAgentWiring } from "../src/runtime/agentWiring.js";
  6. const temporaryDirs: string[] = [];
  7. async function makeTempDir(): Promise<string> {
  8. const dir = await mkdtemp(join(tmpdir(), "zcode-server-agent-wiring-"));
  9. temporaryDirs.push(dir);
  10. return dir;
  11. }
  12. afterEach(async () => {
  13. await Promise.all(temporaryDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  14. });
  15. describe("resolveBundledAgentWiring", () => {
  16. it("returns wiring for a bundled zcode.cjs next to the entry", async () => {
  17. const entryDir = await makeTempDir();
  18. const bundlePath = join(entryDir, "zcode.cjs");
  19. await writeFile(bundlePath, "// bundle\n", "utf8");
  20. const wiring = await resolveBundledAgentWiring(entryDir, {});
  21. expect(wiring).not.toBeNull();
  22. expect(wiring?.ZCODE_AGENT_SERVER_COMMAND).toBe(process.execPath);
  23. expect(JSON.parse(wiring?.ZCODE_AGENT_SERVER_ARGS_JSON ?? "[]")).toEqual([
  24. bundlePath,
  25. "app-server",
  26. "--stdio",
  27. ]);
  28. });
  29. it("keeps an explicit ZCODE_AGENT_SERVER_COMMAND untouched", async () => {
  30. const entryDir = await makeTempDir();
  31. await writeFile(join(entryDir, "zcode.cjs"), "// bundle\n", "utf8");
  32. const wiring = await resolveBundledAgentWiring(entryDir, {
  33. ZCODE_AGENT_SERVER_COMMAND: "/custom/agent",
  34. });
  35. expect(wiring).toBeNull();
  36. });
  37. it("returns null when no bundled zcode.cjs exists (dev workspace)", async () => {
  38. const entryDir = await makeTempDir();
  39. expect(await resolveBundledAgentWiring(entryDir, {})).toBeNull();
  40. });
  41. it("recomputes automatic wiring from each candidate release runtime", () => {
  42. const oldRuntime = "/releases/old/runtime";
  43. const newRuntime = "/releases/new/runtime";
  44. expect(createReleaseAgentWiring(oldRuntime, join(oldRuntime, "node"), {})).toEqual({
  45. ZCODE_AGENT_SERVER_COMMAND: join(oldRuntime, "node"),
  46. ZCODE_AGENT_SERVER_ARGS_JSON: JSON.stringify([join(oldRuntime, "zcode.cjs"), "app-server", "--stdio"]),
  47. });
  48. expect(createReleaseAgentWiring(newRuntime, join(newRuntime, "node"), {})).toEqual({
  49. ZCODE_AGENT_SERVER_COMMAND: join(newRuntime, "node"),
  50. ZCODE_AGENT_SERVER_ARGS_JSON: JSON.stringify([join(newRuntime, "zcode.cjs"), "app-server", "--stdio"]),
  51. });
  52. });
  53. it("does not replace an explicit agent command while computing release wiring", () => {
  54. expect(createReleaseAgentWiring("/release/runtime", "/release/runtime/node", {
  55. ZCODE_AGENT_SERVER_COMMAND: "/custom/agent",
  56. })).toBeNull();
  57. });
  58. });