|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { describe, it, expect, beforeEach, vi } from "vitest" |
| 3 | +import { CodeIndexConfigManager } from "../config-manager" |
| 4 | +import { ContextProxy } from "../../../core/config/ContextProxy" |
| 5 | + |
| 6 | +describe("Workspace-level Indexing Toggle", () => { |
| 7 | + let configManager: CodeIndexConfigManager |
| 8 | + let mockContextProxy: ContextProxy |
| 9 | + let mockContext: vscode.ExtensionContext |
| 10 | + const testWorkspacePath = "/test/workspace" |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + // Mock ContextProxy |
| 14 | + mockContextProxy = { |
| 15 | + getValue: vi.fn(), |
| 16 | + setValue: vi.fn(), |
| 17 | + getGlobalState: vi.fn(), |
| 18 | + updateGlobalState: vi.fn(), |
| 19 | + getSecret: vi.fn(), |
| 20 | + storeSecret: vi.fn(), |
| 21 | + } as any |
| 22 | + |
| 23 | + // Mock VSCode Extension Context |
| 24 | + mockContext = { |
| 25 | + workspaceState: { |
| 26 | + get: vi.fn(), |
| 27 | + update: vi.fn(), |
| 28 | + }, |
| 29 | + globalState: { |
| 30 | + get: vi.fn(), |
| 31 | + update: vi.fn(), |
| 32 | + }, |
| 33 | + secrets: { |
| 34 | + get: vi.fn(), |
| 35 | + store: vi.fn(), |
| 36 | + }, |
| 37 | + } as any |
| 38 | + |
| 39 | + // Initialize config manager with mocks |
| 40 | + configManager = new CodeIndexConfigManager(mockContextProxy, testWorkspacePath, mockContext) |
| 41 | + }) |
| 42 | + |
| 43 | + describe("Workspace-specific settings", () => { |
| 44 | + it("should inherit global setting when workspace setting is not set", () => { |
| 45 | + // Mock global setting enabled |
| 46 | + vi.spyOn(mockContextProxy, "getGlobalState").mockReturnValue({ |
| 47 | + codebaseIndexEnabled: true, |
| 48 | + }) |
| 49 | + |
| 50 | + // Mock no workspace-specific setting |
| 51 | + vi.spyOn(mockContext.workspaceState, "get").mockReturnValue(undefined) |
| 52 | + |
| 53 | + // Should inherit global setting (true) |
| 54 | + expect(configManager.isFeatureEnabled).toBe(true) |
| 55 | + }) |
| 56 | + |
| 57 | + it("should use workspace setting when explicitly set to false", () => { |
| 58 | + // Mock global setting enabled |
| 59 | + vi.spyOn(mockContextProxy, "getGlobalState").mockReturnValue({ |
| 60 | + codebaseIndexEnabled: true, |
| 61 | + }) |
| 62 | + |
| 63 | + // Mock workspace-specific setting disabled |
| 64 | + const workspaceKey = `codebaseIndexEnabled_${Buffer.from(testWorkspacePath).toString("base64")}` |
| 65 | + vi.spyOn(mockContext.workspaceState, "get").mockImplementation((key) => { |
| 66 | + if (key === workspaceKey) return false |
| 67 | + return undefined |
| 68 | + }) |
| 69 | + |
| 70 | + // Create a new instance to trigger loadWorkspaceSettings |
| 71 | + const newConfigManager = new CodeIndexConfigManager(mockContextProxy, testWorkspacePath, mockContext) |
| 72 | + |
| 73 | + // Should use workspace setting (false) instead of global (true) |
| 74 | + expect(newConfigManager.getWorkspaceIndexEnabled(testWorkspacePath)).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + it("should use workspace setting when explicitly set to true", () => { |
| 78 | + // Mock global setting disabled |
| 79 | + vi.spyOn(mockContextProxy, "getGlobalState").mockReturnValue({ |
| 80 | + codebaseIndexEnabled: false, |
| 81 | + }) |
| 82 | + |
| 83 | + // Mock workspace-specific setting enabled |
| 84 | + const workspaceKey = `codebaseIndexEnabled_${Buffer.from(testWorkspacePath).toString("base64")}` |
| 85 | + vi.spyOn(mockContext.workspaceState, "get").mockImplementation((key) => { |
| 86 | + if (key === workspaceKey) return true |
| 87 | + return undefined |
| 88 | + }) |
| 89 | + |
| 90 | + // Create a new instance to trigger loadWorkspaceSettings |
| 91 | + const newConfigManager = new CodeIndexConfigManager(mockContextProxy, testWorkspacePath, mockContext) |
| 92 | + |
| 93 | + // Workspace setting should be true |
| 94 | + expect(newConfigManager.getWorkspaceIndexEnabled(testWorkspacePath)).toBe(true) |
| 95 | + // But overall feature should still be disabled due to global setting |
| 96 | + expect(newConfigManager.isFeatureEnabled).toBe(false) |
| 97 | + }) |
| 98 | + |
| 99 | + it("should persist workspace setting when changed", async () => { |
| 100 | + const updateSpy = vi.spyOn(mockContext.workspaceState, "update") |
| 101 | + |
| 102 | + await configManager.setWorkspaceIndexEnabled(testWorkspacePath, false) |
| 103 | + |
| 104 | + const expectedKey = `codebaseIndexEnabled_${Buffer.from(testWorkspacePath).toString("base64")}` |
| 105 | + expect(updateSpy).toHaveBeenCalledWith(expectedKey, false) |
| 106 | + }) |
| 107 | + |
| 108 | + it("should correctly identify when workspace has specific setting", () => { |
| 109 | + // No workspace-specific setting |
| 110 | + vi.spyOn(mockContext.workspaceState, "get").mockReturnValue(undefined) |
| 111 | + expect(configManager.hasWorkspaceSpecificSetting()).toBe(false) |
| 112 | + |
| 113 | + // With workspace-specific setting |
| 114 | + const workspaceKey = `codebaseIndexEnabled_${Buffer.from(testWorkspacePath).toString("base64")}` |
| 115 | + vi.spyOn(mockContext.workspaceState, "get").mockImplementation((key) => { |
| 116 | + if (key === workspaceKey) return true |
| 117 | + return undefined |
| 118 | + }) |
| 119 | + |
| 120 | + const newConfigManager = new CodeIndexConfigManager(mockContextProxy, testWorkspacePath, mockContext) |
| 121 | + newConfigManager.loadWorkspaceSettings() |
| 122 | + |
| 123 | + expect(newConfigManager.hasWorkspaceSpecificSetting()).toBe(true) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe("Multi-root workspace handling", () => { |
| 128 | + it("should handle different settings for different workspace folders", () => { |
| 129 | + const workspace1 = "/workspace1" |
| 130 | + const workspace2 = "/workspace2" |
| 131 | + |
| 132 | + // Mock different settings for each workspace |
| 133 | + vi.spyOn(mockContext.workspaceState, "get").mockImplementation((key) => { |
| 134 | + const key1 = `codebaseIndexEnabled_${Buffer.from(workspace1).toString("base64")}` |
| 135 | + const key2 = `codebaseIndexEnabled_${Buffer.from(workspace2).toString("base64")}` |
| 136 | + |
| 137 | + if (key === key1) return true |
| 138 | + if (key === key2) return false |
| 139 | + return undefined |
| 140 | + }) |
| 141 | + |
| 142 | + // Create managers for each workspace |
| 143 | + const manager1 = new CodeIndexConfigManager(mockContextProxy, workspace1, mockContext) |
| 144 | + const manager2 = new CodeIndexConfigManager(mockContextProxy, workspace2, mockContext) |
| 145 | + |
| 146 | + manager1.loadWorkspaceSettings() |
| 147 | + manager2.loadWorkspaceSettings() |
| 148 | + |
| 149 | + expect(manager1.getWorkspaceIndexEnabled(workspace1)).toBe(true) |
| 150 | + expect(manager2.getWorkspaceIndexEnabled(workspace2)).toBe(false) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe("Global setting disabled", () => { |
| 155 | + it("should always return false when global setting is disabled", () => { |
| 156 | + // Mock global setting disabled |
| 157 | + vi.spyOn(mockContextProxy, "getGlobalState").mockReturnValue({ |
| 158 | + codebaseIndexEnabled: false, |
| 159 | + }) |
| 160 | + |
| 161 | + // Even with workspace setting enabled |
| 162 | + const workspaceKey = `codebaseIndexEnabled_${Buffer.from(testWorkspacePath).toString("base64")}` |
| 163 | + vi.spyOn(mockContext.workspaceState, "get").mockImplementation((key) => { |
| 164 | + if (key === workspaceKey) return true |
| 165 | + return undefined |
| 166 | + }) |
| 167 | + |
| 168 | + const newConfigManager = new CodeIndexConfigManager(mockContextProxy, testWorkspacePath, mockContext) |
| 169 | + |
| 170 | + // Feature should be disabled |
| 171 | + expect(newConfigManager.isFeatureEnabled).toBe(false) |
| 172 | + }) |
| 173 | + }) |
| 174 | +}) |
0 commit comments