test_thermal.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. import pytest
  3. from hth_sdk.generated.thermal.contract import CONTRACT_SHA256
  4. from hth_sdk.thermal import ThermalHTH, _validate_binding, decode_snapshot
  5. class Backend:
  6. def __init__(self):
  7. self.targets = []
  8. self.stimuli = []
  9. def set_target(self, temperature_c):
  10. self.targets.append(temperature_c)
  11. def get_snapshot_payload(self):
  12. return bytes(193)
  13. def run_stimulus(self, duration_seconds, ptc, front, rear):
  14. self.stimuli.append((duration_seconds, list(ptc), list(front), list(rear)))
  15. def test_target_stop_and_snapshot() -> None:
  16. backend = Backend()
  17. hth = ThermalHTH(backend)
  18. hth.set_target(70)
  19. hth.stop()
  20. assert backend.targets == [70, 0]
  21. assert hth.get_snapshot().decoded["bmc_raw"] == [0] * 24
  22. def test_production_rejects_stimulus() -> None:
  23. with pytest.raises(NotImplementedError):
  24. ThermalHTH(Backend()).run_stimulus(1, [0] * 24, [0] * 4, [0] * 4)
  25. def test_identification_validates_and_runs_stimulus() -> None:
  26. backend = Backend()
  27. hth = ThermalHTH(backend, firmware_profile="6400-identification")
  28. hth.run_stimulus(2, [1] * 24, [20] * 4, [30] * 4)
  29. assert backend.stimuli[0][0] == 2
  30. def test_snapshot_length_is_exact() -> None:
  31. with pytest.raises(ValueError):
  32. decode_snapshot(bytes(192))
  33. @pytest.mark.parametrize("profile", ["production", "identification", "unknown"])
  34. def test_profile_aliases_and_unknown_are_rejected(profile: str) -> None:
  35. with pytest.raises(ValueError):
  36. ThermalHTH(Backend(), firmware_profile=profile)
  37. def test_profile_and_contract_binding_is_strict() -> None:
  38. _validate_binding("6400-control", "6400-control", CONTRACT_SHA256)
  39. with pytest.raises(RuntimeError, match="profile mismatch"):
  40. _validate_binding("6400-control", "6400-identification", CONTRACT_SHA256)
  41. with pytest.raises(RuntimeError, match="contract hash mismatch"):
  42. _validate_binding("6400-control", "6400-control", "0" * 64)