conversation-session-native-search-setting.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  2. import { openWorkspaceDraftFromSidebar } from "#e2e/helpers/workspace-agent-warmup";
  3. // NSE01-NSE04/NSE09:增强 Find/Grep 只在根 Session runtime 物化时读取全局设置。
  4. import {
  5. TID_SETTINGS_BACK_BUTTON,
  6. TID_SETTINGS_NATIVE_SEARCH_SWITCH,
  7. TID_SETTINGS_PAGE,
  8. TID_SETTINGS_SECTION_NAV,
  9. TID_TASK_SETTINGS_BUTTON,
  10. TID_TOOL_SUMMARY_TRIGGER,
  11. testId,
  12. } from "@zcode/shared/test-ids";
  13. import { clearAppData, clickTestIdByDom, readSettings } from "#e2e/helpers/desktop-app";
  14. import {
  15. expandAssistantHistoriesWithContent,
  16. expandVisibleSummaryGroups,
  17. getToolCallDiagnostics,
  18. } from "#e2e/helpers/conversation-session-tool-diagnostics";
  19. import { restartIntoWorkspace } from "#e2e/helpers/model-provider-restart";
  20. import {
  21. approveV4Permission,
  22. getV4PaneSnapshot,
  23. prepareV4ConversationE2E,
  24. selectV4TaskById,
  25. sendV4Prompt,
  26. waitForV4Pane,
  27. waitForV4TimelineContaining,
  28. } from "#e2e/helpers/v4-conversation";
  29. const INITIAL_TOOL_CALL_ID = "toolu_e2e_nse_enabled_initial";
  30. const ACTIVE_TOOL_CALL_ID = "toolu_e2e_nse_enabled_active";
  31. const DISABLED_TOOL_CALL_ID = "toolu_e2e_nse_disabled_new";
  32. const ENABLED_COLD_RESUME_TOOL_CALL_ID = "toolu_e2e_nse_enabled_cold_resume";
  33. const DISABLED_COLD_RESUME_TOOL_CALL_ID = "toolu_e2e_nse_disabled_cold_resume";
  34. describe("增强 Find 与 Grep Session 生命周期", () => {
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("默认启用,active Session 保持原值,新建和冷恢复 Session 读取最新设置", async () => {
  40. await prepareV4ConversationE2E();
  41. await openNativeSearchSettings();
  42. expect(await readNativeSearchSwitch()).toBe(true);
  43. await closeSettings();
  44. await sendV4Prompt("E2E_NSE_ENABLED_INITIAL 执行增强搜索版本检查");
  45. expect(await approveV4Permission()).toBe(true);
  46. await waitForV4TimelineContaining("E2E_NSE_ENABLED_INITIAL_DONE", 60_000);
  47. const initialSessionId = await requireActiveSessionId();
  48. const initialOutput = await waitForBashOutput(INITIAL_TOOL_CALL_ID);
  49. assertEnabledOutput(initialOutput);
  50. const expectedRgVersion = readRgVersion(initialOutput);
  51. await setNativeSearchEnhancementsEnabled(false, initialSessionId);
  52. await openNativeSearchSettings();
  53. expect(await readNativeSearchSwitch()).toBe(false);
  54. await closeSettings(initialSessionId);
  55. await sendV4Prompt("E2E_NSE_ENABLED_ACTIVE 切换设置后检查当前会话");
  56. expect(await approveV4Permission()).toBe(true);
  57. await waitForV4TimelineContaining("E2E_NSE_ENABLED_ACTIVE_DONE", 60_000);
  58. expect(await requireActiveSessionId()).toBe(initialSessionId);
  59. const activeOutput = await waitForBashOutput(ACTIVE_TOOL_CALL_ID);
  60. assertEnabledOutput(activeOutput);
  61. expect(readRgVersion(activeOutput)).toBe(expectedRgVersion);
  62. // 冷恢复夹具固定在项目目录;全局新建会创建独立 cwd,不能复用该目录的记录器。
  63. await openWorkspaceDraftFromSidebar(DEFAULT_WORKSPACE);
  64. await sendV4Prompt("E2E_NSE_DISABLED_NEW 检查关闭增强搜索后的新会话");
  65. expect(await approveV4Permission()).toBe(true);
  66. await waitForV4TimelineContaining("E2E_NSE_DISABLED_NEW_DONE", 60_000);
  67. const disabledSessionId = await requireActiveSessionId();
  68. expect(disabledSessionId).not.toBe(initialSessionId);
  69. const disabledOutput = await waitForBashOutput(DISABLED_TOOL_CALL_ID);
  70. assertDisabledOutput(disabledOutput);
  71. expect(readRgVersion(disabledOutput)).toBe(expectedRgVersion);
  72. await setNativeSearchEnhancementsEnabled(true, disabledSessionId);
  73. await restartIntoWorkspace();
  74. await openNativeSearchSettings();
  75. expect(await readNativeSearchSwitch()).toBe(true);
  76. await closeSettings();
  77. await selectV4TaskById(disabledSessionId);
  78. await sendV4Prompt("E2E_NSE_ENABLED_COLD_RESUME 冷恢复后检查最新设置");
  79. expect(await approveV4Permission()).toBe(true);
  80. await waitForV4TimelineContaining("E2E_NSE_ENABLED_COLD_RESUME_DONE", 60_000);
  81. const enabledColdResumeOutput = await waitForBashOutput(ENABLED_COLD_RESUME_TOOL_CALL_ID);
  82. assertEnabledOutput(enabledColdResumeOutput);
  83. expect(readRgVersion(enabledColdResumeOutput)).toBe(expectedRgVersion);
  84. await setNativeSearchEnhancementsEnabled(false, disabledSessionId);
  85. await restartIntoWorkspace();
  86. await openNativeSearchSettings();
  87. expect(await readNativeSearchSwitch()).toBe(false);
  88. await closeSettings();
  89. await selectV4TaskById(disabledSessionId);
  90. await sendV4Prompt("E2E_NSE_DISABLED_COLD_RESUME 冷恢复后检查最新关闭设置");
  91. expect(await approveV4Permission()).toBe(true);
  92. await waitForV4TimelineContaining("E2E_NSE_DISABLED_COLD_RESUME_DONE", 60_000);
  93. const disabledColdResumeOutput = await waitForBashOutput(DISABLED_COLD_RESUME_TOOL_CALL_ID);
  94. assertDisabledOutput(disabledColdResumeOutput);
  95. expect(readRgVersion(disabledColdResumeOutput)).toBe(expectedRgVersion);
  96. });
  97. });
  98. async function openNativeSearchSettings(): Promise<void> {
  99. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  100. timeout: 15_000,
  101. timeoutMsg: "没有找到设置入口按钮",
  102. });
  103. await browser.waitUntil(
  104. async () =>
  105. browser.execute(
  106. (settingsPageTestId) =>
  107. Boolean(document.querySelector(`[data-testid="${settingsPageTestId}"]`)),
  108. TID_SETTINGS_PAGE,
  109. ),
  110. { timeout: 15_000, timeoutMsg: "设置页没有打开" },
  111. );
  112. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "agentBehavior"), {
  113. timeout: 15_000,
  114. timeoutMsg: "设置页没有行为与工具分区入口",
  115. });
  116. await browser.waitUntil(async () => (await readNativeSearchSwitch()) !== null, {
  117. timeout: 15_000,
  118. timeoutMsg: "行为与工具设置没有增强 Find 与 Grep 开关",
  119. });
  120. }
  121. async function closeSettings(expectedSessionId?: string): Promise<void> {
  122. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  123. timeout: 15_000,
  124. timeoutMsg: "设置页返回按钮没有出现",
  125. });
  126. await waitForV4Pane(
  127. (snapshot) =>
  128. expectedSessionId ? snapshot.sessionId === expectedSessionId : snapshot.sessionId !== null,
  129. expectedSessionId
  130. ? `返回工作区后没有恢复 Session ${expectedSessionId}`
  131. : "返回工作区后 V4 pane 没有恢复",
  132. 30_000,
  133. );
  134. }
  135. async function setNativeSearchEnhancementsEnabled(
  136. enabled: boolean,
  137. expectedSessionId: string,
  138. ): Promise<void> {
  139. await openNativeSearchSettings();
  140. if ((await readNativeSearchSwitch()) !== enabled) {
  141. await clickTestIdByDom(TID_SETTINGS_NATIVE_SEARCH_SWITCH, {
  142. timeout: 15_000,
  143. timeoutMsg: "增强 Find 与 Grep 开关不可点击",
  144. });
  145. }
  146. await browser.waitUntil(
  147. async () => {
  148. const checked = await readNativeSearchSwitch();
  149. const persisted = (await readSettings()).nativeSearchEnhancementsEnabled;
  150. return checked === enabled && persisted === enabled;
  151. },
  152. {
  153. timeout: 15_000,
  154. timeoutMsg: `增强 Find 与 Grep 设置没有持久化为 ${String(enabled)}`,
  155. },
  156. );
  157. await closeSettings(expectedSessionId);
  158. }
  159. function readNativeSearchSwitch(): Promise<boolean | null> {
  160. return browser.execute((switchTestId) => {
  161. const control = document.querySelector<HTMLElement>(`[data-testid="${switchTestId}"]`);
  162. if (!control) return null;
  163. return (
  164. control.getAttribute("aria-checked") === "true" ||
  165. control.getAttribute("data-state") === "checked"
  166. );
  167. }, TID_SETTINGS_NATIVE_SEARCH_SWITCH);
  168. }
  169. async function requireActiveSessionId(): Promise<string> {
  170. const sessionId = (await getV4PaneSnapshot()).sessionId;
  171. if (!sessionId || sessionId === "draft") {
  172. throw new Error(`V4 pane 没有活动 Session: ${String(sessionId)}`);
  173. }
  174. return sessionId;
  175. }
  176. async function waitForBashOutput(toolCallId: string): Promise<string> {
  177. let latestOutput: string | null = null;
  178. await browser.waitUntil(
  179. async () => {
  180. await expandAssistantHistoriesWithContent();
  181. await expandVisibleSummaryGroups();
  182. await browser.execute(
  183. (id, summaryTriggerPrefix) => {
  184. const block = document.querySelector<HTMLElement>(`[data-tool-call-id="${id}"]`);
  185. const trigger = block?.querySelector<HTMLElement>(
  186. `[data-testid^="${summaryTriggerPrefix}-"][aria-expanded]`,
  187. );
  188. if (trigger?.getAttribute("aria-expanded") === "false") {
  189. trigger.click();
  190. }
  191. },
  192. toolCallId,
  193. TID_TOOL_SUMMARY_TRIGGER,
  194. );
  195. const diagnostics = await getToolCallDiagnostics({
  196. type: "toolCallId",
  197. value: toolCallId,
  198. });
  199. latestOutput = diagnostics.matchedBlock?.text ?? null;
  200. return (
  201. diagnostics.matchedBlock?.status === "completed" &&
  202. latestOutput?.includes("ripgrep ") === true
  203. );
  204. },
  205. {
  206. timeout: 60_000,
  207. timeoutMsg: `没有读取到 Bash 真实输出: ${toolCallId}`,
  208. },
  209. );
  210. if (!latestOutput) {
  211. throw new Error(`Bash 输出为空: ${toolCallId}`);
  212. }
  213. return normalizeOutput(latestOutput);
  214. }
  215. function assertEnabledOutput(output: string): void {
  216. expect(output).toContain("grep-kind=function");
  217. expect(output).toContain("ugrep 7.8.4");
  218. expect(output).toContain("ripgrep ");
  219. if (process.platform === "win32") {
  220. expect(output).toContain("find-kind=file");
  221. expect(output).toContain("GNU findutils");
  222. } else {
  223. expect(output).toContain("find-kind=function");
  224. expect(output).toContain("bfs 4.1.1");
  225. }
  226. }
  227. function assertDisabledOutput(output: string): void {
  228. expect(output).toContain("find-kind=file");
  229. expect(output).toContain("grep-kind=file");
  230. expect(output).toContain("__E2E_NSE_FIND_RESULT__\n.");
  231. expect(output).toContain("__E2E_NSE_GREP_RESULT__\nneedle");
  232. expect(output).toContain("ripgrep ");
  233. expect(output).not.toContain("bfs 4.1.1");
  234. expect(output).not.toContain("ugrep 7.8.4");
  235. }
  236. function readRgVersion(output: string): string {
  237. const version = output.match(/ripgrep [^\n]+/)?.[0]?.trim();
  238. if (!version) throw new Error(`Bash 输出缺少 ripgrep 版本: ${output}`);
  239. return version;
  240. }
  241. function normalizeOutput(output: string): string {
  242. return output.replaceAll("\\", "/").replaceAll("\r\n", "\n");
  243. }