summaryrefslogtreecommitdiff
path: root/packages/cli/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/cli/src')
-rw-r--r--packages/cli/src/config/config.test.ts50
-rw-r--r--packages/cli/src/config/config.ts7
2 files changed, 53 insertions, 4 deletions
diff --git a/packages/cli/src/config/config.test.ts b/packages/cli/src/config/config.test.ts
index 26964761..1859943b 100644
--- a/packages/cli/src/config/config.test.ts
+++ b/packages/cli/src/config/config.test.ts
@@ -36,8 +36,8 @@ vi.mock('@gemini-cli/core', async () => {
loadEnvironment: vi.fn(),
Config: vi.fn((params) => ({
// Mock the config object and its methods
- getApiKey: () => params.apiKey,
- getModel: () => params.model,
+ getApiKey: () => params.contentGeneratorConfig.apiKey,
+ getModel: () => params.contentGeneratorConfig.model,
getSandbox: () => params.sandbox,
getTargetDir: () => params.targetDir,
getDebugMode: () => params.debugMode,
@@ -51,7 +51,7 @@ vi.mock('@gemini-cli/core', async () => {
getUserAgent: () => params.userAgent,
getUserMemory: () => params.userMemory,
getGeminiMdFileCount: () => params.geminiMdFileCount,
- getVertexAI: () => params.vertexai,
+ getVertexAI: () => params.contentGeneratorConfig.vertexai,
getShowMemoryUsage: () => params.showMemoryUsage, // Added for the test
getTelemetry: () => params.telemetry,
// Add any other methods that are called on the config object
@@ -175,6 +175,50 @@ describe('loadCliConfig telemetry', () => {
});
});
+describe('API Key Handling', () => {
+ const originalEnv = { ...process.env };
+ const originalArgv = process.argv;
+
+ beforeEach(() => {
+ vi.resetAllMocks();
+ process.argv = ['node', 'script.js'];
+ });
+
+ afterEach(() => {
+ process.env = originalEnv;
+ process.argv = originalArgv;
+ });
+
+ it('should use GEMINI_API_KEY from env', async () => {
+ process.env.GEMINI_API_KEY = 'gemini-key';
+ delete process.env.GOOGLE_API_KEY;
+
+ const settings: Settings = {};
+ const result = await loadCliConfig(settings, []);
+ expect(result.getApiKey()).toBe('gemini-key');
+ });
+
+ it('should use GOOGLE_API_KEY and warn when both GOOGLE_API_KEY and GEMINI_API_KEY are set', async () => {
+ const consoleWarnSpy = vi
+ .spyOn(console, 'warn')
+ .mockImplementation(() => {});
+
+ process.env.GEMINI_API_KEY = 'gemini-key';
+ process.env.GOOGLE_API_KEY = 'google-key';
+
+ const settings: Settings = {};
+ const result = await loadCliConfig(settings, []);
+
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
+ '[WARN]',
+ 'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.',
+ );
+ expect(result.getApiKey()).toBe('google-key');
+
+ consoleWarnSpy.mockRestore();
+ });
+});
+
describe('Hierarchical Memory Loading (config.ts) - Placeholder Suite', () => {
beforeEach(() => {
vi.resetAllMocks();
diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts
index 1cff28f8..83cfb296 100644
--- a/packages/cli/src/config/config.ts
+++ b/packages/cli/src/config/config.ts
@@ -188,6 +188,11 @@ async function createContentGeneratorConfig(
const hasVertexProjectLocationConfig =
!!googleCloudProject && !!googleCloudLocation;
+ if (hasGeminiApiKey && hasGoogleApiKey) {
+ logger.warn(
+ 'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.',
+ );
+ }
if (!hasGeminiApiKey && !hasGoogleApiKey && !hasVertexProjectLocationConfig) {
logger.error(
'No valid API authentication configuration found. Please set ONE of the following combinations in your environment variables or .env file:\n' +
@@ -203,7 +208,7 @@ async function createContentGeneratorConfig(
const config: ContentGeneratorConfig = {
model: argv.model || DEFAULT_GEMINI_MODEL,
- apiKey: geminiApiKey || googleApiKey || '',
+ apiKey: googleApiKey || geminiApiKey || '',
vertexai: hasGeminiApiKey ? false : undefined,
};