| 1 | import * as fs from "fs"; |
| 2 | import { |
| 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 |
| 20 | describe("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.filePath; |
| 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: "assistant-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: "assistant", |
| 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: "assistant", |
| 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 | filePath: "./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 | filePath: "./src/__tests__/local-files/duplicate-test-assistant.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 | filePath: "./src/__tests__/local-files/rules.yaml", |
| 293 | }, |
| 294 | { |
| 295 | uriType: "file", |
| 296 | filePath: "./src/__tests__/local-files/mcpServer.yaml", |
| 297 | }, |
| 298 | { |
| 299 | uriType: "file", |
| 300 | filePath: "./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.skip("should prioritize org over user / package secrets", () => {}); |
| 334 | }); |