Files
Investidor-V2/tools/portal-service-followup-upload-boundary-qa.cjs
T
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

14 lines
3.7 KiB
JavaScript

const {start,assert,DIR,path,fs,sanitized,TARGET}=require('./portal-service-followup-browser-lib.cjs');
const cp=require('node:child_process'),crypto=require('node:crypto'),PHP='C:/laragon/bin/php/php-8.5.11-Win32-vs17-x64/php.exe';
void(async()=>{const q=await start('service-followup-20mib-boundary-qa');let ctx;try{
const a=await q.context('promoter');ctx=a.ctx;const page=a.page;await q.login(page,'promoter');
const imageProof=JSON.parse(fs.readFileSync(path.join(DIR,'service-followup-large-image-web-qa.json'),'utf8'));assert.equal(imageProof.target,TARGET);const id=imageProof.opportunity_id;
const response=await q.open(page,'/minhas-oportunidades/'+id+'/resources'),t=await response.text(),enc=t.match(/data-page="([^"]+)"/),p=enc&&enc[1].startsWith('{')?JSON.parse(enc[1].replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&#039;/g,"'")).props:JSON.parse(t.match(/<script[^>]*type="application\/json"[^>]*>([\s\S]*?)<\/script>/)[1]).props;
const seed=fs.readFileSync(path.join(DIR,'service-followup-large-qa.pdf')),size=20*1048576,bytes=Buffer.concat([seed,Buffer.alloc(size-seed.length,10)]),hash=crypto.createHash('sha256').update(bytes).digest('hex'),key=crypto.randomUUID();
q.proof.opportunity_id=id;q.proof.bytes=size;q.proof.sha256=hash;
const out=await page.evaluate(async({id,etag,base64,key})=>{const f=new FormData();f.set('etag',etag);f.set('creation_key',key);f.set('reason','SIMULAÇÃO QA: testar fronteira exata de 20 MiB no PHP web e worker real, sem efeito externo.');f.set('payload',JSON.stringify({title:'SIMULAÇÃO QA — fronteira 20 MiB',classification:'restricted',visibility:'internal'}));f.set('file',new File([Uint8Array.from(atob(base64),c=>c.charCodeAt(0))],'SIMULACAO-QA-20MiB.pdf',{type:'application/pdf'}));const token=decodeURIComponent(document.cookie.split('; ').find(v=>v.startsWith('XSRF-TOKEN='))?.slice(11)||'');const r=await fetch('/minhas-oportunidades/'+id+'/resources/documents',{method:'POST',headers:{Accept:'application/json','X-XSRF-TOKEN':token},body:f});return {status:r.status,data:await r.json()};},{id,etag:p.etag,base64:bytes.toString('base64'),key});assert.equal(out.status,201);q.proof.resource_id=out.data.id;q.proof.upload_status=out.status;q.save();
const worker=cp.spawnSync(PHP,[path.join(__dirname,'portal-service-followup-worker-qa.php')],{encoding:'utf8',timeout:60000});q.proof.worker_exit=worker.status;if(worker.status!==0){q.proof.worker_diagnostic=sanitized(worker.stderr||worker.stdout);throw Error('Boundary worker failed; reserved job must be recovered, not resubmitted');}
const state=JSON.parse(cp.execFileSync(PHP,[path.join(__dirname,'portal-service-followup-inspect.php'),String(id)],{encoding:'utf8',timeout:60000}));const document=state.rows.find(r=>r.resource_id===out.data.id);assert(document);assert.equal(document.malware_scan_status,'clean');assert(document.plaintext_hash_matches);assert.equal(state.jobs.length,0);q.proof.final_state=state;
const recovered=await page.evaluate(async url=>{const r=await fetch(url),b=await r.arrayBuffer(),h=await crypto.subtle.digest('SHA-256',b);return {status:r.status,hash:[...new Uint8Array(h)].map(v=>v.toString(16).padStart(2,'0')).join('')};},'/minhas-oportunidades/'+id+'/resources/documents/'+out.data.id+'/download');assert.equal(recovered.status,200);assert.equal(recovered.hash,hash);q.proof.checks.exact_20mib_saved_scanned_and_recovered=true;q.proof.result='passed';q.save();console.log(JSON.stringify({result:'passed',bytes:size,checks:q.proof.checks,target:TARGET,active_writes:0}));
}catch(e){q.proof.result='failed';q.proof.failure=sanitized(e);q.save();console.log(JSON.stringify({result:'failed',failure:q.proof.failure}));process.exitCode=1;}finally{if(ctx)await ctx.close();await q.browser.close();}})();