import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import {randomUUID} from 'node:crypto'; import {fixture} from './helpers.mjs'; import {createDeliveryProject,applyDeliveryFix,deliveryGit} from './fixtures/delivery-project.mjs'; import {CheckRunner} from '../src/core/check-runner.mjs'; import {taskDeliveryProgress} from '../src/core/context/delivery-progress.mjs'; test('reviewed application invalidates old checks, names current SHA and advances through a new approved check to Git preview',async t=>{ const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll()); const snapshot=()=>taskDeliveryProgress(f.store,f.session,run,{checks}); assert.equal(await snapshot(),null);const before=deliveryGit(f,['rev-parse','HEAD']); const original=await checks.plan(f.session);await(await checks.start(f.session,original.id)).promise; applyDeliveryFix(f);let progress=await snapshot();assert.equal(progress.stage,'needs_current_check_plan'); assert.equal(progress.latest_check.stale,true);assert.ok(progress.files.every(file=>file.current&&file.current_sha256===file.applied_sha256)); assert.deepEqual(progress.next_action,{tool:'plan_checks',arguments:{names:['test','build']}}); const plan=await checks.plan(f.session);progress=await snapshot();assert.equal(progress.stage,'ready_for_checks'); assert.deepEqual(progress.next_action,{tool:'run_checks',arguments:{plan_id:plan.id}});assert.equal(f.store.core.check_job.size,1,'snapshot never executes a check'); const passed=await(await checks.start(f.session,plan.id)).promise;assert.equal(passed.status,'passed'); progress=await snapshot();assert.equal(progress.stage,'ready_for_git_preview');assert.equal(progress.latest_check.current_source,true); assert.equal(progress.next_action.tool,'plan_delivery');assert.equal(deliveryGit(f,['rev-parse','HEAD']),before,'snapshot cannot publish a commit'); fs.appendFileSync(path.join(f.projectRoot,'src/math.mjs'),'// owner changed\n');progress=await snapshot(); assert.equal(progress.stage,'applied_files_need_review');assert.equal(progress.next_action,null);assert.equal(progress.files.find(file=>file.path==='src/math.mjs').current,false); }); test('skip-tests, foreign task, unknown operations, diagnostic and parent review retain their boundaries',async t=>{ const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll());applyDeliveryFix(f); const task=f.store.core.task.get(f.session.taskId);task.contract={...task.contract,checks:'skip'}; const snapshot=options=>taskDeliveryProgress(f.store,f.session,run,{checks,...options}); assert.equal((await snapshot()).stage,'checks_not_requested');assert.equal((await snapshot()).next_action.tool,'delivery_report'); assert.equal((await snapshot({worker:true})).next_action,null);assert.equal(f.store.core.check_plan.size,0); assert.equal(await taskDeliveryProgress(f.store,f.session,{...run,purpose:'diagnostic'},{checks}),null); f.session.changes[0].taskId='foreign-task';assert.equal((await snapshot()).files.length,1); const id='owned-unknown';f.store.core.tool.set(id,{id,sessionId:f.session.id,projectId:f.project.id,taskId:task.id,requiresVerification:true}); assert.equal((await snapshot()).stage,'awaiting_review_or_verification');assert.equal((await snapshot()).next_action,null); }); test('before the first change, real baseline failures persist across later reads; passed baselines and changed sources cannot certify delivery',async t=>{ const f=fixture(t),run=createDeliveryProject(f),checks=new CheckRunner(f.store);f.onCleanup(()=>checks.stopAll()); const snapshot=()=>taskDeliveryProgress(f.store,f.session,run,{checks}); assert.equal(await snapshot(),null); const plan=await checks.plan(f.session),job=await(await checks.start(f.session,plan.id)).promise; assert.equal(job.status,'failed'); let current=await snapshot();assert.equal(current.has_applied_changes,false);assert.equal(current.stage,'checks_need_repair'); assert.equal(current.latest_check.id,job.id);assert.equal(current.latest_check.current_source,true); assert.ok(current.latest_check.results.every(result=>result.log_artifact_id&&result.log_sha256)); const later=randomUUID();f.store.core.tool.set(later,{schemaVersion:1,id:later,taskId:run.taskId,runId:run.id,sessionId:f.session.id,projectId:f.project.id,tool:'read_file',state:'completed'}); assert.deepEqual(await snapshot(),current); fs.writeFileSync(path.join(f.projectRoot,'src/add.mjs'),'export const add = (a,b) => a+b;\n'); current=await snapshot();assert.equal(current.latest_check.current_source,false);assert.notEqual(current.stage,'ready_for_git_preview'); // An actual current passed baseline still has no reviewed implementation. const updated=await checks.plan(f.session),passed=await(await checks.start(f.session,updated.id)).promise; assert.equal(passed.status,'passed');current=await snapshot();assert.equal(current.stage,'baseline_passed_without_changes');assert.equal(current.next_action,null); const artifact=f.store.core.artifact.get(passed.results[0].logArtifactId),log=path.join(f.data,'artifacts',artifact.relative); fs.writeFileSync(log,'corrupted external evidence');current=await snapshot(); assert.equal(current.latest_check.current_source,false);assert.equal(current.stage,'requires_current_evidence_verification');assert.equal(current.next_action,null); f.store.core.tool.set('unknown',{id:'unknown',taskId:run.taskId,sessionId:f.session.id,projectId:f.project.id,requiresVerification:true}); assert.equal((await snapshot()).stage,'awaiting_review_or_verification'); });