| 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 - Streaming", () => { |
| 9 | test("should handle streaming deltas for multiple tool calls", async () => { |
| 10 | const { ideMessenger, store } = await renderWithProviders(<Chat />); |
| 11 | |
| 12 | // Mock required responses |
| 13 | ideMessenger.responses["tools/evaluatePolicy"] = { |
| 14 | policy: "allowedWithPermission", |
| 15 | }; |
| 16 | ideMessenger.responses["context/getSymbolsForFiles"] = {}; |
| 17 | |
| 18 | // Setup mock model with direct Redux dispatch |
| 19 | const currentConfig = store.getState().config.config; |
| 20 | store.dispatch( |
| 21 | updateConfig({ |
| 22 | ...currentConfig, |
| 23 | selectedModelByRole: { |
| 24 | ...currentConfig.selectedModelByRole, |
| 25 | chat: { |
| 26 | model: "mock", |
| 27 | provider: "mock", |
| 28 | title: "Mock LLM", |
| 29 | underlyingProviderName: "mock", |
| 30 | }, |
| 31 | }, |
| 32 | modelsByRole: { |
| 33 | ...currentConfig.modelsByRole, |
| 34 | chat: [ |
| 35 | ...(currentConfig.modelsByRole.chat || []), |
| 36 | { |
| 37 | model: "mock", |
| 38 | provider: "mock", |
| 39 | title: "Mock LLM", |
| 40 | underlyingProviderName: "mock", |
| 41 | }, |
| 42 | ], |
| 43 | }, |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | // Simulate streaming behavior with deltas |
| 48 | const STREAMING_RESPONSE: ChatMessage[] = [ |
| 49 | { |
| 50 | role: "assistant", |
| 51 | content: "I'll call both tools in parallel.", |
| 52 | }, |
| 53 | { |
| 54 | role: "assistant", |
| 55 | content: "", |
| 56 | toolCalls: [ |
| 57 | { |
| 58 | id: "tool-call-1", |
| 59 | type: "function", |
| 60 | function: { |
| 61 | name: "read_file", |
| 62 | arguments: "", |
| 63 | }, |
| 64 | }, |
| 65 | ], |
| 66 | }, |
| 67 | { |
| 68 | role: "assistant", |
| 69 | content: "", |
| 70 | toolCalls: [ |
| 71 | { |
| 72 | id: "tool-call-1", |
| 73 | type: "function", |
| 74 | function: { |
| 75 | name: "read_file", |
| 76 | arguments: '{"filepath": "test.js"}', |
| 77 | }, |
| 78 | }, |
| 79 | ], |
| 80 | }, |
| 81 | { |
| 82 | role: "assistant", |
| 83 | content: "", |
| 84 | toolCalls: [ |
| 85 | { |
| 86 | id: "tool-call-2", |
| 87 | type: "function", |
| 88 | function: { |
| 89 | name: "write_file", |
| 90 | arguments: '{"filepath": "output.js", "content": "test"}', |
| 91 | }, |
| 92 | }, |
| 93 | ], |
| 94 | }, |
| 95 | ]; |
| 96 | |
| 97 | await sendInputWithMockedResponse( |
| 98 | ideMessenger, |
| 99 | "Use both tools with streaming", |
| 100 | STREAMING_RESPONSE, |
| 101 | ); |
| 102 | |
| 103 | // Wait for streaming to complete |
| 104 | await act(async () => { |
| 105 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 106 | }); |
| 107 | |
| 108 | const state = store.getState(); |
| 109 | const history = state.session.history; |
| 110 | |
| 111 | // Should have only one assistant message after streaming |
| 112 | const assistantMessages = history.filter( |
| 113 | (item) => item.message.role === "assistant", |
| 114 | ); |
| 115 | expect(assistantMessages).toHaveLength(1); |
| 116 | |
| 117 | const assistantMessage = assistantMessages[0]; |
| 118 | |
| 119 | // Should preserve original content |
| 120 | expect(assistantMessage.message.content).toContain( |
| 121 | "I'll call both tools in parallel", |
| 122 | ); |
| 123 | |
| 124 | // Should have both tool calls after streaming completes |
| 125 | expect(assistantMessage.toolCallStates).toHaveLength(2); |
| 126 | |
| 127 | // Verify tool calls are properly constructed |
| 128 | const toolCallIds = assistantMessage.toolCallStates!.map( |
| 129 | (tc) => tc.toolCallId, |
| 130 | ); |
| 131 | expect(toolCallIds).toContain("tool-call-1"); |
| 132 | expect(toolCallIds).toContain("tool-call-2"); |
| 133 | }); |
| 134 | }); |