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

Add real installed Windows Electron CDP native login, five operational routes, screenshot and bank-disabled regression

davoodmehraban89 committed Oct 26, 2026 at 08:14 UTC 77c6ddac8dc849f40096db76b08fbf4eef9756d6
1 file changed +132
qa/windows/v7-installed-native-cdp-smoke.mjs new
+132
@@ -0,0 +1,132 @@
1 +import assert from 'node:assert/strict';
2 +import {spawn,execFileSync} from 'node:child_process';
3 +import {existsSync,mkdirSync,writeFileSync} from 'node:fs';
4 +import {join} from 'node:path';
5 +const [exe,fixtureDir]=process.argv.slice(2);
6 +assert.equal(process.platform,'win32');
7 +assert(exe&&existsSync(exe)&&fixtureDir,'EXACT_INSTALLED_EXE_AND_ISOLATED_DIR_REQUIRED');
8 +mkdirSync(fixtureDir,{recursive:true});
9 +const root='http://127.0.0.1:3210',debug='http://127.0.0.1:9345';
10 +const sleep=ms=>new Promise(ok=>setTimeout(ok,ms));
11 +const report={status:'NOT_RUN',cdpConnected:false,bankDisabled:false,
12 + pages:[],actualLocalClick:false,stage:'BOOT',reconnections:0};
13 +let app,conn;
14 +async function targets(){
15 + const response=await fetch(debug+'/json/list',{signal:AbortSignal.timeout(2000)});
16 + assert(response.ok,'CDP_TARGET_LIST_UNAVAILABLE');
17 + return response.json();
18 +}
19 +async function connect(target){
20 + const ws=new WebSocket(target);
21 + await new Promise((ok,fail)=>{
22 + const timer=setTimeout(()=>fail(Error('CDP_CONNECT_TIMEOUT')),10000);
23 + ws.addEventListener('open',()=>{clearTimeout(timer);ok()},{once:true});
24 + ws.addEventListener('error',()=>{clearTimeout(timer);fail(Error('CDP_CONNECT_ERROR')),{once:true}});
25 + });
26 + let id=0;const pending=new Map();
27 + ws.addEventListener('message',message=>{
28 + let v;try{v=JSON.parse(message.data)}catch{return}
29 + const p=pending.get(v.id);if(!p)return;
30 + pending.delete(v.id);clearTimeout(p.timer);
31 + v.error?p.fail(Error('CDP_REMOTE_ERROR')):p.ok(v.result);
32 + });
33 + const send=(method,params={})=>new Promise((ok,fail)=>{
34 + const n=++id,timer=setTimeout(()=>{pending.delete(n);fail(Error('CDP_COMMAND_TIMEOUT'))},12000);
35 + pending.set(n,{ok,fail,timer});
36 + ws.send(JSON.stringify({id:n,method,params}));
37 + });
38 + const evaluate=async expression=>{
39 + const reply=await send('Runtime.evaluate',{expression,awaitPromise:true,returnByValue:true});
40 + if(reply.exceptionDetails)throw Error('CDP_SCRIPT_ERROR');
41 + return reply.result?.value;
42 + };
43 + await send('Runtime.enable');await send('Page.enable');
44 + return {ws,send,evaluate,uri:target};
45 +}
46 +async function until(fn,timeoutMs=65000){
47 + const stop=Date.now()+timeoutMs;let latest;
48 + while(Date.now()<stop){
49 + if(app?.exitCode!==null&&app?.exitCode!==undefined)throw Error('ELECTRON_EXITED');
50 + try{const outcome=await fn();if(outcome)return outcome}catch(e){latest=e}
51 + await sleep(350);
52 + }
53 + throw latest||Error('CDP_NATIVE_ROUTE_TIMEOUT');
54 +}
55 +async function attach(expectedPath){
56 + const listing=await targets();
57 + const matched=listing.find(p=>p.type==='page'&&p.url?.startsWith(root)&&p.webSocketDebuggerUrl);
58 + if(!matched)return null;
59 + if(!conn||conn.uri!==matched.webSocketDebuggerUrl){
60 + const fresh=await connect(matched.webSocketDebuggerUrl);
61 + try{conn?.ws.close()}catch{}
62 + conn=fresh;report.reconnections++;
63 + }
64 + const result=await conn.evaluate("({route:location.pathname,text:document.body?.innerText||'',anchors:[...document.querySelectorAll('a')].map(a=>a.getAttribute('href'))})");
65 + return result?.route===expectedPath?result:null;
66 +}
67 +async function go(path,heading){
68 + report.stage='ROUTE_'+path.replace(/[^a-z]/gi,'_');
69 + const initial=await attach(path).catch(()=>null);
70 + if(!initial){
71 + if(conn)await conn.send('Page.navigate',{url:root+path}).catch(()=>null);
72 + else {
73 + await until(async()=>{const targetsNow=await targets();return targetsNow.some(p=>p.type==='page'&&p.url?.startsWith(root))});
74 + const existing=await attach('/login').catch(()=>null);
75 + if(!existing){const listing=await targets();const t=listing.find(p=>p.type==='page'&&p.webSocketDebuggerUrl);if(t)conn=await connect(t.webSocketDebuggerUrl)}
76 + await conn?.send('Page.navigate',{url:root+path}).catch(()=>null);
77 + }
78 + }
79 + return until(async()=>{
80 + const p=await attach(path);
81 + return p&&p.text.includes(heading)?p:null;
82 + },65000);
83 +}
84 +try{
85 + const env={...process.env,TREASURYFLOW_DESKTOP_LOCAL:'1',BANK_ADAPTER_MODE:'disabled',
86 + TREASURYFLOW_ENABLE_MAYAN_EXTERNAL_READ:'0',TREASURYFLOW_MAYAN_ENABLE_EXTERNAL_READ:'0',
87 + TREASURYFLOW_ENABLE_SQL_LAB:'0',
88 + TREASURYFLOW_DATA_FILE:join(fixtureDir,'synthetic-v7-ui.sqlite')};
89 + const prior=await fetch(root+'/api/ready',{signal:AbortSignal.timeout(900)}).then(()=>true,()=>false);
90 + assert(!prior,'STALE_LOCAL_SERVICE_BEFORE_TEST');
91 + app=spawn(exe,['--remote-debugging-port=9345','--remote-allow-origins=*'],{env,stdio:'ignore'});
92 + report.stage='LOCAL_READY';
93 + const ready=await until(async()=>{
94 + const r=await fetch(root+'/api/ready',{signal:AbortSignal.timeout(1400)});
95 + if(!r.ok)return null;
96 + const v=await r.json();
97 + return v.status==='ready'&&v.bankAdapter==='disabled'?v:null;
98 + },65000);
99 + assert.equal(ready.bankAdapter,'disabled');
100 + report.bankDisabled=true;
101 + const login=await go('/login','ورود به محیط محلی امن');
102 + report.cdpConnected=true;
103 + assert(login.anchors.includes('/dashboard'),'NATIVE_LOCAL_LOGIN_ENTRY_MISSING');
104 + const clicked=await conn.evaluate("(()=>{const anchor=document.querySelector('a[href=\"/dashboard\"]');if(!anchor)return false;anchor.click();return true})()");
105 + assert(clicked,'NATIVE_ENTRY_NOT_CLICKABLE');
106 + await until(async()=>attach('/dashboard').then(x=>x?.text.includes('داشبورد خزانه‌داری')?x:null),65000);
107 + report.actualLocalClick=true;
108 + for(const [route,title] of [
109 + ['/dashboard','داشبورد خزانه‌داری'],['/payments','حواله‌ها'],
110 + ['/accounts','حساب‌های بانکی'],['/queue','صف ارسال بانکی'],
111 + ['/audit','رویدادهای حسابرسی']]){
112 + const view=await go(route,title);
113 + assert(view.text.includes(title),'NATIVE_ROUTE_HEADING_MISSING');
114 + report.pages.push({route,pass:true});
115 + }
116 + const queue=await go('/queue','صف ارسال بانکی');
117 + assert(queue.text.includes('غیرفعال'),'LIVE_BANK_MUST_REMAIN_DISABLED');
118 + const screen=await conn.send('Page.captureScreenshot',{format:'png'});
119 + assert(typeof screen.data==='string'&&screen.data.length>1000,'NATIVE_REAL_RENDERER_SCREENSHOT_MISSING');
120 + report.screenshot=true;
121 + report.status='PASS';
122 + console.log('GATE_PASS_V7_NONFINAL_REAL_ELECTRON_CDP_LOGIN_CLICK_FIVE_ROUTES_SIX_PAGES_BANK_DISABLED');
123 +}catch(e){
124 + report.status='FAIL';
125 + report.error=String(e?.message||'UNKNOWN').replace(/[^A-Z0-9_]/gi,'_').slice(0,90);
126 + console.log('V7_NATIVE_CDP_FAILURE_CLASS_'+report.error);
127 + process.exitCode=1;
128 +}finally{
129 + try{conn?.ws.close()}catch{}
130 + if(app?.pid)try{execFileSync('taskkill.exe',['/PID',String(app.pid),'/T','/F'],{stdio:'ignore',timeout:14000})}catch{}
131 + writeFileSync(join(fixtureDir,'v7-native-cdp-qa.json'),JSON.stringify(report,null,2));
132 +}
\ No newline at end of file