conversation-session-oversized-inline-image-attachment.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { join } from "node:path";
  2. import { DEFAULT_WORKSPACE, clearAppData } from "../../../helpers/desktop-app.js";
  3. import {
  4. assertUpstreamRequestCapture,
  5. waitForUpstreamNetworkCapture,
  6. } from "../../../helpers/upstream-capture.js";
  7. import { UPSTREAM_MODEL } from "../../../helpers/upstream-provider.js";
  8. import {
  9. E2E_REPLY_TOKEN,
  10. clickChatSend,
  11. countUpstreamRequestsContaining,
  12. getMessages,
  13. prepareConversationE2E,
  14. setComposerPrefill,
  15. waitForAssistantMessageContaining,
  16. waitForChatState,
  17. waitForComposerText,
  18. waitForUserMessageContaining,
  19. waitForVisibleAttachmentNames,
  20. writeOversizedComposerPrefillPngFixture,
  21. } from "../../../helpers/conversation-session.js";
  22. const OVERSIZED_INLINE_IMAGE_TIMEOUT_MS = 180000;
  23. const OVERSIZED_IMAGE_BYTES = 21 * 1024 * 1024;
  24. describe("会话区超大图片附件边界 E2E", () => {
  25. after(async () => {
  26. await clearAppData();
  27. });
  28. it("T04: 无 localPath 的超大图片点击发送后应保留草稿且不发 provider 请求", async function () {
  29. this.timeout(OVERSIZED_INLINE_IMAGE_TIMEOUT_MS);
  30. await prepareConversationE2E();
  31. const runId = Date.now();
  32. const marker = `E2E_OVERSIZED_INLINE_IMAGE_${runId}`;
  33. const filename = `oversized-inline-${runId}.png`;
  34. const prompt = `${marker}: this prompt must stay in composer because the inline image is too large.`;
  35. // 修复验证原因:工单根因是无本地路径图片会在 renderer 内联为 base64。
  36. // 这里只写入无内容的 21MiB image metadata,避免 E2E 为了造状态先解码大 base64。
  37. await setComposerPrefill(null, {
  38. attachmentFilenames: [filename],
  39. attachments: [
  40. {
  41. filename,
  42. kind: "image",
  43. mimeType: "image/png",
  44. sizeBytes: OVERSIZED_IMAGE_BYTES,
  45. },
  46. ],
  47. text: prompt,
  48. });
  49. await waitForComposerText(prompt, "无路径超大图片 case 没有恢复预填正文");
  50. await waitForVisibleAttachmentNames([filename]);
  51. const requestCountBefore = await countUpstreamRequestsContaining(marker);
  52. await clickChatSend();
  53. await waitForOversizedImageError(filename);
  54. await waitForComposerText(prompt, "无路径超大图片发送前拦截后没有保留正文");
  55. await waitForVisibleAttachmentNames([filename]);
  56. await browser.pause(2500);
  57. const requestCountAfter = await countUpstreamRequestsContaining(marker);
  58. expect(requestCountAfter).toBe(requestCountBefore);
  59. const userMessages = await getMessages("user");
  60. expect(userMessages.some((message) => message.text.includes(marker))).toBe(false);
  61. });
  62. it("T05: 有 localPath 的超大图片应以路径引用发送且不携带 dataBase64", async function () {
  63. this.timeout(OVERSIZED_INLINE_IMAGE_TIMEOUT_MS);
  64. await prepareConversationE2E();
  65. const runId = Date.now();
  66. const marker = `E2E_OVERSIZED_LOCAL_PATH_IMAGE_${runId}`;
  67. const filename = `oversized-local-path-${runId}.png`;
  68. const localPath = join(DEFAULT_WORKSPACE, filename);
  69. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  70. await writeOversizedComposerPrefillPngFixture(localPath, OVERSIZED_IMAGE_BYTES);
  71. // 修复验证原因:agent 会按磁盘 stat.size 判断本地图片是否内联。
  72. // 因此 T05 必须使用真实超过 20MiB 的文件,才能证明 provider 请求不会携带 data URL。
  73. await setComposerPrefill(null, {
  74. attachmentFilenames: [filename],
  75. attachments: [
  76. {
  77. filename,
  78. kind: "image",
  79. localPath,
  80. mimeType: "image/png",
  81. sizeBytes: OVERSIZED_IMAGE_BYTES,
  82. },
  83. ],
  84. text: prompt,
  85. });
  86. await waitForComposerText(prompt, "有路径超大图片 case 没有恢复预填正文");
  87. await waitForVisibleAttachmentNames([filename]);
  88. await clickChatSend();
  89. await waitForComposerText("", "有路径超大图片发送后输入框没有清空");
  90. await waitForUserMessageContaining(marker);
  91. const record = await waitForUpstreamNetworkCapture(marker);
  92. assertUpstreamRequestCapture(record, {
  93. expectedText: prompt,
  94. model: UPSTREAM_MODEL,
  95. });
  96. assertLocalPathImageAttachmentRequest(record.requestJson, { filename, localPath });
  97. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  98. await waitForChatState(
  99. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  100. "有路径超大图片发送完成后没有回到 idle",
  101. 90000,
  102. );
  103. });
  104. });
  105. function assertLocalPathImageAttachmentRequest(
  106. requestJson: unknown,
  107. expected: { filename: string; localPath: string },
  108. ) {
  109. expect(containsText(requestJson, expected.filename)).toBe(true);
  110. expect(containsText(requestJson, expected.localPath)).toBe(true);
  111. expect(containsObjectKey(requestJson, "dataBase64")).toBe(false);
  112. expect(containsText(requestJson, "data:image")).toBe(false);
  113. }
  114. function containsText(value: unknown, expected: string): boolean {
  115. if (typeof value === "string") {
  116. return value.includes(expected);
  117. }
  118. if (Array.isArray(value)) {
  119. return value.some((item) => containsText(item, expected));
  120. }
  121. if (!value || typeof value !== "object") {
  122. return false;
  123. }
  124. return Object.values(value).some((child) => containsText(child, expected));
  125. }
  126. function containsObjectKey(value: unknown, expectedKey: string): boolean {
  127. if (Array.isArray(value)) {
  128. return value.some((item) => containsObjectKey(item, expectedKey));
  129. }
  130. if (!value || typeof value !== "object") {
  131. return false;
  132. }
  133. const record = value as Record<string, unknown>;
  134. return (
  135. Object.prototype.hasOwnProperty.call(record, expectedKey) ||
  136. Object.values(record).some((child) => containsObjectKey(child, expectedKey))
  137. );
  138. }
  139. async function waitForOversizedImageError(filename: string) {
  140. let latestText = "";
  141. await browser.waitUntil(
  142. async () => {
  143. latestText = await getAttachmentErrorText();
  144. return latestText.includes(filename) && latestText.includes("20 MB");
  145. },
  146. {
  147. timeout: 10000,
  148. timeoutMsg: `没有显示超大图片附件错误; latest=${latestText}`,
  149. },
  150. );
  151. }
  152. function getAttachmentErrorText() {
  153. return browser.execute(() => {
  154. const candidates = Array.from(document.querySelectorAll<HTMLElement>("p"));
  155. return (
  156. candidates
  157. .map((element) => element.innerText.replace(/\u00a0/g, " ").trim())
  158. .find((text) => text.includes("20 MB")) ?? ""
  159. );
  160. });
  161. }