Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

fix(windows): reuse healthy Electron CDP page and tolerate temporary DevTools restart

davoodmehraban89 committed Oct 17, 2026 at 02:22 UTC 310329b0b49955711de679b066025dfec314c53a
1 file changed +40 -16
qa/windows/electron-native-gui-smoke.mjs
+40 -16
@@ -44,25 +44,49 @@ try{
44
45 const origin='http://127.0.0.1:3210';
46 const go=async(path,heading)=>{
47 - // A full Next.js navigation may replace the renderer's CDP target and
48 - // invalidate pending Page.navigate replies. Reattach by URL after navigation.
47 report.stage='native-route:'+path;
48 const dest=origin+path;
51 - const pending=conn.command('Page.navigate',{url:dest}).catch(()=>null);
52 - await Promise.race([pending,sleep(850)]);
53 - const socket=await wait(async()=>{
54 - const response=await fetch('http://127.0.0.1:'+port+'/json/list',{signal:AbortSignal.timeout(1500)});
55 - if(!response.ok)return null;
56 - const pages=await response.json();
57 - return pages.find(p=>p.type==='page'&&p.url?.startsWith(dest)&&p.webSocketDebuggerUrl)?.webSocketDebuggerUrl;
58 - },35000);
59 - try{conn.ws.close()}catch{}
60 - conn=await connect(socket);
61 - report.cdpReconnections++;
49 + const pageInfo="({path:location.pathname,text:document.body?.innerText||'',errorToast:!!document.querySelector('.toast-error')})";
50 + const isReady=x=>x?.path===path&&x.text.includes(heading)&&!x.errorToast&&!x.text.includes('خطا در بارگذاری صفحه');
51 + // Electron often starts on /login already: never force a full renderer reload
52 + // if the current live renderer is showing the right, hydrated page.
53 + const existing=await conn.evaluate(pageInfo).catch(()=>null);
54 + if(isReady(existing))return existing;
55 + const navigation=conn.command('Page.navigate',{url:dest}).catch(()=>null);
56 + await Promise.race([navigation,sleep(850)]);
57 + let reconnects=0;
58 return wait(async()=>{
63 - const x=await conn.evaluate("({path:location.pathname,text:document.body?.innerText||'',errorToast:!!document.querySelector('.toast-error')})");
64 - return x?.path===path&&x.text.includes(heading)&&!x.errorToast&&!x.text.includes('خطا در بارگذاری صفحه')?x:null;
65 - },30000);
59 + if(proc.exitCode!==null)throw Error('INSTALLED_ELECTRON_EXITED_'+proc.exitCode);
60 + const current=await conn.evaluate(pageInfo).catch(()=>null);
61 + if(isReady(current))return current;
62 + // During a full Next page navigation Chromium can temporarily close
63 + // its DevTools HTTP listener. Retry discovery; don't mark a real page
64 + // as broken solely because /json/list briefly refused a connection.
65 + let pages=[];
66 + try{
67 + const response=await fetch('http://127.0.0.1:'+port+'/json/list',{signal:AbortSignal.timeout(1800)});
68 + if(response.ok)pages=await response.json();
69 + }catch{
70 + report.cdpTransientDiscoveryFailures=(report.cdpTransientDiscoveryFailures||0)+1;
71 + return null;
72 + }
73 + const target=pages.find(p=>p.type==='page'&&p.url?.startsWith(dest)&&p.webSocketDebuggerUrl);
74 + if(!target||reconnects>=5)return null;
75 + reconnects++;
76 + let candidate;
77 + try{
78 + candidate=await connect(target.webSocketDebuggerUrl);
79 + const data=await candidate.evaluate(pageInfo);
80 + if(!isReady(data)){candidate.ws.close();return null;}
81 + try{conn.ws.close()}catch{}
82 + conn=candidate;
83 + report.cdpReconnections++;
84 + return data;
85 + }catch{
86 + try{candidate?.ws.close()}catch{}
87 + return null;
88 + }
89 + },60000);
90 };
91 const iban=createSyntheticIranianIban;
92 const submit=async (page,marker,fields)=>{