diff options
| author | Tommaso Sciortino <[email protected]> | 2025-07-07 15:52:04 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-07 22:52:04 +0000 |
| commit | 426b6905dad7c7b437b665b4750f1307e278d7ce (patch) | |
| tree | f2975a9a0df7396e466ba7d7a9c7b7a312bd379a | |
| parent | 48c2aa296a7ed8b17e861d54682b0ea71695ca66 (diff) | |
Fix typo and add tests for auth validation. (#3491)
| -rw-r--r-- | packages/cli/src/config/auth.test.ts | 75 | ||||
| -rw-r--r-- | packages/cli/src/config/auth.ts | 5 |
2 files changed, 79 insertions, 1 deletions
diff --git a/packages/cli/src/config/auth.test.ts b/packages/cli/src/config/auth.test.ts new file mode 100644 index 00000000..a4c0cf25 --- /dev/null +++ b/packages/cli/src/config/auth.test.ts @@ -0,0 +1,75 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { AuthType } from '@google/gemini-cli-core'; +import { vi } from 'vitest'; +import { validateAuthMethod } from './auth.js'; + +vi.mock('./settings.js', () => ({ + loadEnvironment: vi.fn(), +})); + +describe('validateAuthMethod', () => { + const originalEnv = process.env; + + beforeEach(() => { + vi.resetModules(); + process.env = {}; + }); + + afterEach(() => { + process.env = originalEnv; + }); + + it('should return null for LOGIN_WITH_GOOGLE', () => { + expect(validateAuthMethod(AuthType.LOGIN_WITH_GOOGLE)).toBeNull(); + }); + + it('should return null for CLOUD_SHELL', () => { + expect(validateAuthMethod(AuthType.CLOUD_SHELL)).toBeNull(); + }); + + describe('USE_GEMINI', () => { + it('should return null if GEMINI_API_KEY is set', () => { + process.env.GEMINI_API_KEY = 'test-key'; + expect(validateAuthMethod(AuthType.USE_GEMINI)).toBeNull(); + }); + + it('should return an error message if GEMINI_API_KEY is not set', () => { + expect(validateAuthMethod(AuthType.USE_GEMINI)).toBe( + 'GEMINI_API_KEY environment variable not found. Add that to your .env and try again, no reload needed!', + ); + }); + }); + + describe('USE_VERTEX_AI', () => { + it('should return null if GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION are set', () => { + process.env.GOOGLE_CLOUD_PROJECT = 'test-project'; + process.env.GOOGLE_CLOUD_LOCATION = 'test-location'; + expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull(); + }); + + it('should return null if GOOGLE_API_KEY is set', () => { + process.env.GOOGLE_API_KEY = 'test-api-key'; + expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull(); + }); + + it('should return an error message if no required environment variables are set', () => { + expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBe( + 'Must specify GOOGLE_GENAI_USE_VERTEXAI=true and either:\n' + + '• GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION environment variables.\n' + + '• GOOGLE_API_KEY environment variable (if using express mode).\n' + + 'Update your .env and try again, no reload needed!', + ); + }); + }); + + it('should return an error message for an invalid auth method', () => { + expect(validateAuthMethod('invalid-method')).toBe( + 'Invalid auth method selected.', + ); + }); +}); diff --git a/packages/cli/src/config/auth.ts b/packages/cli/src/config/auth.ts index 4561f5d6..0f97b4d5 100644 --- a/packages/cli/src/config/auth.ts +++ b/packages/cli/src/config/auth.ts @@ -9,7 +9,10 @@ import { loadEnvironment } from './settings.js'; export const validateAuthMethod = (authMethod: string): string | null => { loadEnvironment(); - if (authMethod === AuthType.LOGIN_WITH_GOOGLE || AuthType.CLOUD_SHELL) { + if ( + authMethod === AuthType.LOGIN_WITH_GOOGLE || + authMethod === AuthType.CLOUD_SHELL + ) { return null; } |
