diff --git a/packages/cli/src/ui/hooks/useTerminalTheme.test.tsx b/packages/cli/src/ui/hooks/useTerminalTheme.test.tsx index 56b1c84bbf..d20c6149b0 100644 --- a/packages/cli/src/ui/hooks/useTerminalTheme.test.tsx +++ b/packages/cli/src/ui/hooks/useTerminalTheme.test.tsx @@ -196,6 +196,36 @@ describe('useTerminalTheme', () => { expect(mockHandleThemeSelect).not.toHaveBeenCalled(); }); + it('should switch theme even if terminal background report is identical to previousColor if current theme is mismatched', () => { + // Background is dark at startup + config.setTerminalBackground('#000000'); + vi.mocked(config.setTerminalBackground).mockClear(); + // But theme is light + mockSettings.merged.ui.theme = 'default-light'; + + const refreshStatic = vi.fn(); + const { unmount } = renderHook(() => + useTerminalTheme(mockHandleThemeSelect, config, refreshStatic), + ); + + const handler = mockSubscribe.mock.calls[0][0]; + + // Terminal reports the same dark background + handler('rgb:0000/0000/0000'); + + expect(config.setTerminalBackground).not.toHaveBeenCalled(); + expect(themeManager.setTerminalBackground).not.toHaveBeenCalled(); + expect(refreshStatic).not.toHaveBeenCalled(); + // But it SHOULD select the dark theme because of the mismatch! + expect(mockHandleThemeSelect).toHaveBeenCalledWith( + 'default', + expect.anything(), + ); + + mockSettings.merged.ui.theme = 'default'; + unmount(); + }); + it('should not switch theme if autoThemeSwitching is disabled', () => { mockSettings.merged.ui.autoThemeSwitching = false; const { unmount } = renderHook(() => diff --git a/packages/cli/src/ui/hooks/useTerminalTheme.ts b/packages/cli/src/ui/hooks/useTerminalTheme.ts index 9927847758..5590c2a97c 100644 --- a/packages/cli/src/ui/hooks/useTerminalTheme.ts +++ b/packages/cli/src/ui/hooks/useTerminalTheme.ts @@ -59,14 +59,6 @@ export function useTerminalTheme( if (!hexColor) return; const previousColor = config.getTerminalBackground(); - - if (previousColor === hexColor) { - return; - } - - config.setTerminalBackground(hexColor); - themeManager.setTerminalBackground(hexColor); - const luminance = getLuminance(hexColor); const currentThemeName = settings.merged.ui.theme; @@ -77,6 +69,16 @@ export function useTerminalTheme( DefaultLight.name, ); + if (previousColor === hexColor) { + if (newTheme) { + void handleThemeSelect(newTheme, SettingScope.User); + } + return; + } + + config.setTerminalBackground(hexColor); + themeManager.setTerminalBackground(hexColor); + if (newTheme) { void handleThemeSelect(newTheme, SettingScope.User); } else {