test_product_contract_coverage.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "output_target_ram_tb",
  139. "door_motor_engine_tb",
  140. "door_action_dispatcher_tb",
  141. "frame_rx_tb",
  142. "startup_close_seek_tb",
  143. "prod2u_p4_normal_v3_top_runtime_limit_tb",
  144. "temperature_reader_tb",
  145. "host_watchdog_tb",
  146. "intake_solenoid_adapter_tb",
  147. }
  148. gaps: list[str] = []
  149. missing_icarus = sorted(required_icarus - icarus)
  150. if missing_icarus:
  151. gaps.append(f"icarus:{missing_icarus}")
  152. if "runtime_limit_inertness" not in formal_names:
  153. gaps.append("formal:runtime_limit_inertness")
  154. if "startup_close_seek" not in formal_names:
  155. gaps.append("formal:startup_close_seek")
  156. if "tests.unit.test_product_contract_coverage" not in unit:
  157. gaps.append("unit:tests.unit.test_product_contract_coverage")
  158. self.assertEqual(
  159. gaps,
  160. [],
  161. "config/prod2u_p4_verification_targets_v1.json gaps (coordinator): "
  162. + "; ".join(gaps),
  163. )
  164. if __name__ == "__main__":
  165. unittest.main()