|
1 | | -import { getApiKeyFromEnv } from "../provider.js" |
| 1 | +import { getApiKeyFromEnv, getProviderSettings, providerRequiresApiKey } from "../provider.js" |
| 2 | + |
| 3 | +// Bedrock-relevant AWS environment variables. Cleared before each test so the |
| 4 | +// suite is hermetic regardless of the host machine's ambient AWS configuration. |
| 5 | +const AWS_ENV_KEYS = [ |
| 6 | + "AWS_REGION", |
| 7 | + "AWS_DEFAULT_REGION", |
| 8 | + "AWS_PROFILE", |
| 9 | + "AWS_ACCESS_KEY_ID", |
| 10 | + "AWS_SECRET_ACCESS_KEY", |
| 11 | + "AWS_SESSION_TOKEN", |
| 12 | + "AWS_BEDROCK_API_KEY", |
| 13 | +] |
2 | 14 |
|
3 | 15 | describe("getApiKeyFromEnv", () => { |
4 | 16 | const originalEnv = process.env |
@@ -32,3 +44,180 @@ describe("getApiKeyFromEnv", () => { |
32 | 44 | expect(getApiKeyFromEnv("anthropic")).toBeUndefined() |
33 | 45 | }) |
34 | 46 | }) |
| 47 | + |
| 48 | +describe("providerRequiresApiKey", () => { |
| 49 | + it("returns false for bedrock (AWS credential chain / profile / direct creds are valid)", () => { |
| 50 | + expect(providerRequiresApiKey("bedrock")).toBe(false) |
| 51 | + }) |
| 52 | + |
| 53 | + it.each(["anthropic", "openai-native", "gemini", "openrouter", "vercel-ai-gateway"] as const)( |
| 54 | + "returns true for '%s'", |
| 55 | + (provider) => { |
| 56 | + expect(providerRequiresApiKey(provider)).toBe(true) |
| 57 | + }, |
| 58 | + ) |
| 59 | +}) |
| 60 | + |
| 61 | +describe("getProviderSettings", () => { |
| 62 | + const originalEnv = process.env |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + process.env = { ...originalEnv } |
| 66 | + for (const key of AWS_ENV_KEYS) { |
| 67 | + delete process.env[key] |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + process.env = originalEnv |
| 73 | + }) |
| 74 | + |
| 75 | + it("sets apiProvider for the selected provider", () => { |
| 76 | + const config = getProviderSettings("anthropic", undefined, undefined) |
| 77 | + expect(config.apiProvider).toBe("anthropic") |
| 78 | + }) |
| 79 | + |
| 80 | + describe("bedrock authentication modes", () => { |
| 81 | + it("mode 1: bearer token / API key sets awsUseApiKey and awsApiKey", () => { |
| 82 | + const config = getProviderSettings("bedrock", "bearer-token-123", undefined) |
| 83 | + |
| 84 | + expect(config.apiProvider).toBe("bedrock") |
| 85 | + expect(config.awsUseApiKey).toBe(true) |
| 86 | + expect(config.awsApiKey).toBe("bearer-token-123") |
| 87 | + // API-key mode must not enable profile or direct-credential fields. |
| 88 | + expect(config.awsUseProfile).toBeUndefined() |
| 89 | + expect(config.awsProfile).toBeUndefined() |
| 90 | + expect(config.awsAccessKey).toBeUndefined() |
| 91 | + expect(config.awsSecretKey).toBeUndefined() |
| 92 | + }) |
| 93 | + |
| 94 | + it("mode 2: AWS_PROFILE sets awsUseProfile and awsProfile", () => { |
| 95 | + process.env.AWS_PROFILE = "my-sso-profile" |
| 96 | + |
| 97 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 98 | + |
| 99 | + expect(config.awsUseProfile).toBe(true) |
| 100 | + expect(config.awsProfile).toBe("my-sso-profile") |
| 101 | + expect(config.awsUseApiKey).toBeUndefined() |
| 102 | + expect(config.awsApiKey).toBeUndefined() |
| 103 | + expect(config.awsAccessKey).toBeUndefined() |
| 104 | + }) |
| 105 | + |
| 106 | + it("mode 3: direct credentials map AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY", () => { |
| 107 | + process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE" |
| 108 | + process.env.AWS_SECRET_ACCESS_KEY = "secret-example" |
| 109 | + |
| 110 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 111 | + |
| 112 | + expect(config.awsAccessKey).toBe("AKIAEXAMPLE") |
| 113 | + expect(config.awsSecretKey).toBe("secret-example") |
| 114 | + expect(config.awsSessionToken).toBeUndefined() |
| 115 | + expect(config.awsUseApiKey).toBeUndefined() |
| 116 | + expect(config.awsUseProfile).toBeUndefined() |
| 117 | + }) |
| 118 | + |
| 119 | + it("mode 3: direct credentials include AWS_SESSION_TOKEN when present", () => { |
| 120 | + process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE" |
| 121 | + process.env.AWS_SECRET_ACCESS_KEY = "secret-example" |
| 122 | + process.env.AWS_SESSION_TOKEN = "session-token-example" |
| 123 | + |
| 124 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 125 | + |
| 126 | + expect(config.awsAccessKey).toBe("AKIAEXAMPLE") |
| 127 | + expect(config.awsSecretKey).toBe("secret-example") |
| 128 | + expect(config.awsSessionToken).toBe("session-token-example") |
| 129 | + }) |
| 130 | + |
| 131 | + it("mode 4: default credential chain — no creds set does NOT throw and sets no explicit auth fields", () => { |
| 132 | + expect(() => getProviderSettings("bedrock", undefined, undefined)).not.toThrow() |
| 133 | + |
| 134 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 135 | + |
| 136 | + // Region is always resolved, but none of the explicit auth modes engage, |
| 137 | + // leaving the AWS SDK to resolve credentials via its default chain. |
| 138 | + expect(config.awsUseApiKey).toBeUndefined() |
| 139 | + expect(config.awsApiKey).toBeUndefined() |
| 140 | + expect(config.awsUseProfile).toBeUndefined() |
| 141 | + expect(config.awsProfile).toBeUndefined() |
| 142 | + expect(config.awsAccessKey).toBeUndefined() |
| 143 | + expect(config.awsSecretKey).toBeUndefined() |
| 144 | + }) |
| 145 | + |
| 146 | + it("prioritises API key over AWS_PROFILE and direct credentials", () => { |
| 147 | + process.env.AWS_PROFILE = "my-profile" |
| 148 | + process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE" |
| 149 | + process.env.AWS_SECRET_ACCESS_KEY = "secret-example" |
| 150 | + |
| 151 | + const config = getProviderSettings("bedrock", "bearer-token-123", undefined) |
| 152 | + |
| 153 | + expect(config.awsUseApiKey).toBe(true) |
| 154 | + expect(config.awsApiKey).toBe("bearer-token-123") |
| 155 | + expect(config.awsUseProfile).toBeUndefined() |
| 156 | + expect(config.awsAccessKey).toBeUndefined() |
| 157 | + }) |
| 158 | + |
| 159 | + it("prioritises AWS_PROFILE over direct credentials when no API key is present", () => { |
| 160 | + process.env.AWS_PROFILE = "my-profile" |
| 161 | + process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE" |
| 162 | + process.env.AWS_SECRET_ACCESS_KEY = "secret-example" |
| 163 | + |
| 164 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 165 | + |
| 166 | + expect(config.awsUseProfile).toBe(true) |
| 167 | + expect(config.awsProfile).toBe("my-profile") |
| 168 | + expect(config.awsAccessKey).toBeUndefined() |
| 169 | + expect(config.awsSecretKey).toBeUndefined() |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + describe("bedrock region resolution", () => { |
| 174 | + it("defaults awsRegion to us-east-1 when no region env is set", () => { |
| 175 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 176 | + expect(config.awsRegion).toBe("us-east-1") |
| 177 | + }) |
| 178 | + |
| 179 | + it("uses AWS_REGION when set", () => { |
| 180 | + process.env.AWS_REGION = "eu-west-1" |
| 181 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 182 | + expect(config.awsRegion).toBe("eu-west-1") |
| 183 | + }) |
| 184 | + |
| 185 | + it("falls back to AWS_DEFAULT_REGION when AWS_REGION is unset", () => { |
| 186 | + process.env.AWS_DEFAULT_REGION = "ap-southeast-2" |
| 187 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 188 | + expect(config.awsRegion).toBe("ap-southeast-2") |
| 189 | + }) |
| 190 | + |
| 191 | + it("prefers AWS_REGION over AWS_DEFAULT_REGION", () => { |
| 192 | + process.env.AWS_REGION = "eu-west-1" |
| 193 | + process.env.AWS_DEFAULT_REGION = "ap-southeast-2" |
| 194 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 195 | + expect(config.awsRegion).toBe("eu-west-1") |
| 196 | + }) |
| 197 | + }) |
| 198 | + |
| 199 | + describe("bedrock cross-region inference auto-enable", () => { |
| 200 | + it.each([ |
| 201 | + "us.anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 202 | + "eu.anthropic.claude-3-5-sonnet", |
| 203 | + "apac.amazon.nova-pro", |
| 204 | + ])("enables awsUseCrossRegionInference for regional-prefixed model '%s'", (model) => { |
| 205 | + const config = getProviderSettings("bedrock", undefined, model) |
| 206 | + expect(config.apiModelId).toBe(model) |
| 207 | + expect(config.awsUseCrossRegionInference).toBe(true) |
| 208 | + }) |
| 209 | + |
| 210 | + it("does NOT enable cross-region inference for a non-prefixed model", () => { |
| 211 | + const model = "anthropic.claude-3-5-sonnet-20241022-v2:0" |
| 212 | + const config = getProviderSettings("bedrock", undefined, model) |
| 213 | + expect(config.apiModelId).toBe(model) |
| 214 | + expect(config.awsUseCrossRegionInference).toBeUndefined() |
| 215 | + }) |
| 216 | + |
| 217 | + it("does not set apiModelId or cross-region flag when no model is given", () => { |
| 218 | + const config = getProviderSettings("bedrock", undefined, undefined) |
| 219 | + expect(config.apiModelId).toBeUndefined() |
| 220 | + expect(config.awsUseCrossRegionInference).toBeUndefined() |
| 221 | + }) |
| 222 | + }) |
| 223 | +}) |
0 commit comments