27 lines
3.6 KiB
JavaScript
27 lines
3.6 KiB
JavaScript
const fs=require('fs'),path=require('path'),vm=require('vm'),assert=require('assert/strict');
|
|
const root=path.resolve(__dirname,'../..'),qa=path.join(root,'storage/app/private/investmoz-v2/portal-audit/portal-stability-source-20261011_083742');
|
|
const ts=require(path.join(root,'node_modules/typescript'));
|
|
const source=fs.readFileSync(process.argv.includes('--baseline')?path.join(__dirname,'line11-recovery-before-20261015/form.tsx'):path.join(qa,'resources/js/pages/land-assistance/form.tsx'),'utf8');
|
|
async function scenario(kind){
|
|
let state=[],cursor=0,effects=[],cleanups=[],deps=[],tree,props={index:0,parcel:{},onChange(){throw Error('Recovery must never mutate parcel IDs')},onRemove(){}},calls=[];
|
|
const react={useState(initial){const i=cursor++;if(!(i in state))state[i]=initial;return[state[i],v=>{state[i]=typeof v==='function'?v(state[i]):v}]},useMemo(f){cursor++;return f()},useEffect(f,d){const i=cursor++;if(!deps[i]||d.some((x,k)=>x!==deps[i][k])){deps[i]=d;effects.push(()=>{cleanups[i]?.();cleanups[i]=f()})}}};
|
|
const jsx=(type,props)=>({type,props});const module={exports:{}};
|
|
const context={module,exports:module.exports,AbortController,URLSearchParams,fetch:async(url)=>{
|
|
const q=new URL('http://qa'+url).searchParams;calls.push(q.get('id'));
|
|
if(kind==='http-error')return{ok:false,status:503};
|
|
const id=q.get('id'),level=['province','district','admin_post','locality'].indexOf(q.get('type'));
|
|
return{ok:true,status:200,json:async()=>({data:[{id,location_type:q.get('type'),parent_id:kind==='wrong-parent'&&level===1?'wrong':q.get('parent_id')||'country',name:q.get('type')}]})};
|
|
},require(n){if(n==='react')return react;if(n==='react/jsx-runtime')return{jsx,jsxs:jsx};if(n==='@/components/investment-territory')return{TerritoryFields:'territory'};return{};}};
|
|
vm.runInNewContext(ts.transpileModule(source+'\nexport { ParcelEditor as TestParcel };',{compilerOptions:{jsx:ts.JsxEmit.ReactJSX,module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText,context);
|
|
const render=()=>{cursor=0;tree=module.exports.TestParcel(props);while(effects.length)effects.shift()()};
|
|
const flush=async()=>{for(let i=0;i<16;i++){await new Promise(setImmediate);render()}};
|
|
render();await flush();assert.equal(calls.length,0);
|
|
props={...props,parcel:{province_id:'p',district_id:'d',admin_post_id:'a',locality_id:'l'}};render();await flush();
|
|
const nodes=[];function walk(v){if(!v||typeof v!=='object')return;if(v.props){nodes.push(v);for(const value of Object.values(v.props))if(Array.isArray(value))value.forEach(walk);else walk(value)}}walk(tree);
|
|
const field=nodes.find(x=>x.type==='territory');assert(field);
|
|
if(kind==='success'){assert.deepEqual(calls,['p','d','a','l']);assert.equal(field.props.initial.map(x=>x.id).join(','),'p,d,a,l');assert.equal(field.props.disabled,false);assert(!nodes.some(x=>x.props.role==='alert'));}
|
|
else{assert.equal(field.props.disabled,true);const alert=nodes.find(x=>x.props.role==='alert');assert(alert);const message=JSON.stringify(alert);assert(message.includes(kind==='http-error'?'HTTP 503':'Distrito'));assert(message.includes('identificadores guardados'));assert(nodes.some(x=>x.type==='button'&&x.props.children==='Tentar recuperar novamente'));}
|
|
return{scenario:kind,passed:true,requests:calls.length};
|
|
}
|
|
(async()=>{const result=[];for(const kind of ['success','http-error','wrong-parent'])result.push(await scenario(kind));fs.writeFileSync(path.join(__dirname,'line11-recovery-regression-20261015.json'),JSON.stringify({isolated_hook_harness:true,real_QA_component:true,no_browser_or_database_mutation:true,cases:result},null,2)+'\n');console.log(JSON.stringify(result))})().catch(e=>{console.error(e);process.exitCode=1});
|