"""Framing, byte for byte, against docs/CONTRACT.md. The contract is unusually specific about the length prefix ("plain ASCII digits; no leading zeros, no `+`, no spaces"), so these tests assert on exact bytes rather than on round-trips alone -- a reader and writer that agree with each other but not with the document would pass a round-trip test. """ from __future__ import annotations import io import json import pytest from hhstream import protocol # -- Hello ---------------------------------------------------------------- def test_hello_is_one_json_line(): line = protocol.hello(1280, 720, "image/png", 30) assert isinstance(line, bytes) assert line.endswith(b"\n") assert line.count(b"\n") == 1, "the Hello is exactly one line" def test_hello_fields_match_the_contract(): obj = json.loads(protocol.hello(1280, 720, "image/png", 30).decode("utf-8")) assert obj["magic"] == "HHS1" assert obj["width"] == 1280 assert obj["height"] == 720 assert obj["format"] == "image/png" assert obj["fps"] == 30 def test_hello_carries_the_jpeg_format_too(): obj = json.loads(protocol.hello(640, 480, "image/jpeg", 15).decode("utf-8")) assert obj["format"] == "image/jpeg" def test_hello_round_trips_through_parse_hello(): obj = protocol.parse_hello(protocol.hello(1920, 1080, "image/png", 60)) assert obj["magic"] == "HHS1" assert (obj["width"], obj["height"]) == (1920, 1080) assert obj["fps"] == 60 def test_parse_hello_tolerates_the_trailing_newline_or_not(): line = protocol.hello(800, 600, "image/png", 24) assert protocol.parse_hello(line) == protocol.parse_hello(line.rstrip(b"\n")) def test_parse_hello_rejects_a_foreign_magic(rejects): bad = json.dumps( {"magic": "HHS0", "width": 1, "height": 1, "format": "image/png", "fps": 1} ).encode() with rejects(): protocol.parse_hello(bad) @pytest.mark.parametrize("missing", ["width", "height", "format", "fps"]) def test_parse_hello_rejects_a_missing_field(missing, rejects): obj = { "magic": "HHS1", "width": 1280, "height": 720, "format": "image/png", "fps": 30, } del obj[missing] with rejects(): protocol.parse_hello(json.dumps(obj).encode()) @pytest.mark.parametrize("junk", [b"", b"not json", b"[]", b"null", b'{"magic":"HHS1"']) def test_parse_hello_rejects_junk(junk, rejects): with rejects(): protocol.parse_hello(junk) # -- frames --------------------------------------------------------------- def test_frame_prefixes_the_payload_length_in_ascii(): assert protocol.frame(b"hello") == b"5\n" + b"hello" def test_frame_length_counts_the_payload_only(): payload = b"\x00" * 1234 framed = protocol.frame(payload) prefix, _, body = framed.partition(b"\n") assert prefix == b"1234" assert int(prefix) == len(payload) == len(body) def test_frame_length_has_no_leading_zeros_sign_or_spaces(): for n in (1, 7, 10, 99, 100, 65536): prefix = protocol.frame(b"x" * n).split(b"\n", 1)[0] assert prefix == str(n).encode() assert not prefix.startswith(b"0") assert not prefix.startswith(b"+") assert b" " not in prefix def test_frame_is_binary_safe(): payload = bytes(range(256)) + b"\n42\n" + b"\xff\xfe" framed = protocol.frame(payload) assert framed == str(len(payload)).encode() + b"\n" + payload def test_read_frame_round_trips(): payload = bytes(range(256)) * 8 assert protocol.read_frame(io.BytesIO(protocol.frame(payload))) == payload def test_read_frame_reads_exactly_one_frame_and_stops(): stream = io.BytesIO(protocol.frame(b"aaa") + protocol.frame(b"bbbb")) assert protocol.read_frame(stream) == b"aaa" assert protocol.read_frame(stream) == b"bbbb" assert protocol.read_frame(stream) is None def test_read_frame_returns_none_on_a_clean_eof(): assert protocol.read_frame(io.BytesIO(b"")) is None def test_read_frame_raises_on_a_truncated_payload(rejects): with rejects(EOFError): protocol.read_frame(io.BytesIO(b"10\nshort")) @pytest.mark.parametrize( "bad", [ b"007\n0000000", # leading zeros b"+5\nhello", # sign b" 5\nhello", # leading space b"5 \nhello", # trailing space b"abc\nhello", # not a number b"-1\n", # negative b"1_0\nxxxxxxxxxx", # python's int() accepts this; the contract does not ], ) def test_read_frame_rejects_a_malformed_length_prefix(bad, rejects): with rejects(ValueError): protocol.read_frame(io.BytesIO(bad)) def test_read_frame_handles_a_megabyte_payload(): payload = bytes(range(256)) * 4096 assert len(payload) == 1024 * 1024 assert protocol.read_frame(io.BytesIO(protocol.frame(payload))) == payload def test_read_hello_then_frames_is_the_contract_sequence(): wire = ( protocol.hello(1280, 720, "image/png", 30) + protocol.frame(b"one") + protocol.frame(b"two") ) stream = io.BytesIO(wire) hello = protocol.read_hello(stream) assert hello["magic"] == "HHS1" assert (hello["width"], hello["height"]) == (1280, 720) assert protocol.read_frame(stream) == b"one" assert protocol.read_frame(stream) == b"two" assert protocol.read_frame(stream) is None