File size: 4,410 Bytes
52efc7b | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { authCommand } from './authCommand.js';
import { type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import { SettingScope } from '../../config/settings.js';
import type { GeminiClient } from '@google/gemini-cli-core';
vi.mock('@google/gemini-cli-core', async () => {
const actual = await vi.importActual('@google/gemini-cli-core');
return {
...actual,
clearCachedCredentialFile: vi.fn().mockResolvedValue(undefined),
};
});
describe('authCommand', () => {
let mockContext: CommandContext;
beforeEach(() => {
mockContext = createMockCommandContext({
services: {
agentContext: {
geminiClient: {
stripThoughtsFromHistory: vi.fn(),
},
},
},
});
// Add setValue mock to settings
mockContext.services.settings.setValue = vi.fn();
vi.clearAllMocks();
});
it('should have subcommands: signin and signout', () => {
expect(authCommand.subCommands).toBeDefined();
expect(authCommand.subCommands).toHaveLength(2);
expect(authCommand.subCommands?.[0]?.name).toBe('signin');
expect(authCommand.subCommands?.[0]?.altNames).toContain('login');
expect(authCommand.subCommands?.[1]?.name).toBe('signout');
expect(authCommand.subCommands?.[1]?.altNames).toContain('logout');
});
it('should return a dialog action to open the auth dialog when called with no args', () => {
if (!authCommand.action) {
throw new Error('The auth command must have an action.');
}
const result = authCommand.action(mockContext, '');
expect(result).toEqual({
type: 'dialog',
dialog: 'auth',
});
});
it('should have the correct name and description', () => {
expect(authCommand.name).toBe('auth');
expect(authCommand.description).toBe('Manage authentication');
});
describe('auth signin subcommand', () => {
it('should return auth dialog action', () => {
const loginCommand = authCommand.subCommands?.[0];
expect(loginCommand?.name).toBe('signin');
const result = loginCommand!.action!(mockContext, '');
expect(result).toEqual({ type: 'dialog', dialog: 'auth' });
});
});
describe('auth signout subcommand', () => {
it('should clear cached credentials', async () => {
const logoutCommand = authCommand.subCommands?.[1];
expect(logoutCommand?.name).toBe('signout');
const { clearCachedCredentialFile } = await import(
'@google/gemini-cli-core'
);
await logoutCommand!.action!(mockContext, '');
expect(clearCachedCredentialFile).toHaveBeenCalledOnce();
});
it('should clear selectedAuthType setting', async () => {
const logoutCommand = authCommand.subCommands?.[1];
await logoutCommand!.action!(mockContext, '');
expect(mockContext.services.settings.setValue).toHaveBeenCalledWith(
SettingScope.User,
'security.auth.selectedType',
undefined,
);
});
it('should strip thoughts from history', async () => {
const logoutCommand = authCommand.subCommands?.[1];
const mockStripThoughts = vi.fn();
const mockClient = {
stripThoughtsFromHistory: mockStripThoughts,
} as unknown as GeminiClient;
if (mockContext.services.agentContext?.config) {
mockContext.services.agentContext.config.getGeminiClient = vi.fn(
() => mockClient,
);
}
await logoutCommand!.action!(mockContext, '');
expect(
mockContext.services.agentContext?.geminiClient
.stripThoughtsFromHistory,
).toHaveBeenCalled();
});
it('should return logout action to signal explicit state change', async () => {
const logoutCommand = authCommand.subCommands?.[1];
const result = await logoutCommand!.action!(mockContext, '');
expect(result).toEqual({ type: 'logout' });
});
it('should handle missing config gracefully', async () => {
const logoutCommand = authCommand.subCommands?.[1];
mockContext.services.agentContext = null;
const result = await logoutCommand!.action!(mockContext, '');
expect(result).toEqual({ type: 'logout' });
});
});
});
|