Elffuss BraidHub

kikocis/continue public

81ce8cce6ac5 · 12.3 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 });
79
80 describe("Basic Chat Message", () => {
81 it("should append assistant message to history", () => {
82 const initialState = createInitialState();
83 const action = {
84 type: "session/streamUpdate",
85 payload: [
86 {
87 role: "assistant" as const,
88 content: "Here is a response to your message without thinking.",
89 },
90 ],
91 };
92
93 const newState = sessionSlice.reducer(initialState, action);
94
95 expect(newState.history).toHaveLength(2);
96 expect(newState.history[1].message.role).toBe("assistant");
97 expect(newState.history[1].message.content).toBe(
98 "Here is a response to your message without thinking.",
99 );
100 expect(newState.history[1].message.id).toBe("mock-uuid-1");
101 expect(newState.history[1].contextItems).toEqual([]);
102 });
103 });
104
105 describe("Chat Message With Thinking", () => {
106 it("should split thinking and assistant content correctly", () => {
107 const initialState = createInitialState();
108 const action = {
109 type: "session/streamUpdate",
110 payload: [
111 {
112 role: "assistant" as const,
113 content:
114 "<think>I should send the user a response.</think> Here is a response to your message with thinking.",
115 },
116 ],
117 };
118
119 const newState = sessionSlice.reducer(initialState, action);
120
121 expect(newState.history).toHaveLength(2);
122
123 // Check reasoning
124 expect(newState.history[0].reasoning?.text).toBe(
125 "I should send the user a response.",
126 );
127
128 // Check assistant message
129 expect(newState.history[1].message.role).toBe("assistant");
130 expect(newState.history[1].message.content).toBe(
131 "Here is a response to your message with thinking.",
132 );
133 expect(newState.history[1].message.id).toBe("mock-uuid-1");
134 });
135 });
136
137 describe("Tool Call With Response", () => {
138 it("should handle tool call followed by tool response and assistant message", () => {
139 const initialState = createInitialState();
140 const toolCallAction = {
141 type: "session/streamUpdate",
142 payload: [
143 {
144 role: "assistant" as const,
145 content: "<think>I should use a tool call.</think>",
146 toolCalls: [
147 {
148 id: "1234",
149 type: "function" as const,
150 function: {
151 name: "builtin_ls",
152 arguments: '{"dirPath":".","recursive":false}',
153 },
154 },
155 ],
156 },
157 ],
158 };
159
160 let newState = sessionSlice.reducer(initialState, toolCallAction);
161 expect(newState.history).toHaveLength(2);
162
163 // Check reasoning
164 expect(newState.history[0].reasoning?.text).toBe(
165 "I should use a tool call.",
166 );
167
168 // Check generating message
169 expect(newState.history[1].message.role).toBe("assistant");
170 expect(newState.history[1].message.content).toBe("");
171 expect(newState.history[1].toolCallState?.status).toBe("generating");
172 expect(newState.history[1].toolCallState?.toolCallId).toBe("1234");
173
174 const toolResponseAction = {
175 type: "session/streamUpdate",
176 payload: [
177 {
178 role: "tool" as const,
179 toolCallId: "1234",
180 content: "foo.txt\nbar.txt\nexample.php",
181 },
182 {
183 role: "assistant" as const,
184 content: "I see, the tool found 3 files.",
185 },
186 ],
187 };
188 newState = sessionSlice.reducer(newState, toolResponseAction);
189 expect(newState.history).toHaveLength(4);
190
191 // Check tool message
192 expect(newState.history[2].message.role).toBe("tool");
193 expect(newState.history[2].message.content).toBe(
194 "foo.txt\nbar.txt\nexample.php",
195 );
196 expect((newState.history[2].message as any).toolCallId).toBe("1234");
197
198 // Check final assistant message
199 expect(newState.history[3].message.role).toBe("assistant");
200 expect(newState.history[3].message.content).toBe(
201 "I see, the tool found 3 files.",
202 );
203 });
204 });
205
206 describe("Tool Call With Streaming Response", () => {
207 it("should handle streaming assistant response after tool call", () => {
208 const initialState = createInitialState();
209 const toolCallAction = {
210 type: "session/streamUpdate",
211 payload: [
212 {
213 role: "assistant" as const,
214 content: "<think>I should use a tool call.</think>",
215 toolCalls: [
216 {
217 id: "1234",
218 type: "function" as const,
219 function: {
220 name: "builtin_ls",
221 arguments: '{"dirPath":".","recursive":false}',
222 },
223 },
224 ],
225 },
226 ],
227 };
228
229 let newState = sessionSlice.reducer(initialState, toolCallAction);
230 expect(newState.history).toHaveLength(2);
231
232 // Check reasoning
233 expect(newState.history[0].reasoning?.text).toBe(
234 "I should use a tool call.",
235 );
236
237 // Check generating message
238 expect(newState.history[1].message.role).toBe("assistant");
239 expect(newState.history[1].message.content).toBe("");
240 expect(newState.history[1].toolCallState?.status).toBe("generating");
241 expect(newState.history[1].toolCallState?.toolCallId).toBe("1234");
242
243 const toolResponseAction = {
244 type: "session/streamUpdate",
245 payload: [
246 {
247 role: "tool" as const,
248 toolCallId: "1234",
249 content: "foo.txt\nbar.txt\nexample.php",
250 },
251 {
252 role: "assistant" as const,
253 content: "<think>",
254 },
255 {
256 role: "assistant" as const,
257 content: "Good, ",
258 },
259 {
260 role: "assistant" as const,
261 content: "I received a list",
262 },
263 {
264 role: "assistant" as const,
265 content: " of files.",
266 },
267 {
268 role: "assistant" as const,
269 content: "</think>",
270 },
271 {
272 role: "assistant" as const,
273 content: "\n",
274 },
275 {
276 role: "assistant" as const,
277 content: "I see, ",
278 },
279 {
280 role: "assistant" as const,
281 content: "the tool ",
282 },
283 {
284 role: "assistant" as const,
285 content: "found 3 ",
286 },
287 {
288 role: "assistant" as const,
289 content: "files.",
290 },
291 ],
292 };
293
294 newState = sessionSlice.reducer(newState, toolResponseAction);
295
296 expect(newState.history).toHaveLength(4);
297
298 // Check tool message
299 expect(newState.history[2].message.role).toBe("tool");
300 expect(newState.history[2].message.content).toBe(
301 "foo.txt\nbar.txt\nexample.php",
302 );
303
304 // Check response message
305 expect(newState.history[3].message.role).toBe("assistant");
306 expect(newState.history[3].message.content).toBe(
307 "I see, the tool found 3 files.",
308 );
309 expect(newState.history[3].reasoning?.text).toBe(
310 "Good, I received a list of files.",
311 );
312 expect(newState.history[3].reasoning?.active).toBe(false);
313 });
314 });
315
316 describe("Edge Cases", () => {
317 it("should handle empty history gracefully", () => {
318 const initialState = createInitialState();
319 initialState.history = [];
320
321 const action = {
322 type: "session/streamUpdate",
323 payload: [
324 {
325 role: "assistant" as const,
326 content: "Hello",
327 },
328 ],
329 };
330
331 const newState = sessionSlice.reducer(initialState, action);
332
333 // Should not crash and history should remain empty
334 expect(newState.history).toHaveLength(0);
335 });
336
337 it("should handle redacted thinking messages", () => {
338 const initialState = createInitialState();
339 const action = {
340 type: "session/streamUpdate",
341 payload: [
342 {
343 role: "thinking" as const,
344 content: "This should be hidden",
345 redactedThinking: true,
346 },
347 ],
348 };
349
350 const newState = sessionSlice.reducer(initialState, action);
351
352 expect(newState.history).toHaveLength(2);
353 expect(newState.history[1].message.role).toBe("thinking");
354 expect(newState.history[1].message.content).toBe(
355 "internal reasoning is hidden due to safety reasons",
356 );
357 expect((newState.history[1].message as any).redactedThinking).toBe(true);
358 });
359
360 it("should handle signature updates for thinking messages", () => {
361 const initialState = createInitialState();
362 // First add a thinking message
363 initialState.history.push({
364 message: {
365 role: "thinking",
366 content: "Some thinking",
367 id: "thinking-message",
368 },
369 contextItems: [],
370 });
371
372 const action = {
373 type: "session/streamUpdate",
374 payload: [
375 {
376 role: "thinking" as const,
377 signature: "test-signature",
378 },
379 ],
380 };
381
382 const newState = sessionSlice.reducer(initialState, action);
383
384 expect(newState.history).toHaveLength(2);
385 expect((newState.history[1].message as any).signature).toBe(
386 "test-signature",
387 );
388 });
389
390 it("should accumulate content for same role messages", () => {
391 const initialState = createInitialState();
392 // Add an assistant message first
393 initialState.history.push({
394 message: {
395 role: "assistant",
396 content: "Hello ",
397 id: "assistant-message",
398 },
399 contextItems: [],
400 });
401
402 const action = {
403 type: "session/streamUpdate",
404 payload: [
405 {
406 role: "assistant" as const,
407 content: "world!",
408 },
409 ],
410 };
411
412 const newState = sessionSlice.reducer(initialState, action);
413
414 expect(newState.history).toHaveLength(2);
415 expect(newState.history[1].message.content).toBe("Hello world!");
416 });
417 });
418});