import { expect, test } from 'bun:test'; import { createElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import type { BacktestRunResult } from '@stock-bot/types'; import { CompactPerformanceMetrics } from '../../apps/stock/web-app/src/features/backtest/components/CompactPerformanceMetrics'; const metrics = { totalReturn: 0, sharpeRatio: null, maxDrawdown: 0, winRate: 0, totalTrades: 0, profitableTrades: 0, profitFactor: null, calmarRatio: null, sortinoRatio: null, avgWin: 0, avgLoss: 0, }; const text = (html: string) => html.replace(/<[^>]*>/g, ''); test('compact metrics preserve null ratios and render a genuine zero profit', () => { const result: BacktestRunResult = { runId: 'fixture', backtestId: 'fixture', engine: 'typescript', status: 'completed', completedAt: '2024-02-01T00:00:00Z', config: { name: 'fixture', strategy: 'sma', symbols: ['A.US'], startDate: '2024-01-01T00:00:00Z', endDate: '2024-02-01T00:00:00Z', initialCapital: 1000, commission: 0, slippage: 0, dataFrequency: '1d', parameters: {}, }, metrics: { ...metrics, totalProfit: 0 }, equity: [], ohlcData: {}, positions: [], trades: [], analytics: { exposureTime: 0 }, executionTime: 0, }; const rendered = text( renderToStaticMarkup(createElement(CompactPerformanceMetrics, { result, isLoading: false })) ); expect(rendered).toContain('Sharpe Ratio-'); expect(rendered).toContain('Profit Factor-'); expect(rendered).toContain('$0.00'); expect(rendered).not.toMatch(/Infinity|NaN/); });