conversation-session-ask-user-question-keyboard-default-focus.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  3. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  4. import {
  5. prepareV4ConversationE2E,
  6. sendV4Prompt,
  7. waitForV4TimelineContaining,
  8. } from "#e2e/helpers/v4-conversation";
  9. const MARKER_EMPTY_SUBMIT = "E2E_ADF_EMPTY_SUBMIT";
  10. const MARKER_TAB_ENTER = "E2E_ADF_TAB_ENTER";
  11. const MARKER_ENTER_SKIP = "E2E_ADF_ENTER_SKIP";
  12. const MARKER_MULTISELECT = "E2E_ADF_MULTISELECT";
  13. const MARKER_ESCAPE_DISMISS = "E2E_ADF_ESCAPE_DISMISS";
  14. const Q1_SINGLE = "E2E_ADF_Q1_SINGLE_SELECT";
  15. const Q2_MULTI = "E2E_ADF_Q2_MULTI_SELECT";
  16. const Q1_OPT_A = "E2E_ADF_Q1_A";
  17. const Q1_OPT_B = "E2E_ADF_Q1_B";
  18. const Q2_OPT_A = "E2E_ADF_Q2_A";
  19. const Q2_OPT_B = "E2E_ADF_Q2_B";
  20. const Q2_OPT_C = "E2E_ADF_Q2_C";
  21. describe("AskUserQuestion 默认无焦点键盘导航 E2E", () => {
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. });
  26. // Q15: 弹窗打开无预选 → 鼠标点 Submit → 提交空答案
  27. it("弹窗打开无预聚焦,鼠标点 Submit 提交空答案", async function () {
  28. this.timeout(180000);
  29. // Bug 根因:该 case 转正后仍使用 legacy conversation harness,
  30. // formal 证据无法确保操作的是当前 V4 pane。
  31. await prepareV4ConversationE2E();
  32. const runId = Date.now();
  33. const prompt = [
  34. `${MARKER_EMPTY_SUBMIT}_${runId}: Trigger AskUserQuestion with one single-select question.`,
  35. `Question "${Q1_SINGLE}" with options "${Q1_OPT_A}" and "${Q1_OPT_B}".`,
  36. "Do not answer. After the user responds, briefly confirm what was received.",
  37. ].join(" ");
  38. await sendV4Prompt(prompt);
  39. await waitForV4TimelineContaining(`${MARKER_EMPTY_SUBMIT}_${runId}`);
  40. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  41. // 确认弹窗出现且无选项高亮
  42. await waitForElicitationVisible(Q1_SINGLE);
  43. await expectNoOptionPreSelected();
  44. // 直接点 Submit
  45. await clickPrimaryAction();
  46. // 确认提交了空答案
  47. await waitForUpstreamRequest(
  48. {
  49. includes: [`${MARKER_EMPTY_SUBMIT}_${runId}`, "toolu_e2e_adf_empty_submit"],
  50. },
  51. "空答案提交后 Agent 没有收到回复",
  52. 60000,
  53. );
  54. });
  55. // Q18: 弹窗打开无预选 → Tab → 第一项聚焦 → Enter → 选中并推进
  56. it("无预聚焦时按 Tab 聚焦第一项,Enter 选中推进", async function () {
  57. this.timeout(180000);
  58. await prepareV4ConversationE2E();
  59. const runId = Date.now();
  60. const prompt = [
  61. `${MARKER_TAB_ENTER}_${runId}: Trigger AskUserQuestion with two questions.`,
  62. `Q1 single-select "${Q1_SINGLE}" with options "${Q1_OPT_A}" and "${Q1_OPT_B}".`,
  63. `Q2 single-select "${Q2_MULTI}" with options "${Q2_OPT_A}" and "${Q2_OPT_B}".`,
  64. "Do not answer. After the user responds, briefly confirm what was received.",
  65. ].join(" ");
  66. await sendV4Prompt(prompt);
  67. await waitForV4TimelineContaining(`${MARKER_TAB_ENTER}_${runId}`);
  68. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  69. // 确认无预选
  70. await waitForElicitationVisible(Q1_SINGLE);
  71. await expectNoOptionPreSelected();
  72. // Tab → 第一项聚焦
  73. await browser.keys("Tab");
  74. await expectActiveOption(Q1_OPT_A);
  75. // Enter → 选中并推进到 Q2
  76. await browser.keys("Enter");
  77. await waitForElicitationPage("2 / 2", Q2_MULTI);
  78. // Q2 也是无预选(切题后普通问答不预聚焦)。
  79. await expectNoOptionPreSelected();
  80. // 初始 ArrowDown 与 Tab 同样从卡片焦点启动,聚焦第一项但不选中。
  81. await browser.keys("ArrowDown");
  82. await expectActiveOption(Q2_OPT_A);
  83. await expectOptionSelected(Q2_OPT_A, false);
  84. await browser.keys("Enter");
  85. await waitForUpstreamRequest(
  86. { includes: [`${MARKER_TAB_ENTER}_${runId}`, Q1_OPT_A, Q2_OPT_A] },
  87. "Tab+Enter 选择结果没有进入下一次模型请求",
  88. 60000,
  89. );
  90. });
  91. // Q19: 弹窗打开无预选 → 直接 Enter → 跳过当前题推进
  92. it("无预聚焦时直接按 Enter 跳过当前题推进", async function () {
  93. this.timeout(180000);
  94. await prepareV4ConversationE2E();
  95. const runId = Date.now();
  96. const prompt = [
  97. `${MARKER_ENTER_SKIP}_${runId}: Trigger AskUserQuestion with two single-select questions.`,
  98. `Q1 "${Q1_SINGLE}" with options "${Q1_OPT_A}" and "${Q1_OPT_B}".`,
  99. `Q2 "${Q2_MULTI}" with options "${Q2_OPT_A}" and "${Q2_OPT_B}".`,
  100. "Do not answer. After the user responds, briefly confirm what was received.",
  101. ].join(" ");
  102. await sendV4Prompt(prompt);
  103. await waitForV4TimelineContaining(`${MARKER_ENTER_SKIP}_${runId}`);
  104. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  105. await waitForElicitationVisible(Q1_SINGLE);
  106. await expectNoOptionPreSelected();
  107. // 直接 Enter → 跳过 Q1
  108. await browser.keys("Enter");
  109. await waitForElicitationPage("2 / 2", Q2_MULTI);
  110. // Q22:后续题仍处于卡片初始焦点时,Escape 返回上一题。
  111. await expectNoOptionPreSelected();
  112. await browser.keys("Escape");
  113. await waitForElicitationPage("1 / 2", Q1_SINGLE);
  114. await expectNoOptionPreSelected();
  115. // 再次直接 Enter 跳过 Q1,证明返回没有隐式写入第一项。
  116. await browser.keys("Enter");
  117. await waitForElicitationPage("2 / 2", Q2_MULTI);
  118. // Tab → 选中 Q2 第一项 → Enter 提交
  119. await browser.keys("Tab");
  120. await expectActiveOption(Q2_OPT_A);
  121. await browser.keys("Enter");
  122. await waitForUpstreamRequest(
  123. {
  124. includes: [`${MARKER_ENTER_SKIP}_${runId}`, "toolu_e2e_adf_enter_skip", Q2_OPT_A],
  125. },
  126. "Q1 跳过 + Q2 Enter 选中后没有进入模型请求",
  127. 60000,
  128. );
  129. });
  130. // Q20: 多选题 Space 勾选/取消,Enter 确认
  131. it("多选题 Space 勾选取消,Enter 确认提交", async function () {
  132. this.timeout(180000);
  133. await prepareV4ConversationE2E();
  134. const runId = Date.now();
  135. const prompt = [
  136. `${MARKER_MULTISELECT}_${runId}: Trigger AskUserQuestion with one multi-select question.`,
  137. `Multi-select question "${Q2_MULTI}"`,
  138. `with options "${Q2_OPT_A}", "${Q2_OPT_B}", "${Q2_OPT_C}".`,
  139. "Do not answer. After the user responds, briefly confirm what was received.",
  140. ].join(" ");
  141. await sendV4Prompt(prompt);
  142. await waitForV4TimelineContaining(`${MARKER_MULTISELECT}_${runId}`);
  143. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  144. await waitForElicitationVisible(Q2_MULTI);
  145. await expectNoOptionSelected();
  146. // Tab 聚焦第一项
  147. await browser.keys("Tab");
  148. await expectActiveOption(Q2_OPT_A);
  149. // Space 勾选 Q2_OPT_A
  150. await browser.keys(" ");
  151. await expectOptionChecked(Q2_OPT_A, true);
  152. // ArrowDown 移到 Q2_OPT_B
  153. await browser.keys("ArrowDown");
  154. // Space 勾选 Q2_OPT_B
  155. await browser.keys(" ");
  156. await expectOptionChecked(Q2_OPT_B, true);
  157. // ArrowDown 移到 Q2_OPT_C,Space 勾选
  158. await browser.keys("ArrowDown");
  159. await browser.keys(" ");
  160. await expectOptionChecked(Q2_OPT_C, true);
  161. // ArrowUp 回到 Q2_OPT_B,Space 取消勾选
  162. await browser.keys("ArrowUp");
  163. await browser.keys(" ");
  164. await expectOptionChecked(Q2_OPT_B, false);
  165. // Enter 确认提交
  166. await browser.keys("Enter");
  167. await waitForUpstreamRequest(
  168. { includes: [`${MARKER_MULTISELECT}_${runId}`, Q2_OPT_A, Q2_OPT_C] },
  169. "多选 Space + Enter 结果没有进入模型请求",
  170. 60000,
  171. );
  172. });
  173. // Q22: 首题卡片初始焦点 → Escape → dismiss,并把 declined 结果回传 Agent
  174. it("首题无选项聚焦时按 Escape 关闭整组问答", async function () {
  175. this.timeout(180000);
  176. await prepareV4ConversationE2E();
  177. const runId = Date.now();
  178. const prompt = [
  179. `${MARKER_ESCAPE_DISMISS}_${runId}: Trigger AskUserQuestion with one single-select question.`,
  180. `Question "${Q1_SINGLE}" with options "${Q1_OPT_A}" and "${Q1_OPT_B}".`,
  181. "Do not answer. Stop if the user dismisses the question.",
  182. ].join(" ");
  183. await sendV4Prompt(prompt);
  184. await waitForV4TimelineContaining(`${MARKER_ESCAPE_DISMISS}_${runId}`);
  185. await waitForToolCallBlockByToolName("AskUserQuestion", 60000);
  186. await waitForElicitationVisible(Q1_SINGLE);
  187. await expectNoOptionPreSelected();
  188. await browser.keys("Escape");
  189. await waitForElicitationClosed();
  190. // 修复原因:负向 1200ms 抓包计数依赖 artifact 写盘时机,单跑可能在请求已经
  191. // 发出时仍读取旧文件而假绿。当前产品合同是 dismiss 关闭 UI,并把 declined
  192. // tool_result 交给模型收口;用 case-local final fixture 对该请求做正向取证。
  193. await waitForUpstreamRequest(
  194. {
  195. includes: [
  196. `${MARKER_ESCAPE_DISMISS}_${runId}`,
  197. "toolu_e2e_adf_escape_dismiss",
  198. "AskUserQuestion was declined",
  199. ],
  200. },
  201. "Escape dismiss 后 declined tool_result 没有进入模型收口请求",
  202. 60000,
  203. );
  204. await waitForV4TimelineContaining("E2E_ADF_ESCAPE_DISMISS_ACK", 60000);
  205. });
  206. });
  207. // --- helpers ---
  208. interface ElicitationKeyboardSnapshot {
  209. activeText: string;
  210. bodyText: string;
  211. cardFocused: boolean;
  212. pageText: string | null;
  213. selectedTexts: string[];
  214. visible: boolean;
  215. }
  216. async function waitForElicitationVisible(questionContains: string) {
  217. let latest: ElicitationKeyboardSnapshot | null = null;
  218. try {
  219. await browser.waitUntil(
  220. async () => {
  221. latest = await getElicitationKeyboardSnapshot();
  222. return latest.visible && latest.bodyText.includes(questionContains);
  223. },
  224. {
  225. timeout: 60000,
  226. timeoutMsg: `AskUserQuestion 弹窗没有出现或内容不包含 "${questionContains}"`,
  227. },
  228. );
  229. } catch (error) {
  230. latest = await getElicitationKeyboardSnapshot();
  231. throw new Error(`AskUserQuestion 弹窗没有出现; snapshot=${JSON.stringify(latest)}`, {
  232. cause: error,
  233. });
  234. }
  235. }
  236. async function waitForElicitationPage(pageText: string, questionContains: string) {
  237. let latest: ElicitationKeyboardSnapshot | null = null;
  238. try {
  239. await browser.waitUntil(
  240. async () => {
  241. latest = await getElicitationKeyboardSnapshot();
  242. return (
  243. latest.visible &&
  244. latest.pageText === pageText &&
  245. latest.bodyText.includes(questionContains)
  246. );
  247. },
  248. {
  249. timeout: 60000,
  250. timeoutMsg: `AskUserQuestion 没有进入目标题目 ${pageText} ${questionContains}`,
  251. },
  252. );
  253. } catch (error) {
  254. latest = await getElicitationKeyboardSnapshot();
  255. throw new Error(
  256. `AskUserQuestion 没有进入目标题目; expected=${pageText}/${questionContains}; snapshot=${JSON.stringify(latest)}`,
  257. { cause: error },
  258. );
  259. }
  260. }
  261. async function expectNoOptionPreSelected() {
  262. let latest: ElicitationKeyboardSnapshot | null = null;
  263. try {
  264. await browser.waitUntil(
  265. async () => {
  266. latest = await getElicitationKeyboardSnapshot();
  267. return latest.cardFocused && latest.activeText === "" && latest.selectedTexts.length === 0;
  268. },
  269. {
  270. timeout: 10000,
  271. timeoutMsg: "AskUserQuestion 弹窗不应有预选选项",
  272. },
  273. );
  274. } catch (error) {
  275. latest = await getElicitationKeyboardSnapshot();
  276. throw new Error(`AskUserQuestion 弹窗不应有预选选项; snapshot=${JSON.stringify(latest)}`, {
  277. cause: error,
  278. });
  279. }
  280. }
  281. async function expectNoOptionSelected() {
  282. let latest: ElicitationKeyboardSnapshot | null = null;
  283. try {
  284. await browser.waitUntil(
  285. async () => {
  286. latest = await getElicitationKeyboardSnapshot();
  287. return latest.selectedTexts.length === 0;
  288. },
  289. {
  290. timeout: 10000,
  291. timeoutMsg: "AskUserQuestion 弹窗不应有已选选项",
  292. },
  293. );
  294. } catch (error) {
  295. latest = await getElicitationKeyboardSnapshot();
  296. throw new Error(`AskUserQuestion 弹窗不应有已选选项; snapshot=${JSON.stringify(latest)}`, {
  297. cause: error,
  298. });
  299. }
  300. }
  301. async function expectActiveOption(optionText: string) {
  302. let latest: ElicitationKeyboardSnapshot | null = null;
  303. try {
  304. await browser.waitUntil(
  305. async () => {
  306. latest = await getElicitationKeyboardSnapshot();
  307. return latest.activeText.includes(optionText);
  308. },
  309. {
  310. timeout: 10000,
  311. timeoutMsg: `AskUserQuestion 当前焦点选项不是 "${optionText}"`,
  312. },
  313. );
  314. } catch (error) {
  315. latest = await getElicitationKeyboardSnapshot();
  316. throw new Error(
  317. `AskUserQuestion 当前焦点选项不是 "${optionText}"; snapshot=${JSON.stringify(latest)}`,
  318. { cause: error },
  319. );
  320. }
  321. }
  322. async function expectOptionChecked(optionText: string, checked: boolean) {
  323. let latest: ElicitationKeyboardSnapshot | null = null;
  324. try {
  325. await browser.waitUntil(
  326. async () => {
  327. latest = await getElicitationKeyboardSnapshot();
  328. const isSelected = latest.selectedTexts.some((t) => t.includes(optionText));
  329. return isSelected === checked;
  330. },
  331. {
  332. timeout: 10000,
  333. timeoutMsg: `AskUserQuestion 勾选状态不符合预期; option=${optionText} expectedChecked=${checked}`,
  334. },
  335. );
  336. } catch (error) {
  337. latest = await getElicitationKeyboardSnapshot();
  338. throw new Error(
  339. `AskUserQuestion 勾选状态不符合预期; option=${optionText} expectedChecked=${checked}; snapshot=${JSON.stringify(latest)}`,
  340. { cause: error },
  341. );
  342. }
  343. }
  344. async function expectOptionSelected(optionText: string, selected: boolean) {
  345. let latest: ElicitationKeyboardSnapshot | null = null;
  346. try {
  347. await browser.waitUntil(
  348. async () => {
  349. latest = await getElicitationKeyboardSnapshot();
  350. const isSelected = latest.selectedTexts.some((text) => text.includes(optionText));
  351. return isSelected === selected;
  352. },
  353. {
  354. timeout: 10000,
  355. timeoutMsg: `AskUserQuestion 单选状态不符合预期; option=${optionText} expectedSelected=${selected}`,
  356. },
  357. );
  358. } catch (error) {
  359. latest = await getElicitationKeyboardSnapshot();
  360. throw new Error(
  361. `AskUserQuestion 单选状态不符合预期; option=${optionText} expectedSelected=${selected}; snapshot=${JSON.stringify(latest)}`,
  362. { cause: error },
  363. );
  364. }
  365. }
  366. async function clickPrimaryAction() {
  367. const button = await $('[data-elicitation-dialog-footer="true"] button:last-child');
  368. await button.waitForDisplayed({ timeout: 10000 });
  369. await button.click();
  370. }
  371. async function waitForElicitationClosed() {
  372. await browser.waitUntil(
  373. async () =>
  374. !(await browser.execute(() =>
  375. Boolean(document.querySelector('[data-elicitation-dialog-card="true"]')),
  376. )),
  377. {
  378. timeout: 10000,
  379. timeoutMsg: "AskUserQuestion 弹窗没有在 Escape 后关闭",
  380. },
  381. );
  382. }
  383. function getElicitationKeyboardSnapshot(): Promise<ElicitationKeyboardSnapshot> {
  384. return browser.execute(() => {
  385. const normalizeText = (value: string | null | undefined) =>
  386. (value ?? "").replace(/\u00a0/g, " ").trim();
  387. const body = document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]');
  388. const focused = document.activeElement as HTMLElement | null;
  389. const card = document.querySelector<HTMLElement>('[data-elicitation-dialog-card="true"]');
  390. // 修复原因:roving tabindex 会在卡片聚焦时仍给第一项 tabindex=0;只有
  391. // document.activeElement 才能证明选项获得了真实键盘焦点。
  392. const activeOption = focused?.closest<HTMLElement>('button[role="option"],[role="checkbox"]');
  393. const pageText =
  394. normalizeText(body?.innerText)
  395. .match(/\b\d+\s*\/\s*\d+\b/)?.[0]
  396. ?.replace(/\s+/g, " ") ?? null;
  397. const selectedTexts = Array.from(
  398. body?.querySelectorAll<HTMLElement>(
  399. 'button[role="option"][aria-selected="true"],[role="checkbox"][aria-checked="true"]',
  400. ) ?? [],
  401. ).map((element) => normalizeText(element.innerText));
  402. return {
  403. activeText: normalizeText(activeOption?.innerText),
  404. bodyText: normalizeText(body?.innerText),
  405. cardFocused: focused === card,
  406. pageText,
  407. selectedTexts,
  408. visible: Boolean(body),
  409. };
  410. });
  411. }