conversation-session-attachment-upload-progress-gating.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // AP02/AP06:浏览器 File eager upload 的圆环进度与发送/队列门禁。
  2. // 证据层 L3:真实 paste File → renderer begin/chunk/commit → ready AttachmentRef → sendText。
  3. // 观察窗口内若附件仍为 queued/uploading/committing,发送按钮必须 disabled;正式提升前还需
  4. // 录制手机 Web attached-host 与真实 SSH 的网络/日志证据,确认没有提前 sendText/task command。
  5. import {
  6. TID_V4_ATTACHMENT,
  7. TID_V4_ATTACHMENT_UPLOAD_PROGRESS,
  8. TID_V4_COMPOSER_INPUT,
  9. TID_V4_COMPOSER_SEND,
  10. } from "@zcode/shared/test-ids";
  11. import { clearAppData } from "#e2e/helpers/desktop-app";
  12. import {
  13. prepareV4ConversationE2E,
  14. sendV4Prompt,
  15. waitForV4TimelineContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. describe("AP02/AP06:附件上传进度与发送门禁", () => {
  18. after(async () => {
  19. await browser.electron.restoreAllMocks();
  20. await clearAppData();
  21. });
  22. it("大图片上传期间显示百分比且发送 disabled,ready 后才允许提交", async function () {
  23. this.timeout(120000);
  24. await prepareV4ConversationE2E();
  25. const observed = (await browser.executeAsync(
  26. (inputTestId, attachmentPrefix, progressPrefix, sendTestId, done) => {
  27. const input = document.querySelector<HTMLElement>(`[data-testid="${inputTestId}"]`);
  28. const editable =
  29. input?.querySelector<HTMLElement>('[contenteditable="true"]') ??
  30. (input?.getAttribute("contenteditable") === "true" ? input : null);
  31. if (!editable) {
  32. done({ ok: false, reason: "composer-input-missing" });
  33. return;
  34. }
  35. let settled = false;
  36. const finish = (value: {
  37. ariaLabel?: string | null;
  38. ok: boolean;
  39. reason?: string;
  40. sendDisabled?: boolean;
  41. status?: string;
  42. text?: string;
  43. }) => {
  44. if (settled) return;
  45. settled = true;
  46. observer.disconnect();
  47. window.clearTimeout(timeout);
  48. done(value);
  49. };
  50. const capture = () => {
  51. const attachment = document.querySelector<HTMLElement>(
  52. `[data-testid^="${attachmentPrefix}-"]`,
  53. );
  54. const progress = document.querySelector<HTMLElement>(
  55. `[data-testid^="${progressPrefix}-"]`,
  56. );
  57. const send = document.querySelector<HTMLButtonElement>(`[data-testid="${sendTestId}"]`);
  58. if (!attachment || !progress || !send) return;
  59. finish({
  60. ariaLabel: progress.getAttribute("aria-label"),
  61. ok: true,
  62. sendDisabled: send.disabled,
  63. status: attachment.dataset.uploadStatus,
  64. text: progress.innerText,
  65. });
  66. };
  67. const observer = new MutationObserver(capture);
  68. observer.observe(document.body, {
  69. attributes: true,
  70. childList: true,
  71. subtree: true,
  72. });
  73. const timeout = window.setTimeout(
  74. () => finish({ ok: false, reason: "progress-not-observed" }),
  75. 15000,
  76. );
  77. // 接近协议上限的大 File 拉长分块窗口;内容无需是可解码 PNG,附件链路只按 bytes 上传。
  78. const file = new File([new Uint8Array(16 * 1024 * 1024)], "ap02-large.png", {
  79. type: "image/png",
  80. });
  81. const dataTransfer = new DataTransfer();
  82. dataTransfer.items.add(file);
  83. editable.dispatchEvent(
  84. new ClipboardEvent("paste", {
  85. bubbles: true,
  86. cancelable: true,
  87. clipboardData: dataTransfer,
  88. }),
  89. );
  90. capture();
  91. },
  92. TID_V4_COMPOSER_INPUT,
  93. TID_V4_ATTACHMENT,
  94. TID_V4_ATTACHMENT_UPLOAD_PROGRESS,
  95. TID_V4_COMPOSER_SEND,
  96. )) as {
  97. ariaLabel?: string | null;
  98. ok: boolean;
  99. reason?: string;
  100. sendDisabled?: boolean;
  101. status?: string;
  102. text?: string;
  103. };
  104. expect(observed).toMatchObject({ ok: true, sendDisabled: true });
  105. expect(["waitingSession", "queued", "uploading", "committing"]).toContain(observed.status);
  106. expect(`${observed.ariaLabel ?? ""}${observed.text ?? ""}`).toMatch(/%|上传|upload/iu);
  107. await browser.waitUntil(
  108. async () =>
  109. browser.execute(
  110. (attachmentPrefix, progressPrefix, sendTestId) => {
  111. const attachment = document.querySelector<HTMLElement>(
  112. `[data-testid^="${attachmentPrefix}-"]`,
  113. );
  114. const progress = document.querySelector<HTMLElement>(
  115. `[data-testid^="${progressPrefix}-"]`,
  116. );
  117. const send = document.querySelector<HTMLButtonElement>(`[data-testid="${sendTestId}"]`);
  118. return (
  119. attachment?.dataset.uploadStatus === "ready" && !progress && send?.disabled === false
  120. );
  121. },
  122. TID_V4_ATTACHMENT,
  123. TID_V4_ATTACHMENT_UPLOAD_PROGRESS,
  124. TID_V4_COMPOSER_SEND,
  125. ),
  126. { timeout: 60000, timeoutMsg: "附件未完成 eager upload 或发送按钮未解除门禁" },
  127. );
  128. await sendV4Prompt("E2E_AP02_ATTACHMENT_GATE ready 后发送");
  129. await waitForV4TimelineContaining("AP02_ATTACHMENT_GATE_OK", 45000);
  130. });
  131. });