import test from 'node:test' import assert from 'node:assert/strict' import { decodeToken } from '@trumpsignal/core/crypto' import { protectiveStopPct, deriskLadder, isSystem2, isSupportedTradingSource, } from '@trumpsignal/core/signal-categories' import { buildMessage, bodyHash } from '@trumpsignal/core/signed-request' import { simulateExit } from '@trumpsignal/core/backtest' import { computeComposite } from '@trumpsignal/core/macro-scoring' // Fernet's published cross-language verification vector, not generated by this implementation. // https://github.com/fernet/spec/blob/master/verify.json const token = 'gAAAAAAdwJ6wAAECAwQFBgcICQoLDA0ODy021cpGVWKZ_eEwCGM4BLLF_5CV9dOPmrhuVUPgJobwOz7JcbmrR64jVmpU4IwqDA==' const secret = Buffer.from('cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=', 'base64url') test('reads the external Fernet verification vector and rejects tampering', () => { assert.equal(decodeToken(token, secret), 'hello') const raw = Buffer.from(token, 'base64url') raw[30] ^= 1 assert.throws(() => decodeToken(raw.toString('base64url'), secret)) }) test('System 2 remains distinct from Trump and funding is alert-only', () => { assert.equal(isSystem2('truth'), false) assert.equal(isSystem2('btc_bottom_reversal'), true) assert.equal(isSupportedTradingSource('funding_reversal'), false) assert.equal(isSupportedTradingSource('unknown'), false) }) test('actual leverage sets the final de-risk level inside liquidation', () => { assert.equal(protectiveStopPct(2), 35) assert.equal(protectiveStopPct(10), 8.5) assert.deepEqual(deriskLadder(10, 'aggressive'), [ [-5.1, 0.25, false], [-6.8, 0.25, false], [-8.5, 0.5, true], ]) }) test('signed bodies cannot be reordered or substituted', () => { assert.equal(bodyHash({ b: 2, a: 1 }), bodyHash({ a: 1, b: 2 })) assert.notEqual(bodyHash({ trade_id: 1 }), bodyHash({ trade_id: 2 })) assert.equal( buildMessage('view_user', '0xABC', 123, null), 'TrumpSignal ยท view_user\nwallet: 0xabc\ntimestamp: 123\nbody: -', ) }) test('a bar hitting stop and take-profit takes the conservative stop path', () => { const result = simulateExit([{ time_ms: 0, open: 100, high: 110, low: 90, close: 105 }], 'long', { stop_loss_pct: 1.5, take_profit_pct: 2, trailing_stop_pct: null, trailing_activate_at_pct: null, max_hold_hours: 6, }) assert.equal(result.reason, 'stop_loss') assert.equal(result.pnl_pct, -1.5) }) test('missing macro data is excluded, not silently treated as neutral', () => { assert.deepEqual(computeComposite({}), [null, null]) assert.deepEqual(computeComposite({ fear_greed: 0, btc_open_interest_usd: 1e12 }), [100, 'BULL']) })