File size: 13,143 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
describe,
it,
expect,
vi,
beforeEach,
type Mock,
type MockInstance,
afterEach,
} from 'vitest';
import { handleFallback } from './handler.js';
import type { Config } from '../config/config.js';
import type { ModelAvailabilityService } from '../availability/modelAvailabilityService.js';
import { createAvailabilityServiceMock } from '../availability/testUtils.js';
import { AuthType } from '../core/contentGenerator.js';
import {
DEFAULT_GEMINI_FLASH_MODEL,
DEFAULT_GEMINI_MODEL,
DEFAULT_GEMINI_MODEL_AUTO,
PREVIEW_GEMINI_FLASH_MODEL,
PREVIEW_GEMINI_MODEL,
PREVIEW_GEMINI_MODEL_AUTO,
} from '../config/models.js';
import type { FallbackModelHandler } from './types.js';
import { openBrowserSecurely } from '../utils/secure-browser-launcher.js';
import { debugLogger } from '../utils/debugLogger.js';
import * as policyHelpers from '../availability/policyHelpers.js';
import { createDefaultPolicy } from '../availability/policyCatalog.js';
import {
RetryableQuotaError,
TerminalQuotaError,
} from '../utils/googleQuotaErrors.js';
// Mock the telemetry logger and event class
vi.mock('../telemetry/index.js', () => ({
logFlashFallback: vi.fn(),
FlashFallbackEvent: class {},
}));
vi.mock('../utils/secure-browser-launcher.js', () => ({
openBrowserSecurely: vi.fn(),
shouldLaunchBrowser: vi.fn().mockReturnValue(true),
}));
// Mock debugLogger to prevent console pollution and allow spying
vi.mock('../utils/debugLogger.js', () => ({
debugLogger: {
warn: vi.fn(),
error: vi.fn(),
log: vi.fn(),
},
}));
const MOCK_PRO_MODEL = DEFAULT_GEMINI_MODEL;
const FALLBACK_MODEL = DEFAULT_GEMINI_FLASH_MODEL;
const AUTH_OAUTH = AuthType.LOGIN_WITH_GOOGLE;
const createMockConfig = (overrides: Partial<Config> = {}): Config =>
({
fallbackHandler: undefined,
getFallbackModelHandler: vi.fn(),
setActiveModel: vi.fn(),
setModel: vi.fn(),
activateFallbackMode: vi.fn(),
rotateSessionId: vi.fn(),
getModelAvailabilityService: vi.fn(() =>
createAvailabilityServiceMock({
selectedModel: FALLBACK_MODEL,
skipped: [],
}),
),
getActiveModel: vi.fn(() => MOCK_PRO_MODEL),
getModel: vi.fn(() => MOCK_PRO_MODEL),
getUserTier: vi.fn(() => undefined),
isInteractive: vi.fn(() => false),
getHasAccessToPreviewModel: vi.fn(() => false),
...overrides,
}) as unknown as Config;
describe('handleFallback', () => {
let mockConfig: Config;
let mockHandler: Mock<FallbackModelHandler>;
let consoleErrorSpy: MockInstance;
beforeEach(() => {
vi.clearAllMocks();
mockHandler = vi.fn();
// Default setup: OAuth user, Pro model failed, handler injected
mockConfig = createMockConfig({
fallbackModelHandler: mockHandler,
});
// Explicitly set the property to ensure it's present for legacy checks
mockConfig.fallbackModelHandler = mockHandler;
// We mocked debugLogger, so we don't need to spy on console.error for handler failures
// But tests might check console.error usage in legacy code if any?
// The handler uses console.error in legacyHandleFallback.
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
consoleErrorSpy.mockRestore();
});
describe('policy-driven flow', () => {
let policyConfig: Config;
let availability: ModelAvailabilityService;
let policyHandler: Mock<FallbackModelHandler>;
beforeEach(() => {
vi.clearAllMocks();
availability = createAvailabilityServiceMock({
selectedModel: DEFAULT_GEMINI_FLASH_MODEL,
skipped: [],
});
policyHandler = vi.fn().mockResolvedValue('retry_once');
policyConfig = createMockConfig();
// Ensure we test the availability path
vi.mocked(policyConfig.getModelAvailabilityService).mockReturnValue(
availability,
);
vi.mocked(policyConfig.getFallbackModelHandler).mockReturnValue(
policyHandler,
);
});
it('uses availability selection with correct candidates when enabled', async () => {
// Direct mock manipulation since it's already a vi.fn()
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
await handleFallback(policyConfig, DEFAULT_GEMINI_MODEL, AUTH_OAUTH);
expect(availability.selectFirstAvailable).toHaveBeenCalledWith([
DEFAULT_GEMINI_FLASH_MODEL,
]);
});
it('falls back to last resort when availability returns null', async () => {
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
availability.selectFirstAvailable = vi
.fn()
.mockReturnValue({ selectedModel: null, skipped: [] });
policyHandler.mockResolvedValue('retry_once');
await handleFallback(policyConfig, MOCK_PRO_MODEL, AUTH_OAUTH);
expect(policyHandler).toHaveBeenCalledWith(
MOCK_PRO_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
undefined,
);
});
it('executes silent policy action without invoking UI handler', async () => {
const proPolicy = createDefaultPolicy(MOCK_PRO_MODEL);
const flashPolicy = createDefaultPolicy(DEFAULT_GEMINI_FLASH_MODEL);
flashPolicy.actions = {
...flashPolicy.actions,
terminal: 'silent',
unknown: 'silent',
};
flashPolicy.isLastResort = true;
const silentChain = [proPolicy, flashPolicy];
const chainSpy = vi
.spyOn(policyHelpers, 'resolvePolicyChain')
.mockReturnValue(silentChain);
try {
availability.selectFirstAvailable = vi.fn().mockReturnValue({
selectedModel: DEFAULT_GEMINI_FLASH_MODEL,
skipped: [],
});
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(true);
expect(policyConfig.getFallbackModelHandler).not.toHaveBeenCalled();
expect(policyConfig.activateFallbackMode).toHaveBeenCalledWith(
DEFAULT_GEMINI_FLASH_MODEL,
undefined,
);
} finally {
chainSpy.mockRestore();
}
});
it('does not wrap around to upgrade candidates if the current model was selected at the end (e.g. by router)', async () => {
// Last-resort failure (Flash) in [Preview, Pro, Flash] checks Preview then Pro (all upstream).
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
availability.selectFirstAvailable = vi.fn().mockReturnValue({
selectedModel: MOCK_PRO_MODEL,
skipped: [],
});
// Mock activeModel to be unavailable so the utility bypass heuristic is skipped
vi.mocked(availability.snapshot).mockReturnValue({ available: false });
policyHandler.mockResolvedValue('retry_once');
await handleFallback(
policyConfig,
DEFAULT_GEMINI_FLASH_MODEL,
AUTH_OAUTH,
);
expect(availability.selectFirstAvailable).not.toHaveBeenCalled();
expect(policyHandler).toHaveBeenCalledWith(
DEFAULT_GEMINI_FLASH_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
undefined,
);
});
it('successfully follows expected availability response for Preview Chain', async () => {
availability.selectFirstAvailable = vi.fn().mockReturnValue({
selectedModel: PREVIEW_GEMINI_FLASH_MODEL,
skipped: [],
});
policyHandler.mockResolvedValue('retry_once');
vi.mocked(policyConfig.getActiveModel).mockReturnValue(
PREVIEW_GEMINI_MODEL,
);
vi.mocked(policyConfig.getModel).mockReturnValue(
PREVIEW_GEMINI_MODEL_AUTO,
);
vi.mocked(policyConfig.getHasAccessToPreviewModel).mockReturnValue(true);
const result = await handleFallback(
policyConfig,
PREVIEW_GEMINI_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(true);
expect(availability.selectFirstAvailable).toHaveBeenCalledWith([
PREVIEW_GEMINI_FLASH_MODEL,
]);
});
it('should launch upgrade flow and avoid fallback mode when handler returns "upgrade"', async () => {
policyHandler.mockResolvedValue('upgrade');
vi.mocked(openBrowserSecurely).mockResolvedValue(undefined);
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(false);
expect(openBrowserSecurely).toHaveBeenCalledWith(
'https://goo.gle/set-up-gemini-code-assist',
);
expect(policyConfig.activateFallbackMode).not.toHaveBeenCalled();
});
it('should catch errors from the handler, log an error, and return null', async () => {
const handlerError = new Error('UI interaction failed');
policyHandler.mockRejectedValue(handlerError);
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBeNull();
expect(debugLogger.error).toHaveBeenCalledWith(
'Fallback handler failed:',
handlerError,
);
});
it('should pass TerminalQuotaError (429) correctly to the handler', async () => {
const mockGoogleApiError = {
code: 429,
message: 'mock error',
details: [],
};
const terminalError = new TerminalQuotaError(
'Quota error',
mockGoogleApiError,
5,
);
policyHandler.mockResolvedValue('retry_always');
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
terminalError,
);
expect(policyHandler).toHaveBeenCalledWith(
MOCK_PRO_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
terminalError,
);
});
it('should pass RetryableQuotaError correctly to the handler', async () => {
const mockGoogleApiError = {
code: 503,
message: 'mock error',
details: [],
};
const retryableError = new RetryableQuotaError(
'Service unavailable',
mockGoogleApiError,
1000,
);
policyHandler.mockResolvedValue('retry_once');
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
retryableError,
);
expect(policyHandler).toHaveBeenCalledWith(
MOCK_PRO_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
retryableError,
);
});
it('Call the handler with fallback model same as the failed model when the failed model is the last-resort policy', async () => {
// Ensure short-circuit when wrapping to an unavailable upstream model.
availability.selectFirstAvailable = vi
.fn()
.mockReturnValue({ selectedModel: null, skipped: [] });
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
// Mock activeModel to be unavailable so the utility bypass heuristic is skipped
vi.mocked(availability.snapshot).mockReturnValue({ available: false });
const result = await handleFallback(
policyConfig,
DEFAULT_GEMINI_FLASH_MODEL,
AUTH_OAUTH,
);
policyHandler.mockResolvedValue('retry_once');
expect(result).not.toBeNull();
expect(policyHandler).toHaveBeenCalledWith(
DEFAULT_GEMINI_FLASH_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
undefined,
);
});
it('calls activateFallbackMode when handler returns "retry_always"', async () => {
policyHandler.mockResolvedValue('retry_always');
vi.mocked(policyConfig.getModel).mockReturnValue(
DEFAULT_GEMINI_MODEL_AUTO,
);
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(true);
expect(policyConfig.activateFallbackMode).toHaveBeenCalledWith(
FALLBACK_MODEL,
undefined,
);
// TODO: add logging expect statement
});
it('does NOT call activateFallbackMode when handler returns "stop"', async () => {
policyHandler.mockResolvedValue('stop');
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(false);
expect(policyConfig.activateFallbackMode).not.toHaveBeenCalled();
// TODO: add logging expect statement
});
it('does NOT call activateFallbackMode when handler returns "retry_once"', async () => {
policyHandler.mockResolvedValue('retry_once');
const result = await handleFallback(
policyConfig,
MOCK_PRO_MODEL,
AUTH_OAUTH,
);
expect(result).toBe(true);
expect(policyConfig.activateFallbackMode).not.toHaveBeenCalled();
});
});
});
|