Roo-Code/src/api/providers/__tests__/bedrock-vpc-endpoint.test.ts
kcwhite a4c91c65b3
Adding support for custom VPC endpoints when using AWS Bedrock models (#3947)
* feat: Add custom VPC endpoint support for AWS Bedrock

* fix: Fix TypeScript error in Bedrock.tsx

* fix: Update VPC endpoint UI to match Cline's implementation

* Fix AWS Bedrock VPC endpoint UI implementation

- Changed checkbox label to 'Use custom VPC endpoint' to match Cline

- Fixed conditional rendering to show text field when checkbox is checked

- Ensured placeholder text appears correctly

- Maintained proper styling for consistency

* Fix AWS Bedrock VPC endpoint UI implementation to match Cline exactly

- Added state variable to track checkbox selection

- Fixed conditional rendering to show/hide text field based on checkbox state

- Maintained proper styling and placeholder text

* Fix AWS Bedrock VPC endpoint UI implementation with proper event handling

- Fixed checkbox onChange handler to accept boolean directly instead of event object

- Added unit tests to verify the behavior

- Maintained proper styling and placeholder text

* Update Bedrock VPC endpoint tests with proper test IDs

* Improve AWS Bedrock VPC endpoint text field alignment

- Removed left margin from text field to align with checkbox

- Maintained proper styling and placeholder text

* Preserve AWS Bedrock VPC endpoint URL when toggling checkbox

- Added awsBedrockEndpointEnabled field to schema

- Modified Bedrock provider to check both endpoint URL and enabled flag

- Updated UI to preserve endpoint URL when checkbox is toggled

- Maintained proper alignment with checkbox

* Implement AWS Bedrock Custom VPC Endpoint functionality

* fix: update ApiConfiguration to ProviderSettings in Bedrock tests and regenerate types

* fix: update all instances of ApiConfiguration to ProviderSettings in Bedrock tests

* Fixed broken unit test

* Add changeset for Bedrock VPC endpoint support

* informative placeholder

* Bug fixes

* Fixed failing tests

* Add example URLs to Bedrock VPC endpoint section and update tests

* Fix truncated test assertion in Bedrock.test.tsx that was breaking the UI

* Refactor mock components in Bedrock.test.tsx for improved data-testid handling

* feat(i18n): add VPC endpoint translations for AWS Bedrock settings

* test: update Bedrock component tests for internationalized strings

---------

Co-authored-by: Kevin White <kcwhite@ancestry.com>
Co-authored-by: Daniel <57051444+daniel-lxs@users.noreply.github.com>
Co-authored-by: Daniel Riccio <ricciodaniel98@gmail.com>
2025-05-30 22:19:34 -04:00

178 lines
5.6 KiB
TypeScript

// Mock AWS SDK credential providers
jest.mock("@aws-sdk/credential-providers", () => {
const mockFromIni = jest.fn().mockReturnValue({
accessKeyId: "profile-access-key",
secretAccessKey: "profile-secret-key",
})
return { fromIni: mockFromIni }
})
// Mock BedrockRuntimeClient and ConverseStreamCommand
const mockBedrockRuntimeClient = jest.fn()
const mockSend = jest.fn().mockResolvedValue({
stream: [],
})
jest.mock("@aws-sdk/client-bedrock-runtime", () => ({
BedrockRuntimeClient: mockBedrockRuntimeClient.mockImplementation(() => ({
send: mockSend,
})),
ConverseStreamCommand: jest.fn(),
ConverseCommand: jest.fn(),
}))
import { AwsBedrockHandler } from "../bedrock"
describe("AWS Bedrock VPC Endpoint Functionality", () => {
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks()
})
// Test Scenario 1: Input Validation Test
describe("VPC Endpoint URL Validation", () => {
it("should configure client with endpoint URL when both URL and enabled flag are provided", () => {
// Create handler with endpoint URL and enabled flag
new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: "https://bedrock-vpc.example.com",
awsBedrockEndpointEnabled: true,
})
// Verify the client was created with the correct endpoint
expect(mockBedrockRuntimeClient).toHaveBeenCalledWith(
expect.objectContaining({
region: "us-east-1",
endpoint: "https://bedrock-vpc.example.com",
}),
)
})
it("should not configure client with endpoint URL when URL is provided but enabled flag is false", () => {
// Create handler with endpoint URL but disabled flag
new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: "https://bedrock-vpc.example.com",
awsBedrockEndpointEnabled: false,
})
// Verify the client was created without the endpoint
expect(mockBedrockRuntimeClient).toHaveBeenCalledWith(
expect.objectContaining({
region: "us-east-1",
}),
)
// Verify the endpoint property is not present
const clientConfig = mockBedrockRuntimeClient.mock.calls[0][0]
expect(clientConfig).not.toHaveProperty("endpoint")
})
})
// Test Scenario 2: Edge Case Tests
describe("Edge Cases", () => {
it("should handle empty endpoint URL gracefully", () => {
// Create handler with empty endpoint URL but enabled flag
new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: "",
awsBedrockEndpointEnabled: true,
})
// Verify the client was created without the endpoint (since it's empty)
expect(mockBedrockRuntimeClient).toHaveBeenCalledWith(
expect.objectContaining({
region: "us-east-1",
}),
)
// Verify the endpoint property is not present
const clientConfig = mockBedrockRuntimeClient.mock.calls[0][0]
expect(clientConfig).not.toHaveProperty("endpoint")
})
it("should handle undefined endpoint URL gracefully", () => {
// Create handler with undefined endpoint URL but enabled flag
new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: undefined,
awsBedrockEndpointEnabled: true,
})
// Verify the client was created without the endpoint
expect(mockBedrockRuntimeClient).toHaveBeenCalledWith(
expect.objectContaining({
region: "us-east-1",
}),
)
// Verify the endpoint property is not present
const clientConfig = mockBedrockRuntimeClient.mock.calls[0][0]
expect(clientConfig).not.toHaveProperty("endpoint")
})
})
// Test Scenario 4: Error Handling Tests
describe("Error Handling", () => {
it("should handle invalid endpoint URLs by passing them directly to AWS SDK", () => {
// Create handler with an invalid URL format
new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: "invalid-url-format",
awsBedrockEndpointEnabled: true,
})
// Verify the client was created with the invalid endpoint
// (AWS SDK will handle the validation/errors)
expect(mockBedrockRuntimeClient).toHaveBeenCalledWith(
expect.objectContaining({
region: "us-east-1",
endpoint: "invalid-url-format",
}),
)
})
})
// Test Scenario 5: Persistence Tests
describe("Persistence", () => {
it("should maintain consistent behavior across multiple requests", async () => {
// Create handler with endpoint URL and enabled flag
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-east-1",
awsBedrockEndpoint: "https://bedrock-vpc.example.com",
awsBedrockEndpointEnabled: true,
})
// Reset mock to clear the constructor call
mockBedrockRuntimeClient.mockClear()
// Make a request
try {
await handler.completePrompt("Test prompt")
} catch (error) {
// Ignore errors, we're just testing the client configuration
}
// Verify the client was configured with the endpoint
expect(mockSend).toHaveBeenCalled()
})
})
})