in-process-cua-runtime.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { describe, expect, it } from "vitest";
  2. import type { InProcessComputerUseBroker } from "@zcode/zcode-cua/broker/in-process";
  3. import { BROKER_SOCKET_ENV, IN_PROCESS_BROKER_SOCKET } from "@zcode/zcode-cua/broker/socketPath";
  4. import {
  5. ZCODE_CUA_BROKER_SOCKET_ENV_KEY,
  6. ZCODE_CUA_IN_PROCESS_BROKER_SOCKET,
  7. } from "@zcode/shared/runtime-env";
  8. import {
  9. captureComputerUseRuntimeFromEnvironment,
  10. createInProcessComputerUseRuntime,
  11. findSeededCuaAddon,
  12. } from "../src/server.js";
  13. const CONTEXT = { sessionId: "in-process-test", workspacePath: "/tmp" };
  14. function abortedSignal(): AbortSignal {
  15. const controller = new AbortController();
  16. controller.abort();
  17. return controller.signal;
  18. }
  19. describe("in-process Computer Use runtime", () => {
  20. it("agrees with the CLI on the sentinel the CLI mints", () => {
  21. expect(IN_PROCESS_BROKER_SOCKET).toBe(ZCODE_CUA_IN_PROCESS_BROKER_SOCKET);
  22. expect(BROKER_SOCKET_ENV).toBe(ZCODE_CUA_BROKER_SOCKET_ENV_KEY);
  23. });
  24. it("uses the in-process path only for the sentinel socket", () => {
  25. expect(captureComputerUseRuntimeFromEnvironment({})).toBeUndefined();
  26. expect(
  27. captureComputerUseRuntimeFromEnvironment({ ZCODE_CUA_PERMISSION_BROKER_SOCKET: "in-process" }),
  28. ).toBeDefined();
  29. });
  30. it("starts the broker on the first execute, not on construction or closeSession", async () => {
  31. const sockets: string[] = [];
  32. const runtime = createInProcessComputerUseRuntime(
  33. { ZCODE_CUA_PERMISSION_BROKER_SOCKET: "in-process" },
  34. async (socketPath, env) => {
  35. sockets.push(socketPath);
  36. expect(env.ZCODE_CUA_PERMISSION_BROKER_SOCKET).toBeUndefined();
  37. throw new Error("CUA native addon (ax_native.node) not found");
  38. },
  39. );
  40. await runtime.closeSession(CONTEXT);
  41. expect(sockets).toHaveLength(0);
  42. const call = () =>
  43. runtime.execute({ toolName: "list_apps", arguments: {}, context: CONTEXT, signal: abortedSignal() });
  44. await expect(call()).rejects.toThrow("(ax_native.node) not found");
  45. await expect(call()).rejects.toThrow("(ax_native.node) not found");
  46. expect(sockets).toHaveLength(2);
  47. expect(sockets[0]).toBe(sockets[1]);
  48. expect(sockets[0]).not.toBe("in-process");
  49. await runtime.dispose();
  50. });
  51. it("shares one start across concurrent calls and stops it on dispose", async () => {
  52. let starts = 0;
  53. let stops = 0;
  54. let resolveStart: (broker: InProcessComputerUseBroker) => void = () => undefined;
  55. const runtime = createInProcessComputerUseRuntime({}, () => {
  56. starts += 1;
  57. return new Promise((resolve) => {
  58. resolveStart = resolve;
  59. });
  60. });
  61. const call = () =>
  62. runtime
  63. .execute({ toolName: "list_apps", arguments: {}, context: CONTEXT, signal: abortedSignal() })
  64. .catch(() => undefined);
  65. void call();
  66. void call();
  67. const disposed = runtime.dispose();
  68. resolveStart({
  69. socketPath: "unused",
  70. stop: async () => {
  71. stops += 1;
  72. },
  73. });
  74. await disposed;
  75. expect(starts).toBe(1);
  76. expect(stops).toBe(1);
  77. });
  78. });
  79. describe("seeded Computer Use addon lookup", () => {
  80. it("returns the plugin-root addon only when it exists", async () => {
  81. const { mkdtemp, mkdir, writeFile, rm } = await import("node:fs/promises");
  82. const { tmpdir } = await import("node:os");
  83. const { join } = await import("node:path");
  84. const root = await mkdtemp(join(tmpdir(), "zcode-cua-seed-"));
  85. try {
  86. expect(await findSeededCuaAddon(undefined)).toBeUndefined();
  87. expect(await findSeededCuaAddon(root)).toBeUndefined();
  88. await mkdir(join(root, "native"));
  89. await writeFile(join(root, "native", "ax_native.node"), "addon");
  90. expect(await findSeededCuaAddon(root)).toBe(join(root, "native", "ax_native.node"));
  91. } finally {
  92. await rm(root, { force: true, recursive: true });
  93. }
  94. });
  95. });