zcodeProtocolV4AttachmentUpload.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. PROTOCOL_V4_LIMITS,
  4. V4_METHODS,
  5. v4AttachmentAbortParamsSchema,
  6. v4AttachmentBeginParamsSchema,
  7. v4AttachmentBeginResultSchema,
  8. v4AttachmentChunkParamsSchema,
  9. v4AttachmentCommitParamsSchema,
  10. v4AttachmentPreviewSourceParamsSchema,
  11. v4AttachmentPreviewSourceResultSchema,
  12. v4AttachmentReadParamsSchema,
  13. v4AttachmentReadResultSchema,
  14. v4ConversationAttachmentReadParamsSchema,
  15. v4ConversationAttachmentReadResultSchema,
  16. v4ConversationAttachmentStatParamsSchema,
  17. v4ConversationAttachmentStatResultSchema,
  18. } from "../src/zcode-protocol-v4/index.js";
  19. import { VIDEO_INPUT_MAX_BYTES } from "../src/zcode-media-policy.js";
  20. const sha256 = `sha256:${"a".repeat(64)}`;
  21. const VIDEO_INPUT_MAX_MIB = 30;
  22. describe("v4 chunked attachment protocol", () => {
  23. it("只暴露 begin/chunk/commit/abort production wire methods", () => {
  24. expect(V4_METHODS.attachmentBegin).toBe("v4/attachment/begin");
  25. expect(V4_METHODS.attachmentChunk).toBe("v4/attachment/chunk");
  26. expect(V4_METHODS.attachmentCommit).toBe("v4/attachment/commit");
  27. expect(V4_METHODS.attachmentAbort).toBe("v4/attachment/abort");
  28. expect("attachmentPut" in V4_METHODS).toBe(false);
  29. });
  30. it("冻结上传 staging 硬限额", () => {
  31. expect(PROTOCOL_V4_LIMITS.attachmentChunkMaxBytes).toBe(512 * 1024);
  32. expect(PROTOCOL_V4_LIMITS.attachmentUploadMaxChunks).toBe(64);
  33. expect(PROTOCOL_V4_LIMITS.attachmentUploadMaxConcurrent).toBe(16);
  34. expect(PROTOCOL_V4_LIMITS.attachmentUploadMaxStagedBytes).toBe(64 * 1024 * 1024);
  35. expect(PROTOCOL_V4_LIMITS.attachmentUploadTtlMs).toBe(5 * 60_000);
  36. });
  37. it("预览读取不扩张上传边界,并允许 video 使用 30MiB 全局输入上限", () => {
  38. expect(VIDEO_INPUT_MAX_BYTES).toBe(VIDEO_INPUT_MAX_MIB * 1024 * 1024);
  39. expect(PROTOCOL_V4_LIMITS.attachmentMaxBytes).toBe(20 * 1024 * 1024);
  40. expect(PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes).toBe(VIDEO_INPUT_MAX_BYTES);
  41. expect(PROTOCOL_V4_LIMITS.attachmentPreviewMaxChunks).toBe(
  42. VIDEO_INPUT_MAX_BYTES / PROTOCOL_V4_LIMITS.attachmentChunkMaxBytes,
  43. );
  44. expect(PROTOCOL_V4_LIMITS.attachmentReadCacheMaxBytes).toBe(VIDEO_INPUT_MAX_BYTES);
  45. expect(
  46. v4AttachmentReadResultSchema.parse({
  47. dataBase64: "eA==",
  48. mediaType: "video/mp4",
  49. totalBytes: PROTOCOL_V4_LIMITS.attachmentMaxBytes + 1,
  50. nextOffset: 1,
  51. }),
  52. ).toMatchObject({ totalBytes: PROTOCOL_V4_LIMITS.attachmentMaxBytes + 1 });
  53. expect(() =>
  54. v4AttachmentReadResultSchema.parse({
  55. dataBase64: "eA==",
  56. mediaType: "image/png",
  57. totalBytes: PROTOCOL_V4_LIMITS.attachmentMaxBytes + 1,
  58. nextOffset: 1,
  59. }),
  60. ).toThrow();
  61. expect(() =>
  62. v4AttachmentReadResultSchema.parse({
  63. dataBase64: "eA==",
  64. mediaType: "video/mp4",
  65. totalBytes: PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes + 1,
  66. nextOffset: 1,
  67. }),
  68. ).toThrow();
  69. });
  70. it("begin 校验 metadata/checksum,允许明确的 0-byte upload", () => {
  71. const base = {
  72. connectionId: "connection-1",
  73. uploadId: "upload-1",
  74. sessionId: "session-1",
  75. fileName: "shot.png",
  76. mime: "image/png",
  77. totalBytes: 0,
  78. totalChunks: 0,
  79. checksum: sha256,
  80. };
  81. expect(v4AttachmentBeginParamsSchema.parse(base)).toEqual(base);
  82. expect(() =>
  83. v4AttachmentBeginParamsSchema.parse({ ...base, totalBytes: 1, totalChunks: 0 }),
  84. ).toThrow();
  85. expect(() =>
  86. v4AttachmentBeginParamsSchema.parse({ ...base, checksum: "sha256:nope" }),
  87. ).toThrow();
  88. expect(() => v4AttachmentBeginParamsSchema.parse({ ...base, mime: "bad\r\nmime" })).toThrow();
  89. expect(() => v4AttachmentBeginParamsSchema.parse({ ...base, fileName: "bad\0name" })).toThrow();
  90. expect(() =>
  91. v4AttachmentBeginParamsSchema.parse({
  92. ...base,
  93. totalBytes: PROTOCOL_V4_LIMITS.attachmentMaxBytes + 1,
  94. totalChunks: 1,
  95. }),
  96. ).toThrow();
  97. });
  98. it("chunk 只接受合法 base64 且 decoded bytes 不超过 512KiB", () => {
  99. const base = {
  100. connectionId: "connection-1",
  101. uploadId: "upload-1",
  102. sessionId: "session-1",
  103. chunkIndex: 0,
  104. };
  105. expect(v4AttachmentChunkParamsSchema.parse({ ...base, dataBase64: "aGk=" })).toEqual({
  106. ...base,
  107. dataBase64: "aGk=",
  108. });
  109. expect(() => v4AttachmentChunkParamsSchema.parse({ ...base, dataBase64: "%%%=" })).toThrow();
  110. const oversized = Buffer.alloc(PROTOCOL_V4_LIMITS.attachmentChunkMaxBytes + 1).toString(
  111. "base64",
  112. );
  113. expect(() => v4AttachmentChunkParamsSchema.parse({ ...base, dataBase64: oversized })).toThrow();
  114. });
  115. it("result 与 terminal request 均为 strict schema", () => {
  116. expect(
  117. v4AttachmentBeginResultSchema.parse({
  118. uploadId: "upload-1",
  119. state: "committed",
  120. nextChunkIndex: 2,
  121. ref: "zcode-artifact://one",
  122. }),
  123. ).toEqual({
  124. uploadId: "upload-1",
  125. state: "committed",
  126. nextChunkIndex: 2,
  127. ref: "zcode-artifact://one",
  128. });
  129. const terminal = {
  130. connectionId: "connection-1",
  131. uploadId: "upload-1",
  132. sessionId: "session-1",
  133. };
  134. expect(v4AttachmentCommitParamsSchema.parse(terminal)).toEqual(terminal);
  135. expect(v4AttachmentAbortParamsSchema.parse(terminal)).toEqual(terminal);
  136. expect(() => v4AttachmentCommitParamsSchema.parse({ ...terminal, extra: true })).toThrow();
  137. });
  138. it("sent preview accepts exact image/video row targets and rejects other media", () => {
  139. const params = {
  140. sessionId: "session-1",
  141. ref: "/workspace/demo.mov",
  142. target: { rowId: 42, entityId: "message-42" },
  143. attachmentIndex: 1,
  144. offset: 0,
  145. limit: 1024,
  146. };
  147. expect(v4AttachmentReadParamsSchema.parse(params)).toEqual(params);
  148. expect(() =>
  149. v4AttachmentReadParamsSchema.parse({ ...params, attachmentIndex: undefined }),
  150. ).toThrow();
  151. expect(
  152. v4AttachmentReadResultSchema.parse({
  153. dataBase64: "AA==",
  154. mediaType: "video/quicktime",
  155. totalBytes: 1,
  156. nextOffset: null,
  157. }),
  158. ).toMatchObject({ mediaType: "video/quicktime" });
  159. expect(() =>
  160. v4AttachmentReadResultSchema.parse({
  161. dataBase64: "AA==",
  162. mediaType: "audio/mpeg",
  163. totalBytes: 1,
  164. nextOffset: null,
  165. }),
  166. ).toThrow();
  167. });
  168. it("share attachment read accepts non-media text/plain and requires row/index authority", () => {
  169. const params = {
  170. sessionId: "session-1",
  171. ref: "/Users/test/.zcode/tmp/paste-attachments/pasted.txt",
  172. target: { rowId: 42, entityId: "message-42" },
  173. attachmentIndex: 0,
  174. offset: 0,
  175. limit: 1024,
  176. };
  177. expect(V4_METHODS.conversationAttachmentRead).toBe("v4/conversation/attachmentRead");
  178. expect(v4ConversationAttachmentReadParamsSchema.parse(params)).toEqual(params);
  179. expect(
  180. v4ConversationAttachmentReadResultSchema.parse({
  181. dataBase64: "aGVsbG8=",
  182. mediaType: "text/plain",
  183. totalBytes: 5,
  184. nextOffset: null,
  185. }),
  186. ).toMatchObject({ mediaType: "text/plain" });
  187. });
  188. it("share attachment stat is metadata-only and keeps row/index authority", () => {
  189. const params = {
  190. sessionId: "session-1",
  191. ref: "/Users/test/.zcode/tmp/paste-attachments/pasted.txt",
  192. target: { rowId: 42, entityId: "message-42" },
  193. attachmentIndex: 0,
  194. };
  195. expect(V4_METHODS.conversationAttachmentStat).toBe("v4/conversation/attachmentStat");
  196. expect(v4ConversationAttachmentStatParamsSchema.parse(params)).toEqual(params);
  197. expect(
  198. v4ConversationAttachmentStatResultSchema.parse({
  199. mediaType: "text/plain",
  200. totalBytes: 5,
  201. mtimeMs: 7,
  202. }),
  203. ).toEqual({ mediaType: "text/plain", totalBytes: 5, mtimeMs: 7 });
  204. expect(() =>
  205. v4ConversationAttachmentStatParamsSchema.parse({ ...params, target: undefined }),
  206. ).toThrow();
  207. });
  208. it("stat 能表达超出传输上限的真实文件大小(容量超限才可能被判为阻断)", () => {
  209. // Bug 根因:stat 结果曾复用 30MiB 的预览上限,超大附件在 schema 就抛错,
  210. // 于是「已知容量超限」这个确定阻断被降级成 deferred 并静默丢内容。
  211. expect(PROTOCOL_V4_LIMITS.attachmentStatMaxBytes).toBeGreaterThan(
  212. PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes,
  213. );
  214. expect(
  215. v4ConversationAttachmentStatResultSchema.parse({
  216. mediaType: "video/mp4",
  217. totalBytes: PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes + 1,
  218. }),
  219. ).toMatchObject({ totalBytes: PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes + 1 });
  220. // 但 stat 仍有上界:超过它由 gateway 抛结构化 fault,而不是无声接受任意数字。
  221. expect(() =>
  222. v4ConversationAttachmentStatResultSchema.parse({
  223. mediaType: "video/mp4",
  224. totalBytes: PROTOCOL_V4_LIMITS.attachmentStatMaxBytes + 1,
  225. }),
  226. ).toThrow();
  227. // 搬运字节的读取通道边界不受影响。
  228. expect(() =>
  229. v4ConversationAttachmentReadResultSchema.parse({
  230. dataBase64: "eA==",
  231. mediaType: "video/mp4",
  232. totalBytes: PROTOCOL_V4_LIMITS.attachmentPreviewMaxBytes + 1,
  233. nextOffset: null,
  234. }),
  235. ).toThrow();
  236. });
  237. it("sent video local source is a strict desktop-only query result", () => {
  238. const params = {
  239. sessionId: "session-1",
  240. ref: "/workspace/demo.mov",
  241. target: { rowId: 42, entityId: "message-42" },
  242. attachmentIndex: 1,
  243. clientMode: "desktop-continuous",
  244. };
  245. expect(V4_METHODS.attachmentPreviewSource).toBe("v4/attachment/previewSource");
  246. expect(v4AttachmentPreviewSourceParamsSchema.parse(params)).toEqual(params);
  247. expect(() =>
  248. v4AttachmentPreviewSourceParamsSchema.parse({ ...params, attachmentIndex: undefined }),
  249. ).toThrow();
  250. expect(
  251. v4AttachmentPreviewSourceResultSchema.parse({
  252. kind: "local_path",
  253. path: "/workspace/demo.mov",
  254. mediaType: "video/quicktime",
  255. }),
  256. ).toMatchObject({ kind: "local_path", mediaType: "video/quicktime" });
  257. expect(v4AttachmentPreviewSourceResultSchema.parse({ kind: "chunked" })).toEqual({
  258. kind: "chunked",
  259. });
  260. expect(() =>
  261. v4AttachmentPreviewSourceResultSchema.parse({
  262. kind: "local_path",
  263. path: "/workspace/shot.png",
  264. mediaType: "image/png",
  265. }),
  266. ).toThrow();
  267. });
  268. });