"""Tests for the fmt formatting module."""

from vibelexity import fmt


class TestFmtInt:
    def test_small_number(self):
        assert fmt.fmt_int(42) == "42"

    def test_thousands(self):
        assert fmt.fmt_int(200124) == "200,124"

    def test_millions(self):
        assert fmt.fmt_int(1234567) == "1,234,567"

    def test_zero(self):
        assert fmt.fmt_int(0) == "0"

    def test_float_input(self):
        assert fmt.fmt_int(200124.7) == "200,124"


class TestFmtFloat:
    def test_rounds_to_two(self):
        assert fmt.fmt_float(30.399999) == "30.40"

    def test_large_float(self):
        assert fmt.fmt_float(200124.07) == "200,124.07"

    def test_zero(self):
        assert fmt.fmt_float(0.0) == "0.00"

    def test_custom_decimals(self):
        assert fmt.fmt_float(0.02345, 4) == "0.0234"


class TestFmtNumber:
    def test_int(self):
        assert fmt.fmt_number(42) == "42"

    def test_int_large(self):
        assert fmt.fmt_number(200124) == "200,124"

    def test_float_whole(self):
        assert fmt.fmt_number(42.0) == "42"

    def test_float_fractional(self):
        assert fmt.fmt_number(30.4) == "30.40"


class TestColorDisabled:
    def test_header_plain(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.header("Test") == "Test"

    def test_label_plain(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.label("avg:") == "avg:"

    def test_num_plain(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.num("42") == "42"

    def test_path_plain(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.path("/some/file.py") == "/some/file.py"

    def test_func_name_plain(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.func_name("my_func") == "my_func"


class TestColorEnabled:
    def test_header_has_ansi(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", True)
        result = fmt.header("Test")
        assert "\033[" in result
        assert "Test" in result

    def test_num_has_ansi(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", True)
        result = fmt.num("42")
        assert "\033[" in result
        assert "42" in result


class TestRpad:
    def test_alignment_width(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        result = fmt.rpad(42, 10)
        assert len(result) == 10
        assert result == "        42"

    def test_alignment_with_thousands(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        result = fmt.rpad(200124, 10)
        assert result == "   200,124"

    def test_alignment_with_float(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        result = fmt.rpad(30.4, 10)
        assert result == "     30.40"

    def test_alignment_with_custom_formatter(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        result = fmt.rpad(42, 10, lambda x: "X")
        assert len(result) == 10
        assert result == "         X"


class TestFmtCompact:
    def test_small_numbers_no_compaction(self):
        assert fmt.fmt_compact(42) == "42"
        assert fmt.fmt_compact(999) == "999"
        assert fmt.fmt_compact(9999) == "9,999"
        assert fmt.fmt_compact(99999) == "99,999"

    def test_six_digit_numbers_compact(self):
        assert fmt.fmt_compact(100000) == "100 × 10³"
        assert fmt.fmt_compact(123456) == "123.5 × 10³"
        assert fmt.fmt_compact(999999) == "1000 × 10³"

    def test_millions(self):
        assert fmt.fmt_compact(1000000) == "∞"
        assert fmt.fmt_compact(1234567) == "∞"
        assert fmt.fmt_compact(987654321) == "∞"
        assert fmt.fmt_compact(1500000000) == "∞"

    def test_zero(self):
        assert fmt.fmt_compact(0) == "0"

    def test_float_input(self):
        assert fmt.fmt_compact(100000.7) == "100.0 × 10³"


class TestCompositeHelpers:
    def test_colored_num_int(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.colored_num(1000) == "1,000"

    def test_colored_float(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.colored_float(1234.5) == "1,234.50"

    def test_colored_float_custom_decimals(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.colored_float(0.02345, 4) == "0.0234"

    def test_colored_int(self, monkeypatch):
        monkeypatch.setattr(fmt, "_USE_COLOR", False)
        assert fmt.colored_int(1234567) == "1,234,567"
