Files
SGF/scripts/sgf-stock-completion-dom-http.mjs
2026-11-11 09:00:17 +02:00

9 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('purchasing:validator','stock');let r=await s.get('/stock/completion',{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);let posts=0,confirms=0;const calls=[];
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,record_key:'QA-TEST-RECOVERY',version:1,record_id:999999})};}if(options.method==='POST'){posts++;const q=await s.command(url,JSON.parse(options.body));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-stock-completion.mjs?dom-http');const wait=async fn=>{const end=Date.now()+12000;while(!fn()&&Date.now()<end)await new Promise(r=>setTimeout(r,25));assert(fn(),$('message').textContent);};await wait(()=>$('action').children.length>1);assert.equal($('action').value,'');assert.match($('summary').textContent,/Saldo físico: 8/);assert($('records').children.length>0);
$('action').value='save';$('action').onchange();$('f-basis').value='Política QA de prova DOM com parâmetros explicitamente conferidos.';$('reason').value='Prévia DOM→HTTP sem gravar nova política ou movimento.';await $('operation').onsubmit({preventDefault(){}});assert.equal(posts,1);assert.equal($('confirm').hidden,false);assert($('filters').disabled&&$('fields').disabled);assert.match($('impact').textContent,/Não criado/);
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,/Recibo recuperado/);
const buttons=$('records').children[0].querySelectorAll('button');assert.equal(buttons.length,2);buttons[1].onclick();await wait(()=>calls.some(c=>c.url.includes('record_key=')));await wait(()=>$('records').children.length===2);assert.equal($('records').children.length,2);const state=await s.get('/api/stock-completion?family=supplier_return&per_page=25'),v=await state.json();r=await s.get('/api/stock-completion?'+new URLSearchParams({family:'supplier_return',record_key:v.records.data[0].record_key,per_page:25}));const h=await r.json();assert.equal(r.status,200);assert.deepEqual(h.records.data.map(r=>r.version),[3,2,1]);
const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),file=run.root+'/private/receipt-composition-cc50a276c10c/stock-completion-dom-http-20261110.json';fs.writeFileSync(file,JSON.stringify({passed:true,checks:17,real_preview_posts:1,real_confirmation_posts:0,confirmation_transport_mocked:true,history_versions:[3,2,1],browser_visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:17,proof:file}));