Files
jiapuapp/tests/r-business-flow-runtime-smoke.js
T
2026-07-21 07:53:08 +08:00

10 lines
4.7 KiB
JavaScript

const sleep=(ms)=>new Promise(r=>setTimeout(r,ms));const origin=process.argv[2]||"http://localhost:5173";
const connect=async()=>{const pages=await(await fetch("http://127.0.0.1:9222/json/list")).json();const page=pages.find(x=>x.type==="page"&&x.url.startsWith(`${origin}/`));if(!page)throw new Error("No project Chrome page");const socket=new WebSocket(page.webSocketDebuggerUrl);await new Promise((r,j)=>{socket.addEventListener("open",r,{once:true});socket.addEventListener("error",j,{once:true})});let id=0;const pending=new Map();socket.addEventListener("message",e=>{const m=JSON.parse(e.data),p=pending.get(m.id);if(!p)return;pending.delete(m.id);m.error?p.reject(new Error(m.error.message)):p.resolve(m.result)});const send=(method,params={})=>new Promise((resolve,reject)=>{id+=1;pending.set(id,{resolve,reject});socket.send(JSON.stringify({id,method,params}))});return{socket,send};};
const val=async(s,e)=>(await s("Runtime.evaluate",{expression:e,returnByValue:true})).result?.value;const wait=async(s,e,m)=>{for(let i=0;i<60;i++){if(await val(s,e))return;await sleep(100)}throw new Error(m)};let n=0;const open=async(s,route,selector)=>{n+=1;const url=`${origin}/?rBusiness=${n}#${route}`;await s("Page.navigate",{url});await wait(s,`location.href===${JSON.stringify(url)}`,`navigation failed ${route}`);await wait(s,`Boolean(document.querySelector(${JSON.stringify(selector)}))`,`missing ${selector} ${route}`);};const click=(s,q)=>val(s,`document.querySelector(${JSON.stringify(q)}).click()`);const inputAt=(s,q,index,v)=>val(s,`(()=>{const e=document.querySelectorAll(${JSON.stringify(q)})[${index}];e.value=${JSON.stringify(v)};e.dispatchEvent(new Event('input',{bubbles:true}));return e.value})()`);
const routes=[["/pages/records/r03-gift-list?count=50",".record-card"],["/pages/records/r04-gift-editor?mode=create",".form-card"],["/pages/records/r05-ritual-list?count=50",".record-card"],["/pages/records/r06-ritual-detail?ritualId=501",".detail-card"],["/pages/records/r07-ritual-editor?mode=create",".form-card"],["/pages/records/r08-growth-journal",".timeline-card"],["/pages/records/r09-life-events",".timeline-card"],["/pages/records/r10-memo-list",".memo-card"],["/pages/records/r11-merit-records",".merit-card"]];
const run=async()=>{const{socket,send}=await connect();try{await send("Page.enable");await send("Runtime.enable");for(const size of[{width:320,height:568},{width:412,height:915}]){await send("Emulation.setDeviceMetricsOverride",{...size,deviceScaleFactor:1,mobile:true,screenWidth:size.width,screenHeight:size.height});for(const[route,selector]of routes){await open(send,route,selector);if(await val(send,`document.documentElement.scrollWidth>${size.width+1}`))throw new Error(`${route} overflows at ${size.width}`);}await open(send,"/pages/records/r08-growth-journal",".timeline-card");await val(send,"(()=>{const p=document.querySelector('.page-content'),c=document.querySelector('.timeline-card');for(let i=0;i<48;i++)p.insertBefore(c.cloneNode(true),p.lastElementChild);p.querySelectorAll('.timeline-card')[49].scrollIntoView();return p.querySelectorAll('.timeline-card').length})()");if((await val(send,"document.querySelectorAll('.timeline-card').length"))!==50)throw new Error("R08 capacity stress failed");}
await open(send,"/pages/records/r03-gift-list",".record-card");await click(send,".record-card");await wait(send,"location.hash.includes('/pages/records/r04-gift-editor?mode=view&giftId=301')","R03 did not open R04");
await open(send,"/pages/records/r05-ritual-list",".record-card");await click(send,".record-card");await wait(send,"location.hash.includes('/pages/records/r06-ritual-detail?ritualId=501')","R05 did not open R06");
await open(send,"/pages/records/r08-growth-journal",".header-action");await click(send,".header-action");await wait(send,"Boolean(document.querySelector('.app-dialog-layer'))","R08 dialog missing");await inputAt(send,".dialog-form input",0,"第一次远行");await inputAt(send,".dialog-form input",1,"2026 年 7 月");await click(send,".app-dialog__actions .app-button:last-child");await wait(send,"document.querySelector('.timeline-card').innerText.includes('第一次远行')","R08 save failed");
await open(send,"/pages/records/r11-merit-records",".header-action");await click(send,".header-action");await inputAt(send,".dialog-form input",0,"整理旧谱");await inputAt(send,".dialog-form input",1,"汤文清");await click(send,".app-dialog__actions .app-button:last-child");await wait(send,"document.querySelector('.merit-summary').innerText.includes('3 次')","R11 total did not update");process.stdout.write("R-BUSINESS-FLOW-RUNTIME-SMOKE PASS\n");}finally{try{await send("Emulation.clearDeviceMetricsOverride")}catch(_){}socket.close();}};run().catch(e=>{process.stderr.write(`${e.stack||e.message}\n`);process.exit(1)});