"""Tests for the shared format_trip_time utility."""

import locale

from meshy.utils import format_trip_time

NNBSP = " "


class TestFormatTripTime:
    def test_none_returns_empty(self):
        assert format_trip_time(None) == ""

    def test_zero_returns_empty(self):
        assert format_trip_time(0) == ""

    def test_sub_second(self):
        result = format_trip_time(450)
        assert result.endswith(NNBSP + "s")
        num = result.removesuffix(NNBSP + "s")
        assert float(num.replace(",", ".")) == 0.4 or float(num.replace(",", ".")) == 0.5

    def test_one_second(self):
        result = format_trip_time(1000)
        num = result.removesuffix(NNBSP + "s")
        assert float(num.replace(",", ".")) == 1.0

    def test_multi_second(self):
        result = format_trip_time(2500)
        num = result.removesuffix(NNBSP + "s")
        assert float(num.replace(",", ".")) == 2.5

    def test_large_value(self):
        result = format_trip_time(15000)
        num = result.removesuffix(NNBSP + "s")
        assert float(num.replace(",", ".")) == 15.0

    def test_small_value(self):
        result = format_trip_time(50)
        assert result.endswith(NNBSP + "s")
        num = result.removesuffix(NNBSP + "s")
        assert float(num.replace(",", ".")) == 0.1 or float(num.replace(",", ".")) == 0.0

    def test_narrow_no_break_space_before_unit(self):
        result = format_trip_time(1000)
        assert NNBSP + "s" in result

    def test_one_decimal_place(self):
        result = format_trip_time(3000)
        num = result.removesuffix(NNBSP + "s")
        sep = locale.localeconv()["decimal_point"]
        parts = num.split(sep)
        assert len(parts) == 2
        assert len(parts[1]) == 1
