|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { ContextProxy } from "../contextProxy" |
| 3 | +import { logger } from "../../utils/logging" |
| 4 | +import { GLOBAL_STATE_KEYS, SECRET_KEYS } from "../../shared/globalState" |
| 5 | +import { ApiConfiguration } from "../../shared/api" |
| 6 | + |
| 7 | +// Mock shared/globalState |
| 8 | +jest.mock("../../shared/globalState", () => ({ |
| 9 | + GLOBAL_STATE_KEYS: ["apiProvider", "apiModelId", "mode"], |
| 10 | + SECRET_KEYS: ["apiKey", "openAiApiKey"], |
| 11 | + GlobalStateKey: {}, |
| 12 | + SecretKey: {}, |
| 13 | +})) |
| 14 | + |
| 15 | +// Mock shared/api |
| 16 | +jest.mock("../../shared/api", () => ({ |
| 17 | + API_CONFIG_KEYS: ["apiProvider", "apiModelId"], |
| 18 | + ApiConfiguration: {}, |
| 19 | +})) |
| 20 | + |
| 21 | +// Mock VSCode API |
| 22 | +jest.mock("vscode", () => ({ |
| 23 | + Uri: { |
| 24 | + file: jest.fn((path) => ({ path })), |
| 25 | + }, |
| 26 | + ExtensionMode: { |
| 27 | + Development: 1, |
| 28 | + Production: 2, |
| 29 | + Test: 3, |
| 30 | + }, |
| 31 | +})) |
| 32 | + |
| 33 | +describe("ContextProxy", () => { |
| 34 | + let proxy: ContextProxy |
| 35 | + let mockContext: any |
| 36 | + let mockGlobalState: any |
| 37 | + let mockSecrets: any |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + // Reset mocks |
| 41 | + jest.clearAllMocks() |
| 42 | + |
| 43 | + // Mock globalState |
| 44 | + mockGlobalState = { |
| 45 | + get: jest.fn(), |
| 46 | + update: jest.fn().mockResolvedValue(undefined), |
| 47 | + } |
| 48 | + |
| 49 | + // Mock secrets |
| 50 | + mockSecrets = { |
| 51 | + get: jest.fn().mockResolvedValue("test-secret"), |
| 52 | + store: jest.fn().mockResolvedValue(undefined), |
| 53 | + delete: jest.fn().mockResolvedValue(undefined), |
| 54 | + } |
| 55 | + |
| 56 | + // Mock the extension context |
| 57 | + mockContext = { |
| 58 | + globalState: mockGlobalState, |
| 59 | + secrets: mockSecrets, |
| 60 | + extensionUri: { path: "/test/extension" }, |
| 61 | + extensionPath: "/test/extension", |
| 62 | + globalStorageUri: { path: "/test/storage" }, |
| 63 | + logUri: { path: "/test/logs" }, |
| 64 | + extension: { packageJSON: { version: "1.0.0" } }, |
| 65 | + extensionMode: vscode.ExtensionMode.Development, |
| 66 | + } |
| 67 | + |
| 68 | + // Create proxy instance |
| 69 | + proxy = new ContextProxy(mockContext) |
| 70 | + }) |
| 71 | + |
| 72 | + describe("read-only pass-through properties", () => { |
| 73 | + it("should return extension properties from the original context", () => { |
| 74 | + expect(proxy.extensionUri).toBe(mockContext.extensionUri) |
| 75 | + expect(proxy.extensionPath).toBe(mockContext.extensionPath) |
| 76 | + expect(proxy.globalStorageUri).toBe(mockContext.globalStorageUri) |
| 77 | + expect(proxy.logUri).toBe(mockContext.logUri) |
| 78 | + expect(proxy.extension).toBe(mockContext.extension) |
| 79 | + expect(proxy.extensionMode).toBe(mockContext.extensionMode) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe("constructor", () => { |
| 84 | + it("should initialize state cache with all global state keys", () => { |
| 85 | + expect(mockGlobalState.get).toHaveBeenCalledTimes(GLOBAL_STATE_KEYS.length) |
| 86 | + for (const key of GLOBAL_STATE_KEYS) { |
| 87 | + expect(mockGlobalState.get).toHaveBeenCalledWith(key) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + it("should initialize secret cache with all secret keys", () => { |
| 92 | + expect(mockSecrets.get).toHaveBeenCalledTimes(SECRET_KEYS.length) |
| 93 | + for (const key of SECRET_KEYS) { |
| 94 | + expect(mockSecrets.get).toHaveBeenCalledWith(key) |
| 95 | + } |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe("getGlobalState", () => { |
| 100 | + it("should return value from cache when it exists", async () => { |
| 101 | + // Manually set a value in the cache |
| 102 | + await proxy.updateGlobalState("test-key", "cached-value") |
| 103 | + |
| 104 | + // Should return the cached value |
| 105 | + const result = proxy.getGlobalState("test-key") |
| 106 | + expect(result).toBe("cached-value") |
| 107 | + |
| 108 | + // Original context should be called once during updateGlobalState |
| 109 | + expect(mockGlobalState.get).toHaveBeenCalledTimes(GLOBAL_STATE_KEYS.length) // Only from initialization |
| 110 | + }) |
| 111 | + |
| 112 | + it("should handle default values correctly", async () => { |
| 113 | + // No value in cache |
| 114 | + const result = proxy.getGlobalState("unknown-key", "default-value") |
| 115 | + expect(result).toBe("default-value") |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + describe("updateGlobalState", () => { |
| 120 | + it("should update state directly in original context", async () => { |
| 121 | + await proxy.updateGlobalState("test-key", "new-value") |
| 122 | + |
| 123 | + // Should have called original context |
| 124 | + expect(mockGlobalState.update).toHaveBeenCalledWith("test-key", "new-value") |
| 125 | + |
| 126 | + // Should have stored the value in cache |
| 127 | + const storedValue = await proxy.getGlobalState("test-key") |
| 128 | + expect(storedValue).toBe("new-value") |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + describe("getSecret", () => { |
| 133 | + it("should return value from cache when it exists", async () => { |
| 134 | + // Manually set a value in the cache |
| 135 | + await proxy.storeSecret("api-key", "cached-secret") |
| 136 | + |
| 137 | + // Should return the cached value |
| 138 | + const result = proxy.getSecret("api-key") |
| 139 | + expect(result).toBe("cached-secret") |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + describe("storeSecret", () => { |
| 144 | + it("should store secret directly in original context", async () => { |
| 145 | + await proxy.storeSecret("api-key", "new-secret") |
| 146 | + |
| 147 | + // Should have called original context |
| 148 | + expect(mockSecrets.store).toHaveBeenCalledWith("api-key", "new-secret") |
| 149 | + |
| 150 | + // Should have stored the value in cache |
| 151 | + const storedValue = await proxy.getSecret("api-key") |
| 152 | + expect(storedValue).toBe("new-secret") |
| 153 | + }) |
| 154 | + |
| 155 | + it("should handle undefined value for secret deletion", async () => { |
| 156 | + await proxy.storeSecret("api-key", undefined) |
| 157 | + |
| 158 | + // Should have called delete on original context |
| 159 | + expect(mockSecrets.delete).toHaveBeenCalledWith("api-key") |
| 160 | + |
| 161 | + // Should have stored undefined in cache |
| 162 | + const storedValue = await proxy.getSecret("api-key") |
| 163 | + expect(storedValue).toBeUndefined() |
| 164 | + }) |
| 165 | + |
| 166 | + describe("getApiConfiguration", () => { |
| 167 | + it("should combine global state and secrets into a single ApiConfiguration object", async () => { |
| 168 | + // Mock data in state cache |
| 169 | + await proxy.updateGlobalState("apiProvider", "anthropic") |
| 170 | + await proxy.updateGlobalState("apiModelId", "test-model") |
| 171 | + // Mock data in secrets cache |
| 172 | + await proxy.storeSecret("apiKey", "test-api-key") |
| 173 | + |
| 174 | + const config = proxy.getApiConfiguration() |
| 175 | + |
| 176 | + // Should contain values from global state |
| 177 | + expect(config.apiProvider).toBe("anthropic") |
| 178 | + expect(config.apiModelId).toBe("test-model") |
| 179 | + // Should contain values from secrets |
| 180 | + expect(config.apiKey).toBe("test-api-key") |
| 181 | + }) |
| 182 | + |
| 183 | + it("should handle special case for apiProvider defaulting", async () => { |
| 184 | + // Clear apiProvider but set apiKey |
| 185 | + await proxy.updateGlobalState("apiProvider", undefined) |
| 186 | + await proxy.storeSecret("apiKey", "test-api-key") |
| 187 | + |
| 188 | + const config = proxy.getApiConfiguration() |
| 189 | + |
| 190 | + // Should default to anthropic when apiKey exists |
| 191 | + expect(config.apiProvider).toBe("anthropic") |
| 192 | + |
| 193 | + // Clear both apiProvider and apiKey |
| 194 | + await proxy.updateGlobalState("apiProvider", undefined) |
| 195 | + await proxy.storeSecret("apiKey", undefined) |
| 196 | + |
| 197 | + const configWithoutKey = proxy.getApiConfiguration() |
| 198 | + |
| 199 | + // Should default to openrouter when no apiKey exists |
| 200 | + expect(configWithoutKey.apiProvider).toBe("openrouter") |
| 201 | + }) |
| 202 | + }) |
| 203 | + |
| 204 | + describe("updateApiConfiguration", () => { |
| 205 | + it("should update both global state and secrets", async () => { |
| 206 | + const apiConfig: ApiConfiguration = { |
| 207 | + apiProvider: "anthropic", |
| 208 | + apiModelId: "claude-latest", |
| 209 | + apiKey: "test-api-key", |
| 210 | + } |
| 211 | + |
| 212 | + await proxy.updateApiConfiguration(apiConfig) |
| 213 | + |
| 214 | + // Should update global state |
| 215 | + expect(mockGlobalState.update).toHaveBeenCalledWith("apiProvider", "anthropic") |
| 216 | + expect(mockGlobalState.update).toHaveBeenCalledWith("apiModelId", "claude-latest") |
| 217 | + // Should update secrets |
| 218 | + expect(mockSecrets.store).toHaveBeenCalledWith("apiKey", "test-api-key") |
| 219 | + |
| 220 | + // Check that values are in cache |
| 221 | + expect(proxy.getGlobalState("apiProvider")).toBe("anthropic") |
| 222 | + expect(proxy.getGlobalState("apiModelId")).toBe("claude-latest") |
| 223 | + expect(proxy.getSecret("apiKey")).toBe("test-api-key") |
| 224 | + }) |
| 225 | + |
| 226 | + it("should ignore keys that aren't in either GLOBAL_STATE_KEYS or SECRET_KEYS", async () => { |
| 227 | + // Use type assertion to add an invalid key |
| 228 | + const apiConfig = { |
| 229 | + apiProvider: "anthropic", |
| 230 | + invalidKey: "should be ignored", |
| 231 | + } as ApiConfiguration & { invalidKey: string } |
| 232 | + |
| 233 | + await proxy.updateApiConfiguration(apiConfig) |
| 234 | + |
| 235 | + // Should update keys in GLOBAL_STATE_KEYS |
| 236 | + expect(mockGlobalState.update).toHaveBeenCalledWith("apiProvider", "anthropic") |
| 237 | + // Should not call update/store for invalid keys |
| 238 | + expect(mockGlobalState.update).not.toHaveBeenCalledWith("invalidKey", expect.anything()) |
| 239 | + expect(mockSecrets.store).not.toHaveBeenCalledWith("invalidKey", expect.anything()) |
| 240 | + }) |
| 241 | + }) |
| 242 | + }) |
| 243 | +}) |
0 commit comments