|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'node:test'; |
| 3 | +import { once } from 'node:events'; |
| 4 | +import { translateRequest, responseEvents } from './responses.mjs'; |
| 5 | +import { gatewayServer } from './server.mjs'; |
| 6 | +import { clientCatalog } from './runtime.mjs'; |
| 7 | + |
| 8 | +const route = { id: 'fixture/model', provider: 'fixture', model: 'model', info: { |
| 9 | + name: 'Fixture', context: { contextWindow: 65536 }, defaultMaxTokens: 8192, |
| 10 | + reasoning: { efforts: [{ id: 'off' }, { id: 'low' }], defaultEffort: 'low' }, |
| 11 | +} }; |
| 12 | +async function collect(source) { const result = []; for await (const event of source) result.push(structuredClone(event)); return result; } |
| 13 | +async function* chunks(block, finish = { kind: 'stop' }) { |
| 14 | + yield { type: 'block-start', index: 0, blockType: block.type }; |
| 15 | + if (block.type === 'text') yield { type: 'text-delta', index: 0, text: block.text }; |
| 16 | + yield { type: 'block-end', index: 0, block }; |
| 17 | + yield { type: 'usage', usage: { inputTokens: 12, cacheReadTokens: 3, outputTokens: 8, reasoningTokens: 2 } }; |
| 18 | + yield { type: 'finish', reason: finish }; |
| 19 | +} |
| 20 | + |
| 21 | +test('all exact routes enter the catalog with their own context and efforts', () => { |
| 22 | + const models = clientCatalog({ routes: new Map([[route.id, route]]), defaultModel: route.id }).models; |
| 23 | + assert.equal(models[0].slug, route.id); |
| 24 | + assert.equal(models[0].context_window, 65536); |
| 25 | + assert.deepEqual(models[0].supported_reasoning_levels.map(x => x.effort), ['none', 'low']); |
| 26 | + assert.deepEqual(models[0].input_modalities, ['text']); |
| 27 | + assert.equal(models[0].priority, 0); |
| 28 | +}); |
| 29 | + |
| 30 | +test('instructions, function calls, tool evidence and custom patches survive replay', () => { |
| 31 | + const input = { instructions: 'top instructions', reasoning: { effort: 'none' }, tools: [ |
| 32 | + { type: 'function', name: 'read', parameters: { type: 'object' } }, |
| 33 | + { type: 'custom', name: 'apply_patch' }, |
| 34 | + ], input: [ |
| 35 | + { role: 'developer', content: 'developer instructions' }, |
| 36 | + { role: 'user', content: [{ type: 'input_text', text: 'fix it' }] }, |
| 37 | + { type: 'reasoning', summary: [{ type: 'summary_text', text: 'inspect first' }] }, |
| 38 | + { type: 'function_call', name: 'read', call_id: 'c1', arguments: '{"path":"x"}' }, |
| 39 | + { type: 'function_call_output', call_id: 'c1', output: 'original source' }, |
| 40 | + { type: 'custom_tool_call', name: 'apply_patch', call_id: 'c2', input: '*** Begin Patch\n*** End Patch' }, |
| 41 | + { type: 'custom_tool_call_output', call_id: 'c2', output: 'done' }, |
| 42 | + ] }; |
| 43 | + const before = structuredClone(input); |
| 44 | + const { options, toolMap } = translateRequest(input, route); |
| 45 | + assert.equal(options.system, 'top instructions\n\ndeveloper instructions'); |
| 46 | + assert.equal(options.reasoningEffort, 'off'); |
| 47 | + assert.equal(options.messages[2].content[0].content[0].text, 'original source'); |
| 48 | + assert.deepEqual(JSON.parse(options.messages[3].content[0].arguments), { input: '*** Begin Patch\n*** End Patch' }); |
| 49 | + assert.equal(toolMap.get('apply_patch').type, 'custom'); |
| 50 | + assert.deepEqual(input, before); |
| 51 | +}); |
| 52 | + |
| 53 | +test('unsupported inputs and efforts fail explicitly instead of silently discarding data', () => { |
| 54 | + for (const body of [ |
| 55 | + { input: [], previous_response_id: 'other' }, |
| 56 | + { input: [{ role: 'user', content: [{ type: 'input_image', image_url: 'anything' }] }] }, |
| 57 | + { input: [], reasoning: { effort: 'xhigh' } }, |
| 58 | + { input: [], tools: [{ type: 'web_search' }] }, |
| 59 | + ]) assert.throws(() => translateRequest(body, route)); |
| 60 | +}); |
| 61 | + |
| 62 | +test('text and reasoning preserve terminal status and cached usage', async () => { |
| 63 | + const events = await collect(responseEvents(chunks({ type: 'text', text: 'ready' }), route.id, new Map())); |
| 64 | + assert.equal(events.filter(x => x.type === 'response.output_text.delta').map(x => x.delta).join(''), 'ready'); |
| 65 | + const terminal = events.at(-1); |
| 66 | + assert.equal(terminal.type, 'response.completed'); |
| 67 | + assert.equal(terminal.response.usage.input_tokens, 15); |
| 68 | + assert.equal(terminal.response.usage.input_tokens_details.cached_tokens, 3); |
| 69 | + assert.equal(terminal.response.usage.total_tokens, 23); |
| 70 | + assert.equal(terminal.response.output[0].content[0].text, 'ready'); |
| 71 | + const reasoning = await collect(responseEvents(chunks({ type: 'reasoning', text: 'thought' }), route.id, new Map())); |
| 72 | + assert.equal(reasoning.at(-1).response.output[0].summary[0].text, 'thought'); |
| 73 | +}); |
| 74 | + |
| 75 | +test('custom and namespaced tools round-trip through function-only adapters', async () => { |
| 76 | + const body = { input: [], tools: [{ type: 'namespace', name: 'functions', tools: [{ type: 'custom', name: 'apply_patch' }] }] }; |
| 77 | + const { options, toolMap } = translateRequest(body, route); |
| 78 | + assert.equal(options.tools[0].name, 'functions__apply_patch'); |
| 79 | + const events = await collect(responseEvents(chunks({ type: 'tool-call', name: 'functions__apply_patch', id: 'c1', arguments: '{"input":"patch text"}' }, { kind: 'tool-calls' }), route.id, toolMap)); |
| 80 | + const call = events.at(-1).response.output[0]; |
| 81 | + assert.equal(call.type, 'custom_tool_call'); assert.equal(call.namespace, 'functions'); |
| 82 | + assert.equal(call.name, 'apply_patch'); assert.equal(call.input, 'patch text'); |
| 83 | + assert.equal(call.call_id, 'c1'); |
| 84 | +}); |
| 85 | + |
| 86 | +test('truncation and context errors never become completed responses', async () => { |
| 87 | + for (const [finish, type] of [ |
| 88 | + [{ kind: 'max-tokens' }, 'response.incomplete'], |
| 89 | + [{ kind: 'error', failure: { code: 'CONTEXT_WINDOW_EXCEEDED', message: 'too large' } }, 'response.failed'], |
| 90 | + ]) { |
| 91 | + const events = await collect(responseEvents(chunks({ type: 'text', text: 'partial' }, finish), route.id, new Map())); |
| 92 | + assert.equal(events.at(-1).type, type); |
| 93 | + assert.ok(!events.some(e => e.type === 'response.completed')); |
| 94 | + const tools = new Map([['write', { type: 'function', name: 'write' }]]); |
| 95 | + const failedCall = await collect(responseEvents(chunks({ type: 'tool-call', name: 'write', id: 'c', arguments: '{}' }, finish), route.id, tools)); |
| 96 | + assert.ok(!failedCall.some(e => e.type === 'response.output_item.done'), 'failed generations expose no executable tool call'); |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +test('HTTP authentication, routing, SSE and disconnect cancellation work end to end', async t => { |
| 101 | + let sawAbort = false; |
| 102 | + const adapter = { async *stream(options) { |
| 103 | + assert.equal(options.provider, 'fixture'); assert.equal(options.model, 'model'); |
| 104 | + if (options.messages[0]?.content[0]?.text === 'wait') { |
| 105 | + await new Promise(resolve => options.signal.addEventListener('abort', () => { sawAbort = true; resolve(); }, { once: true })); |
| 106 | + yield { type: 'finish', reason: { kind: 'aborted', failure: { code: 'ABORTED', message: 'aborted' } } }; return; |
| 107 | + } |
| 108 | + yield* chunks({ type: 'text', text: 'ready' }); |
| 109 | + } }; |
| 110 | + const server = gatewayServer({ routes: new Map([[route.id, { ...route, adapter }]]) }, 'test-only'); |
| 111 | + server.listen(0, '127.0.0.1'); await once(server, 'listening'); |
| 112 | + t.after(() => { server.closeAllConnections(); server.close(); }); |
| 113 | + const base = `http://127.0.0.1:${server.address().port}`; |
| 114 | + assert.equal((await fetch(base + '/v1/models')).status, 401); |
| 115 | + const headers = { authorization: 'Bearer test-only', 'content-type': 'application/json' }; |
| 116 | + const models = await (await fetch(base + '/v1/models', { headers })).json(); |
| 117 | + assert.equal(models.data[0].id, route.id); |
| 118 | + assert.equal((await fetch(base + '/v1/responses', { method: 'POST', headers, body: JSON.stringify({ model: 'missing', input: 'x' }) })).status, 404); |
| 119 | + const response = await fetch(base + '/v1/responses', { method: 'POST', headers, body: JSON.stringify({ model: route.id, input: 'x', stream: true }) }); |
| 120 | + const text = await response.text(); assert.match(text, /event: response.completed/); assert.match(text, /ready/); |
| 121 | + const cancel = new AbortController(); |
| 122 | + const pending = await fetch(base + '/v1/responses', { method: 'POST', headers, body: JSON.stringify({ model: route.id, input: 'wait', stream: true }), signal: cancel.signal }); |
| 123 | + await pending.body.getReader().read(); cancel.abort(); |
| 124 | + for (let i = 0; i < 20 && !sawAbort; i++) await new Promise(resolve => setTimeout(resolve, 10)); |
| 125 | + assert.equal(sawAbort, true); |
| 126 | +}); |
| 127 | + |
| 128 | +test('catalog reload is authenticated and preserves working routes on failure', async t => { |
| 129 | + let fail = false; |
| 130 | + const initial = { routes: new Map([[route.id, route]]) }; |
| 131 | + const next = { routes: new Map([['fixture/second', { ...route, id: 'fixture/second' }]]), failures: [] }; |
| 132 | + const server = gatewayServer(initial, 'test-only', async () => { |
| 133 | + if (fail) return { ...initial, failures: [{ message: 'offline' }] }; |
| 134 | + return next; |
| 135 | + }); |
| 136 | + server.listen(0, '127.0.0.1'); await once(server, 'listening'); |
| 137 | + t.after(() => { server.closeAllConnections(); server.close(); }); |
| 138 | + const base = `http://127.0.0.1:${server.address().port}`; |
| 139 | + assert.equal((await fetch(base + '/reload', { method: 'POST' })).status, 401); |
| 140 | + const headers = { authorization: 'Bearer test-only' }; |
| 141 | + assert.equal((await fetch(base + '/reload', { method: 'POST', headers })).status, 200); |
| 142 | + fail = true; |
| 143 | + assert.equal((await fetch(base + '/reload', { method: 'POST', headers })).status, 503); |
| 144 | + const models = await (await fetch(base + '/v1/models', { headers })).json(); |
| 145 | + assert.equal(models.data[0].id, 'fixture/second'); |
| 146 | +}); |
0 commit comments