|
| 1 | +import nock from 'nock'; |
| 2 | +import { AnthropicClient } from '../../../src/integrations/anthropic/client'; |
| 3 | +import { |
| 4 | + GarmrService, |
| 5 | + GarmrNoopService, |
| 6 | +} from '../../../src/integrations/garmr'; |
| 7 | +import type { AnthropicResponse } from '../../../src/integrations/anthropic/types'; |
| 8 | + |
| 9 | +describe('AnthropicClient', () => { |
| 10 | + const API_KEY = 'test-api-key'; |
| 11 | + let client: AnthropicClient; |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + process.env.ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages'; |
| 15 | + process.env.ANTHROPIC_VERSION = '2023-06-01'; |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + nock.cleanAll(); |
| 20 | + client = new AnthropicClient(API_KEY); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + nock.cleanAll(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('constructor', () => { |
| 28 | + it('should use GarmrNoopService by default', () => { |
| 29 | + const defaultClient = new AnthropicClient(API_KEY); |
| 30 | + expect(defaultClient.garmr).toBeInstanceOf(GarmrNoopService); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should use provided Garmr service', () => { |
| 34 | + const customGarmr = new GarmrService({ |
| 35 | + service: 'test', |
| 36 | + breakerOpts: { |
| 37 | + halfOpenAfter: 10000, |
| 38 | + threshold: 0.2, |
| 39 | + duration: 30000, |
| 40 | + }, |
| 41 | + retryOpts: { |
| 42 | + maxAttempts: 3, |
| 43 | + backoff: 500, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const customClient = new AnthropicClient(API_KEY, { garmr: customGarmr }); |
| 48 | + expect(customClient.garmr).toBe(customGarmr); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('createMessage', () => { |
| 53 | + const mockResponse: AnthropicResponse = { |
| 54 | + content: [ |
| 55 | + { |
| 56 | + input: {}, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }; |
| 60 | + |
| 61 | + it('should send request with correct headers', async () => { |
| 62 | + const scope = nock('https://api.anthropic.com') |
| 63 | + .post('/v1/messages') |
| 64 | + .matchHeader('Content-Type', 'application/json') |
| 65 | + .matchHeader('x-api-key', API_KEY) |
| 66 | + .matchHeader('anthropic-version', '2023-06-01') |
| 67 | + .reply(200, mockResponse); |
| 68 | + |
| 69 | + await client.createMessage({ |
| 70 | + model: 'claude-sonnet-4-5-20250929', |
| 71 | + max_tokens: 1024, |
| 72 | + system: 'You are a helpful assistant.', |
| 73 | + messages: [{ role: 'user', content: 'Hello' }], |
| 74 | + }); |
| 75 | + |
| 76 | + expect(scope.isDone()).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return parsed response on success', async () => { |
| 80 | + nock('https://api.anthropic.com') |
| 81 | + .post('/v1/messages') |
| 82 | + .reply(200, mockResponse); |
| 83 | + |
| 84 | + const result = await client.createMessage({ |
| 85 | + model: 'claude-sonnet-4-5-20250929', |
| 86 | + max_tokens: 1024, |
| 87 | + system: 'You are a helpful assistant.', |
| 88 | + messages: [{ role: 'user', content: 'Hello' }], |
| 89 | + }); |
| 90 | + |
| 91 | + expect(result).toEqual(mockResponse); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle tool use response', async () => { |
| 95 | + const toolUseResponse: AnthropicResponse = { |
| 96 | + content: [ |
| 97 | + { |
| 98 | + input: { |
| 99 | + englishName: 'Google', |
| 100 | + nativeName: 'Google', |
| 101 | + domain: 'google.com', |
| 102 | + }, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }; |
| 106 | + |
| 107 | + nock('https://api.anthropic.com') |
| 108 | + .post('/v1/messages') |
| 109 | + .reply(200, toolUseResponse); |
| 110 | + |
| 111 | + const result = await client.createMessage({ |
| 112 | + model: 'claude-sonnet-4-5-20250929', |
| 113 | + max_tokens: 1024, |
| 114 | + system: 'You are a helpful assistant.', |
| 115 | + messages: [{ role: 'user', content: 'Google' }], |
| 116 | + tools: [ |
| 117 | + { |
| 118 | + name: 'organization_info', |
| 119 | + description: 'Gets information about the given organization', |
| 120 | + input_schema: { |
| 121 | + type: 'object', |
| 122 | + properties: { |
| 123 | + englishName: { type: 'string' }, |
| 124 | + nativeName: { type: 'string' }, |
| 125 | + domain: { type: 'string' }, |
| 126 | + }, |
| 127 | + required: ['englishName', 'nativeName', 'domain'], |
| 128 | + }, |
| 129 | + }, |
| 130 | + ], |
| 131 | + tool_choice: { |
| 132 | + type: 'tool', |
| 133 | + name: 'organization_info', |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + expect(result.content[0].input).toEqual({ |
| 138 | + englishName: 'Google', |
| 139 | + nativeName: 'Google', |
| 140 | + domain: 'google.com', |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should throw error on API failure', async () => { |
| 145 | + nock('https://api.anthropic.com') |
| 146 | + .post('/v1/messages') |
| 147 | + .reply(400, 'Bad Request'); |
| 148 | + |
| 149 | + await expect( |
| 150 | + client.createMessage({ |
| 151 | + model: 'claude-sonnet-4-5-20250929', |
| 152 | + max_tokens: 1024, |
| 153 | + system: 'You are a helpful assistant.', |
| 154 | + messages: [{ role: 'user', content: 'Hello' }], |
| 155 | + }), |
| 156 | + ).rejects.toThrow('Anthropic API error: 400 Bad Request - Bad Request'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should throw error on 401 unauthorized', async () => { |
| 160 | + nock('https://api.anthropic.com') |
| 161 | + .post('/v1/messages') |
| 162 | + .reply(401, 'Unauthorized'); |
| 163 | + |
| 164 | + await expect( |
| 165 | + client.createMessage({ |
| 166 | + model: 'claude-sonnet-4-5-20250929', |
| 167 | + max_tokens: 1024, |
| 168 | + system: 'You are a helpful assistant.', |
| 169 | + messages: [{ role: 'user', content: 'Hello' }], |
| 170 | + }), |
| 171 | + ).rejects.toThrow('Anthropic API error: 401 Unauthorized - Unauthorized'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should throw error on 429 rate limit', async () => { |
| 175 | + nock('https://api.anthropic.com') |
| 176 | + .post('/v1/messages') |
| 177 | + .reply(429, 'Rate limit exceeded'); |
| 178 | + |
| 179 | + await expect( |
| 180 | + client.createMessage({ |
| 181 | + model: 'claude-sonnet-4-5-20250929', |
| 182 | + max_tokens: 1024, |
| 183 | + system: 'You are a helpful assistant.', |
| 184 | + messages: [{ role: 'user', content: 'Hello' }], |
| 185 | + }), |
| 186 | + ).rejects.toThrow( |
| 187 | + 'Anthropic API error: 429 Too Many Requests - Rate limit exceeded', |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should throw error on 500 server error', async () => { |
| 192 | + nock('https://api.anthropic.com') |
| 193 | + .post('/v1/messages') |
| 194 | + .reply(500, 'Internal Server Error'); |
| 195 | + |
| 196 | + await expect( |
| 197 | + client.createMessage({ |
| 198 | + model: 'claude-sonnet-4-5-20250929', |
| 199 | + max_tokens: 1024, |
| 200 | + system: 'You are a helpful assistant.', |
| 201 | + messages: [{ role: 'user', content: 'Hello' }], |
| 202 | + }), |
| 203 | + ).rejects.toThrow( |
| 204 | + 'Anthropic API error: 500 Internal Server Error - Internal Server Error', |
| 205 | + ); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should include error details in error message', async () => { |
| 209 | + const errorResponse = JSON.stringify({ |
| 210 | + error: { |
| 211 | + type: 'invalid_request_error', |
| 212 | + message: 'max_tokens must be greater than 0', |
| 213 | + }, |
| 214 | + }); |
| 215 | + |
| 216 | + nock('https://api.anthropic.com') |
| 217 | + .post('/v1/messages') |
| 218 | + .reply(400, errorResponse); |
| 219 | + |
| 220 | + await expect( |
| 221 | + client.createMessage({ |
| 222 | + model: 'claude-sonnet-4-5-20250929', |
| 223 | + max_tokens: 0, |
| 224 | + system: 'You are a helpful assistant.', |
| 225 | + messages: [{ role: 'user', content: 'Hello' }], |
| 226 | + }), |
| 227 | + ).rejects.toThrow(errorResponse); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments