Files
Agent-Workbench/tests/workspace-recovery.test.mjs

69 lines
5.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { fixture, until, workspace } from './helpers.mjs';
import { createDeliveryProject, deliveryGit } from './fixtures/delivery-project.mjs';
import { captureWorkspaceSource, assertWorkspaceSource } from './fixtures/workspace-project.mjs';
import { WorkspaceManager, verifyWorkspaceProject } from '../src/core/workspaces/git-workspaces.mjs';
import { Store } from '../src/server/store.mjs';
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
for (const phase of ['intent', 'git_started', 'git_finished', 'verifying', 'registering', 'project_registered', 'session_registered', 'ready']) {
test('actual process exit at workspace ' + phase + ' preserves source and reconciles without creation replay', { timeout: 60000 }, async t => {
const f = fixture(t); createDeliveryProject(f);
const original = captureWorkspaceSource(f), manager = new WorkspaceManager(f.store), planned = await manager.prepare(f.session, 'recovery-' + phase);
f.store.close();
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/workspace-crash-worker.mjs'), f.data, f.session.id, planned.id, phase],
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null, 25000); assert.equal(child.exitCode, 73, errors);
const cut = JSON.parse(output.trim()), store = new Store(f.data); f.onCleanup(() => store.close());
const session = store.session(f.session.id), record = store.owned('workspace', planned.id, session.id), adapter = new WorkspaceManager(store, { initialize: false });
f.onCleanup(() => adapter.stopAll());
assert.equal(record.status, phase === 'ready' ? 'ready' : 'unknown');
if (alive(cut.gitPid)) await assert.rejects(adapter.reconcile(session, record.id, record.fingerprint), /进程仍在运行/);
await until(() => !alive(cut.gitPid), 15000);
const beforeRefs = deliveryGit(f, ['for-each-ref', '--format=%(refname) %(objectname)']);
assertWorkspaceSource(assert, f, original, record.branch);
if (phase !== 'ready') await assert.rejects(adapter.start(session, record.id, record.fingerprint), /不能再次执行/);
const reconciled = await adapter.reconcile(session, record.id, record.fingerprint);
if (phase === 'intent') {
assert.equal(reconciled.status, 'unknown'); assert.equal(fs.existsSync(record.root), false);
await adapter.acknowledge(session, record.id, record.fingerprint, record.observation.fingerprint);
assert.equal(record.status, 'resolved_external'); assert.equal(store.projects.length, 1);
} else if (record.observation?.ready || phase === 'ready') {
assert.equal(reconciled.status, 'ready', reconciled.summary); assert.equal(store.projects.length, 2);
await verifyWorkspaceProject(store, store.project(record.childProjectId));
const again = await adapter.reconcile(session, record.id, record.fingerprint);
assert.equal(again.childSessionId, record.childSessionId); assert.equal(store.sessions.size, 2);
} else {
assert.equal(phase, 'git_started', record.summary); assert.equal(record.status, 'unknown');
await adapter.acknowledge(session, record.id, record.fingerprint, record.observation.fingerprint);
assert.equal(record.status, 'resolved_external');
}
assert.equal(deliveryGit(f, ['for-each-ref', '--format=%(refname) %(objectname)']), beforeRefs);
assertWorkspaceSource(assert, f, original, record.branch);
});
}
test('outside changes at project registration remain unknown and preserve the already registered child identity', async t => {
const f = fixture(t); createDeliveryProject(f);
const manager = new WorkspaceManager(f.store, { faultInjector: (phase, record) => {
if (phase === 'project_registered') fs.writeFileSync(path.join(record.root, 'src/add.mjs'), 'Owner changed the child during registration\n');
} });
const record = await manager.prepare(f.session, 'registration-race');
await (await manager.start(f.session, record.id, record.fingerprint)).promise;
assert.equal(record.status, 'unknown'); assert.equal(f.store.project(record.childProjectId).workspace.id, record.id);
const child = f.store.session(record.childSessionId); child.archived = true; f.store.save(child);
const verifier = new WorkspaceManager(f.store);
await verifier.reconcile(f.session, record.id, record.fingerprint);
assert.equal(record.status, 'unknown'); assert.equal(child.archived, true);
assert.equal(fs.readFileSync(path.join(record.root, 'src/add.mjs'), 'utf8'), 'Owner changed the child during registration\n');
await assert.rejects(verifyWorkspaceProject(f.store, f.store.project(record.childProjectId)), /恢复证据/);
await verifier.acknowledge(f.session, record.id, record.fingerprint, record.observation.fingerprint);
assert.equal(record.status, 'resolved_external'); assert.equal(child.archived, true);
});