topicResourceBridge.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createTopicResourceBridge } from "../src/zcode-agent/topicResourceBridge.js";
  3. const request = {
  4. requestId: "request-a",
  5. taskId: "task-a",
  6. inputId: "input-a",
  7. authorizationId: "auth-a",
  8. messageId: "file-a",
  9. resourceIndex: 0,
  10. };
  11. describe("topic resource reverse bridge", () => {
  12. it("withholds the reference when cancelled during the final authorization check", async () => {
  13. const bridge = createTopicResourceBridge({
  14. read: async () => ({
  15. kind: "file",
  16. filename: "a.txt",
  17. mimeType: "text/plain",
  18. sizeBytes: 0,
  19. dataBase64: "",
  20. }),
  21. upload: async () => ({
  22. ref: "artifact://a",
  23. fileName: "a.txt",
  24. mime: "text/plain",
  25. bytes: 0,
  26. }),
  27. validate: async () => {
  28. bridge.cancel(request);
  29. },
  30. });
  31. await expect(bridge.read(request)).rejects.toThrow("cancelled");
  32. bridge.dispose();
  33. });
  34. it("withholds the reference when authorization expires during upload", async () => {
  35. const bridge = createTopicResourceBridge({
  36. read: async () => ({
  37. kind: "file",
  38. filename: "a.txt",
  39. mimeType: "text/plain",
  40. sizeBytes: 0,
  41. dataBase64: "",
  42. }),
  43. upload: async () => ({
  44. ref: "artifact://a",
  45. fileName: "a.txt",
  46. mime: "text/plain",
  47. bytes: 0,
  48. }),
  49. validate: async () => {
  50. throw new Error("Group disabled");
  51. },
  52. });
  53. await expect(bridge.read(request)).rejects.toThrow("Group disabled");
  54. bridge.dispose();
  55. });
  56. it("uploads only after an authorized download", async () => {
  57. const attachment = {
  58. kind: "file" as const,
  59. filename: "a.txt",
  60. mimeType: "text/plain",
  61. dataBase64: "aGk=",
  62. sizeBytes: 2,
  63. };
  64. const read = vi.fn(async () => attachment);
  65. const upload = vi.fn(async () => ({
  66. ref: "artifact://a",
  67. fileName: "a.txt",
  68. mime: "text/plain",
  69. bytes: 2,
  70. }));
  71. const bridge = createTopicResourceBridge({ read, upload, validate: async () => undefined });
  72. expect(await bridge.read(request)).toMatchObject({ ref: "artifact://a" });
  73. expect(upload).toHaveBeenCalledWith(request, attachment, expect.any(AbortSignal), undefined);
  74. bridge.dispose();
  75. });
  76. it("rejects scope injection before any download", async () => {
  77. const read = vi.fn();
  78. const upload = vi.fn();
  79. const bridge = createTopicResourceBridge({ read, upload, validate: async () => undefined });
  80. await expect(bridge.read({ ...request, workspacePath: "/other" })).rejects.toThrow();
  81. expect(read).not.toHaveBeenCalled();
  82. bridge.dispose();
  83. });
  84. it("cancels the matching request without uploading downloaded bytes", async () => {
  85. let release!: (value: {
  86. kind: "file";
  87. filename: string;
  88. mimeType: string;
  89. sizeBytes: number;
  90. dataBase64: string;
  91. }) => void;
  92. const upload = vi.fn();
  93. const bridge = createTopicResourceBridge({
  94. validate: async () => undefined,
  95. read: () =>
  96. new Promise((resolve) => {
  97. release = resolve;
  98. }),
  99. upload,
  100. });
  101. const result = bridge.read(request);
  102. expect(bridge.cancel({ ...request, taskId: "other" })).toBe(false);
  103. expect(bridge.cancel(request)).toBe(true);
  104. release({
  105. kind: "file",
  106. filename: "a.txt",
  107. mimeType: "text/plain",
  108. sizeBytes: 0,
  109. dataBase64: "",
  110. });
  111. await expect(result).rejects.toThrow();
  112. expect(upload).not.toHaveBeenCalled();
  113. bridge.dispose();
  114. });
  115. });