Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/cli/src/validateNonInterActiveAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('validateNonInterActiveAuth', () => {
let originalEnvGeminiApiKey: string | undefined;
let originalEnvVertexAi: string | undefined;
let originalEnvGcp: string | undefined;
let originalEnvOpenAiApiKey: string | undefined;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
let processExitSpy: ReturnType<typeof vi.spyOn>;
let refreshAuthMock: jest.MockedFunction<
Expand All @@ -25,9 +26,11 @@ describe('validateNonInterActiveAuth', () => {
originalEnvGeminiApiKey = process.env.GEMINI_API_KEY;
originalEnvVertexAi = process.env.GOOGLE_GENAI_USE_VERTEXAI;
originalEnvGcp = process.env.GOOGLE_GENAI_USE_GCA;
originalEnvOpenAiApiKey = process.env.OPENAI_API_KEY;
delete process.env.GEMINI_API_KEY;
delete process.env.GOOGLE_GENAI_USE_VERTEXAI;
delete process.env.GOOGLE_GENAI_USE_GCA;
delete process.env.OPENAI_API_KEY;
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
processExitSpy = vi.spyOn(process, 'exit').mockImplementation((code) => {
throw new Error(`process.exit(${code}) called`);
Expand All @@ -51,6 +54,11 @@ describe('validateNonInterActiveAuth', () => {
} else {
delete process.env.GOOGLE_GENAI_USE_GCA;
}
if (originalEnvOpenAiApiKey !== undefined) {
process.env.OPENAI_API_KEY = originalEnvOpenAiApiKey;
} else {
delete process.env.OPENAI_API_KEY;
}
vi.restoreAllMocks();
});

Expand Down Expand Up @@ -88,6 +96,15 @@ describe('validateNonInterActiveAuth', () => {
expect(refreshAuthMock).toHaveBeenCalledWith(AuthType.USE_GEMINI);
});

it('uses USE_OPENAI if OPENAI_API_KEY is set', async () => {
process.env.OPENAI_API_KEY = 'fake-openai-key';
const nonInteractiveConfig: NonInteractiveConfig = {
refreshAuth: refreshAuthMock,
};
await validateNonInteractiveAuth(undefined, nonInteractiveConfig);
expect(refreshAuthMock).toHaveBeenCalledWith(AuthType.USE_OPENAI);
});

it('uses USE_VERTEX_AI if GOOGLE_GENAI_USE_VERTEXAI is true (with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION)', async () => {
process.env.GOOGLE_GENAI_USE_VERTEXAI = 'true';
process.env.GOOGLE_CLOUD_PROJECT = 'test-project';
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/validateNonInterActiveAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function getAuthTypeFromEnv(): AuthType | undefined {
if (process.env.GEMINI_API_KEY) {
return AuthType.USE_GEMINI;
}
if (process.env.OPENAI_API_KEY) {
return AuthType.USE_OPENAI;
}
return undefined;
}

Expand All @@ -29,7 +32,7 @@ export async function validateNonInteractiveAuth(

if (!effectiveAuthType) {
console.error(
`Please set an Auth method in your ${USER_SETTINGS_PATH} or specify one of the following environment variables before running: GEMINI_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA`,
`Please set an Auth method in your ${USER_SETTINGS_PATH} or specify one of the following environment variables before running: GEMINI_API_KEY, OPENAI_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA`,
);
process.exit(1);
}
Expand Down
Loading