Files
gemini-cli/packages/core/src/utils/environmentContext.test.ts
T
Taylor Mullen ec1dd39513 chore(core): comprehensive system prompt overhaul and optimization
- Unified communication protocols by merging 'Critical Transparency' and 'Explain Before Acting' into a single, unconditional mandate.
- Prioritized core execution lifecycles by moving Development and New Application workflows to the top of the prompt.
- Standardized document hierarchy and visual style using consistent H1 headers and bulleted label patterns.
- Hardened 'Operational Efficiency' protocols with mandatory log redirection for high-volume commands and added a Tip for combined redirection/analysis tool calls.
- Mandated the activation of specialized Agent Skills when a task aligns with their description to prioritize high-fidelity workflows.
- Refined 'Engineering Standards' and 'Workflow: Development' to mandate idiomatic completeness in surgical changes, ensuring quality is never sacrificed for tool-call efficiency.
- Improved technical documentation consistency by unifying tool references with backticks and standardizing vertical rhythm.

Fixes https://github.com/google-gemini/gemini-cli/issues/sys.prompt.overhaul
2026-01-20 13:39:17 -08:00

170 lines
5.2 KiB
TypeScript

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
type Mock,
} from 'vitest';
import {
getEnvironmentContext,
getDirectoryContextString,
} from './environmentContext.js';
import type { Config } from '../config/config.js';
import type { Storage } from '../config/storage.js';
import { getFolderStructure } from './getFolderStructure.js';
vi.mock('../config/config.js');
vi.mock('./getFolderStructure.js', () => ({
getFolderStructure: vi.fn(),
}));
vi.mock('../tools/read-many-files.js');
describe('getDirectoryContextString', () => {
let mockConfig: Partial<Config>;
beforeEach(() => {
mockConfig = {
getWorkspaceContext: vi.fn().mockReturnValue({
getDirectories: vi.fn().mockReturnValue(['/test/dir']),
}),
getFileService: vi.fn(),
storage: {
getProjectTempDir: vi.fn().mockReturnValue('/tmp/project-temp'),
} as unknown as Storage,
};
vi.mocked(getFolderStructure).mockResolvedValue('Mock Folder Structure');
});
afterEach(() => {
vi.resetAllMocks();
});
it('should return context string for a single directory', async () => {
const contextString = await getDirectoryContextString(mockConfig as Config);
expect(contextString).toContain('- **Workspace Directories:**');
expect(contextString).toContain(' - /test/dir');
expect(contextString).toContain(
'- **Directory Structure:**\n\nMock Folder Structure',
);
});
it('should return context string for multiple directories', async () => {
(
vi.mocked(mockConfig.getWorkspaceContext!)().getDirectories as Mock
).mockReturnValue(['/test/dir1', '/test/dir2']);
vi.mocked(getFolderStructure)
.mockResolvedValueOnce('Structure 1')
.mockResolvedValueOnce('Structure 2');
const contextString = await getDirectoryContextString(mockConfig as Config);
expect(contextString).toContain('- **Workspace Directories:**');
expect(contextString).toContain(' - /test/dir1');
expect(contextString).toContain(' - /test/dir2');
expect(contextString).toContain(
'- **Directory Structure:**\n\nStructure 1\nStructure 2',
);
});
});
describe('getEnvironmentContext', () => {
let mockConfig: Partial<Config>;
let mockToolRegistry: { getTool: Mock };
beforeEach(() => {
mockToolRegistry = {
getTool: vi.fn(),
};
mockConfig = {
getWorkspaceContext: vi.fn().mockReturnValue({
getDirectories: vi.fn().mockReturnValue(['/test/dir']),
}),
getFileService: vi.fn(),
getEnvironmentMemory: vi.fn().mockReturnValue('Mock Environment Memory'),
getToolRegistry: vi.fn().mockReturnValue(mockToolRegistry),
storage: {
getProjectTempDir: vi.fn().mockReturnValue('/tmp/project-temp'),
} as unknown as Storage,
};
vi.mocked(getFolderStructure).mockResolvedValue('Mock Folder Structure');
});
afterEach(() => {
vi.resetAllMocks();
});
it('should return basic environment context for a single directory', async () => {
const parts = await getEnvironmentContext(mockConfig as Config);
expect(parts.length).toBe(1);
const context = parts[0].text;
expect(context).toContain('<session_context>');
expect(context).toContain('- **Workspace Directories:**');
expect(context).toContain(' - /test/dir');
expect(context).toContain(
'- **Directory Structure:**\n\nMock Folder Structure',
);
expect(context).toContain('Mock Environment Memory');
expect(context).toContain('</session_context>');
expect(getFolderStructure).toHaveBeenCalledWith('/test/dir', {
fileService: undefined,
});
});
it('should return basic environment context for multiple directories', async () => {
(
vi.mocked(mockConfig.getWorkspaceContext!)().getDirectories as Mock
).mockReturnValue(['/test/dir1', '/test/dir2']);
vi.mocked(getFolderStructure)
.mockResolvedValueOnce('Structure 1')
.mockResolvedValueOnce('Structure 2');
const parts = await getEnvironmentContext(mockConfig as Config);
expect(parts.length).toBe(1);
const context = parts[0].text;
expect(context).toContain('<session_context>');
expect(context).toContain('- **Workspace Directories:**');
expect(context).toContain(' - /test/dir1');
expect(context).toContain(' - /test/dir2');
expect(context).toContain(
'- **Directory Structure:**\n\nStructure 1\nStructure 2',
);
expect(context).toContain('</session_context>');
expect(getFolderStructure).toHaveBeenCalledTimes(2);
});
it('should handle read_many_files returning no content', async () => {
const mockReadManyFilesTool = {
build: vi.fn().mockReturnValue({
execute: vi.fn().mockResolvedValue({ llmContent: '' }),
}),
};
mockToolRegistry.getTool.mockReturnValue(mockReadManyFilesTool);
const parts = await getEnvironmentContext(mockConfig as Config);
expect(parts.length).toBe(1); // No extra part added
});
it('should handle read_many_files tool not being found', async () => {
mockToolRegistry.getTool.mockReturnValue(null);
const parts = await getEnvironmentContext(mockConfig as Config);
expect(parts.length).toBe(1); // No extra part added
});
});