from __future__ import annotations

from mgf.tele.core import DecodedAvlRecord, GeoPoint, ReceivedPacket


def test_geopoint_alt_optional() -> None:
    assert GeoPoint(lon=44.36, lat=33.31).alt is None
    assert GeoPoint(lon=1.0, lat=2.0, alt=300.0).alt == 300.0


def test_received_packet_defaults(record: DecodedAvlRecord) -> None:
    pkt = ReceivedPacket(
        raw_bytes=b"\x00\x01",
        transport="tcp",
        codec_id=8,
        parse_status="ok",
        records=[record],
    )
    assert pkt.parse_error is None
    assert pkt.records == [record]


def test_received_packet_parse_failure_carries_bytes_no_records() -> None:
    pkt = ReceivedPacket(
        raw_bytes=b"\xde\xad",
        transport="tcp",
        codec_id=0,
        parse_status="malformed",
        parse_error="bad preamble",
    )
    # Forensic invariant: bytes preserved, no records, error explained.
    assert pkt.raw_bytes == b"\xde\xad"
    assert pkt.records == []
    assert pkt.parse_error == "bad preamble"


def test_frames_are_frozen(record: DecodedAvlRecord) -> None:
    import dataclasses

    import pytest

    with pytest.raises(dataclasses.FrozenInstanceError):
        record.speed_kmh = 99.0  # type: ignore[misc]
