File size: 4,964 Bytes
84aa3bf | 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 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi } from 'vitest';
import {
SettingScope,
type LoadedSettings,
type LoadableSettingScope,
} from '../config/settings.js';
import { enableAgent, disableAgent } from './agentSettings.js';
function createMockLoadedSettings(opts: {
userSettings?: Record<string, unknown>;
workspaceSettings?: Record<string, unknown>;
userPath?: string;
workspacePath?: string;
}): LoadedSettings {
const scopes: Record<
string,
{
settings: Record<string, unknown>;
originalSettings: Record<string, unknown>;
path: string;
}
> = {
[SettingScope.User]: {
settings: opts.userSettings ?? {},
originalSettings: opts.userSettings ?? {},
path: opts.userPath ?? '/home/user/.gemini/settings.json',
},
[SettingScope.Workspace]: {
settings: opts.workspaceSettings ?? {},
originalSettings: opts.workspaceSettings ?? {},
path: opts.workspacePath ?? '/project/.gemini/settings.json',
},
};
return {
forScope: vi.fn((scope: LoadableSettingScope) => scopes[scope]),
setValue: vi.fn(),
} as unknown as LoadedSettings;
}
describe('agentSettings', () => {
describe('agentStrategy (via enableAgent / disableAgent)', () => {
describe('enableAgent', () => {
it('should return no-op when the agent is already enabled in both scopes', () => {
const settings = createMockLoadedSettings({
userSettings: {
agents: { overrides: { 'my-agent': { enabled: true } } },
},
workspaceSettings: {
agents: { overrides: { 'my-agent': { enabled: true } } },
},
});
const result = enableAgent(settings, 'my-agent');
expect(result.status).toBe('no-op');
expect(result.action).toBe('enable');
expect(result.agentName).toBe('my-agent');
expect(result.modifiedScopes).toHaveLength(0);
expect(settings.setValue).not.toHaveBeenCalled();
});
it('should enable the agent when not present in any scope', () => {
const settings = createMockLoadedSettings({
userSettings: {},
workspaceSettings: {},
});
const result = enableAgent(settings, 'my-agent');
expect(result.status).toBe('success');
expect(result.action).toBe('enable');
expect(result.agentName).toBe('my-agent');
expect(result.modifiedScopes).toHaveLength(2);
expect(settings.setValue).toHaveBeenCalledTimes(2);
});
it('should enable the agent only in the scope where it is not enabled', () => {
const settings = createMockLoadedSettings({
userSettings: {
agents: { overrides: { 'my-agent': { enabled: true } } },
},
workspaceSettings: {
agents: { overrides: { 'my-agent': { enabled: false } } },
},
});
const result = enableAgent(settings, 'my-agent');
expect(result.status).toBe('success');
expect(result.modifiedScopes).toHaveLength(1);
expect(result.modifiedScopes[0].scope).toBe(SettingScope.Workspace);
expect(result.alreadyInStateScopes).toHaveLength(1);
expect(result.alreadyInStateScopes[0].scope).toBe(SettingScope.User);
expect(settings.setValue).toHaveBeenCalledTimes(1);
});
});
describe('disableAgent', () => {
it('should return no-op when agent is already explicitly disabled', () => {
const settings = createMockLoadedSettings({
userSettings: {
agents: { overrides: { 'my-agent': { enabled: false } } },
},
});
const result = disableAgent(settings, 'my-agent', SettingScope.User);
expect(result.status).toBe('no-op');
expect(result.action).toBe('disable');
expect(result.agentName).toBe('my-agent');
expect(settings.setValue).not.toHaveBeenCalled();
});
it('should disable the agent when it is currently enabled', () => {
const settings = createMockLoadedSettings({
userSettings: {
agents: { overrides: { 'my-agent': { enabled: true } } },
},
});
const result = disableAgent(settings, 'my-agent', SettingScope.User);
expect(result.status).toBe('success');
expect(result.action).toBe('disable');
expect(result.modifiedScopes).toHaveLength(1);
expect(result.modifiedScopes[0].scope).toBe(SettingScope.User);
expect(settings.setValue).toHaveBeenCalledTimes(1);
});
it('should return error for an invalid scope', () => {
const settings = createMockLoadedSettings({});
const result = disableAgent(settings, 'my-agent', SettingScope.Session);
expect(result.status).toBe('error');
expect(result.error).toContain('Invalid settings scope');
});
});
});
});
|