conversation-session-ask-user-question-custom-enter-multi-question.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_ENTER_MULTI";
  18. const TOOL_MARKER = "E2E_ASK_USER_QUESTION_ENTER_MULTI_TOOL";
  19. const FIRST_QUESTION_MARKER = "E2E_ASK_USER_QUESTION_ENTER_FIRST";
  20. const SECOND_QUESTION_MARKER = "E2E_ASK_USER_QUESTION_ENTER_SECOND";
  21. const FIRST_ANSWER_MARKER = "E2E_ASK_USER_QUESTION_ENTER_FIRST_ANSWER";
  22. const SECOND_ANSWER_MARKER = "E2E_ASK_USER_QUESTION_ENTER_SECOND_ANSWER";
  23. describe("会话区 AskUserQuestion 多题 custom input Enter E2E", () => {
  24. after(async () => {
  25. await browser.electron.restoreAllMocks();
  26. await clearAppData();
  27. });
  28. it("非最后一题 custom input 按 Enter 应进入下一题而不是提前提交", async function () {
  29. this.timeout(150000);
  30. await prepareV4ConversationE2E();
  31. const runId = Date.now();
  32. const prompt = [
  33. `${PROMPT_MARKER}_${runId}: Trigger the AskUserQuestion tool exactly once.`,
  34. `${TOOL_MARKER}: ask two questions before continuing.`,
  35. "The first answer will be submitted by pressing Enter in the custom input.",
  36. "After the user answers both questions, continue with a brief acknowledgement.",
  37. ].join(" ");
  38. const firstAnswer = `${FIRST_ANSWER_MARKER}_${runId}`;
  39. const secondAnswer = `${SECOND_ANSWER_MARKER}_${runId}`;
  40. await sendV4Prompt(prompt);
  41. await waitForV4ComposerText("", "AskUserQuestion 多题首发后输入框没有清空");
  42. await waitForV4UserMessageContaining(`${PROMPT_MARKER}_${runId}`);
  43. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  44. await waitForElicitationDialogContaining(FIRST_QUESTION_MARKER);
  45. // 回归点:custom input 的 Enter 不能绕过多题分页,否则第 1 题会把整个问答提前提交。
  46. await pressEnterInCustomElicitationInput(firstAnswer);
  47. await waitForElicitationDialogContaining(SECOND_QUESTION_MARKER, 10000);
  48. await expectNoUpstreamRequestForTextWithin(firstAnswer, 900);
  49. await submitCustomElicitationAnswer(secondAnswer);
  50. await waitForUpstreamRequest(
  51. {
  52. includes: [firstAnswer, secondAnswer, TOOL_MARKER],
  53. },
  54. "AskUserQuestion 多题自定义回答没有一起进入下一次模型请求",
  55. 60000,
  56. );
  57. await waitForExpandedAskUserQuestionBlockContaining(firstAnswer, 8000);
  58. await waitForExpandedAskUserQuestionBlockContaining(secondAnswer, 8000);
  59. });
  60. });
  61. async function pressEnterInCustomElicitationInput(answer: string) {
  62. const input = await fillCustomElicitationAnswer(answer);
  63. await input.click();
  64. await browser.keys("Enter");
  65. }
  66. async function submitCustomElicitationAnswer(answer: string) {
  67. await fillCustomElicitationAnswer(answer);
  68. const clicked = (await browser.execute(() => {
  69. const normalizeText = (value: string | null | undefined) =>
  70. (value ?? "").replace(/\u00a0/g, " ").trim();
  71. const footer = document.querySelector<HTMLElement>('[data-elicitation-dialog-footer="true"]');
  72. const button = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  73. (candidate) => {
  74. const text = normalizeText(candidate.textContent);
  75. return text === "Submit" || text === "提交";
  76. },
  77. );
  78. button?.click();
  79. return Boolean(button);
  80. })) as boolean;
  81. expect(clicked).toBe(true);
  82. }
  83. async function fillCustomElicitationAnswer(answer: string) {
  84. const input = await $('[data-elicitation-dialog-body="true"] textarea');
  85. await input.waitForDisplayed({
  86. timeout: 30000,
  87. timeoutMsg: "AskUserQuestion 自定义回答输入框没有出现",
  88. });
  89. await input.click();
  90. await input.setValue(answer);
  91. await browser.waitUntil(async () => (await input.getValue()) === answer, {
  92. timeout: 10000,
  93. timeoutMsg: "AskUserQuestion 自定义回答没有写入输入框",
  94. });
  95. return input;
  96. }
  97. async function waitForElicitationDialogContaining(text: string, timeout = 60000) {
  98. let latest = "";
  99. try {
  100. await browser.waitUntil(
  101. async () => {
  102. latest = await getElicitationDialogText();
  103. return latest.includes(text);
  104. },
  105. {
  106. timeout,
  107. timeoutMsg: `AskUserQuestion 问答卡片没有显示预期内容: ${text}`,
  108. },
  109. );
  110. } catch (error) {
  111. latest = await getElicitationDialogText();
  112. throw new Error(`AskUserQuestion 问答卡片没有显示预期内容: ${text}; latest=${latest}`, {
  113. cause: error,
  114. });
  115. }
  116. }
  117. function getElicitationDialogText() {
  118. return browser.execute(() => {
  119. const normalizeText = (value: string | null | undefined) =>
  120. (value ?? "").replace(/\u00a0/g, " ").trim();
  121. return normalizeText(
  122. document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]')?.innerText,
  123. );
  124. });
  125. }
  126. async function waitForExpandedAskUserQuestionBlockContaining(text: string, timeout: number) {
  127. let latest: AskUserQuestionBlockDiagnostics | null = null;
  128. try {
  129. await browser.waitUntil(
  130. async () => {
  131. // V4 已移除旧历史属性;失败录像中的已工作详情仍折叠,须先经真实 UI 展开。
  132. await expandAssistantHistoriesWithContent();
  133. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  134. return latest.matched;
  135. },
  136. {
  137. timeout,
  138. timeoutMsg: "AskUserQuestion 自定义回答没有显示在已询问消息详情里",
  139. },
  140. );
  141. } catch (error) {
  142. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  143. throw new Error(
  144. `AskUserQuestion 自定义回答没有显示在已询问消息详情里; diagnostics=${JSON.stringify(latest)}`,
  145. { cause: error },
  146. );
  147. }
  148. }
  149. interface AskUserQuestionBlockDiagnostics {
  150. blocks: Array<{
  151. expanded: boolean;
  152. text: string;
  153. toolCallId: string | null;
  154. }>;
  155. matched: boolean;
  156. }
  157. function getExpandedAskUserQuestionBlockDiagnostics(
  158. text: string,
  159. ): Promise<AskUserQuestionBlockDiagnostics> {
  160. return browser.execute(
  161. (toolBlockPrefix, summaryTriggerPrefix, expectedText) => {
  162. const normalizeText = (value: string | null | undefined) =>
  163. (value ?? "").replace(/\u00a0/g, " ").trim();
  164. const blocks = Array.from(
  165. document.querySelectorAll<HTMLElement>(
  166. `[data-testid^="${toolBlockPrefix}-"][data-tool-name="AskUserQuestion"]`,
  167. ),
  168. );
  169. for (const block of blocks) {
  170. const trigger = block.querySelector<HTMLElement>(
  171. `[data-testid^="${summaryTriggerPrefix}-"]`,
  172. );
  173. if (trigger && trigger.getAttribute("aria-expanded") !== "true") {
  174. trigger.click();
  175. }
  176. }
  177. const snapshots = blocks.map((block) => {
  178. const trigger = block.querySelector<HTMLElement>(
  179. `[data-testid^="${summaryTriggerPrefix}-"]`,
  180. );
  181. return {
  182. expanded: trigger?.getAttribute("aria-expanded") === "true",
  183. text: normalizeText(block.innerText),
  184. toolCallId: block.getAttribute("data-tool-call-id"),
  185. };
  186. });
  187. return {
  188. blocks: snapshots,
  189. matched: snapshots.some((block) => block.text.includes(expectedText)),
  190. };
  191. },
  192. TID_CHAT_TOOL_CALL_BLOCK,
  193. TID_TOOL_SUMMARY_TRIGGER,
  194. text,
  195. );
  196. }