test_product_contract_coverage.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python3
  2. """Static product-contract coverage gates for the 3-axis six-limit SET_ALL profile."""
  3. from __future__ import annotations
  4. import json
  5. import re
  6. import unittest
  7. from pathlib import Path
  8. from tools.prod2u_simple_protocol import (
  9. FrameType,
  10. build_get_temperature,
  11. build_set_all,
  12. build_temperature_data,
  13. decode_frame,
  14. decode_temperature_data,
  15. )
  16. ROOT = Path(__file__).resolve().parents[2]
  17. MANIFEST_PATH = ROOT / "config" / "prod2u_p4_verification_targets_v1.json"
  18. def _read(relative: str) -> str:
  19. return (ROOT / relative).read_text(encoding="utf-8")
  20. class ProductContractCoverageTests(unittest.TestCase):
  21. def test_protocol_set_all_is_exactly_11_bytes_with_golden_crc(self):
  22. zero = bytes.fromhex("A55A010B00000000000000000000005873")
  23. mixed = bytes.fromhex("A55A010B0A141E280019326401020087D5")
  24. self.assertEqual(build_set_all([0] * 4, [0] * 4, 0, 0, 0), zero)
  25. self.assertEqual(build_set_all((10, 20, 30, 40), (0, 25, 50, 100), 1, 2, 0), mixed)
  26. self.assertEqual(len(decode_frame(zero).payload), 11)
  27. self.assertEqual(decode_frame(mixed).frame_type, FrameType.SET_ALL)
  28. def test_temperature_contract_is_exactly_27_values_and_33_byte_frame(self):
  29. self.assertEqual(build_get_temperature().hex(" ").upper(), "A5 5A 02 00 6D 7B")
  30. values = tuple(range(27))
  31. raw = build_temperature_data(values)
  32. self.assertEqual(len(raw), 33)
  33. self.assertEqual(raw[2], 0x82)
  34. self.assertEqual(raw[3], 27)
  35. self.assertEqual(decode_temperature_data(decode_frame(raw)), values)
  36. def test_project_adapter_tb_covers_atomicity_ptc_duplicate_dir_and_busy_ignore(self):
  37. source = _read("tests/sim/project_adapter_tb.v")
  38. for marker in (
  39. "chamber0 PTC duplicate",
  40. "FAN target mapping mismatch",
  41. "open DIR contract",
  42. "close/open DIR contract",
  43. "busy must ignore",
  44. "invalid door broke SET_ALL atomicity",
  45. "legacy 14-byte frame must be rejected",
  46. "submit_frame(8'h01, 11)",
  47. "AXIS_COUNT(3)",
  48. ):
  49. self.assertIn(marker, source)
  50. def test_motor_and_dispatcher_tb_cover_dir_450_and_busy_ignore(self):
  51. motor = _read("tests/sim/door_motor_engine_tb.v")
  52. dispatcher = _read("tests/sim/door_action_dispatcher_tb.v")
  53. for marker in (
  54. "HOST_STEPS = 450",
  55. "mixed DIR not latched",
  56. "busy must ignore",
  57. "AXIS_COUNT = 3",
  58. ):
  59. self.assertIn(marker, motor)
  60. for marker in (
  61. "open all mask/dir",
  62. "close all mask/dir",
  63. "busy must ignore",
  64. "OPEN_DIR(0)",
  65. "CLOSE_DIR(1)",
  66. "z_door_cmd",
  67. ):
  68. self.assertIn(marker, dispatcher)
  69. def test_startup_and_runtime_limit_cover_close_backoff_and_hard_stop(self):
  70. startup = _read("tests/sim/startup_close_seek_tb.v")
  71. runtime = _read("tests/sim/prod2u_p4_normal_v3_top_runtime_limit_tb.v")
  72. formal = _read("tests/formal/startup_close_seek_formal.v")
  73. mux = _read("tests/formal/runtime_limit_hard_stop_formal.v")
  74. self.assertIn("post-done limit toggle", startup)
  75. self.assertIn("runtime LS stop failed", runtime)
  76. self.assertIn("runtime HS stop failed", runtime)
  77. self.assertRegex(formal, r"assert\s*\(\s*motor_stop_mask\s*==\s*3'b000\s*\)")
  78. self.assertRegex(formal, r"assert\s*\(\s*startup_done\s*\)")
  79. self.assertIn("startup_done ? runtime_limit_stop_mask", mux)
  80. self.assertRegex(mux, r"assert\s*\(\s*motor_stop_mask\s*==\s*runtime_limit_stop_mask\s*\)")
  81. def test_cocotb_checks_dir_semantics_six_limits_and_en_permanent_high(self):
  82. source = _read("tests/cocotb/test_prod2u_door_temp_uart_e2e.py")
  83. self.assertIn("DIR=0 (open)", source)
  84. self.assertIn("DIR=1 (close)", source)
  85. self.assertIn("MOTOR_STEPS = 450", source)
  86. self.assertIn("TEMPERATURE_DATA, 27", source)
  87. self.assertIn("z_limit_ls", source)
  88. self.assertIn("motor_en_n.value) == 1", source)
  89. def test_manifest_lists_new_contract_targets(self):
  90. manifest = json.loads(MANIFEST_PATH.read_text(encoding="utf-8"))
  91. icarus = set(manifest["layers"]["icarus"])
  92. formal_names = {entry["name"] for entry in manifest["layers"]["formal"]}
  93. unit = set(manifest["layers"]["unit"])
  94. required_icarus = {
  95. "project_adapter_tb",
  96. "output_target_ram_tb",
  97. "door_motor_engine_tb",
  98. "door_action_dispatcher_tb",
  99. "frame_rx_tb",
  100. "startup_close_seek_tb",
  101. "prod2u_p4_normal_v3_top_runtime_limit_tb",
  102. "temperature_reader_tb",
  103. "host_watchdog_tb",
  104. }
  105. gaps: list[str] = []
  106. missing_icarus = sorted(required_icarus - icarus)
  107. if missing_icarus:
  108. gaps.append(f"icarus:{missing_icarus}")
  109. if "runtime_limit_hard_stop" not in formal_names:
  110. gaps.append("formal:runtime_limit_hard_stop")
  111. if "startup_close_seek" not in formal_names:
  112. gaps.append("formal:startup_close_seek")
  113. if "door_action_dispatcher_axis3" not in formal_names:
  114. gaps.append("formal:door_action_dispatcher_axis3")
  115. if "tests.unit.test_three_axis_door_contract" not in unit:
  116. gaps.append("unit:tests.unit.test_three_axis_door_contract")
  117. forbidden_icarus = {
  118. "intake_solenoid_adapter_tb",
  119. "project_adapter_motor_compat_tb",
  120. "door_action_dispatcher_axis3_tb",
  121. "door_motor_engine_axis3_tb",
  122. "startup_close_seek_axis3_tb",
  123. }
  124. if icarus & forbidden_icarus:
  125. gaps.append(f"icarus:obsolete:{sorted(icarus & forbidden_icarus)}")
  126. formal_by_name = {entry["name"]: entry for entry in manifest["layers"]["formal"]}
  127. for cover_name in (
  128. "startup_close_seek",
  129. "runtime_limit_hard_stop",
  130. "simple_frame_contract",
  131. "door_action_dispatcher_axis3",
  132. ):
  133. if "cover" not in formal_by_name[cover_name]["tasks"]:
  134. gaps.append(f"formal:{cover_name}:missing_cover_task")
  135. self.assertEqual(gaps, [], "verification manifest gaps: " + "; ".join(gaps))
  136. def test_dispatcher_formal_covers_z_axis_and_busy_ignore(self):
  137. formal = _read("tests/formal/door_action_dispatcher_axis3_formal.v")
  138. self.assertIn("cover", formal)
  139. self.assertIn("AXIS_COUNT(3)", formal)
  140. self.assertIn("z_door_cmd", formal)
  141. self.assertIn("motor_busy", formal)
  142. if __name__ == "__main__":
  143. unittest.main()