conversation-session-ask-user-question-custom-mixed-manual-multi-question.test.ts 10 KB

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