Intelligence, Advanced Charting & Trading Platform

1 #!/usr/bin/env node
2 // Local-only WebSocket smoke test. ALL events are FICTITIOUS TEST FIXTURES.
3 // DO NOT route this script to a public host or any production database.
4 import assert from "node:assert/strict";
5 const base=process.env.CHARTNOVA_LOCAL_TEST_ORIGIN??"http://127.0.0.1:8789";
6 const url=new URL(base);
7 if(!["127.0.0.1","localhost"].includes(url.hostname)||url.protocol!=="http:")
8 throw Error("Test must target an HTTP localhost Worker only");
9 if(typeof WebSocket!=="function")throw Error("Node WebSocket global required (Node 22+)");
10 const LOCAL_TOKEN="CHARTNOVA_LOCAL_ONLY_FAKE_PUBLISH_TOKEN_12345678901234567890";
11 const SYMBOL="EUR_USD_ECB";
12 const sleep=(ms)=>new Promise(r=>setTimeout(r,ms));
13 const timeout=AbortSignal.timeout(16000);
14 async function waitForType(socket,type){
15 return await new Promise((resolve,reject)=>{
16 const abort=()=>{cleanup();reject(Error("Timeout waiting for "+type))};
17 const cleanup=()=>{socket.removeEventListener("message",onmessage);socket.removeEventListener("error",onerror);timeout.removeEventListener("abort",abort)};
18 const onerror=()=>{cleanup();reject(Error("WebSocket test socket error"))};
19 const onmessage=(event)=>{
20 let data;try{data=JSON.parse(event.data)}catch{return}
21 if(data.type!==type)return;
22 cleanup();resolve(data);
23 };
24 socket.addEventListener("message",onmessage);
25 socket.addEventListener("error",onerror);
26 timeout.addEventListener("abort",abort,{once:true});
27 });
28 }
29 const wsUrl=new URL("/stream?symbol="+SYMBOL,url);
30 wsUrl.protocol="ws:";
31 const ws=new WebSocket(wsUrl);
32 try{
33 await new Promise((resolve,reject)=>{
34 const abort=()=>reject(Error("Local Worker WebSocket open timeout"));
35 ws.addEventListener("open",resolve,{once:true});
36 ws.addEventListener("error",()=>reject(Error("Local Worker socket open failed")),{once:true});
37 timeout.addEventListener("abort",abort,{once:true});
38 });
39 const readyPromise=waitForType(ws,"session.ready");
40 ws.send(JSON.stringify({version:1,type:"stream.subscribe",symbols:[SYMBOL]}));
41 const ready=await readyPromise;
42 assert.equal(ready.symbol,SYMBOL);
43 const updatePromise=waitForType(ws,"market.update");
44 const fake={
45 dataKind:"reference_point",symbol:SYMBOL,source:"test_local_only",unit:"USD_PER_EUR",
46 price:"1.2345",sourceTimestamp:"2026-10-09T12:00:00Z",receivedAt:"2026-10-09T12:00:01Z",
47 quality:"GOOD",licenseEvidence:"test-only-do-not-publish",rights:"approved_public"
48 };
49 const response=await fetch(new URL("/internal/market-update",url),{
50 method:"POST",
51 headers:{"content-type":"application/json","authorization":"Bearer "+LOCAL_TOKEN},
52 body:JSON.stringify(fake),signal:timeout
53 });
54 assert.equal(response.status,200,"Local publisher should accept test-only credentials");
55 const publish=await response.json();
56 assert.equal(publish.ok,true);
57 assert.equal(publish.delivered,1);
58 const frame=await updatePromise;
59 assert.equal(frame.version,1);
60 assert.equal(frame.payload.symbol,SYMBOL);
61 assert.equal(frame.payload.dataKind,"reference_point");
62 assert.equal(frame.payload.price,"1.2345");
63 assert(!JSON.stringify(frame).includes(LOCAL_TOKEN));
64 assert(!JSON.stringify(frame).includes("test-only-do-not-publish"));
65 console.log(JSON.stringify({ok:true,environment:"localhost_only",fixture:"synthetic_test_event_not_persisted",symbol:SYMBOL,sequence:frame.sequence,delivered:publish.delivered}));
66 }finally{
67 ws.close();
68 await sleep(100);
69 }