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

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