Merge branch 'main' into pe/config-yaml-sourcemap
This commit is contained in:
commit
e4d7ea1f6d
7 changed files with 2021 additions and 5 deletions
1
.github/workflows/pr_checks.yaml
vendored
1
.github/workflows/pr_checks.yaml
vendored
|
|
@ -408,6 +408,7 @@ jobs:
|
|||
run: |
|
||||
cd core
|
||||
npm test
|
||||
npm run vitest
|
||||
env:
|
||||
IGNORE_API_KEY_TESTS: ${{ github.event.pull_request.head.repo.fork == true || github.actor == 'dependabot[bot]' }}
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
|
|
|
|||
233
core/config/yaml/LocalPlatformClient.test.ts
Normal file
233
core/config/yaml/LocalPlatformClient.test.ts
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
import { FQSN, SecretResult, SecretType } from "@continuedev/config-yaml";
|
||||
import {
|
||||
afterEach,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
Mock,
|
||||
test,
|
||||
vi,
|
||||
} from "vitest";
|
||||
import { IDE } from "../..";
|
||||
import { ControlPlaneClient } from "../../control-plane/client";
|
||||
import { LocalPlatformClient } from "./LocalPlatformClient";
|
||||
|
||||
vi.mock("../../util/paths", { spy: true });
|
||||
|
||||
describe("LocalPlatformClient", () => {
|
||||
const testFQSN: FQSN = {
|
||||
packageSlugs: [
|
||||
{
|
||||
ownerSlug: "test-owner-slug",
|
||||
packageSlug: "test-package-slug",
|
||||
},
|
||||
],
|
||||
secretName: "TEST_CONTINUE_SECRET_KEY",
|
||||
};
|
||||
const testFQSN2: FQSN = {
|
||||
packageSlugs: [
|
||||
{
|
||||
ownerSlug: "test-owner-slug-2",
|
||||
packageSlug: "test-package-slug-2",
|
||||
},
|
||||
],
|
||||
secretName: "TEST_WORKSPACE_SECRET_KEY",
|
||||
};
|
||||
|
||||
const testResolvedFQSN: SecretResult = {
|
||||
found: true,
|
||||
fqsn: testFQSN,
|
||||
secretLocation: {
|
||||
secretName: testFQSN.secretName,
|
||||
secretType: SecretType.Organization,
|
||||
orgSlug: "test-org-slug",
|
||||
},
|
||||
};
|
||||
|
||||
let testControlPlaneClient: ControlPlaneClient;
|
||||
let testIde: IDE;
|
||||
beforeEach(
|
||||
/**dynamic import before each test for test isolation */
|
||||
async () => {
|
||||
const testFixtures = await import("../../test/fixtures");
|
||||
testControlPlaneClient = testFixtures.testControlPlaneClient;
|
||||
testIde = testFixtures.testIde;
|
||||
},
|
||||
);
|
||||
|
||||
let secretValue: string;
|
||||
let envKeyValues: Record<string, unknown>;
|
||||
let envKeyValuesString: string;
|
||||
beforeEach(
|
||||
/**generate unique env key value pairs for each test */
|
||||
() => {
|
||||
secretValue = Math.floor(Math.random() * 100) + "";
|
||||
envKeyValues = {
|
||||
TEST_CONTINUE_SECRET_KEY: secretValue,
|
||||
TEST_WORKSPACE_SECRET_KEY: secretValue + "-workspace",
|
||||
};
|
||||
envKeyValuesString = Object.entries(envKeyValues)
|
||||
.map(([key, value]) => `${key}=${value}`)
|
||||
.join("\n");
|
||||
},
|
||||
);
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.restoreAllMocks();
|
||||
vi.resetModules(); // clear dynamic imported module cache
|
||||
});
|
||||
|
||||
test("should not be able to resolve FQSNs if they do not exist", async () => {
|
||||
const localPlatformClient = new LocalPlatformClient(
|
||||
null,
|
||||
testControlPlaneClient,
|
||||
testIde,
|
||||
);
|
||||
const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]);
|
||||
|
||||
expect(resolvedFQSNs.length).toBeGreaterThan(0);
|
||||
expect(resolvedFQSNs[0]?.found).toBe(false);
|
||||
});
|
||||
|
||||
test("should be able to resolve FQSNs if they exist", async () => {
|
||||
testControlPlaneClient.resolveFQSNs = vi.fn(async () => [testResolvedFQSN]);
|
||||
const localPlatformClient = new LocalPlatformClient(
|
||||
null,
|
||||
testControlPlaneClient,
|
||||
testIde,
|
||||
);
|
||||
const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]);
|
||||
expect(testControlPlaneClient.resolveFQSNs).toHaveBeenCalled();
|
||||
expect(resolvedFQSNs).toEqual([testResolvedFQSN]);
|
||||
expect(resolvedFQSNs[0]?.found).toBe(true);
|
||||
});
|
||||
|
||||
describe("searches for secrets in local .env files", () => {
|
||||
let getContinueDotEnv: Mock;
|
||||
beforeEach(async () => {
|
||||
const utilPaths = await import("../../util/paths");
|
||||
getContinueDotEnv = vi.fn(() => envKeyValues);
|
||||
utilPaths.getContinueDotEnv = getContinueDotEnv;
|
||||
});
|
||||
|
||||
test("should be able to get secrets from ~/.continue/.env files", async () => {
|
||||
const localPlatformClient = new LocalPlatformClient(
|
||||
null,
|
||||
testControlPlaneClient,
|
||||
testIde,
|
||||
);
|
||||
const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]);
|
||||
expect(getContinueDotEnv).toHaveBeenCalled();
|
||||
expect(resolvedFQSNs.length).toBe(1);
|
||||
expect(
|
||||
(resolvedFQSNs[0] as SecretResult & { value: unknown })?.value,
|
||||
).toBe(secretValue);
|
||||
console.log("debug1 resolved fqsn", resolvedFQSNs);
|
||||
});
|
||||
});
|
||||
|
||||
describe("should be able to get secrets from workspace .env files", () => {
|
||||
test("should get secrets from <workspace>/.continue/.env and <workspace>/.env", async () => {
|
||||
const originalIdeFileExists = testIde.fileExists;
|
||||
testIde.fileExists = vi.fn(async (fileUri: string) =>
|
||||
fileUri.includes(".env") ? true : originalIdeFileExists(fileUri),
|
||||
);
|
||||
|
||||
const originalIdeReadFile = testIde.readFile;
|
||||
const randomValueForContinueDirDotEnv =
|
||||
"continue-dir-" + Math.floor(Math.random() * 100);
|
||||
const randomValueForWorkspaceDotEnv =
|
||||
"dotenv-" + Math.floor(Math.random() * 100);
|
||||
|
||||
testIde.readFile = vi.fn(async (fileUri: string) => {
|
||||
// fileUri should contain .continue/.env and not .env
|
||||
if (fileUri.match(/.*\.continue\/\.env.*/gi)?.length) {
|
||||
return (
|
||||
envKeyValuesString.split("\n")[0] + randomValueForContinueDirDotEnv
|
||||
);
|
||||
}
|
||||
// filUri should contain .env and not .continue/.env
|
||||
else if (fileUri.match(/.*(?<!\.continue\/)\.env.*/gi)?.length) {
|
||||
return (
|
||||
envKeyValuesString.split("\n")[1] + randomValueForWorkspaceDotEnv
|
||||
);
|
||||
}
|
||||
return originalIdeReadFile(fileUri);
|
||||
});
|
||||
|
||||
const localPlatformClient = new LocalPlatformClient(
|
||||
null,
|
||||
testControlPlaneClient,
|
||||
testIde,
|
||||
);
|
||||
const resolvedFQSNs = await localPlatformClient.resolveFQSNs([
|
||||
testFQSN,
|
||||
testFQSN2,
|
||||
]);
|
||||
|
||||
// both the secrets should be present as they are retrieved from different files
|
||||
|
||||
expect(resolvedFQSNs.length).toBe(2);
|
||||
|
||||
const continueDirSecretValue = (
|
||||
resolvedFQSNs[0] as SecretResult & { value: unknown }
|
||||
)?.value;
|
||||
const dotEnvSecretValue = (
|
||||
resolvedFQSNs[1] as SecretResult & { value: unknown }
|
||||
)?.value;
|
||||
expect(continueDirSecretValue).toContain(secretValue);
|
||||
expect(continueDirSecretValue).toContain(randomValueForContinueDirDotEnv);
|
||||
expect(dotEnvSecretValue).toContain(secretValue + "-workspace");
|
||||
expect(dotEnvSecretValue).toContain(randomValueForWorkspaceDotEnv);
|
||||
});
|
||||
|
||||
test("should first get secrets from <workspace>/.continue/.env and then <workspace>/.env", async () => {
|
||||
const originalIdeFileExists = testIde.fileExists;
|
||||
testIde.fileExists = vi.fn(async (fileUri: string) =>
|
||||
fileUri.includes(".env") ? true : originalIdeFileExists(fileUri),
|
||||
);
|
||||
|
||||
const randomValueForContinueDirDotEnv =
|
||||
"continue-dir-" + Math.floor(Math.random() * 100);
|
||||
const randomValueForWorkspaceDotEnv =
|
||||
"dotenv-" + Math.floor(Math.random() * 100);
|
||||
|
||||
const originalIdeReadFile = testIde.readFile;
|
||||
testIde.readFile = vi.fn(async (fileUri: string) => {
|
||||
// fileUri should contain .continue/.env and not .env
|
||||
if (fileUri.match(/.*\.continue\/\.env.*/gi)?.length) {
|
||||
return (
|
||||
envKeyValuesString.split("\n")[0] + randomValueForContinueDirDotEnv
|
||||
);
|
||||
}
|
||||
// filUri should contain .env and not .continue/.env
|
||||
else if (fileUri.match(/.*(?<!\.continue\/)\.env.*/gi)?.length) {
|
||||
return (
|
||||
envKeyValuesString.split("\n")[0] + randomValueForWorkspaceDotEnv
|
||||
);
|
||||
}
|
||||
return originalIdeReadFile(fileUri);
|
||||
});
|
||||
|
||||
const localPlatformClient = new LocalPlatformClient(
|
||||
null,
|
||||
testControlPlaneClient,
|
||||
testIde,
|
||||
);
|
||||
const resolvedFQSNs = await localPlatformClient.resolveFQSNs([testFQSN]);
|
||||
|
||||
expect(resolvedFQSNs.length).toBe(1);
|
||||
expect(
|
||||
(resolvedFQSNs[0] as SecretResult & { value: unknown })?.value,
|
||||
).toContain(secretValue);
|
||||
// we check that workspace <workspace>.continue/.env does not override the <workspace>/.env secret
|
||||
expect(
|
||||
(resolvedFQSNs[0] as SecretResult & { value: unknown })?.value,
|
||||
).toContain(randomValueForContinueDirDotEnv);
|
||||
expect(
|
||||
(resolvedFQSNs[0] as SecretResult & { value: unknown })?.value,
|
||||
).not.toContain(randomValueForWorkspaceDotEnv);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -17,12 +17,16 @@ export class LocalPlatformClient implements PlatformClient {
|
|||
private readonly ide: IDE,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* searches for the first valid secret file in order of ~/.continue/.env, <workspace>/.continue/.env, <workspace>/.env
|
||||
*/
|
||||
private async findSecretInEnvFiles(
|
||||
fqsn: FQSN,
|
||||
): Promise<SecretResult | undefined> {
|
||||
const secretValue =
|
||||
this.findSecretInLocalEnvFile(fqsn) ??
|
||||
(await this.findSecretInWorkspaceEnvFiles(fqsn));
|
||||
(await this.findSecretInWorkspaceEnvFiles(fqsn, true)) ??
|
||||
(await this.findSecretInWorkspaceEnvFiles(fqsn, false));
|
||||
|
||||
if (secretValue) {
|
||||
return {
|
||||
|
|
@ -52,12 +56,16 @@ export class LocalPlatformClient implements PlatformClient {
|
|||
|
||||
private async findSecretInWorkspaceEnvFiles(
|
||||
fqsn: FQSN,
|
||||
insideContinue: boolean,
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
const workspaceDirs = await this.ide.getWorkspaceDirs();
|
||||
|
||||
for (const folder of workspaceDirs) {
|
||||
const envFilePath = joinPathsToUri(folder, ".env");
|
||||
const envFilePath = joinPathsToUri(
|
||||
folder,
|
||||
insideContinue ? ".continue" : "",
|
||||
".env",
|
||||
);
|
||||
try {
|
||||
const fileExists = await this.ide.fileExists(envFilePath);
|
||||
if (fileExists) {
|
||||
|
|
|
|||
|
|
@ -29,4 +29,7 @@ export default {
|
|||
globalSetup: "<rootDir>/test/jest.global-setup.ts",
|
||||
setupFilesAfterEnv: ["<rootDir>/test/jest.setup-after-env.js"],
|
||||
maxWorkers: 1, // equivalent to CLI --runInBand
|
||||
modulePathIgnorePatterns: [
|
||||
"<rootDir>/config/yaml/LocalPlatformClient.test.ts",
|
||||
],
|
||||
};
|
||||
|
|
|
|||
1766
core/package-lock.json
generated
1766
core/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,6 +4,7 @@
|
|||
"description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js",
|
||||
"scripts": {
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
|
||||
"vitest": "vitest run LocalPlatformClient",
|
||||
"test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage && open ./coverage/lcov-report/index.html",
|
||||
"tsc:check": "tsc -p ./ --noEmit",
|
||||
"build:npm": "tsc -p ./tsconfig.npm.json",
|
||||
|
|
@ -41,7 +42,8 @@
|
|||
"onnxruntime-common": "1.14.0",
|
||||
"onnxruntime-web": "1.14.0",
|
||||
"ts-jest": "^29.1.1",
|
||||
"typescript": "^5.6.3"
|
||||
"typescript": "^5.6.3",
|
||||
"vitest": "^3.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-bedrock-runtime": "^3.779.0",
|
||||
|
|
|
|||
5
core/vitest.config.ts
Normal file
5
core/vitest.config.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {},
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue