|
| 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 | + |
| 9 | +import { AwsBedrockHandler, StreamEvent } from "../bedrock" |
| 10 | +import { ApiHandlerOptions } from "../../../shared/api" |
| 11 | +import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime" |
| 12 | +import { logger } from "../../../utils/logging" |
| 13 | + |
| 14 | +describe("AwsBedrockHandler createMessage", () => { |
| 15 | + let mockSend: jest.SpyInstance |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + // Mock the BedrockRuntimeClient.prototype.send method |
| 19 | + mockSend = jest.spyOn(BedrockRuntimeClient.prototype, "send").mockImplementation(async () => { |
| 20 | + return { |
| 21 | + stream: createMockStream([]), |
| 22 | + } |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + mockSend.mockRestore() |
| 28 | + }) |
| 29 | + |
| 30 | + // Helper function to create a mock async iterable stream |
| 31 | + function createMockStream(events: StreamEvent[]) { |
| 32 | + return { |
| 33 | + [Symbol.asyncIterator]: async function* () { |
| 34 | + for (const event of events) { |
| 35 | + yield event |
| 36 | + } |
| 37 | + // Always yield a metadata event at the end |
| 38 | + yield { |
| 39 | + metadata: { |
| 40 | + usage: { |
| 41 | + inputTokens: 100, |
| 42 | + outputTokens: 200, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + it("should log debug information during createMessage with custom ARN", async () => { |
| 51 | + // Create a handler with a custom ARN |
| 52 | + const mockOptions: ApiHandlerOptions = { |
| 53 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 54 | + awsAccessKey: "test-access-key", |
| 55 | + awsSecretKey: "test-secret-key", |
| 56 | + awsRegion: "us-east-1", |
| 57 | + awsCustomArn: "arn:aws:bedrock:us-east-1:123456789:foundation-model/custom-model", |
| 58 | + } |
| 59 | + |
| 60 | + const handler = new AwsBedrockHandler(mockOptions) |
| 61 | + |
| 62 | + // Mock the stream to include various events that trigger debug logs |
| 63 | + mockSend.mockImplementationOnce(async () => { |
| 64 | + return { |
| 65 | + stream: createMockStream([ |
| 66 | + // Event with invokedModelId |
| 67 | + { |
| 68 | + trace: { |
| 69 | + promptRouter: { |
| 70 | + invokedModelId: |
| 71 | + "arn:aws:bedrock:us-east-1:123456789:foundation-model/anthropic.claude-3-sonnet-20240229-v1:0", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + // Content events |
| 76 | + { |
| 77 | + contentBlockStart: { |
| 78 | + start: { |
| 79 | + text: "Hello", |
| 80 | + }, |
| 81 | + contentBlockIndex: 0, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + contentBlockDelta: { |
| 86 | + delta: { |
| 87 | + text: ", world!", |
| 88 | + }, |
| 89 | + contentBlockIndex: 0, |
| 90 | + }, |
| 91 | + }, |
| 92 | + ]), |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + // Create a message generator |
| 97 | + const messageGenerator = handler.createMessage("system prompt", [{ role: "user", content: "user message" }]) |
| 98 | + |
| 99 | + // Collect all yielded events |
| 100 | + const events = [] |
| 101 | + for await (const event of messageGenerator) { |
| 102 | + events.push(event) |
| 103 | + } |
| 104 | + |
| 105 | + // Verify that events were yielded |
| 106 | + expect(events.length).toBeGreaterThan(0) |
| 107 | + |
| 108 | + // Verify that debug logs were called |
| 109 | + expect(logger.debug).toHaveBeenCalledWith( |
| 110 | + "Using custom ARN for Bedrock request", |
| 111 | + expect.objectContaining({ |
| 112 | + ctx: "bedrock", |
| 113 | + customArn: mockOptions.awsCustomArn, |
| 114 | + }), |
| 115 | + ) |
| 116 | + |
| 117 | + expect(logger.debug).toHaveBeenCalledWith( |
| 118 | + "Bedrock invokedModelId detected", |
| 119 | + expect.objectContaining({ |
| 120 | + ctx: "bedrock", |
| 121 | + invokedModelId: |
| 122 | + "arn:aws:bedrock:us-east-1:123456789:foundation-model/anthropic.claude-3-sonnet-20240229-v1:0", |
| 123 | + }), |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + it("should log debug information during createMessage with cross-region inference", async () => { |
| 128 | + // Create a handler with cross-region inference |
| 129 | + const mockOptions: ApiHandlerOptions = { |
| 130 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 131 | + awsAccessKey: "test-access-key", |
| 132 | + awsSecretKey: "test-secret-key", |
| 133 | + awsRegion: "us-east-1", |
| 134 | + awsUseCrossRegionInference: true, |
| 135 | + } |
| 136 | + |
| 137 | + const handler = new AwsBedrockHandler(mockOptions) |
| 138 | + |
| 139 | + // Create a message generator |
| 140 | + const messageGenerator = handler.createMessage("system prompt", [{ role: "user", content: "user message" }]) |
| 141 | + |
| 142 | + // Collect all yielded events |
| 143 | + const events = [] |
| 144 | + for await (const event of messageGenerator) { |
| 145 | + events.push(event) |
| 146 | + } |
| 147 | + |
| 148 | + // Verify that events were yielded |
| 149 | + expect(events.length).toBeGreaterThan(0) |
| 150 | + }) |
| 151 | +}) |
0 commit comments