from unittest import TestCase
from unittest.mock import MagicMock

from lib.options.options_hub_lib import build_options_hub_snapshot


class OptionsHubLibTests(TestCase):
    def test_build_options_hub_snapshot_disabled(self):
        out = build_options_hub_snapshot({"enabled": False})
        self.assertFalse(out["enabled"])
        self.assertTrue(out["ok"])

    def test_build_options_hub_snapshot_positions(self):
        cfg = {
            "enabled": True,
            "exchange_options": object(),
            "options_api_ready": lambda ex: (True, ""),
            "fetch_option_positions": lambda ex: [
                {"instId": "ETH-USD_UM-260703-1800-C", "pos": "2", "upl": "1.5", "markPx": "0.1"}
            ],
            "format_position_row": lambda p: {
                "inst_id": p.get("instId"),
                "pos": 2,
                "upl": 1.5,
                "mark_px": 0.1,
            },
            "fetch_options_balances": lambda ex: {"trading_usdc": 9.5, "funding_usdc": 12.0},
            "get_db": MagicMock(),
            "trade_budget": 10,
            "account_label": "OKX期权",
        }
        out = build_options_hub_snapshot(cfg)
        self.assertTrue(out["ok"])
        self.assertEqual(out["position_count"], 1)
        self.assertEqual(out["upl_total_usdc"], 1.5)
        self.assertEqual(out["trading_usdc"], 9.5)
