Files
Agent-Workbench/tests/file-results-api.test.mjs

36 lines
3.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, launchWorkbench } from './helpers.mjs';
import { beginPlanTask } from './fixtures/task-plan-fixture.mjs';
import { readText } from '../src/server/files.mjs';
test('current-file HTTP receipts bind Task/Run and bytes, persist on restart and end without altering original runs or user Git data', async t => {
const f = fixture(t); beginPlanTask(f, '创建 README.md 文件,沿用已有内容,无需测试');
const taskId = f.session.taskId, runId = f.session.runId, sha256 = readText(f.project, 'README.md').sha256;
const originalRun = structuredClone(f.store.core.run.get(runId)); f.store.close();
let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
const input = { taskId, runId, files: [{ path: 'README.md', sha256 }], reason: '实际读取现成说明,符合当前文件交付范围。' };
await assert.rejects(host.api(route + '/file-results', { ...input, taskId: crypto.randomUUID() }), /切换/);
await assert.rejects(host.api(route + '/file-results', { ...input, runId: crypto.randomUUID() }), /切换/);
await assert.rejects(host.api(route + '/file-results', { ...input, status: 'completed' }), /核对需要/);
await assert.rejects(host.api(route + '/file-results', { ...input, files: [{ path: '../outside.md', sha256 }] }), /项目内文件/);
const saved = await host.api(route + '/file-results', input);
assert.equal(saved.results.length, 1); assert.equal(saved.results[0].verification, 'current');
const again = await host.api(route + '/file-results', input); assert.equal(again.results.length, 1);
await host.close(); host = await launchWorkbench(t, f.data);
const restored = await host.api(route + '/file-results'); assert.deepEqual(restored.results, saved.results);
fs.appendFileSync(path.join(f.projectRoot, 'README.md'), 'User edit\n');
assert.equal((await host.api(route + '/file-results')).results[0].verification, 'stale');
await assert.rejects(host.api(route + '/file-results/complete', { taskId, runId }), /当前有效成果/);
await assert.rejects(host.api(route + '/file-results', input), /变化/);
const file = await host.api('/projects/' + f.project.id + '/file?path=README.md');
await host.api(route + '/file-results', { ...input, files: [{ path: file.path, sha256: file.sha256 }], reason: '重新核对用户编辑后的完整说明。' });
const completed = await host.api(route + '/file-results/complete', { taskId, runId });
assert.equal(completed.session.status, 'completed'); assert.equal(completed.session.execution.task.delivery.status, 'existing_verified');
assert.deepEqual(completed.session.execution.run, originalRun); assert.equal(completed.session.changes.length, 0);
assert.equal(completed.session.execution.checks.length, 0); assert.equal(completed.session.execution.deliveries.length, 0);
assert.match(fs.readFileSync(path.join(f.projectRoot, 'README.md'), 'utf8'), /User edit/);
});