191 lines
6.7 KiB
JavaScript
191 lines
6.7 KiB
JavaScript
'use strict';
|
|
const test = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs'),
|
|
os = require('node:os'),
|
|
path = require('node:path');
|
|
const { AssistantStore } = require('../../src/modules/assistant/store.cjs'),
|
|
{ AssistantService } = require('../../src/modules/assistant/service.cjs'),
|
|
{ OperationJournal } = require('../../src/modules/assistant/operation-journal.cjs');
|
|
const admin = { user: { id: 'u1', username: 'admin', role: 'admin' } },
|
|
viewer = { user: { id: 'u2', username: 'viewer', role: 'viewer' } };
|
|
function fixture(t) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'controlled-ai-')),
|
|
store = new AssistantStore(dir),
|
|
journal = new OperationJournal({ file: path.join(dir, 'ops.sqlite') });
|
|
t.after(async () => {
|
|
await service.stop();
|
|
journal.close();
|
|
store.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
store.saveSettings({
|
|
...store.settings(),
|
|
apiKey: 'TEST-KEY-PRIVATE',
|
|
enabled: true,
|
|
runtimePath: path.join(dir, 'runtime.js'),
|
|
apiBase: 'https://example.test/anthropic',
|
|
model: 'fixture',
|
|
allowActions: true,
|
|
allowCodeRepair: true,
|
|
repairRepository: dir,
|
|
});
|
|
let called = 0;
|
|
const repair = {
|
|
readSources: () => ({
|
|
baselineCommit: 'a'.repeat(40),
|
|
files: [{ path: 'src/a.cjs', content: 'module.exports=1;' }],
|
|
}),
|
|
run: async (input) => {
|
|
called++;
|
|
assert.equal(input.expectedCommit, 'a'.repeat(40));
|
|
return {
|
|
operationId: 'repair-1',
|
|
status: 'rolled_back',
|
|
previousCommit: input.expectedCommit,
|
|
resultCommit: 'b'.repeat(40),
|
|
testOutcome: 'failed',
|
|
};
|
|
},
|
|
};
|
|
const runtime = {
|
|
probe: async () => ({ available: true }),
|
|
chat: async (input) => {
|
|
assert.equal(input.context.repair.files[0].path, 'src/a.cjs');
|
|
return JSON.stringify({
|
|
reply: '修复建议',
|
|
actions: [],
|
|
repair: { files: [{ path: 'src/a.cjs', content: 'module.exports=2;' }] },
|
|
});
|
|
},
|
|
};
|
|
const service = new AssistantService({ store, runtime, journal, repairFactory: () => repair });
|
|
return { store, journal, service, calls: () => called };
|
|
}
|
|
test('admin selected source becomes a reviewed repair proposal; execution and rollback result remain auditable', async (t) => {
|
|
const f = fixture(t),
|
|
c = f.store.create('u1'),
|
|
turn = f.service.sendRepair(c.id, admin, '修复测试', ['src/a.cjs']);
|
|
await f.service.running.get(turn.id).task;
|
|
const saved = f.store.conversation(c.id, 'u1').turns[0];
|
|
assert.equal(saved.status, 'success', saved.error);
|
|
assert.equal(f.calls(), 0);
|
|
const action = saved.actions[0];
|
|
assert.equal(action.kind, 'repair_code');
|
|
assert.equal(action.beforeFiles[0].content, 'module.exports=1;');
|
|
const result = await f.service.executeAction(c.id, action.id, admin);
|
|
assert.equal(result.result.status, 'rolled_back');
|
|
assert.equal(f.calls(), 1);
|
|
assert(f.service.operations(admin).length >= 4);
|
|
assert.deepEqual(f.service.operations(viewer), []);
|
|
assert.doesNotMatch(JSON.stringify(f.service.operations(admin)), /TEST-KEY-PRIVATE/);
|
|
await assert.rejects(f.service.executeAction(c.id, action.id, admin), (e) => e.status === 409);
|
|
});
|
|
test('repair permission is checked again at execution and viewers cannot request code', async (t) => {
|
|
const f = fixture(t),
|
|
c = f.store.create('u1'),
|
|
turn = f.service.sendRepair(c.id, admin, '修复测试', ['src/a.cjs']);
|
|
await f.service.running.get(turn.id).task;
|
|
const a = f.store.conversation(c.id, 'u1').turns[0].actions[0];
|
|
f.store.saveSettings({ ...f.store.settings(), apiKey: '', allowCodeRepair: false });
|
|
await assert.rejects(f.service.executeAction(c.id, a.id, admin), (e) => e.status === 403);
|
|
assert.equal(f.calls(), 0);
|
|
assert.throws(
|
|
() => f.service.sendRepair(c.id, viewer, '修改', ['src/a.cjs']),
|
|
(e) => e.status === 403,
|
|
);
|
|
});
|
|
test('repair response cannot change unselected paths or expose the configured model key', () => {
|
|
const { parseRepairResponse } = require('../../src/modules/assistant/repair-response.cjs'),
|
|
context = { baselineCommit: 'a'.repeat(40), files: [{ path: 'src/a.cjs', content: 'old' }] };
|
|
for (const file of [
|
|
{ path: 'src/b.cjs', content: 'new' },
|
|
{ path: 'src/a.cjs', content: 'SECRET-CONFIG-KEY' },
|
|
])
|
|
assert.throws(() =>
|
|
parseRepairResponse(
|
|
JSON.stringify({ reply: 'proposal', actions: [], repair: { files: [file] } }),
|
|
{},
|
|
admin.user,
|
|
context,
|
|
'SECRET-CONFIG-KEY',
|
|
),
|
|
);
|
|
assert.throws(() =>
|
|
parseRepairResponse(
|
|
JSON.stringify({
|
|
reply: 'proposal',
|
|
actions: [],
|
|
repair: { files: [{ path: 'src/a.cjs', content: 'new' }] },
|
|
}),
|
|
{},
|
|
admin.user,
|
|
null,
|
|
'',
|
|
),
|
|
);
|
|
});
|
|
test('audit failure stops before calling the model and leaves a recoverable error turn', async (t) => {
|
|
const f = fixture(t),
|
|
c = f.store.create('u1');
|
|
f.journal.append = () => {
|
|
throw Error('journal unavailable');
|
|
};
|
|
assert.throws(
|
|
() => f.service.sendRepair(c.id, admin, 'repair', ['src/a.cjs']),
|
|
(e) => e.status === 503,
|
|
);
|
|
assert.equal(f.store.conversation(c.id, 'u1').turns[0].status, 'error');
|
|
assert.equal(f.service.running.size, 0);
|
|
});
|
|
test('business diagnosis includes authorized automatic orders and blockers without fields or frozen mail bodies', () => {
|
|
const { snapshot } = require('../../src/modules/assistant/policy.cjs'),
|
|
rows = {
|
|
orders: [
|
|
{
|
|
id: 'own',
|
|
sample: { connectionId: 'a', fields: { secret: 'PRIVATE-FIELD' } },
|
|
event: { orderNo: '123', connectionId: 'a', processName: '流程', nodeName: '通知' },
|
|
status: 'pending',
|
|
archive: { files: [{}] },
|
|
},
|
|
{
|
|
id: 'hidden',
|
|
sample: { connectionId: 'b' },
|
|
event: { orderNo: '999' },
|
|
status: 'failed',
|
|
},
|
|
],
|
|
batches: [],
|
|
'execution-tasks': [
|
|
{
|
|
id: 'task',
|
|
connectionId: 'a',
|
|
status: 'blocked',
|
|
error: 'missing field',
|
|
row: { processName: '流程', nodeName: '通知' },
|
|
},
|
|
],
|
|
},
|
|
workspace = {
|
|
snapshot: () => ({
|
|
connections: [
|
|
{ id: 'a', owner: 'operator' },
|
|
{ id: 'b', owner: 'other' },
|
|
],
|
|
exceptions: [],
|
|
}),
|
|
recordSources: () => [
|
|
{ id: 'a', owner: 'operator' },
|
|
{ id: 'b', owner: 'other' },
|
|
],
|
|
},
|
|
s = snapshot(
|
|
{ workspace, orderRules: { all: (k) => rows[k] || [] } },
|
|
{ id: 'op', username: 'operator', role: 'operator' },
|
|
);
|
|
assert.equal(s.business.orders.length, 1);
|
|
assert.equal(s.business.blockers[0].id, 'task');
|
|
assert.doesNotMatch(JSON.stringify(s.business), /PRIVATE-FIELD|999/);
|
|
});
|