"""
Testy modułu billing — usd_to_sat z mocked rate.
"""

from __future__ import annotations

import os
import sys
import unittest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from mcp_nostr_explorer import billing  # noqa: E402


class TestUsdToSat(unittest.IsolatedAsyncioTestCase):

    async def asyncSetUp(self):
        # Sztywny kurs — żeby testy były deterministyczne
        billing.set_btc_usd_rate(80_000.0)

    async def asyncTearDown(self):
        billing.set_btc_usd_rate(None)

    async def test_kurs_z_cache_bez_pobrania(self):
        rate = await billing.get_btc_usd_rate()
        self.assertEqual(rate, 80_000.0)

    async def test_zero_usd_zwraca_zero_sat(self):
        self.assertEqual(await billing.usd_to_sat(0), 0)
        self.assertEqual(await billing.usd_to_sat(-1), 0)

    async def test_jeden_dolar_to_1250_sat(self):
        # 100M / 80k = 1250
        self.assertEqual(await billing.usd_to_sat(1.0), 1250)

    async def test_centówka_zaokraglona(self):
        # 0.01 USD * 1e8 / 80000 = 12.5 → round half-even = 12
        self.assertEqual(await billing.usd_to_sat(0.01), 12)

    async def test_drobny_koszt_payperq(self):
        # Realny przykład: claude-haiku tania tura ~$0.0014 → ~1.75 → 2 sat
        self.assertEqual(await billing.usd_to_sat(0.0014), 2)

    async def test_zmiana_kursu_zmienia_konwersje(self):
        billing.set_btc_usd_rate(40_000.0)
        # 1 USD * 1e8 / 40k = 2500
        self.assertEqual(await billing.usd_to_sat(1.0), 2500)


if __name__ == "__main__":
    unittest.main()
