settingsDataBaseDirControl.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. const capturedButtons: Array<Record<string, unknown>> = [];
  5. const capturedInputs: Array<Record<string, unknown>> = [];
  6. const capturedSwitches: Array<Record<string, unknown>> = [];
  7. vi.mock("react", async () => {
  8. const actual = await vi.importActual<typeof import("react")>("react");
  9. return {
  10. ...actual,
  11. useState: (initialValue: unknown) => [initialValue, vi.fn()] as const,
  12. };
  13. });
  14. vi.mock("@/components/lib/utils.js", () => ({
  15. cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(" "),
  16. }));
  17. vi.mock("@/components/ui/button.js", () => ({
  18. Button: ({ children, ...props }: { children: unknown; [key: string]: unknown }) => {
  19. capturedButtons.push({ children, ...props });
  20. return createElement("button", props, children);
  21. },
  22. }));
  23. vi.mock("@/components/ui/input.js", () => ({
  24. Input: (props: Record<string, unknown>) => {
  25. capturedInputs.push(props);
  26. return createElement("input", props);
  27. },
  28. }));
  29. vi.mock("@/components/ui/switch.js", () => ({
  30. Switch: (props: Record<string, unknown>) => {
  31. capturedSwitches.push(props);
  32. return createElement("input", { type: "checkbox", ...props });
  33. },
  34. }));
  35. vi.mock("@/components/ui/select.js", () => ({
  36. Select: ({ children }: { children: unknown }) => createElement("div", null, children),
  37. SelectContent: ({ children }: { children: unknown }) => createElement("div", null, children),
  38. SelectItem: ({ children }: { children: unknown }) => createElement("div", null, children),
  39. SelectTrigger: ({ children }: { children: unknown }) => createElement("button", null, children),
  40. SelectValue: () => createElement("span"),
  41. }));
  42. vi.mock("@/components/ui/card.js", () => ({
  43. Card: ({ children, ...props }: { children: unknown; [key: string]: unknown }) =>
  44. createElement("div", props, children),
  45. CardContent: ({ children, ...props }: { children: unknown; [key: string]: unknown }) =>
  46. createElement("div", props, children),
  47. }));
  48. vi.mock("@/settings/SettingsPageParts.js", () => ({
  49. SettingsBadge: ({ children }: { children: unknown }) => createElement("span", null, children),
  50. SettingsGroupCard: ({ children }: { children: unknown }) =>
  51. createElement("section", { "data-settings-group": true }, children),
  52. SettingsRow: ({
  53. control,
  54. description,
  55. detail,
  56. }: {
  57. control: unknown;
  58. description?: unknown;
  59. detail?: unknown;
  60. }) => createElement("div", null, description, control, detail),
  61. }));
  62. vi.mock("@/i18n/IntlProvider.js", () => ({
  63. useZCodeIntl: () => ({
  64. intl: {
  65. formatMessage: ({ id }: { id: string }) => {
  66. const messages: Record<string, string> = {
  67. "settings.themeMode": "主题模式",
  68. "settings.themeMode.system": "跟随系统",
  69. "settings.themeMode.dark": "深色",
  70. "settings.themeMode.light": "浅色",
  71. "settings.themeMode.zai-dark": "深色",
  72. "settings.themeMode.zai-light": "浅色",
  73. "settings.locale": "界面语言",
  74. "settings.locale.system": "系统默认",
  75. "settings.locale.zh-CN": "简体中文",
  76. "settings.locale.en-US": "English",
  77. "settings.terminalProfile": "继承系统终端 Profile",
  78. "settings.terminalProfileDescription": "desc",
  79. "settings.terminalFontFamily": "终端字体",
  80. "settings.terminalFontFamilyDescription": "desc",
  81. "settings.terminalFontFamilyPlaceholder": "留空自动继承",
  82. "settings.integratedTerminalShell": "集成终端Shell",
  83. "settings.integratedTerminalShellDescription": "desc",
  84. "settings.integratedTerminalShell.auto": "自动选择",
  85. "settings.nativeSearchEnhancements": "增强 Find 和 Grep",
  86. "settings.nativeSearchEnhancementsDescription": "desc",
  87. "settings.toolSearchExperimentalNotice": "desc",
  88. "settings.memory": "记忆",
  89. "settings.memory.workspaceMemory": "工作区记忆",
  90. "settings.memoryDescription": "desc",
  91. "settings.desktopChromiumHardwareAcceleration": "Chrome 硬件加速",
  92. "settings.desktopChromiumHardwareAccelerationDescription": "desc",
  93. "settings.notification": "任务通知",
  94. "settings.notificationDescription": "desc",
  95. "settings.notificationSound": "通知声音",
  96. "settings.notificationSoundDescription": "desc",
  97. "settings.messageStreamShowReasoning": "显示思考过程",
  98. "settings.messageStreamShowReasoningDescription": "desc",
  99. "settings.messageStreamShowTodos": "显示流式待办",
  100. "settings.messageStreamShowTodosDescription": "desc",
  101. "settings.toolGroupingExplore": "分组探索工具",
  102. "settings.toolGroupingExploreDescription": "desc",
  103. "settings.toolGroupingTerminal": "分组终端命令",
  104. "settings.toolGroupingTerminalDescription": "desc",
  105. "settings.toolGroupingChanges": "分组文件更改",
  106. "settings.toolGroupingChangesDescription": "desc",
  107. "settings.zcodeInteractionBehavior": "交互行为",
  108. "settings.zcodeInteractionBehaviorDescription": "desc",
  109. "settings.zcodeInteractionBehavior.option.queue": "队列",
  110. "settings.zcodeInteractionBehavior.option.guide": "引导",
  111. "settings.askUserQuestionAutoResolution": "提问自动继续",
  112. "settings.askUserQuestionAutoResolutionDescription": "desc",
  113. "settings.modelIoFullRetention": "完整保留模型 I/O",
  114. "settings.modelIoFullRetentionDescription": "desc",
  115. "settings.performanceMode": "性能模式",
  116. "settings.performanceModeDescription": "desc",
  117. "settings.taskAutoArchive": "自动归档旧任务",
  118. "settings.taskAutoArchiveDescription": "desc",
  119. "settings.taskAutoArchiveDays": "归档保留时长",
  120. "settings.taskAutoArchiveDaysDescription": "desc",
  121. "settings.taskAutoArchiveDays.option.3": "3 天后归档",
  122. "settings.taskAutoArchiveDays.option.7": "7 天后归档",
  123. "settings.taskAutoArchiveDays.option.14": "14 天后归档",
  124. "settings.taskAutoArchiveDays.option.30": "30 天后归档",
  125. "settings.dataBaseDir": "数据存储路径",
  126. "settings.dataBaseDirDescription": "desc",
  127. "settings.dataBaseDirPlaceholder": "默认:用户主目录",
  128. "settings.dataBaseDirBrowse": "选择文件夹",
  129. "settings.dataBaseDirSave": "保存",
  130. };
  131. return messages[id] ?? id;
  132. },
  133. },
  134. }),
  135. }));
  136. function findButton(label: string) {
  137. return capturedButtons.find((button) => button.children === label);
  138. }
  139. async function renderGeneralSection(
  140. overrides: Partial<
  141. Parameters<typeof import("../src/settingsPageHelpers.js").GeneralSectionContent>[0]
  142. > = {},
  143. ) {
  144. const { GeneralSectionContent } = await import("../src/settingsPageHelpers.js");
  145. return renderToStaticMarkup(
  146. createElement(GeneralSectionContent, {
  147. theme: "system",
  148. localePreference: "system",
  149. notificationEnabled: true,
  150. notificationSoundEnabled: true,
  151. closeToTrayOnWindows: true,
  152. keepAwakeWhileRunning: false,
  153. isDesktop: true,
  154. receivePreviewUpdates: false,
  155. autoDownloadAndInstallUpdates: false,
  156. desktopChromiumHardwareAccelerationEnabled: true,
  157. dataBaseDir: "/Users/test",
  158. terminalInheritSystemProfile: true,
  159. terminalFontFamily: "",
  160. nativeSearchEnhancementsEnabled: true,
  161. defaultHomeDir: "/Users/test",
  162. setTheme: vi.fn(),
  163. setLocalePreference: vi.fn(),
  164. setNotificationEnabled: vi.fn(),
  165. setNotificationSoundEnabled: vi.fn(),
  166. messageStreamShowReasoning: true,
  167. messageStreamShowTodos: false,
  168. toolGroupingExploreEnabled: true,
  169. toolGroupingTerminalEnabled: true,
  170. toolGroupingChangesEnabled: false,
  171. zcodeInteractionBehavior: "queue",
  172. askUserQuestionAutoResolutionEnabled: true,
  173. modelIoFullRetentionEnabled: false,
  174. onDataBaseDirChange: vi.fn(async () => {}),
  175. onSelectDataBaseDir: vi.fn(async () => "/Users/test/data"),
  176. onTerminalInheritSystemProfileChange: vi.fn(async () => {}),
  177. onTerminalFontFamilyChange: vi.fn(async () => {}),
  178. onNativeSearchEnhancementsEnabledChange: vi.fn(async () => {}),
  179. onToolSearchModeChange: vi.fn(async () => {}),
  180. onDesktopChromiumHardwareAccelerationChange: vi.fn(async () => {}),
  181. onCloseToTrayOnWindowsChange: vi.fn(async () => {}),
  182. onKeepAwakeWhileRunningChange: vi.fn(async () => {}),
  183. onReceivePreviewUpdatesChange: vi.fn(async () => {}),
  184. onAutoDownloadAndInstallUpdatesChange: vi.fn(async () => {}),
  185. onMessageStreamShowReasoningChange: vi.fn(async () => {}),
  186. onMessageStreamShowTodosChange: vi.fn(async () => {}),
  187. onToolGroupingExploreEnabledChange: vi.fn(async () => {}),
  188. onToolGroupingTerminalEnabledChange: vi.fn(async () => {}),
  189. onToolGroupingChangesEnabledChange: vi.fn(async () => {}),
  190. onZCodeInteractionBehaviorChange: vi.fn(async () => {}),
  191. onAskUserQuestionAutoResolutionEnabledChange: vi.fn(async () => {}),
  192. onModelIoFullRetentionEnabledChange: vi.fn(async () => {}),
  193. onOptimizeAgentExperienceEnabledChange: vi.fn(async () => {}),
  194. onOpenOnboardingDialog: vi.fn(),
  195. ...overrides,
  196. }),
  197. );
  198. }
  199. describe("GeneralSectionContent data directory control", () => {
  200. beforeEach(() => {
  201. capturedButtons.length = 0;
  202. capturedInputs.length = 0;
  203. capturedSwitches.length = 0;
  204. });
  205. afterEach(() => {
  206. vi.resetModules();
  207. });
  208. it("数据目录改为只读展示,避免继续手输路径", async () => {
  209. await renderGeneralSection();
  210. expect(capturedInputs.some((input) => input.readOnly === true)).toBe(true);
  211. });
  212. it("ModelIO 读取关闭值并通过标准设置开关提交", async () => {
  213. const onModelIoFullRetentionEnabledChange = vi.fn(async () => {});
  214. await renderGeneralSection({ onModelIoFullRetentionEnabledChange });
  215. const modelIoSwitch = capturedSwitches.find(
  216. (entry) => entry["aria-label"] === "完整保留模型 I/O",
  217. );
  218. expect(modelIoSwitch?.checked).toBe(false);
  219. expect(modelIoSwitch?.onCheckedChange).toBeTypeOf("function");
  220. (modelIoSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(true);
  221. expect(onModelIoFullRetentionEnabledChange).toHaveBeenCalledWith(true);
  222. });
  223. it("点击选择文件夹会调用系统目录选择回调", async () => {
  224. const onSelectDataBaseDir = vi.fn(async () => "/Users/test/data");
  225. await renderGeneralSection({ onSelectDataBaseDir });
  226. await (findButton("选择文件夹")?.onClick as (() => Promise<void>) | undefined)?.();
  227. expect(onSelectDataBaseDir).toHaveBeenCalledTimes(1);
  228. });
  229. it("Windows host 会展示集成终端 Shell 下拉选项", async () => {
  230. const markup = await renderGeneralSection({
  231. showIntegratedTerminalShell: true,
  232. integratedTerminalShellOptions: [
  233. {
  234. dialect: "git-bash",
  235. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  236. label: "Git Bash",
  237. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  238. source: "system",
  239. },
  240. ],
  241. });
  242. expect(markup).toContain("自动选择");
  243. expect(markup).toContain("Git Bash");
  244. });
  245. it("增强搜索读取持久值并写回用户切换", async () => {
  246. const onNativeSearchEnhancementsEnabledChange = vi.fn(async () => {});
  247. await renderGeneralSection({
  248. nativeSearchEnhancementsEnabled: false,
  249. onNativeSearchEnhancementsEnabledChange,
  250. });
  251. const nativeSearchSwitch = capturedSwitches.find(
  252. (item) => item["aria-label"] === "增强 Find 和 Grep",
  253. );
  254. expect(nativeSearchSwitch?.checked).toBe(false);
  255. (nativeSearchSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(true);
  256. expect(onNativeSearchEnhancementsEnabledChange).toHaveBeenCalledWith(true);
  257. });
  258. it("桌面端展示 Chrome 硬件加速开关,并把用户选择写回设置", async () => {
  259. const onDesktopChromiumHardwareAccelerationChange = vi.fn(async () => {});
  260. await renderGeneralSection({
  261. onDesktopChromiumHardwareAccelerationChange,
  262. });
  263. const hardwareAccelerationSwitch = capturedSwitches.find(
  264. (item) => item["aria-label"] === "Chrome 硬件加速",
  265. );
  266. expect(hardwareAccelerationSwitch?.checked).toBe(true);
  267. (hardwareAccelerationSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(
  268. false,
  269. );
  270. expect(onDesktopChromiumHardwareAccelerationChange).toHaveBeenCalledWith(false);
  271. });
  272. it("reasoning 与 Todo 读取持久值并写回用户选择", async () => {
  273. const onMessageStreamShowReasoningChange = vi.fn(async () => {});
  274. const onMessageStreamShowTodosChange = vi.fn(async () => {});
  275. await renderGeneralSection({
  276. onMessageStreamShowReasoningChange,
  277. onMessageStreamShowTodosChange,
  278. });
  279. const reasoningSwitch = capturedSwitches.find((item) => item["aria-label"] === "显示思考过程");
  280. const todosSwitch = capturedSwitches.find((item) => item["aria-label"] === "显示流式待办");
  281. expect(reasoningSwitch?.checked).toBe(true);
  282. expect(todosSwitch?.checked).toBe(false);
  283. (reasoningSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(false);
  284. (todosSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(true);
  285. expect(onMessageStreamShowReasoningChange).toHaveBeenCalledWith(false);
  286. expect(onMessageStreamShowTodosChange).toHaveBeenCalledWith(true);
  287. });
  288. it("工具分组开关读取持久值,并把用户操作写回设置回调", async () => {
  289. const onToolGroupingExploreEnabledChange = vi.fn(async () => {});
  290. const onToolGroupingTerminalEnabledChange = vi.fn(async () => {});
  291. const onToolGroupingChangesEnabledChange = vi.fn(async () => {});
  292. await renderGeneralSection({
  293. onToolGroupingExploreEnabledChange,
  294. onToolGroupingTerminalEnabledChange,
  295. onToolGroupingChangesEnabledChange,
  296. });
  297. const exploreSwitch = capturedSwitches.find((item) => item["aria-label"] === "分组探索工具");
  298. const terminalSwitch = capturedSwitches.find((item) => item["aria-label"] === "分组终端命令");
  299. const changesSwitch = capturedSwitches.find((item) => item["aria-label"] === "分组文件更改");
  300. expect(exploreSwitch?.checked).toBe(true);
  301. expect(terminalSwitch?.checked).toBe(true);
  302. expect(changesSwitch?.checked).toBe(false);
  303. (exploreSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(false);
  304. (terminalSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(false);
  305. (changesSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(true);
  306. expect(onToolGroupingExploreEnabledChange).toHaveBeenCalledWith(false);
  307. expect(onToolGroupingTerminalEnabledChange).toHaveBeenCalledWith(false);
  308. expect(onToolGroupingChangesEnabledChange).toHaveBeenCalledWith(true);
  309. });
  310. it("提问自动继续开关读取持久值并写回选择", async () => {
  311. const onAskUserQuestionAutoResolutionEnabledChange = vi.fn(async () => {});
  312. await renderGeneralSection({
  313. askUserQuestionAutoResolutionEnabled: false,
  314. onAskUserQuestionAutoResolutionEnabledChange,
  315. });
  316. const autoResolutionSwitch = capturedSwitches.find(
  317. (item) => item["aria-label"] === "提问自动继续",
  318. );
  319. expect(autoResolutionSwitch?.checked).toBe(false);
  320. (autoResolutionSwitch?.onCheckedChange as ((checked: boolean) => void) | undefined)?.(true);
  321. expect(onAskUserQuestionAutoResolutionEnabledChange).toHaveBeenCalledWith(true);
  322. });
  323. });