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
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
import { handleAtCommand } from './atCommandProcessor.js';
import { Config, ToolResult } from '@gemini-code/server';
import { ToolCallStatus } from '../types.js'; // Adjusted import
import { /* PartListUnion, */ Part } from '@google/genai'; // Removed PartListUnion
import { UseHistoryManagerReturn } from './useHistoryManager.js';
import * as fsPromises from 'fs/promises'; // Import for mocking stat
import type { Stats } from 'fs'; // Import Stats type for mocking
// Mock Config and ToolRegistry
const mockGetToolRegistry = vi.fn();
const mockGetTargetDir = vi.fn();
const mockConfig = {
getToolRegistry: mockGetToolRegistry,
getTargetDir: mockGetTargetDir,
} as unknown as Config;
// Mock read_many_files tool
const mockReadManyFilesExecute = vi.fn();
const mockReadManyFilesTool = {
name: 'read_many_files',
displayName: 'Read Many Files',
description: 'Reads multiple files.',
execute: mockReadManyFilesExecute,
getDescription: vi.fn((params) => `Read files: ${params.paths.join(', ')}`),
};
// Mock addItem from useHistoryManager
const mockAddItem: Mock<UseHistoryManagerReturn['addItem']> = vi.fn();
const mockOnDebugMessage: Mock<(message: string) => void> = vi.fn();
vi.mock('fs/promises', async () => {
const actual = await vi.importActual('fs/promises');
return {
...actual,
stat: vi.fn(), // Mock stat here
};
});
describe('handleAtCommand', () => {
let abortController: AbortController;
beforeEach(() => {
vi.resetAllMocks();
abortController = new AbortController();
mockGetTargetDir.mockReturnValue('/test/dir');
mockGetToolRegistry.mockReturnValue({
getTool: vi.fn((toolName: string) => {
if (toolName === 'read_many_files') {
return mockReadManyFilesTool;
}
return undefined;
}),
});
// Default mock for fs.stat if not overridden by a specific test
vi.mocked(fsPromises.stat).mockResolvedValue({
isDirectory: () => false,
} as unknown as Stats);
});
afterEach(() => {
abortController.abort(); // Ensure any pending operations are cancelled
});
it('should pass through query if no @ command is present', async () => {
const query = 'regular user query';
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 123,
signal: abortController.signal,
});
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: query },
123,
);
expect(result.processedQuery).toEqual([{ text: query }]);
expect(result.shouldProceed).toBe(true);
expect(mockReadManyFilesExecute).not.toHaveBeenCalled();
});
it('should pass through query if only a lone @ symbol is present', async () => {
const queryWithSpaces = ' @ ';
// const trimmedQuery = queryWithSpaces.trim(); // Not needed for addItem expectation here
const result = await handleAtCommand({
query: queryWithSpaces, // Pass the version with spaces to the function
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 124,
signal: abortController.signal,
});
// For a lone '@', addItem is called with the *original untrimmed* query
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: queryWithSpaces },
124,
);
// processedQuery should also be the original untrimmed version for lone @
expect(result.processedQuery).toEqual([{ text: queryWithSpaces }]);
expect(result.shouldProceed).toBe(true);
expect(mockOnDebugMessage).toHaveBeenCalledWith(
'Lone @ detected, passing directly to LLM.',
);
});
it('should process a valid text file path', async () => {
const filePath = 'path/to/file.txt';
const query = `@${filePath}`;
const fileContent = 'This is the file content.';
mockReadManyFilesExecute.mockResolvedValue({
llmContent: fileContent,
returnDisplay: 'Read 1 file.',
} as ToolResult);
// fs.stat will use the default mock (isDirectory: false)
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 125,
signal: abortController.signal,
});
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: query },
125,
);
expect(mockReadManyFilesExecute).toHaveBeenCalledWith(
{ paths: [filePath] },
abortController.signal,
);
expect(mockAddItem).toHaveBeenCalledWith(
expect.objectContaining({
type: 'tool_group',
tools: expect.arrayContaining([
expect.objectContaining({
name: 'Read Many Files',
status: ToolCallStatus.Success,
resultDisplay: 'Read 1 file.',
}),
]),
}),
125,
);
expect(result.processedQuery).toEqual([
'\n--- Content from: ${contentLabel} ---\n',
fileContent,
'\n--- End of content ---\n',
]);
expect(result.shouldProceed).toBe(true);
});
it('should process a valid directory path and convert to glob', async () => {
const dirPath = 'path/to/dir';
const query = `@${dirPath}`;
const dirContent = [
'Content of file 1.',
'Content of file 2.',
'Content of file 3.',
];
vi.mocked(fsPromises.stat).mockResolvedValue({
isDirectory: () => true,
} as unknown as Stats);
mockReadManyFilesExecute.mockResolvedValue({
llmContent: dirContent,
returnDisplay: 'Read directory contents.',
} as ToolResult);
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 126,
signal: abortController.signal,
});
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: query },
126,
);
expect(mockReadManyFilesExecute).toHaveBeenCalledWith(
{ paths: [`${dirPath}/**`] }, // Expect glob pattern
abortController.signal,
);
expect(mockOnDebugMessage).toHaveBeenCalledWith(
`Path resolved to directory, using glob: ${dirPath}/**`,
);
expect(mockAddItem).toHaveBeenCalledWith(
expect.objectContaining({ type: 'tool_group' }),
126,
);
expect(result.processedQuery).toEqual([
'\n--- Content from: ${contentLabel} ---\n',
...dirContent,
'\n--- End of content ---\n',
]);
expect(result.shouldProceed).toBe(true);
});
it('should process a valid image file path', async () => {
const imagePath = 'path/to/image.png';
const query = `@${imagePath}`;
const imageData: Part = {
inlineData: { mimeType: 'image/png', data: 'base64imagedata' },
};
mockReadManyFilesExecute.mockResolvedValue({
llmContent: [imageData],
returnDisplay: 'Read 1 image.',
} as ToolResult);
// fs.stat will use the default mock (isDirectory: false)
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 127,
signal: abortController.signal,
});
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: query },
127,
);
expect(mockReadManyFilesExecute).toHaveBeenCalledWith(
{ paths: [imagePath] },
abortController.signal,
);
expect(mockAddItem).toHaveBeenCalledWith(
expect.objectContaining({
type: 'tool_group',
tools: expect.arrayContaining([
expect.objectContaining({
name: 'Read Many Files',
status: ToolCallStatus.Success,
resultDisplay: 'Read 1 image.',
}),
]),
}),
127,
);
expect(result.processedQuery).toEqual([
'\n--- Content from: ${contentLabel} ---\n',
imageData,
'\n--- End of content ---\n',
]);
expect(result.shouldProceed).toBe(true);
});
it('should handle query with text before and after @command', async () => {
const textBefore = 'Explain this:';
const filePath = 'doc.md';
const textAfter = 'in detail.';
const query = `${textBefore} @${filePath} ${textAfter}`;
const fileContent = 'Markdown content.';
mockReadManyFilesExecute.mockResolvedValue({
llmContent: fileContent,
returnDisplay: 'Read 1 doc.',
} as ToolResult);
// fs.stat will use the default mock (isDirectory: false)
const result = await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 128,
signal: abortController.signal,
});
expect(mockAddItem).toHaveBeenCalledWith(
{ type: 'user', text: query }, // Expect original query for addItem
128,
);
expect(result.processedQuery).toEqual([
{ text: textBefore },
'\n--- Content from: ${contentLabel} ---\n',
fileContent,
'\n--- End of content ---\n',
{ text: textAfter },
]);
expect(result.shouldProceed).toBe(true);
});
it('should correctly unescape paths with escaped spaces', async () => {
const rawPath = 'path/to/my\\ file.txt';
const unescapedPath = 'path/to/my file.txt';
const query = `@${rawPath}`;
const fileContent = 'Content of file with space.';
mockReadManyFilesExecute.mockResolvedValue({
llmContent: fileContent,
returnDisplay: 'Read 1 file.',
} as ToolResult);
// fs.stat will use the default mock (isDirectory: false)
await handleAtCommand({
query,
config: mockConfig,
addItem: mockAddItem,
onDebugMessage: mockOnDebugMessage,
messageId: 129,
signal: abortController.signal,
});
expect(mockReadManyFilesExecute).toHaveBeenCalledWith(
{ paths: [unescapedPath] }, // Expect unescaped path
abortController.signal,
);
});
});
|