conversation-session-ask-user-question-auto-resolution-setting.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { ASK_USER_QUESTION_E2E_CLOCK_SCALE_ENV } from "@zcode/shared";
  2. import {
  3. TID_SETTINGS_ASK_USER_QUESTION_AUTO_RESOLUTION_SWITCH,
  4. TID_SETTINGS_BACK_BUTTON,
  5. TID_SETTINGS_PAGE,
  6. TID_SETTINGS_SECTION_NAV,
  7. TID_TASK_SETTINGS_BUTTON,
  8. testId,
  9. } from "@zcode/shared/test-ids";
  10. import { clearAppData, clickTestIdByDom, readSettings } from "#e2e/helpers/desktop-app";
  11. import {
  12. prepareV4ConversationE2E,
  13. sendV4Prompt,
  14. waitForV4AssistantMessageContaining,
  15. waitForV4ComposerText,
  16. waitForV4UserMessageContaining,
  17. } from "#e2e/helpers/v4-conversation";
  18. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  19. const FIRST_PROMPT = "E2E_ASK_AUTO_RESOLUTION_SETTING_FIRST";
  20. const FIRST_ANSWER = "E2E_ASK_AUTO_RESOLUTION_SETTING_FIRST_ANSWER";
  21. const FIRST_FINAL = "E2E_ASK_AUTO_RESOLUTION_SETTING_FIRST_DONE";
  22. const SECOND_PROMPT = "E2E_ASK_AUTO_RESOLUTION_SETTING_SECOND";
  23. const SECOND_FINAL = "E2E_ASK_AUTO_RESOLUTION_SETTING_SECOND_AUTO_DONE";
  24. describe("AskUserQuestion 自动继续设置 GUI 候选", () => {
  25. after(async () => {
  26. await browser.electron.restoreAllMocks();
  27. await clearAppData();
  28. });
  29. it("计时中关闭后跨 deadline 仍等待,重开不追溯,新问题恢复自动继续", async function () {
  30. this.timeout(180_000);
  31. const clockScale = requireInjectedClockScale();
  32. const acceleratedDeadlineMs = Math.ceil(300_000 / clockScale);
  33. await prepareV4ConversationE2E();
  34. await setAutoResolutionEnabled(true);
  35. await sendV4Prompt(
  36. `${FIRST_PROMPT}: call AskUserQuestion exactly once, then wait for my answer. ` +
  37. `After the answer, reply with ${FIRST_FINAL}.`,
  38. );
  39. await waitForV4ComposerText("", "首个 AskUserQuestion 提交后输入框没有清空");
  40. await waitForV4UserMessageContaining(FIRST_PROMPT);
  41. await waitForToolCallBlockByToolName("AskUserQuestion", 60_000);
  42. await waitForElicitationDialog(FIRST_PROMPT);
  43. await setAutoResolutionEnabled(false);
  44. await waitForElicitationDialog(FIRST_PROMPT);
  45. await browser.pause(acceleratedDeadlineMs + 1_000);
  46. await waitForElicitationDialog(FIRST_PROMPT);
  47. await setAutoResolutionEnabled(true);
  48. await browser.pause(acceleratedDeadlineMs + 1_000);
  49. await waitForElicitationDialog(FIRST_PROMPT);
  50. await submitCustomAnswer(FIRST_ANSWER);
  51. await waitForV4AssistantMessageContaining(FIRST_FINAL);
  52. await sendV4Prompt(
  53. `${SECOND_PROMPT}: call AskUserQuestion exactly once, then wait for my answer. ` +
  54. `After automatic continuation, reply with ${SECOND_FINAL}.`,
  55. );
  56. await waitForV4ComposerText("", "第二个 AskUserQuestion 提交后输入框没有清空");
  57. await waitForToolCallBlockByToolName("AskUserQuestion", 60_000);
  58. await waitForElicitationDialog(SECOND_PROMPT);
  59. await waitForV4AssistantMessageContaining(SECOND_FINAL);
  60. });
  61. });
  62. function requireInjectedClockScale(): number {
  63. const rawScale = process.env[ASK_USER_QUESTION_E2E_CLOCK_SCALE_ENV]?.trim();
  64. const scale = Number(rawScale);
  65. if (!Number.isFinite(scale) || scale < 20 || scale > 30) {
  66. throw new Error(
  67. `本候选需要 ${ASK_USER_QUESTION_E2E_CLOCK_SCALE_ENV}=20(允许 20-30),` +
  68. "以 test 环境的可注入 runtime clock 跨越原五分钟 deadline。",
  69. );
  70. }
  71. return scale;
  72. }
  73. async function setAutoResolutionEnabled(enabled: boolean): Promise<void> {
  74. await openAgentBehaviorSettings();
  75. if ((await readAutoResolutionSwitch()) !== enabled) {
  76. await clickTestIdByDom(TID_SETTINGS_ASK_USER_QUESTION_AUTO_RESOLUTION_SWITCH, {
  77. timeout: 15_000,
  78. timeoutMsg: "提问自动继续开关不可点击",
  79. });
  80. }
  81. await browser.waitUntil(
  82. async () => {
  83. const checked = await readAutoResolutionSwitch();
  84. const persisted = (await readSettings()).askUserQuestionAutoResolutionEnabled;
  85. return checked === enabled && persisted === enabled;
  86. },
  87. {
  88. timeout: 15_000,
  89. timeoutMsg: `提问自动继续设置没有持久化为 ${String(enabled)}`,
  90. },
  91. );
  92. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  93. timeout: 15_000,
  94. timeoutMsg: "设置页返回按钮没有出现",
  95. });
  96. }
  97. async function openAgentBehaviorSettings(): Promise<void> {
  98. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  99. timeout: 15_000,
  100. timeoutMsg: "没有找到设置入口按钮",
  101. });
  102. await browser.waitUntil(
  103. async () =>
  104. browser.execute(
  105. (settingsPageTestId) =>
  106. Boolean(document.querySelector(`[data-testid="${settingsPageTestId}"]`)),
  107. TID_SETTINGS_PAGE,
  108. ),
  109. { timeout: 15_000, timeoutMsg: "设置页没有打开" },
  110. );
  111. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "agentBehavior"), {
  112. timeout: 15_000,
  113. timeoutMsg: "设置页没有行为与工具分区入口",
  114. });
  115. await browser.waitUntil(async () => (await readAutoResolutionSwitch()) !== null, {
  116. timeout: 15_000,
  117. timeoutMsg: "行为与工具设置没有提问自动继续开关",
  118. });
  119. }
  120. function readAutoResolutionSwitch(): Promise<boolean | null> {
  121. return browser.execute((switchTestId) => {
  122. const control = document.querySelector<HTMLElement>(`[data-testid="${switchTestId}"]`);
  123. if (!control) return null;
  124. return (
  125. control.getAttribute("aria-checked") === "true" ||
  126. control.getAttribute("data-state") === "checked"
  127. );
  128. }, TID_SETTINGS_ASK_USER_QUESTION_AUTO_RESOLUTION_SWITCH);
  129. }
  130. async function waitForElicitationDialog(marker: string): Promise<void> {
  131. let latest = "";
  132. await browser.waitUntil(
  133. async () => {
  134. latest = await browser.execute(() => {
  135. const body = document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]');
  136. return body?.innerText ?? "";
  137. });
  138. return latest.includes(marker);
  139. },
  140. {
  141. timeout: 15_000,
  142. timeoutMsg: `AskUserQuestion 仍未处于等待态: marker=${marker}; latest=${latest}`,
  143. },
  144. );
  145. }
  146. async function submitCustomAnswer(answer: string): Promise<void> {
  147. const input = await $('[data-elicitation-dialog-body="true"] textarea');
  148. await input.waitForDisplayed({ timeout: 15_000 });
  149. await input.setValue(answer);
  150. const submitted = (await browser.execute(() => {
  151. const footer = document.querySelector<HTMLElement>('[data-elicitation-dialog-footer="true"]');
  152. const button = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  153. (candidate) => {
  154. const text = (candidate.textContent ?? "").trim();
  155. return text === "Submit" || text === "提交";
  156. },
  157. );
  158. button?.click();
  159. return Boolean(button);
  160. })) as boolean;
  161. expect(submitted).toBe(true);
  162. }