@davoodmehraban89 / VELYQOR / commits / 9934fa9
Fork 0

Intelligence, Advanced Charting & Trading Platform

test(chartnova): complete lazy catalog source-patch integration and failure tests

davoodmehraban89 committed Oct 15, 2026 at 14:44 UTC 9934fa9fffd7e4054921ed156b012feb49b5a5e4
1 file changed +82 -1
scripts/patch-chartnova-v26-light-catalog.test.mjs
+82 -1
@@ -3,4 +3,85 @@ import assert from 'node:assert/strict';
3 import { Script } from 'node:vm';
4 import { patchChartNovaV26 } from './patch-chartnova-v26-light-catalog.mjs';
5
6 -const jsx=String.raw
\ No newline at end of file
6 +const jsx=[
7 + 'const marketState={items:[],source:null,receivedAt:null,selected:"USD-IRT"};',
8 + 'const data={};let chosen="usd";',
9 + 'function instrument(symbol){return marketState.items.find(x=>x.symbol===symbol)}',
10 + 'function el(id){return document.getElementById(id)}',
11 + 'function renderMarkets(){const m={history:[]};void(m.history?.length+" نمونه ساعتی")}',
12 + 'function renderTechnical(){const m=instrument(marketState.selected);if(!m)return; const history=m.history||[];void history;}',
13 + 'function renderSelected(){const code="USD-IRT",m=instrument(code);if(m?.history?.length>1){void m.history}}',
14 + 'function renderWidget(){}',
15 + 'async function loadMarkets(){',
16 + ' const response=await fetch("/api/markets",{cache:"no-store"});',
17 + ' const payload=await response.json();',
18 + ' marketState.items=payload.markets;',
19 + ' renderMarkets();renderTechnical();renderSelected();',
20 + '}',
21 + 'async function refresh(){',
22 + ' await Promise.allSettled([loadMarkets()]);',
23 + ' renderSelected();renderMarkets();renderTechnical();',
24 + '}'
25 +].join('\n');
26 +function v26(js=jsx){
27 + const html='<!doctype html><html lang="fa"><body><div id="app"></div><script>'+js+'</script></body></html>';
28 + return 'export default {async fetch(request,env){const url=new URL(request.url);'
29 + +'const noChange='+JSON.stringify('x'.repeat(41000))+';'
30 + +'if(url.pathname==="/api/markets"&&request.method==="GET"){return Response.json({ok:true})}'
31 + +'return new Response('+JSON.stringify(html)+',{headers:{"content-type":"text/html; charset=utf-8"}})'
32 + +'},async scheduled(event,env){}}';
33 +}
34 +const snapshot=()=>({
35 + ok:true,source:'gheymat.online',fetchedAt:'2026-10-15T10:59:00Z',
36 + markets:[{
37 + symbol:'USD-IRT',name:'دلار',base:'USD',quote:'IRT',category:'CURRENCY',
38 + sellPrice:100000,buyPrice:100100,change:2,updatedAt:'2026-10-15T10:59:00Z',
39 + isUpToDate:true,history:Array.from({length:168},(_,i)=>90000+i)
40 + }]
41 +});
42 +test('patches only known anchors and retains old route, while loading history on demand',()=>{
43 + const input=v26(),r=patchChartNovaV26(input);
44 + assert.ok(r.source.length>input.length);
45 + assert.ok(r.newJS.includes('"/api/markets/summary"'));
46 + assert.ok(r.newJS.includes('requestChartnovaSeries'));
47 + assert.ok(r.newJS.includes('chartnovaSeriesCache'));
48 + assert.equal(r.newHTML.replace(r.newJS,r.oldJS),r.oldHTML);
49 + assert.ok(r.source.includes('async scheduled(event,env)'));
50 + assert.ok(r.source.includes('if(url.pathname==="/api/markets"&&request.method==="GET"){'));
51 + assert.doesNotThrow(()=>new Script(r.newJS));
52 + assert.ok(!r.newJS.includes('renderSelected();renderMarkets();renderTechnical();'));
53 +});
54 +test('updated Worker serves lightweight JSON from unchanged source catalog',async()=>{
55 + const transformed=patchChartNovaV26(v26());
56 + const uri='data:text/javascript;base64,'+Buffer.from(transformed.source).toString('base64');
57 + const mod=await import(uri),cache=snapshot(),before=JSON.stringify(cache);
58 + const result=await mod.default.fetch(new Request('https://chartnova.ir/api/markets/summary'),{
59 + MARKET_CACHE:{get:async key=>key==='markets:gheymat:v1'?cache:null}
60 + });
61 + assert.equal(result.status,200);
62 + const data=await result.json();
63 + assert.equal(data.ok,true);
64 + assert.equal(data.markets[0].symbol,'USD-IRT');
65 + assert.equal(data.markets[0].history,undefined);
66 + assert.equal(data.markets[0].historyCount,168);
67 + assert.equal(JSON.stringify(cache),before);
68 + assert.equal(result.headers.get('cache-control'),'no-store');
69 +});
70 +test('refuses missing catalog instead of inventing prices',async()=>{
71 + const transformed=patchChartNovaV26(v26());
72 + const mod=await import('data:text/javascript;base64,'+Buffer.from(transformed.source).toString('base64'));
73 + const result=await mod.default.fetch(new Request('https://chartnova.ir/api/markets/summary'),{
74 + MARKET_CACHE:{get:async()=>null}
75 + });
76 + assert.equal(result.status,503);
77 + assert.equal((await result.json()).error,'market_data_not_collected');
78 +});
79 +test('rejects Cloudflare multipart and most-recent uploaded v27 instead of active v26',()=>{
80 + assert.throws(()=>patchChartNovaV26('--boundary\r\n'+v26()),/clean v26/);
81 + assert.throws(()=>patchChartNovaV26(v26().replace('const noChange=', 'const chartnovaRefreshInFlight=null;const noChange=')),/Version mismatch/);
82 +});
83 +test('fails closed against unknown original HTML/JS and repeated application',()=>{
84 + assert.throws(()=>patchChartNovaV26(v26(jsx.replace('fetch("/api/markets"', 'fetch("/api/not-markets"'))),/small snapshot fetch/);
85 + assert.throws(()=>patchChartNovaV26(v26(jsx.replace('renderMarkets();renderTechnical();renderSelected();','renderMarkets();'))),/lazy loaded markets/);
86 + assert.throws(()=>patchChartNovaV26(patchChartNovaV26(v26()).source),/Version mismatch/);
87 +});