8 lines
3.3 KiB
JavaScript
8 lines
3.3 KiB
JavaScript
import fs from'node:fs';import assert from'node:assert/strict';import{parsePage,installDom}from'./purchasing-workspace-dom.mjs';import{session}from'./sgf-single-http.mjs';
|
|
const nativeFetch=globalThis.fetch,s=await session('sgf:finance_prepare','commercial');let r=await s.get('/commercial/documents?kind=credit_note',{headers:{Accept:'text/html'}});assert.equal(r.status,200);const document=parsePage(await r.text());installDom(document);document.getElementById=id=>document.querySelector('#'+id);globalThis.window={addEventListener(){}};const $=id=>document.getElementById(id),calls=[];let posts=0,confirms=0;
|
|
globalThis.fetch=async(url,options={})=>{if(url.startsWith('http:'))return nativeFetch(url,options);calls.push({url,body:options.body});if(url.endsWith('/confirm')){confirms++;if(confirms===1)throw Error('Simulated lost response');return{status:200,json:async()=>({replayed:true,id:999999,version:3,state:'prepared'})};}if(options.method==='POST'){posts++;const q=await s.command(url,JSON.parse(options.body),options.headers['Idempotency-Key']);return{status:q.status,json:async()=>q.value};}const q=await s.get(url);return{status:q.status,json:()=>q.json()};};
|
|
await import('../public/sgf-service-documents.mjs?dom-http');const wait=async f=>{const end=Date.now()+12000;while(!f()&&Date.now()<end)await new Promise(r=>setTimeout(r,25));assert(f(),$('message').textContent);};await wait(()=>$('records').children.length===1);assert.equal($('action').value,'');const buttons=$('records').children[0].querySelectorAll('button');buttons[0].onclick();assert($('action').children.some(o=>o.value==='cancel_prepare'));$('action').value='cancel_prepare';await $('action').onchange();$('reason').value='Prévia de anulação QA com impacto conferido, sem confirmação real.';await $('operation').onsubmit({preventDefault(){}});assert.equal(posts,1);assert.equal($('confirm').hidden,false);assert($('fields').disabled&&$('filters').disabled);assert.match($('impact').textContent,/500/);assert.match($('impact').textContent,/600/);assert.match($('impact').textContent,/Factura original preservada: Sim/);assert.match($('impact').textContent,/Devolução de dinheiro: Não/);
|
|
await $('confirm').onclick();assert.equal($('retry').hidden,false);assert($('fields').disabled);await $('retry').onclick();assert.equal(confirms,2);const sent=calls.filter(c=>c.url.endsWith('/confirm'));assert.equal(sent[0].body,sent[1].body);assert.equal($('fields').disabled,false);assert.match($('receipt').textContent,/Recuperada sem duplicação/);
|
|
buttons[1].onclick();await wait(()=>calls.some(c=>c.url.includes('document_key=')));await wait(()=>$('records').children.length===3);assert.equal($('records').children.length,3);r=await s.get('/api/service-documents?kind=credit_note&per_page=25');const current=await r.json();assert.equal(current.documents.data[0].output.cancellations.length,0);
|
|
const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),file=run.root+'/private/receipt-composition-cc50a276c10c/service-documents-dom-http-20261110.json';fs.writeFileSync(file,JSON.stringify({passed:true,checks:19,real_preview_posts:1,real_confirmation_posts:0,confirmation_transport_mocked:true,history_versions:3,selected_cancellations_committed:0,browser_visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:19,proof:file}));
|