Intelligence, Advanced Charting & Trading Platform
test(worker): verify opt-in Neon v2 REST integration, secret redaction and ECB candle rejection
davoodmehraban89 committed
Oct 12, 2026 at 06:27 UTC
7e3283f902511ccd02d9c748e1078a722f9d1b02
1 file changed
+51
deploy/chartnova/history-v2-worker.test.mjs
new
+51
@@ -0,0 +1,51 @@
1
+import test from "node:test";
2
+import assert from "node:assert/strict";
3
+import worker from "./worker.mjs";
4
+
5
+// No live credentials in this test; SQL results are TEST-ONLY mock observations.
6
+const secret="postgresql://chartnova_history_ro:TEST_ONLY_SECRET@ep-example.aws.neon.tech/neondb?sslmode=require";
7
+const query=new URLSearchParams({symbol:"EUR_USD_ECB",from:"2026-10-01",to:"2026-10-10",
8
+ dataKind:"reference_point",timeframe:"1d",limit:"10"});
9
+const endpoint="https://app.chartnova.ir/api/v2/history?"+query;
10
+const env=(withSecret=false)=>({MARKET_CACHE:{get:async()=>null,put:async()=>{}},...(withSecret?{CHARTNOVA_HISTORY_DATABASE_URL:secret}:{})});
11
+test("v2 URL is unavailable unless server-side restricted Neon credential is configured",async()=>{
12
+ const r=await worker.fetch(new Request(endpoint),env());
13
+ assert.equal(r.status,503);assert.equal((await r.json()).error,"history_storage_not_configured");
14
+});
15
+test("v2 URL rejects a superuser database connection",async()=>{
16
+ const e=env(true);e.CHARTNOVA_HISTORY_DATABASE_URL=secret.replace("chartnova_history_ro","neondb_owner");
17
+ const r=await worker.fetch(new Request(endpoint),e);
18
+ assert.equal(r.status,503);
19
+ assert(!JSON.stringify(await r.json()).includes("neondb_owner"));
20
+});
21
+test("v2 URL reads and sanitizes real-shape public ECB view observations",async()=>{
22
+ const prior=globalThis.fetch;let dbCalls=0;
23
+ globalThis.fetch=async(url,options)=>{
24
+ dbCalls++;
25
+ assert.equal(url,"https://ep-example.aws.neon.tech/sql");
26
+ assert.equal(options.headers["Neon-Connection-String"],secret);
27
+ return Response.json({rows:[{
28
+ symbol:"EUR_USD_ECB",quote_asset:"USD",source:"ecb_exr",
29
+ observation_at:"2026-10-09T23:59:59.000Z",price:"1.12",quality:"GOOD",
30
+ flags:{source_series:"EXR.D.USD.EUR.SP00.A",license:"ECB/ESCB open statistics; attribution required",
31
+ data_kind:"daily_reference_point_not_ohlc",observation_date:"2026-10-09",privateInternalField:"DO_NOT_PUBLISH"},
32
+ fakeDatabaseToken:"DO_NOT_PUBLISH"
33
+ }]});
34
+ };
35
+ try{
36
+ const r=await worker.fetch(new Request(endpoint),env(true));
37
+ assert.equal(r.status,200);
38
+ const body=await r.text();
39
+ assert(!body.includes("TEST_ONLY_SECRET"));assert(!body.includes("DO_NOT_PUBLISH"));
40
+ const data=JSON.parse(body);
41
+ assert.equal(data.dataKind,"reference_point");assert.equal(data.rights,"approved_public");
42
+ assert.equal(data.count,1);assert.equal(data.series[0].price,1.12);
43
+ assert.equal(dbCalls,1);
44
+ }finally{globalThis.fetch=prior}
45
+});
46
+test("v2 endpoint never pretends ECB rates are OHLC",async()=>{
47
+ const url=new URL(endpoint);url.searchParams.set("dataKind","source_ohlc");
48
+ const r=await worker.fetch(new Request(url),env(true));
49
+ assert.equal(r.status,503);
50
+ assert.equal((await r.json()).error,"licensed_history_unavailable");
51
+});