Files
stack/packages/cli/tests/request-lifecycle.test.mjs

129 lines
6.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
const fixtureModule = await import('./request-lifecycle-fixture.mjs').catch(e => {
if (e.code === 'ERR_MODULE_NOT_FOUND' && e.message.includes('request-lifecycle-fixture.mjs')) return {};
throw e;
});
async function fixture(t, options) {
assert.equal(typeof fixtureModule.lifecycleFixture, 'function', 'Task 8 needs the process-backed acceptance fixture');
return fixtureModule.lifecycleFixture(t, options);
}
const delivered = evidence => {
assert.equal(evidence.state.phase, 'complete');
assert.ok(evidence.state.delivery_evidence);
assert.equal(evidence.tasks.length, 1);
assert.equal(evidence.deliveries.length, 1);
assert.equal(evidence.projects.length, 1);
assert.equal(evidence.creates, 1);
assert.equal(evidence.userRelays, 0);
assert.equal(evidence.userShellCommands, 0);
assert.ok(evidence.tasks[0].at > evidence.activation.at);
assert.notEqual(evidence.coder.run, evidence.reviewer.run);
assert.notEqual(evidence.coder.workspace, evidence.reviewer.workspace);
assert.ok(evidence.manifests.coder.includes('push_working'));
assert.ok(evidence.manifests.reviewer.includes('fetch_review'));
assert.ok(!evidence.manifests.pm.includes('launch'));
assert.equal(evidence.source.text, evidence.intent);
assert.match(evidence.coderTests, /pass 1/);
assert.match(evidence.reviewerTests, /pass 1/);
assert.match(evidence.download, /25:00/);
assert.doesNotMatch(evidence.download, /https?:\/\/|<script[^>]+src=|<link[^>]+href=/i);
assert.equal(createHash('sha256').update(evidence.download).digest('hex'), evidence.state.delivery_evidence.artifact.sha256);
assert.ok(evidence.notification, 'the chief and assistant completed the existing originating-session route');
};
test('autonomous_original_request_delivers_offline_html', { timeout: 120000 }, async t => {
const f = await fixture(t);
await f.start();
assert.equal((await f.evidence()).creates, 0);
await f.select('autonomous');
const e = await f.evidence(); delivered(e);
const original = JSON.parse(readFileSync(new URL('./fixtures/pomodoro-request.json', import.meta.url)));
assert.equal(e.source.text, original.text);
assert.equal(createHash('sha256').update(original.text).digest('hex'), original.source_sha256);
assert.equal(original.provenance.raw_runtime_event_exported, false);
assert.deepEqual(e.provenance, original.provenance);
assert.deepEqual(e.original_quote, original);
assert.equal(e.ownerSelections, 1);
});
test('guided_answers_and_exact_approval', { timeout: 120000 }, async t => {
const f = await fixture(t); await f.start(); await f.select('guided');
assert.equal((await f.evidence()).state.wait.kind, 'question');
await f.answer('Use a visual and audible phase-end cue after Start.');
const first = (await f.evidence()).state.candidate;
assert.equal((await f.evidence()).creates, 0);
await f.approve('no');
const revised = (await f.evidence()).state.candidate;
assert.notEqual(revised.records_commit, first.records_commit);
await f.approve('yes');
const e = await f.evidence(); delivered(e);
assert.equal(e.snapshot.records_commit, revised.records_commit);
assert.equal(e.snapshot.approval.via, 'console');
});
for (const revise of [false, true]) test(`attach_existing_and_revise_missing_requirement (${revise ? 'revision' : 'matching snapshot'})`, { timeout: 180000 }, async t => {
const f = await fixture(t); await f.start(); await f.select('autonomous');
const original = await f.evidence();
await f.attach({ revise });
const e = await f.evidence(); delivered(e);
assert.deepEqual(e.projects, original.projects);
assert.equal(e.state.project, original.state.project);
assert.equal(e.allCreates, 2);
assert.equal(e.snapshot.id === original.snapshot.id, !revise);
if (revise) assert.notEqual(e.snapshot.records_commit, original.snapshot.records_commit);
});
test('cancellation during PRD approval preserves the product and never creates a task', { timeout: 120000 }, async t => {
const f = await fixture(t); await f.start(); await f.select('guided'); await f.answer('Offline browser.');
await f.cancel();
const e = await f.evidence();
assert.equal(e.state.phase, 'cancelled');
assert.equal(e.creates, 0);
assert.equal(e.deliveries.length, 0);
assert.equal(e.projects.length, 1);
await f.restart(); assert.equal((await f.evidence()).state.phase, 'cancelled');
});
test('mode change during approval invalidates the old candidate before continuing', { timeout: 120000 }, async t => {
const f = await fixture(t); await f.start(); await f.select('guided'); await f.answer('Offline browser.');
const old = (await f.evidence()).state.wait.subject;
await f.mode('autonomous');
const e = await f.evidence(); delivered(e);
assert.notEqual(e.snapshot.approval.decision, old);
assert.equal(e.snapshot.mode_revision, 2);
});
for (const point of ['selection-resolution', 'mapping', 'intent', 'prd-resolution', 'tracker-post', 'working-push', 'review-verdict', 'export-before-notification']) {
test(`crash recovery after ${point}`, { timeout: 180000 }, async t => {
const f = await fixture(t); await f.start();
await f.kill(point);
const before = await f.evidence();
await f.restart();
const after = await f.evidence(); delivered(after);
for (const table of ['events','request_events','request_effects','request_snapshots','request_snapshot_events']) {
for (const row of before.history[table]) assert.deepEqual(after.history[table].find(next => next.seq === row.seq && next.id === row.id), row, `${table} historical row`);
}
if (before.state.snapshot) assert.equal(after.state.snapshot, before.state.snapshot);
if (before.state.delivery) assert.equal(after.state.delivery, before.state.delivery);
if (before.commit) assert.equal(after.commit, before.commit);
assert.equal(after.request, before.request);
});
}
test('an unprovable tracker create stays visibly uncertain without a duplicate or delivery claim', { timeout: 180000 }, async t => {
const f = await fixture(t); await f.start(); await f.kill('tracker-post');
f.hideTrackerOutcome();
await f.restart();
let e = await f.evidence();
assert.equal(e.allCreates, 1);
assert.notEqual(e.state.phase, 'complete');
assert.equal(e.deliveries.length, 0);
assert.ok(e.uncertain.length);
f.showTrackerOutcome(); await f.continue();
e = await f.evidence(); delivered(e);
});