|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import express from 'express'; |
| 3 | +import Anthropic from '@anthropic-ai/sdk'; |
| 4 | + |
| 5 | +function startMockAnthropicServer() { |
| 6 | + const app = express(); |
| 7 | + app.use(express.json()); |
| 8 | + |
| 9 | + app.post('/anthropic/v1/messages', (req, res) => { |
| 10 | + const model = req.body.model; |
| 11 | + |
| 12 | + res.set('request-id', 'req_withresponse_test'); |
| 13 | + |
| 14 | + if (req.body.stream) { |
| 15 | + res.set('content-type', 'text/event-stream'); |
| 16 | + res.flushHeaders(); |
| 17 | + |
| 18 | + const events = [ |
| 19 | + `event: message_start\ndata: ${JSON.stringify({ type: 'message_start', message: { id: 'msg_stream_withresponse', type: 'message', role: 'assistant', model, content: [], usage: { input_tokens: 10 } } })}\n\n`, |
| 20 | + `event: content_block_start\ndata: ${JSON.stringify({ type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } })}\n\n`, |
| 21 | + `event: content_block_delta\ndata: ${JSON.stringify({ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Streaming with response!' } })}\n\n`, |
| 22 | + `event: content_block_stop\ndata: ${JSON.stringify({ type: 'content_block_stop', index: 0 })}\n\n`, |
| 23 | + `event: message_delta\ndata: ${JSON.stringify({ type: 'message_delta', delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 5 } })}\n\n`, |
| 24 | + `event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop' })}\n\n`, |
| 25 | + ]; |
| 26 | + |
| 27 | + let i = 0; |
| 28 | + const interval = setInterval(() => { |
| 29 | + if (i < events.length) { |
| 30 | + res.write(events[i]); |
| 31 | + i++; |
| 32 | + } else { |
| 33 | + clearInterval(interval); |
| 34 | + res.end(); |
| 35 | + } |
| 36 | + }, 10); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + res.send({ |
| 41 | + id: 'msg_withresponse', |
| 42 | + type: 'message', |
| 43 | + model: model, |
| 44 | + role: 'assistant', |
| 45 | + content: [ |
| 46 | + { |
| 47 | + type: 'text', |
| 48 | + text: 'Testing .withResponse() method!', |
| 49 | + }, |
| 50 | + ], |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + return new Promise(resolve => { |
| 55 | + const server = app.listen(0, () => { |
| 56 | + resolve(server); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function run() { |
| 62 | + const server = await startMockAnthropicServer(); |
| 63 | + |
| 64 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 65 | + const client = new Anthropic({ |
| 66 | + apiKey: 'mock-api-key', |
| 67 | + baseURL: `http://localhost:${server.address().port}/anthropic`, |
| 68 | + }); |
| 69 | + |
| 70 | + // Test 1: Verify .withResponse() method is preserved and works correctly |
| 71 | + const result = client.messages.create({ |
| 72 | + model: 'claude-3-haiku-20240307', |
| 73 | + messages: [{ role: 'user', content: 'Test withResponse' }], |
| 74 | + }); |
| 75 | + |
| 76 | + // Verify .withResponse() method exists and can be called |
| 77 | + if (typeof result.withResponse !== 'function') { |
| 78 | + throw new Error('.withResponse() method does not exist'); |
| 79 | + } |
| 80 | + |
| 81 | + // Call .withResponse() and verify structure |
| 82 | + const withResponseResult = await result.withResponse(); |
| 83 | + |
| 84 | + // Verify expected properties are present |
| 85 | + if (!withResponseResult.data) { |
| 86 | + throw new Error('.withResponse() did not return data'); |
| 87 | + } |
| 88 | + if (!withResponseResult.response) { |
| 89 | + throw new Error('.withResponse() did not return response'); |
| 90 | + } |
| 91 | + if (withResponseResult.request_id === undefined) { |
| 92 | + throw new Error('.withResponse() did not return request_id'); |
| 93 | + } |
| 94 | + |
| 95 | + // Verify returned data structure matches expected Anthropic response |
| 96 | + const { data } = withResponseResult; |
| 97 | + if (data.id !== 'msg_withresponse') { |
| 98 | + throw new Error(`Expected data.id to be 'msg_withresponse', got '${data.id}'`); |
| 99 | + } |
| 100 | + if (data.model !== 'claude-3-haiku-20240307') { |
| 101 | + throw new Error(`Expected data.model to be 'claude-3-haiku-20240307', got '${data.model}'`); |
| 102 | + } |
| 103 | + if (data.content[0].text !== 'Testing .withResponse() method!') { |
| 104 | + throw new Error( |
| 105 | + `Expected data.content[0].text to be 'Testing .withResponse() method!', got '${data.content[0].text}'`, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + // Verify response is a Response object with correct headers |
| 110 | + if (!(withResponseResult.response instanceof Response)) { |
| 111 | + throw new Error('response is not a Response object'); |
| 112 | + } |
| 113 | + if (withResponseResult.response.headers.get('request-id') !== 'req_withresponse_test') { |
| 114 | + throw new Error( |
| 115 | + `Expected request-id header 'req_withresponse_test', got '${withResponseResult.response.headers.get('request-id')}'`, |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + // Verify request_id matches the header |
| 120 | + if (withResponseResult.request_id !== 'req_withresponse_test') { |
| 121 | + throw new Error(`Expected request_id 'req_withresponse_test', got '${withResponseResult.request_id}'`); |
| 122 | + } |
| 123 | + |
| 124 | + // Test 2: Verify .asResponse() method works |
| 125 | + const result2 = client.messages.create({ |
| 126 | + model: 'claude-3-haiku-20240307', |
| 127 | + messages: [{ role: 'user', content: 'Test asResponse' }], |
| 128 | + }); |
| 129 | + |
| 130 | + // Verify .asResponse() method exists and can be called |
| 131 | + if (typeof result2.asResponse !== 'function') { |
| 132 | + throw new Error('.asResponse() method does not exist'); |
| 133 | + } |
| 134 | + |
| 135 | + // Call .asResponse() and verify it returns raw Response |
| 136 | + const rawResponse = await result2.asResponse(); |
| 137 | + |
| 138 | + // Verify response is a Response object with correct headers |
| 139 | + if (!(rawResponse instanceof Response)) { |
| 140 | + throw new Error('.asResponse() did not return a Response object'); |
| 141 | + } |
| 142 | + |
| 143 | + // Verify response has correct status |
| 144 | + if (rawResponse.status !== 200) { |
| 145 | + throw new Error(`Expected status 200, got ${rawResponse.status}`); |
| 146 | + } |
| 147 | + |
| 148 | + // Verify response headers |
| 149 | + if (rawResponse.headers.get('request-id') !== 'req_withresponse_test') { |
| 150 | + throw new Error( |
| 151 | + `Expected request-id header 'req_withresponse_test', got '${rawResponse.headers.get('request-id')}'`, |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + // Test 3: Verify .withResponse() works with streaming (stream: true) |
| 156 | + const streamResult = client.messages.create({ |
| 157 | + model: 'claude-3-haiku-20240307', |
| 158 | + messages: [{ role: 'user', content: 'Test stream withResponse' }], |
| 159 | + stream: true, |
| 160 | + }); |
| 161 | + |
| 162 | + if (typeof streamResult.withResponse !== 'function') { |
| 163 | + throw new Error('.withResponse() method does not exist on streaming result'); |
| 164 | + } |
| 165 | + |
| 166 | + const streamWithResponse = await streamResult.withResponse(); |
| 167 | + |
| 168 | + if (!streamWithResponse.data) { |
| 169 | + throw new Error('streaming .withResponse() did not return data'); |
| 170 | + } |
| 171 | + if (!streamWithResponse.response) { |
| 172 | + throw new Error('streaming .withResponse() did not return response'); |
| 173 | + } |
| 174 | + if (streamWithResponse.request_id === undefined) { |
| 175 | + throw new Error('streaming .withResponse() did not return request_id'); |
| 176 | + } |
| 177 | + |
| 178 | + if (!(streamWithResponse.response instanceof Response)) { |
| 179 | + throw new Error('streaming response is not a Response object'); |
| 180 | + } |
| 181 | + if (streamWithResponse.response.headers.get('request-id') !== 'req_withresponse_test') { |
| 182 | + throw new Error( |
| 183 | + `Expected request-id header 'req_withresponse_test', got '${streamWithResponse.response.headers.get('request-id')}'`, |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + // Consume the stream to allow span to complete |
| 188 | + for await (const _ of streamWithResponse.data) { |
| 189 | + void _; |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + // Wait for the stream event handler to finish |
| 194 | + await Sentry.flush(2000); |
| 195 | + |
| 196 | + server.close(); |
| 197 | +} |
| 198 | + |
| 199 | +run(); |
0 commit comments