Skip to content

Commit 6537b02

Browse files
committed
fix: address Copilot review comments
- Add 'cause' option when throwing errors in scanner.ts to preserve original error context - Move vi.mock declarations before imports in manager.spec.ts for proper hoisting - Replace vitest.fn() and vitest.spyOn() with vi.fn() and vi.spyOn() for consistency
1 parent c2ffd9b commit 6537b02

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/services/code-index/__tests__/manager.spec.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { CodeIndexManager } from "../manager"
2-
31
// Mock vscode module
4-
vitest.mock("vscode", () => ({
2+
vi.mock("vscode", () => ({
53
workspace: {
64
workspaceFolders: [
75
{
@@ -14,18 +12,20 @@ vitest.mock("vscode", () => ({
1412
}))
1513

1614
// Mock only the essential dependencies
17-
vitest.mock("../../../utils/path", () => ({
18-
getWorkspacePath: vitest.fn(() => "/test/workspace"),
15+
vi.mock("../../../utils/path", () => ({
16+
getWorkspacePath: vi.fn(() => "/test/workspace"),
1917
}))
2018

21-
vitest.mock("../state-manager", () => ({
22-
CodeIndexStateManager: vitest.fn().mockImplementation(() => ({
23-
onProgressUpdate: vitest.fn(),
24-
getCurrentStatus: vitest.fn(),
25-
dispose: vitest.fn(),
19+
vi.mock("../state-manager", () => ({
20+
CodeIndexStateManager: vi.fn().mockImplementation(() => ({
21+
onProgressUpdate: vi.fn(),
22+
getCurrentStatus: vi.fn(),
23+
dispose: vi.fn(),
2624
})),
2725
}))
2826

27+
import { CodeIndexManager } from "../manager"
28+
2929
describe("CodeIndexManager - handleSettingsChange regression", () => {
3030
let mockContext: any
3131
let manager: CodeIndexManager
@@ -40,7 +40,7 @@ describe("CodeIndexManager - handleSettingsChange regression", () => {
4040
globalState: {} as any,
4141
extensionUri: {} as any,
4242
extensionPath: "/test/extension",
43-
asAbsolutePath: vitest.fn(),
43+
asAbsolutePath: vi.fn(),
4444
storageUri: {} as any,
4545
storagePath: "/test/storage",
4646
globalStorageUri: {} as any,
@@ -71,13 +71,13 @@ describe("CodeIndexManager - handleSettingsChange regression", () => {
7171

7272
// Mock a minimal config manager that simulates first-time configuration
7373
const mockConfigManager = {
74-
loadConfiguration: vitest.fn().mockResolvedValue({ requiresRestart: true }),
74+
loadConfiguration: vi.fn().mockResolvedValue({ requiresRestart: true }),
7575
}
7676
;(manager as any)._configManager = mockConfigManager
7777

7878
// Mock the feature state to simulate valid configuration that would normally trigger restart
79-
vitest.spyOn(manager, "isFeatureEnabled", "get").mockReturnValue(true)
80-
vitest.spyOn(manager, "isFeatureConfigured", "get").mockReturnValue(true)
79+
vi.spyOn(manager, "isFeatureEnabled", "get").mockReturnValue(true)
80+
vi.spyOn(manager, "isFeatureConfigured", "get").mockReturnValue(true)
8181

8282
// The key test: this should NOT throw "CodeIndexManager not initialized" error
8383
await expect(manager.handleSettingsChange()).resolves.not.toThrow()
@@ -89,10 +89,10 @@ describe("CodeIndexManager - handleSettingsChange regression", () => {
8989
it("should work normally when manager is initialized", async () => {
9090
// Mock a complete config manager with all required properties
9191
const mockConfigManager = {
92-
loadConfiguration: vitest.fn().mockResolvedValue({ requiresRestart: true }),
92+
loadConfiguration: vi.fn().mockResolvedValue({ requiresRestart: true }),
9393
isFeatureConfigured: true,
9494
isFeatureEnabled: true,
95-
getConfig: vitest.fn().mockReturnValue({
95+
getConfig: vi.fn().mockReturnValue({
9696
isEnabled: true,
9797
isConfigured: true,
9898
embedderProvider: "openai",
@@ -106,20 +106,20 @@ describe("CodeIndexManager - handleSettingsChange regression", () => {
106106
;(manager as any)._configManager = mockConfigManager
107107

108108
// Simulate an initialized manager by setting the required properties
109-
;(manager as any)._orchestrator = { stopWatcher: vitest.fn() }
109+
;(manager as any)._orchestrator = { stopWatcher: vi.fn() }
110110
;(manager as any)._searchService = {}
111111
;(manager as any)._cacheManager = {}
112112

113113
// Verify manager is considered initialized
114114
expect(manager.isInitialized).toBe(true)
115115

116116
// Mock the methods that would be called during restart
117-
const recreateServicesSpy = vitest.spyOn(manager as any, "_recreateServices").mockImplementation(() => {})
118-
const startIndexingSpy = vitest.spyOn(manager, "startIndexing").mockResolvedValue()
117+
const recreateServicesSpy = vi.spyOn(manager as any, "_recreateServices").mockImplementation(() => {})
118+
const startIndexingSpy = vi.spyOn(manager, "startIndexing").mockResolvedValue()
119119

120120
// Mock the feature state
121-
vitest.spyOn(manager, "isFeatureEnabled", "get").mockReturnValue(true)
122-
vitest.spyOn(manager, "isFeatureConfigured", "get").mockReturnValue(true)
121+
vi.spyOn(manager, "isFeatureEnabled", "get").mockReturnValue(true)
122+
vi.spyOn(manager, "isFeatureConfigured", "get").mockReturnValue(true)
123123

124124
await manager.handleSettingsChange()
125125

src/services/code-index/processors/scanner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export class DirectoryScanner implements IDirectoryScanner {
312312
// Re-throw the error with workspace context
313313
throw new Error(
314314
`Failed to delete points for ${uniqueFilePaths.length} files. Workspace: ${scanWorkspace}. ${deleteError instanceof Error ? deleteError.message : String(deleteError)}`,
315+
{ cause: deleteError },
315316
)
316317
}
317318
}

0 commit comments

Comments
 (0)