Intelligence, Advanced Charting & Trading Platform
feature/paper-trading-risk-controls-2026-10-14
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 { |
| 4 | createPaperWorkspace,addPaperPosition,markPaperWorkspacePosition,closePaperWorkspacePosition, |
| 5 | paperWorkspaceSummary,restorePaperWorkspace,exportPaperWorkspaceCSV |
| 6 | } from '../src/paper-workspace.mjs'; |
| 7 | |
| 8 | const stamp=n=>'2026-10-14T12:00:'+String(n).padStart(2,'0')+'Z'; |
| 9 | const options=(symbol='XAU_USD',n=0)=>({ |
| 10 | symbol,entryPrice:100,capital:1000,side:'LONG',stopLossPercent:1, |
| 11 | takeProfitPercent:2,trailingStopPercent:1,at:stamp(n) |
| 12 | }); |
| 13 | const one=()=>addPaperPosition(createPaperWorkspace(),options()); |
| 14 | const id=w=>w.positions[0].id; |
| 15 | test('creates empty paper-only workspace with no implied traded capital',()=>{ |
| 16 | const w=createPaperWorkspace(); |
| 17 | assert.deepEqual(paperWorkspaceSummary(w),{total:0,open:0,closed:0,positions:[]}); |
| 18 | }); |
| 19 | test('supports several independent manual positions without summing quote units',()=>{ |
| 20 | let w=one();w=addPaperPosition(w,options('EUR_USD_ECB',1)); |
| 21 | let s=paperWorkspaceSummary(w); |
| 22 | assert.equal(s.open,2);assert.deepEqual(s.positions.map(p=>p.symbol),['XAU_USD','EUR_USD_ECB']); |
| 23 | assert.ok(!('netPnL' in s)); |
| 24 | }); |
| 25 | test('manual marks update only target position and close at most once',()=>{ |
| 26 | let w=one(),first=id(w); |
| 27 | w=addPaperPosition(w,options('GOLD18_IRT',1)); |
| 28 | w=markPaperWorkspacePosition(w,first,{price:101,at:stamp(2)}); |
| 29 | assert.equal(w.positions[0].position.markPrice,101); |
| 30 | assert.equal(w.positions[1].position.markPrice,100); |
| 31 | w=closePaperWorkspacePosition(w,first,{price:100.5,at:stamp(3)}); |
| 32 | assert.equal(w.positions[0].position.exit.reason,'MANUAL'); |
| 33 | assert.throws(()=>closePaperWorkspacePosition(w,first,{price:100,at:stamp(4)}),/OPEN/); |
| 34 | }); |
| 35 | test('automatic stop and TP states are preserved through round-trip storage',()=>{ |
| 36 | let w=one();w=markPaperWorkspacePosition(w,id(w),{price:102,at:stamp(1)}); |
| 37 | assert.equal(w.positions[0].position.status,'CLOSED'); |
| 38 | const restored=restorePaperWorkspace(JSON.stringify(w)); |
| 39 | assert.deepEqual(restored,w); |
| 40 | assert.equal(restored.positions[0].position.exit.reason,'TAKE_PROFIT'); |
| 41 | }); |
| 42 | test('trailing mark, manual close and open positions can all roundtrip',()=>{ |
| 43 | let w=one(); |
| 44 | w=markPaperWorkspacePosition(w,id(w),{price:100.25,at:stamp(1)}); |
| 45 | assert.deepEqual(restorePaperWorkspace(JSON.stringify(w)),w); |
| 46 | w=closePaperWorkspacePosition(w,id(w),{price:100.1,at:stamp(2)}); |
| 47 | assert.deepEqual(restorePaperWorkspace(JSON.stringify(w)),w); |
| 48 | }); |
| 49 | test('tampered derived P&L fails restore even if valid JSON',()=>{ |
| 50 | const p=JSON.parse(JSON.stringify(one())); |
| 51 | p.positions[0].position.estimatedPnL=1000000; |
| 52 | assert.throws(()=>restorePaperWorkspace(JSON.stringify(p)),/does not match/); |
| 53 | }); |
| 54 | test('rejects forged sources, mutated opening timestamps and injected events',()=>{ |
| 55 | const base=one(); |
| 56 | const p=structuredClone(base);p.positions[0].position.events[0].source='LIVE_TRADE'; |
| 57 | assert.throws(()=>restorePaperWorkspace(JSON.stringify(p)),/opening event/); |
| 58 | const q=structuredClone(base);q.positions[0].position.events.push({type:'EXECUTE',at:stamp(1)}); |
| 59 | assert.throws(()=>restorePaperWorkspace(JSON.stringify(q)),/unsupported/); |
| 60 | }); |
| 61 | test('rejects inconsistent ID sequence and duplicate IDs',()=>{ |
| 62 | const w=one(); |
| 63 | const p=structuredClone(w);p.nextId=1; |
| 64 | assert.throws(()=>restorePaperWorkspace(JSON.stringify(p)),/sequence/); |
| 65 | const q=structuredClone(w);q.positions.push(structuredClone(q.positions[0]));q.nextId=3; |
| 66 | assert.throws(()=>restorePaperWorkspace(JSON.stringify(q)),/duplicate/); |
| 67 | }); |
| 68 | test('rejects missing source text, corrupted JSON and oversized payloads',()=>{ |
| 69 | assert.throws(()=>restorePaperWorkspace(''),/invalid/); |
| 70 | assert.throws(()=>restorePaperWorkspace('{oops'),/JSON/); |
| 71 | assert.throws(()=>restorePaperWorkspace('x'.repeat(300001)),/payload/); |
| 72 | }); |
| 73 | test('stops at ten open positions without deleting existing history',()=>{ |
| 74 | let w=createPaperWorkspace(); |
| 75 | for(let i=0;i<10;i++)w=addPaperPosition(w,options('XAU_USD',i)); |
| 76 | assert.equal(paperWorkspaceSummary(w).open,10); |
| 77 | assert.throws(()=>addPaperPosition(w,options('XAU_USD',10)),/maximum 10/); |
| 78 | assert.equal(w.positions.length,10); |
| 79 | }); |
| 80 | test('rejects missing identifiers and nonmonotonic timestamps',()=>{ |
| 81 | let w=one(); |
| 82 | assert.throws(()=>markPaperWorkspacePosition(w,'P9999',{price:101,at:stamp(1)}),/not found/); |
| 83 | assert.throws(()=>markPaperWorkspacePosition(w,id(w),{price:101,at:stamp(0)}),/increase/); |
| 84 | }); |
| 85 | test('CSV export is scoped to manual events and contains no broker claims',()=>{ |
| 86 | let w=one();w=markPaperWorkspacePosition(w,id(w),{price:102,at:stamp(1)}); |
| 87 | const csv=exportPaperWorkspaceCSV(w); |
| 88 | assert.match(csv,/XAU_USD/);assert.match(csv,/TAKE_PROFIT/); |
| 89 | assert.doesNotMatch(csv,/BROKER|EXECUTED_ORDER/); |
| 90 | }); |
| 91 | test('workspace snapshots have no mutable link back to earlier states',()=>{ |
| 92 | let w=one(); |
| 93 | const prev=w; |
| 94 | w=markPaperWorkspacePosition(w,id(w),{price:101,at:stamp(1)}); |
| 95 | assert.equal(prev.positions[0].position.events.length,1); |
| 96 | assert.equal(w.positions[0].position.events.length,2); |
| 97 | }); |