File size: 4,318 Bytes
84aa3bf | 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 | /**
* @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({
authError: null,
accountSuspensionInfo: 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,
accountSuspensionInfo: 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,
);
// Wait for the background promise to resolve
await new Promise((resolve) => setTimeout(resolve, 0));
expect(result).toEqual({
authError: null,
accountSuspensionInfo: 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({
authError: 'Auth failed',
accountSuspensionInfo: null,
});
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');
});
});
|