| 1 | import { act } from "@testing-library/react"; |
| 2 | import { ChatMessage } from "core"; |
| 3 | import { renderWithProviders } from "../../../util/test/render"; |
| 4 | import { sendInputWithMockedResponse } from "../../../util/test/utils"; |
| 5 | import { Chat } from "../Chat"; |
| 6 | import { updateConfig } from "../../../redux/slices/configSlice"; |
| 7 | |
| 8 | describe("Parallel Tool Calls - Basic", () => { |
| 9 | const PARALLEL_TOOL_CALL_RESPONSE: ChatMessage[] = [ |
| 10 | { |
| 11 | role: "assistant", |
| 12 | content: "I'll call both tools in parallel.", |
| 13 | }, |
| 14 | { |
| 15 | role: "assistant", |
| 16 | content: "", |
| 17 | toolCalls: [ |
| 18 | { |
| 19 | id: "tool-call-1", |
| 20 | type: "function", |
| 21 | function: { |
| 22 | name: "read_file", |
| 23 | arguments: JSON.stringify({ filepath: "test.js" }), |
| 24 | }, |
| 25 | }, |
| 26 | { |
| 27 | id: "tool-call-2", |
| 28 | type: "function", |
| 29 | function: { |
| 30 | name: "write_file", |
| 31 | arguments: JSON.stringify({ |
| 32 | filepath: "output.js", |
| 33 | content: "test", |
| 34 | }), |
| 35 | }, |
| 36 | }, |
| 37 | ], |
| 38 | }, |
| 39 | ]; |
| 40 | |
| 41 | test("should handle assistant message with multiple tool calls", async () => { |
| 42 | const { ideMessenger, store } = await renderWithProviders(<Chat />); |
| 43 | |
| 44 | // Mock required responses |
| 45 | ideMessenger.responses["tools/evaluatePolicy"] = { |
| 46 | policy: "allowedWithPermission", |
| 47 | }; |
| 48 | ideMessenger.responses["context/getSymbolsForFiles"] = {}; |
| 49 | |
| 50 | // Setup mock model with direct Redux dispatch |
| 51 | const currentConfig = store.getState().config.config; |
| 52 | store.dispatch( |
| 53 | updateConfig({ |
| 54 | ...currentConfig, |
| 55 | selectedModelByRole: { |
| 56 | ...currentConfig.selectedModelByRole, |
| 57 | chat: { |
| 58 | model: "mock", |
| 59 | provider: "mock", |
| 60 | title: "Mock LLM", |
| 61 | underlyingProviderName: "mock", |
| 62 | }, |
| 63 | }, |
| 64 | modelsByRole: { |
| 65 | ...currentConfig.modelsByRole, |
| 66 | chat: [ |
| 67 | ...(currentConfig.modelsByRole.chat || []), |
| 68 | { |
| 69 | model: "mock", |
| 70 | provider: "mock", |
| 71 | title: "Mock LLM", |
| 72 | underlyingProviderName: "mock", |
| 73 | }, |
| 74 | ], |
| 75 | }, |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | await sendInputWithMockedResponse( |
| 80 | ideMessenger, |
| 81 | "Use both tools", |
| 82 | PARALLEL_TOOL_CALL_RESPONSE, |
| 83 | ); |
| 84 | |
| 85 | // Wait for streaming to complete |
| 86 | await act(async () => { |
| 87 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 88 | }); |
| 89 | |
| 90 | const state = store.getState(); |
| 91 | const history = state.session.history; |
| 92 | |
| 93 | // Should have user message and assistant message |
| 94 | expect(history).toHaveLength(2); |
| 95 | |
| 96 | // Find assistant message with tool calls |
| 97 | const assistantMessage = history.find( |
| 98 | (item) => |
| 99 | item.message.role === "assistant" && item.toolCallStates?.length, |
| 100 | ); |
| 101 | |
| 102 | expect(assistantMessage).toBeDefined(); |
| 103 | expect(assistantMessage!.toolCallStates).toHaveLength(2); |
| 104 | expect(assistantMessage!.toolCallStates![0].toolCallId).toBe("tool-call-1"); |
| 105 | expect(assistantMessage!.toolCallStates![1].toolCallId).toBe("tool-call-2"); |
| 106 | }); |
| 107 | }); |