| | |
| | |
| | |
| | |
| | |
| |
|
| | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| | import { |
| | main, |
| | setupUnhandledRejectionHandler, |
| | validateDnsResolutionOrder, |
| | startInteractiveUI, |
| | } from './gemini.js'; |
| | import type { SettingsFile } from './config/settings.js'; |
| | import { LoadedSettings, loadSettings } from './config/settings.js'; |
| | import { appEvents, AppEvent } from './utils/events.js'; |
| | import type { Config } from '@google/gemini-cli-core'; |
| | import { FatalConfigError } from '@google/gemini-cli-core'; |
| |
|
| | |
| | class MockProcessExitError extends Error { |
| | constructor(readonly code?: string | number | null | undefined) { |
| | super('PROCESS_EXIT_MOCKED'); |
| | this.name = 'MockProcessExitError'; |
| | } |
| | } |
| |
|
| | |
| | vi.mock('./config/settings.js', async (importOriginal) => { |
| | const actual = await importOriginal<typeof import('./config/settings.js')>(); |
| | return { |
| | ...actual, |
| | loadSettings: vi.fn(), |
| | }; |
| | }); |
| |
|
| | vi.mock('./config/config.js', () => ({ |
| | loadCliConfig: vi.fn().mockResolvedValue({ |
| | config: { |
| | getSandbox: vi.fn(() => false), |
| | getQuestion: vi.fn(() => ''), |
| | }, |
| | modelWasSwitched: false, |
| | originalModelBeforeSwitch: null, |
| | finalModel: 'test-model', |
| | }), |
| | })); |
| |
|
| | vi.mock('read-package-up', () => ({ |
| | readPackageUp: vi.fn().mockResolvedValue({ |
| | packageJson: { name: 'test-pkg', version: 'test-version' }, |
| | path: '/fake/path/package.json', |
| | }), |
| | })); |
| |
|
| | vi.mock('update-notifier', () => ({ |
| | default: vi.fn(() => ({ |
| | notify: vi.fn(), |
| | })), |
| | })); |
| |
|
| | vi.mock('./utils/events.js', async (importOriginal) => { |
| | const actual = await importOriginal<typeof import('./utils/events.js')>(); |
| | return { |
| | ...actual, |
| | appEvents: { |
| | emit: vi.fn(), |
| | }, |
| | }; |
| | }); |
| |
|
| | vi.mock('./utils/sandbox.js', () => ({ |
| | sandbox_command: vi.fn(() => ''), |
| | start_sandbox: vi.fn(() => Promise.resolve()), |
| | })); |
| |
|
| | describe('gemini.tsx main function', () => { |
| | let loadSettingsMock: ReturnType<typeof vi.mocked<typeof loadSettings>>; |
| | let originalEnvGeminiSandbox: string | undefined; |
| | let originalEnvSandbox: string | undefined; |
| | let initialUnhandledRejectionListeners: NodeJS.UnhandledRejectionListener[] = |
| | []; |
| |
|
| | const processExitSpy = vi |
| | .spyOn(process, 'exit') |
| | .mockImplementation((code) => { |
| | throw new MockProcessExitError(code); |
| | }); |
| |
|
| | beforeEach(() => { |
| | loadSettingsMock = vi.mocked(loadSettings); |
| |
|
| | |
| | originalEnvGeminiSandbox = process.env['GEMINI_SANDBOX']; |
| | originalEnvSandbox = process.env['SANDBOX']; |
| | delete process.env['GEMINI_SANDBOX']; |
| | delete process.env['SANDBOX']; |
| |
|
| | initialUnhandledRejectionListeners = |
| | process.listeners('unhandledRejection'); |
| | }); |
| |
|
| | afterEach(() => { |
| | |
| | if (originalEnvGeminiSandbox !== undefined) { |
| | process.env['GEMINI_SANDBOX'] = originalEnvGeminiSandbox; |
| | } else { |
| | delete process.env['GEMINI_SANDBOX']; |
| | } |
| | if (originalEnvSandbox !== undefined) { |
| | process.env['SANDBOX'] = originalEnvSandbox; |
| | } else { |
| | delete process.env['SANDBOX']; |
| | } |
| |
|
| | const currentListeners = process.listeners('unhandledRejection'); |
| | const addedListener = currentListeners.find( |
| | (listener) => !initialUnhandledRejectionListeners.includes(listener), |
| | ); |
| |
|
| | if (addedListener) { |
| | process.removeListener('unhandledRejection', addedListener); |
| | } |
| | vi.restoreAllMocks(); |
| | }); |
| |
|
| | it('should throw InvalidConfigurationError if settings have errors', async () => { |
| | const settingsError = { |
| | message: 'Test settings error', |
| | path: '/test/settings.json', |
| | }; |
| | const userSettingsFile: SettingsFile = { |
| | path: '/user/settings.json', |
| | settings: {}, |
| | }; |
| | const workspaceSettingsFile: SettingsFile = { |
| | path: '/workspace/.gemini/settings.json', |
| | settings: {}, |
| | }; |
| | const systemSettingsFile: SettingsFile = { |
| | path: '/system/settings.json', |
| | settings: {}, |
| | }; |
| | const systemDefaultsFile: SettingsFile = { |
| | path: '/system/system-defaults.json', |
| | settings: {}, |
| | }; |
| | const mockLoadedSettings = new LoadedSettings( |
| | systemSettingsFile, |
| | systemDefaultsFile, |
| | userSettingsFile, |
| | workspaceSettingsFile, |
| | [settingsError], |
| | true, |
| | new Set(), |
| | ); |
| |
|
| | loadSettingsMock.mockReturnValue(mockLoadedSettings); |
| |
|
| | await expect(main()).rejects.toThrow(FatalConfigError); |
| | }); |
| |
|
| | it('should log unhandled promise rejections and open debug console on first error', async () => { |
| | const appEventsMock = vi.mocked(appEvents); |
| | const rejectionError = new Error('Test unhandled rejection'); |
| |
|
| | setupUnhandledRejectionHandler(); |
| | |
| | |
| | |
| | process.emit('unhandledRejection', rejectionError, Promise.resolve()); |
| |
|
| | |
| | await new Promise(process.nextTick); |
| |
|
| | expect(appEventsMock.emit).toHaveBeenCalledWith(AppEvent.OpenDebugConsole); |
| | expect(appEventsMock.emit).toHaveBeenCalledWith( |
| | AppEvent.LogError, |
| | expect.stringContaining('Unhandled Promise Rejection'), |
| | ); |
| | expect(appEventsMock.emit).toHaveBeenCalledWith( |
| | AppEvent.LogError, |
| | expect.stringContaining('Please file a bug report using the /bug tool.'), |
| | ); |
| |
|
| | |
| | const secondRejectionError = new Error('Second test unhandled rejection'); |
| | process.emit('unhandledRejection', secondRejectionError, Promise.resolve()); |
| | await new Promise(process.nextTick); |
| |
|
| | |
| | const openDebugConsoleCalls = appEventsMock.emit.mock.calls.filter( |
| | (call) => call[0] === AppEvent.OpenDebugConsole, |
| | ); |
| | expect(openDebugConsoleCalls.length).toBe(1); |
| |
|
| | |
| | processExitSpy.mockRestore(); |
| | }); |
| | }); |
| |
|
| | describe('validateDnsResolutionOrder', () => { |
| | let consoleWarnSpy: ReturnType<typeof vi.spyOn>; |
| |
|
| | beforeEach(() => { |
| | consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| | }); |
| |
|
| | afterEach(() => { |
| | consoleWarnSpy.mockRestore(); |
| | }); |
| |
|
| | it('should return "ipv4first" when the input is "ipv4first"', () => { |
| | expect(validateDnsResolutionOrder('ipv4first')).toBe('ipv4first'); |
| | expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| | }); |
| |
|
| | it('should return "verbatim" when the input is "verbatim"', () => { |
| | expect(validateDnsResolutionOrder('verbatim')).toBe('verbatim'); |
| | expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| | }); |
| |
|
| | it('should return the default "ipv4first" when the input is undefined', () => { |
| | expect(validateDnsResolutionOrder(undefined)).toBe('ipv4first'); |
| | expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| | }); |
| |
|
| | it('should return the default "ipv4first" and log a warning for an invalid string', () => { |
| | expect(validateDnsResolutionOrder('invalid-value')).toBe('ipv4first'); |
| | expect(consoleWarnSpy).toHaveBeenCalledOnce(); |
| | expect(consoleWarnSpy).toHaveBeenCalledWith( |
| | 'Invalid value for dnsResolutionOrder in settings: "invalid-value". Using default "ipv4first".', |
| | ); |
| | }); |
| | }); |
| |
|
| | describe('startInteractiveUI', () => { |
| | |
| | const mockConfig = { |
| | getProjectRoot: () => '/root', |
| | getScreenReader: () => false, |
| | } as Config; |
| | const mockSettings = { |
| | merged: { |
| | ui: { |
| | hideWindowTitle: false, |
| | }, |
| | }, |
| | } as LoadedSettings; |
| | const mockStartupWarnings = ['warning1']; |
| | const mockWorkspaceRoot = '/root'; |
| |
|
| | vi.mock('./utils/version.js', () => ({ |
| | getCliVersion: vi.fn(() => Promise.resolve('1.0.0')), |
| | })); |
| |
|
| | vi.mock('./ui/utils/kittyProtocolDetector.js', () => ({ |
| | detectAndEnableKittyProtocol: vi.fn(() => Promise.resolve()), |
| | })); |
| |
|
| | vi.mock('./ui/utils/updateCheck.js', () => ({ |
| | checkForUpdates: vi.fn(() => Promise.resolve(null)), |
| | })); |
| |
|
| | vi.mock('./utils/cleanup.js', () => ({ |
| | cleanupCheckpoints: vi.fn(() => Promise.resolve()), |
| | registerCleanup: vi.fn(), |
| | })); |
| |
|
| | vi.mock('ink', () => ({ |
| | render: vi.fn().mockReturnValue({ unmount: vi.fn() }), |
| | })); |
| |
|
| | beforeEach(() => { |
| | vi.clearAllMocks(); |
| | }); |
| |
|
| | it('should render the UI with proper React context and exitOnCtrlC disabled', async () => { |
| | const { render } = await import('ink'); |
| | const renderSpy = vi.mocked(render); |
| |
|
| | await startInteractiveUI( |
| | mockConfig, |
| | mockSettings, |
| | mockStartupWarnings, |
| | mockWorkspaceRoot, |
| | ); |
| |
|
| | |
| | expect(renderSpy).toHaveBeenCalledTimes(1); |
| | const [reactElement, options] = renderSpy.mock.calls[0]; |
| |
|
| | |
| | expect(options).toEqual({ |
| | exitOnCtrlC: false, |
| | isScreenReaderEnabled: false, |
| | }); |
| |
|
| | |
| | expect(reactElement).toBeDefined(); |
| | }); |
| |
|
| | it('should perform all startup tasks in correct order', async () => { |
| | const { getCliVersion } = await import('./utils/version.js'); |
| | const { detectAndEnableKittyProtocol } = await import( |
| | './ui/utils/kittyProtocolDetector.js' |
| | ); |
| | const { checkForUpdates } = await import('./ui/utils/updateCheck.js'); |
| | const { registerCleanup } = await import('./utils/cleanup.js'); |
| |
|
| | await startInteractiveUI( |
| | mockConfig, |
| | mockSettings, |
| | mockStartupWarnings, |
| | mockWorkspaceRoot, |
| | ); |
| |
|
| | |
| | expect(getCliVersion).toHaveBeenCalledTimes(1); |
| | expect(detectAndEnableKittyProtocol).toHaveBeenCalledTimes(1); |
| | expect(registerCleanup).toHaveBeenCalledTimes(1); |
| |
|
| | |
| | const cleanupFn = vi.mocked(registerCleanup).mock.calls[0][0]; |
| | expect(typeof cleanupFn).toBe('function'); |
| |
|
| | |
| | |
| | await new Promise((resolve) => setTimeout(resolve, 0)); |
| | expect(checkForUpdates).toHaveBeenCalledTimes(1); |
| | }); |
| | }); |
| |
|