"""Curated candidate-count expectation bound to board-data/build-info digests.""" from __future__ import annotations import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parents[2] GENERATED = ROOT / "tools" / "board-viewer" / "generated" EXPECTATION = Path(__file__).resolve().parent / "fixtures" / "candidate_count_expectation.json" REQUIRED_CAPABILITIES = ( "gpio", "pwm", "gpio_output", "gpio_input", "tach", "presence", "uart_tx", ) REQUIRED_BLOCKERS = { "unverified_or_non_cross_verified", "power", "ground", "reserved", "fixed_load_conflict", } def _sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def test_candidate_count_expectation_bound_to_generated_hashes() -> None: expectation = json.loads(EXPECTATION.read_text(encoding="utf-8")) board_data = GENERATED / "board-data.json" build_info = GENERATED / "build-info.json" assert expectation["schema"] == "prod2u-board-viewer-candidate-count-expectation-v1" assert board_data.is_file(), "generated board-data.json required for hash-bound expectation" assert build_info.is_file(), "generated build-info.json required for hash-bound expectation" board_data_sha = _sha256(board_data) build_info_sha = _sha256(build_info) build_info_payload = json.loads(build_info.read_text(encoding="utf-8")) bound = expectation["bound_to"] assert board_data_sha == bound["board_data_sha256"] assert build_info_sha == bound["build_info_sha256"] assert board_data_sha == bound["board_data_asset_sha256"] assert board_data_sha == build_info_payload["assets"]["board-data.json"]["sha256"] assert bound["brd_sha256"] == build_info_payload["sources"]["brd"]["sha256"] counts = expectation["candidate_counts"] assert set(REQUIRED_CAPABILITIES).issubset(counts) for capability in REQUIRED_CAPABILITIES: assert isinstance(counts[capability], int) assert counts[capability] >= 0 keys = expectation.get("candidate_keys", {}).get(capability) assert isinstance(keys, list) assert len(keys) == counts[capability] assert REQUIRED_BLOCKERS.issubset(set(expectation["blockers_must_remain"]))