File size: 12,493 Bytes
52efc7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Patch: Unset NO_COLOR at the very top before any imports
if (process.env['NO_COLOR'] !== undefined) {
delete process.env['NO_COLOR'];
}
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { themeManager, DEFAULT_THEME } from './theme-manager.js';
import { debugLogger, type CustomTheme } from '@google/gemini-cli-core';
import * as fs from 'node:fs';
import * as os from 'node:os';
import type * as osActual from 'node:os';
vi.mock('node:fs');
vi.mock('node:os', async (importOriginal) => {
const actualOs = await importOriginal<typeof osActual>();
return {
...actualOs,
homedir: vi.fn(),
platform: vi.fn(() => 'linux'),
};
});
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
homedir: () => os.homedir(),
};
});
const validCustomTheme: CustomTheme = {
type: 'custom',
name: 'MyCustomTheme',
Background: '#000000',
Foreground: '#ffffff',
LightBlue: '#89BDCD',
AccentBlue: '#3B82F6',
AccentPurple: '#8B5CF6',
AccentCyan: '#06B6D4',
AccentGreen: '#3CA84B',
AccentYellow: 'yellow',
AccentRed: 'red',
DiffAdded: 'green',
DiffRemoved: 'red',
Comment: 'gray',
Gray: 'gray',
};
describe('ThemeManager', () => {
beforeEach(() => {
// Reset themeManager state and inject mocks
themeManager.reinitialize({ fs, homedir: os.homedir });
themeManager.loadCustomThemes({});
themeManager.setActiveTheme(DEFAULT_THEME.name);
themeManager.setTerminalBackground(undefined);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should load valid custom themes', () => {
themeManager.loadCustomThemes({ MyCustomTheme: validCustomTheme });
expect(themeManager.getCustomThemeNames()).toContain('MyCustomTheme');
expect(themeManager.isCustomTheme('MyCustomTheme')).toBe(true);
});
it('should set and get the active theme', () => {
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
themeManager.setActiveTheme('Ayu');
expect(themeManager.getActiveTheme().name).toBe('Ayu');
});
it('should set and get a custom active theme', () => {
themeManager.loadCustomThemes({ MyCustomTheme: validCustomTheme });
themeManager.setActiveTheme('MyCustomTheme');
expect(themeManager.getActiveTheme().name).toBe('MyCustomTheme');
});
it('should return false when setting a non-existent theme', () => {
expect(themeManager.setActiveTheme('NonExistentTheme')).toBe(false);
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
});
it('should list available themes including custom themes', () => {
themeManager.loadCustomThemes({ MyCustomTheme: validCustomTheme });
const available = themeManager.getAvailableThemes();
expect(
available.some(
(t: { name: string; isCustom?: boolean }) =>
t.name === 'MyCustomTheme' && t.isCustom,
),
).toBe(true);
});
it('should get a theme by name', () => {
expect(themeManager.getTheme('Ayu')).toBeDefined();
themeManager.loadCustomThemes({ MyCustomTheme: validCustomTheme });
expect(themeManager.getTheme('MyCustomTheme')).toBeDefined();
});
it('should fall back to default theme if active theme is invalid', () => {
(themeManager as unknown as { activeTheme: unknown }).activeTheme = {
name: 'NonExistent',
type: 'custom',
};
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
});
it('should return NoColorTheme if NO_COLOR is set', () => {
const original = process.env['NO_COLOR'];
process.env['NO_COLOR'] = '1';
expect(themeManager.getActiveTheme().name).toBe('NoColor');
if (original === undefined) {
delete process.env['NO_COLOR'];
} else {
process.env['NO_COLOR'] = original;
}
});
describe('when loading a theme from a file', () => {
const mockThemePath = './my-theme.json';
const mockTheme: CustomTheme = {
...validCustomTheme,
name: 'My File Theme',
};
beforeEach(() => {
vi.mocked(os.homedir).mockReturnValue('/home/user');
vi.spyOn(fs, 'realpathSync').mockImplementation((p) => p as string);
});
it('should load a theme from a valid file path', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify(mockTheme));
const result = themeManager.setActiveTheme('/home/user/my-theme.json');
expect(result).toBe(true);
const activeTheme = themeManager.getActiveTheme();
expect(activeTheme.name).toBe('My File Theme');
expect(fs.readFileSync).toHaveBeenCalledWith(
expect.stringContaining('my-theme.json'),
'utf-8',
);
});
it('should not load a theme if the file does not exist', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
const result = themeManager.setActiveTheme(mockThemePath);
expect(result).toBe(false);
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
});
it('should not load a theme from a file with invalid JSON', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'readFileSync').mockReturnValue('invalid json');
const result = themeManager.setActiveTheme(mockThemePath);
expect(result).toBe(false);
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
});
it('should not load a theme from an untrusted file path and log a message', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify(mockTheme));
const consoleWarnSpy = vi
.spyOn(debugLogger, 'warn')
.mockImplementation(() => {});
const result = themeManager.setActiveTheme('/untrusted/my-theme.json');
expect(result).toBe(false);
expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name);
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('is outside your home directory'),
);
consoleWarnSpy.mockRestore();
});
});
describe('extension themes', () => {
it('should register and unregister themes from extensions with namespacing', () => {
const extTheme: CustomTheme = {
...validCustomTheme,
name: 'ExtensionTheme',
};
const extensionName = 'test-extension';
const namespacedName = `ExtensionTheme (${extensionName})`;
themeManager.registerExtensionThemes(extensionName, [extTheme]);
expect(themeManager.getCustomThemeNames()).toContain(namespacedName);
expect(themeManager.isCustomTheme(namespacedName)).toBe(true);
themeManager.unregisterExtensionThemes(extensionName, [extTheme]);
expect(themeManager.getCustomThemeNames()).not.toContain(namespacedName);
expect(themeManager.isCustomTheme(namespacedName)).toBe(false);
});
it('should not allow extension themes to overwrite built-in themes even with prefixing', () => {
// availableThemes has 'Ayu'.
// We verify that it DOES prefix, so it won't collide even if extension name is similar.
themeManager.registerExtensionThemes('Ext', [
{ ...validCustomTheme, name: 'Theme' },
]);
expect(themeManager.getCustomThemeNames()).toContain('Theme (Ext)');
});
it('should allow extension themes and settings themes to coexist', () => {
const extTheme: CustomTheme = {
...validCustomTheme,
name: 'ExtensionTheme',
};
const settingsTheme: CustomTheme = {
...validCustomTheme,
name: 'SettingsTheme',
};
themeManager.registerExtensionThemes('Ext', [extTheme]);
themeManager.loadCustomThemes({ SettingsTheme: settingsTheme });
expect(themeManager.getCustomThemeNames()).toContain(
'ExtensionTheme (Ext)',
);
expect(themeManager.getCustomThemeNames()).toContain('SettingsTheme');
expect(themeManager.isCustomTheme('ExtensionTheme (Ext)')).toBe(true);
expect(themeManager.isCustomTheme('SettingsTheme')).toBe(true);
});
});
describe('terminalBackground override', () => {
it('should store and retrieve terminal background', () => {
themeManager.setTerminalBackground('#123456');
expect(themeManager.getTerminalBackground()).toBe('#123456');
themeManager.setTerminalBackground(undefined);
expect(themeManager.getTerminalBackground()).toBeUndefined();
});
it('should override background.primary in semantic colors when terminal background is set', () => {
const color = '#1a1a1a';
themeManager.setTerminalBackground(color);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors.background.primary).toBe(color);
});
it('should override Background in colors when terminal background is set', () => {
const color = '#1a1a1a';
themeManager.setTerminalBackground(color);
const colors = themeManager.getColors();
expect(colors.Background).toBe(color);
});
it('should re-calculate dependent semantic colors when terminal background is set', () => {
themeManager.setTerminalBackground('#000000');
const semanticColors = themeManager.getSemanticColors();
// border.default should be interpolated from background (#000000) and Gray
// ui.dark should be interpolated from Gray and background (#000000)
expect(semanticColors.border.default).toBeDefined();
expect(semanticColors.ui.dark).toBeDefined();
expect(semanticColors.border.default).not.toBe(
DEFAULT_THEME.semanticColors.border.default,
);
});
it('should return original semantic colors when terminal background is NOT set', () => {
themeManager.setTerminalBackground(undefined);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors).toEqual(DEFAULT_THEME.semanticColors);
});
it('should NOT override background when theme is incompatible (Light theme on Dark terminal)', () => {
themeManager.setActiveTheme('Default Light');
const darkTerminalBg = '#000000';
themeManager.setTerminalBackground(darkTerminalBg);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors.background.primary).toBe(
themeManager.getTheme('Default Light')!.colors.Background,
);
const colors = themeManager.getColors();
expect(colors.Background).toBe(
themeManager.getTheme('Default Light')!.colors.Background,
);
});
it('should NOT override background when theme is incompatible (Dark theme on Light terminal)', () => {
themeManager.setActiveTheme('Default');
const lightTerminalBg = '#FFFFFF';
themeManager.setTerminalBackground(lightTerminalBg);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors.background.primary).toBe(
themeManager.getTheme('Default')!.colors.Background,
);
const colors = themeManager.getColors();
expect(colors.Background).toBe(
themeManager.getTheme('Default')!.colors.Background,
);
});
it('should override background for custom theme when compatible', () => {
themeManager.loadCustomThemes({
MyDark: {
name: 'MyDark',
type: 'custom',
Background: '#000000',
Foreground: '#ffffff',
},
});
themeManager.setActiveTheme('MyDark');
const darkTerminalBg = '#1a1a1a';
themeManager.setTerminalBackground(darkTerminalBg);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors.background.primary).toBe(darkTerminalBg);
});
it('should NOT override background for custom theme when incompatible', () => {
themeManager.loadCustomThemes({
MyLight: {
name: 'MyLight',
type: 'custom',
Background: '#ffffff',
Foreground: '#000000',
},
});
themeManager.setActiveTheme('MyLight');
const darkTerminalBg = '#000000';
themeManager.setTerminalBackground(darkTerminalBg);
const semanticColors = themeManager.getSemanticColors();
expect(semanticColors.background.primary).toBe('#ffffff');
});
});
});
|