Files
Investidor-V2/tools/portal-workflow-activation-browser-continue.cjs
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

56 lines
6.8 KiB
JavaScript

// Reuses the REAL existing browser/session; public Playwright APIs, no authentication injection.
// Only the two explicitly designated holders are allowed. No credentials/codes/cookie values recorded.
const fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict');
module.exports=async function continueAuthorized({browser,contextA,pageA,proof,events,inspect,getPlan,matching,actor,gate,write,PRIV,BASE,REV,first}){
assert([2,13].includes(first));const second=first===2?13:2;
const commands=path.join(PRIV,'workflow-activation-browser-continuation-command.json');
let contextB,pageB,current=pageA,last,done=false;proof.holder_order={requester:first,confirmer:second};proof.controller_recovered_without_replacing_authenticated_session=true;write();
async function observation(){
await inspect(current);proof.current_actor_id=await actor(current);
if(contextB&&current===pageB&&proof.current_actor_id===second){
const v=await getPlan(pageB,new URL(proof.confirmation_url,BASE).pathname);
proof.bootstrap_confirmation_preview=Object.fromEntries(['approval_id','requested_by','second_user_id','target','hash','can_confirm','mfa_confirmed_in_session','code_current','expired','consumed'].map(k=>[k,v[k]]));
}
write();
}
await observation();let observed=current.url();
while(!done){
await new Promise(r=>setTimeout(r,1200));
if(current.isClosed()){proof.phase='browser_closed';write();break;}
if(current.url()!==observed){observed=current.url();try{await observation();}catch(e){proof.phase='observation_stopped';proof.observation_error={type:e.name,message:e.message.slice(0,180)};write();}}
if(!fs.existsSync(commands))continue;
const c=JSON.parse(fs.readFileSync(commands));if(c.id===last)continue;last=c.id;assert.equal(c.revision,REV);
try{
if(c.action==='inspect')await observation();
else if(c.action==='profiles'){
gate();assert.equal(await actor(pageA),first);const v=await getPlan(pageA,'/management/profile-install');matching(v,'profiles');assert(!v.installed);assert.equal(v.hash,c.preview_hash);
await pageA.getByLabel('Fundamento',{exact:true}).fill('Instalação exclusiva 040000 após 030000, candidata 41caea0f900f autorizada em 11/10/2026. Preservar COUNTRY02 e manter os fluxos operacionais desabilitados.');
await pageA.getByLabel('Confirmo exclusivamente a ação indicada nesta base',{exact:true}).check();
const response=pageA.waitForResponse(r=>r.request().method()==='POST'&&new URL(r.url()).pathname==='/management/profile-install');
await pageA.getByRole('button',{name:'Instalar schema 040000',exact:true}).click();assert.equal((await response).status(),200);
await pageA.reload({waitUntil:'networkidle'});await observation();assert(proof.profiles_preview.installed);proof.criteria.profiles_authenticated_UI_installed=true;write();
}else if(c.action==='request-bootstrap'){
gate();assert.equal(await actor(pageA),first);const v=await getPlan(pageA,'/management/profile-install');matching(v,'profiles');assert(v.installed&&!v.bootstrap.initialized&&!v.bootstrap.pending);assert.equal(v.hash,c.preview_hash);
await pageA.getByLabel('Fundamento',{exact:true}).fill('Designação explícita das duas contas existentes 2 e 13, de pessoas distintas conforme confirmação do responsável. Pedido pelo titular '+first+' e confirmação independente por '+second+', com sessões e MFA próprios. Candidata 41caea0f900f autorizada; fluxos operacionais OFF.');
await pageA.getByLabel('ID da primeira conta explicitamente designada',{exact:true}).fill(String(first));await pageA.getByLabel('ID da segunda conta explicitamente designada',{exact:true}).fill(String(second));await pageA.getByLabel('Confirmo exclusivamente a ação indicada nesta base',{exact:true}).check();
const response=pageA.waitForResponse(r=>r.request().method()==='POST'&&new URL(r.url()).pathname==='/management/profile-install/bootstrap');
await pageA.getByRole('button',{name:'Pedir confirmação ao segundo titular',exact:true}).click();const res=await response;assert.equal(res.status(),200);const result=await res.json();assert(result.confirmation_url);proof.confirmation_url=result.confirmation_url;proof.criteria.first_holder_requested_without_self_approval=true;
await pageA.goto(BASE+new URL(result.confirmation_url,BASE).pathname,{waitUntil:'networkidle'});const own=await getPlan(pageA,new URL(result.confirmation_url,BASE).pathname);assert.equal(Number(own.requested_by),first);assert.equal(Number(own.second_user_id),second);assert(!own.can_confirm&&!own.consumed);proof.criteria.first_holder_cannot_confirm=true;write();
}else if(c.action==='open-second'){
assert(proof.confirmation_url&&!contextB);contextB=await browser.newContext({locale:'pt-MZ',viewport:{width:1280,height:900},ignoreHTTPSErrors:false});pageB=await contextB.newPage();events(pageB,'second_holder');current=pageB;
await pageB.goto(BASE+new URL(proof.confirmation_url,BASE).pathname,{waitUntil:'networkidle'});await observation();proof.criteria.second_holder_own_browser_session=true;write();
}else if(c.action==='confirm-bootstrap'){
gate();assert.equal(await actor(pageB),second);const url=new URL(proof.confirmation_url,BASE).pathname,v=await getPlan(pageB,url);
assert.equal(v.target.database,'investemoz');assert.equal(Number(v.target.port),5432);assert.equal(Number(v.requested_by),first);assert.equal(Number(v.second_user_id),second);assert(v.can_confirm&&v.mfa_confirmed_in_session&&v.code_current&&!v.expired&&!v.consumed);assert.equal(v.hash,c.preview_hash);
await pageB.getByLabel('Fundamento da confirmação',{exact:true}).fill('Confirmo de forma independente como titular '+second+' em sessão própria com MFA, a dupla explicitamente designada 2 e 13. Candidata 41caea0f900f autorizada. Não atribuir papéis operacionais nem habilitar fluxos.');
await pageB.getByLabel('Confirmo apenas estas duas contas e esta base',{exact:true}).check();
const response=pageB.waitForResponse(r=>r.request().method()==='POST'&&new URL(r.url()).pathname===url+'/confirm');
await pageB.getByRole('button',{name:'Confirmar designação cruzada',exact:true}).click();assert.equal((await response).status(),200);await pageB.getByRole('status').filter({hasText:'Dupla inicializada por confirmação cruzada'}).waitFor();proof.criteria.second_holder_confirmed_with_own_MFA=true;proof.phase='bootstrap_confirmed_operational_flags_OFF';write();
}else if(c.action==='close'){done=true;proof.phase='completed';write();}
else throw new Error('Unsupported bounded action');
proof.last_command={id:c.id,action:c.action,result:'passed'};write();console.log(JSON.stringify(proof.last_command));
}catch(e){proof.last_command={id:c.id,action:c.action,result:'stopped',error_type:e.name,message:e.message.slice(0,180)};write();console.log(JSON.stringify(proof.last_command));}
}
if(done)await browser.close();
};