| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- #!/usr/bin/env python3
- """Static product-contract coverage gates for the active 11-byte SET_ALL profile.
- Authority is AGENTS.md / requirements / protocol docs — not current RTL behavior.
- These checks ensure the verification tree keeps the required contract assertions
- instead of silently weakening or deleting them.
- """
- from __future__ import annotations
- import json
- import re
- import unittest
- from pathlib import Path
- from tools.prod2u_simple_protocol import (
- FrameType,
- build_get_temperature,
- build_set_all,
- build_temperature_data,
- decode_frame,
- decode_temperature_data,
- )
- ROOT = Path(__file__).resolve().parents[2]
- MANIFEST_PATH = ROOT / "config" / "prod2u_p4_verification_targets_v1.json"
- def _read(relative: str) -> str:
- return (ROOT / relative).read_text(encoding="utf-8")
- class ProductContractCoverageTests(unittest.TestCase):
- def test_protocol_set_all_is_exactly_11_bytes_with_golden_crc(self):
- zero = bytes.fromhex("A55A010B00000000000000000000005873")
- mixed = bytes.fromhex("A55A010B0A141E280019326401020087D5")
- self.assertEqual(build_set_all([0] * 4, [0] * 4, 0, 0, 0), zero)
- self.assertEqual(build_set_all((10, 20, 30, 40), (0, 25, 50, 100), 1, 2, 0), mixed)
- self.assertEqual(len(decode_frame(zero).payload), 11)
- self.assertEqual(decode_frame(mixed).frame_type, FrameType.SET_ALL)
- def test_temperature_contract_is_exactly_27_values_and_33_byte_frame(self):
- self.assertEqual(build_get_temperature().hex(" ").upper(), "A5 5A 02 00 6D 7B")
- values = tuple(range(27))
- raw = build_temperature_data(values)
- self.assertEqual(len(raw), 33)
- self.assertEqual(raw[2], 0x82)
- self.assertEqual(raw[3], 27)
- self.assertEqual(decode_temperature_data(decode_frame(raw)), values)
- def test_project_adapter_tb_covers_atomicity_ptc_duplicate_dir_and_override(self):
- source = _read("tests/sim/project_adapter_tb.v")
- for marker in (
- "chamber0 PTC duplicate",
- "FAN target mapping mismatch",
- "open DIR contract",
- "close DIR contract",
- "busy override",
- "stop override",
- "invalid door broke SET_ALL atomicity",
- "legacy 14-byte frame must be rejected",
- "submit_frame(8'h01, 11)",
- ):
- self.assertIn(marker, source)
- def test_output_target_ram_tb_covers_boot_watchdog_and_commit_atomicity(self):
- source = _read("tests/sim/output_target_ram_tb.v")
- for marker in (
- "boot PTC",
- "boot FAN",
- "watchdog PTC",
- "watchdog FAN",
- "8'd20",
- "8'd10",
- "shadow PTC leak",
- "uncommitted",
- ):
- self.assertIn(marker, source)
- def test_motor_and_dispatcher_tb_cover_dir_450_and_override(self):
- motor = _read("tests/sim/door_motor_engine_tb.v")
- dispatcher = _read("tests/sim/door_action_dispatcher_tb.v")
- for marker in (
- ".FIXED_STEPS(450)",
- "open DIR not latched",
- "override DIR not latched",
- "override must drop Y",
- ):
- self.assertIn(marker, motor)
- for marker in (
- "open both DIR/mask",
- "close both DIR/mask",
- "busy override",
- "busy stop override",
- "OPEN_DIR(0)",
- "CLOSE_DIR(1)",
- ):
- self.assertIn(marker, dispatcher)
- def test_startup_and_runtime_limit_cover_oneshot_and_inertness(self):
- startup = _read("tests/sim/startup_close_seek_tb.v")
- runtime = _read("tests/sim/prod2u_p4_normal_v3_top_runtime_limit_tb.v")
- formal = _read("tests/formal/startup_close_seek_formal.v")
- mux = _read("tests/formal/runtime_limit_inertness_formal.v")
- self.assertIn("post-done limit toggle", startup)
- self.assertIn("motor_stop_mask not inert", startup)
- self.assertIn("motor_stop_mask not zero while startup_done is set", runtime)
- self.assertIn("expected host x_step=450", runtime)
- self.assertRegex(formal, r"assert\s*\(\s*motor_stop_mask\s*==\s*2'b00\s*\)")
- self.assertRegex(formal, r"assert\s*\(\s*startup_done\s*\)")
- self.assertIn("startup_done ? 2'b00 : startup_motor_stop_mask", mux)
- self.assertRegex(mux, r"assert\s*\(\s*motor_stop_mask\s*==\s*2'b00\s*\)")
- def test_frame_rx_tb_uses_11_byte_boundary_and_golden_vectors(self):
- source = _read("tests/sim/frame_rx_tb.v")
- self.assertIn("MAX_PAYLOAD(11)", source)
- self.assertIn("golden SET_ALL", source)
- self.assertIn("legacy 14-byte SET_ALL published", source)
- self.assertIn("bad CRC published", source)
- self.assertNotRegex(source, r"MAX_PAYLOAD\s*\(\s*30\s*\)")
- def test_temperature_reader_tb_requires_all_27_indices(self):
- source = _read("tests/sim/temperature_reader_tb.v")
- self.assertIn("writes !== 27", source)
- self.assertIn("every index 0..26", source)
- self.assertIn("addr_seen", source)
- def test_cocotb_checks_dir_semantics_and_27_byte_temperature_payload(self):
- source = _read("tests/cocotb/test_prod2u_door_temp_uart_e2e.py")
- self.assertIn("DIR=0 (open)", source)
- self.assertIn("DIR=1 (close)", source)
- self.assertIn("MOTOR_STEPS = 450", source)
- self.assertIn("TEMPERATURE_DATA, 27", source)
- self.assertNotRegex(source, r"(?m)^\s*pytest\.skip\(|^\s*unittest\.skip\(|^\s*self\.skipTest\(")
- def test_no_unconditional_skips_in_active_contract_unit_tests(self):
- pattern = re.compile(r"^\s*(?:pytest\.skip|unittest\.skip|@unittest\.skip)\b", re.M)
- # Conditional env skips (formal receipt) are allowed; bare skips are not.
- for relative in (
- "tests/unit/test_protocol.py",
- "tests/unit/test_product_contract_coverage.py",
- "tests/unit/test_startup_close_seek.py",
- "tests/unit/test_phase2_contract.py",
- ):
- source = _read(relative)
- with self.subTest(relative=relative):
- self.assertIsNone(pattern.search(source), f"unconditional skip in {relative}")
- def test_manifest_lists_new_contract_targets(self):
- """Coordinator must register new benches/formal in the active target list."""
- manifest = json.loads(MANIFEST_PATH.read_text(encoding="utf-8"))
- icarus = set(manifest["layers"]["icarus"])
- formal_names = {entry["name"] for entry in manifest["layers"]["formal"]}
- unit = set(manifest["layers"]["unit"])
- required_icarus = {
- "project_adapter_tb",
- "project_adapter_motor_compat_tb",
- "output_target_ram_tb",
- "door_motor_engine_tb",
- "door_action_dispatcher_tb",
- "door_action_dispatcher_axis3_tb",
- "frame_rx_tb",
- "startup_close_seek_tb",
- "prod2u_p4_normal_v3_top_runtime_limit_tb",
- "temperature_reader_tb",
- "host_watchdog_tb",
- "intake_solenoid_adapter_tb",
- }
- gaps: list[str] = []
- missing_icarus = sorted(required_icarus - icarus)
- if missing_icarus:
- gaps.append(f"icarus:{missing_icarus}")
- if "runtime_limit_inertness" not in formal_names:
- gaps.append("formal:runtime_limit_inertness")
- if "startup_close_seek" not in formal_names:
- gaps.append("formal:startup_close_seek")
- if "door_action_dispatcher_axis3" not in formal_names:
- gaps.append("formal:door_action_dispatcher_axis3")
- if "tests.unit.test_product_contract_coverage" not in unit:
- gaps.append("unit:tests.unit.test_product_contract_coverage")
- if "tests.unit.test_intake_dual_profile" not in unit:
- gaps.append("unit:tests.unit.test_intake_dual_profile")
- formal_by_name = {
- entry["name"]: entry for entry in manifest["layers"]["formal"]
- }
- for bmc_name in (
- "frame_command_contract",
- "frame_tx_contract",
- "simple_frame_contract",
- "startup_close_seek",
- "runtime_limit_inertness",
- "door_action_dispatcher_axis3",
- ):
- tasks = formal_by_name[bmc_name]["tasks"]
- if "bmc" not in tasks:
- gaps.append(f"formal:{bmc_name}:missing_bmc_task")
- if "prove" in tasks:
- gaps.append(f"formal:{bmc_name}:mislabeled_prove_task")
- for cover_name in (
- "startup_close_seek",
- "runtime_limit_inertness",
- "simple_frame_contract",
- "door_action_dispatcher_axis3",
- ):
- if "cover" not in formal_by_name[cover_name]["tasks"]:
- gaps.append(f"formal:{cover_name}:missing_cover_task")
- # frame_command: BMC asserts + source-level cover(); registered cover task
- # omitted on purpose (depth-96 cover of full adapter stack OOMs ~2GiB hosts).
- if "cover" in formal_by_name["frame_command_contract"]["tasks"]:
- gaps.append("formal:frame_command_contract:unexpected_cover_task")
- self.assertEqual(
- gaps,
- [],
- "config/prod2u_p4_verification_targets_v1.json gaps (coordinator): "
- + "; ".join(gaps),
- )
- def test_motor_compat_tb_covers_intake_axis_and_stop(self):
- source = _read("tests/sim/project_adapter_motor_compat_tb.v")
- for marker in (
- "AXIS_COUNT(3)",
- "motor_active_mask",
- "intake",
- ):
- self.assertIn(marker, source)
- formal = _read("tests/formal/door_action_dispatcher_axis3_formal.v")
- self.assertIn("cover", formal)
- self.assertIn("AXIS_COUNT(3)", formal)
- self.assertIn("intake_door_cmd", formal)
- if __name__ == "__main__":
- unittest.main()
|