settingsPageIntegratedTerminalShell.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. const selectedSection = vi.hoisted(() => ({ value: "general" }));
  5. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  6. const stateUpdates = vi.hoisted(() => [] as unknown[]);
  7. const pendingEffects = vi.hoisted(() => [] as Array<() => void | (() => void)>);
  8. const generalSectionCapture = vi.hoisted(
  9. () => ({ props: undefined }) as { props: Record<string, unknown> | undefined },
  10. );
  11. const settingsPageConfigCalls = vi.hoisted(
  12. () =>
  13. [] as Array<{
  14. isDesktop?: boolean;
  15. isMacDesktop?: boolean;
  16. isWindowsDesktop?: boolean;
  17. }>,
  18. );
  19. const sharedSettingsState = vi.hoisted(
  20. () =>
  21. ({ value: undefined }) as {
  22. value: Record<string, unknown> | undefined;
  23. },
  24. );
  25. const sourceServerManagementState = vi.hoisted(
  26. () =>
  27. ({ value: undefined }) as {
  28. value: Record<string, unknown> | undefined;
  29. },
  30. );
  31. const remoteSystemInfo = vi.fn(async () => ({
  32. homedir: "C:\\Users\\remote",
  33. platform: "win32" as const,
  34. }));
  35. const remoteListIntegratedTerminalShells = vi.fn(async () => [
  36. {
  37. dialect: "git-bash" as const,
  38. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  39. label: "Git Bash",
  40. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  41. source: "system" as const,
  42. },
  43. ]);
  44. const localSystemInfo = vi.fn(async () => ({
  45. homedir: "/Users/local",
  46. platform: "darwin" as const,
  47. }));
  48. const localListIntegratedTerminalShells = vi.fn(async () => []);
  49. const remoteServices = {
  50. codingPlanSubscriptionService: {
  51. getEnterprisePricing: vi.fn(async () => ({ productList: [] })),
  52. },
  53. settingService: {
  54. get: vi.fn(async () => ({})),
  55. update: vi.fn(async () => {}),
  56. updateDataBaseDir: vi.fn(async () => {}),
  57. },
  58. systemService: {
  59. info: remoteSystemInfo,
  60. listIntegratedTerminalShells: remoteListIntegratedTerminalShells,
  61. },
  62. };
  63. const localServices = {
  64. // 回归:不能让本机与 Source 共用一个替身,否则写错机器也会通过。
  65. settingService: {
  66. get: vi.fn(async () => ({})),
  67. update: vi.fn(async () => {}),
  68. updateDataBaseDir: vi.fn(async () => {}),
  69. },
  70. systemService: {
  71. info: localSystemInfo,
  72. listIntegratedTerminalShells: localListIntegratedTerminalShells,
  73. },
  74. };
  75. vi.mock("react", async () => {
  76. const actual = await vi.importActual<typeof import("react")>("react");
  77. return {
  78. ...actual,
  79. useState: (initial: unknown) => {
  80. const [value, setValue] = actual.useState(initial);
  81. return [
  82. value,
  83. (next: unknown) => {
  84. stateUpdates.push(next);
  85. setValue(next);
  86. },
  87. ];
  88. },
  89. useEffect: (effect: () => void | (() => void)) => {
  90. pendingEffects.push(effect);
  91. },
  92. };
  93. });
  94. vi.mock("@/components/ui/button.js", () => ({
  95. Button: ({ children, ...props }: { children: unknown; [key: string]: unknown }) =>
  96. createElement("button", props, children),
  97. }));
  98. vi.mock("@/components/ui/tabs.js", () => ({
  99. Tabs: ({ children }: { children: unknown }) => createElement("div", null, children),
  100. TabsList: ({ children }: { children: unknown }) => createElement("div", null, children),
  101. TabsTrigger: ({ children }: { children: unknown }) => createElement("button", null, children),
  102. }));
  103. vi.mock("@/DesktopWindowFrame.js", () => ({
  104. DesktopWindowFrame: ({ children }: { children: unknown }) => createElement("div", null, children),
  105. }));
  106. vi.mock("@/WorkspaceSidebarFooter.js", () => ({
  107. WorkspaceSidebarFooter: () => null,
  108. }));
  109. vi.mock("@/ControlHintTooltip.js", () => ({
  110. ControlHintTooltip: ({ children }: { children: unknown }) => children,
  111. }));
  112. vi.mock("@/components/lib/utils.js", () => ({
  113. cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(" "),
  114. }));
  115. vi.mock("@/i18n/IntlProvider.js", () => ({
  116. useZCodeIntl: () => ({
  117. intl: {
  118. formatMessage: ({ id }: { id: string }) => id,
  119. },
  120. localePreference: "system",
  121. setLocalePreference: vi.fn(),
  122. }),
  123. }));
  124. const mockPlatform = {
  125. syncAppSettings: vi.fn(),
  126. canOpenCommunity: vi.fn(async () => false),
  127. getZCodeStdioTapDevState: vi.fn(async () => ({
  128. enabled: false,
  129. visible: false,
  130. })),
  131. };
  132. vi.mock("@/hooks/usePlatform.js", () => ({
  133. usePlatform: () => mockPlatform,
  134. useSelectDirectory: () => vi.fn(async () => null),
  135. }));
  136. vi.mock("@/hooks/useServices.js", () => ({
  137. ServiceProvider: ({ children }: { children: unknown }) => createElement("div", null, children),
  138. useOptionalServices: () => remoteServices,
  139. useServices: () => remoteServices,
  140. }));
  141. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  142. useBaseWorkspaceServices: () =>
  143. sourceServerManagementState.value
  144. ? {
  145. ...localServices,
  146. sourceServerManagementService: sourceServerManagementState.value,
  147. }
  148. : localServices,
  149. useWorkspaceServicesResolution: () => ({
  150. services: remoteServices,
  151. remoteSessionId: null,
  152. isRemoteTarget: false,
  153. connectionKind: "local-ready",
  154. rpcReady: true,
  155. }),
  156. }));
  157. const updateSharedSettings = vi.hoisted(() => vi.fn(async () => {}));
  158. vi.mock("@/hooks/useSettingService.js", () => ({
  159. useSettings: () => ({ settings: sharedSettingsState.value, update: updateSharedSettings }),
  160. }));
  161. vi.mock("@/hooks/useModelProviders.js", () => ({
  162. useModelProviders: () => ({ loading: false, modelProviders: [] }),
  163. }));
  164. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  165. useUsageEntitlement: () => ({
  166. loading: false,
  167. snapshot: undefined,
  168. error: undefined,
  169. }),
  170. }));
  171. vi.mock("@/store/StoreProvider.js", () => ({
  172. useZCodeStoreWithDefault: (_selector: unknown, fallback: unknown) => fallback,
  173. useZCodeStore: (selector: (state: Record<string, unknown>) => unknown) =>
  174. selector({
  175. codePreviewSettings: {},
  176. notificationEnabled: true,
  177. notificationSoundEnabled: true,
  178. requestOnboardingDialog: vi.fn(),
  179. setCodePreviewSettings: vi.fn(),
  180. setNotificationEnabled: vi.fn(),
  181. setNotificationSoundEnabled: vi.fn(),
  182. setTheme: vi.fn(),
  183. theme: "system",
  184. }),
  185. }));
  186. vi.mock("@/store/TabStoreProvider.js", () => ({
  187. useOptionalTabStore: () => undefined,
  188. useTabStore: (selector: (state: Record<string, unknown>) => unknown) =>
  189. selector({
  190. activeWorkspaceIdentity: undefined,
  191. activeWorkspacePath: undefined,
  192. tabs: [],
  193. }),
  194. }));
  195. vi.mock("@/lib/settingsNavigation.js", () => ({
  196. addPendingSettingsSectionListener: () => () => {},
  197. consumeInitialSettingsSection: () => selectedSection.value,
  198. consumePendingSettingsPluginTab: () => undefined,
  199. consumePendingSettingsPluginOrigin: () => undefined,
  200. consumePendingSettingsPluginScopeKey: () => undefined,
  201. clearPendingSettingsPluginOrigin: () => undefined,
  202. clearPendingSettingsPluginScopeKey: () => undefined,
  203. consumePendingSettingsModelProviderTarget: () => undefined,
  204. consumePendingSettingsUsageTab: () => undefined,
  205. consumePendingSettingsSourceServerId: () => undefined,
  206. consumePendingSettingsSourceServerDetails: () => false,
  207. resolveSettingsSection: (section: string) => section,
  208. shouldFallbackSettingsUsageTabToApp: () => false,
  209. writeLastSettingsSectionPreference: vi.fn(),
  210. }));
  211. vi.mock("@/settings/CodingPlanUpgradeDialogProvider.js", () => ({
  212. useCodingPlanUpgradeDialog: () => ({
  213. openCodingPlanUpgrade: vi.fn(),
  214. }),
  215. }));
  216. vi.mock("@/lib/sidebarUsageCodingPlanProviderPreference.js", () => ({
  217. readSidebarUsageCodingPlanProviderPreference: () => undefined,
  218. readSidebarUsageCodingPlanSourcePreference: () => undefined,
  219. writeSidebarUsageCodingPlanProviderPreference: vi.fn(),
  220. }));
  221. vi.mock("@/lib/codingPlanProvider.js", () => ({}));
  222. vi.mock("@/settings/ModelProviderSection.js", () => ({
  223. ModelProviderSection: () => null,
  224. }));
  225. vi.mock("@/settings/McpSettingsSection.js", () => ({
  226. McpSettingsSection: () => null,
  227. }));
  228. vi.mock("@/settings/UsageStatsSection.js", () => ({
  229. UsageStatsSection: () => null,
  230. }));
  231. vi.mock("@/settings/SkillsSection.js", () => ({
  232. SkillsSection: () => null,
  233. }));
  234. vi.mock("@/project-sidebar/ArchiveManagement.js", () => ({ ArchiveManagement: () => null }));
  235. vi.mock("@/settings/PluginsSection.js", () => ({
  236. PluginsSection: () => null,
  237. }));
  238. vi.mock("@/settings/CommandsSection.js", () => ({
  239. CommandsSection: () => null,
  240. }));
  241. vi.mock("@/settings/HooksSection.js", () => ({
  242. HooksSection: () => null,
  243. }));
  244. vi.mock("@/settings/BrowserSettingsSection.js", () => ({
  245. BrowserSettingsSection: (props: Record<string, unknown>) => {
  246. generalSectionCapture.props = props;
  247. return createElement("div");
  248. },
  249. }));
  250. vi.mock("@/settings/MigrationSection.js", () => ({
  251. MigrationSection: () => null,
  252. }));
  253. vi.mock("../src/settingsCodePreview.js", () => ({
  254. AppearanceSectionContent: () => null,
  255. }));
  256. vi.mock("../src/settingsPageHelpers.js", () => ({
  257. createSettingsPageConfig: (options: {
  258. isDesktop?: boolean;
  259. isMacDesktop?: boolean;
  260. isWindowsDesktop?: boolean;
  261. }) => {
  262. settingsPageConfigCalls.push(options);
  263. const settingsSections = [
  264. {
  265. id: selectedSection.value,
  266. titleId: "settings.general",
  267. navLabelId: "settings.general",
  268. icon: () => null,
  269. },
  270. ];
  271. return {
  272. settingsSectionGroups: [
  273. {
  274. id: "basics",
  275. titleId: "settings.general",
  276. sections: settingsSections,
  277. },
  278. ],
  279. settingsSections,
  280. };
  281. },
  282. resolveSettingsSectionForPlatform: (section: string) => section,
  283. isSettingsSectionAvailable: () => true,
  284. GeneralSectionContent: (props: Record<string, unknown>) => {
  285. generalSectionCapture.props = props;
  286. return createElement("div", null, "general");
  287. },
  288. GeneralSectionHeader: () => null,
  289. TaskAutoArchiveSettings: (props: Record<string, unknown>) => {
  290. generalSectionCapture.props = props;
  291. return null;
  292. },
  293. }));
  294. async function flushPromises() {
  295. await Promise.resolve();
  296. await Promise.resolve();
  297. }
  298. function flushPendingEffects() {
  299. const effects = pendingEffects.splice(0);
  300. for (const effect of effects) {
  301. effect();
  302. }
  303. }
  304. describe("SettingsPage integrated terminal shell", () => {
  305. beforeEach(() => {
  306. pendingEffects.length = 0;
  307. selectedSection.value = "general";
  308. stateUpdates.length = 0;
  309. localServices.settingService.get.mockReset().mockResolvedValue({});
  310. remoteServices.settingService.get.mockReset().mockResolvedValue({});
  311. generalSectionCapture.props = undefined;
  312. settingsPageConfigCalls.length = 0;
  313. sharedSettingsState.value = undefined;
  314. sourceServerManagementState.value = undefined;
  315. vi.clearAllMocks();
  316. localServices.settingService.update.mockReset().mockResolvedValue(undefined);
  317. remoteSystemInfo.mockResolvedValue({
  318. homedir: "C:\\Users\\remote",
  319. platform: "win32",
  320. });
  321. remoteListIntegratedTerminalShells.mockResolvedValue([
  322. {
  323. dialect: "git-bash",
  324. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  325. label: "Git Bash",
  326. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  327. source: "system",
  328. },
  329. ]);
  330. localSystemInfo.mockResolvedValue({
  331. homedir: "/Users/local",
  332. platform: "darwin",
  333. });
  334. localListIntegratedTerminalShells.mockResolvedValue([]);
  335. });
  336. // Bugfix: 本用例是该文件第一个 import SettingsPage 的测试,独吞整棵设置页模块图的
  337. // 冷启动(vitest 按文件隔离 worker,模块缓存不跨文件共享);全量高负载下冷导入会
  338. // 顶穿全局 testTimeout 30s。只放宽这一条冷启动用例,后续用例命中缓存不受影响。
  339. it("renders the settings navigation as labelled groups", async () => {
  340. const { SettingsPage } = await import("../src/SettingsPage.js");
  341. const html = renderToStaticMarkup(createElement(SettingsPage, { isWindowsDesktop: true }));
  342. expect(settingsPageConfigCalls).toEqual([
  343. {
  344. isDesktop: false,
  345. isMacDesktop: false,
  346. isWindowsDesktop: true,
  347. },
  348. ]);
  349. expect(html).toContain('aria-label="settings.navLabel"');
  350. expect(html).toContain('role="group"');
  351. expect(html).toContain('aria-labelledby="settings-sidebar-group-basics"');
  352. expect(html).toContain('id="settings-sidebar-group-basics"');
  353. }, 90_000);
  354. it("defaults ToolSearch off and saves the selected mode through shared settings", async () => {
  355. const { SettingsPage } = await import("../src/SettingsPage.js");
  356. renderToStaticMarkup(createElement(SettingsPage, {}));
  357. expect(generalSectionCapture.props?.toolSearchMode).toBe("off");
  358. const change = generalSectionCapture.props?.onToolSearchModeChange as (
  359. mode: "auto" | "local" | "off",
  360. ) => Promise<void>;
  361. for (const mode of ["auto", "local", "off"] as const) {
  362. await change(mode);
  363. expect(updateSharedSettings).toHaveBeenLastCalledWith({ toolSearchMode: mode });
  364. }
  365. });
  366. it("defaults message stream reasoning to visible before settings hydrate", async () => {
  367. const { SettingsPage } = await import("../src/SettingsPage.js");
  368. renderToStaticMarkup(createElement(SettingsPage, {}));
  369. expect(generalSectionCapture.props?.messageStreamShowReasoning).toBe(true);
  370. });
  371. it("uses the local host system service for the global shell setting", async () => {
  372. const { SettingsPage } = await import("../src/SettingsPage.js");
  373. renderToStaticMarkup(createElement(SettingsPage, {}));
  374. flushPendingEffects();
  375. await flushPromises();
  376. expect(localSystemInfo).toHaveBeenCalled();
  377. expect(remoteSystemInfo).not.toHaveBeenCalled();
  378. expect(localListIntegratedTerminalShells).not.toHaveBeenCalled();
  379. expect(remoteListIntegratedTerminalShells).not.toHaveBeenCalled();
  380. });
  381. it("shows inline window controls on Linux desktop", async () => {
  382. const { SettingsPage } = await import("../src/SettingsPage.js");
  383. const html = renderToStaticMarkup(
  384. createElement(SettingsPage, {
  385. isDesktop: true,
  386. isMacDesktop: false,
  387. isWindowsDesktop: false,
  388. }),
  389. );
  390. expect(html).toContain('data-testid="desktop-window-controls"');
  391. });
  392. it("lists shell options from the local host even when the remote workspace is non-Windows", async () => {
  393. remoteSystemInfo.mockResolvedValue({
  394. homedir: "/home/remote",
  395. platform: "linux",
  396. });
  397. localSystemInfo.mockResolvedValue({
  398. homedir: "C:\\Users\\local",
  399. platform: "win32",
  400. });
  401. const { SettingsPage } = await import("../src/SettingsPage.js");
  402. renderToStaticMarkup(createElement(SettingsPage, {}));
  403. flushPendingEffects();
  404. await flushPromises();
  405. expect(localSystemInfo).toHaveBeenCalled();
  406. expect(localListIntegratedTerminalShells).toHaveBeenCalled();
  407. expect(remoteSystemInfo).not.toHaveBeenCalled();
  408. expect(remoteListIntegratedTerminalShells).not.toHaveBeenCalled();
  409. });
  410. it.each([
  411. [
  412. "onDesktopChromiumHardwareAccelerationChange",
  413. false,
  414. { desktopChromiumHardwareAccelerationEnabled: false },
  415. ],
  416. ["onCloseToTrayOnWindowsChange", false, { closeToTrayOnWindows: false }],
  417. [
  418. "onIntegratedTerminalShellChange",
  419. {
  420. mode: "shell",
  421. id: "powershell",
  422. dialect: "powershell",
  423. label: "PowerShell",
  424. path: "C:\\PowerShell\\pwsh.exe",
  425. },
  426. {
  427. integratedTerminalShell: {
  428. mode: "shell",
  429. id: "powershell",
  430. dialect: "powershell",
  431. label: "PowerShell",
  432. path: "C:\\PowerShell\\pwsh.exe",
  433. },
  434. },
  435. ],
  436. ["onHttpProxyChange", " http://127.0.0.1:7890 ", { httpProxy: "http://127.0.0.1:7890" }],
  437. [
  438. "onHttpProxyNoProxyChange",
  439. " localhost, , .example.test ",
  440. { httpProxyNoProxy: "localhost,.example.test" },
  441. ],
  442. [
  443. "onHttpProxyCaCertPathChange",
  444. " /local/root-ca.pem ",
  445. { httpProxyCaCertPath: "/local/root-ca.pem" },
  446. ],
  447. ])(
  448. "persists Desktop %s on the base Host while Source is active",
  449. async (callback, value, patch) => {
  450. const { SettingsPage } = await import("../src/SettingsPage.js");
  451. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  452. await (generalSectionCapture.props![callback] as (value: unknown) => Promise<void>)(value);
  453. expect(localServices.settingService.update).toHaveBeenCalledWith(patch);
  454. expect(remoteServices.settingService.update).not.toHaveBeenCalled();
  455. },
  456. );
  457. it("keeps native directory migration on the Desktop", async () => {
  458. const { SettingsPage } = await import("../src/SettingsPage.js");
  459. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  460. await (generalSectionCapture.props!.onDataBaseDirChange as (dir: string) => Promise<void>)(
  461. "/local/selected-folder",
  462. );
  463. expect(localServices.settingService.updateDataBaseDir).toHaveBeenCalledWith(
  464. "/local/selected-folder",
  465. );
  466. expect(remoteServices.settingService.updateDataBaseDir).not.toHaveBeenCalled();
  467. });
  468. it("propagates local save failures without updating the tray or falling back to Source", async () => {
  469. localServices.settingService.update.mockRejectedValueOnce(new Error("local disk failure"));
  470. const { SettingsPage } = await import("../src/SettingsPage.js");
  471. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  472. await expect(
  473. (
  474. generalSectionCapture.props!.onCloseToTrayOnWindowsChange as (
  475. enabled: boolean,
  476. ) => Promise<void>
  477. )(false),
  478. ).rejects.toThrow("local disk failure");
  479. expect(mockPlatform.syncAppSettings).not.toHaveBeenCalled();
  480. expect(remoteServices.settingService.update).not.toHaveBeenCalled();
  481. });
  482. it.each([
  483. ["onTerminalInheritSystemProfileChange", false, { terminalInheritSystemProfile: false }],
  484. ["onTerminalFontFamilyChange", " Source Mono ", { terminalFontFamily: "Source Mono" }],
  485. ["onTaskAutoArchiveEnabledChange", true, { taskAutoArchiveEnabled: true }],
  486. ["onTaskAutoArchiveOlderThanDaysChange", 30, { taskAutoArchiveOlderThanDays: 30 }],
  487. ])("preserves target Environment ownership for %s", async (callback, value, patch) => {
  488. const { SettingsPage } = await import("../src/SettingsPage.js");
  489. if (String(callback).startsWith("onTaskAutoArchive")) selectedSection.value = "archives";
  490. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  491. await (generalSectionCapture.props![callback] as (value: unknown) => Promise<void>)(value);
  492. expect(remoteServices.settingService.update).toHaveBeenCalledWith(patch);
  493. expect(localServices.settingService.update).not.toHaveBeenCalled();
  494. });
  495. it("persists embedded browser certificate policy on Desktop", async () => {
  496. selectedSection.value = "browser";
  497. const { SettingsPage } = await import("../src/SettingsPage.js");
  498. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  499. await (
  500. generalSectionCapture.props!.onEmbeddedBrowserAllowInsecureCertificatesChange as (
  501. enabled: boolean,
  502. ) => Promise<void>
  503. )(true);
  504. expect(localServices.settingService.update).toHaveBeenCalledWith({
  505. embeddedBrowserAllowInsecureCertificates: true,
  506. });
  507. expect(remoteServices.settingService.update).not.toHaveBeenCalled();
  508. });
  509. it("propagates local migration failure without invoking Source migration", async () => {
  510. localServices.settingService.updateDataBaseDir.mockRejectedValueOnce(new Error("copy denied"));
  511. const { SettingsPage } = await import("../src/SettingsPage.js");
  512. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  513. await expect(
  514. (generalSectionCapture.props!.onDataBaseDirChange as (dir: string) => Promise<void>)(
  515. "/local/denied",
  516. ),
  517. ).rejects.toThrow("copy denied");
  518. expect(remoteServices.settingService.updateDataBaseDir).not.toHaveBeenCalled();
  519. });
  520. it("hydrates each field from its owning Environment despite conflicting snapshots", async () => {
  521. localServices.settingService.get.mockResolvedValueOnce({
  522. dataBaseDir: "/desktop/data",
  523. httpProxy: "http://desktop-proxy:7890",
  524. terminalFontFamily: "wrong local font",
  525. taskAutoArchiveOlderThanDays: 91,
  526. });
  527. remoteServices.settingService.get.mockResolvedValueOnce({
  528. dataBaseDir: "/source/data",
  529. httpProxy: "http://source-proxy:7890",
  530. terminalFontFamily: "Source Mono",
  531. taskAutoArchiveOlderThanDays: 30,
  532. });
  533. const { SettingsPage } = await import("../src/SettingsPage.js");
  534. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  535. flushPendingEffects();
  536. await flushPromises();
  537. expect(stateUpdates).toEqual(
  538. expect.arrayContaining(["/desktop/data", "http://desktop-proxy:7890", "Source Mono", 30]),
  539. );
  540. for (const wrong of ["/source/data", "http://source-proxy:7890", "wrong local font", 91])
  541. expect(stateUpdates).not.toContain(wrong);
  542. });
  543. it("ignores pending settings reads after the owning page is detached", async () => {
  544. let completeLocal!: (value: Record<string, unknown>) => void;
  545. let completeSource!: (value: Record<string, unknown>) => void;
  546. localServices.settingService.get.mockReturnValueOnce(
  547. new Promise((resolve) => {
  548. completeLocal = resolve;
  549. }),
  550. );
  551. remoteServices.settingService.get.mockReturnValueOnce(
  552. new Promise((resolve) => {
  553. completeSource = resolve;
  554. }),
  555. );
  556. const { SettingsPage } = await import("../src/SettingsPage.js");
  557. renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  558. const cleanups = pendingEffects.splice(0).map((effect) => effect());
  559. for (const cleanup of cleanups) cleanup?.();
  560. completeLocal({ dataBaseDir: "/stale/desktop" });
  561. completeSource({ terminalFontFamily: "stale Source font" });
  562. await flushPromises();
  563. expect(stateUpdates).not.toContain("/stale/desktop");
  564. expect(stateUpdates).not.toContain("stale Source font");
  565. });
  566. });