-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathaskFollowupQuestionTool.spec.ts
More file actions
517 lines (395 loc) · 17.9 KB
/
Copy pathaskFollowupQuestionTool.spec.ts
File metadata and controls
517 lines (395 loc) · 17.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { describe, it, expect, vi, beforeEach } from "vitest"
import type { Task } from "../../task/Task"
import type { ToolUse } from "../../../shared/tools"
import type { ToolCallbacks } from "../BaseTool"
import { AskFollowupQuestionTool, askFollowupQuestionTool } from "../AskFollowupQuestionTool"
import { formatResponse } from "../../prompts/responses"
import { NativeToolCallParser } from "../../assistant-message/NativeToolCallParser"
describe("AskFollowupQuestionTool", () => {
let tool: AskFollowupQuestionTool
let mockTask: Task
let mockCallbacks: ToolCallbacks
beforeEach(() => {
vi.clearAllMocks()
tool = new AskFollowupQuestionTool()
mockTask = {
consecutiveMistakeCount: 0,
recordToolError: vi.fn(),
didToolFailInCurrentTurn: false,
sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing parameter error"),
ask: vi.fn().mockResolvedValue({ text: "User answer", images: [] }),
say: vi.fn().mockResolvedValue(undefined),
} as unknown as Task
mockCallbacks = {
askApproval: vi.fn().mockResolvedValue(true),
handleError: vi.fn(),
pushToolResult: vi.fn(),
}
})
function createBlock(
params: { question?: string; follow_up?: Array<{ text: string; mode?: string }> },
partial = false,
): ToolUse<"ask_followup_question"> {
return {
type: "tool_use" as const,
name: "ask_followup_question" as const,
params: params as any,
partial,
nativeArgs: {
question: params.question ?? "",
follow_up: params.follow_up ?? [],
},
} as unknown as ToolUse<"ask_followup_question">
}
// ===== Parameter validation tests =====
it("should handle missing question parameter", async () => {
const params = { question: "", follow_up: [{ text: "Yes" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.consecutiveMistakeCount).toBe(1)
expect(mockTask.recordToolError).toHaveBeenCalledWith("ask_followup_question")
expect(mockTask.didToolFailInCurrentTurn).toBe(true)
expect(mockTask.sayAndCreateMissingParamError).toHaveBeenCalledWith("ask_followup_question", "question")
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith("Missing parameter error")
})
it("should handle missing follow_up parameter (null)", async () => {
const params = { question: "What?", follow_up: null as any }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.consecutiveMistakeCount).toBe(1)
expect(mockTask.recordToolError).toHaveBeenCalledWith("ask_followup_question")
expect(mockTask.didToolFailInCurrentTurn).toBe(true)
expect(mockTask.sayAndCreateMissingParamError).toHaveBeenCalledWith("ask_followup_question", "follow_up")
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith("Missing parameter error")
})
it("should handle missing follow_up parameter (undefined)", async () => {
const params = { question: "What?", follow_up: undefined as any }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.sayAndCreateMissingParamError).toHaveBeenCalledWith("ask_followup_question", "follow_up")
})
it("should handle follow_up that is not an array", async () => {
const params = { question: "What?", follow_up: "not-an-array" as any }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.consecutiveMistakeCount).toBe(1)
expect(mockTask.recordToolError).toHaveBeenCalledWith("ask_followup_question")
expect(mockTask.didToolFailInCurrentTurn).toBe(true)
expect(mockTask.sayAndCreateMissingParamError).toHaveBeenCalledWith("ask_followup_question", "follow_up")
})
// ===== Happy path tests =====
it("should ask the user a followup question with suggestions", async () => {
const params = {
question: "What would you like to do?",
follow_up: [{ text: "Option A" }, { text: "Option B" }],
}
await tool.execute(params, mockTask, mockCallbacks)
const expectedJson = JSON.stringify({
question: "What would you like to do?",
suggest: [
{ answer: "Option A", mode: undefined },
{ answer: "Option B", mode: undefined },
],
})
expect(mockTask.ask).toHaveBeenCalledWith("followup", expectedJson, false)
})
it("should include mode in suggestions when provided", async () => {
const params = {
question: "Switch mode?",
follow_up: [
{ text: "Use code mode", mode: "code" },
{ text: "Use architect mode", mode: "architect" },
],
}
await tool.execute(params, mockTask, mockCallbacks)
const expectedJson = JSON.stringify({
question: "Switch mode?",
suggest: [
{ answer: "Use code mode", mode: "code" },
{ answer: "Use architect mode", mode: "architect" },
],
})
expect(mockTask.ask).toHaveBeenCalledWith("followup", expectedJson, false)
})
it("should normalize malformed object mode values", async () => {
const params = {
question: "Switch mode?",
follow_up: [{ text: "Use code mode", mode: { mode_slug: "code" } }],
} as any
await tool.execute(params, mockTask, mockCallbacks)
const expectedJson = JSON.stringify({
question: "Switch mode?",
suggest: [{ answer: "Use code mode", mode: "code" }],
})
expect(mockTask.ask).toHaveBeenCalledWith("followup", expectedJson, false)
})
it("should say user_feedback and push tool result after user answers", async () => {
const params = {
question: "Which approach?",
follow_up: [{ text: "Approach 1" }],
}
;(mockTask.ask as any).mockResolvedValue({ text: "I'll go with Approach 1", images: [] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "I'll go with Approach 1", [])
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
formatResponse.toolResult(`<user_message>\nI'll go with Approach 1\n</user_message>`, []),
)
})
it("should pass images from user response to toolResult", async () => {
const params = {
question: "Look at this?",
follow_up: [{ text: "Yes" }],
}
const validDataUrl = "data:image/png;base64,iVBORw0KGgo="
;(mockTask.ask as any).mockResolvedValue({ text: "See image", images: [validDataUrl] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "See image", [validDataUrl])
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
formatResponse.toolResult("<user_message>\nSee image\n</user_message>", [validDataUrl]),
)
})
it("should reset consecutiveMistakeCount to 0 on success", async () => {
mockTask.consecutiveMistakeCount = 3
const params = { question: "What?", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.consecutiveMistakeCount).toBe(0)
})
it("should handle user providing empty text response", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
;(mockTask.ask as any).mockResolvedValue({ text: "", images: [] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "", [])
})
// ===== Error handling tests =====
it("should call handleError when task.ask throws", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
const error = new Error("ask failed")
;(mockTask.ask as any).mockRejectedValue(error)
await tool.execute(params, mockTask, mockCallbacks)
expect(mockCallbacks.handleError).toHaveBeenCalledWith("asking question", error)
})
it("should not call pushToolResult when task.ask throws", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
;(mockTask.ask as any).mockRejectedValue(new Error("fail"))
await tool.execute(params, mockTask, mockCallbacks)
expect(mockCallbacks.pushToolResult).not.toHaveBeenCalled()
})
it("should call handleError when task.say throws", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
const error = new Error("say failed")
;(mockTask.say as any).mockRejectedValue(error)
await tool.execute(params, mockTask, mockCallbacks)
expect(mockCallbacks.handleError).toHaveBeenCalledWith("asking question", error)
})
// ===== handlePartial tests =====
it("should show question during partial streaming via handlePartial", async () => {
const block = createBlock({ question: "What is your preference?", follow_up: [{ text: "A" }] }, true)
await tool.handlePartial(mockTask, block)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "What is your preference?", true)
})
it("should prefer nativeArgs.question over params.question in handlePartial", async () => {
const block = {
type: "tool_use" as const,
name: "ask_followup_question" as const,
params: { question: "old question" },
partial: true,
nativeArgs: { question: "new question from nativeArgs" },
} as unknown as ToolUse<"ask_followup_question">
await tool.handlePartial(mockTask, block)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "new question from nativeArgs", true)
})
it("should fall back to params.question when nativeArgs is undefined in handlePartial", async () => {
const block = {
type: "tool_use" as const,
name: "ask_followup_question" as const,
params: { question: "fallback question" },
partial: true,
nativeArgs: undefined,
} as unknown as ToolUse<"ask_followup_question">
await tool.handlePartial(mockTask, block)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "fallback question", true)
})
it("should use empty string when question is undefined in handlePartial", async () => {
const block = {
type: "tool_use" as const,
name: "ask_followup_question" as const,
params: {},
partial: true,
nativeArgs: undefined,
} as unknown as ToolUse<"ask_followup_question">
await tool.handlePartial(mockTask, block)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "", true)
})
it("should silently catch errors during handlePartial", async () => {
const block = createBlock({ question: "What?", follow_up: [{ text: "A" }] }, true)
;(mockTask.ask as any).mockRejectedValue(new Error("partial failed"))
// Should not throw
await expect(tool.handlePartial(mockTask, block)).resolves.toBeUndefined()
})
it("should pass block.partial flag to task.ask in handlePartial", async () => {
const blockPartial = createBlock({ question: "Q?", follow_up: [] }, true)
await tool.handlePartial(mockTask, blockPartial)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "Q?", true)
})
// ===== Edge case tests =====
it("should handle empty follow_up array", async () => {
const params = { question: "What?", follow_up: [] }
await tool.execute(params, mockTask, mockCallbacks)
const expectedJson = JSON.stringify({
question: "What?",
suggest: [],
})
expect(mockTask.ask).toHaveBeenCalledWith("followup", expectedJson, false)
})
it("should not call askApproval", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockCallbacks.askApproval).not.toHaveBeenCalled()
})
it("should have correct tool name", () => {
expect(tool.name).toBe("ask_followup_question")
})
// ===== Additional coverage tests =====
it("should pass multiple images from user response", async () => {
const params = { question: "Look at these?", follow_up: [{ text: "Yes" }] }
const img1 = "data:image/png;base64,iVBORw0KGgo="
const img2 = "data:image/jpeg;base64,/9j/4AAQSkZJRg=="
;(mockTask.ask as any).mockResolvedValue({ text: "See images", images: [img1, img2] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "See images", [img1, img2])
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
formatResponse.toolResult("<user_message>\nSee images\n</user_message>", [img1, img2]),
)
})
it("should pass empty images array when user provides no images", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
;(mockTask.ask as any).mockResolvedValue({ text: "answer", images: [] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "answer", [])
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
formatResponse.toolResult("<user_message>\nanswer\n</user_message>", []),
)
})
it("should handle null text in user response by using empty string", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
;(mockTask.ask as any).mockResolvedValue({ text: null, images: [] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "", [])
})
it("should increment consecutiveMistakeCount from existing value", async () => {
mockTask.consecutiveMistakeCount = 2
const params = { question: "", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.consecutiveMistakeCount).toBe(3)
})
it("should not set didToolFailInCurrentTurn on success", async () => {
mockTask.didToolFailInCurrentTurn = false
const params = { question: "What?", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.didToolFailInCurrentTurn).toBe(false)
})
it("should not call recordToolError on success", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.recordToolError).not.toHaveBeenCalled()
})
it("should handle question with only whitespace (truthy)", async () => {
const params = { question: " ", follow_up: [{ text: "A" }] }
await tool.execute(params, mockTask, mockCallbacks)
// Whitespace-only question is truthy, so it should proceed normally
expect(mockTask.ask).toHaveBeenCalledWith("followup", expect.stringContaining(" "), false)
expect(mockTask.recordToolError).not.toHaveBeenCalled()
})
it("should handle follow_up with mixed mode and undefined mode suggestions", async () => {
const params = {
question: "Choose?",
follow_up: [
{ text: "With mode", mode: "code" },
{ text: "Without mode" },
{ text: "Another with mode", mode: "architect" },
],
}
await tool.execute(params, mockTask, mockCallbacks)
const expectedJson = JSON.stringify({
question: "Choose?",
suggest: [
{ answer: "With mode", mode: "code" },
{ answer: "Without mode", mode: undefined },
{ answer: "Another with mode", mode: "architect" },
],
})
expect(mockTask.ask).toHaveBeenCalledWith("followup", expectedJson, false)
})
it("should handle handlePartial with nativeArgs containing empty string question", async () => {
const block = {
type: "tool_use" as const,
name: "ask_followup_question" as const,
params: { question: "should not use this" },
partial: true,
nativeArgs: { question: "" },
} as unknown as ToolUse<"ask_followup_question">
await tool.handlePartial(mockTask, block)
// nativeArgs.question is "" which is used directly (not a fallback since ?? only falls back for null/undefined)
expect(mockTask.ask).toHaveBeenCalledWith("followup", "", true)
})
it("should handle execute when task.ask resolves with undefined text", async () => {
const params = { question: "What?", follow_up: [{ text: "A" }] }
;(mockTask.ask as any).mockResolvedValue({ text: undefined, images: [] })
await tool.execute(params, mockTask, mockCallbacks)
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "", [])
// pushToolResult should receive normalized empty string, NOT "undefined"
const toolResultArg = (mockCallbacks.pushToolResult as any).mock.calls[0][0]
expect(toolResultArg).not.toContain("undefined")
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
formatResponse.toolResult("<user_message>\n\n</user_message>", []),
)
})
it("should export a singleton askFollowupQuestionTool instance", () => {
expect(askFollowupQuestionTool).toBeInstanceOf(AskFollowupQuestionTool)
expect(askFollowupQuestionTool.name).toBe("ask_followup_question")
})
// ===== NativeToolCallParser integration tests for ask_followup_question =====
describe("NativeToolCallParser.createPartialToolUse for ask_followup_question", () => {
beforeEach(() => {
NativeToolCallParser.clearAllStreamingToolCalls()
NativeToolCallParser.clearRawChunkState()
})
it("should build nativeArgs with question and follow_up during streaming", () => {
// Start a streaming tool call
NativeToolCallParser.startStreamingToolCall("call_123", "ask_followup_question")
// Simulate streaming JSON chunks
const chunk1 = '{"question":"What would you like?","follow_up":[{"text":"Option 1","mode":"code"}'
const result1 = NativeToolCallParser.processStreamingChunk("call_123", chunk1)
expect(result1).not.toBeNull()
expect(result1?.name).toBe("ask_followup_question")
expect(result1?.params.question).toBe("What would you like?")
expect(result1?.nativeArgs).toBeDefined()
// Use type assertion to access the specific fields
const nativeArgs = result1?.nativeArgs as {
question: string
follow_up?: Array<{ text: string; mode?: string }>
}
expect(nativeArgs?.question).toBe("What would you like?")
// partial-json should parse the incomplete array
expect(nativeArgs?.follow_up).toBeDefined()
})
it("should finalize with complete nativeArgs", () => {
NativeToolCallParser.startStreamingToolCall("call_456", "ask_followup_question")
// Add complete JSON
const completeJson =
'{"question":"Choose an option","follow_up":[{"text":"Yes","mode":"code"},{"text":"No","mode":null}]}'
NativeToolCallParser.processStreamingChunk("call_456", completeJson)
const result = NativeToolCallParser.finalizeStreamingToolCall("call_456")
expect(result).not.toBeNull()
expect(result?.type).toBe("tool_use")
expect(result?.name).toBe("ask_followup_question")
expect(result?.partial).toBe(false)
// Type guard: regular tools have type 'tool_use', MCP tools have type 'mcp_tool_use'
if (result?.type === "tool_use") {
expect(result.nativeArgs).toEqual({
question: "Choose an option",
follow_up: [
{ text: "Yes", mode: "code" },
{ text: "No", mode: null },
],
})
}
})
})
})