Files
gemini-cli/packages/core/src/utils/compatibility.test.ts
T
Spencer bd530829a2 test(core): fix env var leakage in compatibility tests
Clears environment variables like TERMINAL_EMULATOR, JETBRAINS_IDE, TMUX, STY, TERM, and TERM_PROGRAM before running compatibility tests to prevent local developer environments from causing false positives.

refactor(core): standardize warning prefixes

Replaces hardcoded warning emojis with the standard 'Warning:' prefix for consistency with existing warnings.
2026-03-12 20:34:26 +00:00

466 lines
14 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import os from 'node:os';
import {
isWindows10,
isJetBrainsTerminal,
isTmux,
isGnuScreen,
isLowColorTmux,
isDumbTerminal,
getTerminalNameFromEnv,
supports256Colors,
supportsTrueColor,
getCompatibilityWarnings,
WarningPriority,
} from './compatibility.js';
vi.mock('node:os', () => ({
default: {
platform: vi.fn(),
release: vi.fn(),
},
}));
describe('compatibility', () => {
const originalGetColorDepth = process.stdout.getColorDepth;
afterEach(() => {
process.stdout.getColorDepth = originalGetColorDepth;
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
describe('isWindows10', () => {
it.each<{
platform: NodeJS.Platform;
release: string;
expected: boolean;
desc: string;
}>([
{
platform: 'win32',
release: '10.0.19041',
expected: true,
desc: 'Windows 10 (build < 22000)',
},
{
platform: 'win32',
release: '10.0.22000',
expected: false,
desc: 'Windows 11 (build >= 22000)',
},
{
platform: 'darwin',
release: '20.6.0',
expected: false,
desc: 'non-Windows platforms',
},
])(
'should return $expected for $desc',
({ platform, release, expected }) => {
vi.mocked(os.platform).mockReturnValue(platform);
vi.mocked(os.release).mockReturnValue(release);
expect(isWindows10()).toBe(expected);
},
);
});
describe('isJetBrainsTerminal', () => {
it.each<{
env: Record<string, string>;
expected: boolean;
desc: string;
}>([
{
env: { TERMINAL_EMULATOR: 'JetBrains-JediTerm' },
expected: true,
desc: 'TERMINAL_EMULATOR starts with JetBrains',
},
{
env: { JETBRAINS_IDE: 'IntelliJ' },
expected: true,
desc: 'JETBRAINS_IDE is set',
},
{
env: { TERMINAL_EMULATOR: 'xterm' },
expected: false,
desc: 'other terminals',
},
{ env: {}, expected: false, desc: 'no env vars set' },
])('should return $expected when $desc', ({ env, expected }) => {
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('JETBRAINS_IDE', '');
for (const [key, value] of Object.entries(env)) {
vi.stubEnv(key, value);
}
expect(isJetBrainsTerminal()).toBe(expected);
});
});
describe('isTmux', () => {
it('should return true when TMUX is set', () => {
vi.stubEnv('TMUX', '/tmp/tmux-1001/default,1425,0');
expect(isTmux()).toBe(true);
});
it('should return false when TMUX is not set', () => {
vi.stubEnv('TMUX', '');
expect(isTmux()).toBe(false);
});
});
describe('isGnuScreen', () => {
it('should return true when STY is set', () => {
vi.stubEnv('STY', '1234.pts-0.host');
expect(isGnuScreen()).toBe(true);
});
it('should return false when STY is not set', () => {
vi.stubEnv('STY', '');
expect(isGnuScreen()).toBe(false);
});
});
describe('isLowColorTmux', () => {
it('should return true when TERM=screen and COLORTERM is not set', () => {
vi.stubEnv('TERM', 'screen');
vi.stubEnv('COLORTERM', '');
expect(isLowColorTmux()).toBe(true);
});
it('should return false when TERM=screen and COLORTERM is set', () => {
vi.stubEnv('TERM', 'screen');
vi.stubEnv('COLORTERM', 'truecolor');
expect(isLowColorTmux()).toBe(false);
});
it('should return false when TERM=xterm-256color', () => {
vi.stubEnv('TERM', 'xterm-256color');
vi.stubEnv('COLORTERM', '');
expect(isLowColorTmux()).toBe(false);
});
});
describe('isDumbTerminal', () => {
it('should return true when TERM=dumb', () => {
vi.stubEnv('TERM', 'dumb');
expect(isDumbTerminal()).toBe(true);
});
it('should return true when TERM=vt100', () => {
vi.stubEnv('TERM', 'vt100');
expect(isDumbTerminal()).toBe(true);
});
it('should return false when TERM=xterm', () => {
vi.stubEnv('TERM', 'xterm');
expect(isDumbTerminal()).toBe(false);
});
});
describe('getTerminalNameFromEnv', () => {
it('should prioritize TERM_PROGRAM', () => {
vi.stubEnv('TERM_PROGRAM', 'iTerm.app');
expect(getTerminalNameFromEnv()).toBe('iTerm.app');
});
it('should use JETBRAINS_IDE if TERM_PROGRAM is missing', () => {
vi.stubEnv('TERM_PROGRAM', '');
vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm');
vi.stubEnv('JETBRAINS_IDE', 'PyCharm');
expect(getTerminalNameFromEnv()).toBe('PyCharm');
});
it('should use TMUX if other vars are missing', () => {
vi.stubEnv('TERM_PROGRAM', '');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('JETBRAINS_IDE', '');
vi.stubEnv('TMUX', 'some-socket');
expect(getTerminalNameFromEnv()).toBe('tmux');
});
it('should use STY if other vars are missing', () => {
vi.stubEnv('TERM_PROGRAM', '');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('JETBRAINS_IDE', '');
vi.stubEnv('TMUX', '');
vi.stubEnv('STY', 'some-session');
expect(getTerminalNameFromEnv()).toBe('GNU screen');
});
it('should return Unknown if no vars are set', () => {
vi.stubEnv('TERM_PROGRAM', '');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('JETBRAINS_IDE', '');
vi.stubEnv('TMUX', '');
vi.stubEnv('STY', '');
expect(getTerminalNameFromEnv()).toBe('Unknown');
});
});
describe('supports256Colors', () => {
it.each<{
depth: number;
term?: string;
expected: boolean;
desc: string;
}>([
{
depth: 8,
term: undefined,
expected: true,
desc: 'getColorDepth returns >= 8',
},
{
depth: 4,
term: 'xterm-256color',
expected: true,
desc: 'TERM contains 256color',
},
{
depth: 4,
term: 'xterm',
expected: false,
desc: '256 colors are not supported',
},
])('should return $expected when $desc', ({ depth, term, expected }) => {
process.stdout.getColorDepth = vi.fn().mockReturnValue(depth);
if (term !== undefined) {
vi.stubEnv('TERM', term);
} else {
vi.stubEnv('TERM', '');
}
expect(supports256Colors()).toBe(expected);
});
});
describe('supportsTrueColor', () => {
it.each<{
colorterm: string;
depth: number;
expected: boolean;
desc: string;
}>([
{
colorterm: 'truecolor',
depth: 8,
expected: true,
desc: 'COLORTERM is truecolor',
},
{
colorterm: '24bit',
depth: 8,
expected: true,
desc: 'COLORTERM is 24bit',
},
{
colorterm: '',
depth: 24,
expected: true,
desc: 'getColorDepth returns >= 24',
},
{
colorterm: '',
depth: 8,
expected: false,
desc: 'true color is not supported',
},
])(
'should return $expected when $desc',
({ colorterm, depth, expected }) => {
vi.stubEnv('COLORTERM', colorterm);
process.stdout.getColorDepth = vi.fn().mockReturnValue(depth);
expect(supportsTrueColor()).toBe(expected);
},
);
});
describe('getCompatibilityWarnings', () => {
beforeEach(() => {
// Clear out potential local environment variables that might trigger warnings
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('JETBRAINS_IDE', '');
vi.stubEnv('TMUX', '');
vi.stubEnv('STY', '');
vi.stubEnv('TERM', 'xterm-256color'); // Prevent dumb terminal warning
vi.stubEnv('TERM_PROGRAM', '');
// Default to supporting true color to keep existing tests simple
vi.stubEnv('COLORTERM', 'truecolor');
process.stdout.getColorDepth = vi.fn().mockReturnValue(24);
});
it('should return Windows 10 warning when detected', () => {
vi.mocked(os.platform).mockReturnValue('win32');
vi.mocked(os.release).mockReturnValue('10.0.19041');
vi.stubEnv('TERMINAL_EMULATOR', '');
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'windows-10',
message: expect.stringContaining('Windows 10 detected'),
}),
);
});
it('should return JetBrains warning when detected and in alternate buffer', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm');
const warnings = getCompatibilityWarnings({ isAlternateBuffer: true });
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'jetbrains-terminal',
message: expect.stringContaining('JetBrains terminal detected'),
priority: WarningPriority.High,
}),
);
});
it('should return tmux warning when detected and in alternate buffer', () => {
vi.stubEnv('TMUX', '/tmp/tmux-1001/default,1,0');
const warnings = getCompatibilityWarnings({ isAlternateBuffer: true });
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'tmux-alternate-buffer',
message: expect.stringContaining('tmux detected'),
priority: WarningPriority.High,
}),
);
});
it('should return low-color tmux warning when detected', () => {
vi.stubEnv('TERM', 'screen');
vi.stubEnv('COLORTERM', '');
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'low-color-tmux',
message: expect.stringContaining('Limited color support detected'),
priority: WarningPriority.High,
}),
);
});
it('should return GNU screen warning when detected', () => {
vi.stubEnv('STY', '1234.pts-0.host');
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'gnu-screen',
message: expect.stringContaining('GNU screen detected'),
priority: WarningPriority.Low,
}),
);
});
it('should return dumb terminal warning when detected', () => {
vi.stubEnv('TERM', 'dumb');
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'dumb-terminal',
message: expect.stringContaining('Basic terminal detected'),
priority: WarningPriority.High,
}),
);
});
it('should not return JetBrains warning when detected but NOT in alternate buffer', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm');
const warnings = getCompatibilityWarnings({ isAlternateBuffer: false });
expect(
warnings.find((w) => w.id === 'jetbrains-terminal'),
).toBeUndefined();
});
it('should return 256-color warning when 256 colors are not supported', () => {
vi.mocked(os.platform).mockReturnValue('linux');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('COLORTERM', '');
vi.stubEnv('TERM', 'xterm');
process.stdout.getColorDepth = vi.fn().mockReturnValue(4);
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: '256-color',
message: expect.stringContaining('256-color support not detected'),
priority: WarningPriority.High,
}),
);
// Should NOT show true-color warning if 256-color warning is shown
expect(warnings.find((w) => w.id === 'true-color')).toBeUndefined();
});
it('should return true color warning when 256 colors are supported but true color is not, and not Apple Terminal', () => {
vi.mocked(os.platform).mockReturnValue('linux');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('COLORTERM', '');
vi.stubEnv('TERM_PROGRAM', 'xterm');
process.stdout.getColorDepth = vi.fn().mockReturnValue(8);
const warnings = getCompatibilityWarnings();
expect(warnings).toContainEqual(
expect.objectContaining({
id: 'true-color',
message: expect.stringContaining(
'True color (24-bit) support not detected',
),
priority: WarningPriority.Low,
}),
);
});
it('should NOT return true color warning for Apple Terminal', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('COLORTERM', '');
vi.stubEnv('TERM_PROGRAM', 'Apple_Terminal');
process.stdout.getColorDepth = vi.fn().mockReturnValue(8);
const warnings = getCompatibilityWarnings();
expect(warnings.find((w) => w.id === 'true-color')).toBeUndefined();
});
it('should return all warnings when all are detected', () => {
vi.mocked(os.platform).mockReturnValue('win32');
vi.mocked(os.release).mockReturnValue('10.0.19041');
vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm');
vi.stubEnv('COLORTERM', '');
vi.stubEnv('TERM_PROGRAM', 'xterm');
process.stdout.getColorDepth = vi.fn().mockReturnValue(8);
const warnings = getCompatibilityWarnings({ isAlternateBuffer: true });
expect(warnings).toHaveLength(3);
expect(warnings[0].message).toContain('Windows 10 detected');
expect(warnings[1].message).toContain('JetBrains');
expect(warnings[2].message).toContain(
'True color (24-bit) support not detected',
);
});
it('should return no warnings in a standard environment with true color', () => {
vi.mocked(os.platform).mockReturnValue('darwin');
vi.stubEnv('TERMINAL_EMULATOR', '');
vi.stubEnv('COLORTERM', 'truecolor');
const warnings = getCompatibilityWarnings();
expect(warnings).toHaveLength(0);
});
});
});