Improve error messages on failed onboarding (#17357)

This commit is contained in:
Gaurav
2026-01-26 06:31:19 -08:00
committed by GitHub
parent cb772a5b7f
commit 5fe328c56a
17 changed files with 458 additions and 56 deletions
+219 -1
View File
@@ -5,7 +5,13 @@
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { setupUser, ProjectIdRequiredError } from './setup.js';
import {
ProjectIdRequiredError,
setupUser,
ValidationCancelledError,
} from './setup.js';
import { ValidationRequiredError } from '../utils/googleQuotaErrors.js';
import { ChangeAuthRequestedError } from '../utils/errors.js';
import { CodeAssistServer } from '../code_assist/server.js';
import type { OAuth2Client } from 'google-auth-library';
import type { GeminiUserTier } from './types.js';
@@ -307,3 +313,215 @@ describe('setupUser for new user', () => {
});
});
});
describe('setupUser validation', () => {
let mockLoad: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.resetAllMocks();
mockLoad = vi.fn();
vi.mocked(CodeAssistServer).mockImplementation(
() =>
({
loadCodeAssist: mockLoad,
}) as unknown as CodeAssistServer,
);
});
afterEach(() => {
vi.unstubAllEnvs();
});
it('should throw error if LoadCodeAssist returns ineligible tiers and no current tier', async () => {
mockLoad.mockResolvedValue({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'User is not eligible',
reasonCode: 'INELIGIBLE_ACCOUNT',
tierId: 'standard-tier',
tierName: 'standard',
},
],
});
await expect(setupUser({} as OAuth2Client)).rejects.toThrow(
'User is not eligible',
);
});
it('should retry if validation handler returns verify', async () => {
// First call fails
mockLoad.mockResolvedValueOnce({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'User is not eligible',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
validationLearnMoreUrl: 'https://example.com/learn',
},
],
});
// Second call succeeds
mockLoad.mockResolvedValueOnce({
currentTier: mockPaidTier,
cloudaicompanionProject: 'test-project',
});
const mockValidationHandler = vi.fn().mockResolvedValue('verify');
const result = await setupUser({} as OAuth2Client, mockValidationHandler);
expect(mockValidationHandler).toHaveBeenCalledWith(
'https://example.com/verify',
'User is not eligible',
);
expect(mockLoad).toHaveBeenCalledTimes(2);
expect(result).toEqual({
projectId: 'test-project',
userTier: 'standard-tier',
userTierName: 'paid',
});
});
it('should throw if validation handler returns cancel', async () => {
mockLoad.mockResolvedValue({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'User is not eligible',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
},
],
});
const mockValidationHandler = vi.fn().mockResolvedValue('cancel');
await expect(
setupUser({} as OAuth2Client, mockValidationHandler),
).rejects.toThrow(ValidationCancelledError);
expect(mockValidationHandler).toHaveBeenCalled();
expect(mockLoad).toHaveBeenCalledTimes(1);
});
it('should throw ChangeAuthRequestedError if validation handler returns change_auth', async () => {
mockLoad.mockResolvedValue({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'User is not eligible',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
},
],
});
const mockValidationHandler = vi.fn().mockResolvedValue('change_auth');
await expect(
setupUser({} as OAuth2Client, mockValidationHandler),
).rejects.toThrow(ChangeAuthRequestedError);
expect(mockValidationHandler).toHaveBeenCalled();
expect(mockLoad).toHaveBeenCalledTimes(1);
});
it('should throw ValidationRequiredError without handler', async () => {
mockLoad.mockResolvedValue({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'Please verify your account',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
},
],
});
await expect(setupUser({} as OAuth2Client)).rejects.toThrow(
ValidationRequiredError,
);
expect(mockLoad).toHaveBeenCalledTimes(1);
});
it('should throw error if LoadCodeAssist returns empty response', async () => {
mockLoad.mockResolvedValue(null);
await expect(setupUser({} as OAuth2Client)).rejects.toThrow(
'LoadCodeAssist returned empty response',
);
});
it('should retry multiple times when validation handler keeps returning verify', async () => {
// First two calls fail with validation required
mockLoad
.mockResolvedValueOnce({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'Verify 1',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
},
],
})
.mockResolvedValueOnce({
currentTier: null,
ineligibleTiers: [
{
reasonMessage: 'Verify 2',
reasonCode: 'VALIDATION_REQUIRED',
tierId: 'standard-tier',
tierName: 'standard',
validationUrl: 'https://example.com/verify',
},
],
})
.mockResolvedValueOnce({
currentTier: mockPaidTier,
cloudaicompanionProject: 'test-project',
});
const mockValidationHandler = vi.fn().mockResolvedValue('verify');
const result = await setupUser({} as OAuth2Client, mockValidationHandler);
expect(mockValidationHandler).toHaveBeenCalledTimes(2);
expect(mockLoad).toHaveBeenCalledTimes(3);
expect(result).toEqual({
projectId: 'test-project',
userTier: 'standard-tier',
userTierName: 'paid',
});
});
});
describe('ValidationRequiredError', () => {
const error = new ValidationRequiredError(
'Account validation required: Please verify',
undefined,
'https://example.com/verify',
'Please verify',
);
it('should be an instance of Error', () => {
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(ValidationRequiredError);
});
it('should have the correct properties', () => {
expect(error.validationLink).toBe('https://example.com/verify');
expect(error.validationDescription).toBe('Please verify');
});
});