conversation-session-ask-user-question-custom-click-multi-question.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { TID_CHAT_TOOL_CALL_BLOCK, TID_TOOL_SUMMARY_TRIGGER } from "@zcode/shared/test-ids";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. expectNoUpstreamRequestForTextWithin,
  5. waitForUpstreamRequest,
  6. } from "#e2e/helpers/conversation-session-network";
  7. import {
  8. prepareV4ConversationE2E,
  9. sendV4Prompt,
  10. waitForV4ComposerText,
  11. waitForV4UserMessageContaining,
  12. } from "#e2e/helpers/v4-conversation";
  13. import {
  14. expandAssistantHistoriesWithContent,
  15. waitForToolCallBlockByToolName,
  16. } from "#e2e/helpers/conversation-session-tool";
  17. const PROMPT_MARKER = "E2E_ASK_USER_QUESTION_CUSTOM_CLICK_MULTI";
  18. const TOOL_MARKER = "E2E_ASK_USER_QUESTION_CLICK_MULTI_TOOL";
  19. const QUESTION_ONE_MARKER = "E2E_AQ_CLICK_MULTI_Q1_CUSTOM";
  20. const QUESTION_TWO_MARKER = "E2E_AQ_CLICK_MULTI_Q2_CUSTOM";
  21. const QUESTION_THREE_MARKER = "E2E_AQ_CLICK_MULTI_Q3_CUSTOM";
  22. const ANSWER_ONE_MARKER = "E2E_AQ_CLICK_MULTI_A1";
  23. const ANSWER_TWO_MARKER = "E2E_AQ_CLICK_MULTI_A2";
  24. const ANSWER_THREE_MARKER = "E2E_AQ_CLICK_MULTI_A3";
  25. describe("会话区 AskUserQuestion 多题 custom input 鼠标点击 E2E", () => {
  26. after(async () => {
  27. await browser.electron.restoreAllMocks();
  28. await clearAppData();
  29. });
  30. it("三题都手打自定义答案后鼠标点击继续/提交不应丢失中间题", async function () {
  31. this.timeout(180000);
  32. await prepareV4ConversationE2E();
  33. const runId = Date.now();
  34. const prompt = [
  35. `${PROMPT_MARKER}_${runId}: Trigger the AskUserQuestion tool exactly once.`,
  36. `${TOOL_MARKER}: ask exactly three questions in one AskUserQuestion call.`,
  37. `Question 1 text must include ${QUESTION_ONE_MARKER}.`,
  38. `Question 2 text must include ${QUESTION_TWO_MARKER}.`,
  39. `Question 3 text must include ${QUESTION_THREE_MARKER}.`,
  40. "Each question should provide exactly two non-Other options; the client will provide the custom answer input.",
  41. "The user will type custom answers and use mouse clicks to continue or submit.",
  42. "After the user answers all questions, continue with a brief acknowledgement.",
  43. ].join(" ");
  44. const firstAnswer = `${ANSWER_ONE_MARKER}_${runId}`;
  45. const secondAnswer = `${ANSWER_TWO_MARKER}_${runId}`;
  46. const thirdAnswer = `${ANSWER_THREE_MARKER}_${runId}`;
  47. await sendV4Prompt(prompt);
  48. await waitForV4ComposerText("", "AskUserQuestion 鼠标点击多题首发后输入框没有清空");
  49. await waitForV4UserMessageContaining(`${PROMPT_MARKER}_${runId}`);
  50. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  51. await waitForElicitationPage("1 / 3", QUESTION_ONE_MARKER);
  52. await typeCustomElicitationAnswer(firstAnswer);
  53. await clickPrimaryElicitationAction(["Continue", "继续"]);
  54. await waitForElicitationPage("2 / 3", QUESTION_TWO_MARKER, 10000);
  55. await expectNoUpstreamRequestForTextWithin(firstAnswer, 900);
  56. await typeCustomElicitationAnswer(secondAnswer);
  57. await clickPrimaryElicitationAction(["Continue", "继续"]);
  58. await waitForElicitationPage("3 / 3", QUESTION_THREE_MARKER, 10000);
  59. await expectNoUpstreamRequestForTextWithin(secondAnswer, 900);
  60. await typeCustomElicitationAnswer(thirdAnswer);
  61. await clickPrimaryElicitationAction(["Submit", "提交"]);
  62. await waitForUpstreamRequest(
  63. {
  64. includes: [firstAnswer, secondAnswer, thirdAnswer, TOOL_MARKER],
  65. },
  66. "AskUserQuestion 三题自定义鼠标提交后没有把全部答案带入下一次模型请求",
  67. 60000,
  68. );
  69. await waitForExpandedAskUserQuestionBlockContaining(firstAnswer, 8000);
  70. await waitForExpandedAskUserQuestionBlockContaining(secondAnswer, 8000);
  71. await waitForExpandedAskUserQuestionBlockContaining(thirdAnswer, 8000);
  72. await expectNoFailedAskUserQuestionBlockForMarkers([
  73. QUESTION_ONE_MARKER,
  74. QUESTION_TWO_MARKER,
  75. QUESTION_THREE_MARKER,
  76. ]);
  77. });
  78. });
  79. async function typeCustomElicitationAnswer(answer: string) {
  80. const input = await getCustomElicitationInput();
  81. await input.click();
  82. await browser.keys(answer);
  83. await browser.waitUntil(async () => (await input.getValue()) === answer, {
  84. timeout: 10000,
  85. timeoutMsg: "AskUserQuestion 自定义回答没有通过键盘逐字写入输入框",
  86. });
  87. }
  88. async function getCustomElicitationInput() {
  89. const input = await $('[data-elicitation-dialog-body="true"] textarea');
  90. await input.waitForDisplayed({
  91. timeout: 30000,
  92. timeoutMsg: "AskUserQuestion 自定义回答输入框没有出现",
  93. });
  94. return input;
  95. }
  96. async function clickPrimaryElicitationAction(actionLabels: string[]) {
  97. const footer = await $('[data-elicitation-dialog-footer="true"]');
  98. await footer.waitForDisplayed({
  99. timeout: 10000,
  100. timeoutMsg: "AskUserQuestion 操作按钮区域没有出现",
  101. });
  102. const buttons = await footer.$$("button");
  103. const actualLabels: string[] = [];
  104. for (const button of buttons) {
  105. const label = normalizeText(await button.getText());
  106. actualLabels.push(label);
  107. if (actionLabels.includes(label)) {
  108. await button.click();
  109. return;
  110. }
  111. }
  112. throw new Error(
  113. `AskUserQuestion 鼠标操作按钮没有出现: expected=${actionLabels.join("|")}; actual=${actualLabels.join("|")}`,
  114. );
  115. }
  116. function normalizeText(value: string | null | undefined) {
  117. return (value ?? "").replace(/\u00a0/g, " ").trim();
  118. }
  119. async function waitForElicitationPage(pageText: string, questionText: string, timeout = 60000) {
  120. let latest = "";
  121. try {
  122. await browser.waitUntil(
  123. async () => {
  124. latest = await getElicitationDialogText();
  125. return latest.includes(pageText) && latest.includes(questionText);
  126. },
  127. {
  128. timeout,
  129. timeoutMsg: `AskUserQuestion 问答卡片没有进入目标题目: ${pageText} ${questionText}`,
  130. },
  131. );
  132. } catch (error) {
  133. latest = await getElicitationDialogText();
  134. throw new Error(
  135. `AskUserQuestion 问答卡片没有进入目标题目: ${pageText} ${questionText}; latest=${latest}`,
  136. { cause: error },
  137. );
  138. }
  139. }
  140. function getElicitationDialogText() {
  141. return browser.execute(() => {
  142. const normalizeText = (value: string | null | undefined) =>
  143. (value ?? "").replace(/\u00a0/g, " ").trim();
  144. return normalizeText(
  145. document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]')?.innerText,
  146. );
  147. });
  148. }
  149. async function waitForExpandedAskUserQuestionBlockContaining(text: string, timeout: number) {
  150. let latest: AskUserQuestionBlockDiagnostics | null = null;
  151. try {
  152. await browser.waitUntil(
  153. async () => {
  154. // V4 已移除旧历史属性;失败录像中的已工作详情仍折叠,须先经真实 UI 展开。
  155. await expandAssistantHistoriesWithContent();
  156. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  157. return latest.matched;
  158. },
  159. {
  160. timeout,
  161. timeoutMsg: "AskUserQuestion 自定义回答没有显示在已询问消息详情里",
  162. },
  163. );
  164. } catch (error) {
  165. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  166. throw new Error(
  167. `AskUserQuestion 自定义回答没有显示在已询问消息详情里; diagnostics=${JSON.stringify(latest)}`,
  168. { cause: error },
  169. );
  170. }
  171. }
  172. async function expectNoFailedAskUserQuestionBlockForMarkers(markers: string[]) {
  173. const diagnostics = await getExpandedAskUserQuestionBlockDiagnostics("");
  174. const failureTexts = ["Failed", "失败", "No answer provided", "未提供回答", "未回答"];
  175. const failedBlocks = diagnostics.blocks.filter(
  176. (block) =>
  177. markers.every((marker) => block.text.includes(marker)) &&
  178. failureTexts.some((failureText) => block.text.includes(failureText)),
  179. );
  180. if (failedBlocks.length > 0) {
  181. throw new Error(
  182. `AskUserQuestion 鼠标提交后额外出现未回答或失败的同组问题块; diagnostics=${JSON.stringify(
  183. diagnostics,
  184. )}`,
  185. );
  186. }
  187. }
  188. interface AskUserQuestionBlockDiagnostics {
  189. blocks: Array<{
  190. expanded: boolean;
  191. text: string;
  192. toolCallId: string | null;
  193. }>;
  194. matched: boolean;
  195. }
  196. function getExpandedAskUserQuestionBlockDiagnostics(
  197. text: string,
  198. ): Promise<AskUserQuestionBlockDiagnostics> {
  199. return browser.execute(
  200. (toolBlockPrefix, summaryTriggerPrefix, expectedText) => {
  201. const normalizeText = (value: string | null | undefined) =>
  202. (value ?? "").replace(/\u00a0/g, " ").trim();
  203. const blocks = Array.from(
  204. document.querySelectorAll<HTMLElement>(
  205. `[data-testid^="${toolBlockPrefix}-"][data-tool-name="AskUserQuestion"]`,
  206. ),
  207. );
  208. for (const block of blocks) {
  209. const trigger = block.querySelector<HTMLElement>(
  210. `[data-testid^="${summaryTriggerPrefix}-"]`,
  211. );
  212. if (trigger && trigger.getAttribute("aria-expanded") !== "true") {
  213. trigger.click();
  214. }
  215. }
  216. const snapshots = blocks.map((block) => {
  217. const trigger = block.querySelector<HTMLElement>(
  218. `[data-testid^="${summaryTriggerPrefix}-"]`,
  219. );
  220. return {
  221. expanded: trigger?.getAttribute("aria-expanded") === "true",
  222. text: normalizeText(block.innerText),
  223. toolCallId: block.getAttribute("data-tool-call-id"),
  224. };
  225. });
  226. return {
  227. blocks: snapshots,
  228. matched: snapshots.some((block) => block.text.includes(expectedText)),
  229. };
  230. },
  231. TID_CHAT_TOOL_CALL_BLOCK,
  232. TID_TOOL_SUMMARY_TRIGGER,
  233. text,
  234. );
  235. }