from importlib.util import module_from_spec, spec_from_file_location from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SPEC = spec_from_file_location("hex_gui", ROOT / "tools/6400_hex_gui.py") assert SPEC and SPEC.loader GUI = module_from_spec(SPEC) SPEC.loader.exec_module(GUI) def test_gui_uses_generated_contract_and_golden_vectors() -> None: assert GUI.CONTRACT["contract_sha256"] == GUI.GOLDEN["contract_sha256"] assert GUI.frame_hex(GUI.set_all([0] * 24 + [20] * 8)).replace(" ", "") == GUI.GOLDEN["frames"]["set_all_safe"] assert GUI.frame_hex(GUI.encode(2)).replace(" ", "") == GUI.GOLDEN["frames"]["get_snapshot"] assert GUI.frame_hex(GUI.encode(4, bytes((70,)))).replace(" ", "") == GUI.GOLDEN["frames"]["set_pid_target_70"] def test_gui_parses_unified_ack_and_rejects_bad_crc() -> None: ack = bytes.fromhex(GUI.GOLDEN["frames"]["set_all_ack"]) assert "status=OK" in GUI.describe(ack) damaged = ack[:-1] + bytes((ack[-1] ^ 1,)) try: GUI.decode(damaged) except ValueError as exc: assert "CRC mismatch" in str(exc) else: raise AssertionError("bad CRC accepted") def test_gui_has_no_serial_access_path() -> None: source = (ROOT / "tools/6400_hex_gui.py").read_text(encoding="utf-8") assert "import serial" not in source assert "Serial(" not in source assert "pyserial" not in source.lower()