| 1 | import { FQSN, SecretResult, SecretType } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | afterEach, |
| 4 | beforeEach, |
| 5 | describe, |
| 6 | expect, |
| 7 | Mock, |
| 8 | test, |
| 9 | vi, |
| 10 | } from "vitest"; |
| 11 | import { IDE } from "../.."; |
| 12 | import { ControlPlaneClient } from "../../control-plane/client"; |
| 13 | import { LocalPlatformClient } from "./LocalPlatformClient"; |
| 14 | |
| 15 | vi.mock("../../util/paths", { spy: true }); |
| 16 | |
| 17 | describe("LocalPlatformClient", () => { |
| 18 | const testFQSN: FQSN = { |
| 19 | packageSlugs: [ |
| 20 | { |
| 21 | ownerSlug: "test-owner-slug", |
| 22 | packageSlug: "test-package-slug", |
| 23 | }, |
| 24 | ], |
| 25 | secretName: "TEST_CONTINUE_SECRET_KEY", |
| 26 | }; |
| 27 | const testFQSN2: FQSN = { |
| 28 | packageSlugs: [ |
| 29 | { |
| 30 | ownerSlug: "test-owner-slug-2", |
| 31 | packageSlug: "test-package-slug-2", |
| 32 | }, |
| 33 | ], |
| 34 | secretName: "TEST_WORKSPACE_SECRET_KEY", |
| 35 | }; |
| 36 | |
| 37 | const testResolvedFQSN: SecretResult = { |
| 38 | found: true, |
| 39 | fqsn: testFQSN, |
| 40 | secretLocation: { |
| 41 | secretName: testFQSN.secretName, |
| 42 | secretType: SecretType.Organization, |
| 43 | orgSlug: "test-org-slug", |
| 44 | }, |
| 45 | }; |
| 46 | |
| 47 | let testControlPlaneClient: ControlPlaneClient; |
| 48 | let testIde: IDE; |
| 49 | beforeEach( |
| 50 | /**dynamic import before each test for test isolation */ |
| 51 | async () => { |
| 52 | const testFixtures = await import("../../test/fixtures"); |
| 53 | testControlPlaneClient = testFixtures.testControlPlaneClient; |
| 54 | testIde = testFixtures.testIde; |
| 55 | }, |
| 56 | ); |
| 57 | |
| 58 | let secretValue: string; |
| 59 | let envKeyValues: Record<string, unknown>; |
| 60 | let envKeyValuesString: string; |
| 61 | beforeEach( |
| 62 | /**generate unique env key value pairs for each test */ |
| 63 | () => { |
| 64 | secretValue = Math.floor(Math.random() * 100) + ""; |
| 65 | envKeyValues = { |
| 66 | TEST_CONTINUE_SECRET_KEY: secretValue, |
| 67 | TEST_WORKSPACE_SECRET_KEY: secretValue + "-workspace", |
| 68 | }; |
| 69 | envKeyValuesString = Object.entries(envKeyValues) |
| 70 | .map(([key, value]) => `${key}=${value}`) |
| 71 | .join("\n"); |
| 72 | }, |
| 73 | ); |
| 74 | |
| 75 | afterEach(() => { |
| 76 | vi.resetAllMocks(); |
| 77 | vi.restoreAllMocks(); |
| 78 | vi.resetModules(); // clear dynamic imported module cache |
| 79 | }); |
| 80 | |
| 81 | test("should not be able to resolve FQSNs if they do not exist", async () => { |
| 82 | const localPlatformClient = new LocalPlatformClient( |
| 83 | null, |
| 84 | testControlPlaneClient, |
| 85 | testIde, |
| 86 | ); |
| 87 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 88 | |
| 89 | expect(resolvedFQSNs.length).toBeGreaterThan(0); |
| 90 | expect(resolvedFQSNs[0]?.found).toBe(false); |
| 91 | }); |
| 92 | |
| 93 | test("should be able to resolve FQSNs if they exist", async () => { |
| 94 | testControlPlaneClient.resolveFQSNs = vi.fn(async () => [testResolvedFQSN]); |
| 95 | const localPlatformClient = new LocalPlatformClient( |
| 96 | null, |
| 97 | testControlPlaneClient, |
| 98 | testIde, |
| 99 | ); |
| 100 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 101 | expect(testControlPlaneClient.resolveFQSNs).toHaveBeenCalled(); |
| 102 | expect(resolvedFQSNs).toEqual([testResolvedFQSN]); |
| 103 | expect(resolvedFQSNs[0]?.found).toBe(true); |
| 104 | }); |
| 105 | |
| 106 | describe("searches for secrets in local .env files", () => { |
| 107 | let getContinueDotEnv: Mock; |
| 108 | beforeEach(async () => { |
| 109 | const utilPaths = await import("../../util/paths"); |
| 110 | getContinueDotEnv = vi.fn(() => envKeyValues); |
| 111 | utilPaths.getContinueDotEnv = getContinueDotEnv; |
| 112 | }); |
| 113 | |
| 114 | test("should be able to get secrets from ~/.continue/.env files", async () => { |
| 115 | const localPlatformClient = new LocalPlatformClient( |
| 116 | null, |
| 117 | testControlPlaneClient, |
| 118 | testIde, |
| 119 | ); |
| 120 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 121 | expect(getContinueDotEnv).toHaveBeenCalled(); |
| 122 | expect(resolvedFQSNs.length).toBe(1); |
| 123 | expect( |
| 124 | (resolvedFQSNs[0] as SecretResult & { value: unknown })?.value, |
| 125 | ).toBe(secretValue); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | describe("should be able to get secrets from workspace .env files", () => { |
| 130 | test("should get secrets from <workspace>/.continue/.env and <workspace>/.env", async () => { |
| 131 | const originalIdeFileExists = testIde.fileExists; |
| 132 | testIde.fileExists = vi.fn(async (fileUri: string) => |
| 133 | fileUri.includes(".env") ? true : originalIdeFileExists(fileUri), |
| 134 | ); |
| 135 | |
| 136 | const originalIdeReadFile = testIde.readFile; |
| 137 | const randomValueForContinueDirDotEnv = |
| 138 | "continue-dir-" + Math.floor(Math.random() * 100); |
| 139 | const randomValueForWorkspaceDotEnv = |
| 140 | "dotenv-" + Math.floor(Math.random() * 100); |
| 141 | |
| 142 | testIde.readFile = vi.fn(async (fileUri: string) => { |
| 143 | // fileUri should contain .continue/.env and not .env |
| 144 | if (fileUri.match(/.*\.continue\/\.env.*/gi)?.length) { |
| 145 | return ( |
| 146 | envKeyValuesString.split("\n")[0] + randomValueForContinueDirDotEnv |
| 147 | ); |
| 148 | } |
| 149 | // filUri should contain .env and not .continue/.env |
| 150 | else if (fileUri.match(/.*(?<!\.continue\/)\.env.*/gi)?.length) { |
| 151 | return ( |
| 152 | envKeyValuesString.split("\n")[1] + randomValueForWorkspaceDotEnv |
| 153 | ); |
| 154 | } |
| 155 | return originalIdeReadFile(fileUri); |
| 156 | }); |
| 157 | |
| 158 | const localPlatformClient = new LocalPlatformClient( |
| 159 | null, |
| 160 | testControlPlaneClient, |
| 161 | testIde, |
| 162 | ); |
| 163 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([ |
| 164 | testFQSN, |
| 165 | testFQSN2, |
| 166 | ]); |
| 167 | |
| 168 | // both the secrets should be present as they are retrieved from different files |
| 169 | |
| 170 | expect(resolvedFQSNs.length).toBe(2); |
| 171 | |
| 172 | const continueDirSecretValue = ( |
| 173 | resolvedFQSNs[0] as SecretResult & { value: unknown } |
| 174 | )?.value; |
| 175 | const dotEnvSecretValue = ( |
| 176 | resolvedFQSNs[1] as SecretResult & { value: unknown } |
| 177 | )?.value; |
| 178 | expect(continueDirSecretValue).toContain(secretValue); |
| 179 | expect(continueDirSecretValue).toContain(randomValueForContinueDirDotEnv); |
| 180 | expect(dotEnvSecretValue).toContain(secretValue + "-workspace"); |
| 181 | expect(dotEnvSecretValue).toContain(randomValueForWorkspaceDotEnv); |
| 182 | }); |
| 183 | |
| 184 | test("should first get secrets from <workspace>/.continue/.env and then <workspace>/.env", async () => { |
| 185 | const originalIdeFileExists = testIde.fileExists; |
| 186 | testIde.fileExists = vi.fn(async (fileUri: string) => |
| 187 | fileUri.includes(".env") ? true : originalIdeFileExists(fileUri), |
| 188 | ); |
| 189 | |
| 190 | const randomValueForContinueDirDotEnv = |
| 191 | "continue-dir-" + Math.floor(Math.random() * 100); |
| 192 | const randomValueForWorkspaceDotEnv = |
| 193 | "dotenv-" + Math.floor(Math.random() * 100); |
| 194 | |
| 195 | const originalIdeReadFile = testIde.readFile; |
| 196 | testIde.readFile = vi.fn(async (fileUri: string) => { |
| 197 | // fileUri should contain .continue/.env and not .env |
| 198 | if (fileUri.match(/.*\.continue\/\.env.*/gi)?.length) { |
| 199 | return ( |
| 200 | envKeyValuesString.split("\n")[0] + randomValueForContinueDirDotEnv |
| 201 | ); |
| 202 | } |
| 203 | // filUri should contain .env and not .continue/.env |
| 204 | else if (fileUri.match(/.*(?<!\.continue\/)\.env.*/gi)?.length) { |
| 205 | return ( |
| 206 | envKeyValuesString.split("\n")[0] + randomValueForWorkspaceDotEnv |
| 207 | ); |
| 208 | } |
| 209 | return originalIdeReadFile(fileUri); |
| 210 | }); |
| 211 | |
| 212 | const localPlatformClient = new LocalPlatformClient( |
| 213 | null, |
| 214 | testControlPlaneClient, |
| 215 | testIde, |
| 216 | ); |
| 217 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 218 | |
| 219 | expect(resolvedFQSNs.length).toBe(1); |
| 220 | expect( |
| 221 | (resolvedFQSNs[0] as SecretResult & { value: unknown })?.value, |
| 222 | ).toContain(secretValue); |
| 223 | // we check that workspace <workspace>.continue/.env does not override the <workspace>/.env secret |
| 224 | expect( |
| 225 | (resolvedFQSNs[0] as SecretResult & { value: unknown })?.value, |
| 226 | ).toContain(randomValueForContinueDirDotEnv); |
| 227 | expect( |
| 228 | (resolvedFQSNs[0] as SecretResult & { value: unknown })?.value, |
| 229 | ).not.toContain(randomValueForWorkspaceDotEnv); |
| 230 | }); |
| 231 | }); |
| 232 | |
| 233 | describe("should be able to get secrets from process.env", () => { |
| 234 | const ogProcessEnv = { ...process.env }; |
| 235 | |
| 236 | beforeEach(async () => { |
| 237 | // Ensure secrets are not found in ControlPlane initially for these tests |
| 238 | const mockResolveFQSNsNotFound = async ( |
| 239 | fqsns: FQSN[], |
| 240 | ): Promise<(SecretResult | undefined)[]> => |
| 241 | fqsns.map((fqsn) => ({ |
| 242 | found: false, |
| 243 | fqsn, |
| 244 | secretLocation: { |
| 245 | secretName: fqsn.secretName, |
| 246 | secretType: SecretType.NotFound as SecretType.NotFound, |
| 247 | }, |
| 248 | })); |
| 249 | testControlPlaneClient.resolveFQSNs = vi.fn(mockResolveFQSNsNotFound); |
| 250 | |
| 251 | // Ensure secrets are not found in local .env files |
| 252 | const utilPaths = await import("../../util/paths"); |
| 253 | utilPaths.getContinueDotEnv = vi.fn(() => ({})); |
| 254 | |
| 255 | // Ensure secrets are not found in workspace .env files |
| 256 | testIde.fileExists = vi.fn(async () => false); |
| 257 | testIde.readFile = vi.fn(async () => ""); |
| 258 | |
| 259 | // Clear any potentially set process.env variables from previous tests in this block |
| 260 | delete process.env[testFQSN.secretName]; |
| 261 | delete process.env[testFQSN2.secretName]; |
| 262 | }); |
| 263 | |
| 264 | afterEach(() => { |
| 265 | // Restore original process.env |
| 266 | process.env = { ...ogProcessEnv }; |
| 267 | }); |
| 268 | |
| 269 | test("should resolve secret from process.env if not found elsewhere", async () => { |
| 270 | const processEnvSecretValue = "secret-from-process-env"; |
| 271 | process.env[testFQSN.secretName] = processEnvSecretValue; |
| 272 | |
| 273 | const localPlatformClient = new LocalPlatformClient( |
| 274 | null, |
| 275 | testControlPlaneClient, |
| 276 | testIde, |
| 277 | ); |
| 278 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 279 | |
| 280 | expect(resolvedFQSNs.length).toBe(1); |
| 281 | const result = resolvedFQSNs[0]; |
| 282 | expect(result?.found).toBe(true); |
| 283 | expect((result as SecretResult & { value: unknown })?.value).toBe( |
| 284 | processEnvSecretValue, |
| 285 | ); |
| 286 | expect(result?.secretLocation?.secretType).toBe(SecretType.ProcessEnv); |
| 287 | // Check if the specific ProcessEnvSecretLocation is correctly formed |
| 288 | expect(result?.secretLocation).toEqual( |
| 289 | expect.objectContaining({ |
| 290 | secretName: testFQSN.secretName, |
| 291 | secretType: SecretType.ProcessEnv, |
| 292 | }), |
| 293 | ); |
| 294 | }); |
| 295 | |
| 296 | test("should return not found if secret is not in process.env or other locations", async () => { |
| 297 | // Ensure it's not in process.env |
| 298 | expect(process.env[testFQSN.secretName]).toBeUndefined(); |
| 299 | |
| 300 | const localPlatformClient = new LocalPlatformClient( |
| 301 | null, |
| 302 | testControlPlaneClient, |
| 303 | testIde, |
| 304 | ); |
| 305 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 306 | |
| 307 | expect(resolvedFQSNs.length).toBe(1); |
| 308 | expect(resolvedFQSNs[0]?.found).toBe(false); |
| 309 | expect(resolvedFQSNs[0]?.secretLocation?.secretType).toBe( |
| 310 | SecretType.NotFound, |
| 311 | ); |
| 312 | }); |
| 313 | |
| 314 | test("should prioritize ControlPlane over process.env", async () => { |
| 315 | const controlPlaneValue = "secret-from-control-plane"; |
| 316 | |
| 317 | const mockResolveFQSNsControlPlaneFound = async (): Promise< |
| 318 | (SecretResult | undefined)[] |
| 319 | > => [ |
| 320 | { |
| 321 | found: true, |
| 322 | fqsn: testFQSN, |
| 323 | value: controlPlaneValue, |
| 324 | secretLocation: { |
| 325 | secretType: SecretType.Organization as SecretType.Organization, |
| 326 | orgSlug: (testResolvedFQSN.secretLocation as any).orgSlug, |
| 327 | secretName: testFQSN.secretName, |
| 328 | }, |
| 329 | }, |
| 330 | ]; |
| 331 | testControlPlaneClient.resolveFQSNs = vi.fn( |
| 332 | mockResolveFQSNsControlPlaneFound, |
| 333 | ); |
| 334 | |
| 335 | process.env[testFQSN.secretName] = |
| 336 | "secret-from-process-env-should-be-ignored"; |
| 337 | |
| 338 | const localPlatformClient = new LocalPlatformClient( |
| 339 | null, |
| 340 | testControlPlaneClient, |
| 341 | testIde, |
| 342 | ); |
| 343 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 344 | |
| 345 | expect(resolvedFQSNs.length).toBe(1); |
| 346 | const result = resolvedFQSNs[0]; |
| 347 | expect(result?.found).toBe(true); |
| 348 | expect((result as SecretResult & { value: unknown })?.value).toBe( |
| 349 | controlPlaneValue, |
| 350 | ); |
| 351 | expect(result?.secretLocation?.secretType).toBe(SecretType.Organization); |
| 352 | }); |
| 353 | |
| 354 | test("should prioritize local ~/.continue/.env file over process.env", async () => { |
| 355 | const localEnvFileValue = "secret-from-local-dot-continue-env"; |
| 356 | const utilPaths = await import("../../util/paths"); |
| 357 | utilPaths.getContinueDotEnv = vi.fn(() => ({ |
| 358 | [testFQSN.secretName]: localEnvFileValue, |
| 359 | })); |
| 360 | |
| 361 | process.env[testFQSN.secretName] = |
| 362 | "secret-from-process-env-should-be-ignored"; |
| 363 | |
| 364 | const localPlatformClient = new LocalPlatformClient( |
| 365 | null, |
| 366 | testControlPlaneClient, |
| 367 | testIde, |
| 368 | ); |
| 369 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 370 | |
| 371 | expect(resolvedFQSNs.length).toBe(1); |
| 372 | const result = resolvedFQSNs[0]; |
| 373 | expect(result?.found).toBe(true); |
| 374 | expect((result as SecretResult & { value: unknown })?.value).toBe( |
| 375 | localEnvFileValue, |
| 376 | ); |
| 377 | expect(result?.secretLocation?.secretType).toBe(SecretType.LocalEnv); |
| 378 | }); |
| 379 | |
| 380 | test("should prioritize workspace .env files over process.env", async () => { |
| 381 | const workspaceContinueEnvValue = "secret-from-workspace-continue-env"; |
| 382 | testIde.fileExists = vi.fn(async (fileUri: string) => |
| 383 | // Only mock existence for <workspace>/.continue/.env |
| 384 | fileUri.includes(".continue/.env"), |
| 385 | ); |
| 386 | testIde.readFile = vi.fn(async (fileUri: string) => { |
| 387 | if (fileUri.includes(".continue/.env")) { |
| 388 | return `${testFQSN.secretName}=${workspaceContinueEnvValue}`; |
| 389 | } |
| 390 | return ""; |
| 391 | }); |
| 392 | |
| 393 | process.env[testFQSN.secretName] = |
| 394 | "secret-from-process-env-should-be-ignored"; |
| 395 | |
| 396 | const localPlatformClient = new LocalPlatformClient( |
| 397 | null, |
| 398 | testControlPlaneClient, |
| 399 | testIde, |
| 400 | ); |
| 401 | const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]); |
| 402 | |
| 403 | expect(resolvedFQSNs.length).toBe(1); |
| 404 | const result = resolvedFQSNs[0]; |
| 405 | expect(result?.found).toBe(true); |
| 406 | expect((result as SecretResult & { value: unknown })?.value).toBe( |
| 407 | workspaceContinueEnvValue, |
| 408 | ); |
| 409 | // This should be LocalEnv because findSecretInEnvFiles returns LocalEnv for workspace files too |
| 410 | expect(result?.secretLocation?.secretType).toBe(SecretType.LocalEnv); |
| 411 | }); |
| 412 | }); |
| 413 | }); |