|
| 1 | +// npx vitest services/code-index/__tests__/manager-watcher-integration.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 4 | +import { CodeIndexManager } from "../manager" |
| 5 | +import * as vscode from "vscode" |
| 6 | + |
| 7 | +// Mock dependencies |
| 8 | +vi.mock("vscode") |
| 9 | +vi.mock("../service-factory") |
| 10 | +vi.mock("../git-branch-watcher") |
| 11 | +vi.mock("../../../utils/git") |
| 12 | + |
| 13 | +import { ServiceFactory } from "../service-factory" |
| 14 | +import { GitBranchWatcher } from "../git-branch-watcher" |
| 15 | +import { getCurrentBranch } from "../../../utils/git" |
| 16 | + |
| 17 | +const mockedServiceFactory = vi.mocked(ServiceFactory) |
| 18 | +const mockedGitBranchWatcher = vi.mocked(GitBranchWatcher) |
| 19 | +const mockedGetCurrentBranch = vi.mocked(getCurrentBranch) |
| 20 | + |
| 21 | +describe("CodeIndexManager + GitBranchWatcher Integration", () => { |
| 22 | + let manager: CodeIndexManager |
| 23 | + let mockContext: vscode.ExtensionContext |
| 24 | + let mockServiceFactory: any |
| 25 | + let mockVectorStore: any |
| 26 | + let mockOrchestrator: any |
| 27 | + let mockWatcher: any |
| 28 | + let branchChangeCallback: ((oldBranch: string | undefined, newBranch: string | undefined) => Promise<void>) | null = |
| 29 | + null |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + vi.clearAllMocks() |
| 33 | + |
| 34 | + // Setup mock context |
| 35 | + mockContext = { |
| 36 | + subscriptions: [], |
| 37 | + globalState: { |
| 38 | + get: vi.fn(), |
| 39 | + update: vi.fn(), |
| 40 | + }, |
| 41 | + workspaceState: { |
| 42 | + get: vi.fn(), |
| 43 | + update: vi.fn(), |
| 44 | + }, |
| 45 | + } as any |
| 46 | + |
| 47 | + // Setup mock vector store |
| 48 | + mockVectorStore = { |
| 49 | + initialize: vi.fn().mockResolvedValue(undefined), |
| 50 | + invalidateBranchCache: vi.fn(), |
| 51 | + getCurrentBranch: vi.fn().mockReturnValue("main"), |
| 52 | + upsert: vi.fn().mockResolvedValue(undefined), |
| 53 | + search: vi.fn().mockResolvedValue([]), |
| 54 | + } |
| 55 | + |
| 56 | + // Setup mock orchestrator |
| 57 | + mockOrchestrator = { |
| 58 | + getVectorStore: vi.fn().mockReturnValue(mockVectorStore), |
| 59 | + startIndexing: vi.fn().mockResolvedValue(undefined), |
| 60 | + stopIndexing: vi.fn().mockResolvedValue(undefined), |
| 61 | + dispose: vi.fn(), |
| 62 | + } |
| 63 | + |
| 64 | + // Setup mock service factory |
| 65 | + mockServiceFactory = { |
| 66 | + createVectorStore: vi.fn().mockResolvedValue(mockVectorStore), |
| 67 | + createOrchestrator: vi.fn().mockResolvedValue(mockOrchestrator), |
| 68 | + configManager: { |
| 69 | + getConfig: vi.fn().mockReturnValue({ |
| 70 | + branchIsolationEnabled: true, |
| 71 | + qdrantUrl: "http://localhost:6333", |
| 72 | + embedderProvider: "openai", |
| 73 | + }), |
| 74 | + isFeatureConfigured: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + mockedServiceFactory.mockImplementation(() => mockServiceFactory as any) |
| 79 | + |
| 80 | + // Setup mock watcher - capture the callback |
| 81 | + mockWatcher = { |
| 82 | + initialize: vi.fn().mockResolvedValue(undefined), |
| 83 | + getCurrentBranch: vi.fn().mockReturnValue("main"), |
| 84 | + dispose: vi.fn(), |
| 85 | + } |
| 86 | + |
| 87 | + mockedGitBranchWatcher.mockImplementation((workspacePath: string, callback: any, config: any) => { |
| 88 | + branchChangeCallback = callback |
| 89 | + return mockWatcher as any |
| 90 | + }) |
| 91 | + |
| 92 | + // Setup git mock |
| 93 | + mockedGetCurrentBranch.mockResolvedValue("main") |
| 94 | + }) |
| 95 | + |
| 96 | + afterEach(() => { |
| 97 | + if (manager) { |
| 98 | + manager.dispose() |
| 99 | + } |
| 100 | + branchChangeCallback = null |
| 101 | + }) |
| 102 | + |
| 103 | + describe("branch change handling", () => { |
| 104 | + it("should invalidate cache and reinitialize vector store on branch change", async () => { |
| 105 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 106 | + |
| 107 | + // Start the manager |
| 108 | + await manager.start() |
| 109 | + |
| 110 | + expect(mockWatcher.initialize).toHaveBeenCalled() |
| 111 | + expect(mockOrchestrator.startIndexing).toHaveBeenCalled() |
| 112 | + |
| 113 | + // Simulate branch change |
| 114 | + vi.clearAllMocks() |
| 115 | + mockVectorStore.getCurrentBranch.mockReturnValue("feature-branch") |
| 116 | + |
| 117 | + if (branchChangeCallback) { |
| 118 | + await branchChangeCallback("main", "feature-branch") |
| 119 | + } |
| 120 | + |
| 121 | + // Should invalidate cache |
| 122 | + expect(mockVectorStore.invalidateBranchCache).toHaveBeenCalled() |
| 123 | + |
| 124 | + // Should reinitialize vector store |
| 125 | + expect(mockVectorStore.initialize).toHaveBeenCalled() |
| 126 | + |
| 127 | + // Should restart indexing |
| 128 | + expect(mockOrchestrator.startIndexing).toHaveBeenCalled() |
| 129 | + }) |
| 130 | + |
| 131 | + it("should recreate services if orchestrator doesn't exist", async () => { |
| 132 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 133 | + |
| 134 | + await manager.start() |
| 135 | + |
| 136 | + // Simulate orchestrator being disposed |
| 137 | + mockOrchestrator.getVectorStore.mockReturnValue(null) |
| 138 | + |
| 139 | + vi.clearAllMocks() |
| 140 | + |
| 141 | + if (branchChangeCallback) { |
| 142 | + await branchChangeCallback("main", "feature-branch") |
| 143 | + } |
| 144 | + |
| 145 | + // Should recreate services |
| 146 | + expect(mockServiceFactory.createVectorStore).toHaveBeenCalled() |
| 147 | + expect(mockServiceFactory.createOrchestrator).toHaveBeenCalled() |
| 148 | + }) |
| 149 | + |
| 150 | + it("should handle branch change errors gracefully", async () => { |
| 151 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 152 | + |
| 153 | + await manager.start() |
| 154 | + |
| 155 | + // Make vector store initialization fail |
| 156 | + mockVectorStore.initialize.mockRejectedValueOnce(new Error("Init failed")) |
| 157 | + |
| 158 | + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 159 | + |
| 160 | + if (branchChangeCallback) { |
| 161 | + await branchChangeCallback("main", "feature-branch") |
| 162 | + } |
| 163 | + |
| 164 | + // Should log error |
| 165 | + expect(consoleErrorSpy).toHaveBeenCalled() |
| 166 | + |
| 167 | + consoleErrorSpy.mockRestore() |
| 168 | + }) |
| 169 | + |
| 170 | + it("should not process branch changes when manager is stopped", async () => { |
| 171 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 172 | + |
| 173 | + await manager.start() |
| 174 | + await manager.stop() |
| 175 | + |
| 176 | + vi.clearAllMocks() |
| 177 | + |
| 178 | + if (branchChangeCallback) { |
| 179 | + await branchChangeCallback("main", "feature-branch") |
| 180 | + } |
| 181 | + |
| 182 | + // Should not invalidate cache or reinitialize |
| 183 | + expect(mockVectorStore.invalidateBranchCache).not.toHaveBeenCalled() |
| 184 | + expect(mockVectorStore.initialize).not.toHaveBeenCalled() |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe("watcher lifecycle", () => { |
| 189 | + it("should initialize watcher when manager starts", async () => { |
| 190 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 191 | + |
| 192 | + await manager.start() |
| 193 | + |
| 194 | + expect(mockedGitBranchWatcher).toHaveBeenCalledWith( |
| 195 | + "/test/workspace", |
| 196 | + expect.any(Function), |
| 197 | + expect.objectContaining({ |
| 198 | + enabled: true, |
| 199 | + }), |
| 200 | + ) |
| 201 | + |
| 202 | + expect(mockWatcher.initialize).toHaveBeenCalled() |
| 203 | + }) |
| 204 | + |
| 205 | + it("should dispose watcher when manager is disposed", async () => { |
| 206 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 207 | + |
| 208 | + await manager.start() |
| 209 | + |
| 210 | + manager.dispose() |
| 211 | + |
| 212 | + expect(mockWatcher.dispose).toHaveBeenCalled() |
| 213 | + }) |
| 214 | + |
| 215 | + it("should not create watcher when branch isolation is disabled", async () => { |
| 216 | + mockServiceFactory.configManager.getConfig.mockReturnValue({ |
| 217 | + branchIsolationEnabled: false, |
| 218 | + qdrantUrl: "http://localhost:6333", |
| 219 | + embedderProvider: "openai", |
| 220 | + }) |
| 221 | + |
| 222 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 223 | + |
| 224 | + await manager.start() |
| 225 | + |
| 226 | + expect(mockedGitBranchWatcher).not.toHaveBeenCalled() |
| 227 | + }) |
| 228 | + }) |
| 229 | + |
| 230 | + describe("state consistency", () => { |
| 231 | + it("should maintain consistent state across multiple branch changes", async () => { |
| 232 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 233 | + |
| 234 | + await manager.start() |
| 235 | + |
| 236 | + // First branch change |
| 237 | + mockVectorStore.getCurrentBranch.mockReturnValue("feature-1") |
| 238 | + if (branchChangeCallback) { |
| 239 | + await branchChangeCallback("main", "feature-1") |
| 240 | + } |
| 241 | + |
| 242 | + expect(mockVectorStore.invalidateBranchCache).toHaveBeenCalledTimes(1) |
| 243 | + |
| 244 | + // Second branch change |
| 245 | + vi.clearAllMocks() |
| 246 | + mockVectorStore.getCurrentBranch.mockReturnValue("feature-2") |
| 247 | + if (branchChangeCallback) { |
| 248 | + await branchChangeCallback("feature-1", "feature-2") |
| 249 | + } |
| 250 | + |
| 251 | + expect(mockVectorStore.invalidateBranchCache).toHaveBeenCalledTimes(1) |
| 252 | + |
| 253 | + // Third branch change back to main |
| 254 | + vi.clearAllMocks() |
| 255 | + mockVectorStore.getCurrentBranch.mockReturnValue("main") |
| 256 | + if (branchChangeCallback) { |
| 257 | + await branchChangeCallback("feature-2", "main") |
| 258 | + } |
| 259 | + |
| 260 | + expect(mockVectorStore.invalidateBranchCache).toHaveBeenCalledTimes(1) |
| 261 | + }) |
| 262 | + |
| 263 | + it("should handle rapid branch changes with debouncing", async () => { |
| 264 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 265 | + |
| 266 | + await manager.start() |
| 267 | + |
| 268 | + // Simulate rapid branch changes (watcher handles debouncing) |
| 269 | + // The callback should only be called once per actual change |
| 270 | + if (branchChangeCallback) { |
| 271 | + await branchChangeCallback("main", "feature-1") |
| 272 | + await branchChangeCallback("feature-1", "feature-2") |
| 273 | + await branchChangeCallback("feature-2", "feature-3") |
| 274 | + } |
| 275 | + |
| 276 | + // Each change should invalidate cache |
| 277 | + expect(mockVectorStore.invalidateBranchCache).toHaveBeenCalledTimes(3) |
| 278 | + }) |
| 279 | + }) |
| 280 | + |
| 281 | + describe("error recovery", () => { |
| 282 | + it("should recover from vector store initialization failure", async () => { |
| 283 | + manager = new CodeIndexManager(mockContext, "/test/workspace") |
| 284 | + |
| 285 | + await manager.start() |
| 286 | + |
| 287 | + // First branch change fails |
| 288 | + mockVectorStore.initialize.mockRejectedValueOnce(new Error("Init failed")) |
| 289 | + |
| 290 | + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 291 | + |
| 292 | + if (branchChangeCallback) { |
| 293 | + await branchChangeCallback("main", "feature-1") |
| 294 | + } |
| 295 | + |
| 296 | + expect(consoleErrorSpy).toHaveBeenCalled() |
| 297 | + |
| 298 | + // Second branch change succeeds |
| 299 | + vi.clearAllMocks() |
| 300 | + mockVectorStore.initialize.mockResolvedValue(undefined) |
| 301 | + |
| 302 | + if (branchChangeCallback) { |
| 303 | + await branchChangeCallback("feature-1", "feature-2") |
| 304 | + } |
| 305 | + |
| 306 | + expect(mockVectorStore.initialize).toHaveBeenCalled() |
| 307 | + expect(mockOrchestrator.startIndexing).toHaveBeenCalled() |
| 308 | + |
| 309 | + consoleErrorSpy.mockRestore() |
| 310 | + }) |
| 311 | + }) |
| 312 | +}) |
0 commit comments