from lib.hub.hub_monitor_totals_lib import aggregate_monitor_board_totals


def test_aggregate_monitor_board_totals_sums_rows():
    rows = [
        {
            "day_stats": {
                "ok": True,
                "opens_today": 2,
                "trade_stats": {
                    "closed_count": 1,
                    "win_count": 1,
                    "loss_count": 0,
                    "win_pnl_u": 5.5,
                    "loss_pnl_u": 0,
                },
            },
            "agent": {"positions": [{"contracts": 1}], "total_unrealized_pnl": 1.2},
        },
        {
            "day_stats": {
                "ok": True,
                "opens_today": 1,
                "trade_stats": {
                    "closed_count": 2,
                    "win_count": 0,
                    "loss_count": 2,
                    "win_pnl_u": 0,
                    "loss_pnl_u": -3.0,
                },
            },
            "agent": {"positions": [], "total_unrealized_pnl": 0},
        },
    ]
    out = aggregate_monitor_board_totals(rows, trading_day="2026-07-04", reset_hour=8)
    assert out["open_count"] == 3
    assert out["closed_count"] == 3
    assert out["win_count"] == 1
    assert out["loss_count"] == 2
    assert out["win_pnl_u"] == 5.5
    assert out["loss_pnl_u"] == -3.0
    assert out["open_position_count"] == 1
    assert out["float_pnl_u"] == 1.2


def test_aggregate_monitor_board_totals_includes_options():
    rows = [
        {
            "capabilities": ["options"],
            "options": {
                "ok": True,
                "enabled": True,
                "positions": [{"inst_id": "X"}, {"inst_id": "Y"}],
                "upl_total_usdc": 1.5,
            },
            "agent": {"positions": [], "total_unrealized_pnl": 0},
        }
    ]
    out = aggregate_monitor_board_totals(rows, trading_day="2026-07-04", reset_hour=8)
    assert out["options_open_position_count"] == 2
    assert out["open_position_count"] == 2
    assert out["options_float_pnl_u"] == 1.5
    assert out["float_pnl_u"] == 1.5


def test_summarize_trades_win_loss_amounts():
    from lib.hub.hub_trades_lib import summarize_trades

    stats = summarize_trades(
        [{"pnl_amount": 2.5}, {"pnl_amount": -1.0}, {"pnl_amount": 0}]
    )
    assert stats["win_count"] == 1
    assert stats["loss_count"] == 1
    assert stats["win_pnl_u"] == 2.5
    assert stats["loss_pnl_u"] == -1.0
