This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathpresentAssistantMessage-parallel-tools.spec.ts
More file actions
307 lines (265 loc) · 8.94 KB
/
Copy pathpresentAssistantMessage-parallel-tools.spec.ts
File metadata and controls
307 lines (265 loc) · 8.94 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-parallel-tools.spec.ts
import { describe, it, expect, beforeEach, vi } from "vitest"
import { presentAssistantMessage } from "../presentAssistantMessage"
// Mock dependencies
vi.mock("../../task/Task")
vi.mock("../../tools/validateToolUse", () => ({
validateToolUse: vi.fn(),
isValidToolName: vi.fn(() => true),
}))
vi.mock("@roo-code/telemetry", () => ({
TelemetryService: {
instance: {
captureToolUsage: vi.fn(),
captureConsecutiveMistakeError: vi.fn(),
},
},
}))
// Mock the tool handlers to avoid complex setup
vi.mock("../../tools/ListFilesTool", () => ({
listFilesTool: {
handle: vi.fn().mockImplementation(async (cline, block, callbacks) => {
// Simulate async tool execution - tool result is pushed asynchronously
await Promise.resolve()
callbacks.pushToolResult("list_files result")
}),
},
}))
vi.mock("../../tools/ReadFileTool", () => ({
readFileTool: {
handle: vi.fn().mockImplementation(async (cline, block, callbacks) => {
// Simulate async tool execution - tool result is pushed asynchronously
await Promise.resolve()
callbacks.pushToolResult("read_file result")
}),
getReadFileToolDescription: vi.fn(() => "[read_file]"),
},
}))
describe("presentAssistantMessage - Parallel Tool Execution Timing", () => {
let mockTask: any
beforeEach(() => {
// Create a mock Task with minimal properties needed for testing
mockTask = {
taskId: "test-task-id",
instanceId: "test-instance",
abort: false,
presentAssistantMessageLocked: false,
presentAssistantMessageHasPendingUpdates: false,
currentStreamingContentIndex: 0,
assistantMessageContent: [],
userMessageContent: [],
userMessageContentReady: false,
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
consecutiveMistakeCount: 0,
clineMessages: [],
api: {
getModel: () => ({ id: "test-model", info: {} }),
},
browserSession: {
closeBrowser: vi.fn().mockResolvedValue(undefined),
},
recordToolUsage: vi.fn(),
recordToolError: vi.fn(),
toolRepetitionDetector: {
check: vi.fn().mockReturnValue({ allowExecution: true }),
},
providerRef: {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
}),
}),
},
say: vi.fn().mockResolvedValue(undefined),
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
}
// Add pushToolResultToUserContent method
mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: any) => {
const existingResult = mockTask.userMessageContent.find(
(block: any) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
)
if (existingResult) {
return false
}
mockTask.userMessageContent.push(toolResult)
return true
})
})
it("should NOT set userMessageContentReady until all tool_results are collected for parallel tools", async () => {
// Set up multiple tool_use blocks (parallel tool calls)
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "tool_call_1",
name: "list_files",
params: { path: "/test" },
partial: false,
},
{
type: "tool_use",
id: "tool_call_2",
name: "read_file",
params: { path: "/test/file.txt" },
partial: false,
},
]
mockTask.didCompleteReadingStream = true
// Process first tool
await presentAssistantMessage(mockTask)
// After processing first tool, userMessageContentReady should NOT be true
// because tool_call_2 doesn't have a tool_result yet
// Note: Due to how the mock is set up, the first tool should push its result
// but the second tool hasn't been processed yet
expect(mockTask.userMessageContent.length).toBeGreaterThanOrEqual(0)
// If only one tool_result exists for two tool_use blocks, userMessageContentReady should be false
if (mockTask.userMessageContent.length === 1) {
expect(mockTask.userMessageContentReady).toBe(false)
}
})
it("should set userMessageContentReady when all tool_results are collected", async () => {
// Set up a single tool_use block
const toolCallId = "tool_call_single"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "list_files",
params: { path: "/test" },
partial: false,
},
]
mockTask.didCompleteReadingStream = true
await presentAssistantMessage(mockTask)
// After the tool executes and pushes its result, userMessageContentReady should be true
// because there's 1 tool_use and 1 tool_result
const toolResultCount = mockTask.userMessageContent.filter((b: any) => b.type === "tool_result").length
if (toolResultCount === 1) {
expect(mockTask.userMessageContentReady).toBe(true)
}
})
it("should handle text-only content without waiting for tool_results", async () => {
// Set up a text-only content block (no tools)
mockTask.assistantMessageContent = [
{
type: "text",
content: "Hello, this is a text response",
partial: false,
},
]
mockTask.didCompleteReadingStream = true
await presentAssistantMessage(mockTask)
// With no tool_use blocks, userMessageContentReady should be true after processing text
expect(mockTask.userMessageContentReady).toBe(true)
})
it("should wait for tool_results even when didRejectTool is true", async () => {
// Set up multiple tool_use blocks
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "tool_call_rejected_1",
name: "list_files",
params: { path: "/test" },
partial: false,
},
{
type: "tool_use",
id: "tool_call_rejected_2",
name: "read_file",
params: { path: "/test/file.txt" },
partial: false,
},
]
mockTask.didRejectTool = true
mockTask.didCompleteReadingStream = true
await presentAssistantMessage(mockTask)
// When didRejectTool is true, error tool_results should be pushed for each tool
// Both should have tool_results (skipped messages)
const toolResults = mockTask.userMessageContent.filter((b: any) => b.type === "tool_result")
// The function should have pushed error tool_results for rejected tools
expect(toolResults.length).toBeGreaterThan(0)
// If all tool_results are collected, userMessageContentReady should be true
const toolUseCount = mockTask.assistantMessageContent.filter(
(b: any) => b.type === "tool_use" || b.type === "mcp_tool_use",
).length
if (toolResults.length >= toolUseCount) {
expect(mockTask.userMessageContentReady).toBe(true)
}
})
it("should not set userMessageContentReady if stream is not complete", async () => {
// Set up a tool_use block
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "tool_call_stream",
name: "list_files",
params: { path: "/test" },
partial: false,
},
]
// Stream is NOT complete
mockTask.didCompleteReadingStream = false
await presentAssistantMessage(mockTask)
// Even if the tool executed, userMessageContentReady should NOT be true
// because the stream hasn't completed yet (more content may arrive)
// Note: The fix specifically checks both conditions
expect(mockTask.userMessageContentReady).toBe(false)
})
it("should handle mcp_tool_use blocks the same as tool_use blocks", async () => {
// Set up an mcp_tool_use block (MCP tool)
mockTask.assistantMessageContent = [
{
type: "mcp_tool_use",
id: "mcp_tool_call_1",
name: "mcp_server_tool",
serverName: "test_server",
toolName: "test_tool",
arguments: {},
partial: false,
},
]
mockTask.didRejectTool = true // Use rejection to get a simple tool_result
mockTask.didCompleteReadingStream = true
await presentAssistantMessage(mockTask)
// The mcp_tool_use should be treated similarly - needs tool_result before ready
const toolResults = mockTask.userMessageContent.filter((b: any) => b.type === "tool_result")
const toolUseCount = mockTask.assistantMessageContent.filter(
(b: any) => b.type === "tool_use" || b.type === "mcp_tool_use",
).length
// If all tool_results are collected, userMessageContentReady should be true
if (toolResults.length >= toolUseCount) {
expect(mockTask.userMessageContentReady).toBe(true)
}
})
it("should correctly count mixed tool_use and mcp_tool_use blocks", async () => {
// Set up mixed tool blocks
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "regular_tool_1",
name: "list_files",
params: { path: "/test" },
partial: false,
},
{
type: "mcp_tool_use",
id: "mcp_tool_1",
name: "mcp_server_tool",
serverName: "test_server",
toolName: "test_tool",
arguments: {},
partial: false,
},
]
mockTask.didRejectTool = true // Simplify by using rejection
mockTask.didCompleteReadingStream = true
await presentAssistantMessage(mockTask)
// Both tool types should require tool_results
const toolUseCount = mockTask.assistantMessageContent.filter(
(b: any) => b.type === "tool_use" || b.type === "mcp_tool_use",
).length
expect(toolUseCount).toBe(2)
})
})