Elffuss BraidHub

kikocis/continue public

main · 13.2 KiBRawPermalink
1import { ChatMessage } from "core";
2import { renderChatMessage } from "core/util/messageContent";
3import { v4 as uuidv4 } from "uuid";
4import { beforeEach, describe, expect, it, vi } from "vitest";
5import { addToolCallDeltaToState } from "../../util/toolCallState";
6import { ChatHistoryItemWithMessageId, sessionSlice } from "./sessionSlice";
7
8// Mock dependencies
9vi.mock("uuid");
10vi.mock("core/util/messageContent");
11vi.mock("../../util/toolCallState");
12
13const mockUuidv4 = vi.mocked(uuidv4);
14const mockRenderChatMessage = vi.mocked(renderChatMessage);
15const mockAddToolCallDeltaToState = vi.mocked(addToolCallDeltaToState);
16
17describe("sessionSlice streamUpdate", () => {
18 beforeEach(() => {
19 // Reset mocks before each test
20 vi.clearAllMocks();
21
22 // Mock uuidv4 to return predictable values
23 let callCount = 0;
24 mockUuidv4.mockImplementation(() => `mock-uuid-${++callCount}`);
25
26 // Mock renderChatMessage to return content as is
27 mockRenderChatMessage.mockImplementation((message: ChatMessage) => {
28 if (typeof message.content === "string") {
29 return message.content;
30 }
31 return "";
32 });
33
34 // Mock addToolCallDeltaToState
35 mockAddToolCallDeltaToState.mockImplementation((delta, state) => {
36 return {
37 status: "generating" as const,
38 toolCall: {
39 id: delta.id || "mock-tool-id",
40 type: "function" as const,
41 function: {
42 name: delta.function?.name || "mock-function",
43 arguments: delta.function?.arguments || "{}",
44 },
45 },
46 toolCallId: delta.id || "mock-tool-id",
47 parsedArgs: {},
48 };
49 });
50 });
51
52 const createInitialState = () => ({
53 lastSessionId: undefined,
54 allSessionMetadata: [],
55 history: [
56 {
57 message: {
58 role: "user" as const,
59 content: "This is a test.",
60 id: "initial-user-message",
61 },
62 contextItems: [],
63 },
64 ] as ChatHistoryItemWithMessageId[],
65 isStreaming: false,
66 title: "Test Session",
67 id: "test-session-id",
68 streamAborter: new AbortController(),
69 symbols: {},
70 mode: "chat" as const,
71 isInEdit: false,
72 codeBlockApplyStates: {
73 states: [],
74 curIndex: 0,
75 },
76 newestToolbarPreviewForInput: {},
77 isSessionMetadataLoading: false,
78 compactionLoading: {},
79 });
80
81 describe("Basic Chat Message", () => {
82 it("should append assistant message to history", () => {
83 const initialState = createInitialState();
84 const action = {
85 type: "session/streamUpdate",
86 payload: [
87 {
88 role: "assistant" as const,
89 content: "Here is a response to your message without thinking.",
90 },
91 ],
92 };
93
94 const newState = sessionSlice.reducer(initialState, action);
95
96 expect(newState.history).toHaveLength(2);
97 expect(newState.history[1].message.role).toBe("assistant");
98 expect(newState.history[1].message.content).toBe(
99 "Here is a response to your message without thinking.",
100 );
101 expect(newState.history[1].message.id).toBe("mock-uuid-1");
102 expect(newState.history[1].contextItems).toEqual([]);
103 });
104 });
105
106 describe("Chat Message With Thinking", () => {
107 it("should split thinking and assistant content correctly", () => {
108 const initialState = createInitialState();
109 const action = {
110 type: "session/streamUpdate",
111 payload: [
112 {
113 role: "assistant" as const,
114 content:
115 "<think>I should send the user a response.</think> Here is a response to your message with thinking.",
116 },
117 ],
118 };
119
120 const newState = sessionSlice.reducer(initialState, action);
121
122 expect(newState.history).toHaveLength(2);
123
124 // Check reasoning
125 expect(newState.history[0].reasoning?.text).toBe(
126 "I should send the user a response.",
127 );
128
129 // Check assistant message
130 expect(newState.history[1].message.role).toBe("assistant");
131 expect(newState.history[1].message.content).toBe(
132 "Here is a response to your message with thinking.",
133 );
134 expect(newState.history[1].message.id).toBe("mock-uuid-1");
135 });
136 });
137
138 describe("Tool Call With Response", () => {
139 it("should handle tool call followed by tool response and assistant message", () => {
140 const initialState = createInitialState();
141 const toolCallAction = {
142 type: "session/streamUpdate",
143 payload: [
144 {
145 role: "assistant" as const,
146 content: "<think>I should use a tool call.</think>",
147 toolCalls: [
148 {
149 id: "1234",
150 type: "function" as const,
151 function: {
152 name: "builtin_ls",
153 arguments: '{"dirPath":".","recursive":false}',
154 },
155 },
156 ],
157 },
158 ],
159 };
160
161 let newState = sessionSlice.reducer(initialState, toolCallAction);
162 expect(newState.history).toHaveLength(2);
163
164 // Check reasoning
165 expect(newState.history[0].reasoning?.text).toBe(
166 "I should use a tool call.",
167 );
168
169 // Check generating message
170 expect(newState.history[1].message.role).toBe("assistant");
171 expect(newState.history[1].message.content).toBe("");
172 expect(newState.history[1].toolCallStates?.[0]?.status).toBe(
173 "generating",
174 );
175 expect(newState.history[1].toolCallStates?.[0]?.toolCallId).toBe("1234");
176
177 const toolResponseAction = {
178 type: "session/streamUpdate",
179 payload: [
180 {
181 role: "tool" as const,
182 toolCallId: "1234",
183 content: "foo.txt\nbar.txt\nexample.php",
184 },
185 {
186 role: "assistant" as const,
187 content: "I see, the tool found 3 files.",
188 },
189 ],
190 };
191 newState = sessionSlice.reducer(newState, toolResponseAction);
192 expect(newState.history).toHaveLength(4);
193
194 // Check tool message
195 expect(newState.history[2].message.role).toBe("tool");
196 expect(newState.history[2].message.content).toBe(
197 "foo.txt\nbar.txt\nexample.php",
198 );
199 expect((newState.history[2].message as any).toolCallId).toBe("1234");
200
201 // Check final assistant message
202 expect(newState.history[3].message.role).toBe("assistant");
203 expect(newState.history[3].message.content).toBe(
204 "I see, the tool found 3 files.",
205 );
206 });
207 });
208
209 describe("Tool Call With Streaming Response", () => {
210 it("should handle streaming assistant response after tool call", () => {
211 const initialState = createInitialState();
212 const toolCallAction = {
213 type: "session/streamUpdate",
214 payload: [
215 {
216 role: "assistant" as const,
217 content: "<think>I should use a tool call.</think>",
218 toolCalls: [
219 {
220 id: "1234",
221 type: "function" as const,
222 function: {
223 name: "builtin_ls",
224 arguments: '{"dirPath":".","recursive":false}',
225 },
226 },
227 ],
228 },
229 ],
230 };
231
232 let newState = sessionSlice.reducer(initialState, toolCallAction);
233 expect(newState.history).toHaveLength(2);
234
235 // Check reasoning
236 expect(newState.history[0].reasoning?.text).toBe(
237 "I should use a tool call.",
238 );
239
240 // Check generating message
241 expect(newState.history[1].message.role).toBe("assistant");
242 expect(newState.history[1].message.content).toBe("");
243 expect(newState.history[1].toolCallStates?.[0]?.status).toBe(
244 "generating",
245 );
246 expect(newState.history[1].toolCallStates?.[0]?.toolCallId).toBe("1234");
247
248 const toolResponseAction = {
249 type: "session/streamUpdate",
250 payload: [
251 {
252 role: "tool" as const,
253 toolCallId: "1234",
254 content: "foo.txt\nbar.txt\nexample.php",
255 },
256 {
257 role: "assistant" as const,
258 content: "<think>",
259 },
260 {
261 role: "assistant" as const,
262 content: "Good, ",
263 },
264 {
265 role: "assistant" as const,
266 content: "I received a list",
267 },
268 {
269 role: "assistant" as const,
270 content: " of files.",
271 },
272 {
273 role: "assistant" as const,
274 content: "</think>",
275 },
276 {
277 role: "assistant" as const,
278 content: "\n",
279 },
280 {
281 role: "assistant" as const,
282 content: "I see, ",
283 },
284 {
285 role: "assistant" as const,
286 content: "the tool ",
287 },
288 {
289 role: "assistant" as const,
290 content: "found 3 ",
291 },
292 {
293 role: "assistant" as const,
294 content: "files.",
295 },
296 ],
297 };
298
299 newState = sessionSlice.reducer(newState, toolResponseAction);
300
301 expect(newState.history).toHaveLength(4);
302
303 // Check tool message
304 expect(newState.history[2].message.role).toBe("tool");
305 expect(newState.history[2].message.content).toBe(
306 "foo.txt\nbar.txt\nexample.php",
307 );
308
309 // Check response message
310 expect(newState.history[3].message.role).toBe("assistant");
311 expect(newState.history[3].message.content).toBe(
312 "I see, the tool found 3 files.",
313 );
314 expect(newState.history[3].reasoning?.text).toBe(
315 "Good, I received a list of files.",
316 );
317 expect(newState.history[3].reasoning?.active).toBe(false);
318 });
319 });
320
321 describe("Edge Cases", () => {
322 it("should handle empty history gracefully", () => {
323 const initialState = createInitialState();
324 initialState.history = [];
325
326 const action = {
327 type: "session/streamUpdate",
328 payload: [
329 {
330 role: "assistant" as const,
331 content: "Hello",
332 },
333 ],
334 };
335
336 const newState = sessionSlice.reducer(initialState, action);
337
338 // Should not crash and history should remain empty
339 expect(newState.history).toHaveLength(0);
340 });
341
342 it("should handle redacted thinking messages", () => {
343 const initialState = createInitialState();
344 const action = {
345 type: "session/streamUpdate",
346 payload: [
347 {
348 role: "thinking" as const,
349 content: "This should be hidden",
350 redactedThinking: true,
351 },
352 ],
353 };
354
355 const newState = sessionSlice.reducer(initialState, action);
356
357 expect(newState.history).toHaveLength(2);
358 expect(newState.history[1].message.role).toBe("thinking");
359 expect(newState.history[1].message.content).toBe(
360 "internal reasoning is hidden due to safety reasons",
361 );
362 expect((newState.history[1].message as any).redactedThinking).toBe(true);
363 });
364
365 it("should handle signature updates for thinking messages", () => {
366 const initialState = createInitialState();
367 // First add a thinking message
368 initialState.history.push({
369 message: {
370 role: "thinking",
371 content: "Some thinking",
372 id: "thinking-message",
373 },
374 contextItems: [],
375 });
376
377 const action = {
378 type: "session/streamUpdate",
379 payload: [
380 {
381 role: "thinking" as const,
382 signature: "test-signature",
383 },
384 ],
385 };
386
387 const newState = sessionSlice.reducer(initialState, action);
388
389 expect(newState.history).toHaveLength(2);
390 expect((newState.history[1].message as any).signature).toBe(
391 "test-signature",
392 );
393 });
394
395 it("should accumulate content for same role messages", () => {
396 const initialState = createInitialState();
397 // Add an assistant message first
398 initialState.history.push({
399 message: {
400 role: "assistant",
401 content: "Hello ",
402 id: "assistant-message",
403 },
404 contextItems: [],
405 });
406
407 const action = {
408 type: "session/streamUpdate",
409 payload: [
410 {
411 role: "assistant" as const,
412 content: "world!",
413 },
414 ],
415 };
416
417 const newState = sessionSlice.reducer(initialState, action);
418
419 expect(newState.history).toHaveLength(2);
420 expect(newState.history[1].message.content).toBe("Hello world!");
421 });
422
423 it("should handle basic tool call streaming", () => {
424 const initialState = createInitialState();
425 const toolCallId = "call_123";
426
427 const action = {
428 type: "session/streamUpdate",
429 payload: [
430 {
431 role: "assistant" as const,
432 content: "",
433 toolCalls: [
434 {
435 id: toolCallId,
436 type: "function" as const,
437 function: {
438 name: "test_tool",
439 arguments: '{"arg":"value"}',
440 },
441 },
442 ],
443 },
444 ],
445 };
446
447 const newState = sessionSlice.reducer(initialState, action);
448
449 expect(newState.history).toHaveLength(2);
450 expect(newState.history[1].message.role).toBe("assistant");
451 expect(newState.history[1].toolCallStates).toHaveLength(1);
452 });
453 });
454});