Intelligence, Advanced Charting & Trading Platform
feature/chartnova-multimarket-chart-ws-foundation
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import {buildNeonEcbHistoryLookup} from "../src/neon-ecb-history.mjs"; |
| 4 | import {serveHistoryV2} from "../src/history-api.mjs"; |
| 5 | import {normalizeHistoryResponse} from "../src/chart-adapter.mjs"; |
| 6 | |
| 7 | // TEST-ONLY fixture prices and credentials. No real Neon password or fabricated production rates. |
| 8 | const fakeUrl="postgresql://chartnova_history_ro:FICTIONAL_TEST_PASSWORD@ep-example.aws.neon.tech/neondb?sslmode=require"; |
| 9 | const flags=(day)=>({data_kind:"daily_reference_point_not_ohlc",observation_date:day, |
| 10 | license:"ECB/ESCB open statistics; attribution required",source_series:"EXR.D.USD.EUR.SP00.A"}); |
| 11 | const sample=(day,price)=>({symbol:"EUR_USD_ECB",quote_asset:"USD",source:"ecb_exr", |
| 12 | observation_at:day+"T23:59:59.000Z",price,quality:"GOOD",flags:flags(day)}); |
| 13 | const url=(other={})=>new Request("https://app.chartnova.ir/api/v2/history?"+ |
| 14 | new URLSearchParams({symbol:"EUR_USD_ECB",from:"2026-10-01",to:"2026-10-10",timeframe:"1d", |
| 15 | dataKind:"reference_point",limit:"1",...other})); |
| 16 | test("view adapter is parameterized, serves oldest-last pagination and never leaks secret",async()=>{ |
| 17 | let statements=0,actualParams; |
| 18 | const lookup=buildNeonEcbHistoryLookup({databaseUrl:fakeUrl,fetchImpl:async (endpoint,options)=>{ |
| 19 | statements++; |
| 20 | assert.equal(endpoint,"https://ep-example.aws.neon.tech/sql"); |
| 21 | assert.equal(options.headers["Neon-Connection-String"],fakeUrl); |
| 22 | const body=JSON.parse(options.body);actualParams=body.params; |
| 23 | assert.equal(body.query.includes("public.chartnova_history_public"),true); |
| 24 | assert.match(body.query,/symbol=\$1/); |
| 25 | assert.match(body.query,/LIMIT \$5::integer/); |
| 26 | assert(!/INSERT|DELETE|UPDATE|DROP|ALTER/i.test(body.query)); |
| 27 | return Response.json({rows:[sample("2026-10-09","1.12"),sample("2026-10-08","1.11")]}); |
| 28 | }}); |
| 29 | const response=await serveHistoryV2(url(),{lookup}); |
| 30 | assert.equal(response.status,200); |
| 31 | const serialized=await response.text(); |
| 32 | assert.equal(serialized.includes("FICTIONAL_TEST_PASSWORD"),false); |
| 33 | const payload=JSON.parse(serialized); |
| 34 | assert.equal(payload.rights,"approved_public"); |
| 35 | assert.equal(payload.page.hasMore,true); |
| 36 | assert.equal(payload.page.nextCursor,"2026-10-09T23:59:59.000Z"); |
| 37 | assert.deepEqual(actualParams,["EUR_USD_ECB","2026-10-01T00:00:00.000Z","2026-10-11T00:00:00.000Z",null,2]); |
| 38 | const parsed=normalizeHistoryResponse(payload,"EUR_USD_ECB"); |
| 39 | assert.equal(parsed.kind,"reference"); |
| 40 | assert.deepEqual(parsed.data,[{time:"2026-10-09",value:1.12}]); |
| 41 | assert.equal(statements,1); |
| 42 | }); |
| 43 | test("unconfigured reader or privileged role never queries Neon",()=>{ |
| 44 | assert.throws(()=>buildNeonEcbHistoryLookup({databaseUrl:""}),/not_configured/); |
| 45 | assert.throws(()=>buildNeonEcbHistoryLookup({databaseUrl:fakeUrl.replace("chartnova_history_ro","neondb_owner")}),/restricted_history_reader/); |
| 46 | assert.throws(()=>buildNeonEcbHistoryLookup({databaseUrl:fakeUrl.replace("neon.tech","example.com")}),/not_configured/); |
| 47 | }); |
| 48 | test("licensed v2 repository cannot turn ECB reference rows into OHLC",async()=>{ |
| 49 | let called=false; |
| 50 | const lookup=buildNeonEcbHistoryLookup({databaseUrl:fakeUrl,fetchImpl:()=>{called=true;throw Error("should not fetch")}}); |
| 51 | const result=await serveHistoryV2(url({dataKind:"source_ohlc"}),{lookup}); |
| 52 | assert.equal(result.status,503);assert.equal(called,false); |
| 53 | }); |
| 54 | test("provenance mismatch, untrusted source, excessive rows and wrong order are rejected",async()=>{ |
| 55 | const cases=[ |
| 56 | [{...sample("2026-10-09","1.12"),flags:{...flags("2026-10-09"),data_kind:"source_ohlc"}}], |
| 57 | [{...sample("2026-10-09","1.12"),source:"unknown_provider"}], |
| 58 | [{...sample("2026-10-09","1.12"),price:"-42"}], |
| 59 | [sample("2026-10-08","1.11"),sample("2026-10-09","1.12")], |
| 60 | [sample("2026-10-09","1.12"),sample("2026-10-08","1.11"),sample("2026-10-07","1.10")], |
| 61 | ]; |
| 62 | for(const rows of cases){ |
| 63 | const lookup=buildNeonEcbHistoryLookup({databaseUrl:fakeUrl,fetchImpl:async()=>Response.json({rows})}); |
| 64 | const response=await serveHistoryV2(url(),{lookup}); |
| 65 | assert.equal(response.status,502,JSON.stringify(rows)); |
| 66 | assert.equal((await response.text()).includes("FICTIONAL_TEST_PASSWORD"),false); |
| 67 | } |
| 68 | }); |
| 69 | test("cursor is bound to SQL and rejects rows with equal timestamp",async()=>{ |
| 70 | const lookup=buildNeonEcbHistoryLookup({databaseUrl:fakeUrl,fetchImpl:async(_endpoint,{body})=>{ |
| 71 | const {params}=JSON.parse(body); |
| 72 | assert.equal(params[3],"2026-10-09T23:59:59.000Z"); |
| 73 | return Response.json({rows:[sample("2026-10-09","1.12")]}); |
| 74 | }}); |
| 75 | const response=await serveHistoryV2(url({cursor:"2026-10-09T23:59:59.000Z"}),{lookup}); |
| 76 | assert.equal(response.status,502,"storage returning cursor-equal data must fail validation"); |
| 77 | }); |