|
| 1 | +// npx vitest run src/api/providers/__tests__/bedrock-inference-profiles.spec.ts |
| 2 | + |
| 3 | +import { AWS_INFERENCE_PROFILE_MAPPING } from "@roo-code/types" |
| 4 | +import { AwsBedrockHandler } from "../bedrock" |
| 5 | +import { ApiHandlerOptions } from "../../../shared/api" |
| 6 | + |
| 7 | +// Mock AWS SDK |
| 8 | +vitest.mock("@aws-sdk/client-bedrock-runtime", () => { |
| 9 | + return { |
| 10 | + BedrockRuntimeClient: vitest.fn().mockImplementation(() => ({ |
| 11 | + send: vitest.fn(), |
| 12 | + config: { region: "us-east-1" }, |
| 13 | + })), |
| 14 | + ConverseCommand: vitest.fn(), |
| 15 | + ConverseStreamCommand: vitest.fn(), |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +describe("AWS Bedrock Inference Profiles", () => { |
| 20 | + // Helper function to create a handler with specific options |
| 21 | + const createHandler = (options: Partial<ApiHandlerOptions> = {}) => { |
| 22 | + const defaultOptions: ApiHandlerOptions = { |
| 23 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 24 | + awsRegion: "us-east-1", |
| 25 | + ...options, |
| 26 | + } |
| 27 | + return new AwsBedrockHandler(defaultOptions) |
| 28 | + } |
| 29 | + |
| 30 | + describe("AWS_INFERENCE_PROFILE_MAPPING constant", () => { |
| 31 | + it("should contain all expected region mappings", () => { |
| 32 | + expect(AWS_INFERENCE_PROFILE_MAPPING).toEqual([ |
| 33 | + ["us-gov-", "ug."], |
| 34 | + ["us-", "us."], |
| 35 | + ["eu-", "eu."], |
| 36 | + ["ap-", "apac."], |
| 37 | + ["ca-", "ca."], |
| 38 | + ["sa-", "sa."], |
| 39 | + ]) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should be ordered by pattern length (descending)", () => { |
| 43 | + const lengths = AWS_INFERENCE_PROFILE_MAPPING.map(([pattern]) => pattern.length) |
| 44 | + const sortedLengths = [...lengths].sort((a, b) => b - a) |
| 45 | + expect(lengths).toEqual(sortedLengths) |
| 46 | + }) |
| 47 | + |
| 48 | + it("should have valid inference profile prefixes", () => { |
| 49 | + AWS_INFERENCE_PROFILE_MAPPING.forEach(([regionPattern, inferenceProfile]) => { |
| 50 | + expect(regionPattern).toMatch(/^[a-z-]+$/) |
| 51 | + expect(inferenceProfile).toMatch(/^[a-z]+\.$/) |
| 52 | + }) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe("getPrefixForRegion function", () => { |
| 57 | + it("should return correct prefix for US government regions", () => { |
| 58 | + const handler = createHandler() |
| 59 | + expect((handler as any).constructor.getPrefixForRegion("us-gov-east-1")).toBe("ug.") |
| 60 | + expect((handler as any).constructor.getPrefixForRegion("us-gov-west-1")).toBe("ug.") |
| 61 | + }) |
| 62 | + |
| 63 | + it("should return correct prefix for US commercial regions", () => { |
| 64 | + const handler = createHandler() |
| 65 | + expect((handler as any).constructor.getPrefixForRegion("us-east-1")).toBe("us.") |
| 66 | + expect((handler as any).constructor.getPrefixForRegion("us-west-1")).toBe("us.") |
| 67 | + expect((handler as any).constructor.getPrefixForRegion("us-west-2")).toBe("us.") |
| 68 | + }) |
| 69 | + |
| 70 | + it("should return correct prefix for European regions", () => { |
| 71 | + const handler = createHandler() |
| 72 | + expect((handler as any).constructor.getPrefixForRegion("eu-west-1")).toBe("eu.") |
| 73 | + expect((handler as any).constructor.getPrefixForRegion("eu-central-1")).toBe("eu.") |
| 74 | + expect((handler as any).constructor.getPrefixForRegion("eu-north-1")).toBe("eu.") |
| 75 | + expect((handler as any).constructor.getPrefixForRegion("eu-south-1")).toBe("eu.") |
| 76 | + }) |
| 77 | + |
| 78 | + it("should return correct prefix for Asia Pacific regions", () => { |
| 79 | + const handler = createHandler() |
| 80 | + expect((handler as any).constructor.getPrefixForRegion("ap-southeast-1")).toBe("apac.") |
| 81 | + expect((handler as any).constructor.getPrefixForRegion("ap-northeast-1")).toBe("apac.") |
| 82 | + expect((handler as any).constructor.getPrefixForRegion("ap-south-1")).toBe("apac.") |
| 83 | + expect((handler as any).constructor.getPrefixForRegion("ap-east-1")).toBe("apac.") |
| 84 | + }) |
| 85 | + |
| 86 | + it("should return correct prefix for Canada regions", () => { |
| 87 | + const handler = createHandler() |
| 88 | + expect((handler as any).constructor.getPrefixForRegion("ca-central-1")).toBe("ca.") |
| 89 | + expect((handler as any).constructor.getPrefixForRegion("ca-west-1")).toBe("ca.") |
| 90 | + }) |
| 91 | + |
| 92 | + it("should return correct prefix for South America regions", () => { |
| 93 | + const handler = createHandler() |
| 94 | + expect((handler as any).constructor.getPrefixForRegion("sa-east-1")).toBe("sa.") |
| 95 | + }) |
| 96 | + |
| 97 | + it("should return undefined for unsupported regions", () => { |
| 98 | + const handler = createHandler() |
| 99 | + expect((handler as any).constructor.getPrefixForRegion("af-south-1")).toBeUndefined() |
| 100 | + expect((handler as any).constructor.getPrefixForRegion("me-south-1")).toBeUndefined() |
| 101 | + expect((handler as any).constructor.getPrefixForRegion("cn-north-1")).toBeUndefined() |
| 102 | + expect((handler as any).constructor.getPrefixForRegion("invalid-region")).toBeUndefined() |
| 103 | + }) |
| 104 | + |
| 105 | + it("should prioritize longer patterns over shorter ones", () => { |
| 106 | + const handler = createHandler() |
| 107 | + // us-gov- should be matched before us- |
| 108 | + expect((handler as any).constructor.getPrefixForRegion("us-gov-east-1")).toBe("ug.") |
| 109 | + expect((handler as any).constructor.getPrefixForRegion("us-gov-west-1")).toBe("ug.") |
| 110 | + |
| 111 | + // Regular us- regions should still work |
| 112 | + expect((handler as any).constructor.getPrefixForRegion("us-east-1")).toBe("us.") |
| 113 | + expect((handler as any).constructor.getPrefixForRegion("us-west-2")).toBe("us.") |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe("Cross-region inference integration", () => { |
| 118 | + it("should apply ug. prefix for US government regions", () => { |
| 119 | + const handler = createHandler({ |
| 120 | + awsUseCrossRegionInference: true, |
| 121 | + awsRegion: "us-gov-east-1", |
| 122 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 123 | + }) |
| 124 | + |
| 125 | + const model = handler.getModel() |
| 126 | + expect(model.id).toBe("ug.anthropic.claude-3-sonnet-20240229-v1:0") |
| 127 | + }) |
| 128 | + |
| 129 | + it("should apply us. prefix for US commercial regions", () => { |
| 130 | + const handler = createHandler({ |
| 131 | + awsUseCrossRegionInference: true, |
| 132 | + awsRegion: "us-east-1", |
| 133 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 134 | + }) |
| 135 | + |
| 136 | + const model = handler.getModel() |
| 137 | + expect(model.id).toBe("us.anthropic.claude-3-sonnet-20240229-v1:0") |
| 138 | + }) |
| 139 | + |
| 140 | + it("should apply eu. prefix for European regions", () => { |
| 141 | + const handler = createHandler({ |
| 142 | + awsUseCrossRegionInference: true, |
| 143 | + awsRegion: "eu-west-1", |
| 144 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 145 | + }) |
| 146 | + |
| 147 | + const model = handler.getModel() |
| 148 | + expect(model.id).toBe("eu.anthropic.claude-3-sonnet-20240229-v1:0") |
| 149 | + }) |
| 150 | + |
| 151 | + it("should apply apac. prefix for Asia Pacific regions", () => { |
| 152 | + const handler = createHandler({ |
| 153 | + awsUseCrossRegionInference: true, |
| 154 | + awsRegion: "ap-southeast-1", |
| 155 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 156 | + }) |
| 157 | + |
| 158 | + const model = handler.getModel() |
| 159 | + expect(model.id).toBe("apac.anthropic.claude-3-sonnet-20240229-v1:0") |
| 160 | + }) |
| 161 | + |
| 162 | + it("should apply ca. prefix for Canada regions", () => { |
| 163 | + const handler = createHandler({ |
| 164 | + awsUseCrossRegionInference: true, |
| 165 | + awsRegion: "ca-central-1", |
| 166 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 167 | + }) |
| 168 | + |
| 169 | + const model = handler.getModel() |
| 170 | + expect(model.id).toBe("ca.anthropic.claude-3-sonnet-20240229-v1:0") |
| 171 | + }) |
| 172 | + |
| 173 | + it("should apply sa. prefix for South America regions", () => { |
| 174 | + const handler = createHandler({ |
| 175 | + awsUseCrossRegionInference: true, |
| 176 | + awsRegion: "sa-east-1", |
| 177 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 178 | + }) |
| 179 | + |
| 180 | + const model = handler.getModel() |
| 181 | + expect(model.id).toBe("sa.anthropic.claude-3-sonnet-20240229-v1:0") |
| 182 | + }) |
| 183 | + |
| 184 | + it("should not apply prefix when cross-region inference is disabled", () => { |
| 185 | + const handler = createHandler({ |
| 186 | + awsUseCrossRegionInference: false, |
| 187 | + awsRegion: "us-gov-east-1", |
| 188 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 189 | + }) |
| 190 | + |
| 191 | + const model = handler.getModel() |
| 192 | + expect(model.id).toBe("anthropic.claude-3-sonnet-20240229-v1:0") |
| 193 | + }) |
| 194 | + |
| 195 | + it("should handle unsupported regions gracefully", () => { |
| 196 | + const handler = createHandler({ |
| 197 | + awsUseCrossRegionInference: true, |
| 198 | + awsRegion: "af-south-1", // Unsupported region |
| 199 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 200 | + }) |
| 201 | + |
| 202 | + const model = handler.getModel() |
| 203 | + // Should remain unchanged when no prefix is found |
| 204 | + expect(model.id).toBe("anthropic.claude-3-sonnet-20240229-v1:0") |
| 205 | + }) |
| 206 | + |
| 207 | + it("should work with different model IDs", () => { |
| 208 | + const testModels = [ |
| 209 | + "anthropic.claude-3-haiku-20240307-v1:0", |
| 210 | + "anthropic.claude-3-opus-20240229-v1:0", |
| 211 | + "amazon.nova-pro-v1:0", |
| 212 | + "meta.llama3-1-70b-instruct-v1:0", |
| 213 | + ] |
| 214 | + |
| 215 | + testModels.forEach((modelId) => { |
| 216 | + const handler = createHandler({ |
| 217 | + awsUseCrossRegionInference: true, |
| 218 | + awsRegion: "eu-west-1", |
| 219 | + apiModelId: modelId, |
| 220 | + }) |
| 221 | + |
| 222 | + const model = handler.getModel() |
| 223 | + expect(model.id).toBe(`eu.${modelId}`) |
| 224 | + }) |
| 225 | + }) |
| 226 | + |
| 227 | + it("should prioritize us-gov- over us- in cross-region inference", () => { |
| 228 | + // Test that us-gov-east-1 gets ug. prefix, not us. |
| 229 | + const govHandler = createHandler({ |
| 230 | + awsUseCrossRegionInference: true, |
| 231 | + awsRegion: "us-gov-east-1", |
| 232 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 233 | + }) |
| 234 | + |
| 235 | + const govModel = govHandler.getModel() |
| 236 | + expect(govModel.id).toBe("ug.anthropic.claude-3-sonnet-20240229-v1:0") |
| 237 | + |
| 238 | + // Test that regular us-east-1 still gets us. prefix |
| 239 | + const usHandler = createHandler({ |
| 240 | + awsUseCrossRegionInference: true, |
| 241 | + awsRegion: "us-east-1", |
| 242 | + apiModelId: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 243 | + }) |
| 244 | + |
| 245 | + const usModel = usHandler.getModel() |
| 246 | + expect(usModel.id).toBe("us.anthropic.claude-3-sonnet-20240229-v1:0") |
| 247 | + }) |
| 248 | + }) |
| 249 | +}) |
0 commit comments