Elffuss BraidHub

kikocis/continue public

627d7d0d1f85 · 7.7 KiBRawPermalink
1import { FQSN, SecretResult, SecretType } from "@continuedev/config-yaml";
2import {
3 afterEach,
4 beforeEach,
5 describe,
6 expect,
7 Mock,
8 test,
9 vi,
10} from "vitest";
11import { IDE } from "../..";
12import { ControlPlaneClient } from "../../control-plane/client";
13import { LocalPlatformClient } from "./LocalPlatformClient";
14
15vi.mock("../../util/paths", { spy: true });
16
17describe("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});