Intelligence, Advanced Charting & Trading Platform

1 import test from 'node:test';
2 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=[
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 +'\nasync function stored(env,key){return env.MARKET_CACHE.get(key,"json")}';
34 }
35 const snapshot=()=>({
36 ok:true,source:'gheymat.online',fetchedAt:'2026-10-15T10:59:00Z',
37 markets:[{
38 symbol:'USD-IRT',name:'دلار',base:'USD',quote:'IRT',category:'CURRENCY',
39 sellPrice:100000,buyPrice:100100,change:2,updatedAt:'2026-10-15T10:59:00Z',
40 isUpToDate:true,history:Array.from({length:168},(_,i)=>90000+i)
41 }]
42 });
43 test('patches only known anchors and retains old route, while loading history on demand',()=>{
44 const input=v26(),r=patchChartNovaV26(input);
45 assert.ok(r.source.length>input.length);
46 assert.ok(r.newJS.includes('"/api/markets/summary"'));
47 assert.ok(r.newJS.includes('requestChartnovaSeries'));
48 assert.ok(r.newJS.includes('chartnovaSeriesCache'));
49 assert.equal(r.newHTML.replace(r.newJS,r.oldJS),r.oldHTML);
50 assert.ok(r.source.includes('async scheduled(event,env)'));
51 assert.ok(r.source.includes('if(url.pathname==="/api/markets"&&request.method==="GET"){'));
52 assert.doesNotThrow(()=>new Script(r.newJS));
53 assert.ok(!r.newJS.includes('renderSelected();renderMarkets();renderTechnical();'));
54 });
55 test('updated Worker serves lightweight JSON from unchanged source catalog',async()=>{
56 const transformed=patchChartNovaV26(v26());
57 const uri='data:text/javascript;base64,'+Buffer.from(transformed.source).toString('base64');
58 const mod=await import(uri),cache=snapshot(),before=JSON.stringify(cache);
59 const result=await mod.default.fetch(new Request('https://chartnova.ir/api/markets/summary'),{
60 MARKET_CACHE:{get:async key=>key==='markets:gheymat:v1'?cache:null}
61 });
62 assert.equal(result.status,200);
63 const data=await result.json();
64 assert.equal(data.ok,true);
65 assert.equal(data.markets[0].symbol,'USD-IRT');
66 assert.equal(data.markets[0].history,undefined);
67 assert.equal(data.markets[0].historyCount,168);
68 assert.equal(JSON.stringify(cache),before);
69 assert.equal(result.headers.get('cache-control'),'no-store');
70 });
71 test('refuses missing catalog instead of inventing prices',async()=>{
72 const transformed=patchChartNovaV26(v26());
73 const mod=await import('data:text/javascript;base64,'+Buffer.from(transformed.source).toString('base64'));
74 const result=await mod.default.fetch(new Request('https://chartnova.ir/api/markets/summary'),{
75 MARKET_CACHE:{get:async()=>null}
76 });
77 assert.equal(result.status,503);
78 assert.equal((await result.json()).error,'market_data_not_collected');
79 });
80 test('rejects Cloudflare multipart and most-recent uploaded v27 instead of active v26',()=>{
81 assert.throws(()=>patchChartNovaV26('--boundary\r\n'+v26()),/clean v26/);
82 assert.throws(()=>patchChartNovaV26(v26().replace('const noChange=', 'const chartnovaRefreshInFlight=null;const noChange=')),/Version mismatch/);
83 });
84 test('fails closed against unknown original HTML/JS and repeated application',()=>{
85 assert.throws(()=>patchChartNovaV26(v26(jsx.replace('fetch("/api/markets"', 'fetch("/api/not-markets"'))),/small snapshot fetch/);
86 assert.throws(()=>patchChartNovaV26(v26(jsx.replace('renderMarkets();renderTechnical();renderSelected();','renderMarkets();'))),/lazy loaded markets/);
87 assert.throws(()=>patchChartNovaV26(patchChartNovaV26(v26()).source),/Version mismatch/);
88 });