mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-19 02:20:42 -07:00
Improve code coverage for cli package (#13724)
This commit is contained in:
56
packages/cli/src/core/auth.test.ts
Normal file
56
packages/cli/src/core/auth.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { performInitialAuth } from './auth.js';
|
||||
import { type Config } from '@google/gemini-cli-core';
|
||||
|
||||
vi.mock('@google/gemini-cli-core', () => ({
|
||||
AuthType: {
|
||||
OAUTH: 'oauth',
|
||||
},
|
||||
getErrorMessage: (e: unknown) => (e as Error).message,
|
||||
}));
|
||||
|
||||
const AuthType = {
|
||||
OAUTH: 'oauth',
|
||||
} as const;
|
||||
|
||||
describe('auth', () => {
|
||||
let mockConfig: Config;
|
||||
|
||||
beforeEach(() => {
|
||||
mockConfig = {
|
||||
refreshAuth: vi.fn(),
|
||||
} as unknown as Config;
|
||||
});
|
||||
|
||||
it('should return null if authType is undefined', async () => {
|
||||
const result = await performInitialAuth(mockConfig, undefined);
|
||||
expect(result).toBeNull();
|
||||
expect(mockConfig.refreshAuth).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return null on successful auth', async () => {
|
||||
const result = await performInitialAuth(
|
||||
mockConfig,
|
||||
AuthType.OAUTH as unknown as Parameters<typeof performInitialAuth>[1],
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
expect(mockConfig.refreshAuth).toHaveBeenCalledWith(AuthType.OAUTH);
|
||||
});
|
||||
|
||||
it('should return error message on failed auth', async () => {
|
||||
const error = new Error('Auth failed');
|
||||
vi.mocked(mockConfig.refreshAuth).mockRejectedValue(error);
|
||||
const result = await performInitialAuth(
|
||||
mockConfig,
|
||||
AuthType.OAUTH as unknown as Parameters<typeof performInitialAuth>[1],
|
||||
);
|
||||
expect(result).toBe('Failed to login. Message: Auth failed');
|
||||
expect(mockConfig.refreshAuth).toHaveBeenCalledWith(AuthType.OAUTH);
|
||||
});
|
||||
});
|
||||
148
packages/cli/src/core/initializer.test.ts
Normal file
148
packages/cli/src/core/initializer.test.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { initializeApp } from './initializer.js';
|
||||
import {
|
||||
IdeClient,
|
||||
logIdeConnection,
|
||||
logCliConfiguration,
|
||||
type Config,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { performInitialAuth } from './auth.js';
|
||||
import { validateTheme } from './theme.js';
|
||||
import { type LoadedSettings } from '../config/settings.js';
|
||||
|
||||
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import('@google/gemini-cli-core')>();
|
||||
return {
|
||||
...actual,
|
||||
IdeClient: {
|
||||
getInstance: vi.fn(),
|
||||
},
|
||||
logIdeConnection: vi.fn(),
|
||||
logCliConfiguration: vi.fn(),
|
||||
StartSessionEvent: vi.fn(),
|
||||
IdeConnectionEvent: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('./auth.js', () => ({
|
||||
performInitialAuth: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('./theme.js', () => ({
|
||||
validateTheme: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('initializer', () => {
|
||||
let mockConfig: {
|
||||
getToolRegistry: ReturnType<typeof vi.fn>;
|
||||
getIdeMode: ReturnType<typeof vi.fn>;
|
||||
getGeminiMdFileCount: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let mockSettings: LoadedSettings;
|
||||
let mockIdeClient: {
|
||||
connect: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockConfig = {
|
||||
getToolRegistry: vi.fn(),
|
||||
getIdeMode: vi.fn().mockReturnValue(false),
|
||||
getGeminiMdFileCount: vi.fn().mockReturnValue(5),
|
||||
};
|
||||
mockSettings = {
|
||||
merged: {
|
||||
security: {
|
||||
auth: {
|
||||
selectedType: 'oauth',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as LoadedSettings;
|
||||
mockIdeClient = {
|
||||
connect: vi.fn(),
|
||||
};
|
||||
vi.mocked(IdeClient.getInstance).mockResolvedValue(
|
||||
mockIdeClient as unknown as IdeClient,
|
||||
);
|
||||
vi.mocked(performInitialAuth).mockResolvedValue(null);
|
||||
vi.mocked(validateTheme).mockReturnValue(null);
|
||||
});
|
||||
|
||||
it('should initialize correctly in non-IDE mode', async () => {
|
||||
const result = await initializeApp(
|
||||
mockConfig as unknown as Config,
|
||||
mockSettings,
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
authError: null,
|
||||
themeError: null,
|
||||
shouldOpenAuthDialog: false,
|
||||
geminiMdFileCount: 5,
|
||||
});
|
||||
expect(performInitialAuth).toHaveBeenCalledWith(mockConfig, 'oauth');
|
||||
expect(validateTheme).toHaveBeenCalledWith(mockSettings);
|
||||
expect(logCliConfiguration).toHaveBeenCalled();
|
||||
expect(IdeClient.getInstance).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should initialize correctly in IDE mode', async () => {
|
||||
mockConfig.getIdeMode.mockReturnValue(true);
|
||||
const result = await initializeApp(
|
||||
mockConfig as unknown as Config,
|
||||
mockSettings,
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
authError: null,
|
||||
themeError: null,
|
||||
shouldOpenAuthDialog: false,
|
||||
geminiMdFileCount: 5,
|
||||
});
|
||||
expect(IdeClient.getInstance).toHaveBeenCalled();
|
||||
expect(mockIdeClient.connect).toHaveBeenCalled();
|
||||
expect(logIdeConnection).toHaveBeenCalledWith(
|
||||
mockConfig as unknown as Config,
|
||||
expect.any(Object),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle auth error', async () => {
|
||||
vi.mocked(performInitialAuth).mockResolvedValue('Auth failed');
|
||||
const result = await initializeApp(
|
||||
mockConfig as unknown as Config,
|
||||
mockSettings,
|
||||
);
|
||||
|
||||
expect(result.authError).toBe('Auth failed');
|
||||
expect(result.shouldOpenAuthDialog).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle undefined auth type', async () => {
|
||||
mockSettings.merged.security!.auth!.selectedType = undefined;
|
||||
const result = await initializeApp(
|
||||
mockConfig as unknown as Config,
|
||||
mockSettings,
|
||||
);
|
||||
|
||||
expect(result.shouldOpenAuthDialog).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle theme error', async () => {
|
||||
vi.mocked(validateTheme).mockReturnValue('Theme not found');
|
||||
const result = await initializeApp(
|
||||
mockConfig as unknown as Config,
|
||||
mockSettings,
|
||||
);
|
||||
|
||||
expect(result.themeError).toBe('Theme not found');
|
||||
});
|
||||
});
|
||||
54
packages/cli/src/core/theme.test.ts
Normal file
54
packages/cli/src/core/theme.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { validateTheme } from './theme.js';
|
||||
import { themeManager } from '../ui/themes/theme-manager.js';
|
||||
import { type LoadedSettings } from '../config/settings.js';
|
||||
|
||||
vi.mock('../ui/themes/theme-manager.js', () => ({
|
||||
themeManager: {
|
||||
findThemeByName: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('theme', () => {
|
||||
let mockSettings: LoadedSettings;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockSettings = {
|
||||
merged: {
|
||||
ui: {
|
||||
theme: 'test-theme',
|
||||
},
|
||||
},
|
||||
} as unknown as LoadedSettings;
|
||||
});
|
||||
|
||||
it('should return null if theme is found', () => {
|
||||
vi.mocked(themeManager.findThemeByName).mockReturnValue(
|
||||
{} as unknown as ReturnType<typeof themeManager.findThemeByName>,
|
||||
);
|
||||
const result = validateTheme(mockSettings);
|
||||
expect(result).toBeNull();
|
||||
expect(themeManager.findThemeByName).toHaveBeenCalledWith('test-theme');
|
||||
});
|
||||
|
||||
it('should return error message if theme is not found', () => {
|
||||
vi.mocked(themeManager.findThemeByName).mockReturnValue(undefined);
|
||||
const result = validateTheme(mockSettings);
|
||||
expect(result).toBe('Theme "test-theme" not found.');
|
||||
expect(themeManager.findThemeByName).toHaveBeenCalledWith('test-theme');
|
||||
});
|
||||
|
||||
it('should return null if theme is undefined', () => {
|
||||
mockSettings.merged.ui!.theme = undefined;
|
||||
const result = validateTheme(mockSettings);
|
||||
expect(result).toBeNull();
|
||||
expect(themeManager.findThemeByName).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user