conversationShareMockRoute.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { createHash } from "node:crypto";
  2. import { createServer } from "node:http";
  3. import { once } from "node:events";
  4. import { expect, it, vi } from "vitest";
  5. import { createConversationShareMockRoute } from "#e2e/helpers/conversation-share-mock-route";
  6. it("分享 HTTP Mock 保留 multipart 文件字节和描述,校验真实上传摘要", async () => {
  7. const route = await 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. });
  71. it("分享夹具装载失败在服务器监听前暴露,不留到首个 HTTP 请求", async () => {
  72. const fixtureUrl = new URL(
  73. "../../services/test/helpers/conversationShareMockApiClient.ts",
  74. import.meta.url,
  75. ).href;
  76. vi.doMock(fixtureUrl, () => {
  77. throw new Error("fixture initialization failed");
  78. });
  79. try {
  80. await expect(
  81. Promise.resolve().then(() => createConversationShareMockRoute()),
  82. ).rejects.toMatchObject({
  83. cause: { message: "fixture initialization failed" },
  84. });
  85. } finally {
  86. vi.doUnmock(fixtureUrl);
  87. }
  88. });