conversation-session-v4-attachment.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // T06 + T07 + D12 门禁:仅附件首发;已发送图片预览;latest query 删除附件后 branch cut。
  2. // 证据层 L3:
  3. // 1) 两个 PNG 经真实 paste + eager begin/chunk/commit,正文保持空字符串;
  4. // 2) 首发允许仅 AttachmentRef[],user row 保留两个有序 ref;
  5. // 3) edit 删除第一个并提交,UI active branch 与 provider-visible request 都仅含第二个 ref。
  6. import {
  7. TID_V4_EDIT_ATTACHMENT_REMOVE,
  8. TID_V4_EDIT_SUBMIT,
  9. TID_V4_ROW_ATTACHMENTS,
  10. testId,
  11. } from "@zcode/shared";
  12. import { clearAppData, clickTestIdByDom } from "../helpers/desktop-app.js";
  13. import {
  14. beginFirstV4UserQueryEdit,
  15. clickV4Send,
  16. pasteV4ComposerImageAttachment,
  17. prepareV4ConversationE2E,
  18. waitForV4Pane,
  19. waitForV4TimelineContaining,
  20. } from "../helpers/v4-conversation.js";
  21. describe("v4 T06 + D12:仅附件发送与 edit 删除附件", () => {
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. });
  26. it("仅附件首发 → edit 删除一个 → active branch 与 provider 仅保留剩余 ref", async () => {
  27. await prepareV4ConversationE2E();
  28. // 1) 两个附件都走真实 chunk transaction;composer 正文始终为空。
  29. const removedFilename = await pasteV4ComposerImageAttachment("e2e-remove-me.png");
  30. const keptFilename = await pasteV4ComposerImageAttachment("e2e-keep-me.png");
  31. // 2) eager upload ready 后发送按钮因 attachments 可用;点击时不再启动上传。
  32. await clickV4Send();
  33. await waitForV4TimelineContaining("V4_ATTACHMENT_ONLY_SEND_OK", 45000);
  34. await waitForV4Pane(
  35. (s) => !s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  36. "仅附件首发后没有回到空闲态且绑定 session",
  37. 45000,
  38. );
  39. await waitForV4ImageAttachments([removedFilename, keptFilename], 30000);
  40. // T07:已发送图片不再只是一个带假手型的静态 chip;点击后必须通过
  41. // v4/attachment/read 拉取 artifact,并真正渲染 Blob 图片预览。
  42. const clickedPreview = await browser.execute(
  43. (attachmentsPrefix, filename) => {
  44. const cards = Array.from(
  45. document.querySelectorAll<HTMLElement>(
  46. `[data-testid^="${attachmentsPrefix}-"] [data-v4-user-input-media-attachment="true"]`,
  47. ),
  48. );
  49. const target = cards.find(
  50. (candidate) => candidate.querySelector<HTMLImageElement>("img")?.alt === filename,
  51. );
  52. target?.click();
  53. return Boolean(target);
  54. },
  55. TID_V4_ROW_ATTACHMENTS,
  56. keptFilename,
  57. );
  58. expect(clickedPreview).toBe(true);
  59. await browser.waitUntil(
  60. async () =>
  61. browser.execute((filename) => {
  62. const dialog = document.querySelector<HTMLElement>('[role="dialog"]');
  63. const image = dialog?.querySelector<HTMLImageElement>(`img[alt="${filename}"]`);
  64. return Boolean(image?.src.startsWith("blob:"));
  65. }, keptFilename),
  66. { timeout: 15000, timeoutMsg: "已发送图片点击后没有打开 Blob 图片预览" },
  67. );
  68. await browser.keys(["Escape"]);
  69. // 3) 行内 edit 不接上传状态机,只删除局部 refs;索引 0 对应第一个附件。
  70. const rowId = await beginFirstV4UserQueryEdit("");
  71. await clickTestIdByDom(testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${rowId}-0`), {
  72. timeout: 15000,
  73. timeoutMsg: `v4 edit 第一个附件删除按钮没有出现: rowId=${rowId}`,
  74. });
  75. await clickTestIdByDom(testId(TID_V4_EDIT_SUBMIT, rowId), {
  76. timeout: 15000,
  77. timeoutMsg: `v4 仅附件 edit 提交按钮不可点击: rowId=${rowId}`,
  78. });
  79. // fixture 以 provider placeholder 同时证明:新请求含 kept、不含 removed。
  80. await waitForV4TimelineContaining("V4_ATTACHMENT_EDIT_ONE_OK", 45000);
  81. await waitForV4ImageAttachments([keptFilename], 30000);
  82. });
  83. });
  84. async function waitForV4ImageAttachments(expectedFilenames: string[], timeout: number) {
  85. await browser.waitUntil(
  86. () =>
  87. browser.execute(
  88. (attachmentsPrefix, expected) => {
  89. const cards = Array.from(
  90. document.querySelectorAll<HTMLElement>(
  91. `[data-testid^="${attachmentsPrefix}-"] [data-v4-user-input-media-attachment="true"]`,
  92. ),
  93. );
  94. return (
  95. cards.length === expected.length &&
  96. cards.every((card, index) => {
  97. const image = card.querySelector<HTMLImageElement>("img");
  98. return (
  99. card.getAttribute("role") === "button" &&
  100. image?.alt === expected[index] &&
  101. image?.src.startsWith("blob:") === true
  102. );
  103. })
  104. );
  105. },
  106. TID_V4_ROW_ATTACHMENTS,
  107. expectedFilenames,
  108. ),
  109. { timeout, timeoutMsg: `user 行图片附件没有按原顺序收敛: ${expectedFilenames.join(", ")}` },
  110. );
  111. }