test_candidate_count_expectation.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Curated candidate-count expectation bound to board-data/build-info digests."""
  2. from __future__ import annotations
  3. import hashlib
  4. import json
  5. from pathlib import Path
  6. ROOT = Path(__file__).resolve().parents[2]
  7. GENERATED = ROOT / "tools" / "board-viewer" / "generated"
  8. EXPECTATION = Path(__file__).resolve().parent / "fixtures" / "candidate_count_expectation.json"
  9. REQUIRED_CAPABILITIES = (
  10. "gpio",
  11. "pwm",
  12. "gpio_output",
  13. "gpio_input",
  14. "tach",
  15. "presence",
  16. "uart_tx",
  17. )
  18. REQUIRED_BLOCKERS = {
  19. "unverified_or_non_cross_verified",
  20. "power",
  21. "ground",
  22. "reserved",
  23. "fixed_load_conflict",
  24. }
  25. def _sha256(path: Path) -> str:
  26. return hashlib.sha256(path.read_bytes()).hexdigest()
  27. def test_candidate_count_expectation_bound_to_generated_hashes() -> None:
  28. expectation = json.loads(EXPECTATION.read_text(encoding="utf-8"))
  29. board_data = GENERATED / "board-data.json"
  30. build_info = GENERATED / "build-info.json"
  31. assert expectation["schema"] == "prod2u-board-viewer-candidate-count-expectation-v1"
  32. assert board_data.is_file(), "generated board-data.json required for hash-bound expectation"
  33. assert build_info.is_file(), "generated build-info.json required for hash-bound expectation"
  34. board_data_sha = _sha256(board_data)
  35. build_info_sha = _sha256(build_info)
  36. build_info_payload = json.loads(build_info.read_text(encoding="utf-8"))
  37. bound = expectation["bound_to"]
  38. assert board_data_sha == bound["board_data_sha256"]
  39. assert build_info_sha == bound["build_info_sha256"]
  40. assert board_data_sha == bound["board_data_asset_sha256"]
  41. assert board_data_sha == build_info_payload["assets"]["board-data.json"]["sha256"]
  42. assert bound["brd_sha256"] == build_info_payload["sources"]["brd"]["sha256"]
  43. counts = expectation["candidate_counts"]
  44. assert set(REQUIRED_CAPABILITIES).issubset(counts)
  45. for capability in REQUIRED_CAPABILITIES:
  46. assert isinstance(counts[capability], int)
  47. assert counts[capability] >= 0
  48. keys = expectation.get("candidate_keys", {}).get(capability)
  49. assert isinstance(keys, list)
  50. assert len(keys) == counts[capability]
  51. assert REQUIRED_BLOCKERS.issubset(set(expectation["blockers_must_remain"]))