Elffuss BraidHub

kikocis/continue public

main · 13.9 KiBRawPermalink
1import * as fs from "fs";
2import {
3 decodeSecretLocation,
4 FQSN,
5 PackageIdentifier,
6 packageIdentifierToShorthandSlug,
7 PlatformClient,
8 PlatformSecretStore,
9 Registry,
10 resolveFQSN,
11 resolveSecretLocationInProxy,
12 SecretLocation,
13 SecretResult,
14 SecretStore,
15 SecretType,
16 unrollAssistant,
17} from "../index.js";
18
19// Test e2e flows from raw yaml -> unroll -> client render -> resolve secrets on proxy
20describe("E2E Scenarios", () => {
21 const userSecrets: Record<string, string> = {
22 OPENAI_API_KEY: "sk-123",
23 };
24
25 const orgSecrets: Record<string, string> = {
26 GEMINI_API_KEY: "gemini-api-key",
27 };
28
29 const proxyEnvSecrets: Record<string, string> = {
30 ANTHROPIC_API_KEY: "sk-ant-env",
31 GEMINI_API_KEY: "gemini-api-key-env",
32 };
33
34 const localUserSecretStore: SecretStore = {
35 get: async function (secretName: string): Promise<string | undefined> {
36 return userSecrets[secretName];
37 },
38 set: function (secretName: string, secretValue: string): Promise<void> {
39 throw new Error("Function not implemented.");
40 },
41 };
42
43 const platformClient: PlatformClient = {
44 resolveFQSNs: async function (
45 fqsns: FQSN[],
46 ): Promise<(SecretResult | undefined)[]> {
47 return await Promise.all(
48 fqsns.map((fqsn) =>
49 resolveFQSN("test-user", fqsn, platformSecretStore, "test-org"),
50 ),
51 );
52 },
53 };
54
55 const environmentSecretStore: SecretStore = {
56 get: async function (secretName: string): Promise<string | undefined> {
57 return proxyEnvSecrets[secretName];
58 },
59 set: function (secretName: string, secretValue: string): Promise<void> {
60 throw new Error("Function not implemented.");
61 },
62 };
63
64 const platformSecretStore: PlatformSecretStore = {
65 getSecretFromSecretLocation: async function (
66 secretLocation: SecretLocation,
67 ): Promise<string | undefined> {
68 switch (secretLocation.secretType) {
69 case SecretType.Package:
70 return undefined;
71 case SecretType.User:
72 return userSecrets[secretLocation.secretName];
73 case SecretType.Organization:
74 return orgSecrets[secretLocation.secretName];
75 case SecretType.ModelsAddOn:
76 case SecretType.FreeTrial:
77 if (
78 secretLocation.blockSlug.ownerSlug === "test-org" &&
79 secretLocation.blockSlug.packageSlug === "claude35sonnet" &&
80 secretLocation.secretName === "ANTHROPIC_API_KEY"
81 ) {
82 return "sk-ant";
83 }
84 return undefined;
85 case SecretType.NotFound:
86 return undefined;
87 default:
88 return undefined;
89 }
90 },
91 };
92
93 const registry: Registry = {
94 getContent: async function (id: PackageIdentifier): Promise<string> {
95 const slug = packageIdentifierToShorthandSlug(id);
96 const filePath =
97 id.uriType === "slug"
98 ? `./src/__tests__/packages/${slug}.yaml`
99 : id.fileUri;
100 return fs.readFileSync(filePath).toString();
101 },
102 };
103
104 it("should unroll assistant with a single block that doesn't exist", async () => {
105 const unrolledConfig = await unrollAssistant(
106 {
107 uriType: "slug",
108 fullSlug: {
109 ownerSlug: "test-org",
110 packageSlug: "agent-with-non-existing-block",
111 versionSlug: "latest",
112 },
113 },
114 registry,
115 {
116 renderSecrets: true,
117 platformClient,
118 orgScopeId: "test-org",
119 currentUserSlug: "test-user",
120 onPremProxyUrl: null,
121 },
122 );
123
124 expect(unrolledConfig.config?.rules?.[0]).toBeNull();
125 });
126
127 it("should correctly unroll assistant", async () => {
128 const unrolledConfig = await unrollAssistant(
129 {
130 uriType: "slug",
131 fullSlug: {
132 ownerSlug: "test-org",
133 packageSlug: "agent",
134 versionSlug: "latest",
135 },
136 },
137 registry,
138 {
139 renderSecrets: true,
140 platformClient,
141 orgScopeId: "test-org",
142 currentUserSlug: "test-user",
143 onPremProxyUrl: null,
144 },
145 );
146
147 const config = unrolledConfig.config;
148
149 // Test that packages were correctly unrolled and params replaced
150 expect(config?.models?.length).toBe(4);
151
152 const openAiModel = config?.models?.[0]!;
153 expect(openAiModel.apiKey).toBe("sk-123");
154
155 const geminiModel = config?.models?.[1]!;
156 expect(geminiModel.provider).toBe("continue-proxy");
157 expect(geminiModel.apiKey).toBeUndefined();
158 const geminiSecretLocation = "organization:test-org/GEMINI_API_KEY";
159 expect((geminiModel as any).apiKeyLocation).toBe(geminiSecretLocation);
160
161 const anthropicModel = config?.models?.[2]!;
162 expect(anthropicModel.provider).toBe("continue-proxy");
163 expect(anthropicModel.apiKey).toBeUndefined();
164 const anthropicSecretLocation =
165 "models_add_on:test-org/claude35sonnet/ANTHROPIC_API_KEY";
166 expect((anthropicModel as any).apiKeyLocation).toBe(
167 anthropicSecretLocation,
168 );
169
170 const proxyOllamaModel = config?.models?.[3]!;
171 expect(proxyOllamaModel.provider).toBe("ollama");
172 expect(proxyOllamaModel.defaultCompletionOptions?.stream).toBe(false);
173
174 expect(config?.rules?.length).toBe(2);
175 expect(config?.docs?.[0]?.startUrl).toBe(
176 "https://docs.python.org/release/3.13.1",
177 );
178 expect(config?.docs?.[0]?.rootUrl).toBe(
179 "https://docs.python.org/release/3.13.1",
180 );
181
182 // Test that proxy can correctly resolve secrets
183 const decodedAnthropicSecretLocation = decodeSecretLocation(
184 anthropicSecretLocation,
185 );
186 const decodedGeminiSecretLocation =
187 decodeSecretLocation(geminiSecretLocation);
188
189 // With environment
190 const antSecretValue = await resolveSecretLocationInProxy(
191 decodedAnthropicSecretLocation,
192 platformSecretStore,
193 environmentSecretStore,
194 );
195 expect(antSecretValue).toBe("sk-ant-env");
196 const geminiSecretValue = await resolveSecretLocationInProxy(
197 decodedGeminiSecretLocation,
198 platformSecretStore,
199 environmentSecretStore,
200 );
201 expect(geminiSecretValue).toBe("gemini-api-key-env");
202
203 // Without environment
204 const antSecretValue2 = await resolveSecretLocationInProxy(
205 decodedAnthropicSecretLocation,
206 platformSecretStore,
207 undefined,
208 );
209 expect(antSecretValue2).toBe("sk-ant");
210 const geminiSecretValue2 = await resolveSecretLocationInProxy(
211 decodedGeminiSecretLocation,
212 platformSecretStore,
213 undefined,
214 );
215 expect(geminiSecretValue2).toBe("gemini-api-key");
216 });
217
218 it("should correctly unroll assistant with injected blocks", async () => {
219 const unrolledConfig = await unrollAssistant(
220 {
221 uriType: "slug",
222 fullSlug: {
223 ownerSlug: "test-org",
224 packageSlug: "agent",
225 versionSlug: "latest",
226 },
227 },
228 registry,
229 {
230 renderSecrets: true,
231 platformClient,
232 orgScopeId: "test-org",
233 currentUserSlug: "test-user",
234 onPremProxyUrl: null,
235 // Add injected blocks
236 injectBlocks: [
237 {
238 uriType: "slug",
239 fullSlug: {
240 ownerSlug: "test-org",
241 packageSlug: "rules",
242 versionSlug: "latest",
243 },
244 },
245 {
246 uriType: "file",
247 fileUri: "./src/__tests__/local-files/rules.yaml",
248 },
249 ],
250 },
251 );
252
253 const config = unrolledConfig.config;
254 const errors = unrolledConfig.errors;
255
256 // The original rules array should have two items
257 expect(config?.rules?.length).toBe(3); // Now 3 with the injected block
258
259 // Check the original doc is still there
260 expect(config?.docs?.[0]?.startUrl).toBe(
261 "https://docs.python.org/release/3.13.1",
262 );
263
264 // Check the injected doc block was added
265 expect(
266 typeof config?.rules?.[2] !== "string" &&
267 config?.rules?.[2]?.rule === "Be humble",
268 );
269
270 // Check if we receive one error that is caused by duplicate rules
271 expect(errors?.length).toBe(1);
272 expect(errors?.[0].message.includes("Duplicate rules detected"));
273 });
274
275 it("duplicate detection should happen in the assistant config first and then the intected blocks", async () => {
276 const unrolledConfig = await unrollAssistant(
277 {
278 uriType: "file",
279 fileUri: "./src/__tests__/local-files/duplicate-test-agent.yaml",
280 },
281 registry,
282 {
283 renderSecrets: true,
284 platformClient,
285 orgScopeId: "test-org",
286 currentUserSlug: "test-user",
287 onPremProxyUrl: null,
288 // Add injected blocks
289 injectBlocks: [
290 {
291 uriType: "file",
292 fileUri: "./src/__tests__/local-files/rules.yaml",
293 },
294 {
295 uriType: "file",
296 fileUri: "./src/__tests__/local-files/mcpServer.yaml",
297 },
298 {
299 uriType: "file",
300 fileUri: "./src/__tests__/local-files/prompt.yaml",
301 },
302 ],
303 },
304 );
305
306 const config = unrolledConfig.config;
307 const errors = unrolledConfig.errors;
308
309 // Check if all the duplicate blocks get removed
310 expect(config?.models?.length).toBe(1);
311 expect(config?.context?.length).toBe(1);
312 expect(config?.mcpServers?.length).toBe(1);
313 expect(config?.rules?.length).toBe(1);
314 expect(config?.prompts?.length).toBe(1);
315 expect(config?.docs?.length).toBe(1);
316
317 // Check if there are 8 duplication detected
318 expect(errors?.length).toBe(8);
319
320 // Beginning of the assistant config duplication check
321 expect(
322 errors?.[0].message.includes("Duplicate models named gpt-5 detected"),
323 ).toBe(true);
324 // Beginning of the injected blocks duplication check
325 expect(errors?.[4].message.includes("Duplicate rules detected")).toBe(true);
326 expect(
327 errors?.[6].message.includes(
328 "Duplicate mcpServers named Browser search detected",
329 ),
330 ).toBe(true);
331 });
332
333 it("should throw when a block is blocklisted", async () => {
334 const result = await unrollAssistant(
335 {
336 uriType: "slug",
337 fullSlug: {
338 ownerSlug: "test-org",
339 packageSlug: "agent",
340 versionSlug: "latest",
341 },
342 },
343 registry,
344 {
345 renderSecrets: true,
346 platformClient,
347 orgScopeId: "test-org",
348 currentUserSlug: "test-user",
349 onPremProxyUrl: null,
350 blocklistedBlocks: [
351 {
352 ownerSlug: "test-org",
353 packageSlug: "gemini",
354 },
355 ],
356 },
357 );
358
359 // Should contain an error about the blocklisted block
360 expect(result.errors).toBeDefined();
361 expect(result.errors?.length).toBeGreaterThan(0);
362 expect(
363 result.errors?.some((error) =>
364 error.message.includes(
365 "test-org/gemini is block listed and can not be used.",
366 ),
367 ),
368 ).toBe(true);
369 });
370
371 it("should return an error when a block is not on the allowlist", async () => {
372 const result = await unrollAssistant(
373 {
374 uriType: "slug",
375 fullSlug: {
376 ownerSlug: "test-org",
377 packageSlug: "agent",
378 versionSlug: "latest",
379 },
380 },
381 registry,
382 {
383 renderSecrets: true,
384 platformClient,
385 orgScopeId: "test-org",
386 currentUserSlug: "test-user",
387 onPremProxyUrl: null,
388 allowlistedBlocks: [
389 {
390 ownerSlug: "test-org",
391 packageSlug: "docs",
392 },
393 {
394 ownerSlug: "test-org",
395 packageSlug: "rules",
396 },
397 {
398 ownerSlug: "test-org",
399 packageSlug: "claude35sonnet",
400 },
401 ],
402 },
403 );
404
405 // Should contain an error about the non-allowlisted block (gemini)
406 expect(result.errors).toBeDefined();
407 expect(result.errors?.length).toBeGreaterThan(0);
408 expect(
409 result.errors?.some((error) =>
410 error.message.includes(
411 "test-org/gemini is block listed and can not be used.",
412 ),
413 ),
414 ).toBe(true);
415 });
416
417 it("should allow blocks when they are on the allowlist", async () => {
418 const result = await unrollAssistant(
419 {
420 uriType: "slug",
421 fullSlug: {
422 ownerSlug: "test-org",
423 packageSlug: "agent",
424 versionSlug: "latest",
425 },
426 },
427 registry,
428 {
429 renderSecrets: true,
430 platformClient,
431 orgScopeId: "test-org",
432 currentUserSlug: "test-user",
433 onPremProxyUrl: null,
434 allowlistedBlocks: [
435 {
436 ownerSlug: "test-org",
437 packageSlug: "docs",
438 },
439 {
440 ownerSlug: "test-org",
441 packageSlug: "rules",
442 },
443 {
444 ownerSlug: "test-org",
445 packageSlug: "gemini",
446 },
447 {
448 ownerSlug: "test-org",
449 packageSlug: "claude35sonnet",
450 },
451 ],
452 },
453 );
454
455 // Should successfully unroll without errors for allowed blocks
456 expect(result.config?.models?.length).toBe(4);
457 expect(result.config?.rules?.length).toBe(2);
458 expect(result.config?.docs?.length).toBe(1);
459 });
460
461 it("should not affect file-based blocks with allow/blocklists", async () => {
462 const result = await unrollAssistant(
463 {
464 uriType: "file",
465 fileUri: "./src/__tests__/local-files/duplicate-test-agent.yaml",
466 },
467 registry,
468 {
469 renderSecrets: true,
470 platformClient,
471 orgScopeId: "test-org",
472 currentUserSlug: "test-user",
473 onPremProxyUrl: null,
474 blocklistedBlocks: [
475 {
476 ownerSlug: "test-org",
477 packageSlug: "docs",
478 },
479 ],
480 injectBlocks: [
481 {
482 uriType: "file",
483 fileUri: "./src/__tests__/local-files/rules.yaml",
484 },
485 ],
486 },
487 );
488
489 // File-based blocks should not be affected by blocklists
490 expect(result.config?.models?.length).toBe(1);
491 expect(result.config?.context?.length).toBe(1);
492 expect(result.config?.rules?.length).toBeGreaterThan(0);
493 });
494
495 it.skip("should prioritize org over user / package secrets", () => {});
496});