"""期权净盈亏汇总与持仓卡口径一致."""
from unittest import TestCase
from unittest.mock import patch

from lib.options.options_positions_lib import net_pnl_from_display_row, sum_options_net_pnl_usdc


class OptionsNetPnlSumTests(TestCase):
    def test_net_pnl_from_display_row(self):
        self.assertEqual(
            net_pnl_from_display_row({"close_preview": {"estimated_pnl": -2.8}, "premium_paid": 4.95}),
            -2.8,
        )
        self.assertIsNone(
            net_pnl_from_display_row({"close_preview": {"bid_invalid": True, "estimated_pnl": -1}})
        )
        self.assertEqual(
            net_pnl_from_display_row(
                {"close_preview": {"total_received": 2.15}, "premium_paid": 4.95}
            ),
            round(2.15 - 4.95, 4),
        )

    @patch("lib.options.options_positions_lib.build_display_option_positions")
    def test_sum_options_net_pnl_usdc(self, mock_build):
        mock_build.return_value = [
            {"close_preview": {"estimated_pnl": -2.8}},
            {"close_preview": {"estimated_pnl": 1.0}},
            {"close_preview": {"bid_invalid": True, "estimated_pnl": 9}},
        ]
        cfg = {"fetch_option_positions": lambda ex: [{"instId": "X"}]}
        self.assertEqual(sum_options_net_pnl_usdc(cfg, object()), -1.8)
