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 fixture required');const root=path.join(state.root,'private/ledger-'+tag),demo=read(path.join(root,'cost-demo.json')),out=path.join(root,'composition-demo-http.json');if(fs.existsSync(out))throw Error('Completed proof preserved');let checks=0,prepareHtml;const ok=(v,m)=>{checks++;assert(v,m);}; 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 entry=await get('/qa/cost-enter/'+role+'?open=workspace');ok(entry.status===302,'Role opens unified workspace');const auth=await get('/qa/cost-enter/'+role);context=auth.data.context;const w=await get('/api/general-ledger/workspace');ok(w.status===200&&w.data.all_current_source_views&&w.data.book.journals.length===1&&w.data.checks.rows.length===0&&w.data.period.status==='open','Initial composed state not consumed');ok(!w.data.panels.sales.rows[0].confirmed_opening&&w.data.panels.sales.rows[0].original_journal_id===null&&w.data.panels.costs.rows[0].calculation_version===2&&w.data.panels.costs.rows[0].recorded_correction_result_id===null&&w.data.panels.suppliers.rows[0].original_journal_id===null,'Existing sources await individual financial/accounting decisions');const page=await get('/general-ledger/workspace');ok(page.status===200&&(page.text.includes('data-action="period-prepare"')===(role==='prepare')),'Role-specific initial controls');if(role==='prepare')prepareHtml=page.text;for(const url of ['/general-ledger/sales?invoice_id=1','/general-ledger/costs?order_id=1','/general-ledger/flow?source=1','/general-ledger','/']){const r=await get(url);ok(r.status===200,'Existing linked workflow '+url);if(url==='/general-ledger')ok(r.text.includes('/general-ledger/workspace'),'Generic diary links back to composition');}} const script=[...prepareHtml.matchAll(/