conversation-session-selection-comments.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { join } from "node:path";
  2. import { clearAppData, pinE2ELocale } from "#e2e/helpers/desktop-app";
  3. import {
  4. countUpstreamRequests,
  5. waitForUpstreamRequest,
  6. } from "#e2e/helpers/conversation-session-network";
  7. import {
  8. clickSelectionAction,
  9. dragSelectMainTimelineText,
  10. openMainTimelineAnnotation,
  11. readMainComposerSelectionChip,
  12. readMainTimelineAnnotationMarks,
  13. typeSelectionComment,
  14. } from "#e2e/helpers/conversation-selection-comment";
  15. import {
  16. clickV4Send,
  17. editFirstV4UserQuery,
  18. prepareV4ConversationE2E,
  19. sendV4Prompt,
  20. setV4ComposerText,
  21. waitForV4AssistantMessageContaining,
  22. waitForV4ConversationState,
  23. waitForV4UserMessageContaining,
  24. } from "#e2e/helpers/v4-conversation";
  25. const artifacts = () => process.env.ZCODE_E2E_ARTIFACT_DIR!;
  26. const ANSWER_ONE = "E2E_SCM_ANSWER_ONE";
  27. const ANSWER_TWO = "E2E_SCM_ANSWER_TWO";
  28. const COMMENT_KEEP = "E2E_SCM_COMMENT_KEEP";
  29. const COMMENT_DROP = "E2E_SCM_COMMENT_DROP";
  30. const COMMENT_ADDED = "E2E_SCM_COMMENT_ADDED";
  31. /** 第一轮回答里表格之前的正文,用来验证“仅原文”注释(不写评论直接回车)。 */
  32. const QUOTE_ONLY = "下面是结果";
  33. const COMMENT_GUIDANCE = "user annotation on that exact quoted text";
  34. async function waitForIdle(message: string): Promise<void> {
  35. await waitForV4ConversationState(
  36. (state) => state.state === "idle" && state.activeInputId === null,
  37. message,
  38. 60000,
  39. );
  40. }
  41. /**
  42. * 等待 composer 注释 chip 为 count 条,并且正文编号标记的形态与 variants 一致
  43. * (comment = 有评论实心,quote = 仅原文空心,按编号顺序)。
  44. */
  45. async function waitForAnnotations(count: number, variants: readonly string[]): Promise<void> {
  46. await browser.waitUntil(
  47. async () => {
  48. const chip = await readMainComposerSelectionChip();
  49. const marks = await readMainTimelineAnnotationMarks();
  50. return (
  51. chip.count === count &&
  52. (chip.label ?? "").includes(`${count} 条注释`) &&
  53. JSON.stringify(marks.map((mark) => mark.variant)) === JSON.stringify(variants) &&
  54. marks.every((mark, index) => mark.number === index + 1)
  55. );
  56. },
  57. {
  58. timeout: 15000,
  59. timeoutMsg: `注释状态不符,期望 ${count} 条 ${JSON.stringify(variants)}: ${JSON.stringify({
  60. chip: await readMainComposerSelectionChip(),
  61. marks: await readMainTimelineAnnotationMarks(),
  62. })}`,
  63. },
  64. );
  65. }
  66. describe("选区注释 manual review", () => {
  67. after(async () => {
  68. await clearAppData();
  69. });
  70. it("SCM01-06、SCM10: 选区注释新增/仅原文/编辑/删除撤销、rewind 删除来源注释、注释随消息发给模型、悬停已发送标记", async function () {
  71. this.timeout(240000);
  72. await prepareV4ConversationE2E();
  73. await pinE2ELocale("zh-CN");
  74. await sendV4Prompt("E2E_SCM_SEED_ONE: answer with a markdown table");
  75. await waitForV4AssistantMessageContaining(ANSWER_ONE, 60000);
  76. await waitForIdle("第一轮回答没有完成");
  77. await sendV4Prompt("E2E_SCM_SEED_TWO: answer with a code block");
  78. await waitForV4AssistantMessageContaining(ANSWER_TWO, 60000);
  79. await waitForIdle("第二轮回答没有完成");
  80. // SCM01:表格单元格里的回答文字 → 注释浮层 → 键盘输入评论并回车保存。
  81. await dragSelectMainTimelineText(ANSWER_ONE);
  82. await browser.saveScreenshot(join(artifacts(), "scm01-selection-menu.png"));
  83. await clickSelectionAction("comment");
  84. await typeSelectionComment(COMMENT_KEEP);
  85. await browser.saveScreenshot(join(artifacts(), "scm01-comment-editor.png"));
  86. await browser.keys("Enter");
  87. await $("[data-conversation-selection-comment-editor]").waitForDisplayed({ reverse: true });
  88. await waitForAnnotations(1, ["comment"]);
  89. // 第二条注释挂在第二轮回答上,随后由 rewind 删除来源。
  90. await dragSelectMainTimelineText(ANSWER_TWO);
  91. await clickSelectionAction("comment");
  92. await typeSelectionComment(COMMENT_DROP);
  93. await $("[data-conversation-selection-comment-save]").click();
  94. await waitForAnnotations(2, ["comment", "comment"]);
  95. // SCM04:不写评论直接回车 = 只加原文(取代原“添加到当前任务”),正文为空心编号。
  96. await dragSelectMainTimelineText(QUOTE_ONLY);
  97. expect(await $('[data-conversation-selection-action="add-to-task"]').isExisting()).toBe(false);
  98. await clickSelectionAction("comment");
  99. await $("[data-conversation-selection-comment-input]").waitForDisplayed({ timeout: 5000 });
  100. await browser.saveScreenshot(join(artifacts(), "scm04-pending-mark.png"));
  101. await browser.keys("Enter");
  102. await $("[data-conversation-selection-comment-editor]").waitForDisplayed({ reverse: true });
  103. await waitForAnnotations(3, ["comment", "comment", "quote"]);
  104. await $("[data-conversation-selection-reference-count]").moveTo();
  105. await $("[data-conversation-annotation-add-comment]").waitForDisplayed({ timeout: 5000 });
  106. await browser.saveScreenshot(join(artifacts(), "scm04-composer-chip-hover.png"));
  107. // SCM05:点正文 ③ → 编辑浮层补评论,空心变实心。
  108. await openMainTimelineAnnotation(3);
  109. await typeSelectionComment(COMMENT_ADDED);
  110. await browser.saveScreenshot(join(artifacts(), "scm05-edit-annotation.png"));
  111. await browser.keys("Enter");
  112. await waitForAnnotations(3, ["comment", "comment", "comment"]);
  113. // SCM06:编辑浮层删除整条注释,6 秒内撤销后原位恢复,编号不变。
  114. await openMainTimelineAnnotation(3);
  115. await $("[data-conversation-selection-comment-delete]").click();
  116. await waitForAnnotations(2, ["comment", "comment"]);
  117. await browser.saveScreenshot(join(artifacts(), "scm06-deleted-toast.png"));
  118. // toast 有淡入动画,按文字在 toast 宿主里找“撤销”并点击,失败时带上宿主内容方便定位。
  119. await browser.waitUntil(
  120. async () =>
  121. browser.execute(() => {
  122. const button = Array.from(
  123. document.querySelectorAll<HTMLButtonElement>("#zcode-toast-host button"),
  124. ).find((candidate) => candidate.textContent?.trim() === "撤销");
  125. if (!button) return false;
  126. button.click();
  127. return true;
  128. }),
  129. {
  130. timeout: 5000,
  131. timeoutMsg: `删除注释后没有出现“撤销”: ${await browser.execute(
  132. () => document.querySelector("#zcode-toast-host")?.textContent ?? "<no toast host>",
  133. )}`,
  134. },
  135. );
  136. await waitForAnnotations(3, ["comment", "comment", "comment"]);
  137. // 浅色主题下复看浮层、注释输入与 chip;Esc 取消不改变已有注释。
  138. await browser.execute(() => {
  139. (
  140. window as typeof window & { __testActions: { setTheme(value: string): void } }
  141. ).__testActions.setTheme("zai-light");
  142. });
  143. await browser.waitUntil(() =>
  144. browser.execute(() => document.documentElement.classList.contains("theme-zai-light")),
  145. );
  146. await dragSelectMainTimelineText(ANSWER_ONE);
  147. await browser.saveScreenshot(join(artifacts(), "scm01-light-selection-menu.png"));
  148. await clickSelectionAction("comment");
  149. await typeSelectionComment("E2E_SCM_CANCELLED");
  150. await browser.saveScreenshot(join(artifacts(), "scm01-light-comment-editor.png"));
  151. await browser.keys("Escape");
  152. await $("[data-conversation-selection-comment-editor]").waitForDisplayed({ reverse: true });
  153. await waitForAnnotations(3, ["comment", "comment", "comment"]);
  154. await $("[data-conversation-selection-reference-count]").moveTo();
  155. await $("[data-conversation-selection-comment]").waitForDisplayed({ timeout: 5000 });
  156. await browser.saveScreenshot(join(artifacts(), "scm01-light-composer-chip-hover.png"));
  157. // SCM02:编辑最新问题会删除第二轮回答所在行;该行的注释同步移除,第一轮的两条保留并重新编号。
  158. await editFirstV4UserQuery("E2E_SCM_SEED_TWO_REWOUND: answer again");
  159. await waitForV4UserMessageContaining("E2E_SCM_SEED_TWO_REWOUND");
  160. await waitForV4AssistantMessageContaining("E2E_SCM_REWOUND_DONE", 60000);
  161. await waitForIdle("rewind 后回答没有完成");
  162. await waitForAnnotations(2, ["comment", "comment"]);
  163. await browser.saveScreenshot(join(artifacts(), "scm02-after-rewind.png"));
  164. // SCM03:注释随下一条消息作为结构化引用发给模型,并附带评论说明;发送后正文标记变灰。
  165. await setV4ComposerText("E2E_SCM_SEND: 请结合注释回答");
  166. await clickV4Send();
  167. await waitForUpstreamRequest(
  168. {
  169. lastUserMessageIncludes: [
  170. "E2E_SCM_SEND",
  171. ANSWER_ONE,
  172. COMMENT_KEEP,
  173. QUOTE_ONLY,
  174. COMMENT_ADDED,
  175. COMMENT_GUIDANCE,
  176. ],
  177. },
  178. "带注释的模型请求没有发出",
  179. 60000,
  180. );
  181. expect(
  182. await countUpstreamRequests({ lastUserMessageIncludes: ["E2E_SCM_SEND", COMMENT_DROP] }),
  183. ).toBe(0);
  184. await waitForV4AssistantMessageContaining("E2E_SCM_SEND_DONE", 60000);
  185. await waitForIdle("带注释的消息没有完成");
  186. expect((await readMainComposerSelectionChip()).count).toBeNull();
  187. await browser.waitUntil(
  188. async () =>
  189. JSON.stringify((await readMainTimelineAnnotationMarks()).map((mark) => mark.variant)) ===
  190. JSON.stringify(["sent-comment", "sent-comment"]),
  191. { timeout: 10000, timeoutMsg: "发送后的注释标记没有变成灰色已发送态" },
  192. );
  193. await browser.saveScreenshot(join(artifacts(), "scm03-sent.png"));
  194. // SCM10:悬停已发送的灰色编号,可以看到当时发出的原文和评论。
  195. await $('[data-annotation-sent-mark="1"]').moveTo();
  196. const sentDetail = await $("[data-annotation-sent-detail]");
  197. await sentDetail.waitForDisplayed({ timeout: 5000 });
  198. const sentDetailText = await sentDetail.getText();
  199. expect(sentDetailText).toContain(ANSWER_ONE);
  200. expect(sentDetailText).toContain(COMMENT_KEEP);
  201. await browser.saveScreenshot(join(artifacts(), "scm10-sent-hover.png"));
  202. // 角标只有露在文字行上方的那一截接收鼠标;探入文字行的底边不挡正文,从那里仍能开始划词。
  203. const hit = await browser.execute(() => {
  204. const badge = document
  205. .querySelector('[data-annotation-sent-mark="1"]')!
  206. .parentElement!.getBoundingClientRect();
  207. const inOverlay = (x: number, y: number) =>
  208. document.elementFromPoint(x, y)?.closest("[data-conversation-annotation-marks]") != null;
  209. const x = badge.left + badge.width / 2;
  210. return { upper: inOverlay(x, badge.top + 4), lowerEdge: inOverlay(x, badge.bottom - 1) };
  211. });
  212. expect(hit).toEqual({ upper: true, lowerEdge: false });
  213. });
  214. });