| 1 | import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | import { BrowserSerializedContinueConfig } from ".."; |
| 3 | |
| 4 | // Create the mock before importing anything else |
| 5 | const mockDecodeSecretLocation = vi.fn(); |
| 6 | |
| 7 | // Mock the module |
| 8 | vi.mock("@continuedev/config-yaml", () => ({ |
| 9 | SecretType: { |
| 10 | User: "user", |
| 11 | Organization: "organization", |
| 12 | FreeTrial: "free_trial", |
| 13 | }, |
| 14 | decodeSecretLocation: mockDecodeSecretLocation, |
| 15 | })); |
| 16 | |
| 17 | describe("usesFreeTrialApiKey", () => { |
| 18 | let usesFreeTrialApiKey: typeof import("./usesFreeTrialApiKey").usesFreeTrialApiKey; |
| 19 | let SecretType: typeof import("@continuedev/config-yaml").SecretType; |
| 20 | |
| 21 | beforeEach(async () => { |
| 22 | mockDecodeSecretLocation.mockReset(); |
| 23 | usesFreeTrialApiKey = (await import("./usesFreeTrialApiKey")) |
| 24 | .usesFreeTrialApiKey; |
| 25 | SecretType = (await import("@continuedev/config-yaml")).SecretType; |
| 26 | }); |
| 27 | |
| 28 | test("usesFreeTrialApiKey should return false when config is null", () => { |
| 29 | const result = usesFreeTrialApiKey(null); |
| 30 | expect(result).toBe(false); |
| 31 | }); |
| 32 | |
| 33 | test("usesFreeTrialApiKey should return false when config is undefined", () => { |
| 34 | const result = usesFreeTrialApiKey(undefined as any); |
| 35 | expect(result).toBe(false); |
| 36 | }); |
| 37 | |
| 38 | test("usesFreeTrialApiKey should return false when no models have apiKeyLocation", () => { |
| 39 | const config: BrowserSerializedContinueConfig = { |
| 40 | modelsByRole: { |
| 41 | chat: [ |
| 42 | { |
| 43 | title: "Model 1", |
| 44 | provider: "test", |
| 45 | model: "test-model", |
| 46 | underlyingProviderName: "test", |
| 47 | }, |
| 48 | { |
| 49 | title: "Model 2", |
| 50 | provider: "test", |
| 51 | model: "test-model-2", |
| 52 | underlyingProviderName: "test", |
| 53 | }, |
| 54 | ], |
| 55 | edit: [], |
| 56 | apply: [], |
| 57 | summarize: [], |
| 58 | autocomplete: [], |
| 59 | rerank: [], |
| 60 | embed: [], |
| 61 | }, |
| 62 | selectedModelByRole: { |
| 63 | chat: null, |
| 64 | edit: null, |
| 65 | apply: null, |
| 66 | summarize: null, |
| 67 | autocomplete: null, |
| 68 | rerank: null, |
| 69 | embed: null, |
| 70 | }, |
| 71 | contextProviders: [], |
| 72 | slashCommands: [], |
| 73 | tools: [], |
| 74 | mcpServerStatuses: [], |
| 75 | usePlatform: false, |
| 76 | rules: [], |
| 77 | }; |
| 78 | |
| 79 | const result = usesFreeTrialApiKey(config); |
| 80 | expect(result).toBe(false); |
| 81 | }); |
| 82 | |
| 83 | test("usesFreeTrialApiKey should return false when models have apiKeyLocation but none are free trial", () => { |
| 84 | const config: BrowserSerializedContinueConfig = { |
| 85 | modelsByRole: { |
| 86 | chat: [ |
| 87 | { |
| 88 | title: "Model 1", |
| 89 | provider: "test", |
| 90 | model: "test-model", |
| 91 | apiKeyLocation: "user:testuser/api-key", |
| 92 | underlyingProviderName: "test", |
| 93 | }, |
| 94 | ], |
| 95 | edit: [ |
| 96 | { |
| 97 | title: "Model 2", |
| 98 | provider: "test", |
| 99 | model: "test-model-2", |
| 100 | apiKeyLocation: "organization:testorg/api-key", |
| 101 | underlyingProviderName: "test", |
| 102 | }, |
| 103 | ], |
| 104 | apply: [], |
| 105 | summarize: [], |
| 106 | autocomplete: [], |
| 107 | rerank: [], |
| 108 | embed: [], |
| 109 | }, |
| 110 | selectedModelByRole: { |
| 111 | chat: null, |
| 112 | edit: null, |
| 113 | apply: null, |
| 114 | summarize: null, |
| 115 | autocomplete: null, |
| 116 | rerank: null, |
| 117 | embed: null, |
| 118 | }, |
| 119 | contextProviders: [], |
| 120 | slashCommands: [], |
| 121 | tools: [], |
| 122 | mcpServerStatuses: [], |
| 123 | usePlatform: false, |
| 124 | rules: [], |
| 125 | }; |
| 126 | |
| 127 | mockDecodeSecretLocation |
| 128 | .mockReturnValueOnce({ |
| 129 | secretType: SecretType.User, |
| 130 | userSlug: "testuser", |
| 131 | secretName: "api-key", |
| 132 | }) |
| 133 | .mockReturnValueOnce({ |
| 134 | secretType: SecretType.Organization, |
| 135 | orgSlug: "testorg", |
| 136 | secretName: "api-key", |
| 137 | }); |
| 138 | |
| 139 | const result = usesFreeTrialApiKey(config); |
| 140 | expect(result).toBe(false); |
| 141 | expect(mockDecodeSecretLocation).toHaveBeenCalledTimes(2); |
| 142 | }); |
| 143 | |
| 144 | test("usesFreeTrialApiKey should return true when at least one model uses free trial API key", () => { |
| 145 | const config: BrowserSerializedContinueConfig = { |
| 146 | modelsByRole: { |
| 147 | chat: [ |
| 148 | { |
| 149 | title: "Model 1", |
| 150 | provider: "test", |
| 151 | model: "test-model", |
| 152 | apiKeyLocation: "user:testuser/api-key", |
| 153 | underlyingProviderName: "test", |
| 154 | }, |
| 155 | { |
| 156 | title: "Free Trial Model", |
| 157 | provider: "test", |
| 158 | model: "free-trial-model", |
| 159 | apiKeyLocation: "free_trial:owner/package/api-key", |
| 160 | underlyingProviderName: "test", |
| 161 | }, |
| 162 | ], |
| 163 | edit: [], |
| 164 | apply: [], |
| 165 | summarize: [], |
| 166 | autocomplete: [], |
| 167 | rerank: [], |
| 168 | embed: [], |
| 169 | }, |
| 170 | selectedModelByRole: { |
| 171 | chat: null, |
| 172 | edit: null, |
| 173 | apply: null, |
| 174 | summarize: null, |
| 175 | autocomplete: null, |
| 176 | rerank: null, |
| 177 | embed: null, |
| 178 | }, |
| 179 | contextProviders: [], |
| 180 | slashCommands: [], |
| 181 | tools: [], |
| 182 | mcpServerStatuses: [], |
| 183 | usePlatform: false, |
| 184 | rules: [], |
| 185 | }; |
| 186 | |
| 187 | mockDecodeSecretLocation |
| 188 | .mockReturnValueOnce({ |
| 189 | secretType: SecretType.User, |
| 190 | userSlug: "testuser", |
| 191 | secretName: "api-key", |
| 192 | }) |
| 193 | .mockReturnValueOnce({ |
| 194 | secretType: SecretType.FreeTrial, |
| 195 | blockSlug: { ownerSlug: "owner", packageSlug: "package" }, |
| 196 | secretName: "api-key", |
| 197 | }); |
| 198 | |
| 199 | const result = usesFreeTrialApiKey(config); |
| 200 | expect(result).toBe(true); |
| 201 | expect(mockDecodeSecretLocation).toHaveBeenCalledTimes(2); |
| 202 | }); |
| 203 | |
| 204 | test("usesFreeTrialApiKey should return true when free trial model is in a different role", () => { |
| 205 | const config: BrowserSerializedContinueConfig = { |
| 206 | modelsByRole: { |
| 207 | chat: [ |
| 208 | { |
| 209 | title: "Model 1", |
| 210 | provider: "test", |
| 211 | model: "test-model", |
| 212 | apiKeyLocation: "user:testuser/api-key", |
| 213 | underlyingProviderName: "test", |
| 214 | }, |
| 215 | ], |
| 216 | edit: [ |
| 217 | { |
| 218 | title: "Free Trial Edit Model", |
| 219 | provider: "test", |
| 220 | model: "free-trial-edit-model", |
| 221 | apiKeyLocation: "free_trial:owner/package/api-key", |
| 222 | underlyingProviderName: "test", |
| 223 | }, |
| 224 | ], |
| 225 | apply: [], |
| 226 | summarize: [], |
| 227 | autocomplete: [], |
| 228 | rerank: [], |
| 229 | embed: [], |
| 230 | }, |
| 231 | selectedModelByRole: { |
| 232 | chat: null, |
| 233 | edit: null, |
| 234 | apply: null, |
| 235 | summarize: null, |
| 236 | autocomplete: null, |
| 237 | rerank: null, |
| 238 | embed: null, |
| 239 | }, |
| 240 | contextProviders: [], |
| 241 | slashCommands: [], |
| 242 | tools: [], |
| 243 | mcpServerStatuses: [], |
| 244 | usePlatform: false, |
| 245 | rules: [], |
| 246 | }; |
| 247 | |
| 248 | mockDecodeSecretLocation |
| 249 | .mockReturnValueOnce({ |
| 250 | secretType: SecretType.User, |
| 251 | userSlug: "testuser", |
| 252 | secretName: "api-key", |
| 253 | }) |
| 254 | .mockReturnValueOnce({ |
| 255 | secretType: SecretType.FreeTrial, |
| 256 | blockSlug: { ownerSlug: "owner", packageSlug: "package" }, |
| 257 | secretName: "api-key", |
| 258 | }); |
| 259 | |
| 260 | const result = usesFreeTrialApiKey(config); |
| 261 | expect(result).toBe(true); |
| 262 | }); |
| 263 | |
| 264 | test("usesFreeTrialApiKey should return false and log error when decodeSecretLocation throws", () => { |
| 265 | const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 266 | |
| 267 | const config: BrowserSerializedContinueConfig = { |
| 268 | modelsByRole: { |
| 269 | chat: [ |
| 270 | { |
| 271 | title: "Model 1", |
| 272 | provider: "test", |
| 273 | model: "test-model", |
| 274 | apiKeyLocation: "invalid-secret-location", |
| 275 | underlyingProviderName: "test", |
| 276 | }, |
| 277 | ], |
| 278 | edit: [], |
| 279 | apply: [], |
| 280 | summarize: [], |
| 281 | autocomplete: [], |
| 282 | rerank: [], |
| 283 | embed: [], |
| 284 | }, |
| 285 | selectedModelByRole: { |
| 286 | chat: null, |
| 287 | edit: null, |
| 288 | apply: null, |
| 289 | summarize: null, |
| 290 | autocomplete: null, |
| 291 | rerank: null, |
| 292 | embed: null, |
| 293 | }, |
| 294 | contextProviders: [], |
| 295 | slashCommands: [], |
| 296 | tools: [], |
| 297 | mcpServerStatuses: [], |
| 298 | usePlatform: false, |
| 299 | rules: [], |
| 300 | }; |
| 301 | |
| 302 | mockDecodeSecretLocation.mockImplementation(() => { |
| 303 | throw new Error("Invalid secret location format"); |
| 304 | }); |
| 305 | |
| 306 | const result = usesFreeTrialApiKey(config); |
| 307 | expect(result).toBe(false); |
| 308 | expect(consoleSpy).toHaveBeenCalledWith( |
| 309 | "Error checking for free trial API key:", |
| 310 | expect.any(Error), |
| 311 | ); |
| 312 | |
| 313 | consoleSpy.mockRestore(); |
| 314 | }); |
| 315 | |
| 316 | test("usesFreeTrialApiKey should handle empty modelsByRole object", () => { |
| 317 | const config: BrowserSerializedContinueConfig = { |
| 318 | modelsByRole: { |
| 319 | chat: [], |
| 320 | edit: [], |
| 321 | apply: [], |
| 322 | summarize: [], |
| 323 | autocomplete: [], |
| 324 | rerank: [], |
| 325 | embed: [], |
| 326 | }, |
| 327 | selectedModelByRole: { |
| 328 | chat: null, |
| 329 | edit: null, |
| 330 | apply: null, |
| 331 | summarize: null, |
| 332 | autocomplete: null, |
| 333 | rerank: null, |
| 334 | embed: null, |
| 335 | }, |
| 336 | contextProviders: [], |
| 337 | slashCommands: [], |
| 338 | tools: [], |
| 339 | mcpServerStatuses: [], |
| 340 | usePlatform: false, |
| 341 | rules: [], |
| 342 | }; |
| 343 | |
| 344 | const result = usesFreeTrialApiKey(config); |
| 345 | expect(result).toBe(false); |
| 346 | expect(mockDecodeSecretLocation).not.toHaveBeenCalled(); |
| 347 | }); |
| 348 | }); |