conversation-session-attachment-only-wire-padding.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // T06W:附件-only query 的 UI/session 事实保持空正文,只在 provider wire 边界补固定 (no content)。
  2. // 证据层 L3:真实 paste → begin/chunk/commit → draft firstInput → Anthropic Messages request matcher。
  3. import { TID_V4_COMPOSER_INPUT, TID_V4_ROW_ATTACHMENTS } from "@zcode/shared/test-ids";
  4. import { clearAppData } from "#e2e/helpers/desktop-app";
  5. import { waitForUpstreamNetworkCapture } from "#e2e/helpers/upstream-capture";
  6. import {
  7. clickV4Send,
  8. prepareV4ConversationE2E,
  9. waitForV4Pane,
  10. waitForV4RowAttachments,
  11. waitForV4TimelineContaining,
  12. } from "#e2e/helpers/v4-conversation";
  13. const CASE_TIMEOUT_MS = 120_000;
  14. const FILENAME = "e2e-attachment-wire-padding.txt";
  15. const ATTACHMENT_CONTENT = "E2E_ATTACHMENT_WIRE_CONTEXT";
  16. const REPLY_MARKER = "ATTACHMENT_WIRE_PADDING_OK";
  17. describe("T06W:attachment-only provider wire padding", () => {
  18. after(async () => {
  19. await browser.electron.restoreAllMocks();
  20. await clearAppData();
  21. });
  22. it("空正文附件首发只在 provider wire user content 补固定 fallback", async function () {
  23. this.timeout(CASE_TIMEOUT_MS);
  24. await prepareV4ConversationE2E();
  25. expect(await pasteTextAttachment()).toBe(true);
  26. await clickV4Send();
  27. const capture = await waitForUpstreamNetworkCapture(ATTACHMENT_CONTENT);
  28. // 当前 adapter 契约使用非空 fallback,避免 Provider trim 掉旧版空格占位。
  29. expect(
  30. readUserWireTextParts(capture.requestJson).filter((part) => part === "(no content)"),
  31. ).toHaveLength(1);
  32. await waitForV4TimelineContaining(REPLY_MARKER, 60_000);
  33. await waitForV4Pane(
  34. (snapshot) =>
  35. !snapshot.canStop && Boolean(snapshot.sessionId && snapshot.sessionId !== "draft"),
  36. "T06W attachment-only 首发后没有回到 completed session",
  37. 60_000,
  38. );
  39. // Bug 根因:附件上下文从 user query 拆出后,provider 会收到空 user content 并拒绝。
  40. // 补位必须只发生在 adapter wire 边界;user row 仍只展示真实附件,不新增可见正文。
  41. const attachmentsText = await waitForV4RowAttachments(30_000);
  42. expect(attachmentsText).toContain(FILENAME);
  43. const userRowText = await browser.execute(
  44. (prefix) =>
  45. document.querySelector(`[data-testid^="${prefix}-"]`)?.closest<HTMLElement>("[data-row-id]")
  46. ?.innerText ?? null,
  47. TID_V4_ROW_ATTACHMENTS,
  48. );
  49. expect(userRowText).not.toBeNull();
  50. expect(userRowText).not.toContain("(no content)");
  51. });
  52. });
  53. async function pasteTextAttachment(): Promise<boolean> {
  54. return browser.execute(
  55. (inputTestId, filename, content) => {
  56. const input = document.querySelector<HTMLElement>(`[data-testid="${inputTestId}"]`);
  57. const editable =
  58. input?.querySelector<HTMLElement>('[contenteditable="true"]') ??
  59. (input?.getAttribute("contenteditable") === "true" ? input : null);
  60. if (!editable) return false;
  61. const clipboard = new DataTransfer();
  62. clipboard.items.add(new File([content], filename, { type: "text/plain" }));
  63. // Bug 根因:文本附件会被拆成 prompt-attachment reminder,真实 user query 因而为空;
  64. // 这里必须只提供 File、不附带 clipboard plain text,才能稳定命中 adapter padding 分支。
  65. editable.focus();
  66. editable.dispatchEvent(
  67. new ClipboardEvent("paste", {
  68. clipboardData: clipboard,
  69. bubbles: true,
  70. cancelable: true,
  71. }),
  72. );
  73. return true;
  74. },
  75. TID_V4_COMPOSER_INPUT,
  76. FILENAME,
  77. ATTACHMENT_CONTENT,
  78. );
  79. }
  80. function readUserWireTextParts(value: unknown): string[] {
  81. if (!value || typeof value !== "object") return [];
  82. const messages = (value as { messages?: unknown }).messages;
  83. if (!Array.isArray(messages)) return [];
  84. const parts: string[] = [];
  85. for (const message of messages) {
  86. if (
  87. !message ||
  88. typeof message !== "object" ||
  89. (message as { role?: unknown }).role !== "user"
  90. ) {
  91. continue;
  92. }
  93. const content = (message as { content?: unknown }).content;
  94. if (typeof content === "string") {
  95. parts.push(content);
  96. continue;
  97. }
  98. if (!Array.isArray(content)) continue;
  99. for (const part of content) {
  100. if (
  101. part &&
  102. typeof part === "object" &&
  103. (part as { type?: unknown }).type === "text" &&
  104. typeof (part as { text?: unknown }).text === "string"
  105. ) {
  106. parts.push((part as { text: string }).text);
  107. }
  108. }
  109. }
  110. return parts;
  111. }