import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import assert from 'node:assert/strict';import {Element,walk,parsePage,installDom} from './purchasing-workspace-dom.mjs';import {continuationHref,appendStageLinks} from '../public/purchasing-focus.mjs'; const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),root=path.join(run.root,'private/receipt-composition-cc50a276c10c'),flow=JSON.parse(fs.readFileSync(path.join(root,'completion-flow-result.json'))),out=path.join(root,'completion-handoffs-result.json'),ids=flow.source_ids,base='http://127.0.0.1:8085';assert(flow.passed&&!fs.existsSync(out));const checks=[],ok=(v,name)=>{assert(v,name);checks.push(name);},cookies=new Map,original=globalThis.fetch;let pending=0; async function raw(url,options={}){const h=new Headers(options.headers);h.set('Cookie',[...cookies].map(([k,v])=>k+'='+v).join('; '));const r=await original(base+url,{...options,headers:h});for(const cookie of r.headers.getSetCookie()){const p=cookie.split(';')[0],i=p.indexOf('=');cookies.set(p.slice(0,i),p.slice(i+1));}return r;} globalThis.fetch=async(url,options={})=>{assert((options.method??'GET')==='GET');pending++;try{return await raw(url,options);}finally{pending--;}}; const settle=async()=>{for(let i=0;i<1200;i++){await new Promise(r=>setTimeout(r,25));if(!pending){await new Promise(r=>setTimeout(r,30));if(!pending)return;}}throw Error('Handoff read did not settle');}; try{ for(const [role,stage,file,query,destination]of [['requester','requests','request-workspace','?request='+ids.request,'/purchasing/sourcing?request='+ids.request],['validator','receipts','receipt-workspace','?order='+ids.order+'&receipt='+ids.receipt,'/purchasing/invoices?order='+ids.order]]){ const entry=await raw('/qa/enter/'+role),v=await entry.json();assert.equal(entry.status,200);const response=await raw('/purchasing/'+stage+query,{headers:{Accept:'text/html','X-SGF-Context':v.context}}),html=await response.text();assert.equal(response.status,200);const doc=parsePage(html);installDom(doc);globalThis.location={search:query};await import('../public/'+file+'.mjs?handoff='+stage);await settle(); const link=walk(doc.root).find(e=>e.tag==='a'&&e.href?.startsWith('/compras?')&&new URL('http://qa'+e.href).searchParams.get('return_to')===destination);ok(Boolean(link),'C09 '+role+' continua para a próxima etapa por escolha explícita da função, sem403 nem copiar IDs'); } // Preserve the original presentation contract outside this one installation. globalThis.document={createElement:tag=>new Element(tag),querySelector:()=>null};const box=new Element('section');appendStageLinks({dataset:{continuityEnabled:'true'}},box,[{href:'/purchasing/invoices?order=1',label:'Facturas'}]);ok(continuationHref('sourcing',{request:1})===null&&box.children.length===0,'C09 instalações anteriores sem a entrada única conservam os links restritos pelo menu'); const sources=['public/purchasing-focus.mjs','public/request-workspace.mjs'].map(file=>({file,sha256:crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex')}));fs.writeFileSync(out,JSON.stringify({passed:true,checks,sources,business_commands:0,same_installation:true,dom_simulation:true,visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length})); }finally{globalThis.fetch=original;}