from datetime import datetime, timezone

from pipelet_ocpp_broker import ensure_utc_timestamp


def test_ensure_utc_timestamp_strips_trailing_milliseconds():
    assert (
        ensure_utc_timestamp("2025-10-03T10:38:35.000Z")
        == "2025-10-03T10:38:35Z"
    )


def test_ensure_utc_timestamp_preserves_non_zero_fraction():
    dt = datetime(2025, 10, 3, 10, 38, 35, 120000, tzinfo=timezone.utc)
    assert ensure_utc_timestamp(dt) == "2025-10-03T10:38:35.120Z"


def test_ensure_utc_timestamp_handles_blank_values():
    assert ensure_utc_timestamp(None) is None
    assert ensure_utc_timestamp("   ") is None


def test_ensure_utc_timestamp_keeps_unparseable_strings():
    value = "not-a-timestamp"
    assert ensure_utc_timestamp(value) == value


