"""Границы PHY, расчёт полезной скорости и отдельный компактный заголовок.""" from __future__ import annotations import pytest from protocol.video003_compact_header import HEADER_SIZE as COMPACT_SIZE from protocol.video003_compact_header import decode, encode from protocol.video_stream_transport import ( HEADER_SIZE as CURRENT_SIZE, FrameType, VideoFragment, VideoTransportError, ) from tools.video003_design import estimate, run def test_current_packet_limit_and_exact_max_video_fragment() -> None: result = run() assert result["max_full_phy_packet_bytes"] == 1024 assert result["current_fixed_lab_generator_phy_payload_bytes"] == 256 assert result["packet_header_bytes"] == 8 assert result["packet_crc_bytes"] == 4 assert result["video_header_bytes"] == 17 assert result["max_format_phy_payload_bytes"] == 1012 assert result["max_format_video_fragment_bytes"] == 995 assert len(result["rows"]) == 7 * 4 * 5 assert sorted(set(row["gap_ms"] for row in result["rows"])) == [0, 0.5, 1, 2, 5] with pytest.raises(ValueError, match="1024"): estimate("QPSK", 500_000, "3/4", 1013, 0) def test_maximum_does_not_reach_600k_even_without_video_header() -> None: result = run() best = result["best_calculated"] assert best["modulation"] == "QPSK" assert best["symbol_rate"] == 500_000 assert best["fec_rate"] == "3/4" assert best["full_packet_bytes"] == 1024 assert best["gap_ms"] == 0 assert best["useful_video_rate_bps"] == pytest.approx(574_148.8747836122) assert best["useful_video_rate_bps"] < 600_000 no_video_header = estimate("QPSK", 500_000, "3/4", 1012, 0, video_header_bytes=0) assert no_video_header.useful_video_rate_bps < 600_000 def test_compact_header_roundtrip_preserves_identity_age_and_reassembly_fields() -> None: assert CURRENT_SIZE == 17 and COMPACT_SIZE == 13 fragment = VideoFragment(0x56303032, 0xFFFFFFFE, 255, 256, FrameType.KEYFRAME, 299, b"video") wire = encode(fragment, 1012) assert len(wire) == COMPACT_SIZE + len(fragment.data) assert decode(wire, 1012) == fragment assert (CURRENT_SIZE - COMPACT_SIZE) / CURRENT_SIZE == pytest.approx(4 / 17) with pytest.raises(VideoTransportError): decode(bytes([0x12]) + wire[1:], 1012) with pytest.raises(VideoTransportError): decode(wire[:9], 1012) def test_compact_header_does_not_make_600k_reachable() -> None: row = estimate("QPSK", 500_000, "3/4", 1012, 0, video_header_bytes=COMPACT_SIZE) assert row.video_fragment_bytes == 999 assert row.useful_video_rate_bps == pytest.approx(576_457.0109636468) assert row.useful_video_rate_bps < 600_000