Files
Agent-Workbench/tests/workers-merge.test.mjs

77 lines
5.9 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 } from './helpers.mjs';
import { completedWorkers, ownerBytes } from './fixtures/workers-project.mjs';
import { deliveryGit, writeDeliveryFile } from './fixtures/delivery-project.mjs';
test('verified isolated outputs form a new parent batch, rerun real checks and commit only reviewed source', async t => {
const f = fixture(t), { runtime, manager, group, original } = await completedWorkers(f);
assert.equal(group.status, 'ready_to_collect', group.summary);
await (await manager.collect(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'collected', group.summary);
const collection = await manager.readCollection(f.session, group); assert.equal(collection.conflicts.length, 0);
assert.deepEqual(collection.paths, ['src/a.mjs', 'src/b.mjs']);
await (await manager.prepareMerge(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'merge_prepared', group.summary); assert.deepEqual(ownerBytes(f), original);
const set = f.store.core.change_set.get(group.merge.changeSetId);
assert.equal(set.taskId, group.taskId); assert.equal(set.files.length, 2);
for (const file of set.files) {
const proposed = f.session.changes.find(change => change.id === file.changeId), child = group.children.find(child => child.id === proposed.sourceBinding.workerId);
assert.equal(proposed.taskId, group.taskId); assert.equal(proposed.sourceBinding.collaborationId, group.id);
assert.equal(f.store.session(child.sessionId).changes.some(change => change.id === proposed.id), false);
assert.equal(fs.existsSync(path.join(f.projectRoot, file.path)), false);
}
await assert.rejects(manager.finalize(f.session, group.id, group.fingerprint), /待处理|审查|应用/);
await (await runtime.changeSetManager().start(f.session, set.id, set.fingerprint)).promise;
assert.equal(set.status, 'completed', set.summary); assert.deepEqual(ownerBytes(f), original);
await assert.rejects(manager.finalize(f.session, group.id, group.fingerprint), /真实通过检查/);
const plan = await runtime.checkRunner().plan(f.session), check = await (await runtime.checkRunner().start(f.session, plan.id)).promise;
assert.equal(check.status, 'passed', check.summary);
await assert.rejects(manager.finalize(f.session, group.id, group.fingerprint), /Git交付/);
const gitPlan = await runtime.gitDelivery().plan(f.session, { message: 'Reviewed isolated worker integration' });
await (await runtime.gitDelivery().start(f.session, gitPlan.id)).promise;
const result = await manager.finalize(f.session, group.id, group.fingerprint);
assert.equal(result.status, 'completed'); assert.ok(result.delivery.commit);
assert.deepEqual(ownerBytes(f).files, original.files);
assert.equal(deliveryGit(f, ['show', ':owner-staged.txt']), 'Owner staged change: keep this exact staged content\n');
assert.equal(deliveryGit(f, ['diff', '--cached', '--name-only']).trim(), 'owner-staged.txt');
});
test('overlapping worker paths are explicit conflicts even if each independent task checks successfully', async t => {
const f = fixture(t), { manager, group } = await completedWorkers(f, { paths: ['src/shared.mjs', 'src/shared.mjs'] });
assert.equal(group.status, 'ready_to_collect', group.summary);
await (await manager.collect(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'conflict', group.summary); assert.ok(group.collection.conflicts.some(value => value.path === 'src/shared.mjs' && value.workerIds.length === 2));
assert.throws(() => manager.prepareMerge(f.session, group.id, group.fingerprint), /没有冲突/);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'src/shared.mjs')), false);
});
test('source edits after isolated work preserve the owner file and invalidate collection without proposing a merge', async t => {
const f = fixture(t), { manager, group } = await completedWorkers(f);
writeDeliveryFile(f, 'owner-unstaged.txt', 'New owner work after worker execution\n');
await (await manager.collect(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'stale'); assert.match(group.summary, /来源|已有修改/);
assert.equal(group.merge, null); assert.equal(f.session.changes.length, 0);
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'owner-unstaged.txt'), 'utf8'), 'New owner work after worker execution\n');
});
test('changed child bytes after parent preview block the entire batch before any parent write', async t => {
const f = fixture(t), { runtime, manager, group } = await completedWorkers(f);
await (await manager.collect(f.session, group.id, group.fingerprint)).promise;
await (await manager.prepareMerge(f.session, group.id, group.fingerprint)).promise;
const set = f.store.core.change_set.get(group.merge.changeSetId), child = group.children[1];
fs.writeFileSync(path.join(f.store.project(child.projectId).root, 'src/b.mjs'), 'export const value = 99;\n');
await assert.rejects(runtime.changeSetManager().start(f.session, set.id, set.fingerprint), /原提案|已应用|变化|不一致/);
assert.equal(fs.existsSync(path.join(f.projectRoot, 'src/a.mjs')), false); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src/b.mjs')), false);
});
test('corrupt original check logs cannot become collected outputs or a parent review', async t => {
const f = fixture(t), { manager, group } = await completedWorkers(f), worker = f.store.core.worker.get(group.children[0].id);
const check = f.store.core.check_job.get(worker.result.checkJobId), artifact = f.store.core.artifact.get(check.results[0].logArtifactId);
fs.appendFileSync(path.join(f.store.artifactStore.root, artifact.relative), 'altered original log');
await (await manager.collect(f.session, group.id, group.fingerprint)).promise;
assert.equal(group.status, 'stale'); assert.equal(group.merge, null); assert.equal(f.session.changes.length, 0);
});