345 lines
12 KiB
JavaScript
345 lines
12 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict');
|
|
const { managementFixture } = require('./management-fixture.cjs');
|
|
const provider = () => ({
|
|
version: 0,
|
|
name: 'Fixture OpenAI',
|
|
preset: 'openai',
|
|
protocol: 'openai-completions',
|
|
apiBase: 'https://models.example/v1',
|
|
apiKey: 'TEST_PROVIDER_PRIVATE_KEY',
|
|
enabled: true,
|
|
models: [
|
|
{ id: 'model-a', name: 'Model A' },
|
|
{ id: 'model-b', name: 'Model B' },
|
|
],
|
|
});
|
|
test('provider administration is authenticated, admin-only, versioned and never returns keys', async (t) => {
|
|
const f = await managementFixture(t),
|
|
admin = await f.login(),
|
|
viewer = await f.login('viewer');
|
|
assert.equal((await f.call('/assistant/providers')).status, 401);
|
|
assert.equal((await f.call('/assistant/providers', provider(), viewer)).status, 403);
|
|
const result = await f.call('/assistant/providers', provider(), admin);
|
|
assert.equal(result.status, 201);
|
|
const list = await result.json();
|
|
assert.doesNotMatch(JSON.stringify(list), /TEST_PROVIDER_PRIVATE_KEY|"apiKey":/);
|
|
assert.ok(list.providers.some((p) => p.name === 'Fixture OpenAI'));
|
|
const publicList = await (await f.call('/assistant/providers', undefined, viewer)).json();
|
|
assert.equal(publicList.providers.length, 1);
|
|
assert.equal(publicList.providers[0].apiBase, undefined);
|
|
});
|
|
test('default/per-conversation model routing is used and frozen into each accepted turn', async (t) => {
|
|
const f = await managementFixture(t),
|
|
cookie = await f.login(),
|
|
list = await (await f.call('/assistant/providers', provider(), cookie)).json(),
|
|
p = list.providers.find((row) => row.name === 'Fixture OpenAI');
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/providers/default',
|
|
{ version: list.version, providerId: p.id, modelId: 'model-a' },
|
|
cookie,
|
|
)
|
|
).status,
|
|
200,
|
|
);
|
|
const runtime = {
|
|
version: f.store.settings().version,
|
|
runtimePath: process.execPath,
|
|
enabled: true,
|
|
allowActions: false,
|
|
allowCodeRepair: false,
|
|
repairRepository: '',
|
|
timeoutSeconds: 30,
|
|
dailyLimit: 100,
|
|
};
|
|
assert.equal((await f.call('/assistant/settings', runtime, cookie)).status, 200);
|
|
const c = await (await f.call('/assistant/conversations', {}, cookie)).json();
|
|
const selected = await f.call(
|
|
'/assistant/conversations/' + c.id + '/model',
|
|
{ version: c.version, providerId: p.id, modelId: 'model-b' },
|
|
cookie,
|
|
);
|
|
assert.equal(selected.status, 200);
|
|
const sent = await (
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/messages',
|
|
{ text: 'diagnose', requestId: 'fixed-model' },
|
|
cookie,
|
|
)
|
|
).json();
|
|
await f.assistant.running.get(sent.id)?.task;
|
|
assert.equal(f.lastConfig().model, 'model-b');
|
|
const saved = await (await f.call('/assistant/conversations/' + c.id, undefined, cookie)).json();
|
|
assert.equal(saved.turns[0].modelSelection.providerId, p.id);
|
|
assert.equal(saved.turns[0].modelSelection.modelId, 'model-b');
|
|
assert.doesNotMatch(JSON.stringify(saved), /TEST_PROVIDER_PRIVATE_KEY/);
|
|
});
|
|
test('known credentials of every provider are scrubbed before another provider receives a message', async (t) => {
|
|
const f = await managementFixture(t),
|
|
cookie = await f.login();
|
|
let list = await (await f.call('/assistant/providers', provider(), cookie)).json();
|
|
const first = list.providers.find((p) => p.name === 'Fixture OpenAI');
|
|
list = await (
|
|
await f.call(
|
|
'/assistant/providers',
|
|
{ ...provider(), name: 'Second', apiKey: 'SECOND_PROVIDER_SECRET_RAW_VALUE' },
|
|
cookie,
|
|
)
|
|
).json();
|
|
await f.call(
|
|
'/assistant/providers/default',
|
|
{ version: list.version, providerId: first.id, modelId: 'model-a' },
|
|
cookie,
|
|
);
|
|
f.store.saveSettings({
|
|
version: f.store.settings().version,
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
allowActions: false,
|
|
});
|
|
let input;
|
|
f.assistant.runtime.chat = async (value) => {
|
|
input = value;
|
|
return JSON.stringify({ reply: 'ok', actions: [] });
|
|
};
|
|
const c = await (await f.call('/assistant/conversations', {}, cookie)).json(),
|
|
sent = await (
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/messages',
|
|
{ text: 'diagnose SECOND_PROVIDER_SECRET_RAW_VALUE' },
|
|
cookie,
|
|
)
|
|
).json();
|
|
await f.assistant.running.get(sent.id)?.task;
|
|
assert.doesNotMatch(input.text, /SECOND_PROVIDER_SECRET_RAW_VALUE/);
|
|
assert.doesNotMatch(
|
|
JSON.stringify(
|
|
f.store.conversation(c.id, f.users.users().find((u) => u.username === 'admin').id),
|
|
),
|
|
/SECOND_PROVIDER_SECRET_RAW_VALUE/,
|
|
);
|
|
});
|
|
test('selected repair sources containing another provider credential are rejected before accepting a turn', async (t) => {
|
|
const f = await managementFixture(t);
|
|
f.store.providers.save(provider());
|
|
f.store.providers.save({
|
|
...provider(),
|
|
name: 'Second',
|
|
apiKey: 'SECOND_PROVIDER_SECRET_RAW_VALUE',
|
|
});
|
|
const dir = f.store.providers.list(),
|
|
first = dir.providers.find((p) => p.name === 'Fixture OpenAI');
|
|
f.store.providers.chooseDefault({
|
|
version: dir.version,
|
|
providerId: first.id,
|
|
modelId: 'model-a',
|
|
});
|
|
f.store.saveSettings({
|
|
version: f.store.settings().version,
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
allowActions: true,
|
|
allowCodeRepair: true,
|
|
repairRepository: process.cwd(),
|
|
});
|
|
f.assistant.repairFactory = () => ({
|
|
readSources: () => ({
|
|
baselineCommit: 'a'.repeat(40),
|
|
files: [{ path: 'src/demo.cjs', content: 'const value="SECOND_PROVIDER_SECRET_RAW_VALUE";' }],
|
|
}),
|
|
});
|
|
const user = f.users.users().find((u) => u.username === 'admin'),
|
|
c = f.store.create(user.id);
|
|
assert.throws(
|
|
() => f.assistant.sendRepair(c.id, { user, sessionId: 'fixture' }, 'fix', ['src/demo.cjs']),
|
|
/凭据/,
|
|
);
|
|
assert.equal(f.store.usage().calls, 0);
|
|
});
|
|
test('legacy credentials migrate into the provider vault once and deleting the provider removes the legacy copy', async (t) => {
|
|
const f = await managementFixture(t);
|
|
f.store.saveSettings({
|
|
...f.store.settings(),
|
|
apiKey: 'LEGACY_ONLY_PRIVATE_KEY',
|
|
enabled: false,
|
|
});
|
|
const row = f.store.db.prepare('SELECT * FROM config WHERE id=1').get();
|
|
assert.equal(f.store.decode('config', row.payload).apiKey, '');
|
|
assert.equal(f.store.secretSettings().apiKey, 'LEGACY_ONLY_PRIVATE_KEY');
|
|
const legacy = f.store.providers.list().providers.find((p) => p.id === 'deepseek');
|
|
f.store.providers.remove(legacy.id, { version: legacy.version });
|
|
assert.equal(f.store.secretSettings().apiKey, '');
|
|
assert.equal(f.store.settings().hasApiKey, false);
|
|
});
|
|
test('changing a conversation selection only changes the next request, not the running turn', async (t) => {
|
|
const f = await managementFixture(t),
|
|
cookie = await f.login(),
|
|
list = await (await f.call('/assistant/providers', provider(), cookie)).json(),
|
|
p = list.providers.find((p) => p.name === 'Fixture OpenAI');
|
|
await f.call(
|
|
'/assistant/providers/default',
|
|
{ version: list.version, providerId: p.id, modelId: 'model-a' },
|
|
cookie,
|
|
);
|
|
f.store.saveSettings({
|
|
version: f.store.settings().version,
|
|
enabled: true,
|
|
runtimePath: process.execPath,
|
|
allowActions: false,
|
|
});
|
|
let release, started;
|
|
f.assistant.runtime.chat = async (input) => {
|
|
started = input.config.model;
|
|
return new Promise((r) => (release = () => r(JSON.stringify({ reply: 'done', actions: [] }))));
|
|
};
|
|
const c = await (await f.call('/assistant/conversations', {}, cookie)).json(),
|
|
turn = await (
|
|
await f.call('/assistant/conversations/' + c.id + '/messages', { text: 'pending' }, cookie)
|
|
).json();
|
|
await new Promise((r) => setImmediate(r));
|
|
const current = f.store.conversation(
|
|
c.id,
|
|
f.users.users().find((u) => u.username === 'admin').id,
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/model',
|
|
{ version: current.version, providerId: p.id, modelId: 'model-b' },
|
|
cookie,
|
|
)
|
|
).status,
|
|
200,
|
|
);
|
|
assert.equal(started, 'model-a');
|
|
assert.equal(
|
|
(await f.call('/assistant/providers/' + p.id + '/delete', { version: p.version }, cookie))
|
|
.status,
|
|
409,
|
|
);
|
|
release();
|
|
await f.assistant.running.get(turn.id)?.task;
|
|
assert.equal(
|
|
f.store.conversation(c.id, f.users.users().find((u) => u.username === 'admin').id).turns[0]
|
|
.modelSelection.modelId,
|
|
'model-a',
|
|
);
|
|
});
|
|
test('recycle-bin API cannot act on another owner or refund model usage', async (t) => {
|
|
const f = await managementFixture(t),
|
|
admin = await f.login(),
|
|
viewer = await f.login('viewer'),
|
|
c = await (await f.call('/assistant/conversations', {}, admin)).json();
|
|
const owner = f.users.users().find((u) => u.username === 'admin').id,
|
|
t1 = f.store.beginTurn(c.id, owner, 'used');
|
|
f.store.updateTurn(t1.id, 'success', { reply: 'done' });
|
|
const current = f.store.conversation(c.id, owner);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/delete',
|
|
{ version: current.version },
|
|
viewer,
|
|
)
|
|
).status,
|
|
404,
|
|
);
|
|
const response = await f.call(
|
|
'/assistant/conversations/' + c.id + '/delete',
|
|
{ version: current.version },
|
|
admin,
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const removed = await response.json();
|
|
assert.equal((await f.call('/assistant/conversations/' + c.id, undefined, admin)).status, 404);
|
|
assert.equal(
|
|
(await f.call('/assistant/conversations/' + c.id + '?scope=deleted', undefined, admin)).status,
|
|
200,
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/purge',
|
|
{ version: removed.version },
|
|
admin,
|
|
)
|
|
).status,
|
|
400,
|
|
);
|
|
assert.equal(
|
|
(
|
|
await f.call(
|
|
'/assistant/conversations/' + c.id + '/purge',
|
|
{ version: removed.version, confirm: 'delete' },
|
|
admin,
|
|
)
|
|
).status,
|
|
200,
|
|
);
|
|
assert.equal(f.store.usage().calls, 1);
|
|
});
|
|
test('deleting a subscribed conversation sends a terminal removed event instead of endless reconnect', async (t) => {
|
|
const f = await managementFixture(t),
|
|
cookie = await f.login(),
|
|
c = await (await f.call('/assistant/conversations', {}, cookie)).json(),
|
|
stream = await f.call(
|
|
'/assistant/conversations/' + c.id + '/stream?after=0',
|
|
undefined,
|
|
cookie,
|
|
),
|
|
reader = stream.body.getReader();
|
|
await f.call('/assistant/conversations/' + c.id + '/delete', { version: c.version }, cookie);
|
|
let text = '';
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
text += new TextDecoder().decode(value);
|
|
}
|
|
assert.match(text, /conversation_removed/);
|
|
});
|
|
test(
|
|
'an idle subscribed conversation observes model selection changed from another client',
|
|
{ timeout: 15000 },
|
|
async (t) => {
|
|
const f = await managementFixture(t),
|
|
cookie = await f.login();
|
|
f.store.saveSettings({ ...f.store.settings(), apiKey: 'TEST_METADATA_STREAM_KEY' });
|
|
const c = await (await f.call('/assistant/conversations', {}, cookie)).json(),
|
|
stream = await f.call(
|
|
'/assistant/conversations/' + c.id + '/stream?after=0',
|
|
undefined,
|
|
cookie,
|
|
),
|
|
reader = stream.body.getReader();
|
|
f.store.setModel(c.id, f.users.users().find((u) => u.username === 'admin').id, {
|
|
version: c.version,
|
|
providerId: 'deepseek',
|
|
modelId: f.store.settings().model,
|
|
});
|
|
let text = '',
|
|
timer;
|
|
try {
|
|
await Promise.race([
|
|
(async () => {
|
|
while (!/conversation_updated/.test(text) || !text.includes('"version":2')) {
|
|
const part = await reader.read();
|
|
if (part.done) break;
|
|
text += new TextDecoder().decode(part.value);
|
|
}
|
|
assert.match(text, /conversation_updated/);
|
|
assert.ok(text.includes('"version":2'));
|
|
})(),
|
|
new Promise((_, reject) => {
|
|
timer = setTimeout(() => reject(Error('model update was not published')), 3000);
|
|
}),
|
|
]);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
await reader.cancel();
|
|
}
|
|
},
|
|
);
|