topicResourcePeers.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { IChannel } from "@zcode/rpc";
  3. import type { IZCodeAgentService } from "../src/zcode-agent/zcodeAgent.js";
  4. import { createZCodeAgentConnectionScope } from "../src/zcode-agent/zcodeAgentConnectionScope.js";
  5. import { createTopicResourcePeers } from "../src/zcode-agent/topicResourcePeers.js";
  6. const request = {
  7. requestId: "r",
  8. taskId: "t",
  9. inputId: "i",
  10. authorizationId: "a",
  11. messageId: "m",
  12. resourceIndex: 0,
  13. workspacePath: "/work",
  14. workspaceIdentity: "server://host/work",
  15. remoteSessionId: "remote",
  16. };
  17. describe("shared Server topic resource peers", () => {
  18. it("binds only trusted V4 calls and rejects another live peer claiming the same route", async () => {
  19. const peers = createTopicResourcePeers();
  20. const channelA = { call: vi.fn() } as unknown as IChannel;
  21. const a = peers.attach(channelA);
  22. const b = peers.attach({ call: vi.fn() } as unknown as IChannel);
  23. const base = {
  24. queryConversationCommandsV4: vi.fn(async () => ({ results: [] })),
  25. } as unknown as IZCodeAgentService;
  26. const untrusted = a.wrapAgent(base);
  27. await untrusted.queryConversationCommandsV4({ ...request, commands: [] });
  28. expect(() => peers.getChannel(request)).toThrow();
  29. const scopeA = createZCodeAgentConnectionScope(untrusted, {
  30. connectionId: "a",
  31. clientMode: "desktop-continuous",
  32. role: "trusted-host-relay",
  33. });
  34. const scopeB = createZCodeAgentConnectionScope(b.wrapAgent(base), {
  35. connectionId: "b",
  36. clientMode: "desktop-continuous",
  37. role: "trusted-host-relay",
  38. });
  39. await scopeA.service.queryConversationCommandsV4({ ...request, commands: [] });
  40. expect(peers.getChannel(request)).toBe(channelA);
  41. await expect(
  42. scopeB.service.queryConversationCommandsV4({ ...request, commands: [] }),
  43. ).rejects.toThrow("another");
  44. a.dispose();
  45. expect(() => peers.getChannel(request)).toThrow();
  46. await scopeB.service.queryConversationCommandsV4({ ...request, commands: [] });
  47. expect(peers.getChannel(request)).not.toBe(channelA);
  48. await scopeA.dispose();
  49. await scopeB.dispose();
  50. b.dispose();
  51. });
  52. });