conversationShareMockRoute.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { createHash } from "node:crypto";
  2. import { createServer } from "node:http";
  3. import { once } from "node:events";
  4. import { expect, it } from "vitest";
  5. import { createConversationShareMockRoute } from "./e2e/helpers/conversation-share-mock-route.js";
  6. it("分享 HTTP Mock 保留 multipart 文件字节和描述,校验真实上传摘要", async () => {
  7. const route = createConversationShareMockRoute();
  8. const server = createServer((request, response) => {
  9. void route(request, response).catch((error) => {
  10. response.writeHead(500);
  11. response.end(String(error));
  12. });
  13. });
  14. server.listen(0, "127.0.0.1");
  15. await once(server, "listening");
  16. const address = server.address();
  17. if (!address || typeof address === "string") throw new Error("missing test server address");
  18. const base = `http://127.0.0.1:${address.port}/api/v1/shares`;
  19. const headers = { Authorization: "Bearer test-owner" };
  20. try {
  21. const preparationResponse = await fetch(`${base}/preparations`, {
  22. method: "POST",
  23. headers: { ...headers, "Content-Type": "application/json" },
  24. body: JSON.stringify({
  25. client_request_id: "multipart-regression",
  26. title: "PDF 分享",
  27. schema_version: 1,
  28. access_mode: "public_readonly",
  29. payload_sha256: "a".repeat(64),
  30. artifact_count: 1,
  31. }),
  32. });
  33. expect(preparationResponse.status).toBe(200);
  34. const preparation = (await preparationResponse.json()) as {
  35. data: { preparation_id: string };
  36. };
  37. // 非 UTF-8 字节用于证明 HTTP 适配器没有先解码字符串再重新编码文件。
  38. const bytes = new Uint8Array([37, 80, 68, 70, 255, 0, 128, 10]);
  39. const descriptor = {
  40. artifact_id: "share-artifact-1",
  41. logical_artifact_key: "share-artifact-key-1",
  42. producer_product_turn_id: "share-product-turn-1",
  43. artifact_version: 1,
  44. state: "current",
  45. ref: "zcode-artifact://share/share-artifact-1",
  46. artifact_type: "pdf",
  47. display_name: "报告.pdf",
  48. extension: "pdf",
  49. mime_type: "application/pdf",
  50. size_bytes: bytes.length,
  51. sha256: createHash("sha256").update(bytes).digest("hex"),
  52. };
  53. const form = new FormData();
  54. form.set("descriptor", JSON.stringify(descriptor));
  55. form.set("file", new Blob([bytes], { type: "application/pdf" }), "报告.pdf");
  56. const uploaded = await fetch(
  57. `${base}/preparations/${preparation.data.preparation_id}/artifacts`,
  58. { method: "POST", headers, body: form },
  59. );
  60. const result = await uploaded.json();
  61. expect({ status: uploaded.status, result }).toMatchObject({
  62. status: 200,
  63. result: { data: { artifact_id: descriptor.artifact_id, sha256: descriptor.sha256 } },
  64. });
  65. } finally {
  66. await new Promise<void>((resolve, reject) =>
  67. server.close((error) => (error ? reject(error) : resolve())),
  68. );
  69. }
  70. });