zcodeAgentService.subagentRuntimeConfig.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import {
  3. subagentRuntimeConfigSchema,
  4. zcodeProtocolMethods,
  5. type SubagentRuntimeConfig,
  6. } from "@zcode/shared";
  7. import { offPeakSelectionView } from "./fixtures/offPeakSelection.js";
  8. afterEach(() => {
  9. vi.doUnmock("../src/zcode-agent/zcodeAgentProcessManager.js");
  10. vi.resetModules();
  11. });
  12. describe("subagent runtime config reverse RPC", () => {
  13. it("routes by the bound workspace and validates both request and complete snapshot", async () => {
  14. const handlers: Array<(request: { id: string; method: string; params?: unknown }) => void> = [];
  15. const request = (id: string, params: unknown) =>
  16. handlers.forEach((handler) =>
  17. handler({ id, method: zcodeProtocolMethods.subagentsReadRuntimeConfig, params }),
  18. );
  19. const respond = vi.fn(async () => {}),
  20. respondError = vi.fn(async () => {});
  21. vi.doMock("../src/zcode-agent/zcodeAgentProcessManager.js", () => ({
  22. ZCodeAgentProcessManager: class {
  23. onRuntimeLifecycle() {
  24. return { dispose() {} };
  25. }
  26. onRuntimeRestarted() {
  27. return { dispose() {} };
  28. }
  29. markReady() {}
  30. disposeAll() {}
  31. async getClient() {
  32. return {
  33. request: async (_method: string, params: unknown) => params,
  34. respond,
  35. respondError,
  36. transportKind: "stdio",
  37. onNotification: () => ({ dispose() {} }),
  38. onClose: () => ({ dispose() {} }),
  39. onRequest: (handler: (typeof handlers)[number]) => {
  40. handlers.push(handler);
  41. return { dispose() {} };
  42. },
  43. };
  44. }
  45. },
  46. }));
  47. const configuration = {
  48. kind: "ready" as const,
  49. profiles: [
  50. {
  51. path: "/user/agents/reviewer.md",
  52. source: "user" as const,
  53. name: "reviewer",
  54. description: "Review",
  55. systemPrompt: "Prompt",
  56. },
  57. ],
  58. builtInModelSelectionOverrides: {},
  59. pluginAgentModelSelectionOverrides: {},
  60. };
  61. const pending = Promise.withResolvers<typeof configuration>();
  62. const read = vi
  63. .fn(async (): Promise<SubagentRuntimeConfig> => configuration)
  64. .mockReturnValueOnce(pending.promise);
  65. const { createZCodeAgentService } = await import("../src/zcode-agent/zcodeAgentService.js");
  66. const service = createZCodeAgentService({
  67. readSubagentRuntimeConfig: read,
  68. modelSelectionReadinessSource: { getView: async () => offPeakSelectionView() },
  69. });
  70. const workspace = { workspacePath: "/workspace/a", workspaceIdentity: "test:a" };
  71. try {
  72. await service.initialize(workspace);
  73. request("valid", { sessionId: "session-a" });
  74. await vi.waitFor(() => expect(read).toHaveBeenCalledTimes(1));
  75. expect(respond).not.toHaveBeenCalled();
  76. pending.resolve(configuration);
  77. await vi.waitFor(() => expect(respond).toHaveBeenCalledWith("valid", configuration));
  78. expect(read).toHaveBeenCalledWith(expect.objectContaining(workspace));
  79. request("invalid", { sessionId: "session-a", workspacePath: "/workspace/b" });
  80. await vi.waitFor(() =>
  81. expect(respondError).toHaveBeenCalledWith(
  82. "invalid",
  83. expect.objectContaining({ code: -32602 }),
  84. ),
  85. );
  86. expect(read).toHaveBeenCalledTimes(1);
  87. read.mockResolvedValueOnce({ kind: "built-in-fallback" });
  88. request("fallback", { sessionId: "session-a" });
  89. await vi.waitFor(() =>
  90. expect(respond).toHaveBeenCalledWith("fallback", { kind: "built-in-fallback" }),
  91. );
  92. read.mockImplementationOnce(async () => {
  93. throw new Error("not prepared");
  94. });
  95. request("unavailable", { sessionId: "session-b" });
  96. await vi.waitFor(() =>
  97. expect(respondError).toHaveBeenCalledWith(
  98. "unavailable",
  99. expect.objectContaining({ code: -32603 }),
  100. ),
  101. );
  102. expect(
  103. subagentRuntimeConfigSchema.safeParse({
  104. documents: [{ name: "summary only" }],
  105. kind: "ready",
  106. }).success,
  107. ).toBe(false);
  108. } finally {
  109. service.disposeAll();
  110. }
  111. });
  112. });