File size: 8,920 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 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as path from 'node:path';
import {
resolvePathFromEnv,
isSectionEnabled,
applySubstitutions,
} from './utils.js';
import type { Config } from '../config/config.js';
import type { ToolRegistry } from '../tools/tool-registry.js';
vi.mock('../utils/paths.js', () => ({
homedir: vi.fn().mockReturnValue('/mock/home'),
}));
vi.mock('../utils/debugLogger.js', () => ({
debugLogger: {
warn: vi.fn(),
},
}));
vi.mock('./snippets.js', () => ({
renderSubAgents: vi.fn().mockReturnValue('mocked-sub-agents'),
}));
vi.mock('./snippets.legacy.js', () => ({
renderSubAgents: vi.fn().mockReturnValue('mocked-legacy-sub-agents'),
}));
describe('resolvePathFromEnv', () => {
it('should return default values for undefined input', () => {
const result = resolvePathFromEnv(undefined);
expect(result).toEqual({
isSwitch: false,
value: null,
isDisabled: false,
});
});
it('should return default values for empty string input', () => {
const result = resolvePathFromEnv('');
expect(result).toEqual({
isSwitch: false,
value: null,
isDisabled: false,
});
});
it('should return default values for whitespace-only input', () => {
const result = resolvePathFromEnv(' ');
expect(result).toEqual({
isSwitch: false,
value: null,
isDisabled: false,
});
});
it('should recognize "true" as an enabled switch', () => {
const result = resolvePathFromEnv('true');
expect(result).toEqual({
isSwitch: true,
value: 'true',
isDisabled: false,
});
});
it('should recognize "1" as an enabled switch', () => {
const result = resolvePathFromEnv('1');
expect(result).toEqual({
isSwitch: true,
value: '1',
isDisabled: false,
});
});
it('should recognize "false" as a disabled switch', () => {
const result = resolvePathFromEnv('false');
expect(result).toEqual({
isSwitch: true,
value: 'false',
isDisabled: true,
});
});
it('should recognize "0" as a disabled switch', () => {
const result = resolvePathFromEnv('0');
expect(result).toEqual({
isSwitch: true,
value: '0',
isDisabled: true,
});
});
it('should handle case-insensitive switch values', () => {
const result = resolvePathFromEnv('TRUE');
expect(result).toEqual({
isSwitch: true,
value: 'true',
isDisabled: false,
});
});
it('should handle case-insensitive FALSE', () => {
const result = resolvePathFromEnv('FALSE');
expect(result).toEqual({
isSwitch: true,
value: 'false',
isDisabled: true,
});
});
it('should trim whitespace before evaluating switch values', () => {
const result = resolvePathFromEnv(' true ');
expect(result).toEqual({
isSwitch: true,
value: 'true',
isDisabled: false,
});
});
it('should resolve a regular path', () => {
const p = path.resolve('/some/absolute/path');
const result = resolvePathFromEnv(p);
expect(result.isSwitch).toBe(false);
expect(result.value).toBe(p);
expect(result.isDisabled).toBe(false);
});
it('should resolve a tilde path to the home directory', () => {
const result = resolvePathFromEnv('~/my/custom/path');
expect(result.isSwitch).toBe(false);
expect(result.value).toContain(path.normalize('/mock/home'));
expect(result.value).toContain(path.normalize('my/custom/path'));
expect(result.isDisabled).toBe(false);
});
it('should resolve a bare tilde to the home directory', () => {
const result = resolvePathFromEnv('~');
expect(result.isSwitch).toBe(false);
expect(result.value).toBe(path.resolve('/mock/home'));
expect(result.isDisabled).toBe(false);
});
it('should handle home directory resolution failure gracefully', async () => {
const { homedir } = await import('../utils/paths.js');
vi.mocked(homedir).mockImplementationOnce(() => {
throw new Error('No home directory');
});
const result = resolvePathFromEnv('~/some/path');
expect(result).toEqual({
isSwitch: false,
value: null,
isDisabled: false,
});
});
});
describe('isSectionEnabled', () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it('should return true when the env var is not set', () => {
expect(isSectionEnabled('SOME_KEY')).toBe(true);
});
it('should return true when the env var is set to "1"', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', '1');
expect(isSectionEnabled('SOME_KEY')).toBe(true);
});
it('should return true when the env var is set to "true"', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', 'true');
expect(isSectionEnabled('SOME_KEY')).toBe(true);
});
it('should return false when the env var is set to "0"', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', '0');
expect(isSectionEnabled('SOME_KEY')).toBe(false);
});
it('should return false when the env var is set to "false"', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', 'false');
expect(isSectionEnabled('SOME_KEY')).toBe(false);
});
it('should handle case-insensitive key conversion', () => {
vi.stubEnv('GEMINI_PROMPT_MY_SECTION', '0');
expect(isSectionEnabled('my_section')).toBe(false);
});
it('should handle whitespace around the env var value', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', ' false ');
expect(isSectionEnabled('SOME_KEY')).toBe(false);
});
it('should return true for any non-falsy value', () => {
vi.stubEnv('GEMINI_PROMPT_SOME_KEY', 'enabled');
expect(isSectionEnabled('SOME_KEY')).toBe(true);
});
});
describe('applySubstitutions', () => {
let mockConfig: Config;
beforeEach(() => {
mockConfig = {
get config() {
return this;
},
toolRegistry: {
getAllToolNames: vi.fn().mockReturnValue([]),
getAllTools: vi.fn().mockReturnValue([]),
},
getAgentRegistry: vi.fn().mockReturnValue({
getAllDefinitions: vi.fn().mockReturnValue([]),
getDefinition: vi.fn().mockReturnValue(undefined),
}),
getToolRegistry: vi.fn().mockReturnValue({
getAllToolNames: vi.fn().mockReturnValue([]),
}),
} as unknown as Config;
});
it('should replace ${AgentSkills} with the skills prompt', () => {
const result = applySubstitutions(
'Skills: ${AgentSkills}',
mockConfig,
'my-skills-content',
);
expect(result).toBe('Skills: my-skills-content');
});
it('should replace multiple ${AgentSkills} occurrences', () => {
const result = applySubstitutions(
'${AgentSkills} and ${AgentSkills}',
mockConfig,
'skills',
);
expect(result).toBe('skills and skills');
});
it('should replace ${SubAgents} with rendered sub-agents content', () => {
const result = applySubstitutions(
'Agents: ${SubAgents}',
mockConfig,
'',
true,
);
expect(result).toContain('mocked-sub-agents');
});
it('should use legacy snippets when isGemini3 is false', () => {
const result = applySubstitutions(
'Agents: ${SubAgents}',
mockConfig,
'',
false,
);
expect(result).toContain('mocked-legacy-sub-agents');
});
it('should replace ${AvailableTools} with tool names list', () => {
(mockConfig as unknown as { toolRegistry: ToolRegistry }).toolRegistry = {
getAllToolNames: vi.fn().mockReturnValue(['read_file', 'write_file']),
getAllTools: vi.fn().mockReturnValue([]),
} as unknown as ToolRegistry;
const result = applySubstitutions(
'Tools: ${AvailableTools}',
mockConfig,
'',
);
expect(result).toContain('- read_file');
expect(result).toContain('- write_file');
});
it('should show no tools message when no tools available', () => {
const result = applySubstitutions(
'Tools: ${AvailableTools}',
mockConfig,
'',
);
expect(result).toContain('No tools are currently available.');
});
it('should replace tool-specific ${toolName_ToolName} variables', () => {
(mockConfig as unknown as { toolRegistry: ToolRegistry }).toolRegistry = {
getAllToolNames: vi.fn().mockReturnValue(['read_file']),
getAllTools: vi.fn().mockReturnValue([]),
} as unknown as ToolRegistry;
const result = applySubstitutions(
'Use ${read_file_ToolName} to read',
mockConfig,
'',
);
expect(result).toBe('Use read_file to read');
});
it('should handle a prompt with no substitution placeholders', () => {
const result = applySubstitutions(
'A plain prompt with no variables.',
mockConfig,
'',
);
expect(result).toBe('A plain prompt with no variables.');
});
});
|