58 lines
5.2 KiB
JavaScript
58 lines
5.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { fixture, compatibleFixture, completion, call, until } from './helpers.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
|
|
const add = (hub, server, name, declarations) => hub.upsert({ name, model: 'owned-model', baseUrl: server.baseUrl, requiresKey: false, capabilityDeclarations: declarations });
|
|
test('capability routing skips text-only members before acquiring a lease and records exact refusal in the original Run', async t => {
|
|
const f = fixture(t), text = await compatibleFixture(t, () => completion([], 'Text only')), tools = await compatibleFixture(t, () => completion([], 'Done'));
|
|
const provider = new Provider(), hub = provider.modelHub(f.store), a = add(hub, text, 'Text only', { tools: 'unsupported' }), b = add(hub, tools, 'Code tools', { tools: 'supported' });
|
|
const pool = hub.pool({ name: 'Ability route', members: [{ connectionId: a.id, weight: 10 }, { connectionId: b.id }] }); hub.select({ type: 'pool', id: pool.id });
|
|
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
|
|
assert.equal(f.session.status, 'completed'); assert.equal(text.requests.length, 0); assert.equal(tools.requests.length, 1);
|
|
const attempt = hub.view().attempts[0]; assert.equal(attempt.runId, f.session.runId); assert.equal(attempt.rejections[0].connectionId, a.id); assert.equal(attempt.rejections[0].capability, 'tools');
|
|
});
|
|
|
|
test('all incompatible members reject with no network request, no lease, preserved Task and explicit non-dispatched ledger', async t => {
|
|
const f = fixture(t), server = await compatibleFixture(t, () => completion());
|
|
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Unknown tools', { tools: 'unknown' }); hub.select({ type: 'connection', id: connection.id });
|
|
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
|
|
assert.equal(server.requests.length, 0); assert.equal(f.session.status, 'failed');
|
|
assert.equal(hub.view().attempts[0].status, 'rejected'); assert.equal(hub.view().attempts[0].usageStatus, 'not_dispatched');
|
|
assert.equal(hub.view().attempts[0].runId, f.session.runId); assert.equal([...hub.leases.values()].reduce((a, b) => a + b, 0), 0);
|
|
});
|
|
|
|
test('explicit synthetic tool probe verifies only actual matching call, never executes it and loses verification on revision changes', async t => {
|
|
const f = fixture(t), server = await compatibleFixture(t, (_url, body) => {
|
|
const nonce = body.messages.at(-1).content.match(/nonce ([a-f0-9-]+)/)?.[1];
|
|
return completion([call('probe_owned', 'workbench_capability_echo', { nonce })]);
|
|
});
|
|
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Probe owned', { tools: 'unknown' });
|
|
const result = await hub.probe(connection.id, 'tools'); assert.equal(result.ok, true); assert.equal(server.requests.length, 1);
|
|
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'verified');
|
|
assert.equal(f.session.events.length, 0); assert.equal(f.store.core.tool.size, 0);
|
|
const current = hub.connection(connection.id); hub.upsert({ ...current, name: 'Changed owned' });
|
|
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
|
|
});
|
|
|
|
test('plain text probe cannot verify tools; wrong tool nonce and client supplied evidence cannot claim verified support', async t => {
|
|
const f = fixture(t), server = await compatibleFixture(t, () => completion([], 'OK'));
|
|
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Text probe', { tools: 'unknown' });
|
|
await hub.probe(connection.id); assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'verified');
|
|
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
|
|
await assert.rejects(hub.probe(connection.id, 'tools'), /未标为已验证/);
|
|
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
|
|
});
|
|
|
|
test('a disabled connection invalidates an in-flight probe even when toggle mutates its original registry object', async t => {
|
|
const f = fixture(t); let release;
|
|
const server = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return completion([], 'OK'); });
|
|
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Changing probe', {});
|
|
const pending = hub.probe(connection.id), rejected = assert.rejects(pending, /检测期间连接已变化/);
|
|
await until(() => !!release); hub.toggle(connection.id, false); release(); await rejected;
|
|
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'protocol_declared');
|
|
assert.equal(hub.state.capabilityResults?.[connection.id], undefined);
|
|
assert.equal(hub.view().attempts[0].status, 'completed');
|
|
});
|