import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {mountSupplierDocuments} from '../public/supplier-documents.mjs'; const tag=process.argv[2];assert(/^[a-f0-9]{12}$/.test(tag??''));const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),root=path.join(run.root,'private/receipt-composition-'+tag),out=path.join(root,'receipt-documents-ui.json');assert(!fs.existsSync(out)); class Element{constructor(tag,text){this.tag=tag;this.textContent=text??'';this.children=[];this.dataset={};this.isConnected=true;}append(...v){this.children.push(...v);}replaceChildren(...v){this.children=[...v];}} globalThis.document={createElement:tag=>new Element(tag)};globalThis.Option=class extends Element{constructor(text,value){super('option',text);this.value=value;}}; const checks=[],check=(v,n)=>{assert(v,n);checks.push(n);},walk=e=>[e,...e.children.flatMap(walk)],calls=[];let defer=null; globalThis.fetch=async(url,options)=>{calls.push({url,options});if(options.method==='GET')return {ok:true,json:async()=>({data:[],current_page:1,last_page:1})};if(defer)return await new Promise(resolve=>{defer.resolve=resolve;});return {ok:true,json:async()=>url.endsWith('/preview')?{preview:'sealed',impact:{document:{original_name:'qa.png',mime_type:'image/png',size:3,sha256:'abc'},reused:true,reason:'individual reason'}}:{document_id:1}};}; const rootNode=new Element('root');rootNode.dataset={context:'qa-context',csrf:'qa-csrf'};const mount=(receipt,canUpload=true)=>{const detail=new Element('detail');let reloaded=0;mountSupplierDocuments({id:1,revision:'a'.repeat(64),canUpload,root:rootNode,detail,active:()=>true,reload:async()=>{reloaded++;},message:()=>{},receipt,...(receipt?{basePath:'/api/purchasing-receipts/history/1/documents'}:{})});return {detail,reloaded:()=>reloaded};};const tick=()=>new Promise(resolve=>setTimeout(resolve,0)); const supplier=mount(false);await tick();check(walk(supplier.detail).filter(v=>v.tag==='option').map(v=>v.value).join(',')===',license,tax,conflict,other','shared component preserves original supplier type choices');const receipt=mount(true);await tick();const elements=walk(receipt.detail),form=elements.find(v=>v.tag==='form'),type=elements.find(v=>v.tag==='select'),file=elements.find(v=>v.type==='file'),reason=elements.find(v=>v.tag==='textarea'),confirm=elements.find(v=>v.tag==='button'&&v.textContent==='Confirmar este anexo');check(type.children.map(v=>v.value).join(',')===',delivery_note,quality,other','receipt uses native types and an explicit initially empty choice');check(calls.every(v=>v.options.method==='GET')&&confirm.disabled,'mounting lists evidence without preparing or confirming an upload'); type.value='delivery_note';file.files=[new File(['abc'],'qa.png',{type:'image/png'})];reason.value='individual reason';await form.onsubmit({preventDefault(){}});check(!confirm.disabled&&calls.at(-1).url==='/api/purchasing-receipts/history/1/documents/preview'&&calls.at(-1).options.body.get('revision')==='a'.repeat(64),'explicit receipt preview uses owner endpoint context and original revision');check(elements.some(v=>v.tag==='p'&&v.textContent.includes('quantidades'))||walk(receipt.detail).some(v=>v.textContent.includes('Quantidades')),'receipt impact explains preserved quantities and stock');await confirm.onclick();check(receipt.reloaded()===1&&calls.at(-1).options.body.get('preview')==='sealed','only explicit confirmation sends the preview and reloads receipt history');form.oninput();check(confirm.disabled,'editing invalidates the existing conference'); defer={};const pending=form.onsubmit({preventDefault(){}});await tick();form.oninput();defer.resolve({ok:true,json:async()=>({preview:'stale',impact:{}})});await pending;check(confirm.disabled,'late preview cannot restore confirmation after an input edit');defer=null;const readOnly=mount(true,false);await tick();check(!walk(readOnly.detail).some(v=>v.tag==='form'),'read-only view lists documents without creating an upload form');fs.writeFileSync(out,JSON.stringify({passed:true,checks,dom_simulation:true,visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length}));