3 lines
4.6 KiB
JavaScript
3 lines
4.6 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 response=await s.get('/stock/catalogue',{headers:{Accept:'text/html'}});assert.equal(response.status,200);const document=parsePage(await response.text());installDom(document);document.getElementById=id=>document.querySelector('#'+id);globalThis.window={addEventListener(){}};const $=id=>document.getElementById(id),checks=[],calls=[];let previews=0,confirms=0;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 new UI confirmations');}assert(url.endsWith('/preview'));previews++;const r=await s.command(url,JSON.parse(options.body),options.headers['Idempotency-Key']);return{status:r.status,json:async()=>r.value};}const r=await s.get(url);return{status:r.status,json:()=>r.json()};};await import('../public/sgf-stock-catalogue.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(()=>$('records').children.length>1);ok($('selection').textContent.includes('Escolha explicitamente'),'no first article selected');const old=[...$('records').children].find(r=>r.textContent.includes('Artigo anterior'));assert(old);await old.querySelector('button').onclick();ok($('selection').textContent.includes('apenas consulta')&&$('action').children.length===1,'legacy article read-only without association');$('new').onclick();ok($('action').value==='prepare'&&$('code').value===''&&$('unit').value==='','new article no automatic code unit');$('code').value='QA-DOM-CATALOGO';$('name').value='Artigo de prévia sem confirmação';$('unit').value='UN';$('reason').value='Conferência DOM e HTTP do novo cadastro sem gravar outra preparação.';$('operation').oninput({target:$('name')});await $('operation').onsubmit({preventDefault(){}});ok(previews===1&&confirms===0,'one rollback preview zero confirmations');ok($('impact').textContent.includes('Movimentos:0')&&$('impact').textContent.includes('sem alterar o catálogo físico'),'impact states no stock no canonical creation on prepare');ok($('fields').disabled&&$('filters').disabled&&$('discard').disabled,'preview locks source fields and discard');$('cancel').onclick();ok(!$('fields').disabled&&$('name').value.includes('sem confirmação'),'cancel preserves unsent draft');await old.querySelector('button').onclick();ok($('name').value.includes('sem confirmação')&&$('message').textContent.includes('Conclua'),'choosing another article cannot silently discard edits');$('size').value='100';$('apply').onclick();await wait(()=>$('filters').disabled===false&&calls.some(u=>u.includes('per_page=100')));ok($('name').value.includes('sem confirmação')&&$('reason').value.includes('sem gravar'),'pagination preserves unsent name and reason');$('discard').onclick();ok($('action').value===''&&$('reason').value==='','explicit discard clears form');const own=[...$('records').children].find(r=>r.textContent.includes('QA-CATALOGO-INTEGRADO'));assert(own);await own.querySelector('button').onclick();ok($('selection').textContent.includes('versão actual 2'),'chosen owned item loads actual version');$('action').value='prepare';$('action').onchange();ok($('code').readOnly&&$('unit').readOnly&&!$('name').readOnly,'owned item only name editable');const root=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')).root+'/private/receipt-composition-cc50a276c10c',proof=JSON.parse(fs.readFileSync(root+'/stock-catalogue-http-20261110.json'));$('referenceKey').value=proof.operations.prepare.key;await $('recovery').onsubmit({preventDefault(){}});ok($('receipt').textContent.includes('Nenhuma operação repetida'),'own receipt recovered by GET');response=await s.get('/api/stock-catalogue?view=history&record_key='+proof.record_key);ok((await response.json()).records.total===2&&confirms===0,'two accepted versions untouched');response=await s.get('/api/stock-catalogue?view=items&search=QA-DOM-CATALOGO');ok((await response.json()).records.total===0,'DOM preview leaves no article');const file=root+'/stock-catalogue-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}));
|