conversation-session-large-text-attachment.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { TID_CHAT_ATTACHMENT_BUTTON, TID_CHAT_ATTACHMENT_MENU_ITEM } from "@zcode/shared/test-ids";
  4. import { clearAppData } from "#e2e/helpers/desktop-app";
  5. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  6. import {
  7. getUpstreamRequestEvidence,
  8. getUpstreamRequestRecordCount,
  9. } from "#e2e/helpers/conversation-session-network";
  10. import {
  11. clickV4Send,
  12. getV4ComposerAttachments,
  13. prepareV4ConversationE2E,
  14. setV4ComposerText,
  15. waitForV4Pane,
  16. waitForV4RowAttachments,
  17. waitForV4TimelineContaining,
  18. } from "#e2e/helpers/v4-conversation";
  19. const FILE_BYTES = 16 * 1024 * 1024;
  20. const LINE = "这是一个合法的中文文本附件。\n";
  21. const GBK_LINE = Buffer.from("d6d0cec4".repeat(8) + "0a", "hex");
  22. const ASCII_LINE = "a".repeat(63);
  23. const REPLY = "LARGE_TEXT_ATTACHMENT_OK";
  24. const CASES = [
  25. { name: "only", prompt: "", marker: "E2E_LARGE_TEXT_ONLY_CONTENT", encoding: "utf8" },
  26. {
  27. name: "with-query",
  28. prompt: "E2E_LARGE_TEXT_QUERY 请读取附件。",
  29. marker: "E2E_LARGE_TEXT_QUERY_CONTENT",
  30. encoding: "utf8",
  31. },
  32. {
  33. name: "gbk",
  34. prompt: "E2E_LARGE_TEXT_GBK 请读取附件。",
  35. marker: "E2E_LARGE_TEXT_GBK_CONTENT",
  36. encoding: "gbk",
  37. },
  38. ];
  39. describe("T18:大文本附件编码采样边界", () => {
  40. const directory = resolveE2ERuntimePath("large-text-attachment");
  41. after(async () => {
  42. await browser.electron.restoreAllMocks();
  43. await rm(directory, { recursive: true, force: true });
  44. await clearAppData();
  45. });
  46. for (const testCase of CASES) {
  47. it(`T18 ${testCase.name}: provider 收到正确正文且限于前 2000 行`, async function () {
  48. this.timeout(180_000);
  49. await prepareV4ConversationE2E();
  50. await mkdir(directory, { recursive: true });
  51. const filename = `e2e-large-text-${testCase.name}.txt`;
  52. const path = join(directory, filename);
  53. // Bug 根因:4096 字节样本在中文字符中间结束,旧编码探测拒读后附件正文被过滤。
  54. // marker 位于样本之后且在前 2000 行以内,避免 marker 改变触发问题的字节边界。
  55. const isGbk = testCase.encoding === "gbk";
  56. const line = isGbk ? GBK_LINE : Buffer.from(LINE);
  57. // GBK 的 D6 | D0 位于采样边界;D6 单独是未完成 UTF-8 候选,补读 D0 后必须排除它。
  58. const prefix = isGbk
  59. ? Buffer.concat([Buffer.from(`${ASCII_LINE}\n`.repeat(63) + ASCII_LINE), GBK_LINE])
  60. : Buffer.from(LINE.repeat(200));
  61. const bytes = Buffer.concat([
  62. prefix,
  63. Buffer.from(`${testCase.marker}\n`),
  64. Buffer.alloc(Math.ceil(FILE_BYTES / line.length) * line.length).fill(line),
  65. ]);
  66. expect(bytes.byteLength).toBeGreaterThan(FILE_BYTES);
  67. if (isGbk) {
  68. expect(Array.from(bytes.subarray(4095, 4099))).toEqual([0xd6, 0xd0, 0xce, 0xc4]);
  69. } else {
  70. expect(() => new TextDecoder("utf-8", { fatal: true }).decode(bytes)).not.toThrow();
  71. }
  72. expect(() =>
  73. new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(0, 4096)),
  74. ).toThrow();
  75. await writeFile(path, bytes);
  76. const picker = await browser.electron.mock("dialog", "showOpenDialog");
  77. await picker.mockResolvedValueOnce({ canceled: false, filePaths: [path] });
  78. const trigger = await $(`[data-testid="${TID_CHAT_ATTACHMENT_BUTTON}"]`);
  79. await trigger.waitForClickable();
  80. await trigger.click();
  81. const menu = await $(`[data-testid="${TID_CHAT_ATTACHMENT_MENU_ITEM}"]`);
  82. await menu.waitForClickable();
  83. await menu.click();
  84. await browser.waitUntil(
  85. async () => {
  86. const attachments = await getV4ComposerAttachments();
  87. return attachments.some(
  88. (item) => item.text.includes(filename) && item.uploadStatus === "ready",
  89. );
  90. },
  91. { timeout: 30_000, timeoutMsg: "本地文本附件未 ready" },
  92. );
  93. if (testCase.prompt) await setV4ComposerText(testCase.prompt);
  94. const query = {
  95. includes: testCase.prompt ? [testCase.prompt] : [],
  96. excludes: ["CRITICAL: Respond with TEXT ONLY"],
  97. };
  98. const afterIndex = (await getUpstreamRequestRecordCount()) - 1;
  99. await clickV4Send();
  100. await browser.waitUntil(
  101. async () => (await getUpstreamRequestEvidence(query, { afterIndex })).length > 0,
  102. {
  103. timeout: 60_000,
  104. timeoutMsg: "附件发送未产生 provider 请求",
  105. },
  106. );
  107. // 抓取实际首个主请求,失败时直接显示缺失的正文,不能靠回复成功掩盖附件丢失。
  108. const [request] = await getUpstreamRequestEvidence(query, { afterIndex });
  109. const wire = JSON.stringify(request?.requestJson);
  110. expect({ marker: testCase.marker, present: wire.includes(testCase.marker) }).toEqual({
  111. marker: testCase.marker,
  112. present: true,
  113. });
  114. expect(wire).toContain(filename);
  115. const expectedLine = isGbk ? "中文".repeat(8) : LINE.trimEnd();
  116. expect(wire).toContain(`1\\t${isGbk ? ASCII_LINE : expectedLine}`);
  117. if (isGbk) expect(wire).toContain(`64\\t${ASCII_LINE}${expectedLine}`);
  118. expect(wire).toContain(`2000\\t${expectedLine}`);
  119. expect(wire).not.toContain("2001\\t");
  120. expect(wire).not.toContain("\uFFFD");
  121. if (testCase.prompt) expect(wire).toContain(testCase.prompt);
  122. await waitForV4TimelineContaining(REPLY, 60_000);
  123. await waitForV4Pane((snapshot) => !snapshot.canStop, "附件首发没有完成", 60_000);
  124. expect(await waitForV4RowAttachments(30_000, [filename])).toContain(filename);
  125. await browser.electron.restoreAllMocks();
  126. });
  127. }
  128. });