-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathclearCommand.test.ts
More file actions
290 lines (249 loc) · 9.52 KB
/
clearCommand.test.ts
File metadata and controls
290 lines (249 loc) · 9.52 KB
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { clearCommand } from './clearCommand.js';
import { type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import {
SessionEndReason,
SessionStartSource,
ideContextStore,
} from '@qwen-code/qwen-code-core';
// Mock the telemetry service
vi.mock('@qwen-code/qwen-code-core', async () => {
const actual = await vi.importActual('@qwen-code/qwen-code-core');
return {
...actual,
uiTelemetryService: {
reset: vi.fn(),
},
ideContextStore: {
clear: vi.fn(),
},
};
});
import type { GeminiClient } from '@qwen-code/qwen-code-core';
describe('clearCommand', () => {
let mockContext: CommandContext;
let mockResetChat: ReturnType<typeof vi.fn>;
let mockStartNewSession: ReturnType<typeof vi.fn>;
let mockFireSessionEndEvent: ReturnType<typeof vi.fn>;
let mockFireSessionStartEvent: ReturnType<typeof vi.fn>;
let mockGetHookSystem: ReturnType<typeof vi.fn>;
beforeEach(() => {
mockResetChat = vi.fn().mockResolvedValue(undefined);
mockStartNewSession = vi.fn().mockReturnValue('new-session-id');
mockFireSessionEndEvent = vi.fn().mockResolvedValue(undefined);
mockFireSessionStartEvent = vi.fn().mockResolvedValue(undefined);
mockGetHookSystem = vi.fn().mockReturnValue({
fireSessionEndEvent: mockFireSessionEndEvent,
fireSessionStartEvent: mockFireSessionStartEvent,
});
vi.clearAllMocks();
mockContext = createMockCommandContext({
services: {
config: {
getGeminiClient: () =>
({
resetChat: mockResetChat,
}) as unknown as GeminiClient,
startNewSession: mockStartNewSession,
getHookSystem: mockGetHookSystem,
getDebugLogger: () => ({
warn: vi.fn(),
}),
getModel: () => 'test-model',
getToolRegistry: () => undefined,
getApprovalMode: () => 'default',
},
},
session: {
startNewSession: vi.fn(),
},
});
});
it('should only clear UI when called without flags', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
await clearCommand.action(mockContext, '');
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
expect(mockResetChat).not.toHaveBeenCalled();
expect(mockStartNewSession).not.toHaveBeenCalled();
});
it('should return confirm_action when called with --history and not confirmed', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
const result = await clearCommand.action(mockContext, '--history');
expect(result).toEqual({
type: 'confirm_action',
prompt: 'Are you sure you want to clear the conversation history?',
originalInvocation: { raw: '/clear --history' },
});
expect(mockContext.ui.clear).not.toHaveBeenCalled();
expect(mockResetChat).not.toHaveBeenCalled();
});
it('should return confirm_action when called with --all and not confirmed', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
const result = await clearCommand.action(mockContext, '--all');
expect(result).toEqual({
type: 'confirm_action',
prompt: 'Are you sure you want to completely reset the session?',
originalInvocation: { raw: '/clear --all' },
});
expect(mockContext.ui.clear).not.toHaveBeenCalled();
expect(mockResetChat).not.toHaveBeenCalled();
});
it('should set debug message, start a new session, reset chat, and clear UI when confirmed with --history', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--history');
expect(mockContext.ui.setDebugMessage).toHaveBeenCalledWith(
'Starting a new session, resetting chat, and clearing terminal.',
);
expect(mockStartNewSession).toHaveBeenCalledTimes(1);
expect(mockContext.session.startNewSession).toHaveBeenCalledWith(
'new-session-id',
);
expect(mockResetChat).toHaveBeenCalledTimes(1);
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
expect(ideContextStore.clear).not.toHaveBeenCalled();
});
it('should completely reset session when confirmed with --all', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--all');
expect(mockStartNewSession).toHaveBeenCalledTimes(1);
expect(mockResetChat).toHaveBeenCalledTimes(1);
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
expect(ideContextStore.clear).toHaveBeenCalledTimes(1);
});
it('should fire SessionEnd event before clearing and SessionStart event after clearing when confirmed', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--history');
expect(mockGetHookSystem).toHaveBeenCalled();
expect(mockFireSessionEndEvent).toHaveBeenCalledWith(
SessionEndReason.Clear,
);
expect(mockFireSessionStartEvent).toHaveBeenCalledWith(
SessionStartSource.Clear,
'test-model',
expect.any(String), // permissionMode
);
// SessionEnd should be called before SessionStart
const sessionEndCallOrder =
mockFireSessionEndEvent.mock.invocationCallOrder[0];
const sessionStartCallOrder =
mockFireSessionStartEvent.mock.invocationCallOrder[0];
expect(sessionEndCallOrder).toBeLessThan(sessionStartCallOrder);
});
it('should handle hook errors gracefully and continue execution', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
mockFireSessionEndEvent.mockRejectedValue(
new Error('SessionEnd hook failed'),
);
mockFireSessionStartEvent.mockRejectedValue(
new Error('SessionStart hook failed'),
);
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--history');
// Should still complete the clear operation despite hook errors
expect(mockStartNewSession).toHaveBeenCalledTimes(1);
expect(mockResetChat).toHaveBeenCalledTimes(1);
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
});
it('should clear UI before resetChat for immediate responsiveness', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
const callOrder: string[] = [];
(mockContext.ui.clear as ReturnType<typeof vi.fn>).mockImplementation(
() => {
callOrder.push('ui.clear');
},
);
mockResetChat.mockImplementation(async () => {
callOrder.push('resetChat');
});
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--history');
// ui.clear should be called before resetChat for immediate UI feedback
const clearIndex = callOrder.indexOf('ui.clear');
const resetIndex = callOrder.indexOf('resetChat');
expect(clearIndex).toBeGreaterThanOrEqual(0);
expect(resetIndex).toBeGreaterThanOrEqual(0);
expect(clearIndex).toBeLessThan(resetIndex);
});
it('should not await hook events (fire-and-forget)', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
// Make hooks take a long time - they should not block
let sessionEndResolved = false;
let sessionStartResolved = false;
mockFireSessionEndEvent.mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(() => {
sessionEndResolved = true;
resolve(undefined);
}, 5000);
}),
);
mockFireSessionStartEvent.mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(() => {
sessionStartResolved = true;
resolve(undefined);
}, 5000);
}),
);
mockContext.overwriteConfirmed = true;
await clearCommand.action(mockContext, '--history');
// The action should complete immediately without waiting for hooks
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
expect(mockResetChat).toHaveBeenCalledTimes(1);
// Hooks should have been called but not necessarily resolved
expect(mockFireSessionEndEvent).toHaveBeenCalled();
expect(mockFireSessionStartEvent).toHaveBeenCalled();
// Hooks should NOT have resolved yet since they have 5s timeouts
expect(sessionEndResolved).toBe(false);
expect(sessionStartResolved).toBe(false);
});
it('should not attempt to reset chat if config service is not available', async () => {
if (!clearCommand.action) {
throw new Error('clearCommand must have an action.');
}
const nullConfigContext = createMockCommandContext({
services: {
config: null,
},
session: {
startNewSession: vi.fn(),
},
overwriteConfirmed: true,
});
await clearCommand.action(nullConfigContext, '--history');
expect(nullConfigContext.ui.setDebugMessage).toHaveBeenCalledWith(
'Starting a new session and clearing.',
);
expect(mockResetChat).not.toHaveBeenCalled();
expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
});
});