settingsPageIntegratedTerminalShell.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. const pendingEffects = vi.hoisted(() => [] as Array<() => void | (() => void)>);
  5. const generalSectionCapture = vi.hoisted(
  6. () => ({ props: undefined }) as { props: Record<string, unknown> | undefined },
  7. );
  8. const settingsPageConfigCalls = vi.hoisted(
  9. () =>
  10. [] as Array<{
  11. isDesktop?: boolean;
  12. isMacDesktop?: boolean;
  13. isWindowsDesktop?: boolean;
  14. }>,
  15. );
  16. const remoteSystemInfo = vi.fn(async () => ({
  17. homedir: "C:\\Users\\remote",
  18. platform: "win32" as const,
  19. }));
  20. const remoteListIntegratedTerminalShells = vi.fn(async () => [
  21. {
  22. dialect: "git-bash" as const,
  23. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  24. label: "Git Bash",
  25. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  26. source: "system" as const,
  27. },
  28. ]);
  29. const localSystemInfo = vi.fn(async () => ({
  30. homedir: "/Users/local",
  31. platform: "darwin" as const,
  32. }));
  33. const localListIntegratedTerminalShells = vi.fn(async () => []);
  34. const remoteServices = {
  35. codingPlanSubscriptionService: {
  36. getEnterprisePricing: vi.fn(async () => ({ productList: [] })),
  37. },
  38. settingService: {
  39. get: vi.fn(async () => ({})),
  40. update: vi.fn(async () => {}),
  41. },
  42. systemService: {
  43. info: remoteSystemInfo,
  44. listIntegratedTerminalShells: remoteListIntegratedTerminalShells,
  45. },
  46. };
  47. const localServices = {
  48. settingService: remoteServices.settingService,
  49. systemService: {
  50. info: localSystemInfo,
  51. listIntegratedTerminalShells: localListIntegratedTerminalShells,
  52. },
  53. };
  54. vi.mock("react", async () => {
  55. const actual = await vi.importActual<typeof import("react")>("react");
  56. return {
  57. ...actual,
  58. useEffect: (effect: () => void | (() => void)) => {
  59. pendingEffects.push(effect);
  60. },
  61. };
  62. });
  63. vi.mock("@/components/ui/button.js", () => ({
  64. Button: ({ children, ...props }: { children: unknown; [key: string]: unknown }) =>
  65. createElement("button", props, children),
  66. }));
  67. vi.mock("@/components/ui/tabs.js", () => ({
  68. Tabs: ({ children }: { children: unknown }) => createElement("div", null, children),
  69. TabsList: ({ children }: { children: unknown }) => createElement("div", null, children),
  70. TabsTrigger: ({ children }: { children: unknown }) => createElement("button", null, children),
  71. }));
  72. vi.mock("@/DesktopWindowFrame.js", () => ({
  73. DesktopWindowFrame: ({ children }: { children: unknown }) => createElement("div", null, children),
  74. }));
  75. vi.mock("@/WorkspaceSidebarFooter.js", () => ({
  76. WorkspaceSidebarFooter: () => null,
  77. }));
  78. vi.mock("@/ControlHintTooltip.js", () => ({
  79. ControlHintTooltip: ({ children }: { children: unknown }) => children,
  80. }));
  81. vi.mock("@/components/lib/utils.js", () => ({
  82. cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(" "),
  83. }));
  84. vi.mock("@/i18n/IntlProvider.js", () => ({
  85. useZCodeIntl: () => ({
  86. intl: {
  87. formatMessage: ({ id }: { id: string }) => id,
  88. },
  89. localePreference: "system",
  90. setLocalePreference: vi.fn(),
  91. }),
  92. }));
  93. const mockPlatform = {
  94. canOpenCommunity: vi.fn(async () => false),
  95. getZCodeStdioTapDevState: vi.fn(async () => ({
  96. enabled: false,
  97. visible: false,
  98. })),
  99. getWindowControlsOverlayMetrics: vi.fn(() => ({ rightPaddingPx: 188 })),
  100. onWindowControlsOverlayChanged: vi.fn(
  101. (handler: (metrics: { rightPaddingPx?: number }) => void) => {
  102. handler({ rightPaddingPx: 188 });
  103. return () => {};
  104. },
  105. ),
  106. };
  107. vi.mock("@/hooks/usePlatform.js", () => ({
  108. usePlatform: () => mockPlatform,
  109. useSelectDirectory: () => vi.fn(async () => null),
  110. }));
  111. vi.mock("@/hooks/useServices.js", () => ({
  112. useOptionalServices: () => remoteServices,
  113. useServices: () => remoteServices,
  114. }));
  115. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  116. useBaseWorkspaceServices: () => localServices,
  117. useWorkspaceServicesResolution: () => ({
  118. services: remoteServices,
  119. remoteSessionId: null,
  120. isRemoteTarget: false,
  121. connectionKind: "local-ready",
  122. rpcReady: true,
  123. }),
  124. }));
  125. vi.mock("@/hooks/useSettingService.js", () => ({
  126. useSettings: () => ({ settings: undefined, update: vi.fn() }),
  127. }));
  128. vi.mock("@/hooks/useModelProviders.js", () => ({
  129. useModelProviders: () => ({ loading: false, modelProviders: [] }),
  130. }));
  131. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  132. useUsageEntitlement: () => ({
  133. loading: false,
  134. snapshot: undefined,
  135. error: undefined,
  136. }),
  137. }));
  138. vi.mock("@/store/StoreProvider.js", () => ({
  139. useZCodeStore: (selector: (state: Record<string, unknown>) => unknown) =>
  140. selector({
  141. codePreviewSettings: {},
  142. notificationEnabled: true,
  143. notificationSoundEnabled: true,
  144. requestOnboardingDialog: vi.fn(),
  145. setCodePreviewSettings: vi.fn(),
  146. setNotificationEnabled: vi.fn(),
  147. setNotificationSoundEnabled: vi.fn(),
  148. setTheme: vi.fn(),
  149. theme: "system",
  150. }),
  151. }));
  152. vi.mock("@/store/TabStoreProvider.js", () => ({
  153. useTabStore: (selector: (state: Record<string, unknown>) => unknown) =>
  154. selector({
  155. activeWorkspaceIdentity: undefined,
  156. activeWorkspacePath: undefined,
  157. tabs: [],
  158. }),
  159. }));
  160. vi.mock("@/lib/settingsNavigation.js", () => ({
  161. addPendingSettingsSectionListener: () => () => {},
  162. consumeInitialSettingsSection: () => "general",
  163. consumePendingSettingsPluginTab: () => undefined,
  164. consumePendingSettingsPluginOrigin: () => undefined,
  165. consumePendingSettingsPluginScopeKey: () => undefined,
  166. clearPendingSettingsPluginOrigin: () => undefined,
  167. clearPendingSettingsPluginScopeKey: () => undefined,
  168. consumePendingSettingsModelProviderTarget: () => undefined,
  169. consumePendingSettingsUsageTab: () => undefined,
  170. resolveSettingsSection: (section: string) => section,
  171. shouldFallbackSettingsUsageTabToApp: () => false,
  172. writeLastSettingsSectionPreference: vi.fn(),
  173. }));
  174. vi.mock("@/settings/CodingPlanUpgradeDialogProvider.js", () => ({
  175. useCodingPlanUpgradeDialog: () => ({
  176. openCodingPlanUpgrade: vi.fn(),
  177. }),
  178. }));
  179. vi.mock("@/lib/sidebarUsageCodingPlanProviderPreference.js", () => ({
  180. readSidebarUsageCodingPlanProviderPreference: () => undefined,
  181. readSidebarUsageCodingPlanSourcePreference: () => undefined,
  182. writeSidebarUsageCodingPlanProviderPreference: vi.fn(),
  183. }));
  184. vi.mock("@/lib/codingPlanProvider.js", () => ({}));
  185. vi.mock("@/settings/ModelProviderSection.js", () => ({
  186. ModelProviderSection: () => null,
  187. }));
  188. vi.mock("@/settings/McpSettingsSection.js", () => ({
  189. McpSettingsSection: () => null,
  190. }));
  191. vi.mock("@/settings/UsageStatsSection.js", () => ({
  192. UsageStatsSection: () => null,
  193. }));
  194. vi.mock("@/settings/SkillsSection.js", () => ({
  195. SkillsSection: () => null,
  196. }));
  197. vi.mock("@/settings/PluginsSection.js", () => ({
  198. PluginsSection: () => null,
  199. }));
  200. vi.mock("@/settings/CommandsSection.js", () => ({
  201. CommandsSection: () => null,
  202. }));
  203. vi.mock("@/settings/HooksSection.js", () => ({
  204. HooksSection: () => null,
  205. }));
  206. vi.mock("@/settings/BrowserSettingsSection.js", () => ({
  207. BrowserSettingsSection: () => null,
  208. }));
  209. vi.mock("@/settings/MigrationSection.js", () => ({
  210. MigrationSection: () => null,
  211. }));
  212. vi.mock("../src/settingsCodePreview.js", () => ({
  213. AppearanceSectionContent: () => null,
  214. }));
  215. vi.mock("../src/settingsPageHelpers.js", () => ({
  216. createSettingsPageConfig: (options: {
  217. isDesktop?: boolean;
  218. isMacDesktop?: boolean;
  219. isWindowsDesktop?: boolean;
  220. }) => {
  221. settingsPageConfigCalls.push(options);
  222. const settingsSections = [
  223. {
  224. id: "general",
  225. titleId: "settings.general",
  226. navLabelId: "settings.general",
  227. icon: () => null,
  228. },
  229. ];
  230. return {
  231. settingsSectionGroups: [
  232. {
  233. id: "basics",
  234. titleId: "settings.general",
  235. sections: settingsSections,
  236. },
  237. ],
  238. settingsSections,
  239. };
  240. },
  241. resolveSettingsSectionForPlatform: (section: string) => section,
  242. GeneralSectionContent: (props: Record<string, unknown>) => {
  243. generalSectionCapture.props = props;
  244. return createElement("div", null, "general");
  245. },
  246. GeneralSectionHeader: () => null,
  247. }));
  248. async function flushPromises() {
  249. await Promise.resolve();
  250. await Promise.resolve();
  251. }
  252. function flushPendingEffects() {
  253. const effects = pendingEffects.splice(0);
  254. for (const effect of effects) {
  255. effect();
  256. }
  257. }
  258. describe("SettingsPage integrated terminal shell", () => {
  259. beforeEach(() => {
  260. pendingEffects.length = 0;
  261. generalSectionCapture.props = undefined;
  262. settingsPageConfigCalls.length = 0;
  263. vi.clearAllMocks();
  264. remoteSystemInfo.mockResolvedValue({
  265. homedir: "C:\\Users\\remote",
  266. platform: "win32",
  267. });
  268. remoteListIntegratedTerminalShells.mockResolvedValue([
  269. {
  270. dialect: "git-bash",
  271. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  272. label: "Git Bash",
  273. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  274. source: "system",
  275. },
  276. ]);
  277. localSystemInfo.mockResolvedValue({
  278. homedir: "/Users/local",
  279. platform: "darwin",
  280. });
  281. localListIntegratedTerminalShells.mockResolvedValue([]);
  282. });
  283. // Bugfix: 本用例是该文件第一个 import SettingsPage 的测试,独吞整棵设置页模块图的
  284. // 冷启动(vitest 按文件隔离 worker,模块缓存不跨文件共享);全量高负载下冷导入会
  285. // 顶穿全局 testTimeout 30s。只放宽这一条冷启动用例,后续用例命中缓存不受影响。
  286. it("renders the settings navigation as labelled groups", async () => {
  287. const { SettingsPage } = await import("../src/SettingsPage.js");
  288. const html = renderToStaticMarkup(createElement(SettingsPage, { isWindowsDesktop: true }));
  289. expect(settingsPageConfigCalls).toEqual([
  290. {
  291. isDesktop: false,
  292. isMacDesktop: false,
  293. isWindowsDesktop: true,
  294. },
  295. ]);
  296. expect(html).toContain('aria-label="settings.navLabel"');
  297. expect(html).toContain('role="group"');
  298. expect(html).toContain('aria-labelledby="settings-sidebar-group-basics"');
  299. expect(html).toContain('id="settings-sidebar-group-basics"');
  300. }, 90_000);
  301. it("defaults message stream reasoning to visible before settings hydrate", async () => {
  302. const { SettingsPage } = await import("../src/SettingsPage.js");
  303. renderToStaticMarkup(createElement(SettingsPage, {}));
  304. expect(generalSectionCapture.props?.messageStreamShowReasoning).toBe(true);
  305. });
  306. it("uses the local host system service for the global shell setting", async () => {
  307. const { SettingsPage } = await import("../src/SettingsPage.js");
  308. renderToStaticMarkup(createElement(SettingsPage, {}));
  309. flushPendingEffects();
  310. await flushPromises();
  311. expect(localSystemInfo).toHaveBeenCalled();
  312. expect(remoteSystemInfo).not.toHaveBeenCalled();
  313. expect(localListIntegratedTerminalShells).not.toHaveBeenCalled();
  314. expect(remoteListIntegratedTerminalShells).not.toHaveBeenCalled();
  315. });
  316. it("uses a fixed inline action-group inset instead of native caption metrics", async () => {
  317. const { SettingsPage } = await import("../src/SettingsPage.js");
  318. renderToStaticMarkup(
  319. createElement(SettingsPage, {
  320. isWindowsDesktop: true,
  321. }),
  322. );
  323. const html = renderToStaticMarkup(
  324. createElement(SettingsPage, { isDesktop: true, isWindowsDesktop: true }),
  325. );
  326. expect(html).toContain("mr-[134px]");
  327. expect(mockPlatform.getWindowControlsOverlayMetrics).not.toHaveBeenCalled();
  328. expect(mockPlatform.onWindowControlsOverlayChanged).not.toHaveBeenCalled();
  329. });
  330. it.each([
  331. ["macOS", { isDesktop: true, isMacDesktop: true, isWindowsDesktop: false }],
  332. ["Windows", { isDesktop: true, isMacDesktop: false, isWindowsDesktop: true }],
  333. ["Linux", { isDesktop: true, isMacDesktop: false, isWindowsDesktop: false }],
  334. ])(
  335. "keeps the %s settings content frame 4px from the desktop window edge",
  336. async (_platform, props) => {
  337. const { SettingsPage } = await import("../src/SettingsPage.js");
  338. const html = renderToStaticMarkup(createElement(SettingsPage, props));
  339. expect(html).toContain("grid-cols-[68px_minmax(0,1fr)]");
  340. expect(html).toContain("lg:grid-cols-[268px_minmax(0,1fr)]");
  341. expect(html).toMatch(/data-settings-content-frame="true"[^>]*p-1 pl-0 pt-0/);
  342. expect(html).toContain('data-settings-top-inset="true"');
  343. },
  344. );
  345. it("uses 5px for the Windows settings panel and preserves other platform radii", async () => {
  346. const { SettingsPage } = await import("../src/SettingsPage.js");
  347. const windowsHtml = renderToStaticMarkup(
  348. createElement(SettingsPage, { isDesktop: true, isWindowsDesktop: true }),
  349. );
  350. const macHtml = renderToStaticMarkup(
  351. createElement(SettingsPage, { isDesktop: true, isMacDesktop: true }),
  352. );
  353. const linuxHtml = renderToStaticMarkup(createElement(SettingsPage, { isDesktop: true }));
  354. expect(windowsHtml).toMatch(/data-settings-panel-frame="true"[^>]*rounded-\[5px\]/);
  355. expect(macHtml).toMatch(/data-settings-panel-frame="true"[^>]*rounded-xl/);
  356. expect(linuxHtml).toMatch(/data-settings-panel-frame="true"[^>]*rounded-xl/);
  357. });
  358. it("does not add the desktop frame inset to web settings", async () => {
  359. const { SettingsPage } = await import("../src/SettingsPage.js");
  360. const html = renderToStaticMarkup(createElement(SettingsPage, { isDesktop: false }));
  361. expect(html).toMatch(/data-settings-content-frame="true"[^>]*p-0/);
  362. expect(html).not.toContain('data-settings-top-inset="true"');
  363. });
  364. it("keeps Linux settings titlebar controls on the themed page background", async () => {
  365. const { SettingsPage } = await import("../src/SettingsPage.js");
  366. const html = renderToStaticMarkup(
  367. createElement(SettingsPage, {
  368. isDesktop: true,
  369. isMacDesktop: false,
  370. isWindowsDesktop: false,
  371. }),
  372. );
  373. expect(html).toContain('data-testid="desktop-window-controls"');
  374. expect(html).toContain("lucide-circle-question-mark");
  375. expect(html).not.toContain("lucide-chevron-down");
  376. expect(html).not.toContain("w-[120px]");
  377. expect(html).toContain("mr-[134px]");
  378. });
  379. it("lists shell options from the local host even when the remote workspace is non-Windows", async () => {
  380. remoteSystemInfo.mockResolvedValue({
  381. homedir: "/home/remote",
  382. platform: "linux",
  383. });
  384. localSystemInfo.mockResolvedValue({
  385. homedir: "C:\\Users\\local",
  386. platform: "win32",
  387. });
  388. const { SettingsPage } = await import("../src/SettingsPage.js");
  389. renderToStaticMarkup(createElement(SettingsPage, {}));
  390. flushPendingEffects();
  391. await flushPromises();
  392. expect(localSystemInfo).toHaveBeenCalled();
  393. expect(localListIntegratedTerminalShells).toHaveBeenCalled();
  394. expect(remoteSystemInfo).not.toHaveBeenCalled();
  395. expect(remoteListIntegratedTerminalShells).not.toHaveBeenCalled();
  396. });
  397. });