|
| 1 | +import { createClaudeFetchFn, buildBedrockInvokeUrl } from '../../../src/agents/helpers/bedrockGateway.js' |
| 2 | +import config from '../../../src/config/config.js' |
| 3 | + |
| 4 | +describe('createClaudeFetchFn (Bedrock gateway transport)', () => { |
| 5 | + // The transport posts the native Bedrock InvokeModel request to {baseUrl}/model/{modelId}/invoke |
| 6 | + // with an x-api-key header. These tests pin that contract. The model id is colon-free here so the |
| 7 | + // URL assertion does not depend on how the colon in dated ids is handled. |
| 8 | + const BASE_URL = 'https://bedrock.example.com' |
| 9 | + const API_KEY = 'test-bedrock-key' |
| 10 | + const MODEL_ID = 'us.anthropic.claude-sonnet-4-6' |
| 11 | + |
| 12 | + // Stash the real values once; each test swaps in known values and afterEach restores them. |
| 13 | + const originalFetch = globalThis.fetch |
| 14 | + const originalBaseUrl = config.llms.bedrock.baseUrl |
| 15 | + const originalKey = config.llms.bedrock.key |
| 16 | + |
| 17 | + let capturedUrl: string | undefined |
| 18 | + let capturedInit: { body: string; headers: Record<string, string> } | undefined |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + config.llms.bedrock.baseUrl = BASE_URL |
| 22 | + config.llms.bedrock.key = API_KEY |
| 23 | + capturedUrl = undefined |
| 24 | + capturedInit = undefined |
| 25 | + |
| 26 | + globalThis.fetch = ((url: string, init: { body: string; headers: Record<string, string> }) => { |
| 27 | + capturedUrl = url |
| 28 | + capturedInit = init |
| 29 | + return Promise.resolve({ |
| 30 | + ok: true, |
| 31 | + status: 200, |
| 32 | + statusText: 'OK', |
| 33 | + headers: { get: () => 'application/json' }, |
| 34 | + json: async () => ({ content: [{ type: 'text', text: 'ok' }] }), |
| 35 | + text: async () => '{}' |
| 36 | + }) |
| 37 | + }) as unknown as typeof globalThis.fetch |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + globalThis.fetch = originalFetch |
| 42 | + config.llms.bedrock.baseUrl = originalBaseUrl |
| 43 | + config.llms.bedrock.key = originalKey |
| 44 | + }) |
| 45 | + |
| 46 | + const callFetchFn = () => { |
| 47 | + const fetchFn = createClaudeFetchFn(MODEL_ID, 'bedrock') |
| 48 | + return fetchFn('https://ignored.example', { |
| 49 | + body: JSON.stringify({ |
| 50 | + system: 'You are a helpful assistant', |
| 51 | + messages: [{ role: 'user', content: 'Hello world' }], |
| 52 | + max_tokens: 100 |
| 53 | + }) |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + it('POSTs to the /model/{modelId}/invoke endpoint', async () => { |
| 58 | + await callFetchFn() |
| 59 | + expect(capturedUrl).toBe(`${BASE_URL}/model/${MODEL_ID}/invoke`) |
| 60 | + }) |
| 61 | + |
| 62 | + it('sends the native Bedrock request body, not a wrapper envelope', async () => { |
| 63 | + await callFetchFn() |
| 64 | + const sentBody = JSON.parse(capturedInit!.body) |
| 65 | + |
| 66 | + // Native Bedrock fields sit at the top level of the request. |
| 67 | + expect(sentBody.anthropic_version).toBe('bedrock-2023-05-31') |
| 68 | + expect(sentBody.max_tokens).toBe(100) |
| 69 | + expect(Array.isArray(sentBody.messages)).toBe(true) |
| 70 | + |
| 71 | + // No wrapper keys around the request. |
| 72 | + expect(sentBody).not.toHaveProperty('body') |
| 73 | + expect(sentBody).not.toHaveProperty('modelId') |
| 74 | + expect(sentBody).not.toHaveProperty('accept') |
| 75 | + expect(sentBody).not.toHaveProperty('contentType') |
| 76 | + }) |
| 77 | + |
| 78 | + it('keeps the x-api-key auth header from config', async () => { |
| 79 | + await callFetchFn() |
| 80 | + expect(capturedInit!.headers['x-api-key']).toBe(API_KEY) |
| 81 | + }) |
| 82 | + |
| 83 | + it('refuses an unsafe model id before calling fetch', async () => { |
| 84 | + const fetchFn = createClaudeFetchFn('evil/../path', 'bedrock') |
| 85 | + await expect( |
| 86 | + fetchFn('https://ignored.example', { |
| 87 | + body: JSON.stringify({ |
| 88 | + system: 'You are a helpful assistant', |
| 89 | + messages: [{ role: 'user', content: 'Hello world' }], |
| 90 | + max_tokens: 100 |
| 91 | + }) |
| 92 | + }) |
| 93 | + ).rejects.toThrow() |
| 94 | + expect(capturedUrl).toBeUndefined() |
| 95 | + }) |
| 96 | +}) |
| 97 | + |
| 98 | +describe('buildBedrockInvokeUrl', () => { |
| 99 | + const BASE = 'https://bedrock.example.com' |
| 100 | + |
| 101 | + it('builds the /model/{modelId}/invoke path', () => { |
| 102 | + expect(buildBedrockInvokeUrl(BASE, 'us.anthropic.claude-sonnet-4-6')).toBe( |
| 103 | + `${BASE}/model/us.anthropic.claude-sonnet-4-6/invoke` |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + it('keeps the raw colon in dated model ids', () => { |
| 108 | + expect(buildBedrockInvokeUrl(BASE, 'us.anthropic.claude-haiku-4-5-20251001-v1:0')).toBe( |
| 109 | + `${BASE}/model/us.anthropic.claude-haiku-4-5-20251001-v1:0/invoke` |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + it('strips a trailing slash from the base url so the path has no double slash', () => { |
| 114 | + expect(buildBedrockInvokeUrl(`${BASE}/`, 'us.anthropic.claude-sonnet-4-6')).toBe( |
| 115 | + `${BASE}/model/us.anthropic.claude-sonnet-4-6/invoke` |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('rejects model ids that contain URL-structural characters', () => { |
| 120 | + expect(() => buildBedrockInvokeUrl(BASE, 'foo/bar')).toThrow() |
| 121 | + expect(() => buildBedrockInvokeUrl(BASE, 'foo?x=1')).toThrow() |
| 122 | + expect(() => buildBedrockInvokeUrl(BASE, 'foo#frag')).toThrow() |
| 123 | + expect(() => buildBedrockInvokeUrl(BASE, 'foo%2e')).toThrow() |
| 124 | + expect(() => buildBedrockInvokeUrl(BASE, 'has space')).toThrow() |
| 125 | + expect(() => buildBedrockInvokeUrl(BASE, '')).toThrow() |
| 126 | + }) |
| 127 | +}) |
0 commit comments