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 { isDiagnosticQuestion, taskContract } from '../src/shared/contracts/task-control.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 } });
test('operational questions are diagnostic while explicit repair requests remain execution instructions', () => {
for (const text of ['前面的错误是怎么回事?', '这个报错是什么意思', '解释下这个错误', '请解释刚才执行的错误', '你使用的是什么模型?', 'Why did your fix fail?']) assert.equal(isDiagnosticQuestion(text), true, text);
for (const text of ['先修复系统', '为什么失败,请帮我修复', '这个错误怎么回事,然后修一下', 'fix this error', '继续完成']) assert.equal(isDiagnosticQuestion(text), false, text);
});
test('an explicit prohibition on running engineering checks is not interpreted as a requirement to run them', () => {
for (const goal of ['只读核对指定文件范围,不修改文件,不运行工程检查。', '不执行工程检查,只解释原结果',
'不需要做任何测试', '无需工程检查', 'Read the evidence; do not run checks.', "Don't run tests; explain the original failure."]) {
assert.equal(taskContract({goal}).checks, 'skip', goal);
}
for (const goal of ['修复并运行工程检查', '运行并通过原测试', 'Fix the source and run checks.']) assert.equal(taskContract({goal}).checks, 'required', goal);
});
test('file delivery requires a positive instruction, not saved evidence or a prohibited write', () => {
for (const goal of ['读取保存的原工件,分段取得真实依据,不执行命令或修改文件。',
'读取保存过的 HTML 文件并解释内容', '不要创建文件,只分析结果', '读取工件,不执行命令或创建文件',
'Do not create a file; explain the evidence.', 'Read the saved file without creating HTML.',
'不需要做任何修改 HTML 文件,只核对结果', '解释如何修改 HTML 文件', 'Explain how to modify an HTML file.']) {
assert.equal(taskContract({ goal }).kind, 'general', goal);
}
for (const goal of ['创建一个 HTML 文件,无需测试', '新建 result.txt', '把结果保存为 report.md 文件',
'请写入 SVG 文件', 'Create an HTML file without running tests.', '不要执行命令,创建一个 HTML 文件',
'文件不存在时创建 report.md 文件', '不要修改原有文件而是新建 HTML 文件']) {
assert.equal(taskContract({ goal }).kind, 'file', goal);
}
});
test('an unsubstantiated text claim cannot complete a requested file delivery', async t => {
const f = harness(t, () => answer('HTML 已创建完成。'));
f.runtime.start(f.session.id, '创建一个 HTML 文件,无需测试'); await f.wait();
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'delivery_incomplete');
assert.equal(f.session.changes.length, 0); assert.equal(f.session.approvals.length, 0);
});
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: '', 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'), /