128 lines
10 KiB
JavaScript
128 lines
10 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 { checkedDelivery, deliveryGit as git, writeDeliveryFile as write } from './fixtures/delivery-project.mjs';
|
|
import { Store } from '../src/server/store.mjs';
|
|
import { GitDelivery } from '../src/core/git-delivery.mjs';
|
|
import { inspectProject } from '../src/core/project-inspector.mjs';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { proposeFile, applyChange } from '../src/server/files.mjs';
|
|
|
|
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
|
|
|
|
for (const phase of ['queued', 'preparing', 'index_locked', 'prepared', 'ref_prepared', 'publishing', 'committed', 'index_published', 'finished']) {
|
|
test('actual process exit at Git ' + phase + ' never replays a commit and can reconcile the durable result', { timeout: 60000 }, async t => {
|
|
const f = fixture(t), { delivery } = await checkedDelivery(f), plan = await delivery.plan(f.session);
|
|
const parent = git(f, ['rev-parse', 'HEAD']), stage = git(f, ['show', ':owner-staged.txt']), source = (await inspectProject(f.project)).sourceFingerprint;
|
|
const owner = fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt'));
|
|
f.store.close();
|
|
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/delivery-crash-worker.mjs'), f.data, f.session.id, plan.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()), recovered = new Store(f.data); f.onCleanup(() => recovered.close());
|
|
const session = recovered.session(f.session.id), job = recovered.owned('delivery_job', cut.jobId, session.id), adapter = new GitDelivery(recovered);
|
|
f.onCleanup(() => adapter.stopAll());
|
|
const published = ['committed', 'index_published', 'finished'].includes(phase);
|
|
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
|
|
if (!published) assert.equal(git(f, ['rev-parse', 'HEAD']), parent);
|
|
if (['queued', 'preparing'].includes(phase)) assert.ok(['interrupted', 'unknown'].includes(job.status));
|
|
else if (phase !== 'finished') assert.equal(job.status, 'unknown');
|
|
else assert.equal(job.status, 'completed');
|
|
await new Promise(resolve => setTimeout(resolve, 80)); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), published ? '2' : '1');
|
|
await assert.rejects(adapter.start(session, plan.id), { status: 409 });
|
|
if (job.refProcessPid) await until(() => !alive(job.refProcessPid), 10000);
|
|
const reconciled = await adapter.reconcile(session, job.id);
|
|
assert.equal(reconciled.status, published ? 'completed' : 'interrupted', reconciled.summary); assert.equal(reconciled.requiresVerification, false);
|
|
if (published) assert.equal(reconciled.commit, cut.expectedCommit);
|
|
assert.equal(fs.existsSync(path.join(f.projectRoot, '.git/index.lock')), false);
|
|
assert.equal(git(f, ['show', ':owner-staged.txt']), stage); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'owner-staged.txt')), owner);
|
|
assert.equal((await inspectProject(f.project)).sourceFingerprint, source);
|
|
const report = await adapter.report(session); assert.equal(report.report.status, published ? 'verified_committed' : 'verified_uncommitted');
|
|
if (!published) {
|
|
const fresh = await adapter.plan(session), final = await (await adapter.start(session, fresh.id)).promise;
|
|
assert.equal(final.status, 'completed', final.summary); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
|
|
}
|
|
});
|
|
}
|
|
|
|
test('an unknown Git result cannot overwrite an externally changed index or delete an unrelated lock', async t => {
|
|
const f = fixture(t); await checkedDelivery(f);
|
|
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'committed') throw new Error('Synthetic lost result after actual publication'); } });
|
|
const plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
|
|
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.ok(job.expectedCommit);
|
|
const index = path.join(f.projectRoot, '.git/index'), lock = index + '.lock';
|
|
const original = fs.readFileSync(index); fs.appendFileSync(index, 'Owner changed index');
|
|
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/);
|
|
assert.equal(fs.existsSync(lock), true); assert.ok(fs.readFileSync(index).includes(Buffer.from('Owner changed index')));
|
|
fs.writeFileSync(index, original); const ours = fs.readFileSync(lock); fs.writeFileSync(lock, 'Other operation owns this lock');
|
|
await assert.rejects(adapter.reconcile(f.session, job.id), /索引或遗留锁/); assert.equal(fs.readFileSync(lock, 'utf8'), 'Other operation owns this lock');
|
|
fs.writeFileSync(lock, ours);
|
|
const result = await adapter.reconcile(f.session, job.id); assert.equal(result.status, 'completed');
|
|
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
|
|
});
|
|
|
|
test('known publication with a later source edit retains the commit and marks the delivery for inspection', async t => {
|
|
const f = fixture(t); await checkedDelivery(f);
|
|
let changed = false;
|
|
const adapter = new GitDelivery(f.store, { faultInjector: phase => { if (phase === 'index_published' && !changed) { changed = true; write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b + 0;\n'); } } });
|
|
const job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
|
|
assert.equal(job.status, 'unknown'); assert.equal(job.indexPreserved, true); assert.equal(job.workspacePreserved, false);
|
|
assert.equal(git(f, ['rev-parse', 'HEAD']).trim(), job.expectedCommit); assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
|
|
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\n');
|
|
const runtime = new Runtime(f.store, new Provider());
|
|
assert.throws(() => runtime.start(f.session.id, 'Start another task'), /结果未知/);
|
|
assert.equal((await adapter.report(f.session)).report.status, 'needs_verification');
|
|
write(f, 'src/math.mjs', 'export const sum = (a, b) => a + b;\n');
|
|
assert.equal((await adapter.reconcile(f.session, job.id)).status, 'completed');
|
|
});
|
|
|
|
test('CRLF normalization, multiple applied revisions and unrelated special index flags preserve exact bytes and ownership', async t => {
|
|
const f = fixture(t); await checkedDelivery(f);
|
|
git(f, ['config', 'core.autocrlf', 'true']);
|
|
git(f, ['update-index', '--assume-unchanged', '--', 'owner-unstaged.txt']);
|
|
const beforeFlags = git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']);
|
|
const latest = proposeFile(f.store, f.session, { path: 'src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\r\n', reason: 'Reviewed CRLF revision' });
|
|
applyChange(f.store, f.session, latest.change_id);
|
|
// This later applied revision intentionally invalidates old checks.
|
|
const { CheckRunner } = await import('../src/core/check-runner.mjs');
|
|
const checks = new CheckRunner(f.store); assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
|
|
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session), job = await (await adapter.start(f.session, plan.id)).promise;
|
|
assert.equal(job.status, 'completed', job.summary); assert.equal(git(f, ['ls-files', '-v', '--', 'owner-unstaged.txt']), beforeFlags);
|
|
assert.equal(fs.readFileSync(path.join(f.projectRoot, 'src/math.mjs'), 'utf8'), 'export const sum = (a, b) => a + b + 0;\r\n');
|
|
assert.deepEqual(plan.files.find(file => file.path === 'src/math.mjs').changeIds, f.session.changes.filter(change => change.path === 'src/math.mjs').map(change => change.id));
|
|
});
|
|
|
|
test('adding new durable record kinds to an existing hot Store preserves all existing Maps and records', async t => {
|
|
const f = fixture(t); await checkedDelivery(f);
|
|
const oldCore = { ...f.store.core }; delete oldCore.delivery_plan; delete oldCore.delivery_job;
|
|
f.store.core = oldCore; const tasks = f.store.core.task, sessions = f.store.sessions;
|
|
f.store.ensureStorage();
|
|
assert.notEqual(f.store.core, oldCore); assert.equal(f.store.core.task, tasks); assert.equal(f.store.sessions, sessions);
|
|
assert.ok(f.store.core.delivery_plan instanceof Map); assert.ok(f.store.core.delivery_job instanceof Map);
|
|
const adapter = new GitDelivery(f.store), plan = await adapter.plan(f.session);
|
|
assert.equal((await (await adapter.start(f.session, plan.id)).promise).status, 'completed');
|
|
});
|
|
|
|
test('an unrelated owner deletion is fingerprinted and preserved while linked task paths are rejected', async t => {
|
|
const f = fixture(t); await checkedDelivery(f);
|
|
fs.unlinkSync(path.join(f.projectRoot, 'owner-unstaged.txt'));
|
|
const { CheckRunner } = await import('../src/core/check-runner.mjs'), checks = new CheckRunner(f.store);
|
|
const inspection = await inspectProject(f.project); assert.equal(inspection.scope.complete, true);
|
|
assert.equal((await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise).status, 'passed');
|
|
const adapter = new GitDelivery(f.store), job = await (await adapter.start(f.session, (await adapter.plan(f.session)).id)).promise;
|
|
assert.equal(job.status, 'completed', job.summary); assert.equal(fs.existsSync(path.join(f.projectRoot, 'owner-unstaged.txt')), false);
|
|
assert.match(git(f, ['status', '--porcelain=v1']), / D owner-unstaged.txt/);
|
|
fs.symlinkSync(path.join(f.projectRoot, 'src'), path.join(f.projectRoot, 'linked-src'), process.platform === 'win32' ? 'junction' : 'dir');
|
|
const proposal = proposeFile(f.store, f.session, { path: 'linked-src/math.mjs', content: 'export const sum = (a, b) => a + b + 0;\n', reason: 'Linked path requires an explicit canonical review' });
|
|
applyChange(f.store, f.session, proposal.change_id);
|
|
await (await checks.start(f.session, (await checks.plan(f.session)).id)).promise;
|
|
await assert.rejects(adapter.plan(f.session), /经过链接/);
|
|
assert.equal(git(f, ['rev-list', '--count', 'HEAD']).trim(), '2');
|
|
});
|