| 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 | return fs |
| 97 | .readFileSync(`./src/__tests__/packages/${slug}.yaml`) |
| 98 | .toString(); |
| 99 | }, |
| 100 | }; |
| 101 | |
| 102 | it("should unroll assistant with a single block that doesn't exist", async () => { |
| 103 | const unrolledConfig = await unrollAssistant( |
| 104 | { |
| 105 | uriType: "slug", |
| 106 | fullSlug: { |
| 107 | ownerSlug: "test-org", |
| 108 | packageSlug: "assistant-with-non-existing-block", |
| 109 | versionSlug: "latest", |
| 110 | }, |
| 111 | }, |
| 112 | registry, |
| 113 | { |
| 114 | renderSecrets: true, |
| 115 | platformClient, |
| 116 | orgScopeId: "test-org", |
| 117 | currentUserSlug: "test-user", |
| 118 | onPremProxyUrl: null, |
| 119 | }, |
| 120 | ); |
| 121 | |
| 122 | expect(unrolledConfig.config?.rules?.[0]).toBeNull(); |
| 123 | }); |
| 124 | |
| 125 | it("should correctly unroll assistant", async () => { |
| 126 | const unrolledConfig = await unrollAssistant( |
| 127 | { |
| 128 | uriType: "slug", |
| 129 | fullSlug: { |
| 130 | ownerSlug: "test-org", |
| 131 | packageSlug: "assistant", |
| 132 | versionSlug: "latest", |
| 133 | }, |
| 134 | }, |
| 135 | registry, |
| 136 | { |
| 137 | renderSecrets: true, |
| 138 | platformClient, |
| 139 | orgScopeId: "test-org", |
| 140 | currentUserSlug: "test-user", |
| 141 | onPremProxyUrl: null, |
| 142 | }, |
| 143 | ); |
| 144 | |
| 145 | const config = unrolledConfig.config; |
| 146 | |
| 147 | // Test that packages were correctly unrolled and params replaced |
| 148 | expect(config?.models?.length).toBe(4); |
| 149 | |
| 150 | const openAiModel = config?.models?.[0]!; |
| 151 | expect(openAiModel.apiKey).toBe("sk-123"); |
| 152 | |
| 153 | const geminiModel = config?.models?.[1]!; |
| 154 | expect(geminiModel.provider).toBe("continue-proxy"); |
| 155 | expect(geminiModel.apiKey).toBeUndefined(); |
| 156 | const geminiSecretLocation = "organization:test-org/GEMINI_API_KEY"; |
| 157 | expect((geminiModel as any).apiKeyLocation).toBe(geminiSecretLocation); |
| 158 | |
| 159 | const anthropicModel = config?.models?.[2]!; |
| 160 | expect(anthropicModel.provider).toBe("continue-proxy"); |
| 161 | expect(anthropicModel.apiKey).toBeUndefined(); |
| 162 | const anthropicSecretLocation = |
| 163 | "models_add_on:test-org/claude35sonnet/ANTHROPIC_API_KEY"; |
| 164 | expect((anthropicModel as any).apiKeyLocation).toBe( |
| 165 | anthropicSecretLocation, |
| 166 | ); |
| 167 | |
| 168 | const proxyOllamaModel = config?.models?.[3]!; |
| 169 | expect(proxyOllamaModel.provider).toBe("ollama"); |
| 170 | expect(proxyOllamaModel.defaultCompletionOptions?.stream).toBe(false); |
| 171 | |
| 172 | expect(config?.rules?.length).toBe(2); |
| 173 | expect(config?.docs?.[0]?.startUrl).toBe( |
| 174 | "https://docs.python.org/release/3.13.1", |
| 175 | ); |
| 176 | expect(config?.docs?.[0]?.rootUrl).toBe( |
| 177 | "https://docs.python.org/release/3.13.1", |
| 178 | ); |
| 179 | |
| 180 | // Test that proxy can correctly resolve secrets |
| 181 | const decodedAnthropicSecretLocation = decodeSecretLocation( |
| 182 | anthropicSecretLocation, |
| 183 | ); |
| 184 | const decodedGeminiSecretLocation = |
| 185 | decodeSecretLocation(geminiSecretLocation); |
| 186 | |
| 187 | // With environment |
| 188 | const antSecretValue = await resolveSecretLocationInProxy( |
| 189 | decodedAnthropicSecretLocation, |
| 190 | platformSecretStore, |
| 191 | environmentSecretStore, |
| 192 | ); |
| 193 | expect(antSecretValue).toBe("sk-ant-env"); |
| 194 | const geminiSecretValue = await resolveSecretLocationInProxy( |
| 195 | decodedGeminiSecretLocation, |
| 196 | platformSecretStore, |
| 197 | environmentSecretStore, |
| 198 | ); |
| 199 | expect(geminiSecretValue).toBe("gemini-api-key-env"); |
| 200 | |
| 201 | // Without environment |
| 202 | const antSecretValue2 = await resolveSecretLocationInProxy( |
| 203 | decodedAnthropicSecretLocation, |
| 204 | platformSecretStore, |
| 205 | undefined, |
| 206 | ); |
| 207 | expect(antSecretValue2).toBe("sk-ant"); |
| 208 | const geminiSecretValue2 = await resolveSecretLocationInProxy( |
| 209 | decodedGeminiSecretLocation, |
| 210 | platformSecretStore, |
| 211 | undefined, |
| 212 | ); |
| 213 | expect(geminiSecretValue2).toBe("gemini-api-key"); |
| 214 | }); |
| 215 | |
| 216 | it("should correctly unroll assistant with injected blocks", async () => { |
| 217 | const unrolledConfig = await unrollAssistant( |
| 218 | { |
| 219 | uriType: "slug", |
| 220 | fullSlug: { |
| 221 | ownerSlug: "test-org", |
| 222 | packageSlug: "assistant", |
| 223 | versionSlug: "latest", |
| 224 | }, |
| 225 | }, |
| 226 | registry, |
| 227 | { |
| 228 | renderSecrets: true, |
| 229 | platformClient, |
| 230 | orgScopeId: "test-org", |
| 231 | currentUserSlug: "test-user", |
| 232 | onPremProxyUrl: null, |
| 233 | // Add an injected block |
| 234 | injectBlocks: [ |
| 235 | { |
| 236 | uriType: "slug", |
| 237 | fullSlug: { |
| 238 | ownerSlug: "test-org", |
| 239 | packageSlug: "rules", |
| 240 | versionSlug: "latest", |
| 241 | }, |
| 242 | }, |
| 243 | ], |
| 244 | }, |
| 245 | ); |
| 246 | |
| 247 | const config = unrolledConfig.config; |
| 248 | |
| 249 | // The original rules array should have two items |
| 250 | expect(config?.rules?.length).toBe(3); // Now 3 with the injected block |
| 251 | |
| 252 | // Check the original doc is still there |
| 253 | expect(config?.docs?.[0]?.startUrl).toBe( |
| 254 | "https://docs.python.org/release/3.13.1", |
| 255 | ); |
| 256 | |
| 257 | // Check the injected doc block was added |
| 258 | expect(config?.rules?.[2]).toBe("Be kind"); |
| 259 | }); |
| 260 | |
| 261 | it.skip("should prioritize org over user / package secrets", () => {}); |
| 262 | }); |