|
| 1 | +import type { Mock } from "vitest" |
| 2 | + |
1 | 3 | import { RooCodeEventName, TodoItem } from "@roo-code/types" |
2 | 4 |
|
3 | | -import { AttemptCompletionToolUse } from "../../../shared/tools" |
| 5 | +import { AskApproval, AttemptCompletionToolUse, HandleError, PushToolResult } from "../../../shared/tools" |
4 | 6 |
|
5 | 7 | // Mock the formatResponse module before importing the tool |
6 | 8 | vi.mock("../../prompts/responses", () => ({ |
@@ -44,28 +46,35 @@ import * as vscode from "vscode" |
44 | 46 |
|
45 | 47 | describe("attemptCompletionTool", () => { |
46 | 48 | let mockTask: Partial<Task> |
47 | | - let mockPushToolResult: ReturnType<typeof vi.fn> |
48 | | - let mockAskApproval: ReturnType<typeof vi.fn> |
49 | | - let mockHandleError: ReturnType<typeof vi.fn> |
50 | | - let mockToolDescription: ReturnType<typeof vi.fn> |
51 | | - let mockAskFinishSubTaskApproval: ReturnType<typeof vi.fn> |
52 | | - let mockGetConfiguration: ReturnType<typeof vi.fn> |
53 | | - |
54 | | - beforeEach(() => { |
55 | | - mockCaptureTaskCompleted.mockReset() |
56 | | - mockPushToolResult = vi.fn() |
57 | | - mockAskApproval = vi.fn() |
58 | | - mockHandleError = vi.fn() |
59 | | - mockToolDescription = vi.fn() |
60 | | - mockAskFinishSubTaskApproval = vi.fn() |
61 | | - mockGetConfiguration = vi.fn(() => ({ |
62 | | - get: vi.fn((key: string, defaultValue: any) => { |
| 49 | + let mockPushToolResult: Mock<PushToolResult> |
| 50 | + let mockAskApproval: Mock<AskApproval> |
| 51 | + let mockHandleError: Mock<HandleError> |
| 52 | + let mockToolDescription: Mock<() => string> |
| 53 | + let mockAskFinishSubTaskApproval: Mock<() => Promise<boolean>> |
| 54 | + let mockGetConfiguration: Mock<typeof vscode.workspace.getConfiguration> |
| 55 | + const workspaceConfigurationWithOpenTodoCompletionPrevention = ( |
| 56 | + preventCompletionWithOpenTodos: boolean, |
| 57 | + ): vscode.WorkspaceConfiguration => |
| 58 | + ({ |
| 59 | + get: <T>(key: string, defaultValue: T): T => { |
63 | 60 | if (key === "preventCompletionWithOpenTodos") { |
64 | | - return defaultValue // Default to false unless overridden in test |
| 61 | + return preventCompletionWithOpenTodos as T |
65 | 62 | } |
| 63 | + |
66 | 64 | return defaultValue |
67 | | - }), |
68 | | - })) |
| 65 | + }, |
| 66 | + }) as vscode.WorkspaceConfiguration |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + mockCaptureTaskCompleted.mockReset() |
| 70 | + mockPushToolResult = vi.fn<PushToolResult>() |
| 71 | + mockAskApproval = vi.fn<AskApproval>() |
| 72 | + mockHandleError = vi.fn<HandleError>() |
| 73 | + mockToolDescription = vi.fn<() => string>() |
| 74 | + mockAskFinishSubTaskApproval = vi.fn<() => Promise<boolean>>() |
| 75 | + mockGetConfiguration = vi.fn<typeof vscode.workspace.getConfiguration>(() => |
| 76 | + workspaceConfigurationWithOpenTodoCompletionPrevention(false), |
| 77 | + ) |
69 | 78 |
|
70 | 79 | // Setup vscode mock |
71 | 80 | vi.mocked(vscode.workspace.getConfiguration).mockImplementation(mockGetConfiguration) |
@@ -182,14 +191,7 @@ describe("attemptCompletionTool", () => { |
182 | 191 | mockTask.todoList = todosWithPending |
183 | 192 |
|
184 | 193 | // Enable the setting to prevent completion with open todos |
185 | | - mockGetConfiguration.mockReturnValue({ |
186 | | - get: vi.fn((key: string, defaultValue: any) => { |
187 | | - if (key === "preventCompletionWithOpenTodos") { |
188 | | - return true // Setting is enabled |
189 | | - } |
190 | | - return defaultValue |
191 | | - }), |
192 | | - }) |
| 194 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(true)) |
193 | 195 |
|
194 | 196 | const callbacks: AttemptCompletionCallbacks = { |
195 | 197 | askApproval: mockAskApproval, |
@@ -224,14 +226,7 @@ describe("attemptCompletionTool", () => { |
224 | 226 | mockTask.todoList = todosWithInProgress |
225 | 227 |
|
226 | 228 | // Enable the setting to prevent completion with open todos |
227 | | - mockGetConfiguration.mockReturnValue({ |
228 | | - get: vi.fn((key: string, defaultValue: any) => { |
229 | | - if (key === "preventCompletionWithOpenTodos") { |
230 | | - return true // Setting is enabled |
231 | | - } |
232 | | - return defaultValue |
233 | | - }), |
234 | | - }) |
| 229 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(true)) |
235 | 230 |
|
236 | 231 | const callbacks: AttemptCompletionCallbacks = { |
237 | 232 | askApproval: mockAskApproval, |
@@ -267,14 +262,7 @@ describe("attemptCompletionTool", () => { |
267 | 262 | mockTask.todoList = mixedTodos |
268 | 263 |
|
269 | 264 | // Enable the setting to prevent completion with open todos |
270 | | - mockGetConfiguration.mockReturnValue({ |
271 | | - get: vi.fn((key: string, defaultValue: any) => { |
272 | | - if (key === "preventCompletionWithOpenTodos") { |
273 | | - return true // Setting is enabled |
274 | | - } |
275 | | - return defaultValue |
276 | | - }), |
277 | | - }) |
| 265 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(true)) |
278 | 266 |
|
279 | 267 | const callbacks: AttemptCompletionCallbacks = { |
280 | 268 | askApproval: mockAskApproval, |
@@ -309,14 +297,7 @@ describe("attemptCompletionTool", () => { |
309 | 297 | mockTask.todoList = todosWithPending |
310 | 298 |
|
311 | 299 | // Ensure the setting is disabled (default behavior) |
312 | | - mockGetConfiguration.mockReturnValue({ |
313 | | - get: vi.fn((key: string, defaultValue: any) => { |
314 | | - if (key === "preventCompletionWithOpenTodos") { |
315 | | - return false // Setting is disabled |
316 | | - } |
317 | | - return defaultValue |
318 | | - }), |
319 | | - }) |
| 300 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(false)) |
320 | 301 |
|
321 | 302 | const callbacks: AttemptCompletionCallbacks = { |
322 | 303 | askApproval: mockAskApproval, |
@@ -352,14 +333,7 @@ describe("attemptCompletionTool", () => { |
352 | 333 | mockTask.todoList = todosWithPending |
353 | 334 |
|
354 | 335 | // Enable the setting |
355 | | - mockGetConfiguration.mockReturnValue({ |
356 | | - get: vi.fn((key: string, defaultValue: any) => { |
357 | | - if (key === "preventCompletionWithOpenTodos") { |
358 | | - return true // Setting is enabled |
359 | | - } |
360 | | - return defaultValue |
361 | | - }), |
362 | | - }) |
| 336 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(true)) |
363 | 337 |
|
364 | 338 | const callbacks: AttemptCompletionCallbacks = { |
365 | 339 | askApproval: mockAskApproval, |
@@ -395,14 +369,7 @@ describe("attemptCompletionTool", () => { |
395 | 369 | mockTask.todoList = completedTodos |
396 | 370 |
|
397 | 371 | // Enable the setting |
398 | | - mockGetConfiguration.mockReturnValue({ |
399 | | - get: vi.fn((key: string, defaultValue: any) => { |
400 | | - if (key === "preventCompletionWithOpenTodos") { |
401 | | - return true // Setting is enabled |
402 | | - } |
403 | | - return defaultValue |
404 | | - }), |
405 | | - }) |
| 372 | + mockGetConfiguration.mockReturnValue(workspaceConfigurationWithOpenTodoCompletionPrevention(true)) |
406 | 373 |
|
407 | 374 | const callbacks: AttemptCompletionCallbacks = { |
408 | 375 | askApproval: mockAskApproval, |
|
0 commit comments