80 lines
6.4 KiB
JavaScript
80 lines
6.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash } from 'node:crypto';
|
|
import { fixture, compatibleFixture, completion, until, launchWorkbench } from './helpers.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
import { validateRuntimeBudgets } from '../src/core/models/runtime-settings.mjs';
|
|
|
|
test('selected connection owns the actual endpoint, credential, wire model and mismatch even when legacy is configured for GPT', async t => {
|
|
const f = fixture(t);
|
|
const legacy = await compatibleFixture(t, () => ({ ...completion([], 'Legacy response'), model: 'owned-gpt' }));
|
|
const chosen = await compatibleFixture(t, () => ({ ...completion([], 'Chosen response'), model: 'owned-gpt' }));
|
|
f.store.saveConfig({ mode: 'compatible', baseUrl: legacy.baseUrl, model: 'owned-gpt', maxSteps: 1 });
|
|
const provider = new Provider(); provider.setKey('owned-legacy-key');
|
|
const hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Chosen Claude route', model: 'owned-claude',
|
|
baseUrl: chosen.baseUrl, apiKey: 'owned-chosen-key', modelIdentityPolicy: 'require-match' });
|
|
hub.select({ type: 'connection', id: connection.id });
|
|
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
|
|
runtime.start(f.session.id, 'Reply only; no tools.'); await runtime.active.get(f.session.id).promise;
|
|
assert.equal(legacy.requests.length, 0); assert.equal(chosen.requests.length, 1);
|
|
assert.equal(chosen.requests[0].headers.authorization, 'Bearer owned-chosen-key');
|
|
assert.equal(chosen.requests[0].input.model, 'owned-claude');
|
|
const attempt = hub.view().attempts[0], run = f.store.core.run.get(f.session.runId);
|
|
assert.equal(attempt.connectionId, connection.id); assert.equal(attempt.poolId, null);
|
|
assert.equal(attempt.requestSha256, createHash('sha256').update(JSON.stringify(chosen.requests[0].input)).digest('hex'));
|
|
assert.equal(run.stopReason, 'model_identity'); assert.equal(run.model.model, 'owned-claude');
|
|
assert.equal(f.store.core.tool.size, 0); assert.deepEqual(attempt.modelIdentity.reportedModels, ['owned-gpt']);
|
|
const sealedRun = structuredClone(run);
|
|
hub.select({ type: 'legacy' }, { mode: 'compatible' });
|
|
const next = f.store.createSession(f.project.id); runtime.start(next.id, 'Reply only; no tools.'); await runtime.active.get(next.id).promise;
|
|
assert.equal(legacy.requests.length, 1); assert.equal(chosen.requests.length, 1);
|
|
assert.equal(legacy.requests[0].headers.authorization, 'Bearer owned-legacy-key');
|
|
assert.equal(legacy.requests[0].input.model, 'owned-gpt'); assert.equal(next.status, 'completed'); assert.deepEqual(run, sealedRun);
|
|
});
|
|
|
|
test('budget-only API and demo/real selection preserve routes, reject injected endpoint fields and publish authoritative selection without keys', async t => {
|
|
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
|
|
const connection = await host.api('/model-center/connections', { name: 'Owned model', model: 'owned-real',
|
|
baseUrl: 'http://127.0.0.1:9/v1', apiKey: 'owned-private-key' });
|
|
await host.api('/model-center/select', { type: 'connection', id: connection.id });
|
|
const before = await host.api('/model-center'), original = await host.api('/model-center/selection');
|
|
const result = await host.api('/model-center/runtime-budgets', { contextLimit: 32000, maxSteps: 8, maxRunTokens: 90000 });
|
|
assert.deepEqual(result.modelSelection, { type: 'connection', id: connection.id, name: 'Owned model' });
|
|
assert.equal(result.config.contextLimit, 32000); assert.equal(result.config.baseUrl, original.config.baseUrl);
|
|
assert.deepEqual((await host.api('/model-center')).connections.find(c => c.id === connection.id), before.connections.find(c => c.id === connection.id));
|
|
await assert.rejects(host.api('/model-center/runtime-budgets', { baseUrl: 'http://127.0.0.1:1' }), /只接受运行设置/);
|
|
const unlimitedSteps = await host.api('/model-center/runtime-budgets', { maxSteps: 0 });
|
|
assert.equal(unlimitedSteps.config.maxSteps, 0); assert.deepEqual(unlimitedSteps.modelSelection, result.modelSelection);
|
|
await assert.rejects(host.api('/model-center/runtime-budgets', { maxSteps: -1 }), /范围内的整数/);
|
|
await host.api('/model-center/mode', { mode: 'demo' });
|
|
assert.deepEqual((await host.api('/model-center')).selected, before.selected);
|
|
await host.api('/model-center/select', { type: 'legacy', id: 'legacy-default' });
|
|
const legacy = await host.api('/model-center/selection'); assert.equal(legacy.config.mode, 'compatible');
|
|
assert.equal(legacy.modelSelection.type, 'legacy');
|
|
await host.api('/model-center/select', { type: 'connection', id: connection.id });
|
|
const current = await host.api('/model-center/selection'); assert.equal(current.modelSelection.name, 'Owned model');
|
|
assert.equal(JSON.stringify(current).includes('owned-private-key'), false);
|
|
assert.equal((await host.api('/model-center')).attempts.length, 0);
|
|
for (const input of [null, [], {}, { maxSteps: '8' }, { maxTaskTokens: Infinity }, { apiKey: 'never-save' }]) {
|
|
assert.throws(() => validateRuntimeBudgets(input));
|
|
}
|
|
});
|
|
|
|
test('another session cannot change default model, mode or budgets while a request is active', async t => {
|
|
let release; const pending = new Promise(resolve => { release = resolve; });
|
|
const upstream = await compatibleFixture(t, async () => { await pending; return { ...completion([], 'Done'), model: 'owned-running' }; });
|
|
t.after(() => release());
|
|
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
|
|
const connection = await host.api('/model-center/connections', { name: 'Active model', model: 'owned-running', baseUrl: upstream.baseUrl, requiresKey: false });
|
|
await host.api('/model-center/select', { type: 'connection', id: connection.id });
|
|
await host.api('/sessions/' + f.session.id + '/messages', { content: 'Reply only; no tools.' });
|
|
await until(() => upstream.requests.length === 1);
|
|
const before = await host.api('/model-center/selection');
|
|
for (const [route, input] of [['runtime-budgets', { maxSteps: 8 }], ['mode', { mode: 'demo' }], ['select', { type: 'legacy' }]]) {
|
|
await assert.rejects(host.api('/model-center/' + route, input), error => error.status === 409);
|
|
}
|
|
assert.deepEqual(await host.api('/model-center/selection'), before);
|
|
release(); await until(async () => (await host.api('/sessions/' + f.session.id)).status !== 'running');
|
|
});
|