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
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Config, createServerConfig, ConfigParameters } from './config.js';
import * as path from 'path';
import { setGeminiMdFilename as mockSetGeminiMdFilename } from '../tools/memoryTool.js';
// Mock dependencies that might be called during Config construction or createServerConfig
vi.mock('../tools/tool-registry', () => {
const ToolRegistryMock = vi.fn();
ToolRegistryMock.prototype.registerTool = vi.fn();
ToolRegistryMock.prototype.discoverTools = vi.fn();
ToolRegistryMock.prototype.getAllTools = vi.fn(() => []); // Mock methods if needed
ToolRegistryMock.prototype.getTool = vi.fn();
ToolRegistryMock.prototype.getFunctionDeclarations = vi.fn(() => []);
return { ToolRegistry: ToolRegistryMock };
});
// Mock individual tools if their constructors are complex or have side effects
vi.mock('../tools/ls');
vi.mock('../tools/read-file');
vi.mock('../tools/grep');
vi.mock('../tools/glob');
vi.mock('../tools/edit');
vi.mock('../tools/shell');
vi.mock('../tools/write-file');
vi.mock('../tools/web-fetch');
vi.mock('../tools/read-many-files');
vi.mock('../tools/memoryTool', () => ({
MemoryTool: vi.fn(),
setGeminiMdFilename: vi.fn(),
getCurrentGeminiMdFilename: vi.fn(() => 'GEMINI.md'), // Mock the original filename
DEFAULT_CONTEXT_FILENAME: 'GEMINI.md',
GEMINI_CONFIG_DIR: '.gemini',
}));
describe('Server Config (config.ts)', () => {
const API_KEY = 'server-api-key';
const MODEL = 'gemini-pro';
const SANDBOX = false;
const TARGET_DIR = '/path/to/target';
const DEBUG_MODE = false;
const QUESTION = 'test question';
const FULL_CONTEXT = false;
const USER_AGENT = 'ServerTestAgent/1.0';
const USER_MEMORY = 'Test User Memory';
const TELEMETRY = false;
const baseParams: ConfigParameters = {
apiKey: API_KEY,
model: MODEL,
sandbox: SANDBOX,
targetDir: TARGET_DIR,
debugMode: DEBUG_MODE,
question: QUESTION,
fullContext: FULL_CONTEXT,
userAgent: USER_AGENT,
userMemory: USER_MEMORY,
telemetry: TELEMETRY,
};
beforeEach(() => {
// Reset mocks if necessary
vi.clearAllMocks();
});
it('Config constructor should store userMemory correctly', () => {
const config = new Config(baseParams);
expect(config.getUserMemory()).toBe(USER_MEMORY);
// Verify other getters if needed
expect(config.getApiKey()).toBe(API_KEY);
expect(config.getModel()).toBe(MODEL);
expect(config.getTargetDir()).toBe(path.resolve(TARGET_DIR)); // Check resolved path
expect(config.getUserAgent()).toBe(USER_AGENT);
});
it('Config constructor should default userMemory to empty string if not provided', () => {
const paramsWithoutMemory: ConfigParameters = { ...baseParams };
delete paramsWithoutMemory.userMemory;
const config = new Config(paramsWithoutMemory);
expect(config.getUserMemory()).toBe('');
});
it('createServerConfig should pass userMemory to Config constructor', () => {
const config = createServerConfig(baseParams);
// Check the result of the factory function
expect(config).toBeInstanceOf(Config);
expect(config.getUserMemory()).toBe(USER_MEMORY);
expect(config.getApiKey()).toBe(API_KEY);
expect(config.getUserAgent()).toBe(USER_AGENT);
});
it('createServerConfig should default userMemory if omitted', () => {
const paramsWithoutMemory: ConfigParameters = { ...baseParams };
delete paramsWithoutMemory.userMemory;
const config = createServerConfig(paramsWithoutMemory);
expect(config).toBeInstanceOf(Config);
expect(config.getUserMemory()).toBe(''); // Should default to empty string
});
it('createServerConfig should resolve targetDir', () => {
const relativeDir = './relative/path';
const expectedResolvedDir = path.resolve(relativeDir);
const paramsWithRelativeDir: ConfigParameters = {
...baseParams,
targetDir: relativeDir,
};
const config = createServerConfig(paramsWithRelativeDir);
expect(config.getTargetDir()).toBe(expectedResolvedDir);
});
it('createServerConfig should call setGeminiMdFilename with contextFileName if provided', () => {
const contextFileName = 'CUSTOM_AGENTS.md';
const paramsWithContextFile: ConfigParameters = {
...baseParams,
contextFileName,
};
createServerConfig(paramsWithContextFile);
expect(mockSetGeminiMdFilename).toHaveBeenCalledWith(contextFileName);
});
it('createServerConfig should not call setGeminiMdFilename if contextFileName is not provided', () => {
createServerConfig(baseParams); // baseParams does not have contextFileName
expect(mockSetGeminiMdFilename).not.toHaveBeenCalled();
});
it('Config constructor should call setGeminiMdFilename with contextFileName if provided', () => {
const contextFileName = 'CUSTOM_AGENTS.md';
const paramsWithContextFile: ConfigParameters = {
...baseParams,
contextFileName,
};
new Config(paramsWithContextFile);
expect(mockSetGeminiMdFilename).toHaveBeenCalledWith(contextFileName);
});
it('Config constructor should not call setGeminiMdFilename if contextFileName is not provided', () => {
new Config(baseParams); // baseParams does not have contextFileName
expect(mockSetGeminiMdFilename).not.toHaveBeenCalled();
});
it('should set default file filtering settings when not provided', () => {
const config = new Config(baseParams);
expect(config.getFileFilteringRespectGitIgnore()).toBe(true);
expect(config.getFileFilteringAllowBuildArtifacts()).toBe(false);
});
it('should set custom file filtering settings when provided', () => {
const paramsWithFileFiltering: ConfigParameters = {
...baseParams,
fileFilteringRespectGitIgnore: false,
fileFilteringAllowBuildArtifacts: true,
};
const config = new Config(paramsWithFileFiltering);
expect(config.getFileFilteringRespectGitIgnore()).toBe(false);
expect(config.getFileFilteringAllowBuildArtifacts()).toBe(true);
});
it('Config constructor should set telemetry to true when provided as true', () => {
const paramsWithTelemetry: ConfigParameters = {
...baseParams,
telemetry: true,
};
const config = new Config(paramsWithTelemetry);
expect(config.getTelemetryEnabled()).toBe(true);
});
it('Config constructor should set telemetry to false when provided as false', () => {
const paramsWithTelemetry: ConfigParameters = {
...baseParams,
telemetry: false,
};
const config = new Config(paramsWithTelemetry);
expect(config.getTelemetryEnabled()).toBe(false);
});
it('Config constructor should default telemetry to default value if not provided', () => {
const paramsWithoutTelemetry: ConfigParameters = { ...baseParams };
delete paramsWithoutTelemetry.telemetry;
const config = new Config(paramsWithoutTelemetry);
expect(config.getTelemetryEnabled()).toBe(TELEMETRY);
});
it('createServerConfig should pass telemetry to Config constructor when true', () => {
const paramsWithTelemetry: ConfigParameters = {
...baseParams,
telemetry: true,
};
const config = createServerConfig(paramsWithTelemetry);
expect(config.getTelemetryEnabled()).toBe(true);
});
it('createServerConfig should pass telemetry to Config constructor when false', () => {
const paramsWithTelemetry: ConfigParameters = {
...baseParams,
telemetry: false,
};
const config = createServerConfig(paramsWithTelemetry);
expect(config.getTelemetryEnabled()).toBe(false);
});
it('createServerConfig should default telemetry (to false via Config constructor) if omitted', () => {
const paramsWithoutTelemetry: ConfigParameters = { ...baseParams };
delete paramsWithoutTelemetry.telemetry;
const config = createServerConfig(paramsWithoutTelemetry);
expect(config.getTelemetryEnabled()).toBe(TELEMETRY);
});
it('should have a getFileService method that returns FileDiscoveryService', async () => {
const config = new Config(baseParams);
const fileService = await config.getFileService();
expect(fileService).toBeDefined();
});
});
|