|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import type { AddressInfo } from 'node:net'; |
| 3 | +import { mkdtempSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import test from 'node:test'; |
| 7 | +import Fastify from 'fastify'; |
| 8 | + |
| 9 | +process.env.DATA_DIR = mkdtempSync(join(tmpdir(), 'chatcrystal-config-route-test-')); |
| 10 | + |
| 11 | +const { updateConfig } = await import('../config.js'); |
| 12 | +const { configRoutes } = await import('./config.js'); |
| 13 | + |
| 14 | +test('config test uses an OpenAI-compatible max_output_tokens value accepted by NewAPI', async () => { |
| 15 | + const upstream = Fastify(); |
| 16 | + let capturedMaxOutputTokens: unknown; |
| 17 | + |
| 18 | + upstream.post('/v1/responses', async (req, reply) => { |
| 19 | + const body = req.body as { max_output_tokens?: number; model?: string }; |
| 20 | + capturedMaxOutputTokens = body.max_output_tokens; |
| 21 | + |
| 22 | + if ( |
| 23 | + typeof body.max_output_tokens !== 'number' || |
| 24 | + body.max_output_tokens < 16 |
| 25 | + ) { |
| 26 | + reply.status(400); |
| 27 | + return { |
| 28 | + error: { |
| 29 | + message: `Invalid 'max_output_tokens': integer below minimum value. Expected a value >= 16, but got ${body.max_output_tokens} instead.`, |
| 30 | + type: 'invalid_request_error', |
| 31 | + param: 'max_output_tokens', |
| 32 | + code: 'integer_below_minimum', |
| 33 | + }, |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + return { |
| 38 | + id: 'resp_test', |
| 39 | + created_at: 1_700_000_000, |
| 40 | + model: body.model ?? 'gpt-5.4', |
| 41 | + output: [ |
| 42 | + { |
| 43 | + type: 'message', |
| 44 | + id: 'msg_test', |
| 45 | + role: 'assistant', |
| 46 | + content: [{ type: 'output_text', text: 'OK', annotations: [] }], |
| 47 | + }, |
| 48 | + ], |
| 49 | + usage: { input_tokens: 1, output_tokens: 1 }, |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + upstream.post('/v1/embeddings', async () => ({ |
| 54 | + data: [{ embedding: [0.1, 0.2, 0.3] }], |
| 55 | + usage: { prompt_tokens: 1 }, |
| 56 | + })); |
| 57 | + |
| 58 | + await upstream.listen({ host: '127.0.0.1', port: 0 }); |
| 59 | + const address = upstream.server.address() as AddressInfo; |
| 60 | + const baseURL = `http://127.0.0.1:${address.port}/v1`; |
| 61 | + |
| 62 | + updateConfig({ |
| 63 | + llm: { |
| 64 | + provider: 'custom', |
| 65 | + baseURL, |
| 66 | + apiKey: 'test-api-key', |
| 67 | + model: 'gpt-5.4', |
| 68 | + }, |
| 69 | + embedding: { |
| 70 | + provider: 'custom', |
| 71 | + baseURL, |
| 72 | + apiKey: 'test-api-key', |
| 73 | + model: 'text-embedding-ada-002', |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const app = Fastify(); |
| 78 | + await app.register(configRoutes); |
| 79 | + |
| 80 | + try { |
| 81 | + const response = await app.inject({ |
| 82 | + method: 'POST', |
| 83 | + url: '/api/config/test', |
| 84 | + }); |
| 85 | + const body = response.json(); |
| 86 | + |
| 87 | + assert.equal(response.statusCode, 200); |
| 88 | + assert.equal(body.data.llm.connected, true, body.data.llm.error); |
| 89 | + assert.equal(body.data.llm.response, 'OK'); |
| 90 | + assert.equal( |
| 91 | + body.data.embedding.connected, |
| 92 | + true, |
| 93 | + body.data.embedding.error, |
| 94 | + ); |
| 95 | + assert.equal(body.data.connected, true); |
| 96 | + assert.equal(typeof capturedMaxOutputTokens, 'number'); |
| 97 | + assert.ok(capturedMaxOutputTokens >= 16); |
| 98 | + } finally { |
| 99 | + await app.close(); |
| 100 | + await upstream.close(); |
| 101 | + } |
| 102 | +}); |
0 commit comments