import { describe, it, expect } from 'vitest';
import { buildHistoryLedger, aggregateHistoryLedger } from './tradeHistoryLedger';

describe('Trade History Ledger Diagnostics', () => {
    it('aggregates a simple row correctly', () => {
        const now = Date.now();
        const dbRows = [
            { id: 'h1', profile_id: 'p1', timestamp: now - 60000, pnl: 50, side: 'SELL', symbol: 'BTC', entry_price: 100, exit_price: 110, size: 5, reason: 'exit' }
        ];
        const ledger = buildHistoryLedger({ dbRows, includeRealtime: false });
        expect(ledger).toHaveLength(1);
        expect(ledger[0].pnl).toBe(50);

        const agg = aggregateHistoryLedger(ledger);
        expect(agg.totalPnl).toBe(50);
        expect(agg.byProfile['p1']).toBeDefined();
        expect(agg.byProfile['p1'].lastClosedTradeAt).toBe(ledger[0].timestampMs);
    });
});
