"""Tests for the v2 wire format encoder/decoder."""

from quicproquo.wire import (
    HEADER_SIZE,
    encode_frame,
    decode_header,
    HEALTH,
    ENQUEUE,
)


def test_header_size():
    assert HEADER_SIZE == 10


def test_encode_decode_roundtrip():
    payload = b"\x0a\x05hello"
    frame = encode_frame(HEALTH, 42, payload)

    assert len(frame) == HEADER_SIZE + len(payload)

    method_id, req_id, length = decode_header(frame)
    assert method_id == HEALTH
    assert req_id == 42
    assert length == len(payload)
    assert frame[HEADER_SIZE:] == payload


def test_empty_payload():
    frame = encode_frame(ENQUEUE, 1, b"")
    method_id, req_id, length = decode_header(frame)
    assert method_id == ENQUEUE
    assert req_id == 1
    assert length == 0


def test_decode_header_too_short():
    import pytest

    with pytest.raises(ValueError, match="header too short"):
        decode_header(b"\x00\x01")
