windowTopicResourceRelay.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, expect, it, vi } from "vitest";
  2. import { ServiceCollection, type IBotsService } from "@zcode/services";
  3. import { createWindowTopicResourceRelay } from "../src/host/windowTopicResourceRelay.js";
  4. const request = {
  5. requestId: "r",
  6. taskId: "t",
  7. inputId: "i",
  8. authorizationId: "a",
  9. messageId: "m",
  10. resourceIndex: 0,
  11. workspacePath: "/work",
  12. workspaceIdentity: "ssh://host/work",
  13. remoteSessionId: "remote",
  14. };
  15. describe("window topic resource authority", () => {
  16. it("binds channel sends to the physical remote connection", async () => {
  17. const peer = new ServiceCollection();
  18. const other = new ServiceCollection();
  19. let current = peer;
  20. const reply = vi.fn(async () => ({
  21. status: "sent",
  22. deliveryId: "d",
  23. providerMessageId: "om_sent",
  24. }));
  25. const relay = createWindowTopicResourceRelay({
  26. getPeerServices: () => peer,
  27. resolveServices: () => current,
  28. getBots: () =>
  29. ({
  30. readTopicResource: vi.fn(),
  31. validateTopicResource: vi.fn(),
  32. replyToChannel: reply,
  33. }) as unknown as IBotsService,
  34. });
  35. const value = {
  36. taskId: "t",
  37. inputId: "i",
  38. authorizationId: "a",
  39. toolCallId: "call",
  40. parts: [{ type: "mention", refId: "m1" }],
  41. workspacePath: "/work",
  42. workspaceIdentity: "ssh://host/work",
  43. remoteSessionId: "remote",
  44. trace: { traceId: "trace" },
  45. };
  46. expect(await relay.channel.call("host", "reply", value)).toMatchObject({ status: "sent" });
  47. expect(reply).toHaveBeenCalledWith(value);
  48. current = other;
  49. await expect(relay.channel.call("host", "reply", value)).rejects.toThrow("connection");
  50. expect(reply).toHaveBeenCalledTimes(1);
  51. relay.dispose();
  52. });
  53. it("rejects a request resolved to another remote connection before Bot IO", async () => {
  54. const read = vi.fn();
  55. const validate = vi.fn();
  56. const peer = new ServiceCollection();
  57. const other = new ServiceCollection();
  58. const relay = createWindowTopicResourceRelay({
  59. getPeerServices: () => peer,
  60. resolveServices: () => other,
  61. getBots: () =>
  62. ({ readTopicResource: read, validateTopicResource: validate }) as unknown as IBotsService,
  63. });
  64. await expect(relay.channel.call("host", "begin", request)).rejects.toThrow("connection");
  65. expect(read).not.toHaveBeenCalled();
  66. expect(validate).not.toHaveBeenCalled();
  67. relay.dispose();
  68. });
  69. it("uses the current logical session and rechecks it after download", async () => {
  70. const peer = new ServiceCollection();
  71. let online = true;
  72. const resolve = vi.fn(() => {
  73. if (!online) throw new Error("Disconnected");
  74. return peer;
  75. });
  76. const relay = createWindowTopicResourceRelay({
  77. getPeerServices: () => peer,
  78. resolveServices: resolve,
  79. getBots: () =>
  80. ({
  81. validateTopicResource: async () => undefined,
  82. readTopicResource: async () => {
  83. online = false;
  84. return { kind: "file", filename: "a.txt", mimeType: "text/plain", dataBase64: "aGk=" };
  85. },
  86. }) as unknown as IBotsService,
  87. });
  88. await expect(relay.channel.call("host", "begin", request)).rejects.toThrow("Disconnected");
  89. expect(resolve).toHaveBeenCalledWith({
  90. kind: "remote",
  91. remoteSessionId: "remote",
  92. workspaceIdentity: "ssh://host/work",
  93. workspacePath: "/work",
  94. });
  95. relay.dispose();
  96. });
  97. });