#!/usr/bin/env python3 """Verify the hand-rolled FlatBuffers encoder against the reference runtime. The C code in native/src/net/flatbuf.c builds Hyperion protocol messages without flatcc. This decodes those bytes using the upstream `flatbuffers` Python package, so a layout mistake fails here rather than silently producing a message HyperHDR drops on the floor. Schema (hyperion.ng libsrc/flatbufserver/hyperion_request.fbs): table Register { origin:string (required); priority:int; } table RawImage { data:[ubyte]; width:int = -1; height:int = -1; } table Image { data:ImageType (required); duration:int = -1; } table Clear { priority:int; } union ImageType { RawImage, NV12Image } // RawImage = 1 union Command { Color, Image, Clear, Register } // Image = 2, Register = 4 table Request { command:Command (required); } root_type Request; """ import struct import subprocess import sys import tempfile from pathlib import Path from flatbuffers import number_types as N from flatbuffers.table import Table CMD_IMAGE = 2 CMD_REGISTER = 4 IMGTYPE_RAWIMAGE = 1 FAILURES = [] def check(label, actual, expected): ok = actual == expected status = "ok " if ok else "FAIL" shown = actual if not isinstance(actual, (bytes, bytearray)) else bytes(actual).hex() exp = expected if not isinstance(expected, (bytes, bytearray)) else bytes(expected).hex() print(f" [{status}] {label}: {shown!r}" + ("" if ok else f" (expected {exp!r})")) if not ok: FAILURES.append(label) def unframe(raw: bytes) -> bytes: """Strip and validate the 4-byte big-endian length prefix.""" assert len(raw) >= 4, "message shorter than its length prefix" (declared,) = struct.unpack(">I", raw[:4]) check("length prefix matches payload", declared, len(raw) - 4) return raw[4:] def root_table(payload: bytes) -> Table: pos = struct.unpack_from("