|
| 1 | +import { AwsBedrockHandler } from "../bedrock" |
| 2 | +import { ApiHandlerOptions } from "../../../shared/api" |
| 3 | + |
| 4 | +// Mock the AWS SDK |
| 5 | +jest.mock("@aws-sdk/client-bedrock-runtime", () => { |
| 6 | + const mockSend = jest.fn().mockImplementation(() => { |
| 7 | + return Promise.resolve({ |
| 8 | + output: new TextEncoder().encode(JSON.stringify({ content: "Test response" })), |
| 9 | + }) |
| 10 | + }) |
| 11 | + |
| 12 | + return { |
| 13 | + BedrockRuntimeClient: jest.fn().mockImplementation(() => ({ |
| 14 | + send: mockSend, |
| 15 | + config: { |
| 16 | + region: "us-east-1", |
| 17 | + }, |
| 18 | + })), |
| 19 | + ConverseCommand: jest.fn(), |
| 20 | + ConverseStreamCommand: jest.fn(), |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +describe("AwsBedrockHandler with custom ARN", () => { |
| 25 | + const mockOptions: ApiHandlerOptions = { |
| 26 | + apiModelId: "custom-arn", |
| 27 | + awsCustomArn: "arn:aws:bedrock:us-east-1:123456789012:foundation-model/anthropic.claude-3-sonnet-20240229-v1:0", |
| 28 | + awsRegion: "us-east-1", |
| 29 | + } |
| 30 | + |
| 31 | + it("should use the custom ARN as the model ID", async () => { |
| 32 | + const handler = new AwsBedrockHandler(mockOptions) |
| 33 | + const model = handler.getModel() |
| 34 | + |
| 35 | + expect(model.id).toBe(mockOptions.awsCustomArn) |
| 36 | + expect(model.info).toHaveProperty("maxTokens") |
| 37 | + expect(model.info).toHaveProperty("contextWindow") |
| 38 | + expect(model.info).toHaveProperty("supportsPromptCache") |
| 39 | + }) |
| 40 | + |
| 41 | + it("should extract region from ARN and use it for client configuration", () => { |
| 42 | + // Test with matching region |
| 43 | + const handler1 = new AwsBedrockHandler(mockOptions) |
| 44 | + expect((handler1 as any).client.config.region).toBe("us-east-1") |
| 45 | + |
| 46 | + // Test with mismatched region |
| 47 | + const mismatchOptions = { |
| 48 | + ...mockOptions, |
| 49 | + awsRegion: "us-west-2", |
| 50 | + } |
| 51 | + const handler2 = new AwsBedrockHandler(mismatchOptions) |
| 52 | + // Should use the ARN region, not the provided region |
| 53 | + expect((handler2 as any).client.config.region).toBe("us-east-1") |
| 54 | + }) |
| 55 | + |
| 56 | + it("should validate ARN format", async () => { |
| 57 | + // Invalid ARN format |
| 58 | + const invalidOptions = { |
| 59 | + ...mockOptions, |
| 60 | + awsCustomArn: "invalid-arn-format", |
| 61 | + } |
| 62 | + |
| 63 | + const handler = new AwsBedrockHandler(invalidOptions) |
| 64 | + |
| 65 | + // completePrompt should throw an error for invalid ARN |
| 66 | + await expect(handler.completePrompt("test")).rejects.toThrow("Invalid ARN format") |
| 67 | + }) |
| 68 | + |
| 69 | + it("should complete a prompt successfully with valid ARN", async () => { |
| 70 | + const handler = new AwsBedrockHandler(mockOptions) |
| 71 | + const response = await handler.completePrompt("test prompt") |
| 72 | + |
| 73 | + expect(response).toBe("Test response") |
| 74 | + }) |
| 75 | +}) |
0 commit comments