|
| 1 | +import { describe, test, expect, vi } from "vitest" |
| 2 | +import { attemptCompletionTool } from "../attemptCompletionTool" |
| 3 | +import { Task } from "../../task/Task" |
| 4 | +import { TodoItem } from "@roo-code/types" |
| 5 | +import { ToolUse } from "../../../shared/tools" |
| 6 | + |
| 7 | +// Mock dependencies |
| 8 | +vi.mock("@roo-code/telemetry", () => ({ |
| 9 | + TelemetryService: { |
| 10 | + instance: { |
| 11 | + captureTaskCompleted: vi.fn(), |
| 12 | + }, |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock("../prompts/responses", () => ({ |
| 17 | + formatResponse: { |
| 18 | + toolError: vi.fn((message) => message), |
| 19 | + }, |
| 20 | +})) |
| 21 | + |
| 22 | +describe("attemptCompletionTool", () => { |
| 23 | + const createMockTask = (todoList?: TodoItem[]): Task => { |
| 24 | + const task = { |
| 25 | + todoList, |
| 26 | + consecutiveMistakeCount: 0, |
| 27 | + clineMessages: [], |
| 28 | + recordToolError: vi.fn(), |
| 29 | + sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing parameter error"), |
| 30 | + say: vi.fn(), |
| 31 | + emit: vi.fn(), |
| 32 | + getTokenUsage: vi.fn().mockReturnValue({}), |
| 33 | + toolUsage: {}, |
| 34 | + parentTask: null, |
| 35 | + ask: vi.fn(), |
| 36 | + userMessageContent: [], |
| 37 | + } as unknown as Task |
| 38 | + return task |
| 39 | + } |
| 40 | + |
| 41 | + const createMockToolUse = (result?: string, partial = false): ToolUse => ({ |
| 42 | + type: "tool_use", |
| 43 | + name: "attempt_completion", |
| 44 | + params: { result }, |
| 45 | + partial, |
| 46 | + }) |
| 47 | + |
| 48 | + const mockFunctions = { |
| 49 | + askApproval: vi.fn(), |
| 50 | + handleError: vi.fn(), |
| 51 | + pushToolResult: vi.fn(), |
| 52 | + removeClosingTag: vi.fn((tag, text) => text || ""), |
| 53 | + toolDescription: vi.fn(() => "[attempt_completion]"), |
| 54 | + askFinishSubTaskApproval: vi.fn(), |
| 55 | + } |
| 56 | + |
| 57 | + test("should block completion when there are pending todos", async () => { |
| 58 | + const todoList: TodoItem[] = [ |
| 59 | + { id: "1", content: "Complete task 1", status: "completed" }, |
| 60 | + { id: "2", content: "Complete task 2", status: "pending" }, |
| 61 | + ] |
| 62 | + const task = createMockTask(todoList) |
| 63 | + const toolUse = createMockToolUse("Task completed successfully") |
| 64 | + |
| 65 | + await attemptCompletionTool( |
| 66 | + task, |
| 67 | + toolUse, |
| 68 | + mockFunctions.askApproval, |
| 69 | + mockFunctions.handleError, |
| 70 | + mockFunctions.pushToolResult, |
| 71 | + mockFunctions.removeClosingTag, |
| 72 | + mockFunctions.toolDescription, |
| 73 | + mockFunctions.askFinishSubTaskApproval, |
| 74 | + ) |
| 75 | + |
| 76 | + expect(task.consecutiveMistakeCount).toBe(1) |
| 77 | + expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") |
| 78 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith( |
| 79 | + expect.stringContaining("Cannot attempt completion while there are incomplete todos"), |
| 80 | + ) |
| 81 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("Pending todos:")) |
| 82 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("- [ ] Complete task 2")) |
| 83 | + }) |
| 84 | + |
| 85 | + test("should block completion when there are in_progress todos", async () => { |
| 86 | + const todoList: TodoItem[] = [ |
| 87 | + { id: "1", content: "Complete task 1", status: "completed" }, |
| 88 | + { id: "2", content: "Complete task 2", status: "in_progress" }, |
| 89 | + ] |
| 90 | + const task = createMockTask(todoList) |
| 91 | + const toolUse = createMockToolUse("Task completed successfully") |
| 92 | + |
| 93 | + await attemptCompletionTool( |
| 94 | + task, |
| 95 | + toolUse, |
| 96 | + mockFunctions.askApproval, |
| 97 | + mockFunctions.handleError, |
| 98 | + mockFunctions.pushToolResult, |
| 99 | + mockFunctions.removeClosingTag, |
| 100 | + mockFunctions.toolDescription, |
| 101 | + mockFunctions.askFinishSubTaskApproval, |
| 102 | + ) |
| 103 | + |
| 104 | + expect(task.consecutiveMistakeCount).toBe(1) |
| 105 | + expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") |
| 106 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith( |
| 107 | + expect.stringContaining("Cannot attempt completion while there are incomplete todos"), |
| 108 | + ) |
| 109 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("In Progress todos:")) |
| 110 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("- [-] Complete task 2")) |
| 111 | + }) |
| 112 | + |
| 113 | + test("should allow completion when all todos are completed", async () => { |
| 114 | + const todoList: TodoItem[] = [ |
| 115 | + { id: "1", content: "Complete task 1", status: "completed" }, |
| 116 | + { id: "2", content: "Complete task 2", status: "completed" }, |
| 117 | + ] |
| 118 | + const task = createMockTask(todoList) |
| 119 | + const toolUse = createMockToolUse("Task completed successfully") |
| 120 | + |
| 121 | + // Mock the ask method to return yesButtonClicked for completion |
| 122 | + task.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" }) |
| 123 | + |
| 124 | + await attemptCompletionTool( |
| 125 | + task, |
| 126 | + toolUse, |
| 127 | + mockFunctions.askApproval, |
| 128 | + mockFunctions.handleError, |
| 129 | + mockFunctions.pushToolResult, |
| 130 | + mockFunctions.removeClosingTag, |
| 131 | + mockFunctions.toolDescription, |
| 132 | + mockFunctions.askFinishSubTaskApproval, |
| 133 | + ) |
| 134 | + |
| 135 | + expect(task.consecutiveMistakeCount).toBe(0) |
| 136 | + expect(task.recordToolError).not.toHaveBeenCalled() |
| 137 | + expect(task.say).toHaveBeenCalledWith("completion_result", "Task completed successfully", undefined, false) |
| 138 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith("") |
| 139 | + }) |
| 140 | + |
| 141 | + test("should allow completion when no todos exist", async () => { |
| 142 | + const task = createMockTask() // No todos |
| 143 | + const toolUse = createMockToolUse("Task completed successfully") |
| 144 | + |
| 145 | + // Mock the ask method to return yesButtonClicked for completion |
| 146 | + task.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" }) |
| 147 | + |
| 148 | + await attemptCompletionTool( |
| 149 | + task, |
| 150 | + toolUse, |
| 151 | + mockFunctions.askApproval, |
| 152 | + mockFunctions.handleError, |
| 153 | + mockFunctions.pushToolResult, |
| 154 | + mockFunctions.removeClosingTag, |
| 155 | + mockFunctions.toolDescription, |
| 156 | + mockFunctions.askFinishSubTaskApproval, |
| 157 | + ) |
| 158 | + |
| 159 | + expect(task.consecutiveMistakeCount).toBe(0) |
| 160 | + expect(task.recordToolError).not.toHaveBeenCalled() |
| 161 | + expect(task.say).toHaveBeenCalledWith("completion_result", "Task completed successfully", undefined, false) |
| 162 | + expect(mockFunctions.pushToolResult).toHaveBeenCalledWith("") |
| 163 | + }) |
| 164 | + |
| 165 | + test("should still block completion for missing result parameter", async () => { |
| 166 | + const task = createMockTask() |
| 167 | + const toolUse = createMockToolUse() // No result parameter |
| 168 | + |
| 169 | + await attemptCompletionTool( |
| 170 | + task, |
| 171 | + toolUse, |
| 172 | + mockFunctions.askApproval, |
| 173 | + mockFunctions.handleError, |
| 174 | + mockFunctions.pushToolResult, |
| 175 | + mockFunctions.removeClosingTag, |
| 176 | + mockFunctions.toolDescription, |
| 177 | + mockFunctions.askFinishSubTaskApproval, |
| 178 | + ) |
| 179 | + |
| 180 | + expect(task.consecutiveMistakeCount).toBe(1) |
| 181 | + expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") |
| 182 | + expect(task.sayAndCreateMissingParamError).toHaveBeenCalledWith("attempt_completion", "result") |
| 183 | + }) |
| 184 | +}) |
0 commit comments