-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathpresentAssistantMessage-custom-tool.spec.ts
More file actions
347 lines (304 loc) · 9.54 KB
/
Copy pathpresentAssistantMessage-custom-tool.spec.ts
File metadata and controls
347 lines (304 loc) · 9.54 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts
import { describe, it, expect, beforeEach, vi } from "vitest"
import { presentAssistantMessage } from "../presentAssistantMessage"
import { validateToolUse } from "../../tools/validateToolUse"
// Mock dependencies
vi.mock("../../task/Task")
vi.mock("../../tools/validateToolUse", () => ({
validateToolUse: vi.fn(),
isValidToolName: vi.fn((toolName: string) =>
["read_file", "write_to_file", "ask_followup_question", "attempt_completion", "use_mcp_tool"].includes(
toolName,
),
),
}))
// Mock custom tool registry - must be done inline without external variable references
vi.mock("@roo-code/core", () => ({
customToolRegistry: {
has: vi.fn(),
get: vi.fn(),
},
}))
import { customToolRegistry } from "@roo-code/core"
describe("presentAssistantMessage - Custom Tool Recording", () => {
let mockTask: any
beforeEach(() => {
// Reset all mocks
vi.clearAllMocks()
// 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: [],
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
consecutiveMistakeCount: 0,
clineMessages: [],
api: {
getModel: () => ({ id: "test-model", info: {} }),
},
recordToolUsage: vi.fn(),
recordToolError: vi.fn(),
toolRepetitionDetector: {
check: vi.fn().mockReturnValue({ allowExecution: true }),
},
providerRef: {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
experiments: {
customTools: true, // Enable by default
},
}),
}),
},
say: vi.fn().mockResolvedValue(undefined),
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
}
// Add pushToolResultToUserContent method after mockTask is created so it can reference mockTask
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
})
})
describe("Custom tool usage recording", () => {
it("should record custom tool usage as 'custom_tool' when experiment is enabled", async () => {
const toolCallId = "tool_call_custom_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "my_custom_tool",
params: { value: "test" },
partial: false,
},
]
// Mock customToolRegistry to recognize this as a custom tool
vi.mocked(customToolRegistry.has).mockReturnValue(true)
vi.mocked(customToolRegistry.get).mockReturnValue({
name: "my_custom_tool",
description: "A custom tool",
execute: vi.fn().mockResolvedValue("Custom tool result"),
})
await presentAssistantMessage(mockTask)
// Should record as "custom_tool", not "my_custom_tool"
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("custom_tool")
})
})
describe("Custom tool error recording", () => {
it("should record custom tool error as 'custom_tool'", async () => {
const toolCallId = "tool_call_custom_error_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "failing_custom_tool",
params: {},
partial: false,
},
]
// Mock customToolRegistry with a tool that throws an error
vi.mocked(customToolRegistry.has).mockReturnValue(true)
vi.mocked(customToolRegistry.get).mockReturnValue({
name: "failing_custom_tool",
description: "A failing custom tool",
execute: vi.fn().mockRejectedValue(new Error("Custom tool execution failed")),
})
await presentAssistantMessage(mockTask)
// Should record error as "custom_tool", not "failing_custom_tool"
expect(mockTask.recordToolError).toHaveBeenCalledWith("custom_tool", "Custom tool execution failed")
expect(mockTask.consecutiveMistakeCount).toBe(1)
})
})
describe("Regular tool recording", () => {
it("should record regular tool usage with actual tool name", async () => {
const toolCallId = "tool_call_read_file_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "read_file",
params: { path: "test.txt" },
partial: false,
},
]
// read_file is not a custom tool
vi.mocked(customToolRegistry.has).mockReturnValue(false)
await presentAssistantMessage(mockTask)
// Should record as "read_file", not "custom_tool"
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("read_file")
})
it("should record MCP tool usage as 'use_mcp_tool' (not custom_tool)", async () => {
const toolCallId = "tool_call_mcp_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "use_mcp_tool",
params: {
server_name: "test-server",
tool_name: "test-tool",
arguments: "{}",
},
partial: false,
},
]
vi.mocked(customToolRegistry.has).mockReturnValue(false)
// Mock MCP hub for use_mcp_tool
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
experiments: {
customTools: true,
},
}),
getMcpHub: () => ({
findServerNameBySanitizedName: () => "test-server",
executeToolCall: vi.fn().mockResolvedValue({ content: [{ type: "text", text: "result" }] }),
}),
}),
}
await presentAssistantMessage(mockTask)
// Should record as "use_mcp_tool", not "custom_tool"
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("use_mcp_tool")
})
})
describe("Custom tool experiment gate", () => {
it("should treat custom tool as unknown when experiment is disabled", async () => {
const toolCallId = "tool_call_disabled_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "my_custom_tool",
params: {},
partial: false,
},
]
// Mock provider state with customTools experiment DISABLED
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
experiments: {
customTools: false, // Disabled
},
}),
}),
}
// Even if registry recognizes it, experiment gate should prevent execution
vi.mocked(customToolRegistry.has).mockReturnValue(true)
vi.mocked(customToolRegistry.get).mockReturnValue({
name: "my_custom_tool",
description: "A custom tool",
execute: vi.fn().mockResolvedValue("Should not execute"),
})
await presentAssistantMessage(mockTask)
// Should be treated as unknown tool (not executed)
expect(mockTask.say).toHaveBeenCalledWith("error", "unknownToolError")
expect(mockTask.consecutiveMistakeCount).toBe(1)
// Custom tool should NOT have been executed
const getMock = vi.mocked(customToolRegistry.get)
if (getMock.mock.results.length > 0) {
const customTool = getMock.mock.results[0].value
if (customTool) {
expect(customTool.execute).not.toHaveBeenCalled()
}
}
})
it("should not call customToolRegistry.has() when experiment is disabled", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "tool_call_123",
name: "some_tool",
params: {},
partial: false,
},
]
// Disable experiment
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
experiments: {
customTools: false,
},
}),
}),
}
await presentAssistantMessage(mockTask)
// When experiment is off, shouldn't even check the registry
// (Code checks stateExperiments?.customTools before calling has())
expect(customToolRegistry.has).not.toHaveBeenCalled()
})
})
describe("Validation requirements", () => {
it("normalizes disabledTools aliases before validateToolUse", async () => {
const toolCallId = "tool_call_validation_alias_123"
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: toolCallId,
name: "some_unknown_tool",
params: {},
partial: false,
},
]
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
experiments: {
customTools: false,
},
disabledTools: ["search_and_replace"],
}),
}),
}
await presentAssistantMessage(mockTask)
const validateToolUseMock = vi.mocked(validateToolUse)
expect(validateToolUseMock).toHaveBeenCalled()
const toolRequirements = validateToolUseMock.mock.calls[0][3]
expect(toolRequirements).toMatchObject({
search_and_replace: false,
edit: false,
})
})
})
describe("Partial blocks", () => {
it("should not record usage for partial custom tool blocks", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "tool_call_partial_123",
name: "my_custom_tool",
params: { value: "test" },
partial: true, // Still streaming
},
]
vi.mocked(customToolRegistry.has).mockReturnValue(true)
await presentAssistantMessage(mockTask)
// Should not record usage for partial blocks
expect(mockTask.recordToolUsage).not.toHaveBeenCalled()
})
})
})