import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; const reports=[]; for (const [host,port,page] of [['compras',8005,'/obrigacoes-fornecedores'],['sgf',8006,'/supplier-payables']]) { const cookies=new Map(); async function request(path,method='GET',body) { const r=await fetch(`http://127.0.0.1:${port}${path}`, {method,redirect:'manual',headers:{Cookie:[...cookies].map(([k,v])=>`${k}=${v}`).join('; '),...(body?{'Content-Type':'application/x-www-form-urlencoded'}:{})},...(body?{body:new URLSearchParams(body)}:{})}); for(const c of r.headers.getSetCookie()) {const pair=c.split(';',1)[0],i=pair.indexOf('=');cookies.set(pair.slice(0,i),pair.slice(i+1));} return {status:r.status,text:await r.text(),location:r.headers.get('location')}; } const token=text=>{const m=text.match(/name="_token" value="([^"]+)"/);assert(m,'CSRF token present');return m[1];}; const entry=await request('/demo');assert.equal(entry.status,200);assert(!entry.text.includes('type="password"')); assert.equal((await request('/demo/entrar','POST',{role:'consulta'})).status,419); assert.equal((await request('/demo/entrar','POST',{role:'admin',_token:token(entry.text)})).status,302); let baseline; for(const role of ['preparacao','validacao','consulta']) { const landing=await request('/demo'); const login=await request('/demo/entrar','POST',{role,_token:token(landing.text)}); assert.equal(login.status,302,host+' '+role+' '+login.text.slice(0,150));assert(login.location.endsWith(page)); const ui=await request(page);assert.equal(ui.status,200,host+' authorised page');assert(ui.text.includes('Trocar função ou sair da demonstração')); assert.equal(ui.text.includes('data-action="prepare"'),role==='preparacao'); if(role==='consulta')assert(!ui.text.includes('