121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
import { expect, test } from 'bun:test';
|
|
import { createElement } from 'react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import type { BacktestRunResult } from '@stock-bot/types';
|
|
import { BacktestMetrics } from '../../apps/stock/web-app/src/features/backtest/components/BacktestMetrics';
|
|
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('detailed metrics distinguish undefined ratios from legitimate zero return and exposure', () => {
|
|
const html = renderToStaticMarkup(
|
|
createElement(BacktestMetrics, {
|
|
isLoading: false,
|
|
result: {
|
|
id: 'fixture',
|
|
config: {
|
|
name: 'fixture',
|
|
startDate: new Date('2024-01-01'),
|
|
endDate: new Date('2024-02-01'),
|
|
initialCapital: 1000,
|
|
symbols: ['A.US'],
|
|
strategy: 'sma',
|
|
speedMultiplier: 0,
|
|
commission: 0,
|
|
slippage: 0,
|
|
},
|
|
metrics,
|
|
positions: [],
|
|
trades: [],
|
|
performanceData: [],
|
|
analytics: { exposureTime: 0 },
|
|
},
|
|
})
|
|
);
|
|
const rendered = text(html);
|
|
expect(rendered).toContain('Sharpe RatioN/A');
|
|
expect(rendered).toContain('Profit FactorN/A');
|
|
expect(rendered).toContain('Calmar RatioN/A');
|
|
expect(rendered).toContain('Sortino RatioN/A');
|
|
expect(rendered).toContain('Total Return0.00%');
|
|
expect(rendered).toContain('Exposure Time0.0%');
|
|
expect(rendered).not.toMatch(/Infinity|NaN/);
|
|
});
|
|
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/);
|
|
});
|
|
|
|
test('null_metrics: detailed exposure is unavailable rather than a measured zero', () => {
|
|
const rendered = text(
|
|
renderToStaticMarkup(
|
|
createElement(BacktestMetrics, {
|
|
isLoading: false,
|
|
result: {
|
|
id: 'fixture',
|
|
config: {
|
|
name: 'fixture',
|
|
startDate: new Date('2024-01-01'),
|
|
endDate: new Date('2024-02-01'),
|
|
initialCapital: 1000,
|
|
symbols: ['A.US'],
|
|
strategy: 'sma',
|
|
speedMultiplier: 0,
|
|
commission: 0,
|
|
slippage: 0,
|
|
},
|
|
metrics,
|
|
positions: [],
|
|
trades: [],
|
|
performanceData: [],
|
|
analytics: { exposureTime: null },
|
|
},
|
|
})
|
|
)
|
|
);
|
|
expect(rendered).toContain('Exposure TimeN/A');
|
|
expect(rendered).not.toContain('Exposure Time0.0%');
|
|
});
|