conversation-session-ask-user-question-answer-visible.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. prepareV4ConversationE2E,
  5. sendV4Prompt,
  6. waitForV4ComposerText,
  7. waitForV4UserMessageContaining,
  8. } from "#e2e/helpers/v4-conversation";
  9. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  10. import {
  11. expandAssistantHistoriesWithContent,
  12. waitForToolCallBlockByToolName,
  13. } from "#e2e/helpers/conversation-session-tool";
  14. const PROMPT_MARKER = "E2E_ASK_USER_QUESTION_ANSWER_VISIBLE";
  15. const LEGACY_TOOL_MARKER = "E2E_TOOL_CROSS_PRODUCT_ASK_USER_QUESTION";
  16. const ANSWER_MARKER = "E2E_ASK_USER_QUESTION_CUSTOM_ANSWER";
  17. describe("会话区 AskUserQuestion 问答响应可见性 E2E", () => {
  18. after(async () => {
  19. await browser.electron.restoreAllMocks();
  20. await clearAppData();
  21. });
  22. it("自定义问答响应被 agent 消费后仍应保留在 AskUserQuestion 消息里", async function () {
  23. this.timeout(150000);
  24. await prepareV4ConversationE2E();
  25. const runId = Date.now();
  26. const prompt = [
  27. `${PROMPT_MARKER}_${runId}: Trigger the AskUserQuestion tool exactly once.`,
  28. `${LEGACY_TOOL_MARKER}: ask a single question before continuing.`,
  29. "After the user answers, continue with a brief acknowledgement.",
  30. ].join(" ");
  31. const answer = `${ANSWER_MARKER}_${runId}`;
  32. await sendV4Prompt(prompt);
  33. await waitForV4ComposerText("", "AskUserQuestion 首发后输入框没有清空");
  34. await waitForV4UserMessageContaining(`${PROMPT_MARKER}_${runId}`);
  35. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  36. await waitForElicitationDialog();
  37. await submitCustomElicitationAnswer(answer);
  38. await waitForUpstreamRequest(
  39. {
  40. includes: [answer, LEGACY_TOOL_MARKER],
  41. },
  42. "AskUserQuestion 自定义回答没有进入下一次模型请求",
  43. 60000,
  44. );
  45. await waitForExpandedAskUserQuestionBlockContaining(answer, 8000);
  46. });
  47. });
  48. async function waitForElicitationDialog() {
  49. let latest = "";
  50. try {
  51. await browser.waitUntil(
  52. async () => {
  53. latest = await getElicitationDialogText();
  54. return latest.length > 0;
  55. },
  56. {
  57. timeout: 60000,
  58. timeoutMsg: "AskUserQuestion 问答卡片没有出现",
  59. },
  60. );
  61. } catch (error) {
  62. latest = await getElicitationDialogText();
  63. throw new Error(`AskUserQuestion 问答卡片没有出现; latest=${latest}`, {
  64. cause: error,
  65. });
  66. }
  67. }
  68. async function submitCustomElicitationAnswer(answer: string) {
  69. const input = await $('[data-elicitation-dialog-body="true"] textarea');
  70. await input.waitForDisplayed({
  71. timeout: 30000,
  72. timeoutMsg: "AskUserQuestion 自定义回答输入框没有出现",
  73. });
  74. await input.setValue(answer);
  75. await browser.waitUntil(async () => (await input.getValue()) === answer, {
  76. timeout: 10000,
  77. timeoutMsg: "AskUserQuestion 自定义回答没有写入输入框",
  78. });
  79. const clicked = (await browser.execute(() => {
  80. const normalizeText = (value: string | null | undefined) =>
  81. (value ?? "").replace(/\u00a0/g, " ").trim();
  82. const footer = document.querySelector<HTMLElement>('[data-elicitation-dialog-footer="true"]');
  83. const button = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  84. (candidate) => {
  85. const text = normalizeText(candidate.textContent);
  86. return text === "Submit" || text === "提交";
  87. },
  88. );
  89. button?.click();
  90. return Boolean(button);
  91. })) as boolean;
  92. expect(clicked).toBe(true);
  93. }
  94. function getElicitationDialogText() {
  95. return browser.execute(() => {
  96. const normalizeText = (value: string | null | undefined) =>
  97. (value ?? "").replace(/\u00a0/g, " ").trim();
  98. return normalizeText(
  99. document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]')?.innerText,
  100. );
  101. });
  102. }
  103. async function waitForExpandedAskUserQuestionBlockContaining(text: string, timeout: number) {
  104. let latest: AskUserQuestionBlockDiagnostics | null = null;
  105. try {
  106. await browser.waitUntil(
  107. async () => {
  108. // V4 已移除旧历史属性;失败录像中的已工作详情仍折叠,须先经真实 UI 展开。
  109. await expandAssistantHistoriesWithContent();
  110. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  111. return latest.matched;
  112. },
  113. {
  114. timeout,
  115. timeoutMsg: "AskUserQuestion 自定义回答没有显示在已询问消息详情里",
  116. },
  117. );
  118. } catch (error) {
  119. latest = await getExpandedAskUserQuestionBlockDiagnostics(text);
  120. throw new Error(
  121. `AskUserQuestion 自定义回答没有显示在已询问消息详情里; diagnostics=${JSON.stringify(latest)}`,
  122. { cause: error },
  123. );
  124. }
  125. }
  126. interface AskUserQuestionBlockDiagnostics {
  127. blocks: Array<{
  128. expanded: boolean;
  129. text: string;
  130. toolCallId: string | null;
  131. }>;
  132. matched: boolean;
  133. }
  134. function getExpandedAskUserQuestionBlockDiagnostics(
  135. text: string,
  136. ): Promise<AskUserQuestionBlockDiagnostics> {
  137. return browser.execute(
  138. (toolBlockPrefix, summaryTriggerPrefix, expectedText) => {
  139. const normalizeText = (value: string | null | undefined) =>
  140. (value ?? "").replace(/\u00a0/g, " ").trim();
  141. const blocks = Array.from(
  142. document.querySelectorAll<HTMLElement>(
  143. `[data-testid^="${toolBlockPrefix}-"][data-tool-name="AskUserQuestion"]`,
  144. ),
  145. );
  146. // 修复原因:AskUserQuestion 折叠态只展示统计,回答内容保留在展开详情里。
  147. for (const block of blocks) {
  148. const trigger = block.querySelector<HTMLElement>(
  149. `[data-testid^="${summaryTriggerPrefix}-"]`,
  150. );
  151. if (trigger && trigger.getAttribute("aria-expanded") !== "true") {
  152. trigger.click();
  153. }
  154. }
  155. const snapshots = blocks.map((block) => {
  156. const trigger = block.querySelector<HTMLElement>(
  157. `[data-testid^="${summaryTriggerPrefix}-"]`,
  158. );
  159. return {
  160. expanded: trigger?.getAttribute("aria-expanded") === "true",
  161. text: normalizeText(block.innerText),
  162. toolCallId: block.getAttribute("data-tool-call-id"),
  163. };
  164. });
  165. return {
  166. blocks: snapshots,
  167. matched: snapshots.some((block) => block.text.includes(expectedText)),
  168. };
  169. },
  170. TID_CHAT_TOOL_CALL_BLOCK,
  171. TID_TOOL_SUMMARY_TRIGGER,
  172. text,
  173. );
  174. }