import { describe, expect, it } from 'vitest';
import { buildInsightCards, parseSymbolsInput, toDateInputValue } from './utils';
import type { BacktestResult } from './types';

const mockResult: BacktestResult = {
    mode: 'backtest',
    trades: [],
    summary: {
        netPnlUsd: 0,
        winRate: 0,
        maxDrawdownPct: 0,
        sharpe: 0,
        totalTrades: 0
    },
    timeline: [],
    window: {
        fromTimestamp: 1700000000000,
        toTimestamp: 1700003600000,
        fromIso: '2023-11-14T22:13:20.000Z',
        toIso: '2023-11-14T23:13:20.000Z',
        timezone: 'UTC',
        includesWarmupCandles: true,
        endOfWindowPolicy: 'OPEN_AT_END'
    },
    warmup: {
        startTimestamp: 1700000000000,
        endTimestamp: 1700003600000,
        candlesRequired: { '15m': 10, '1h': 20, '4h': 30 },
        signalsIgnored: 2
    },
    openPositionsAtEnd: [],
    assumptions: {
        fillModel: 'market@candle_close',
        slippageModel: 'deterministic',
        feeModel: 'deterministic',
        latencyModel: 'signal@bar_close -> fill@next_bar_open',
        intraCandleModel: 'ohlc_path',
        triggerResolution: '1m trigger candles',
        warmupEnforced: true,
        deterministicReplay: true,
        replayWindow: '2023-11-14 -> 2023-11-14 (UTC)',
        endOfWindowPolicy: 'OPEN_AT_END',
        disclaimer: 'Backtests are not live guarantees'
    },
    diagnostics: {
        blockedRuleCounts: { TrendBiasRule: 5 },
        entryRejections: { INSUFFICIENT_CAPITAL: 2 },
        lossReasons: { STOP_LOSS: 3 },
        connectorCalls: { fetchOHLCV: 10, placeOrder: 0, getPosition: 0 }
    }
};

describe('backtest utils', () => {
    it('parses symbols deterministically', () => {
        expect(parseSymbolsInput(' btc/usdt, ETH/USDT ,, ')).toEqual(['BTC/USDT', 'ETH/USDT']);
    });

    it('formats date input values', () => {
        expect(toDateInputValue(Date.UTC(2025, 0, 2))).toBe('2025-01-02');
    });

    it('builds insight cards from diagnostics', () => {
        const cards = buildInsightCards(mockResult);
        expect(cards[0].value).toContain('STOP_LOSS');
        expect(cards[1].value).toContain('TrendBiasRule');
        expect(cards[2].value).toContain('INSUFFICIENT_CAPITAL');
    });
});
