Intelligence, Advanced Charting & Trading Platform
test(incident): compare root/app fresh browser sessions and isolated per-origin SW cache failure
davoodmehraban89 committed
Oct 16, 2026 at 17:20 UTC
28aa5b471bc21a626a4ddf78a5b2b67f5cd3767c
1 file changed
+148
scripts/diagnose-chartnova-origin-session-split.mjs
new
+148
@@ -0,0 +1,148 @@
1
+/**
2
+ * Read-only production-origins A/B browser diagnostic.
3
+ *
4
+ * Uses GET only to download PUBLIC HTML + service workers. Browser fetch of
5
+ * app API endpoints is ALWAYS intercepted and fulfilled with synthetic 503.
6
+ * Cache poisoning is restricted to a DISPOSABLE Chromium browser context.
7
+ * Nothing on Cloudflare, production, user devices or persistent storage changes.
8
+ *
9
+ * A "fresh context" mimics a new browser session, NOT Safari Private exactly.
10
+ */
11
+import {chromium,webkit} from 'playwright';
12
+import {createHash} from 'node:crypto';
13
+
14
+const sites=[
15
+ {name:'root',base:'https://chartnova.ir/'},
16
+ {name:'app',base:'https://app.chartnova.ir/'}
17
+];
18
+async function getPublic(url){
19
+ for(let tries=1;tries<=3;tries++){
20
+ try{
21
+ const r=await fetch(url,{signal:AbortSignal.timeout(18000),headers:{'cache-control':'no-cache'}});
22
+ if(r.ok)return r.text();
23
+ }catch{}
24
+ if(tries<3)await new Promise(ok=>setTimeout(ok,tries*900));
25
+ }
26
+ throw Error('EXTERNAL_GET_UNAVAILABLE '+new URL(url).hostname);
27
+}
28
+const assets={};
29
+for(const {name,base} of sites){
30
+ const [html,sw]=await Promise.all([getPublic(base),getPublic(base+'sw.js')]);
31
+ if(!html.includes('class="shell"')||!sw.includes("chartnova-v1"))
32
+ throw Error('Unexpected public UI or SW version at '+name);
33
+ assets[name]={html,sw};
34
+}
35
+const sha=s=>createHash('sha256').update(s,'utf8').digest('hex');
36
+const report={
37
+ rootAndAppHTMLByteIdentical:assets.root.html===assets.app.html,
38
+ rootAndAppServiceWorkerByteIdentical:assets.root.sw===assets.app.sw,
39
+ sha256:{rootHTML:sha(assets.root.html),appHTML:sha(assets.app.html),
40
+ rootSW:sha(assets.root.sw),appSW:sha(assets.app.sw)},
41
+ results:[],simulatedOriginSplit:null,realUserReproduction:false
42
+};
43
+function setupRoutes(context){
44
+ return Promise.all(sites.map(({name,base})=>Promise.all([
45
+ context.route(base,route=>route.fulfill({
46
+ status:200,contentType:'text/html; charset=utf-8',body:assets[name].html
47
+ })),
48
+ context.route(base+'sw.js',route=>route.fulfill({
49
+ status:200,contentType:'application/javascript',body:assets[name].sw
50
+ })),
51
+ context.route(base+'icon.svg',route=>route.fulfill({
52
+ status:200,contentType:'image/svg+xml',body:'<svg xmlns="http://www.w3.org/2000/svg"/>'
53
+ })),
54
+ context.route(base+'manifest.webmanifest',route=>route.fulfill({
55
+ status:200,contentType:'application/manifest+json',
56
+ body:'{"name":"Incident diagnostic","start_url":"/"}'
57
+ }))
58
+ ]))).then(()=>context.route('**/api/**',route=>route.fulfill({
59
+ status:503,contentType:'application/json',
60
+ headers:{'cache-control':'no-store'},
61
+ body:'{"ok":false,"error":"CONTROLLED_SOURCE_OUTAGE_NO_LIVE_API"}'
62
+ })));
63
+}
64
+async function visible(page){
65
+ return page.evaluate(()=>{
66
+ const el=document.querySelector('.shell');
67
+ const b=el?.getBoundingClientRect();
68
+ return !!b&&b.width>100&&b.height>100;
69
+ });
70
+}
71
+async function open(page,url){
72
+ const e=[];
73
+ page.on('pageerror',x=>e.push(String(x.message).slice(0,160)));
74
+ const response=await page.goto(url,{waitUntil:'domcontentloaded',timeout:22000});
75
+ await page.waitForTimeout(1000);
76
+ return {http:response?.status(),visible:await visible(page),jsExceptions:e};
77
+}
78
+let fail=false;
79
+for(const {name,engine} of [{name:'Chromium',engine:chromium},{name:'WebKit',engine:webkit}]){
80
+ const browser=await engine.launch({headless:true});
81
+ try{
82
+ // Independently fresh profiles: not the owner's normal or private profile.
83
+ for(const {name:origin,base} of sites){
84
+ const context=await browser.newContext({viewport:{width:390,height:844},
85
+ isMobile:true,hasTouch:true,serviceWorkers:'block'});
86
+ await setupRoutes(context);
87
+ const page=await context.newPage();
88
+ const r=await open(page,base);
89
+ report.results.push({browser:name,origin,session:'new-disposable-profile',...r});
90
+ if(r.http!==200||!r.visible||r.jsExceptions.length)fail=true;
91
+ await context.close();
92
+ }
93
+ if(name!=='Chromium')continue;
94
+ const context=await browser.newContext({viewport:{width:390,height:844},
95
+ isMobile:true,hasTouch:true,serviceWorkers:'allow'});
96
+ await setupRoutes(context);
97
+ const rootPage=await context.newPage(),appPage=await context.newPage();
98
+ let root=await open(rootPage,sites[0].base);
99
+ let app=await open(appPage,sites[1].base);
100
+ await rootPage.waitForFunction(()=>!!navigator.serviceWorker.controller,undefined,{timeout:14000});
101
+ await appPage.waitForFunction(()=>!!navigator.serviceWorker.controller,undefined,{timeout:14000});
102
+ const beforeRoot=await rootPage.evaluate(async()=>{
103
+ const list=await caches.keys();return {keys:list,hasPage:!!(await (await caches.open('chartnova-v1')).match('/'))}
104
+ });
105
+ const beforeApp=await appPage.evaluate(async()=>{
106
+ const list=await caches.keys();return {keys:list,hasPage:!!(await (await caches.open('chartnova-v1')).match('/'))}
107
+ });
108
+ // Pretend corruption in root's cache ONLY inside ephemeral browser storage.
109
+ await rootPage.evaluate(async()=>{
110
+ const cache=await caches.open('chartnova-v1');
111
+ await cache.put('/',new Response('<!doctype html><html><title>SIMULATED_BAD_ROOT_CACHE</title><body></body></html>',{
112
+ headers:{'content-type':'text/html; charset=utf-8'}
113
+ }));
114
+ });
115
+ let offline=false;
116
+ // Fail all navigation network requests before SW fallback.
117
+ await context.unroute(sites[0].base);
118
+ await context.unroute(sites[1].base);
119
+ await context.route(sites[0].base,r=>offline?r.abort('internetdisconnected'):
120
+ r.fulfill({status:200,contentType:'text/html',body:assets.root.html}));
121
+ await context.route(sites[1].base,r=>offline?r.abort('internetdisconnected'):
122
+ r.fulfill({status:200,contentType:'text/html',body:assets.app.html}));
123
+ offline=true;
124
+ await context.setOffline(true);
125
+ await rootPage.reload({waitUntil:'domcontentloaded',timeout:22000});
126
+ const rootBlank=!(await visible(rootPage));
127
+ await appPage.reload({waitUntil:'domcontentloaded',timeout:22000});
128
+ const appVisible=await visible(appPage);
129
+ offline=false;
130
+ await context.setOffline(false);
131
+ await rootPage.reload({waitUntil:'domcontentloaded',timeout:22000});
132
+ const rootRecovered=await visible(rootPage);
133
+ report.simulatedOriginSplit={
134
+ rootAndAppInitiallyVisible:root.visible&&app.visible,
135
+ rootAndAppHaveSeparateOriginCache:beforeRoot.hasPage&&beforeApp.hasPage,
136
+ rootWasBlankWithDeliberatelyCorruptCacheAndOffline:rootBlank,
137
+ appStayedVisibleWithHealthyCacheAndOffline:appVisible,
138
+ rootRecoveredOnline:rootRecovered,
139
+ disposableBrowserOnly:true,
140
+ doesNotEstablishOwnersCause:true
141
+ };
142
+ if(!Object.values(report.simulatedOriginSplit).slice(0,5).every(Boolean))fail=true;
143
+ await context.close();
144
+ }finally{await browser.close()}
145
+}
146
+console.log('CHARTNOVA_ORIGIN_BROWSER_DIAGNOSTIC '+JSON.stringify(report));
147
+if(!report.rootAndAppHTMLByteIdentical||!report.rootAndAppServiceWorkerByteIdentical||fail)
148
+ process.exitCode=1;