Files
gemini-cli/packages/core/test-setup.ts
T
gemini-cli[bot] f534f3f17a fix(core): route personal OAuth users to stable models and fix test regressions
This PR resolves multiple issues:
1.  **Auth-Aware Model Routing**: Implements routing to stable models for users using personal OAuth (`oauth-personal`) to prevent 404/400 errors for unavailable preview models.
2.  **Config Class Stability**: Fixes a crash in `Config.getAuthType` by adding optional chaining for `contentGeneratorConfig`, which could be uninitialized in some test scenarios.
3.  **Test Environment Isolation**: Fixes widespread test failures in both `core` and `cli` packages by stubbing `GEMINI_CLI_HOME` in global test setups. This prevents CI-specific environment variables from interfering with home directory resolution in tests.
4.  **Lint Fix**: Resolves a lint error in `models_auth.test.ts` caused by using the spread operator on a class-cast object.

Fixes: #26857

cc @NTaylorMullen
2026-05-15 23:09:23 +00:00

65 lines
2.0 KiB
TypeScript

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Unset NO_COLOR environment variable to ensure consistent theme behavior between local and CI test runs
if (process.env.NO_COLOR !== undefined) {
delete process.env.NO_COLOR;
}
import { setSimulate429 } from './src/utils/testUtils.js';
import { vi, afterEach, beforeEach } from 'vitest';
import { coreEvents } from './src/utils/events.js';
// Increase max listeners to avoid warnings in large test suites
coreEvents.setMaxListeners(100);
// Disable 429 simulation globally for all tests
setSimulate429(false);
beforeEach(() => {
// Ensure a clean home directory state for all tests
vi.stubEnv('GEMINI_CLI_HOME', '');
});
afterEach(() => {
vi.unstubAllEnvs();
});
// Default mocks for Storage and ProjectRegistry to prevent disk access in most tests.
// These can be overridden in specific tests using vi.unmock().
vi.mock('./src/config/projectRegistry.js', async (importOriginal) => {
const actual =
await importOriginal<typeof import('./src/config/projectRegistry.js')>();
actual.ProjectRegistry.prototype.initialize = vi.fn(() =>
Promise.resolve(undefined),
);
actual.ProjectRegistry.prototype.getShortId = vi.fn(() =>
Promise.resolve('project-slug'),
);
return actual;
});
vi.mock('./src/config/storageMigration.js', async (importOriginal) => {
const actual =
await importOriginal<typeof import('./src/config/storageMigration.js')>();
actual.StorageMigration.migrateDirectory = vi.fn(() =>
Promise.resolve(undefined),
);
return actual;
});
vi.mock('./src/config/storage.js', async (importOriginal) => {
const actual =
await importOriginal<typeof import('./src/config/storage.js')>();
actual.Storage.prototype.initialize = vi.fn(() => Promise.resolve(undefined));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(actual.Storage.prototype as any).getProjectIdentifier = vi.fn(
() => 'project-slug',
);
return actual;
});