topicResourceRelayChannel.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { ChannelClient, ChannelServer, createQueuePair } from "@zcode/rpc";
  2. import { TOPIC_RESOURCE_RELAY_CHANNEL } from "@zcode/shared";
  3. import { describe, expect, it } from "vitest";
  4. import { createTopicResourceRelayChannel } from "../src/zcode-agent/topicResourceRelayChannel.js";
  5. import { readTopicResourceFromRelay } from "../src/zcode-agent/topicResourceRelayClient.js";
  6. describe("private topic resource reverse RPC", () => {
  7. it("transfers through a private channel while enforcing the owning workspace", async () => {
  8. const [hostProtocol, remoteProtocol] = createQueuePair();
  9. const host = new ChannelServer(hostProtocol, "host");
  10. const remote = new ChannelClient(remoteProtocol);
  11. const bridge = createTopicResourceRelayChannel({
  12. validate: async (request) => {
  13. if (request.workspaceIdentity !== "ssh://owner/work") throw new Error("Foreign workspace");
  14. },
  15. read: async () => ({
  16. kind: "file",
  17. filename: "a.txt",
  18. mimeType: "text/plain",
  19. dataBase64: "aGk=",
  20. }),
  21. });
  22. host.registerChannel(TOPIC_RESOURCE_RELAY_CHANNEL, bridge.channel);
  23. const channel = remote.getChannel(TOPIC_RESOURCE_RELAY_CHANNEL);
  24. const request = {
  25. requestId: "request",
  26. taskId: "task",
  27. inputId: "input",
  28. authorizationId: "auth",
  29. messageId: "message",
  30. resourceIndex: 0,
  31. workspacePath: "/work",
  32. workspaceIdentity: "ssh://owner/work",
  33. remoteSessionId: "remote",
  34. };
  35. try {
  36. expect(
  37. await readTopicResourceFromRelay(channel, request, new AbortController().signal),
  38. ).toMatchObject({ dataBase64: "aGk=" });
  39. await expect(
  40. channel.call("validate", { ...request, workspaceIdentity: "ssh://other/work" }),
  41. ).rejects.toThrow("Foreign workspace");
  42. await expect(channel.call("arbitrary-method", request)).rejects.toThrow();
  43. } finally {
  44. bridge.dispose();
  45. host.dispose();
  46. remote.dispose();
  47. }
  48. });
  49. });