"""Wire-protocol framing tests."""

from __future__ import annotations

from mgf.logship import (
    SHIP_PROTOCOL_VERSION,
    decode_ndjson,
    encode_ndjson,
)


def test_version_is_semver() -> None:
    assert SHIP_PROTOCOL_VERSION.count(".") == 2


def test_ndjson_round_trips() -> None:
    records = [
        {"ts": "t1", "level": "INFO", "logger": "app", "msg": "a"},
        {"ts": "t2", "level": "ERROR", "logger": "app.db", "msg": "b"},
    ]
    body = encode_ndjson(records)
    assert body.endswith(b"\n")
    assert decode_ndjson(body) == records


def test_decode_skips_malformed_and_blank() -> None:
    body = b'{"ts":"t","level":"INFO","logger":"x","msg":"ok"}\nNOT JSON\n\n[]\n'
    out = decode_ndjson(body)
    assert len(out) == 1
    assert out[0]["msg"] == "ok"


def test_encode_is_compact_utf8() -> None:
    body = encode_ndjson([{"msg": "héllo", "n": 1}]).decode("utf-8")
    assert ", " not in body and '": ' not in body  # compact separators
    assert "héllo" in body  # raw UTF-8, not escaped
