import fs from 'node:fs';import path from 'node:path';import assert from 'node:assert/strict';import vm from 'node:vm';import crypto from 'node:crypto'; const read=p=>JSON.parse(fs.readFileSync(p,'utf8')),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),tag=process.argv[2];if(state.port!==55439||!/^[a-f0-9]{12}$/.test(tag??''))throw Error('Owned correction team fixture required');const root=path.join(state.root,'private/ledger-'+tag),out=path.join(root,'cost-correction-demo-http.json'),demo=read(path.join(root,'cost-demo.json')),g=read(path.join(root,'cost-fixture.json'));if(fs.existsSync(out))throw Error('Completed readiness preserved');let checks=0;const ok=(v,m)=>{checks++;assert(v,m);};let prepareHtml; for(const role of ['prepare','validate','read']){const jar=new Map();let context='';const get=async(url)=>{const r=await fetch(new URL(url,demo.url),{headers:{Accept:'application/json','X-SGF-Context':context,Cookie:[...jar].map(([k,v])=>k+'='+v).join('; ')},redirect:'manual',signal:AbortSignal.timeout(15000)});for(const c of r.headers.getSetCookie()){const p=c.split(';')[0],i=p.indexOf('=');jar.set(p.slice(0,i),p.slice(i+1));}const text=await r.text();let data;try{data=JSON.parse(text);}catch{}return{status:r.status,text,data};};const guide=await get('/qa/cost-demo');ok(guide.status===200&&guide.text.includes('275→295'),'Actual correction guide');const auth=await get('/qa/cost-enter/'+role);ok(auth.status===200,'Declared role');context=auth.data.context;const data=await get('/api/general-ledger/costs?order_id='+g.order_id);ok(data.status===200&&data.data.cost.source.principal_minor===29500&&data.data.cost.original_journal.debit_minor===27500&&data.data.corrections.length===0&&data.data.book.journals.length===1&&data.data.period_closures.length===0,'Original275/current295, no correction or close consumed');const page=await get('/general-ledger/costs?order_id='+g.order_id);ok(page.status===200&&page.text.includes('20,00 MZN'),'Explicit delta');ok(role==='prepare'?page.text.includes('data-action="correct-prepare"'):!page.text.includes('data-action='),'Initial role-specific controls');if(role==='prepare')prepareHtml=page.text;const home=await get('/');ok(home.status===200&&/\/build\/assets\//.test(home.text),'Private existing application build');} const script=[...prepareHtml.matchAll(/