@google/gemini-cli 0.1.9-nightly.250708.137ffec3 → 0.1.9-nightly.250708.a4097ae6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/google-gemini-cli-0.1.9.tgz +0 -0
  2. package/dist/package.json +2 -2
  3. package/dist/src/config/config.js +0 -2
  4. package/dist/src/config/config.js.map +1 -1
  5. package/dist/src/gemini.test.d.ts +6 -0
  6. package/dist/src/gemini.test.js +122 -0
  7. package/dist/src/gemini.test.js.map +1 -0
  8. package/dist/src/generated/git-commit.d.ts +1 -1
  9. package/dist/src/generated/git-commit.js +1 -1
  10. package/dist/src/test-utils/mockCommandContext.d.ts +18 -0
  11. package/dist/src/test-utils/mockCommandContext.js +75 -0
  12. package/dist/src/test-utils/mockCommandContext.js.map +1 -0
  13. package/dist/src/ui/App.test.d.ts +6 -0
  14. package/dist/src/ui/App.test.js +300 -0
  15. package/dist/src/ui/App.test.js.map +1 -0
  16. package/dist/src/ui/components/AuthDialog.test.d.ts +6 -0
  17. package/dist/src/ui/components/AuthDialog.test.js +86 -0
  18. package/dist/src/ui/components/AuthDialog.test.js.map +1 -0
  19. package/dist/src/ui/components/HistoryItemDisplay.test.d.ts +6 -0
  20. package/dist/src/ui/components/HistoryItemDisplay.test.js +81 -0
  21. package/dist/src/ui/components/HistoryItemDisplay.test.js.map +1 -0
  22. package/dist/src/ui/components/InputPrompt.test.d.ts +6 -0
  23. package/dist/src/ui/components/InputPrompt.test.js +334 -0
  24. package/dist/src/ui/components/InputPrompt.test.js.map +1 -0
  25. package/dist/src/ui/components/LoadingIndicator.js +1 -2
  26. package/dist/src/ui/components/LoadingIndicator.js.map +1 -1
  27. package/dist/src/ui/components/LoadingIndicator.test.d.ts +6 -0
  28. package/dist/src/ui/components/LoadingIndicator.test.js +141 -0
  29. package/dist/src/ui/components/LoadingIndicator.test.js.map +1 -0
  30. package/dist/src/ui/components/ModelStatsDisplay.test.d.ts +6 -0
  31. package/dist/src/ui/components/ModelStatsDisplay.test.js +217 -0
  32. package/dist/src/ui/components/ModelStatsDisplay.test.js.map +1 -0
  33. package/dist/src/ui/components/SessionSummaryDisplay.test.d.ts +6 -0
  34. package/dist/src/ui/components/SessionSummaryDisplay.test.js +60 -0
  35. package/dist/src/ui/components/SessionSummaryDisplay.test.js.map +1 -0
  36. package/dist/src/ui/components/StatsDisplay.test.d.ts +6 -0
  37. package/dist/src/ui/components/StatsDisplay.test.js +275 -0
  38. package/dist/src/ui/components/StatsDisplay.test.js.map +1 -0
  39. package/dist/src/ui/components/ToolStatsDisplay.test.d.ts +6 -0
  40. package/dist/src/ui/components/ToolStatsDisplay.test.js +160 -0
  41. package/dist/src/ui/components/ToolStatsDisplay.test.js.map +1 -0
  42. package/dist/src/ui/components/messages/DiffRenderer.test.d.ts +6 -0
  43. package/dist/src/ui/components/messages/DiffRenderer.test.js +239 -0
  44. package/dist/src/ui/components/messages/DiffRenderer.test.js.map +1 -0
  45. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.d.ts +6 -0
  46. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.js +37 -0
  47. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.js.map +1 -0
  48. package/dist/src/ui/components/messages/ToolMessage.test.d.ts +6 -0
  49. package/dist/src/ui/components/messages/ToolMessage.test.js +116 -0
  50. package/dist/src/ui/components/messages/ToolMessage.test.js.map +1 -0
  51. package/dist/src/ui/components/shared/MaxSizedBox.test.d.ts +6 -0
  52. package/dist/src/ui/components/shared/MaxSizedBox.test.js +134 -0
  53. package/dist/src/ui/components/shared/MaxSizedBox.test.js.map +1 -0
  54. package/dist/src/ui/contexts/SessionContext.test.d.ts +6 -0
  55. package/dist/src/ui/contexts/SessionContext.test.js +96 -0
  56. package/dist/src/ui/contexts/SessionContext.test.js.map +1 -0
  57. package/dist/src/ui/hooks/useGeminiStream.test.d.ts +6 -0
  58. package/dist/src/ui/hooks/useGeminiStream.test.js +838 -0
  59. package/dist/src/ui/hooks/useGeminiStream.test.js.map +1 -0
  60. package/dist/src/ui/utils/MarkdownDisplay.test.d.ts +6 -0
  61. package/dist/src/ui/utils/MarkdownDisplay.test.js +211 -0
  62. package/dist/src/ui/utils/MarkdownDisplay.test.js.map +1 -0
  63. package/dist/tsconfig.tsbuildinfo +1 -1
  64. package/package.json +3 -3
@@ -0,0 +1,838 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ /* eslint-disable @typescript-eslint/no-explicit-any */
7
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
8
+ import { renderHook, act, waitFor } from '@testing-library/react';
9
+ import { useGeminiStream, mergePartListUnions } from './useGeminiStream.js';
10
+ import { useInput } from 'ink';
11
+ import { useReactToolScheduler, } from './useReactToolScheduler.js';
12
+ import { AuthType } from '@google/gemini-cli-core';
13
+ import { MessageType, StreamingState, } from '../types.js';
14
+ // --- MOCKS ---
15
+ const mockSendMessageStream = vi
16
+ .fn()
17
+ .mockReturnValue((async function* () { })());
18
+ const mockStartChat = vi.fn();
19
+ const MockedGeminiClientClass = vi.hoisted(() => vi.fn().mockImplementation(function (_config) {
20
+ // _config
21
+ this.startChat = mockStartChat;
22
+ this.sendMessageStream = mockSendMessageStream;
23
+ this.addHistory = vi.fn();
24
+ }));
25
+ const MockedUserPromptEvent = vi.hoisted(() => vi.fn().mockImplementation(() => { }));
26
+ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
27
+ const actualCoreModule = (await importOriginal());
28
+ return {
29
+ ...actualCoreModule,
30
+ GitService: vi.fn(),
31
+ GeminiClient: MockedGeminiClientClass,
32
+ UserPromptEvent: MockedUserPromptEvent,
33
+ };
34
+ });
35
+ const mockUseReactToolScheduler = useReactToolScheduler;
36
+ vi.mock('./useReactToolScheduler.js', async (importOriginal) => {
37
+ const actualSchedulerModule = (await importOriginal());
38
+ return {
39
+ ...(actualSchedulerModule || {}),
40
+ useReactToolScheduler: vi.fn(),
41
+ };
42
+ });
43
+ vi.mock('ink', async (importOriginal) => {
44
+ const actualInkModule = (await importOriginal());
45
+ return { ...(actualInkModule || {}), useInput: vi.fn() };
46
+ });
47
+ vi.mock('./shellCommandProcessor.js', () => ({
48
+ useShellCommandProcessor: vi.fn().mockReturnValue({
49
+ handleShellCommand: vi.fn(),
50
+ }),
51
+ }));
52
+ vi.mock('./atCommandProcessor.js', () => ({
53
+ handleAtCommand: vi
54
+ .fn()
55
+ .mockResolvedValue({ shouldProceed: true, processedQuery: 'mocked' }),
56
+ }));
57
+ vi.mock('../utils/markdownUtilities.js', () => ({
58
+ findLastSafeSplitPoint: vi.fn((s) => s.length),
59
+ }));
60
+ vi.mock('./useStateAndRef.js', () => ({
61
+ useStateAndRef: vi.fn((initial) => {
62
+ let val = initial;
63
+ const ref = { current: val };
64
+ const setVal = vi.fn((updater) => {
65
+ if (typeof updater === 'function') {
66
+ val = updater(val);
67
+ }
68
+ else {
69
+ val = updater;
70
+ }
71
+ ref.current = val;
72
+ });
73
+ return [ref, setVal];
74
+ }),
75
+ }));
76
+ vi.mock('./useLogger.js', () => ({
77
+ useLogger: vi.fn().mockReturnValue({
78
+ logMessage: vi.fn().mockResolvedValue(undefined),
79
+ }),
80
+ }));
81
+ const mockStartNewTurn = vi.fn();
82
+ const mockAddUsage = vi.fn();
83
+ vi.mock('../contexts/SessionContext.js', () => ({
84
+ useSessionStats: vi.fn(() => ({
85
+ startNewTurn: mockStartNewTurn,
86
+ addUsage: mockAddUsage,
87
+ })),
88
+ }));
89
+ vi.mock('./slashCommandProcessor.js', () => ({
90
+ handleSlashCommand: vi.fn().mockReturnValue(false),
91
+ }));
92
+ const mockParseAndFormatApiError = vi.hoisted(() => vi.fn());
93
+ vi.mock('../utils/errorParsing.js', () => ({
94
+ parseAndFormatApiError: mockParseAndFormatApiError,
95
+ }));
96
+ // --- END MOCKS ---
97
+ describe('mergePartListUnions', () => {
98
+ it('should merge multiple PartListUnion arrays', () => {
99
+ const list1 = [{ text: 'Hello' }];
100
+ const list2 = [
101
+ { inlineData: { mimeType: 'image/png', data: 'abc' } },
102
+ ];
103
+ const list3 = [{ text: 'World' }, { text: '!' }];
104
+ const result = mergePartListUnions([list1, list2, list3]);
105
+ expect(result).toEqual([
106
+ { text: 'Hello' },
107
+ { inlineData: { mimeType: 'image/png', data: 'abc' } },
108
+ { text: 'World' },
109
+ { text: '!' },
110
+ ]);
111
+ });
112
+ it('should handle empty arrays in the input list', () => {
113
+ const list1 = [{ text: 'First' }];
114
+ const list2 = [];
115
+ const list3 = [{ text: 'Last' }];
116
+ const result = mergePartListUnions([list1, list2, list3]);
117
+ expect(result).toEqual([{ text: 'First' }, { text: 'Last' }]);
118
+ });
119
+ it('should handle a single PartListUnion array', () => {
120
+ const list1 = [
121
+ { text: 'One' },
122
+ { inlineData: { mimeType: 'image/jpeg', data: 'xyz' } },
123
+ ];
124
+ const result = mergePartListUnions([list1]);
125
+ expect(result).toEqual(list1);
126
+ });
127
+ it('should return an empty array if all input arrays are empty', () => {
128
+ const list1 = [];
129
+ const list2 = [];
130
+ const result = mergePartListUnions([list1, list2]);
131
+ expect(result).toEqual([]);
132
+ });
133
+ it('should handle input list being empty', () => {
134
+ const result = mergePartListUnions([]);
135
+ expect(result).toEqual([]);
136
+ });
137
+ it('should correctly merge when PartListUnion items are single Parts not in arrays', () => {
138
+ const part1 = { text: 'Single part 1' };
139
+ const part2 = { inlineData: { mimeType: 'image/gif', data: 'gif' } };
140
+ const listContainingSingleParts = [
141
+ part1,
142
+ [part2],
143
+ { text: 'Another single part' },
144
+ ];
145
+ const result = mergePartListUnions(listContainingSingleParts);
146
+ expect(result).toEqual([
147
+ { text: 'Single part 1' },
148
+ { inlineData: { mimeType: 'image/gif', data: 'gif' } },
149
+ { text: 'Another single part' },
150
+ ]);
151
+ });
152
+ it('should handle a mix of arrays and single parts, including empty arrays and undefined/null parts if they were possible (though PartListUnion typing restricts this)', () => {
153
+ const list1 = [{ text: 'A' }];
154
+ const list2 = [];
155
+ const part3 = { text: 'B' };
156
+ const list4 = [
157
+ { text: 'C' },
158
+ { inlineData: { mimeType: 'text/plain', data: 'D' } },
159
+ ];
160
+ const result = mergePartListUnions([list1, list2, part3, list4]);
161
+ expect(result).toEqual([
162
+ { text: 'A' },
163
+ { text: 'B' },
164
+ { text: 'C' },
165
+ { inlineData: { mimeType: 'text/plain', data: 'D' } },
166
+ ]);
167
+ });
168
+ it('should preserve the order of parts from the input arrays', () => {
169
+ const listA = [{ text: '1' }, { text: '2' }];
170
+ const listB = [{ text: '3' }];
171
+ const listC = [{ text: '4' }, { text: '5' }];
172
+ const result = mergePartListUnions([listA, listB, listC]);
173
+ expect(result).toEqual([
174
+ { text: '1' },
175
+ { text: '2' },
176
+ { text: '3' },
177
+ { text: '4' },
178
+ { text: '5' },
179
+ ]);
180
+ });
181
+ it('should handle cases where some PartListUnion items are single Parts and others are arrays of Parts', () => {
182
+ const singlePart1 = { text: 'First single' };
183
+ const arrayPart1 = [
184
+ { text: 'Array item 1' },
185
+ { text: 'Array item 2' },
186
+ ];
187
+ const singlePart2 = {
188
+ inlineData: { mimeType: 'application/json', data: 'e30=' },
189
+ }; // {}
190
+ const arrayPart2 = [{ text: 'Last array item' }];
191
+ const result = mergePartListUnions([
192
+ singlePart1,
193
+ arrayPart1,
194
+ singlePart2,
195
+ arrayPart2,
196
+ ]);
197
+ expect(result).toEqual([
198
+ { text: 'First single' },
199
+ { text: 'Array item 1' },
200
+ { text: 'Array item 2' },
201
+ { inlineData: { mimeType: 'application/json', data: 'e30=' } },
202
+ { text: 'Last array item' },
203
+ ]);
204
+ });
205
+ });
206
+ // --- Tests for useGeminiStream Hook ---
207
+ describe('useGeminiStream', () => {
208
+ let mockAddItem;
209
+ let mockSetShowHelp;
210
+ let mockConfig;
211
+ let mockOnDebugMessage;
212
+ let mockHandleSlashCommand;
213
+ let mockScheduleToolCalls;
214
+ let mockCancelAllToolCalls;
215
+ let mockMarkToolsAsSubmitted;
216
+ beforeEach(() => {
217
+ vi.clearAllMocks(); // Clear mocks before each test
218
+ mockAddItem = vi.fn();
219
+ mockSetShowHelp = vi.fn();
220
+ // Define the mock for getGeminiClient
221
+ const mockGetGeminiClient = vi.fn().mockImplementation(() => {
222
+ // MockedGeminiClientClass is defined in the module scope by the previous change.
223
+ // It will use the mockStartChat and mockSendMessageStream that are managed within beforeEach.
224
+ const clientInstance = new MockedGeminiClientClass(mockConfig);
225
+ return clientInstance;
226
+ });
227
+ mockConfig = {
228
+ apiKey: 'test-api-key',
229
+ model: 'gemini-pro',
230
+ sandbox: false,
231
+ targetDir: '/test/dir',
232
+ debugMode: false,
233
+ question: undefined,
234
+ fullContext: false,
235
+ coreTools: [],
236
+ toolDiscoveryCommand: undefined,
237
+ toolCallCommand: undefined,
238
+ mcpServerCommand: undefined,
239
+ mcpServers: undefined,
240
+ userAgent: 'test-agent',
241
+ userMemory: '',
242
+ geminiMdFileCount: 0,
243
+ alwaysSkipModificationConfirmation: false,
244
+ vertexai: false,
245
+ showMemoryUsage: false,
246
+ contextFileName: undefined,
247
+ getToolRegistry: vi.fn(() => ({ getToolSchemaList: vi.fn(() => []) })),
248
+ getProjectRoot: vi.fn(() => '/test/dir'),
249
+ getCheckpointingEnabled: vi.fn(() => false),
250
+ getGeminiClient: mockGetGeminiClient,
251
+ getUsageStatisticsEnabled: () => true,
252
+ getDebugMode: () => false,
253
+ addHistory: vi.fn(),
254
+ };
255
+ mockOnDebugMessage = vi.fn();
256
+ mockHandleSlashCommand = vi.fn().mockResolvedValue(false);
257
+ // Mock return value for useReactToolScheduler
258
+ mockScheduleToolCalls = vi.fn();
259
+ mockCancelAllToolCalls = vi.fn();
260
+ mockMarkToolsAsSubmitted = vi.fn();
261
+ // Default mock for useReactToolScheduler to prevent toolCalls being undefined initially
262
+ mockUseReactToolScheduler.mockReturnValue([
263
+ [], // Default to empty array for toolCalls
264
+ mockScheduleToolCalls,
265
+ mockCancelAllToolCalls,
266
+ mockMarkToolsAsSubmitted,
267
+ ]);
268
+ // Reset mocks for GeminiClient instance methods (startChat and sendMessageStream)
269
+ // The GeminiClient constructor itself is mocked at the module level.
270
+ mockStartChat.mockClear().mockResolvedValue({
271
+ sendMessageStream: mockSendMessageStream,
272
+ }); // GeminiChat -> any
273
+ mockSendMessageStream
274
+ .mockClear()
275
+ .mockReturnValue((async function* () { })());
276
+ });
277
+ const mockLoadedSettings = {
278
+ merged: { preferredEditor: 'vscode' },
279
+ user: { path: '/user/settings.json', settings: {} },
280
+ workspace: { path: '/workspace/.gemini/settings.json', settings: {} },
281
+ errors: [],
282
+ forScope: vi.fn(),
283
+ setValue: vi.fn(),
284
+ };
285
+ const renderTestHook = (initialToolCalls = [], geminiClient) => {
286
+ let currentToolCalls = initialToolCalls;
287
+ const setToolCalls = (newToolCalls) => {
288
+ currentToolCalls = newToolCalls;
289
+ };
290
+ mockUseReactToolScheduler.mockImplementation(() => [
291
+ currentToolCalls,
292
+ mockScheduleToolCalls,
293
+ mockCancelAllToolCalls,
294
+ mockMarkToolsAsSubmitted,
295
+ ]);
296
+ const client = geminiClient || mockConfig.getGeminiClient();
297
+ const { result, rerender } = renderHook((props) => {
298
+ // Update the mock's return value if new toolCalls are passed in props
299
+ if (props.toolCalls) {
300
+ setToolCalls(props.toolCalls);
301
+ }
302
+ return useGeminiStream(props.client, props.history, props.addItem, props.setShowHelp, props.config, props.onDebugMessage, props.handleSlashCommand, props.shellModeActive, () => 'vscode', () => { }, () => Promise.resolve());
303
+ }, {
304
+ initialProps: {
305
+ client,
306
+ history: [],
307
+ addItem: mockAddItem,
308
+ setShowHelp: mockSetShowHelp,
309
+ config: mockConfig,
310
+ onDebugMessage: mockOnDebugMessage,
311
+ handleSlashCommand: mockHandleSlashCommand,
312
+ shellModeActive: false,
313
+ loadedSettings: mockLoadedSettings,
314
+ toolCalls: initialToolCalls,
315
+ },
316
+ });
317
+ return {
318
+ result,
319
+ rerender,
320
+ mockMarkToolsAsSubmitted,
321
+ mockSendMessageStream,
322
+ client,
323
+ };
324
+ };
325
+ it('should not submit tool responses if not all tool calls are completed', () => {
326
+ const toolCalls = [
327
+ {
328
+ request: {
329
+ callId: 'call1',
330
+ name: 'tool1',
331
+ args: {},
332
+ isClientInitiated: false,
333
+ },
334
+ status: 'success',
335
+ responseSubmittedToGemini: false,
336
+ response: {
337
+ callId: 'call1',
338
+ responseParts: [{ text: 'tool 1 response' }],
339
+ error: undefined,
340
+ resultDisplay: 'Tool 1 success display',
341
+ },
342
+ tool: {
343
+ name: 'tool1',
344
+ description: 'desc1',
345
+ getDescription: vi.fn(),
346
+ },
347
+ startTime: Date.now(),
348
+ endTime: Date.now(),
349
+ },
350
+ {
351
+ request: { callId: 'call2', name: 'tool2', args: {} },
352
+ status: 'executing',
353
+ responseSubmittedToGemini: false,
354
+ tool: {
355
+ name: 'tool2',
356
+ description: 'desc2',
357
+ getDescription: vi.fn(),
358
+ },
359
+ startTime: Date.now(),
360
+ liveOutput: '...',
361
+ },
362
+ ];
363
+ const { mockMarkToolsAsSubmitted, mockSendMessageStream } = renderTestHook(toolCalls);
364
+ // Effect for submitting tool responses depends on toolCalls and isResponding
365
+ // isResponding is initially false, so the effect should run.
366
+ expect(mockMarkToolsAsSubmitted).not.toHaveBeenCalled();
367
+ expect(mockSendMessageStream).not.toHaveBeenCalled(); // submitQuery uses this
368
+ });
369
+ it('should submit tool responses when all tool calls are completed and ready', async () => {
370
+ const toolCall1ResponseParts = [
371
+ { text: 'tool 1 final response' },
372
+ ];
373
+ const toolCall2ResponseParts = [
374
+ { text: 'tool 2 final response' },
375
+ ];
376
+ const completedToolCalls = [
377
+ {
378
+ request: {
379
+ callId: 'call1',
380
+ name: 'tool1',
381
+ args: {},
382
+ isClientInitiated: false,
383
+ },
384
+ status: 'success',
385
+ responseSubmittedToGemini: false,
386
+ response: { callId: 'call1', responseParts: toolCall1ResponseParts },
387
+ },
388
+ {
389
+ request: {
390
+ callId: 'call2',
391
+ name: 'tool2',
392
+ args: {},
393
+ isClientInitiated: false,
394
+ },
395
+ status: 'error',
396
+ responseSubmittedToGemini: false,
397
+ response: { callId: 'call2', responseParts: toolCall2ResponseParts },
398
+ },
399
+ ];
400
+ // Capture the onComplete callback
401
+ let capturedOnComplete = null;
402
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
403
+ capturedOnComplete = onComplete;
404
+ return [[], mockScheduleToolCalls, mockMarkToolsAsSubmitted];
405
+ });
406
+ renderHook(() => useGeminiStream(new MockedGeminiClientClass(mockConfig), [], mockAddItem, mockSetShowHelp, mockConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, () => Promise.resolve()));
407
+ // Trigger the onComplete callback with completed tools
408
+ await act(async () => {
409
+ if (capturedOnComplete) {
410
+ await capturedOnComplete(completedToolCalls);
411
+ }
412
+ });
413
+ await waitFor(() => {
414
+ expect(mockMarkToolsAsSubmitted).toHaveBeenCalledTimes(1);
415
+ expect(mockSendMessageStream).toHaveBeenCalledTimes(1);
416
+ });
417
+ const expectedMergedResponse = mergePartListUnions([
418
+ toolCall1ResponseParts,
419
+ toolCall2ResponseParts,
420
+ ]);
421
+ expect(mockSendMessageStream).toHaveBeenCalledWith(expectedMergedResponse, expect.any(AbortSignal));
422
+ });
423
+ it('should handle all tool calls being cancelled', async () => {
424
+ const cancelledToolCalls = [
425
+ {
426
+ request: {
427
+ callId: '1',
428
+ name: 'testTool',
429
+ args: {},
430
+ isClientInitiated: false,
431
+ },
432
+ status: 'cancelled',
433
+ response: { callId: '1', responseParts: [{ text: 'cancelled' }] },
434
+ responseSubmittedToGemini: false,
435
+ },
436
+ ];
437
+ const client = new MockedGeminiClientClass(mockConfig);
438
+ // Capture the onComplete callback
439
+ let capturedOnComplete = null;
440
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
441
+ capturedOnComplete = onComplete;
442
+ return [[], mockScheduleToolCalls, mockMarkToolsAsSubmitted];
443
+ });
444
+ renderHook(() => useGeminiStream(client, [], mockAddItem, mockSetShowHelp, mockConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, () => Promise.resolve()));
445
+ // Trigger the onComplete callback with cancelled tools
446
+ await act(async () => {
447
+ if (capturedOnComplete) {
448
+ await capturedOnComplete(cancelledToolCalls);
449
+ }
450
+ });
451
+ await waitFor(() => {
452
+ expect(mockMarkToolsAsSubmitted).toHaveBeenCalledWith(['1']);
453
+ expect(client.addHistory).toHaveBeenCalledWith({
454
+ role: 'user',
455
+ parts: [{ text: 'cancelled' }],
456
+ });
457
+ // Ensure we do NOT call back to the API
458
+ expect(mockSendMessageStream).not.toHaveBeenCalled();
459
+ });
460
+ });
461
+ it('should group multiple cancelled tool call responses into a single history entry', async () => {
462
+ const cancelledToolCall1 = {
463
+ request: {
464
+ callId: 'cancel-1',
465
+ name: 'toolA',
466
+ args: {},
467
+ isClientInitiated: false,
468
+ },
469
+ tool: {
470
+ name: 'toolA',
471
+ description: 'descA',
472
+ getDescription: vi.fn(),
473
+ },
474
+ status: 'cancelled',
475
+ response: {
476
+ callId: 'cancel-1',
477
+ responseParts: [
478
+ { functionResponse: { name: 'toolA', id: 'cancel-1' } },
479
+ ],
480
+ resultDisplay: undefined,
481
+ error: undefined,
482
+ },
483
+ responseSubmittedToGemini: false,
484
+ };
485
+ const cancelledToolCall2 = {
486
+ request: {
487
+ callId: 'cancel-2',
488
+ name: 'toolB',
489
+ args: {},
490
+ isClientInitiated: false,
491
+ },
492
+ tool: {
493
+ name: 'toolB',
494
+ description: 'descB',
495
+ getDescription: vi.fn(),
496
+ },
497
+ status: 'cancelled',
498
+ response: {
499
+ callId: 'cancel-2',
500
+ responseParts: [
501
+ { functionResponse: { name: 'toolB', id: 'cancel-2' } },
502
+ ],
503
+ resultDisplay: undefined,
504
+ error: undefined,
505
+ },
506
+ responseSubmittedToGemini: false,
507
+ };
508
+ const allCancelledTools = [cancelledToolCall1, cancelledToolCall2];
509
+ const client = new MockedGeminiClientClass(mockConfig);
510
+ let capturedOnComplete = null;
511
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
512
+ capturedOnComplete = onComplete;
513
+ return [[], mockScheduleToolCalls, mockMarkToolsAsSubmitted];
514
+ });
515
+ renderHook(() => useGeminiStream(client, [], mockAddItem, mockSetShowHelp, mockConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, () => Promise.resolve()));
516
+ // Trigger the onComplete callback with multiple cancelled tools
517
+ await act(async () => {
518
+ if (capturedOnComplete) {
519
+ await capturedOnComplete(allCancelledTools);
520
+ }
521
+ });
522
+ await waitFor(() => {
523
+ // The tools should be marked as submitted locally
524
+ expect(mockMarkToolsAsSubmitted).toHaveBeenCalledWith([
525
+ 'cancel-1',
526
+ 'cancel-2',
527
+ ]);
528
+ // Crucially, addHistory should be called only ONCE
529
+ expect(client.addHistory).toHaveBeenCalledTimes(1);
530
+ // And that single call should contain BOTH function responses
531
+ expect(client.addHistory).toHaveBeenCalledWith({
532
+ role: 'user',
533
+ parts: [
534
+ ...cancelledToolCall1.response.responseParts,
535
+ ...cancelledToolCall2.response.responseParts,
536
+ ],
537
+ });
538
+ // No message should be sent back to the API for a turn with only cancellations
539
+ expect(mockSendMessageStream).not.toHaveBeenCalled();
540
+ });
541
+ });
542
+ it('should not flicker streaming state to Idle between tool completion and submission', async () => {
543
+ const toolCallResponseParts = [
544
+ { text: 'tool 1 final response' },
545
+ ];
546
+ const initialToolCalls = [
547
+ {
548
+ request: {
549
+ callId: 'call1',
550
+ name: 'tool1',
551
+ args: {},
552
+ isClientInitiated: false,
553
+ },
554
+ status: 'executing',
555
+ responseSubmittedToGemini: false,
556
+ tool: {
557
+ name: 'tool1',
558
+ description: 'desc',
559
+ getDescription: vi.fn(),
560
+ },
561
+ startTime: Date.now(),
562
+ },
563
+ ];
564
+ const completedToolCalls = [
565
+ {
566
+ ...initialToolCalls[0],
567
+ status: 'success',
568
+ response: {
569
+ callId: 'call1',
570
+ responseParts: toolCallResponseParts,
571
+ error: undefined,
572
+ resultDisplay: 'Tool 1 success display',
573
+ },
574
+ endTime: Date.now(),
575
+ },
576
+ ];
577
+ // Capture the onComplete callback
578
+ let capturedOnComplete = null;
579
+ let currentToolCalls = initialToolCalls;
580
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
581
+ capturedOnComplete = onComplete;
582
+ return [
583
+ currentToolCalls,
584
+ mockScheduleToolCalls,
585
+ mockMarkToolsAsSubmitted,
586
+ ];
587
+ });
588
+ const { result, rerender } = renderHook(() => useGeminiStream(new MockedGeminiClientClass(mockConfig), [], mockAddItem, mockSetShowHelp, mockConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, () => Promise.resolve()));
589
+ // 1. Initial state should be Responding because a tool is executing.
590
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
591
+ // 2. Update the tool calls to completed state and rerender
592
+ currentToolCalls = completedToolCalls;
593
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
594
+ capturedOnComplete = onComplete;
595
+ return [
596
+ completedToolCalls,
597
+ mockScheduleToolCalls,
598
+ mockMarkToolsAsSubmitted,
599
+ ];
600
+ });
601
+ act(() => {
602
+ rerender();
603
+ });
604
+ // 3. The state should *still* be Responding, not Idle.
605
+ // This is because the completed tool's response has not been submitted yet.
606
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
607
+ // 4. Trigger the onComplete callback to simulate tool completion
608
+ await act(async () => {
609
+ if (capturedOnComplete) {
610
+ await capturedOnComplete(completedToolCalls);
611
+ }
612
+ });
613
+ // 5. Wait for submitQuery to be called
614
+ await waitFor(() => {
615
+ expect(mockSendMessageStream).toHaveBeenCalledWith(toolCallResponseParts, expect.any(AbortSignal));
616
+ });
617
+ // 6. After submission, the state should remain Responding until the stream completes.
618
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
619
+ });
620
+ describe('User Cancellation', () => {
621
+ let useInputCallback;
622
+ const mockUseInput = useInput;
623
+ beforeEach(() => {
624
+ // Capture the callback passed to useInput
625
+ mockUseInput.mockImplementation((callback) => {
626
+ useInputCallback = callback;
627
+ });
628
+ });
629
+ const simulateEscapeKeyPress = () => {
630
+ act(() => {
631
+ useInputCallback('', { escape: true });
632
+ });
633
+ };
634
+ it('should cancel an in-progress stream when escape is pressed', async () => {
635
+ const mockStream = (async function* () {
636
+ yield { type: 'content', value: 'Part 1' };
637
+ // Keep the stream open
638
+ await new Promise(() => { });
639
+ })();
640
+ mockSendMessageStream.mockReturnValue(mockStream);
641
+ const { result } = renderTestHook();
642
+ // Start a query
643
+ await act(async () => {
644
+ result.current.submitQuery('test query');
645
+ });
646
+ // Wait for the first part of the response
647
+ await waitFor(() => {
648
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
649
+ });
650
+ // Simulate escape key press
651
+ simulateEscapeKeyPress();
652
+ // Verify cancellation message is added
653
+ await waitFor(() => {
654
+ expect(mockAddItem).toHaveBeenCalledWith({
655
+ type: MessageType.INFO,
656
+ text: 'Request cancelled.',
657
+ }, expect.any(Number));
658
+ });
659
+ // Verify state is reset
660
+ expect(result.current.streamingState).toBe(StreamingState.Idle);
661
+ });
662
+ it('should not do anything if escape is pressed when not responding', () => {
663
+ const { result } = renderTestHook();
664
+ expect(result.current.streamingState).toBe(StreamingState.Idle);
665
+ // Simulate escape key press
666
+ simulateEscapeKeyPress();
667
+ // No change should happen, no cancellation message
668
+ expect(mockAddItem).not.toHaveBeenCalledWith(expect.objectContaining({
669
+ text: 'Request cancelled.',
670
+ }), expect.any(Number));
671
+ });
672
+ it('should prevent further processing after cancellation', async () => {
673
+ let continueStream;
674
+ const streamPromise = new Promise((resolve) => {
675
+ continueStream = resolve;
676
+ });
677
+ const mockStream = (async function* () {
678
+ yield { type: 'content', value: 'Initial' };
679
+ await streamPromise; // Wait until we manually continue
680
+ yield { type: 'content', value: ' Canceled' };
681
+ })();
682
+ mockSendMessageStream.mockReturnValue(mockStream);
683
+ const { result } = renderTestHook();
684
+ await act(async () => {
685
+ result.current.submitQuery('long running query');
686
+ });
687
+ await waitFor(() => {
688
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
689
+ });
690
+ // Cancel the request
691
+ simulateEscapeKeyPress();
692
+ // Allow the stream to continue
693
+ act(() => {
694
+ continueStream();
695
+ });
696
+ // Wait a bit to see if the second part is processed
697
+ await new Promise((resolve) => setTimeout(resolve, 50));
698
+ // The text should not have been updated with " Canceled"
699
+ const lastCall = mockAddItem.mock.calls.find((call) => call[0].type === 'gemini');
700
+ expect(lastCall?.[0].text).toBe('Initial');
701
+ // The final state should be idle after cancellation
702
+ expect(result.current.streamingState).toBe(StreamingState.Idle);
703
+ });
704
+ it('should not cancel if a tool call is in progress (not just responding)', async () => {
705
+ const toolCalls = [
706
+ {
707
+ request: { callId: 'call1', name: 'tool1', args: {} },
708
+ status: 'executing',
709
+ responseSubmittedToGemini: false,
710
+ tool: {
711
+ name: 'tool1',
712
+ description: 'desc1',
713
+ getDescription: vi.fn(),
714
+ },
715
+ startTime: Date.now(),
716
+ liveOutput: '...',
717
+ },
718
+ ];
719
+ const abortSpy = vi.spyOn(AbortController.prototype, 'abort');
720
+ const { result } = renderTestHook(toolCalls);
721
+ // State is `Responding` because a tool is running
722
+ expect(result.current.streamingState).toBe(StreamingState.Responding);
723
+ // Try to cancel
724
+ simulateEscapeKeyPress();
725
+ // Nothing should happen because the state is not `Responding`
726
+ expect(abortSpy).not.toHaveBeenCalled();
727
+ });
728
+ });
729
+ describe('Slash Command Handling', () => {
730
+ it('should schedule a tool call when the command processor returns a schedule_tool action', async () => {
731
+ const clientToolRequest = {
732
+ type: 'schedule_tool',
733
+ toolName: 'save_memory',
734
+ toolArgs: { fact: 'test fact' },
735
+ };
736
+ mockHandleSlashCommand.mockResolvedValue(clientToolRequest);
737
+ const { result } = renderTestHook();
738
+ await act(async () => {
739
+ await result.current.submitQuery('/memory add "test fact"');
740
+ });
741
+ await waitFor(() => {
742
+ expect(mockScheduleToolCalls).toHaveBeenCalledWith([
743
+ expect.objectContaining({
744
+ name: 'save_memory',
745
+ args: { fact: 'test fact' },
746
+ isClientInitiated: true,
747
+ }),
748
+ ], expect.any(AbortSignal));
749
+ expect(mockSendMessageStream).not.toHaveBeenCalled();
750
+ });
751
+ });
752
+ it('should stop processing and not call Gemini when a command is handled without a tool call', async () => {
753
+ const uiOnlyCommandResult = {
754
+ type: 'handled',
755
+ };
756
+ mockHandleSlashCommand.mockResolvedValue(uiOnlyCommandResult);
757
+ const { result } = renderTestHook();
758
+ await act(async () => {
759
+ await result.current.submitQuery('/help');
760
+ });
761
+ await waitFor(() => {
762
+ expect(mockHandleSlashCommand).toHaveBeenCalledWith('/help');
763
+ expect(mockScheduleToolCalls).not.toHaveBeenCalled();
764
+ expect(mockSendMessageStream).not.toHaveBeenCalled(); // No LLM call made
765
+ });
766
+ });
767
+ });
768
+ describe('Memory Refresh on save_memory', () => {
769
+ it('should call performMemoryRefresh when a save_memory tool call completes successfully', async () => {
770
+ const mockPerformMemoryRefresh = vi.fn();
771
+ const completedToolCall = {
772
+ request: {
773
+ callId: 'save-mem-call-1',
774
+ name: 'save_memory',
775
+ args: { fact: 'test' },
776
+ isClientInitiated: true,
777
+ },
778
+ status: 'success',
779
+ responseSubmittedToGemini: false,
780
+ response: {
781
+ callId: 'save-mem-call-1',
782
+ responseParts: [{ text: 'Memory saved' }],
783
+ resultDisplay: 'Success: Memory saved',
784
+ error: undefined,
785
+ },
786
+ tool: {
787
+ name: 'save_memory',
788
+ description: 'Saves memory',
789
+ getDescription: vi.fn(),
790
+ },
791
+ };
792
+ // Capture the onComplete callback
793
+ let capturedOnComplete = null;
794
+ mockUseReactToolScheduler.mockImplementation((onComplete) => {
795
+ capturedOnComplete = onComplete;
796
+ return [[], mockScheduleToolCalls, mockMarkToolsAsSubmitted];
797
+ });
798
+ renderHook(() => useGeminiStream(new MockedGeminiClientClass(mockConfig), [], mockAddItem, mockSetShowHelp, mockConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, mockPerformMemoryRefresh));
799
+ // Trigger the onComplete callback with the completed save_memory tool
800
+ await act(async () => {
801
+ if (capturedOnComplete) {
802
+ await capturedOnComplete([completedToolCall]);
803
+ }
804
+ });
805
+ await waitFor(() => {
806
+ expect(mockPerformMemoryRefresh).toHaveBeenCalledTimes(1);
807
+ });
808
+ });
809
+ });
810
+ describe('Error Handling', () => {
811
+ it('should call parseAndFormatApiError with the correct authType on stream initialization failure', async () => {
812
+ // 1. Setup
813
+ const mockError = new Error('Rate limit exceeded');
814
+ const mockAuthType = AuthType.LOGIN_WITH_GOOGLE;
815
+ mockParseAndFormatApiError.mockClear();
816
+ mockSendMessageStream.mockReturnValue((async function* () {
817
+ yield { type: 'content', value: '' };
818
+ throw mockError;
819
+ })());
820
+ const testConfig = {
821
+ ...mockConfig,
822
+ getContentGeneratorConfig: vi.fn(() => ({
823
+ authType: mockAuthType,
824
+ })),
825
+ };
826
+ const { result } = renderHook(() => useGeminiStream(new MockedGeminiClientClass(testConfig), [], mockAddItem, mockSetShowHelp, testConfig, mockOnDebugMessage, mockHandleSlashCommand, false, () => 'vscode', () => { }, () => Promise.resolve()));
827
+ // 2. Action
828
+ await act(async () => {
829
+ await result.current.submitQuery('test query');
830
+ });
831
+ // 3. Assertion
832
+ await waitFor(() => {
833
+ expect(mockParseAndFormatApiError).toHaveBeenCalledWith('Rate limit exceeded', mockAuthType);
834
+ });
835
+ });
836
+ });
837
+ });
838
+ //# sourceMappingURL=useGeminiStream.test.js.map