This commit is contained in:
Fan
2025-08-02 23:44:35 +08:00
committed by GitHub
parent a0dbb40dae
commit dbc0eb4336
2 changed files with 21 additions and 1 deletions

View File

@@ -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<
@@ -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`);
@@ -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();
});
@@ -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';

View File

@@ -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;
}
@@ -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);
}