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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData } from "#e2e/helpers/desktop-app";
  4. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  5. import {
  6. getUpstreamRequestEvidence,
  7. getUpstreamRequestRecordCount,
  8. } from "#e2e/helpers/conversation-session-network";
  9. import {
  10. clickV4Send,
  11. getV4ComposerAttachments,
  12. pickV4ComposerFiles,
  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. await pickV4ComposerFiles([path]);
  77. await browser.waitUntil(
  78. async () => {
  79. const attachments = await getV4ComposerAttachments();
  80. return attachments.some(
  81. (item) => item.text.includes(filename) && item.uploadStatus === "ready",
  82. );
  83. },
  84. { timeout: 30_000, timeoutMsg: "本地文本附件未 ready" },
  85. );
  86. if (testCase.prompt) await setV4ComposerText(testCase.prompt);
  87. const query = {
  88. includes: testCase.prompt ? [testCase.prompt] : [],
  89. excludes: ["CRITICAL: Respond with TEXT ONLY"],
  90. };
  91. const afterIndex = (await getUpstreamRequestRecordCount()) - 1;
  92. await clickV4Send();
  93. await browser.waitUntil(
  94. async () => (await getUpstreamRequestEvidence(query, { afterIndex })).length > 0,
  95. {
  96. timeout: 60_000,
  97. timeoutMsg: "附件发送未产生 provider 请求",
  98. },
  99. );
  100. // 抓取实际首个主请求,失败时直接显示缺失的正文,不能靠回复成功掩盖附件丢失。
  101. const [request] = await getUpstreamRequestEvidence(query, { afterIndex });
  102. const wire = JSON.stringify(request?.requestJson);
  103. expect({ marker: testCase.marker, present: wire.includes(testCase.marker) }).toEqual({
  104. marker: testCase.marker,
  105. present: true,
  106. });
  107. expect(wire).toContain(filename);
  108. const expectedLine = isGbk ? "中文".repeat(8) : LINE.trimEnd();
  109. expect(wire).toContain(`1\\t${isGbk ? ASCII_LINE : expectedLine}`);
  110. if (isGbk) expect(wire).toContain(`64\\t${ASCII_LINE}${expectedLine}`);
  111. expect(wire).toContain(`2000\\t${expectedLine}`);
  112. expect(wire).not.toContain("2001\\t");
  113. expect(wire).not.toContain("\uFFFD");
  114. if (testCase.prompt) expect(wire).toContain(testCase.prompt);
  115. await waitForV4TimelineContaining(REPLY, 60_000);
  116. await waitForV4Pane((snapshot) => !snapshot.canStop, "附件首发没有完成", 60_000);
  117. expect(await waitForV4RowAttachments(30_000, [filename])).toContain(filename);
  118. await browser.electron.restoreAllMocks();
  119. });
  120. }
  121. });