from unittest import TestCase

from lib.hub.hub_options_funds_lib import (
    merge_board_row_balances,
    merge_perp_options_balances,
    options_balances_usdt_equiv,
)


class HubOptionsFundsLibTests(TestCase):
    def test_options_balances_usdt_equiv(self):
        snap = {
            "ok": True,
            "enabled": True,
            "balances": {"funding_usdc": 10, "trading_usdt": 5, "trading_usdc": 2},
        }
        out = options_balances_usdt_equiv(snap)
        self.assertTrue(out["ok"])
        self.assertEqual(out["funding_usdt"], 10.0)
        self.assertEqual(out["trading_usdt"], 7.0)

    def test_merge_perp_options_balances(self):
        out = merge_perp_options_balances(
            100,
            50,
            {
                "ok": True,
                "enabled": True,
                "balances": {"funding_usdc": 8, "trading_usdc": 4},
            },
        )
        self.assertEqual(out["funding_usdt"], 108.0)
        self.assertEqual(out["trading_usdt"], 54.0)
        self.assertEqual(out["total_usdt"], 162.0)

    def test_merge_board_row_balances(self):
        row = {
            "account_ok": True,
            "funding_usdt": 20,
            "trading_usdt": 30,
            "capabilities": ["options"],
            "options": {
                "ok": True,
                "enabled": True,
                "balances": {"funding_usdc": 1, "trading_usdc": 2},
                "positions": [{"inst_id": "X"}],
                "upl_total_usdc": 0.5,
            },
        }
        out = merge_board_row_balances(row)
        self.assertEqual(out["total_usdt"], 53.0)
        self.assertEqual(out["options_open_position_count"], 1)
        self.assertEqual(out["options_float_pnl_u"], 0.5)
