fix(auth): prioritize GEMINI_API_KEY env var and skip unnecessary key… (#14745)

This commit is contained in:
Gal Zahavi
2025-12-12 17:50:21 -08:00
committed by GitHub
parent 942bcfc61e
commit 57c7b9ccce
10 changed files with 122 additions and 19 deletions

View File

@@ -12,8 +12,18 @@ import {
useTextBuffer,
type TextBuffer,
} from '../components/shared/text-buffer.js';
import { clearApiKey } from '@google/gemini-cli-core';
// Mocks
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
clearApiKey: vi.fn().mockResolvedValue(undefined),
};
});
vi.mock('../hooks/useKeypress.js', () => ({
useKeypress: vi.fn(),
}));
@@ -37,7 +47,8 @@ describe('ApiAuthDialog', () => {
let mockBuffer: TextBuffer;
beforeEach(() => {
vi.resetAllMocks();
vi.clearAllMocks();
vi.stubEnv('GEMINI_API_KEY', '');
mockBuffer = {
text: '',
lines: [''],
@@ -91,7 +102,9 @@ describe('ApiAuthDialog', () => {
({ keyName, sequence, expectedCall, args }) => {
mockBuffer.text = 'submitted-key'; // Set for the onSubmit case
render(<ApiAuthDialog onSubmit={onSubmit} onCancel={onCancel} />);
const keypressHandler = mockedUseKeypress.mock.calls[0][0];
// calls[0] is the ApiAuthDialog's useKeypress (Ctrl+C handler)
// calls[1] is the TextInput's useKeypress (typing handler)
const keypressHandler = mockedUseKeypress.mock.calls[1][0];
keypressHandler({
name: keyName,
@@ -117,4 +130,20 @@ describe('ApiAuthDialog', () => {
expect(lastFrame()).toContain('Invalid API Key');
});
it('calls clearApiKey and clears buffer when Ctrl+C is pressed', async () => {
render(<ApiAuthDialog onSubmit={onSubmit} onCancel={onCancel} />);
// calls[0] is the ApiAuthDialog's useKeypress (Ctrl+C handler)
const keypressHandler = mockedUseKeypress.mock.calls[0][0];
await keypressHandler({
name: 'c',
ctrl: true,
meta: false,
shift: false,
});
expect(clearApiKey).toHaveBeenCalled();
expect(mockBuffer.setText).toHaveBeenCalledWith('');
});
});