Intelligence, Advanced Charting & Trading Platform

1 /**
2 * Isolated v27 ORIGINAL Service Worker offline cache behavior.
3 * Real / and /sw.js are obtained via read-only GET; all REST API calls mocked.
4 * Poisoning cache is restricted to a throwaway Playwright browser profile and
5 * demonstrates a possible failure mechanism, not the user's actual root cause.
6 */
7 import {chromium} from 'playwright';
8 const host='https://2cbeb4e2-velyqor-preview.davoodmehraban89.workers.dev/';
9 const check=async path=>{
10 for(let attempt=0;attempt<3;attempt++){
11 try{const r=await fetch(host+path,{signal:AbortSignal.timeout(18000)});if(r.ok)return r.text();}
12 catch(e){if(attempt===2)throw Error('staged_SW_network_unavailable_after_3_attempts');}
13 if(attempt<2)await new Promise(done=>setTimeout(done,1000*(attempt+1)));
14 }
15 throw Error('staged_SW_HTTP_unavailable_after_retries');
16 };
17 const [html,sw]=await Promise.all([check(''),check('sw.js')]);
18 if(!sw.includes("caches.match('/')")||!sw.includes("chartnova-v1"))
19 throw Error('Actual versioned SW script differs from expected cached navigation strategy');
20 const browser=await chromium.launch({headless:true});
21 const report={scenario:'actual_v27_service_worker_offline_and_artificial_poison',versionUrl:host,actualSWBytes:sw.length};
22 try{
23 const ctx=await browser.newContext({viewport:{width:390,height:844},isMobile:true,serviceWorkers:'allow'});
24 await ctx.route('**/api/**',r=>r.fulfill({status:503,contentType:'application/json',body:'{"ok":false,"error":"STAGING_ONLY"}'}));
25 let simulateOffline=false;
26 await ctx.route(host,r=>simulateOffline?r.abort('internetdisconnected'):r.fulfill({status:200,contentType:'text/html; charset=utf-8',body:html}));
27 await ctx.route('**/sw.js',r=>r.fulfill({status:200,contentType:'application/javascript',body:sw}));
28 // Cache installation assets; no request to production and no provider/DB mutation.
29 await ctx.route('**/icon.svg',r=>r.fulfill({status:200,contentType:'image/svg+xml',body:'<svg xmlns="http://www.w3.org/2000/svg"/>'}));
30 await ctx.route('**/manifest.webmanifest',r=>r.fulfill({status:200,contentType:'application/manifest+json',body:'{"name":"ChartNova Preview","start_url":"/"}'}));
31 const page=await ctx.newPage();
32 const exceptions=[];
33 page.on('pageerror',e=>exceptions.push(String(e.message).slice(0,200)));
34 await page.goto(host,{waitUntil:'domcontentloaded',timeout:20000});
35 await page.waitForFunction(()=>!!navigator.serviceWorker?.controller,undefined,{timeout:12000});
36 await page.waitForFunction(async()=> (await caches.keys()).includes('chartnova-v1'),undefined,{timeout:12000});
37 simulateOffline=true;
38 await ctx.setOffline(true);
39 await page.reload({waitUntil:'domcontentloaded',timeout:16000});
40 report.validCachedOfflineVisible=await page.evaluate(()=>!!document.querySelector('.shell')&&
41 document.querySelector('.shell').getBoundingClientRect().width>80);
42 // Artificially corrupt *only the ephemeral browser cache*, never any real user cache.
43 await page.evaluate(async()=>{
44 const c=await caches.open('chartnova-v1');
45 await c.put('/',new Response('<!doctype html><html><head><title>TEST-CORRUPT</title></head><body style="background:#000"></body></html>',{headers:{'content-type':'text/html; charset=utf-8'}}));
46 });
47 await page.reload({waitUntil:'domcontentloaded',timeout:16000});
48 report.artificialBadCachedOfflineBlank=await page.evaluate(()=>
49 document.title==='TEST-CORRUPT'&&!document.querySelector('.shell')&&
50 (document.body?.innerText.length??0)===0);
51 await ctx.setOffline(false);
52 simulateOffline=false;
53 await page.reload({waitUntil:'domcontentloaded',timeout:16000});
54 report.onlineRecoveredVisible=await page.evaluate(()=>
55 !!document.querySelector('.shell')&&document.querySelector('.shell').getBoundingClientRect().width>80);
56 report.pageErrors=exceptions;
57 await ctx.close();
58 }finally{await browser.close()}
59 console.log('CHARTNOVA_V27_REAL_SW_CACHE_TEST '+JSON.stringify(report));
60 if(!report.validCachedOfflineVisible||!report.artificialBadCachedOfflineBlank||
61 !report.onlineRecoveredVisible||report.pageErrors.length)process.exitCode=1;