|
| 1 | +// Use doMock to apply the mock dynamically |
| 2 | +vitest.doMock("../../utils/path", () => ({ |
| 3 | + getWorkspacePath: vitest.fn(() => { |
| 4 | + console.log("getWorkspacePath mock called, returning:", "/mock/workspace") |
| 5 | + return "/mock/workspace" |
| 6 | + }), |
| 7 | +})) |
| 8 | + |
| 9 | +// Mock the RepoPerTaskCheckpointService |
| 10 | +vitest.mock("../../../services/checkpoints", () => ({ |
| 11 | + RepoPerTaskCheckpointService: { |
| 12 | + create: vitest.fn(), |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +// Mock the TelemetryService to prevent unhandled rejections |
| 17 | +vitest.mock("@roo-code/telemetry", () => ({ |
| 18 | + TelemetryService: { |
| 19 | + instance: { |
| 20 | + captureCheckpointCreated: vitest.fn(), |
| 21 | + captureCheckpointRestored: vitest.fn(), |
| 22 | + captureCheckpointDiffed: vitest.fn(), |
| 23 | + }, |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +import { describe, it, expect, beforeEach, afterEach, vitest } from "vitest" |
| 28 | +import * as path from "path" |
| 29 | +import * as fs from "fs/promises" |
| 30 | +import * as os from "os" |
| 31 | +import { EventEmitter } from "events" |
| 32 | + |
| 33 | +// Import these modules after mocks are set up |
| 34 | +let getCheckpointService: any |
| 35 | +let RepoPerTaskCheckpointService: any |
| 36 | + |
| 37 | +// Set up the imports after mocks |
| 38 | +beforeAll(async () => { |
| 39 | + const checkpointsModule = await import("../index") |
| 40 | + const checkpointServiceModule = await import("../../../services/checkpoints") |
| 41 | + getCheckpointService = checkpointsModule.getCheckpointService |
| 42 | + RepoPerTaskCheckpointService = checkpointServiceModule.RepoPerTaskCheckpointService |
| 43 | +}) |
| 44 | + |
| 45 | +// Mock the FileChangeManager to avoid complex dependencies |
| 46 | +const mockFileChangeManager = { |
| 47 | + _baseline: "HEAD" as string, |
| 48 | + getChanges: vitest.fn(), |
| 49 | + updateBaseline: vitest.fn(), |
| 50 | + setFiles: vitest.fn(), |
| 51 | + getLLMOnlyChanges: vitest.fn(), |
| 52 | +} |
| 53 | + |
| 54 | +// Create a temporary directory for mock global storage |
| 55 | +let mockGlobalStorageDir: string |
| 56 | + |
| 57 | +// Mock the provider |
| 58 | +const mockProvider = { |
| 59 | + getFileChangeManager: vitest.fn(() => mockFileChangeManager), |
| 60 | + log: vitest.fn(), |
| 61 | + get context() { |
| 62 | + return { |
| 63 | + globalStorageUri: { |
| 64 | + fsPath: mockGlobalStorageDir, |
| 65 | + }, |
| 66 | + } |
| 67 | + }, |
| 68 | +} |
| 69 | + |
| 70 | +// Mock the Task object with proper typing |
| 71 | +const createMockTask = (options: { taskId: string; hasExistingCheckpoints: boolean; enableCheckpoints?: boolean }) => { |
| 72 | + const mockTask = { |
| 73 | + taskId: options.taskId, |
| 74 | + instanceId: "test-instance", |
| 75 | + rootTask: undefined as any, |
| 76 | + parentTask: undefined as any, |
| 77 | + taskNumber: 1, |
| 78 | + workspacePath: "/mock/workspace", |
| 79 | + enableCheckpoints: options.enableCheckpoints ?? true, |
| 80 | + checkpointService: null as any, |
| 81 | + checkpointServiceInitializing: false, |
| 82 | + ongoingCheckpointSaves: new Map(), |
| 83 | + clineMessages: options.hasExistingCheckpoints |
| 84 | + ? [{ say: "checkpoint_saved", ts: Date.now(), text: "existing-checkpoint-hash" }] |
| 85 | + : [], |
| 86 | + providerRef: { |
| 87 | + deref: () => mockProvider, |
| 88 | + }, |
| 89 | + fileContextTracker: {}, |
| 90 | + // Add minimal required properties to satisfy Task interface |
| 91 | + todoList: undefined, |
| 92 | + userMessageContent: "", |
| 93 | + apiConversationHistory: [], |
| 94 | + customInstructions: "", |
| 95 | + alwaysAllowReadOnly: false, |
| 96 | + alwaysAllowWrite: false, |
| 97 | + alwaysAllowExecute: false, |
| 98 | + alwaysAllowBrowser: false, |
| 99 | + alwaysAllowMcp: false, |
| 100 | + createdAt: Date.now(), |
| 101 | + historyErrors: [], |
| 102 | + askResponse: undefined, |
| 103 | + askResponseText: "", |
| 104 | + abort: vitest.fn(), |
| 105 | + isAborting: false, |
| 106 | + } as any // Cast to any to avoid needing to implement all Task methods |
| 107 | + return mockTask |
| 108 | +} |
| 109 | + |
| 110 | +describe("getCheckpointService orchestration", () => { |
| 111 | + let tmpDir: string |
| 112 | + let mockService: any |
| 113 | + |
| 114 | + beforeEach(async () => { |
| 115 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "checkpoint-test-")) |
| 116 | + mockGlobalStorageDir = path.join(tmpDir, "global-storage") |
| 117 | + await fs.mkdir(mockGlobalStorageDir, { recursive: true }) |
| 118 | + |
| 119 | + // Reset mocks |
| 120 | + vitest.clearAllMocks() |
| 121 | + |
| 122 | + // Override the global vscode mock to have a workspace folder |
| 123 | + const vscode = await import("vscode") |
| 124 | + // @ts-ignore - Mock the workspace.workspaceFolders |
| 125 | + vscode.workspace.workspaceFolders = [ |
| 126 | + { |
| 127 | + uri: { |
| 128 | + fsPath: "/mock/workspace", |
| 129 | + }, |
| 130 | + }, |
| 131 | + ] |
| 132 | + |
| 133 | + // Mock the checkpoint service |
| 134 | + mockService = new EventEmitter() |
| 135 | + mockService.baseHash = "mock-base-hash-abc123" |
| 136 | + mockService.getCurrentCheckpoint = vitest.fn(() => "mock-current-checkpoint-def456") |
| 137 | + mockService.isInitialized = true |
| 138 | + mockService.initShadowGit = vitest.fn(() => { |
| 139 | + // Simulate the initialize event being emitted after initShadowGit completes |
| 140 | + setImmediate(() => { |
| 141 | + mockService.emit("initialize") |
| 142 | + }) |
| 143 | + return Promise.resolve() |
| 144 | + }) |
| 145 | + mockService.saveCheckpoint = vitest.fn(() => { |
| 146 | + return Promise.resolve({ |
| 147 | + commit: "mock-checkpoint-hash", |
| 148 | + message: "Mock checkpoint", |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + // Mock the service creation |
| 153 | + ;(RepoPerTaskCheckpointService.create as any).mockReturnValue(mockService) |
| 154 | + }) |
| 155 | + |
| 156 | + afterEach(async () => { |
| 157 | + await fs.rm(tmpDir, { recursive: true, force: true }) |
| 158 | + vitest.restoreAllMocks() |
| 159 | + }) |
| 160 | + |
| 161 | + describe("Service creation and caching", () => { |
| 162 | + it("should create and return a new checkpoint service", async () => { |
| 163 | + const task = createMockTask({ |
| 164 | + taskId: "new-task-123", |
| 165 | + hasExistingCheckpoints: false, |
| 166 | + }) |
| 167 | + |
| 168 | + const service = await getCheckpointService(task) |
| 169 | + console.log("Service returned:", service) |
| 170 | + expect(service).toBe(mockService) |
| 171 | + expect(RepoPerTaskCheckpointService.create).toHaveBeenCalledWith({ |
| 172 | + taskId: "new-task-123", |
| 173 | + shadowDir: mockGlobalStorageDir, |
| 174 | + workspaceDir: "/mock/workspace", |
| 175 | + log: expect.any(Function), |
| 176 | + }) |
| 177 | + }) |
| 178 | + |
| 179 | + it("should return existing service if already initialized", async () => { |
| 180 | + const task = createMockTask({ |
| 181 | + taskId: "existing-service-task", |
| 182 | + hasExistingCheckpoints: false, |
| 183 | + }) |
| 184 | + |
| 185 | + // Set existing checkpoint service |
| 186 | + task.checkpointService = mockService |
| 187 | + |
| 188 | + const service = await getCheckpointService(task) |
| 189 | + expect(service).toBe(mockService) |
| 190 | + |
| 191 | + // Should not create a new service |
| 192 | + expect(RepoPerTaskCheckpointService.create).not.toHaveBeenCalled() |
| 193 | + }) |
| 194 | + |
| 195 | + it("should return undefined when checkpoints are disabled", async () => { |
| 196 | + const task = createMockTask({ |
| 197 | + taskId: "disabled-task", |
| 198 | + hasExistingCheckpoints: false, |
| 199 | + enableCheckpoints: false, |
| 200 | + }) |
| 201 | + |
| 202 | + const service = await getCheckpointService(task) |
| 203 | + expect(service).toBeUndefined() |
| 204 | + }) |
| 205 | + }) |
| 206 | + |
| 207 | + describe("Service initialization", () => { |
| 208 | + it("should call initShadowGit and set up event handlers", async () => { |
| 209 | + const task = createMockTask({ |
| 210 | + taskId: "init-test-task", |
| 211 | + hasExistingCheckpoints: false, |
| 212 | + }) |
| 213 | + |
| 214 | + const service = await getCheckpointService(task) |
| 215 | + expect(service).toBe(mockService) |
| 216 | + |
| 217 | + // initShadowGit should be called |
| 218 | + expect(mockService.initShadowGit).toHaveBeenCalled() |
| 219 | + |
| 220 | + // Wait for the initialize event to be emitted and the service to be assigned |
| 221 | + await new Promise((resolve) => setImmediate(resolve)) |
| 222 | + |
| 223 | + // Service should be assigned to task after initialization |
| 224 | + expect(task.checkpointService).toBe(mockService) |
| 225 | + }) |
| 226 | + }) |
| 227 | +}) |
0 commit comments