from __future__ import annotations import pytest from hth_sdk.generated.thermal.contract import CONTRACT_SHA256 from hth_sdk.thermal import ThermalHTH, _validate_binding, decode_snapshot class Backend: def __init__(self): self.targets = [] self.stimuli = [] def set_target(self, temperature_c): self.targets.append(temperature_c) def get_snapshot_payload(self): return bytes(193) def run_stimulus(self, duration_seconds, ptc, front, rear): self.stimuli.append((duration_seconds, list(ptc), list(front), list(rear))) def test_target_stop_and_snapshot() -> None: backend = Backend() hth = ThermalHTH(backend) hth.set_target(70) hth.stop() assert backend.targets == [70, 0] assert hth.get_snapshot().decoded["bmc_raw"] == [0] * 24 def test_production_rejects_stimulus() -> None: with pytest.raises(NotImplementedError): ThermalHTH(Backend()).run_stimulus(1, [0] * 24, [0] * 4, [0] * 4) def test_identification_validates_and_runs_stimulus() -> None: backend = Backend() hth = ThermalHTH(backend, firmware_profile="6400-identification") hth.run_stimulus(2, [1] * 24, [20] * 4, [30] * 4) assert backend.stimuli[0][0] == 2 def test_snapshot_length_is_exact() -> None: with pytest.raises(ValueError): decode_snapshot(bytes(192)) @pytest.mark.parametrize("profile", ["production", "identification", "unknown"]) def test_profile_aliases_and_unknown_are_rejected(profile: str) -> None: with pytest.raises(ValueError): ThermalHTH(Backend(), firmware_profile=profile) def test_profile_and_contract_binding_is_strict() -> None: _validate_binding("6400-control", "6400-control", CONTRACT_SHA256) with pytest.raises(RuntimeError, match="profile mismatch"): _validate_binding("6400-control", "6400-identification", CONTRACT_SHA256) with pytest.raises(RuntimeError, match="contract hash mismatch"): _validate_binding("6400-control", "6400-control", "0" * 64)