test_6400_hex_gui_v2.py 1.4 KB

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