85 lines
5.2 KiB
JavaScript
85 lines
5.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { request } from 'node:http';
|
|
import { createLoader, hasModule } from '../src/loader.mjs';
|
|
import { selectModelProvider } from '../src/model-provider-selection.mjs';
|
|
import * as fixture from '../model-provider/fixture/index.mjs';
|
|
import { createEndpoint, HOST } from '../model-provider/fixture/endpoint.mjs';
|
|
|
|
const { FIXTURE_ENTRY, FIXTURE_KEY, FIXTURE_MODEL, FIXTURE_WORD } = fixture;
|
|
|
|
test('the loader accepts the fixture as a model-provider module', async () => {
|
|
assert.equal(hasModule('model-provider', 'fixture'), true);
|
|
const log = [];
|
|
const loader = createLoader({ log: e => log.push(e) });
|
|
const r = await loader.load({ 'model-provider': ['fixture'] });
|
|
assert.deepEqual(r.refused, []);
|
|
assert.deepEqual(log, []);
|
|
assert.deepEqual(loader.snapshot('model-provider').map(m => m.name), ['fixture']);
|
|
});
|
|
|
|
test('catalogue offers one static model for pi with no upstream credential', async () => {
|
|
const c = await fixture.catalogue({ now: () => '2026-11-07T00:00:00.000Z' });
|
|
assert.equal(c.status, 'ready');
|
|
assert.deepEqual(c.models.map(m => [m.key, m.provider, m.model, m.upstream, m.available, m.harnesses]),
|
|
[['fixture-deterministic', 'proxy', FIXTURE_MODEL, 'fixture', true, ['pi']]]);
|
|
});
|
|
|
|
test('route answers only the fixture entry and only for pi', () => {
|
|
const route = fixture.route({ selection: FIXTURE_ENTRY, harness: 'pi' });
|
|
assert.deepEqual(route, { provider: 'proxy', model: FIXTURE_MODEL, upstream: 'fixture', routeId: 'fixture', endpoint: 'root', api: 'anthropic-messages', piProvider: 'mosaic-fixture', compat: null });
|
|
assert.deepEqual(fixture.route({ selection: { ...FIXTURE_ENTRY, model: 'claude-opus' }, harness: 'pi' }), { code: 'model-provider-unavailable' });
|
|
assert.deepEqual(fixture.route({ selection: { ...FIXTURE_ENTRY, upstream: 'zai' }, harness: 'pi' }), { code: 'model-provider-unavailable' });
|
|
assert.deepEqual(fixture.route({ selection: { ...FIXTURE_ENTRY, module: 'cliproxy' }, harness: 'pi' }), { code: 'model-provider-unavailable' });
|
|
assert.deepEqual(fixture.route({ selection: { ...FIXTURE_ENTRY, provider: 'anthropic' }, harness: 'pi' }), { code: 'model-provider-unavailable' });
|
|
assert.deepEqual(fixture.route({ selection: FIXTURE_ENTRY, harness: 'claude-code' }), { code: 'model-harness-incompatible' });
|
|
});
|
|
|
|
test('preflight allows the fixture model for pi without a catalogue and refuses anything else', () => {
|
|
assert.deepEqual(fixture.preflight({ selection: FIXTURE_ENTRY, harness: 'pi', catalogue: { status: 'unavailable' } }), { allow: true });
|
|
assert.deepEqual(fixture.preflight({ selection: FIXTURE_ENTRY, harness: 'claude-code' }), { allow: false, code: 'model-harness-incompatible' });
|
|
assert.deepEqual(fixture.preflight({ selection: { ...FIXTURE_ENTRY, model: 'other' }, harness: 'pi' }), { allow: false, code: 'model-provider-unavailable' });
|
|
});
|
|
|
|
test('selection picks the fixture module for its entry', () => {
|
|
const picked = selectModelProvider([{ name: 'fixture', module: fixture }], FIXTURE_ENTRY);
|
|
assert.equal(picked.name, 'fixture');
|
|
assert.equal(selectModelProvider([{ name: 'fixture', module: fixture }], { ...FIXTURE_ENTRY, module: 'cliproxy' }), null);
|
|
});
|
|
|
|
function call(baseUrl, { method = 'POST', path = '/v1/messages', key = FIXTURE_KEY, body } = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = request(`${baseUrl}${path}`, { method, headers: { 'content-type': 'application/json', ...(key ? { 'x-api-key': key } : {}) } }, res => {
|
|
let text = ''; res.setEncoding('utf8'); res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, type: res.headers['content-type'], text }));
|
|
});
|
|
req.on('error', reject);
|
|
req.end(body === undefined ? undefined : JSON.stringify(body));
|
|
});
|
|
}
|
|
|
|
test('the endpoint binds loopback, answers the fixed word and refuses a wrong key or model', async t => {
|
|
const ep = createEndpoint();
|
|
const { port, baseUrl } = await ep.listen(0);
|
|
t.after(() => ep.close());
|
|
assert.equal(ep.server.address().address, HOST);
|
|
assert.equal(baseUrl, `http://127.0.0.1:${port}`);
|
|
const health = JSON.parse((await call(baseUrl, { method: 'GET', path: '/health' })).text);
|
|
assert.deepEqual([health.ok, health.upstream, health.model], [true, 'none', FIXTURE_MODEL]);
|
|
|
|
const plain = await call(baseUrl, { body: { model: FIXTURE_MODEL, max_tokens: 8, messages: [{ role: 'user', content: 'hi' }] } });
|
|
assert.equal(plain.status, 200);
|
|
assert.equal(JSON.parse(plain.text).content[0].text, FIXTURE_WORD);
|
|
|
|
const stream = await call(baseUrl, { body: { model: FIXTURE_MODEL, stream: true, messages: [] } });
|
|
assert.equal(stream.status, 200);
|
|
assert.match(stream.type, /^text\/event-stream/);
|
|
assert.match(stream.text, new RegExp(`"text":"${FIXTURE_WORD}"`));
|
|
assert.match(stream.text, /event: message_stop/);
|
|
|
|
assert.equal((await call(baseUrl, { key: 'sk-ant-real-looking', body: { model: FIXTURE_MODEL } })).status, 401);
|
|
assert.equal((await call(baseUrl, { key: null, body: { model: FIXTURE_MODEL } })).status, 401);
|
|
assert.equal((await call(baseUrl, { body: { model: 'claude-opus' } })).status, 404);
|
|
assert.equal((await call(baseUrl, { method: 'GET' })).status, 404);
|
|
assert.deepEqual(ep.counts, { messages: 2, refused: 4 });
|
|
});
|