|
| 1 | +// Mock AWS SDK credential providers |
| 2 | +jest.mock("@aws-sdk/credential-providers", () => ({ |
| 3 | + fromIni: jest.fn().mockReturnValue({ |
| 4 | + accessKeyId: "profile-access-key", |
| 5 | + secretAccessKey: "profile-secret-key", |
| 6 | + }), |
| 7 | +})) |
| 8 | + |
1 | 9 | import { AwsBedrockHandler } from "../bedrock" |
2 | 10 | import { MessageContent } from "../../../shared/api" |
3 | 11 | import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime" |
4 | 12 | import { Anthropic } from "@anthropic-ai/sdk" |
| 13 | +import { fromIni } from "@aws-sdk/credential-providers" |
5 | 14 |
|
6 | 15 | describe("AwsBedrockHandler", () => { |
7 | 16 | let handler: AwsBedrockHandler |
@@ -30,6 +39,57 @@ describe("AwsBedrockHandler", () => { |
30 | 39 | }) |
31 | 40 | expect(handlerWithoutCreds).toBeInstanceOf(AwsBedrockHandler) |
32 | 41 | }) |
| 42 | + |
| 43 | + it("should initialize with AWS profile credentials", () => { |
| 44 | + const handlerWithProfile = new AwsBedrockHandler({ |
| 45 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 46 | + awsRegion: "us-east-1", |
| 47 | + awsUseProfile: true, |
| 48 | + awsProfile: "test-profile", |
| 49 | + }) |
| 50 | + expect(handlerWithProfile).toBeInstanceOf(AwsBedrockHandler) |
| 51 | + expect(handlerWithProfile["options"].awsUseProfile).toBe(true) |
| 52 | + expect(handlerWithProfile["options"].awsProfile).toBe("test-profile") |
| 53 | + }) |
| 54 | + |
| 55 | + it("should initialize with AWS profile enabled but no profile set", () => { |
| 56 | + const handlerWithoutProfile = new AwsBedrockHandler({ |
| 57 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 58 | + awsRegion: "us-east-1", |
| 59 | + awsUseProfile: true, |
| 60 | + }) |
| 61 | + expect(handlerWithoutProfile).toBeInstanceOf(AwsBedrockHandler) |
| 62 | + expect(handlerWithoutProfile["options"].awsUseProfile).toBe(true) |
| 63 | + expect(handlerWithoutProfile["options"].awsProfile).toBeUndefined() |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe("AWS SDK client configuration", () => { |
| 68 | + it("should configure client with profile credentials when profile mode is enabled", async () => { |
| 69 | + const handlerWithProfile = new AwsBedrockHandler({ |
| 70 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 71 | + awsRegion: "us-east-1", |
| 72 | + awsUseProfile: true, |
| 73 | + awsProfile: "test-profile", |
| 74 | + }) |
| 75 | + |
| 76 | + // Mock a simple API call to verify credentials are used |
| 77 | + const mockResponse = { |
| 78 | + output: new TextEncoder().encode(JSON.stringify({ content: "test" })), |
| 79 | + } |
| 80 | + const mockSend = jest.fn().mockResolvedValue(mockResponse) |
| 81 | + handlerWithProfile["client"] = { |
| 82 | + send: mockSend, |
| 83 | + } as unknown as BedrockRuntimeClient |
| 84 | + |
| 85 | + await handlerWithProfile.completePrompt("test") |
| 86 | + |
| 87 | + // Verify the client was configured with profile credentials |
| 88 | + expect(mockSend).toHaveBeenCalled() |
| 89 | + expect(fromIni).toHaveBeenCalledWith({ |
| 90 | + profile: "test-profile", |
| 91 | + }) |
| 92 | + }) |
33 | 93 | }) |
34 | 94 |
|
35 | 95 | describe("createMessage", () => { |
|
0 commit comments