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

9 lines
4.5 KiB
JavaScript

// Actual raw client and actual HTTP, with confirmation POSTs prohibited.
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','accounting');const r=await s.get('/fiscal/rules',{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 previews=0,confirms=0;const checks=[],calls=[];const ok=(v,n)=>{assert(v,n);checks.push(n);};
globalThis.fetch=async(url,options={})=>{if(url.startsWith('http:'))return nativeFetch(url,options);calls.push(url);if(options.method==='POST'){if(url.endsWith('/confirm')){confirms++;throw Error('No rule confirmations in UI proof');}assert(url.endsWith('/preview'));previews++;const a=await s.command(url,JSON.parse(options.body),options.headers['Idempotency-Key']);return{status:a.status,json:async()=>a.value};}const a=await s.get(url);return{status:a.status,json:()=>a.json()};};
await import('../public/sgf-fiscal-rules.mjs?dom-http');const wait=async f=>{const end=Date.now()+15000;while(!f()&&Date.now()<end)await new Promise(r=>setTimeout(r,25));assert(f(),$('message').textContent);};await wait(()=>$('events').children.length===2);ok($('selection').textContent.includes('Nenhuma versão'),'no first rule automatically selected');ok($('events').textContent.includes('2026-12-01')&&$('events').textContent.includes('2026-11-30'),'validity bounds visible');
await $('events').children[0].querySelector('button').onclick();ok($('selection').textContent.includes('QA-RATE-TRANSVERSAL'),'explicit version choice');$('action').value='calculate';$('action').onchange();$('base').value='1000';$('on').value='2026-12-01';$('currency').value='MZN';$('reason').value='Conferência UI QA por versão exacta e data, sem confirmação real.';$('operation').oninput();await $('operation').onsubmit({preventDefault(){}});ok(previews===1&&confirms===0,'real rollback calculation preview only');ok($('impact').textContent.includes('170.00')&&$('impact').textContent.includes('1170.00'),'calculation impact explicit before commit');ok($('fields').disabled&&$('filters').disabled,'preview locks both form and catalogue');$('cancel').onclick();ok(!$('fields').disabled&&$('base').value==='1000','cancel keeps unsubmitted base');
$('size').value='100';$('apply').onclick();await wait(()=>$('filters').disabled===false&&calls.some(c=>c.includes('per_page=100')));ok($('base').value==='1000'&&$('reason').value.includes('sem confirmação'),'pagination keeps unsent calculation and reason');$('view').value='history';$('filterKey').value='QA-RATE-TRANSVERSAL';$('apply').onclick();await wait(()=>$('events').children.length===4);ok($('page').textContent.includes('4 registos'),'full four-event rule history visible');$('view').value='calculations';$('apply').onclick();await wait(()=>$('events').children.length===2&&$('events').textContent.includes('Cálculo'));await $('events').children[0].querySelector('button').onclick();ok($('detail').textContent.includes('170.00')&&$('detail').textContent.includes('source_sha256'),'historical calculation shows frozen result and source');ok($('inputs').textContent.includes('Sem regra escolhida'),'choosing a calculation does not adopt its rule');
const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),root=run.root+'/private/receipt-composition-cc50a276c10c',p=JSON.parse(fs.readFileSync(root+'/fiscal-rules-http-20261110.json'));$('referenceKey').value=p.operations['calculate-november'].key;await $('recovery').onsubmit({preventDefault(){}});ok($('receipt').textContent.includes('Nenhuma operação repetida'),'historical own receipt recovered by GET');const after=await s.get('/api/fiscal-rules?view=calculations');ok((await after.json()).records.total===2&&confirms===0,'two saved calculations untouched');const iva=await s.get('/api/fiscal-assessments');const old=await iva.json();ok(iva.status===200&&old.form_current_review&&old.events.total===4,'existing reviewed IVA book/map still current and four versions');const file=root+'/fiscal-rules-dom-http-20261110.json';fs.writeFileSync(file,JSON.stringify({passed:true,checks,preview_posts:previews,confirmation_posts:confirms,real_http_forwarded:true,browser_visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length,proof:file}));