File size: 9,965 Bytes
9332ccf | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { renderWithProviders } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { act } from 'react';
import { AgentConfigDialog } from './AgentConfigDialog.js';
import { LoadedSettings, SettingScope } from '../../config/settings.js';
import type { AgentDefinition } from '@google/gemini-cli-core';
enum TerminalKeys {
ENTER = '\u000D',
TAB = '\t',
UP_ARROW = '\u001B[A',
DOWN_ARROW = '\u001B[B',
ESCAPE = '\u001B',
}
const createMockSettings = (
userSettings = {},
workspaceSettings = {},
): LoadedSettings => {
const settings = new LoadedSettings(
{
settings: { ui: { customThemes: {} }, mcpServers: {}, agents: {} },
originalSettings: {
ui: { customThemes: {} },
mcpServers: {},
agents: {},
},
path: '/system/settings.json',
},
{
settings: {},
originalSettings: {},
path: '/system/system-defaults.json',
},
{
settings: {
ui: { customThemes: {} },
mcpServers: {},
agents: { overrides: {} },
...userSettings,
},
originalSettings: {
ui: { customThemes: {} },
mcpServers: {},
agents: { overrides: {} },
...userSettings,
},
path: '/user/settings.json',
},
{
settings: {
ui: { customThemes: {} },
mcpServers: {},
agents: { overrides: {} },
...workspaceSettings,
},
originalSettings: {
ui: { customThemes: {} },
mcpServers: {},
agents: { overrides: {} },
...workspaceSettings,
},
path: '/workspace/settings.json',
},
true,
[],
);
// Mock setValue
settings.setValue = vi.fn();
return settings;
};
const createMockAgentDefinition = (
overrides: Partial<AgentDefinition> = {},
): AgentDefinition =>
({
name: 'test-agent',
displayName: 'Test Agent',
description: 'A test agent for testing',
kind: 'local',
modelConfig: {
model: 'inherit',
generateContentConfig: {
temperature: 1.0,
},
},
runConfig: {
maxTimeMinutes: 5,
maxTurns: 10,
},
experimental: false,
...overrides,
}) as AgentDefinition;
describe('AgentConfigDialog', () => {
let mockOnClose: ReturnType<typeof vi.fn>;
let mockOnSave: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.clearAllMocks();
mockOnClose = vi.fn();
mockOnSave = vi.fn();
});
const renderDialog = async (
settings: LoadedSettings,
definition: AgentDefinition = createMockAgentDefinition(),
) => {
const result = await renderWithProviders(
<AgentConfigDialog
agentName="test-agent"
displayName="Test Agent"
definition={definition}
settings={settings}
onClose={mockOnClose}
onSave={mockOnSave}
/>,
{ settings, uiState: { mainAreaWidth: 100 } },
);
return result;
};
describe('rendering', () => {
it('should render the dialog with title', async () => {
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings);
expect(lastFrame()).toContain('Configure: Test Agent');
unmount();
});
it('should render all configuration fields', async () => {
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings);
const frame = lastFrame();
expect(frame).toContain('Enabled');
expect(frame).toContain('Model');
expect(frame).toContain('Temperature');
expect(frame).toContain('Top P');
expect(frame).toContain('Top K');
expect(frame).toContain('Max Output Tokens');
expect(frame).toContain('Max Time (minutes)');
expect(frame).toContain('Max Turns');
unmount();
});
it('should render scope selector', async () => {
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings);
expect(lastFrame()).toContain('Apply To');
expect(lastFrame()).toContain('User Settings');
expect(lastFrame()).toContain('Workspace Settings');
unmount();
});
it('should render help text', async () => {
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings);
expect(lastFrame()).toContain('Use Enter to select');
expect(lastFrame()).toContain('Tab to change focus');
expect(lastFrame()).toContain('Esc to close');
unmount();
});
});
describe('keyboard navigation', () => {
it('should close dialog on Escape', async () => {
const settings = createMockSettings();
const { stdin, waitUntilReady, unmount } = await renderDialog(settings);
await act(async () => {
stdin.write(TerminalKeys.ESCAPE);
});
// Escape key has a 50ms timeout in KeypressContext, so we need to wrap waitUntilReady in act
await act(async () => {
await waitUntilReady();
});
await waitFor(() => {
expect(mockOnClose).toHaveBeenCalled();
});
unmount();
});
it('should navigate down with arrow key', async () => {
const settings = createMockSettings();
const { lastFrame, stdin, waitUntilReady, unmount } =
await renderDialog(settings);
// Initially first item (Enabled) should be active
expect(lastFrame()).toContain('●');
// Press down arrow
await act(async () => {
stdin.write(TerminalKeys.DOWN_ARROW);
});
await waitUntilReady();
await waitFor(() => {
// Model field should now be highlighted
expect(lastFrame()).toContain('Model');
});
unmount();
});
it('should switch focus with Tab', async () => {
const settings = createMockSettings();
const { lastFrame, stdin, waitUntilReady, unmount } =
await renderDialog(settings);
// Initially settings section is focused
expect(lastFrame()).toContain('> Configure: Test Agent');
// Press Tab to switch to scope selector
await act(async () => {
stdin.write(TerminalKeys.TAB);
});
await waitUntilReady();
await waitFor(() => {
expect(lastFrame()).toContain('> Apply To');
});
unmount();
});
});
describe('boolean toggle', () => {
it('should toggle enabled field on Enter', async () => {
const settings = createMockSettings();
const { stdin, waitUntilReady, unmount } = await renderDialog(settings);
// Press Enter to toggle the first field (Enabled)
await act(async () => {
stdin.write(TerminalKeys.ENTER);
});
await waitUntilReady();
await waitFor(() => {
expect(settings.setValue).toHaveBeenCalledWith(
SettingScope.User,
'agents.overrides.test-agent.enabled',
false, // Toggles from true (default) to false
);
expect(mockOnSave).toHaveBeenCalled();
});
unmount();
});
});
describe('default values', () => {
it('should show values from agent definition as defaults', async () => {
const definition = createMockAgentDefinition({
modelConfig: {
model: 'gemini-2.0-flash',
generateContentConfig: {
temperature: 0.7,
},
},
runConfig: {
maxTimeMinutes: 10,
maxTurns: 20,
},
});
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings, definition);
const frame = lastFrame();
expect(frame).toContain('gemini-2.0-flash');
expect(frame).toContain('0.7');
expect(frame).toContain('10');
expect(frame).toContain('20');
unmount();
});
it('should show experimental agents as disabled by default', async () => {
const definition = createMockAgentDefinition({
experimental: true,
});
const settings = createMockSettings();
const { lastFrame, unmount } = await renderDialog(settings, definition);
// Experimental agents default to disabled
expect(lastFrame()).toContain('false');
unmount();
});
});
describe('existing overrides', () => {
it('should show existing override values with * indicator', async () => {
const settings = createMockSettings({
agents: {
overrides: {
'test-agent': {
enabled: false,
modelConfig: {
model: 'custom-model',
},
},
},
},
});
const { lastFrame, unmount } = await renderDialog(settings);
const frame = lastFrame();
// Should show the overridden values
expect(frame).toContain('custom-model');
expect(frame).toContain('false');
unmount();
});
it('should respond to availableTerminalHeight and truncate list', async () => {
const settings = createMockSettings();
// Agent config has about 6 base items + 2 per tool
// Render with very small height (20)
const { lastFrame, unmount } = await renderWithProviders(
<AgentConfigDialog
agentName="test-agent"
displayName="Test Agent"
definition={createMockAgentDefinition()}
settings={settings}
onClose={mockOnClose}
onSave={mockOnSave}
availableTerminalHeight={20}
/>,
{ settings, uiState: { mainAreaWidth: 100 } },
);
await waitFor(() =>
expect(lastFrame()).toContain('Configure: Test Agent'),
);
const frame = lastFrame();
// At height 20, it should be heavily truncated and show '▼'
expect(frame).toContain('▼');
unmount();
});
});
});
|