from __future__ import annotations

import math
import unittest

from backend.data.numbers import finite_number, non_nan_number
from backend.data.providers import tushare_client
from backend.features.market import insights
from backend.features.screener import engine as screener_engine
from backend.features.sentiment import engine as sentiment_engine


class NumericNormalizationTests(unittest.TestCase):
    def test_consumers_use_their_declared_shared_policy(self) -> None:
        self.assertIs(tushare_client._number, finite_number)
        self.assertIs(screener_engine._number, finite_number)
        self.assertIs(insights._number, non_nan_number)
        self.assertIs(sentiment_engine._number, non_nan_number)

    def test_finite_policy_preserves_existing_results(self) -> None:
        self.assertEqual(finite_number("12.5"), 12.5)
        self.assertEqual(finite_number(None), 0.0)
        self.assertEqual(finite_number("invalid", 7.0), 7.0)
        self.assertEqual(finite_number(math.nan, 7.0), 7.0)
        self.assertEqual(finite_number(math.inf, 7.0), 7.0)
        self.assertEqual(finite_number(-math.inf, 7.0), 7.0)

    def test_non_nan_policy_keeps_infinity_but_rejects_nan(self) -> None:
        self.assertEqual(non_nan_number("12.5"), 12.5)
        self.assertEqual(non_nan_number(None), 0.0)
        self.assertEqual(non_nan_number("invalid", 7.0), 7.0)
        self.assertEqual(non_nan_number(math.nan, 7.0), 7.0)
        self.assertEqual(non_nan_number(math.inf, 7.0), math.inf)
        self.assertEqual(non_nan_number(-math.inf, 7.0), -math.inf)


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