test_product_contract_coverage.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python3
  2. """Static product-contract coverage gates for the active 11-byte SET_ALL profile.
  3. Authority is AGENTS.md / requirements / protocol docs — not current RTL behavior.
  4. These checks ensure the verification tree keeps the required contract assertions
  5. instead of silently weakening or deleting them.
  6. """
  7. from __future__ import annotations
  8. import json
  9. import re
  10. import unittest
  11. from pathlib import Path
  12. from tools.prod2u_simple_protocol import (
  13. FrameType,
  14. build_get_temperature,
  15. build_set_all,
  16. build_temperature_data,
  17. decode_frame,
  18. decode_temperature_data,
  19. )
  20. ROOT = Path(__file__).resolve().parents[2]
  21. MANIFEST_PATH = ROOT / "config" / "prod2u_p4_verification_targets_v1.json"
  22. def _read(relative: str) -> str:
  23. return (ROOT / relative).read_text(encoding="utf-8")
  24. class ProductContractCoverageTests(unittest.TestCase):
  25. def test_protocol_set_all_is_exactly_11_bytes_with_golden_crc(self):
  26. zero = bytes.fromhex("A55A010B00000000000000000000005873")
  27. mixed = bytes.fromhex("A55A010B0A141E280019326401020087D5")
  28. self.assertEqual(build_set_all([0] * 4, [0] * 4, 0, 0, 0), zero)
  29. self.assertEqual(build_set_all((10, 20, 30, 40), (0, 25, 50, 100), 1, 2, 0), mixed)
  30. self.assertEqual(len(decode_frame(zero).payload), 11)
  31. self.assertEqual(decode_frame(mixed).frame_type, FrameType.SET_ALL)
  32. def test_temperature_contract_is_exactly_27_values_and_33_byte_frame(self):
  33. self.assertEqual(build_get_temperature().hex(" ").upper(), "A5 5A 02 00 6D 7B")
  34. values = tuple(range(27))
  35. raw = build_temperature_data(values)
  36. self.assertEqual(len(raw), 33)
  37. self.assertEqual(raw[2], 0x82)
  38. self.assertEqual(raw[3], 27)
  39. self.assertEqual(decode_temperature_data(decode_frame(raw)), values)
  40. def test_project_adapter_tb_covers_atomicity_ptc_duplicate_dir_and_override(self):
  41. source = _read("tests/sim/project_adapter_tb.v")
  42. for marker in (
  43. "chamber0 PTC duplicate",
  44. "FAN target mapping mismatch",
  45. "open DIR contract",
  46. "close DIR contract",
  47. "busy override",
  48. "stop override",
  49. "invalid door broke SET_ALL atomicity",
  50. "legacy 14-byte frame must be rejected",
  51. "submit_frame(8'h01, 11)",
  52. ):
  53. self.assertIn(marker, source)
  54. def test_output_target_ram_tb_covers_boot_watchdog_and_commit_atomicity(self):
  55. source = _read("tests/sim/output_target_ram_tb.v")
  56. for marker in (
  57. "boot PTC",
  58. "boot FAN",
  59. "watchdog PTC",
  60. "watchdog FAN",
  61. "8'd20",
  62. "8'd10",
  63. "shadow PTC leak",
  64. "uncommitted",
  65. ):
  66. self.assertIn(marker, source)
  67. def test_motor_and_dispatcher_tb_cover_dir_450_and_override(self):
  68. motor = _read("tests/sim/door_motor_engine_tb.v")
  69. dispatcher = _read("tests/sim/door_action_dispatcher_tb.v")
  70. for marker in (
  71. ".FIXED_STEPS(450)",
  72. "open DIR not latched",
  73. "override DIR not latched",
  74. "override must drop Y",
  75. ):
  76. self.assertIn(marker, motor)
  77. for marker in (
  78. "open both DIR/mask",
  79. "close both DIR/mask",
  80. "busy override",
  81. "busy stop override",
  82. "OPEN_DIR(0)",
  83. "CLOSE_DIR(1)",
  84. ):
  85. self.assertIn(marker, dispatcher)
  86. def test_startup_and_runtime_limit_cover_oneshot_and_inertness(self):
  87. startup = _read("tests/sim/startup_close_seek_tb.v")
  88. runtime = _read("tests/sim/prod2u_p4_normal_v3_top_runtime_limit_tb.v")
  89. formal = _read("tests/formal/startup_close_seek_formal.v")
  90. mux = _read("tests/formal/runtime_limit_inertness_formal.v")
  91. self.assertIn("post-done limit toggle", startup)
  92. self.assertIn("motor_stop_mask not inert", startup)
  93. self.assertIn("motor_stop_mask not zero while startup_done is set", runtime)
  94. self.assertIn("expected host x_step=450", runtime)
  95. self.assertRegex(formal, r"assert\s*\(\s*motor_stop_mask\s*==\s*2'b00\s*\)")
  96. self.assertRegex(formal, r"assert\s*\(\s*startup_done\s*\)")
  97. self.assertIn("startup_done ? 2'b00 : startup_motor_stop_mask", mux)
  98. self.assertRegex(mux, r"assert\s*\(\s*motor_stop_mask\s*==\s*2'b00\s*\)")
  99. def test_frame_rx_tb_uses_11_byte_boundary_and_golden_vectors(self):
  100. source = _read("tests/sim/frame_rx_tb.v")
  101. self.assertIn("MAX_PAYLOAD(11)", source)
  102. self.assertIn("golden SET_ALL", source)
  103. self.assertIn("legacy 14-byte SET_ALL published", source)
  104. self.assertIn("bad CRC published", source)
  105. self.assertNotRegex(source, r"MAX_PAYLOAD\s*\(\s*30\s*\)")
  106. def test_temperature_reader_tb_requires_all_27_indices(self):
  107. source = _read("tests/sim/temperature_reader_tb.v")
  108. self.assertIn("writes !== 27", source)
  109. self.assertIn("every index 0..26", source)
  110. self.assertIn("addr_seen", source)
  111. def test_cocotb_checks_dir_semantics_and_27_byte_temperature_payload(self):
  112. source = _read("tests/cocotb/test_prod2u_door_temp_uart_e2e.py")
  113. self.assertIn("DIR=0 (open)", source)
  114. self.assertIn("DIR=1 (close)", source)
  115. self.assertIn("MOTOR_STEPS = 450", source)
  116. self.assertIn("TEMPERATURE_DATA, 27", source)
  117. self.assertNotRegex(source, r"(?m)^\s*pytest\.skip\(|^\s*unittest\.skip\(|^\s*self\.skipTest\(")
  118. def test_no_unconditional_skips_in_active_contract_unit_tests(self):
  119. pattern = re.compile(r"^\s*(?:pytest\.skip|unittest\.skip|@unittest\.skip)\b", re.M)
  120. # Conditional env skips (formal receipt) are allowed; bare skips are not.
  121. for relative in (
  122. "tests/unit/test_protocol.py",
  123. "tests/unit/test_product_contract_coverage.py",
  124. "tests/unit/test_startup_close_seek.py",
  125. "tests/unit/test_phase2_contract.py",
  126. ):
  127. source = _read(relative)
  128. with self.subTest(relative=relative):
  129. self.assertIsNone(pattern.search(source), f"unconditional skip in {relative}")
  130. def test_manifest_lists_new_contract_targets(self):
  131. """Coordinator must register new benches/formal in the active target list."""
  132. manifest = json.loads(MANIFEST_PATH.read_text(encoding="utf-8"))
  133. icarus = set(manifest["layers"]["icarus"])
  134. formal_names = {entry["name"] for entry in manifest["layers"]["formal"]}
  135. unit = set(manifest["layers"]["unit"])
  136. required_icarus = {
  137. "project_adapter_tb",
  138. "project_adapter_motor_compat_tb",
  139. "output_target_ram_tb",
  140. "door_motor_engine_tb",
  141. "door_action_dispatcher_tb",
  142. "door_action_dispatcher_axis3_tb",
  143. "frame_rx_tb",
  144. "startup_close_seek_tb",
  145. "prod2u_p4_normal_v3_top_runtime_limit_tb",
  146. "temperature_reader_tb",
  147. "host_watchdog_tb",
  148. "intake_solenoid_adapter_tb",
  149. }
  150. gaps: list[str] = []
  151. missing_icarus = sorted(required_icarus - icarus)
  152. if missing_icarus:
  153. gaps.append(f"icarus:{missing_icarus}")
  154. if "runtime_limit_inertness" not in formal_names:
  155. gaps.append("formal:runtime_limit_inertness")
  156. if "startup_close_seek" not in formal_names:
  157. gaps.append("formal:startup_close_seek")
  158. if "door_action_dispatcher_axis3" not in formal_names:
  159. gaps.append("formal:door_action_dispatcher_axis3")
  160. if "tests.unit.test_product_contract_coverage" not in unit:
  161. gaps.append("unit:tests.unit.test_product_contract_coverage")
  162. if "tests.unit.test_intake_dual_profile" not in unit:
  163. gaps.append("unit:tests.unit.test_intake_dual_profile")
  164. formal_by_name = {
  165. entry["name"]: entry for entry in manifest["layers"]["formal"]
  166. }
  167. for bmc_name in (
  168. "frame_command_contract",
  169. "frame_tx_contract",
  170. "simple_frame_contract",
  171. "startup_close_seek",
  172. "runtime_limit_inertness",
  173. "door_action_dispatcher_axis3",
  174. ):
  175. tasks = formal_by_name[bmc_name]["tasks"]
  176. if "bmc" not in tasks:
  177. gaps.append(f"formal:{bmc_name}:missing_bmc_task")
  178. if "prove" in tasks:
  179. gaps.append(f"formal:{bmc_name}:mislabeled_prove_task")
  180. for cover_name in (
  181. "startup_close_seek",
  182. "runtime_limit_inertness",
  183. "simple_frame_contract",
  184. "door_action_dispatcher_axis3",
  185. ):
  186. if "cover" not in formal_by_name[cover_name]["tasks"]:
  187. gaps.append(f"formal:{cover_name}:missing_cover_task")
  188. # frame_command: BMC asserts + source-level cover(); registered cover task
  189. # omitted on purpose (depth-96 cover of full adapter stack OOMs ~2GiB hosts).
  190. if "cover" in formal_by_name["frame_command_contract"]["tasks"]:
  191. gaps.append("formal:frame_command_contract:unexpected_cover_task")
  192. self.assertEqual(
  193. gaps,
  194. [],
  195. "config/prod2u_p4_verification_targets_v1.json gaps (coordinator): "
  196. + "; ".join(gaps),
  197. )
  198. def test_motor_compat_tb_covers_intake_axis_and_stop(self):
  199. source = _read("tests/sim/project_adapter_motor_compat_tb.v")
  200. for marker in (
  201. "AXIS_COUNT(3)",
  202. "motor_active_mask",
  203. "intake",
  204. ):
  205. self.assertIn(marker, source)
  206. formal = _read("tests/formal/door_action_dispatcher_axis3_formal.v")
  207. self.assertIn("cover", formal)
  208. self.assertIn("AXIS_COUNT(3)", formal)
  209. self.assertIn("intake_door_cmd", formal)
  210. if __name__ == "__main__":
  211. unittest.main()