conversation-session-ask-user-question-custom-enter-single-question.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. prepareV4ConversationE2E,
  10. sendV4Prompt,
  11. waitForV4ComposerText,
  12. waitForV4UserMessageContaining,
  13. } from "#e2e/helpers/v4-conversation";
  14. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  15. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  16. const PROMPT_MARKER = "E2E_ASK_USER_QUESTION_CUSTOM_ENTER_SINGLE";
  17. const TOOL_MARKER = "E2E_ASK_USER_QUESTION_ENTER_SINGLE_TOOL";
  18. const QUESTION_MARKER = "E2E_ASK_USER_QUESTION_ENTER_SINGLE_QUESTION";
  19. const ANSWER_MARKER = "E2E_ASK_USER_QUESTION_ENTER_SINGLE_ANSWER";
  20. describe("会话区 AskUserQuestion 单题 custom input Enter E2E", () => {
  21. after(async () => {
  22. await browser.electron.restoreAllMocks();
  23. await clearAppData();
  24. });
  25. it("只有一题时 custom input 按 Enter 应直接提交答案", async function () {
  26. this.timeout(150000);
  27. await prepareV4ConversationE2E();
  28. const runId = Date.now();
  29. const prompt = [
  30. `${PROMPT_MARKER}_${runId}: Trigger the AskUserQuestion tool exactly once.`,
  31. `${TOOL_MARKER}: ask one question before continuing.`,
  32. `${QUESTION_MARKER}: include this marker in the question text.`,
  33. "The custom answer will be submitted by pressing Enter in the custom input.",
  34. "After the user answers, continue with a brief acknowledgement.",
  35. ].join(" ");
  36. const answer = `${ANSWER_MARKER}_${runId}`;
  37. await sendV4Prompt(prompt);
  38. await waitForV4ComposerText("", "AskUserQuestion 单题首发后输入框没有清空");
  39. await waitForV4UserMessageContaining(`${PROMPT_MARKER}_${runId}`);
  40. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  41. await waitForElicitationDialogContaining(QUESTION_MARKER);
  42. // 对照多题 case:单题 custom input 的 Enter 应等价于最终提交。
  43. await pressEnterInCustomElicitationInput(answer);
  44. await waitForUpstreamRequest(
  45. {
  46. includes: [answer, TOOL_MARKER],
  47. },
  48. "AskUserQuestion 单题自定义回答按 Enter 后没有进入下一次模型请求",
  49. 60000,
  50. );
  51. await waitForExpandedAskUserQuestionBlockContaining(answer, 8000);
  52. });
  53. });
  54. async function pressEnterInCustomElicitationInput(answer: string) {
  55. const input = await $('[data-elicitation-dialog-body="true"] textarea');
  56. await input.waitForDisplayed({
  57. timeout: 30000,
  58. timeoutMsg: "AskUserQuestion 自定义回答输入框没有出现",
  59. });
  60. await input.click();
  61. await input.setValue(answer);
  62. await browser.waitUntil(async () => (await input.getValue()) === answer, {
  63. timeout: 10000,
  64. timeoutMsg: "AskUserQuestion 自定义回答没有写入输入框",
  65. });
  66. await browser.keys("Enter");
  67. }
  68. async function waitForElicitationDialogContaining(text: string, timeout = 60000) {
  69. let latest = "";
  70. try {
  71. await browser.waitUntil(
  72. async () => {
  73. latest = await getElicitationDialogText();
  74. return latest.includes(text);
  75. },
  76. {
  77. timeout,
  78. timeoutMsg: `AskUserQuestion 问答卡片没有显示预期内容: ${text}`,
  79. },
  80. );
  81. } catch (error) {
  82. latest = await getElicitationDialogText();
  83. throw new Error(`AskUserQuestion 问答卡片没有显示预期内容: ${text}; latest=${latest}`, {
  84. cause: error,
  85. });
  86. }
  87. }
  88. function getElicitationDialogText() {
  89. return browser.execute(() => {
  90. const normalizeText = (value: string | null | undefined) =>
  91. (value ?? "").replace(/\u00a0/g, " ").trim();
  92. return normalizeText(
  93. document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]')?.innerText,
  94. );
  95. });
  96. }
  97. async function waitForExpandedAskUserQuestionBlockContaining(text: string, timeout: number) {
  98. let latest: AskUserQuestionBlockDiagnostics | null = null;
  99. try {
  100. await browser.waitUntil(
  101. async () => {
  102. // V4 历史不再提供 data-history-has-content;按实际 content 节点展开。
  103. await expandAssistantHistoriesWithContent();
  104. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  105. return latest.matched;
  106. },
  107. {
  108. timeout,
  109. timeoutMsg: "AskUserQuestion 自定义回答没有显示在已询问消息详情里",
  110. },
  111. );
  112. } catch (error) {
  113. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  114. throw new Error(
  115. `AskUserQuestion 自定义回答没有显示在已询问消息详情里; diagnostics=${JSON.stringify(latest)}`,
  116. { cause: error },
  117. );
  118. }
  119. }
  120. interface AskUserQuestionBlockDiagnostics {
  121. blocks: Array<{
  122. expanded: boolean;
  123. text: string;
  124. toolCallId: string | null;
  125. }>;
  126. matched: boolean;
  127. }
  128. function getExpandedAskUserQuestionBlockDiagnostics(
  129. text: string,
  130. ): Promise<AskUserQuestionBlockDiagnostics> {
  131. return browser.execute(
  132. (historyTriggerPrefix, toolBlockPrefix, summaryTriggerPrefix, expectedText) => {
  133. const normalizeText = (value: string | null | undefined) =>
  134. (value ?? "").replace(/\u00a0/g, " ").trim();
  135. const historyTriggers = Array.from(
  136. document.querySelectorAll<HTMLElement>(`[data-testid^="${historyTriggerPrefix}-"]`),
  137. );
  138. for (const trigger of historyTriggers) {
  139. if (
  140. trigger.getAttribute("data-history-has-content") === "true" &&
  141. trigger.getAttribute("aria-expanded") !== "true"
  142. ) {
  143. trigger.click();
  144. }
  145. }
  146. const blocks = Array.from(
  147. document.querySelectorAll<HTMLElement>(
  148. `[data-testid^="${toolBlockPrefix}-"][data-tool-name="AskUserQuestion"]`,
  149. ),
  150. );
  151. for (const block of blocks) {
  152. const trigger = block.querySelector<HTMLElement>(
  153. `[data-testid^="${summaryTriggerPrefix}-"]`,
  154. );
  155. if (trigger && trigger.getAttribute("aria-expanded") !== "true") {
  156. trigger.click();
  157. }
  158. }
  159. const snapshots = blocks.map((block) => {
  160. const trigger = block.querySelector<HTMLElement>(
  161. `[data-testid^="${summaryTriggerPrefix}-"]`,
  162. );
  163. return {
  164. expanded: trigger?.getAttribute("aria-expanded") === "true",
  165. text: normalizeText(block.innerText),
  166. toolCallId: block.getAttribute("data-tool-call-id"),
  167. };
  168. });
  169. return {
  170. blocks: snapshots,
  171. matched: snapshots.some((block) => block.text.includes(expectedText)),
  172. };
  173. },
  174. TID_CHAT_ASSISTANT_HISTORY_TRIGGER,
  175. TID_CHAT_TOOL_CALL_BLOCK,
  176. TID_TOOL_SUMMARY_TRIGGER,
  177. text,
  178. );
  179. }