Intelligence, Advanced Charting & Trading Platform

1 /**
2 * Integration test of the actual DEPLOYED v26 HTML in an ephemeral browser.
3 *
4 * Does not upload or deploy a Cloudflare Worker and does not modify the
5 * live site. Only the test browser is served simulated HTML+summary JSON.
6 */
7 import { chromium, webkit } from 'playwright';
8 import {patchChartNovaV26} from './patch-chartnova-v26-light-catalog.mjs';
9 import {marketCatalogSummary} from '../packages/market-domain/src/market-catalog-summary.mjs';
10
11 const ORIGIN='https://chartnova.ir/';
12 const read=async url=>{
13 const response=await fetch(url,{signal:AbortSignal.timeout(15000)});
14 if(!response.ok)throw Error('public preflight '+response.status+' '+url);
15 return response;
16 };
17 const [htmlResponse,catalogResponse]=await Promise.all([
18 read(ORIGIN),read(ORIGIN+'api/markets')
19 ]);
20 const html=await htmlResponse.text();
21 const catalog=await catalogResponse.json();
22 const projected=marketCatalogSummary(catalog);
23 if(!projected.ok||projected.markets.length<40)
24 throw Error('Missing verified live market catalog');
25 const originalSize=JSON.stringify(catalog).length,summarySize=JSON.stringify(projected).length;
26 if(summarySize/originalSize>=0.35)
27 throw Error('Snapshot was not sufficiently smaller than full catalog');
28
29 const syntheticV26=
30 'export default {async fetch(request,env){const url=new URL(request.url);'+
31 'const preserveOriginalBackend='+JSON.stringify('x'.repeat(41000))+';'+
32 'if(url.pathname==="/api/markets"&&request.method==="GET"){return Response.json({ok:true})}'+
33 'return new Response('+JSON.stringify(html)+
34 ',{headers:{"content-type":"text/html; charset=utf-8"}})'+
35 '},async scheduled(event,env){}}';
36 const patched=patchChartNovaV26(syntheticV26);
37 for(const spec of [{browserName:'Chromium',engine:chromium},{browserName:'WebKit',engine:webkit}]){
38 const errors=[],summaryCalls=[],marketCalls=[];
39 const browser=await spec.engine.launch({headless:true});
40 const stats={browser:spec.browserName,model:'staged-v26-html-in-browser',originalBytes:originalSize,summaryBytes:summarySize,
41 savingsPercent:Math.round((1-summarySize/originalSize)*100),catalogCount:projected.markets.length};
42 try{
43 const context=await browser.newContext({viewport:{width:390,height:844},isMobile:true,
44 hasTouch:true,serviceWorkers:'block'});
45 const page=await context.newPage();
46 page.on('pageerror',e=>errors.push(String(e.message).slice(0,300)));
47 await page.route(ORIGIN,route=>route.fulfill({
48 status:200,contentType:'text/html; charset=utf-8',body:patched.newHTML
49 }));
50 await page.route('**/api/markets/summary',route=>{
51 summaryCalls.push(route.request().url());
52 return route.fulfill({status:200,contentType:'application/json',body:JSON.stringify(projected)});
53 });
54 await page.route('**/api/markets?symbol=*',async route=>{
55 marketCalls.push(route.request().url());
56 await new Promise(done=>setTimeout(done,600));
57 await route.continue();
58 });
59 const response=await page.goto(ORIGIN,{waitUntil:'domcontentloaded',timeout:18000});
60 stats.status=response?.status();
61 await page.waitForFunction(()=>{
62 const usd=document.getElementById('usd')?.textContent||'';
63 const gold=document.getElementById('gold18')?.textContent||'';
64 return usd.includes('تومان') && gold.includes('تومان');
65 },undefined,{timeout:15000});
66 await page.waitForFunction(()=>document.getElementById('chartbox')?.querySelector('svg'),undefined,{timeout:18000});
67 stats.dashboard=await page.evaluate(()=>({
68 usd:document.getElementById('usd')?.textContent,
69 gold:document.getElementById('gold18')?.textContent,
70 chart:!!document.getElementById('chartbox')?.querySelector('svg'),
71 marketStatus:document.getElementById('status')?.textContent
72 }));
73 await page.locator('[data-nav="markets"]').first().click();
74 stats.visibleMarkets=await page.locator('#marketgrid .marketitem').count();
75 const eur=page.locator('#marketgrid .marketitem').filter({has:page.locator('code.ticker',{hasText:'EUR-IRT'})});
76 if(await eur.count()){
77 await eur.locator('button.pill').click();
78 await page.waitForFunction(()=>{
79 const title=document.getElementById('techTitle')?.textContent||'';
80 const status=document.getElementById('techStatus')?.textContent||'';
81 return title.includes('EUR-IRT')&&status.includes('نمونه ساعتی');
82 },undefined,{timeout:16000});
83 stats.technical=await page.locator('#techStatus').textContent();
84 }else{
85 throw Error('EUR-IRT not present in live verified catalog');
86 }
87 stats.summaryCalls=summaryCalls.length;
88 stats.historyRequests=marketCalls.map(url=>new URL(url).searchParams.get('symbol'));
89 stats.jsErrors=errors;
90 await context.close();
91 }finally{await browser.close()}
92 console.log('CHARTNOVA_SIMULATED_V26_LAZY_MARKET '+JSON.stringify(stats));
93 if(stats.status!==200||!stats.dashboard?.chart||stats.visibleMarkets!==projected.markets.length||
94 stats.summaryCalls!==1||stats.historyRequests.length<2||
95 stats.historyRequests.some(symbol=>!symbol)||errors.length)
96 throw Error('Browser integration did not meet acceptance criteria');
97 }