|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import { ClineProvider } from "../ClineProvider" |
| 4 | +import { ContextProxy } from "../../config/ContextProxy" |
| 5 | +import { Task } from "../../task/Task" |
| 6 | + |
| 7 | +// Mock vscode module |
| 8 | +vi.mock("vscode", () => ({ |
| 9 | + ExtensionContext: vi.fn(), |
| 10 | + ExtensionMode: { Development: 1, Production: 2, Test: 3 }, |
| 11 | + Uri: { |
| 12 | + file: vi.fn((path: string) => ({ fsPath: path, toString: () => path })), |
| 13 | + parse: vi.fn((uri: string) => ({ fsPath: uri })), |
| 14 | + }, |
| 15 | + workspace: { |
| 16 | + getConfiguration: vi.fn(() => ({ |
| 17 | + get: vi.fn(), |
| 18 | + update: vi.fn(), |
| 19 | + })), |
| 20 | + workspaceFolders: undefined, |
| 21 | + onDidChangeConfiguration: vi.fn(), |
| 22 | + }, |
| 23 | + window: { |
| 24 | + showErrorMessage: vi.fn(), |
| 25 | + showInformationMessage: vi.fn(), |
| 26 | + createTextEditorDecorationType: vi.fn(() => ({ |
| 27 | + dispose: vi.fn(), |
| 28 | + })), |
| 29 | + onDidChangeActiveTextEditor: vi.fn(), |
| 30 | + }, |
| 31 | + env: { |
| 32 | + uriScheme: "vscode", |
| 33 | + language: "en", |
| 34 | + machineId: "test-machine-id", |
| 35 | + sessionId: "test-session-id", |
| 36 | + appName: "Visual Studio Code", |
| 37 | + }, |
| 38 | + version: "1.0.0", |
| 39 | + commands: { |
| 40 | + executeCommand: vi.fn(), |
| 41 | + }, |
| 42 | + Range: vi.fn(), |
| 43 | + Position: vi.fn(), |
| 44 | +})) |
| 45 | + |
| 46 | +// Mock other dependencies |
| 47 | +vi.mock("../../config/ContextProxy") |
| 48 | +vi.mock("../../task/Task") |
| 49 | +vi.mock("../../../services/mcp/McpServerManager", () => ({ |
| 50 | + McpServerManager: { |
| 51 | + getInstance: vi.fn().mockResolvedValue(undefined), |
| 52 | + unregisterProvider: vi.fn(), |
| 53 | + }, |
| 54 | +})) |
| 55 | +vi.mock("../../../services/marketplace", () => ({ |
| 56 | + MarketplaceManager: vi.fn().mockImplementation(() => ({ |
| 57 | + getMarketplaceItems: vi.fn().mockResolvedValue({ organizationMcps: [], marketplaceItems: [], errors: [] }), |
| 58 | + getInstallationMetadata: vi.fn().mockResolvedValue({ project: {}, global: {} }), |
| 59 | + cleanup: vi.fn(), |
| 60 | + })), |
| 61 | +})) |
| 62 | +vi.mock("../../config/CustomModesManager", () => ({ |
| 63 | + CustomModesManager: vi.fn().mockImplementation(() => ({ |
| 64 | + getCustomModes: vi.fn().mockResolvedValue([]), |
| 65 | + dispose: vi.fn(), |
| 66 | + })), |
| 67 | +})) |
| 68 | +vi.mock("../../config/ProviderSettingsManager", () => ({ |
| 69 | + ProviderSettingsManager: vi.fn().mockImplementation(() => ({ |
| 70 | + listConfig: vi.fn().mockResolvedValue([]), |
| 71 | + getModeConfigId: vi.fn().mockResolvedValue(undefined), |
| 72 | + resetAllConfigs: vi.fn(), |
| 73 | + })), |
| 74 | +})) |
| 75 | +vi.mock("../../../integrations/workspace/WorkspaceTracker", () => ({ |
| 76 | + default: vi.fn().mockImplementation(() => ({ |
| 77 | + dispose: vi.fn(), |
| 78 | + })), |
| 79 | +})) |
| 80 | +vi.mock("@roo-code/telemetry", () => ({ |
| 81 | + TelemetryService: { |
| 82 | + instance: { |
| 83 | + setProvider: vi.fn(), |
| 84 | + captureCodeActionUsed: vi.fn(), |
| 85 | + captureModeSwitch: vi.fn(), |
| 86 | + }, |
| 87 | + }, |
| 88 | +})) |
| 89 | +vi.mock("@roo-code/cloud", () => ({ |
| 90 | + CloudService: { |
| 91 | + hasInstance: vi.fn().mockReturnValue(false), |
| 92 | + instance: { |
| 93 | + isAuthenticated: vi.fn().mockReturnValue(false), |
| 94 | + getAllowList: vi.fn().mockResolvedValue("*"), |
| 95 | + getUserInfo: vi.fn().mockReturnValue(null), |
| 96 | + getOrganizationSettings: vi.fn().mockReturnValue(null), |
| 97 | + isTaskSyncEnabled: vi.fn().mockReturnValue(false), |
| 98 | + canShareTask: vi.fn().mockResolvedValue(false), |
| 99 | + getUserSettings: vi.fn().mockReturnValue(null), |
| 100 | + }, |
| 101 | + }, |
| 102 | + BridgeOrchestrator: { |
| 103 | + isEnabled: vi.fn().mockReturnValue(false), |
| 104 | + connectOrDisconnect: vi.fn(), |
| 105 | + getInstance: vi.fn().mockReturnValue(null), |
| 106 | + subscribeToTask: vi.fn(), |
| 107 | + unsubscribeFromTask: vi.fn(), |
| 108 | + }, |
| 109 | + getRooCodeApiUrl: vi.fn().mockReturnValue("https://api.roo-code.com"), |
| 110 | +})) |
| 111 | + |
| 112 | +describe("ClineProvider - clearTask with pending operations", () => { |
| 113 | + let provider: ClineProvider |
| 114 | + let mockContext: vscode.ExtensionContext |
| 115 | + let mockOutputChannel: vscode.OutputChannel |
| 116 | + let mockContextProxy: ContextProxy |
| 117 | + |
| 118 | + beforeEach(() => { |
| 119 | + // Create mock instances |
| 120 | + mockContext = { |
| 121 | + globalStorageUri: { fsPath: "/test/storage" }, |
| 122 | + extension: { packageJSON: { version: "1.0.0", name: "roo-code" } }, |
| 123 | + } as any |
| 124 | + |
| 125 | + mockOutputChannel = { |
| 126 | + appendLine: vi.fn(), |
| 127 | + } as any |
| 128 | + |
| 129 | + mockContextProxy = { |
| 130 | + getValues: vi.fn().mockReturnValue({ |
| 131 | + taskHistory: [], |
| 132 | + mode: "code", |
| 133 | + apiConfiguration: { apiProvider: "anthropic" }, |
| 134 | + }), |
| 135 | + getValue: vi.fn(), |
| 136 | + setValue: vi.fn(), |
| 137 | + setValues: vi.fn(), |
| 138 | + getProviderSettings: vi.fn().mockReturnValue({ apiProvider: "anthropic" }), |
| 139 | + setProviderSettings: vi.fn(), |
| 140 | + resetAllState: vi.fn(), |
| 141 | + extensionUri: { fsPath: "/test/extension" }, |
| 142 | + globalStorageUri: { fsPath: "/test/storage" }, |
| 143 | + } as any |
| 144 | + |
| 145 | + // Create provider instance |
| 146 | + provider = new ClineProvider(mockContext, mockOutputChannel, "sidebar", mockContextProxy) |
| 147 | + }) |
| 148 | + |
| 149 | + afterEach(() => { |
| 150 | + vi.clearAllMocks() |
| 151 | + }) |
| 152 | + |
| 153 | + it("should clear pending edit operations when clearTask is called", async () => { |
| 154 | + // Setup: Create a mock task with a pending edit operation |
| 155 | + const mockTask = { |
| 156 | + taskId: "test-task-123", |
| 157 | + instanceId: "instance-1", |
| 158 | + clineMessages: [], |
| 159 | + apiConversationHistory: [], |
| 160 | + abandoned: false, |
| 161 | + parentTask: undefined, |
| 162 | + abortTask: vi.fn().mockResolvedValue(undefined), |
| 163 | + overwriteClineMessages: vi.fn(), |
| 164 | + overwriteApiConversationHistory: vi.fn(), |
| 165 | + handleWebviewAskResponse: vi.fn(), |
| 166 | + emit: vi.fn(), |
| 167 | + on: vi.fn(), |
| 168 | + off: vi.fn(), |
| 169 | + } as any |
| 170 | + |
| 171 | + // Add task to stack |
| 172 | + ;(provider as any).clineStack = [mockTask] |
| 173 | + |
| 174 | + // Set a pending edit operation for this task |
| 175 | + const operationId = `task-${mockTask.taskId}` |
| 176 | + ;(provider as any).setPendingEditOperation(operationId, { |
| 177 | + messageTs: 123456789, |
| 178 | + editedContent: "edited content", |
| 179 | + images: [], |
| 180 | + messageIndex: 0, |
| 181 | + apiConversationHistoryIndex: 0, |
| 182 | + }) |
| 183 | + |
| 184 | + // Verify the pending operation exists |
| 185 | + expect((provider as any).getPendingEditOperation(operationId)).toBeDefined() |
| 186 | + |
| 187 | + // Act: Call clearTask |
| 188 | + await provider.clearTask() |
| 189 | + |
| 190 | + // Assert: Pending operation should be cleared |
| 191 | + expect((provider as any).getPendingEditOperation(operationId)).toBeUndefined() |
| 192 | + expect((provider as any).clineStack.length).toBe(0) |
| 193 | + }) |
| 194 | + |
| 195 | + it("should not process pending edits when createTaskWithHistoryItem is called after clearTask", async () => { |
| 196 | + // Setup: Create a history item |
| 197 | + const historyItem = { |
| 198 | + id: "test-task-123", |
| 199 | + ts: Date.now(), |
| 200 | + task: "Test task", |
| 201 | + mode: "code", |
| 202 | + workspace: "/test/workspace", |
| 203 | + } |
| 204 | + |
| 205 | + // Set a pending edit operation that should be cleared |
| 206 | + const operationId = `task-${historyItem.id}` |
| 207 | + ;(provider as any).setPendingEditOperation(operationId, { |
| 208 | + messageTs: 123456789, |
| 209 | + editedContent: "edited content that should not be processed", |
| 210 | + images: [], |
| 211 | + messageIndex: 0, |
| 212 | + apiConversationHistoryIndex: 0, |
| 213 | + }) |
| 214 | + |
| 215 | + // Mock Task constructor to capture task creation |
| 216 | + const mockTaskInstance = { |
| 217 | + taskId: historyItem.id, |
| 218 | + instanceId: "instance-1", |
| 219 | + clineMessages: [], |
| 220 | + apiConversationHistory: [], |
| 221 | + abandoned: false, |
| 222 | + overwriteClineMessages: vi.fn(), |
| 223 | + overwriteApiConversationHistory: vi.fn(), |
| 224 | + handleWebviewAskResponse: vi.fn(), |
| 225 | + emit: vi.fn(), |
| 226 | + on: vi.fn(), |
| 227 | + off: vi.fn(), |
| 228 | + } |
| 229 | + |
| 230 | + vi.mocked(Task).mockImplementation(() => mockTaskInstance as any) |
| 231 | + |
| 232 | + // Act: Create task with history item (simulating "Start New Task" after completion) |
| 233 | + await provider.createTaskWithHistoryItem(historyItem as any) |
| 234 | + |
| 235 | + // Wait a bit to ensure any setTimeout callbacks would have executed |
| 236 | + await new Promise((resolve) => setTimeout(resolve, 200)) |
| 237 | + |
| 238 | + // Assert: Pending operation should have been cleared and not processed |
| 239 | + expect((provider as any).getPendingEditOperation(operationId)).toBeUndefined() |
| 240 | + expect(mockTaskInstance.handleWebviewAskResponse).not.toHaveBeenCalled() |
| 241 | + expect(mockTaskInstance.overwriteClineMessages).not.toHaveBeenCalled() |
| 242 | + }) |
| 243 | + |
| 244 | + it("should handle clearTask when no task is active", async () => { |
| 245 | + // Setup: No tasks in stack |
| 246 | + ;(provider as any).clineStack = [] |
| 247 | + |
| 248 | + // Act & Assert: Should not throw |
| 249 | + await expect(provider.clearTask()).resolves.not.toThrow() |
| 250 | + }) |
| 251 | + |
| 252 | + it("should clear pending operations even if task removal fails", async () => { |
| 253 | + // Setup: Create a mock task that will throw during removal |
| 254 | + const mockTask = { |
| 255 | + taskId: "test-task-456", |
| 256 | + instanceId: "instance-2", |
| 257 | + abandoned: false, |
| 258 | + abortTask: vi.fn().mockRejectedValue(new Error("Abort failed")), |
| 259 | + emit: vi.fn(), |
| 260 | + on: vi.fn(), |
| 261 | + off: vi.fn(), |
| 262 | + } as any |
| 263 | + |
| 264 | + ;(provider as any).clineStack = [mockTask] |
| 265 | + |
| 266 | + // Set a pending edit operation |
| 267 | + const operationId = `task-${mockTask.taskId}` |
| 268 | + ;(provider as any).setPendingEditOperation(operationId, { |
| 269 | + messageTs: 987654321, |
| 270 | + editedContent: "content", |
| 271 | + images: [], |
| 272 | + messageIndex: 0, |
| 273 | + apiConversationHistoryIndex: 0, |
| 274 | + }) |
| 275 | + |
| 276 | + // Spy on removeClineFromStack to simulate failure |
| 277 | + const removeSpy = vi |
| 278 | + .spyOn(provider as any, "removeClineFromStack") |
| 279 | + .mockRejectedValue(new Error("Remove failed")) |
| 280 | + |
| 281 | + // Act: Call clearTask (should clear pending operation before attempting removal) |
| 282 | + try { |
| 283 | + await provider.clearTask() |
| 284 | + } catch { |
| 285 | + // Expected to throw due to mocked failure |
| 286 | + } |
| 287 | + |
| 288 | + // Assert: Pending operation should still be cleared despite removal failure |
| 289 | + expect((provider as any).getPendingEditOperation(operationId)).toBeUndefined() |
| 290 | + expect(removeSpy).toHaveBeenCalled() |
| 291 | + }) |
| 292 | +}) |
0 commit comments