// Tests for Derisking Engine (Slice 23) import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { DERISKING_STRATEGIES, suggestDerisking, type DeriskingInput, } from '../derisking/DeriskingEngine.ts'; describe('DeriskingEngine', () => { it('DERISKING_STRATEGIES: has exactly 6 strategies', () => { assert.equal(DERISKING_STRATEGIES.length, 6); }); it('DERISKING_STRATEGIES: all have Alfred framing', () => { for (const s of DERISKING_STRATEGIES) { assert.ok(s.alfredFraming.length > 0, `${s.id} missing Alfred framing`); } }); it('DERISKING_STRATEGIES: option collar requires options sleeve', () => { const collar = DERISKING_STRATEGIES.find((s) => s.id === 'option_protected_collar'); assert.ok(collar?.requiresOptionsSleeve); }); it('suggestDerisking: scale_out triggers when hitting profit target', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 120, avgCost: 100, shares: 100, profitTargets: [115, 130], }; const suggestions = suggestDerisking(input); const scaleOut = suggestions.find((s) => s.strategy.id === 'scale_out_at_targets'); assert.ok(scaleOut, 'scale_out should be suggested'); assert.ok(scaleOut.applicable); }); it('suggestDerisking: stop_trail triggers when in profit with EMA', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 120, avgCost: 100, shares: 100, ema21: 115, }; const suggestions = suggestDerisking(input); const stopTrail = suggestions.find((s) => s.strategy.id === 'stop_trail_ema21_50'); assert.ok(stopTrail?.applicable); }); it('suggestDerisking: thesis_based_partial triggers on weakening', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 100, avgCost: 90, shares: 100, thesisStatus: 'weakening', }; const suggestions = suggestDerisking(input); const partial = suggestions.find((s) => s.strategy.id === 'thesis_based_partial'); assert.ok(partial?.applicable); }); it('suggestDerisking: option_protected_collar gated by options unlock', () => { const inputLow: DeriskingInput = { symbol: 'AAPL', currentPrice: 120, avgCost: 100, shares: 100, optionsUnlockState: 0, }; assert.equal(suggestDerisking(inputLow).find((s) => s.strategy.id === 'option_protected_collar'), undefined); const inputHigh: DeriskingInput = { symbol: 'AAPL', currentPrice: 120, avgCost: 100, shares: 100, optionsUnlockState: 3, }; const suggestions = suggestDerisking(inputHigh); assert.ok(suggestions.find((s) => s.strategy.id === 'option_protected_collar')?.applicable); }); it('suggestDerisking: regime_cut triggers on trending-down', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 100, avgCost: 90, shares: 100, currentRegime: 'trending-down', }; const suggestions = suggestDerisking(input); assert.ok(suggestions.find((s) => s.strategy.id === 'regime_cut')?.applicable); }); it('suggestDerisking: correlation_driven triggers on high correlation', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 100, avgCost: 90, shares: 100, portfolioCorrelation: 0.85, }; const suggestions = suggestDerisking(input); assert.ok(suggestions.find((s) => s.strategy.id === 'correlation_driven')?.applicable); }); it('suggestDerisking: no suggestions when nothing applies', () => { const input: DeriskingInput = { symbol: 'AAPL', currentPrice: 90, avgCost: 100, shares: 100, }; const suggestions = suggestDerisking(input); assert.equal(suggestions.length, 0); }); });