89 lines
6.1 KiB
JavaScript
89 lines
6.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { applyChange } from '../src/server/files.mjs';
|
|
import { fixture, call, assertToolPairs } from './helpers.mjs';
|
|
|
|
const answer = content => ({ message: { role: 'assistant', content }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
|
|
const calls = (...tools) => ({ ...answer(''), message: { role: 'assistant', content: '', tool_calls: tools } });
|
|
function harness(t, respond) {
|
|
const f = fixture(t), provider = new Provider(); let round = 0;
|
|
provider.complete = async (config, messages, project, signal, progress, plan, tools) => respond(round++, { messages, tools, f });
|
|
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
|
|
return { ...f, runtime, wait: () => runtime.active.get(f.session.id).promise };
|
|
}
|
|
|
|
test('HTML without tests ends at reviewed file delivery and never offers automatic rechecking', async t => {
|
|
const f = harness(t, (round, { tools }) => {
|
|
assert.ok(!tools.some(tool => ['run_checks', 'plan_checks'].includes(tool.function.name)));
|
|
return round === 0 ? calls(call('html', 'propose_file', { path: 'bike.html', content: '<!doctype html><svg><circle r="4"/></svg>', reason: '原请求' })) : answer('文件提案已生成。');
|
|
});
|
|
f.runtime.start(f.session.id, '创建一个 HTML,使用 SVG 动画,无需测试。'); await f.wait();
|
|
const task = f.store.core.task.get(f.session.taskId), id = task.id;
|
|
assert.equal(f.session.status, 'needs_review');
|
|
applyChange(f.store, f.session, f.session.changes[0].id);
|
|
assert.equal(f.session.status, 'completed'); assert.equal(task.status, 'completed');
|
|
assert.equal(task.delivery.checks, 'not_requested'); assert.deepEqual(task.delivery.paths, ['bike.html']);
|
|
assert.match(fs.readFileSync(path.join(f.projectRoot, 'bike.html'), 'utf8'), /<svg>/);
|
|
assert.throws(() => f.runtime.continueReviewed(f.session.id), { status: 409 });
|
|
assert.equal(f.store.core.check_job.size, 0); assert.equal(f.session.approvals.length, 0);
|
|
f.runtime.start(f.session.id, '继续完成'); await f.wait();
|
|
assert.equal(f.session.taskId, id); assert.match(task.contract.goal, /SVG 动画/);
|
|
assertToolPairs(f.session.providerMessages);
|
|
});
|
|
|
|
test('diagnostic answers retain the unfinished task, limits and check result, and enforce read-only tools', async t => {
|
|
const f = harness(t, (round, { tools }) => {
|
|
if (round === 0) return answer('尚未交付。');
|
|
if (round === 1) {
|
|
assert.ok(tools.every(tool => !['run_command', 'propose_file', 'run_checks'].includes(tool.function.name)));
|
|
return calls(call('bad-diagnostic-write', 'propose_file', { path: 'unwanted.txt', content: 'wrong', reason: '诊断不应写入' }), call('inspect-error', 'inspect_session', {}));
|
|
}
|
|
return answer('所属任务的检查失败,原任务仍需修复。');
|
|
});
|
|
f.runtime.start(f.session.id, '修复工程并通过测试'); await f.wait();
|
|
const task = f.store.core.task.get(f.session.taskId), taskId = task.id;
|
|
task.verification = { status: 'failed', checkJobId: null }; f.session.status = 'paused'; f.store.save(f.session);
|
|
const instructions = structuredClone(task.instructions);
|
|
f.runtime.start(f.session.id, '前面的错误是怎么回事?'); await f.wait();
|
|
assert.equal(f.session.taskId, taskId); assert.equal(task.status, 'paused'); assert.equal(f.session.status, 'paused');
|
|
assert.deepEqual(task.instructions, instructions); assert.equal(task.verification.status, 'failed');
|
|
const run = f.store.core.run.get(f.session.runId);
|
|
assert.equal(run.purpose, 'diagnostic'); assert.equal(run.status, 'completed'); assert.equal(run.stopReason, 'diagnostic_answered');
|
|
assert.equal(f.session.changes.length, 0); assert.ok(!fs.existsSync(path.join(f.projectRoot, 'unwanted.txt')));
|
|
const denied = JSON.parse(f.session.providerMessages.find(message => message.tool_call_id === 'bad-diagnostic-write').content);
|
|
assert.equal(denied.status, 'error'); assert.match(denied.message, /只读/);
|
|
f.runtime.start(f.session.id, '继续完成'); await f.wait();
|
|
assert.equal(f.session.taskId, taskId); assert.equal(f.session.status, 'paused');
|
|
assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'verification_incomplete');
|
|
});
|
|
|
|
test('review continuation is a system action and adds no testing demand or fake user instruction', async t => {
|
|
let continued;
|
|
const f = harness(t, (round, { messages }) => {
|
|
if (round === 0) return calls(call('doc', 'propose_file', { path: 'result.txt', content: 'done', reason: 'requested' }));
|
|
if (round === 2) continued = messages;
|
|
return answer('改动已生成。');
|
|
});
|
|
f.runtime.start(f.session.id, '创建 result.txt'); await f.wait();
|
|
applyChange(f.store, f.session, f.session.changes[0].id);
|
|
const task = f.store.core.task.get(f.session.taskId), users = f.session.messages.filter(message => message.role === 'user').length;
|
|
f.runtime.continueReviewed(f.session.id); await f.wait();
|
|
assert.equal(task.instructions.length, 0);
|
|
assert.equal(f.session.messages.filter(message => message.role === 'user').length, users);
|
|
assert.equal(f.store.core.run.get(f.session.runId).source, 'review-action');
|
|
assert.match(JSON.stringify(continued), /创建 result.txt/);
|
|
assert.doesNotMatch(JSON.stringify(continued), /识别并实际执行工程检查/);
|
|
});
|
|
|
|
test('a provider cannot execute tests against an explicit no-test task even if it ignores advertised tools', async t => {
|
|
const f = harness(t, round => round === 0 ? calls(call('blocked-check', 'plan_checks', {}), call('blocked-command', 'run_command', { command: 'npm test', reason: 'ignored constraint' })) : answer('按照原要求不测试。'));
|
|
f.runtime.start(f.session.id, '创建 HTML,无需测试'); await f.wait();
|
|
assert.equal(f.session.approvals.length, 0); assert.equal(f.store.core.check_plan.size, 0);
|
|
for (const id of ['blocked-check', 'blocked-command']) assert.match(JSON.parse(f.session.providerMessages.find(message => message.tool_call_id === id).content).message, /无需测试/);
|
|
assertToolPairs(f.session.providerMessages);
|
|
});
|