#!/usr/bin/env python3 """Static product-contract coverage gates for the 3-axis six-limit SET_ALL profile.""" 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_busy_ignore(self): source = _read("tests/sim/project_adapter_tb.v") for marker in ( "chamber0 PTC duplicate", "FAN target mapping mismatch", "open DIR contract", "close/open DIR contract", "busy must ignore", "invalid door broke SET_ALL atomicity", "legacy 14-byte frame must be rejected", "submit_frame(8'h01, 11)", "AXIS_COUNT(3)", ): self.assertIn(marker, source) def test_motor_and_dispatcher_tb_cover_dir_450_and_busy_ignore(self): motor = _read("tests/sim/door_motor_engine_tb.v") dispatcher = _read("tests/sim/door_action_dispatcher_tb.v") for marker in ( "HOST_STEPS = 450", "mixed DIR not latched", "busy must ignore", "AXIS_COUNT = 3", ): self.assertIn(marker, motor) for marker in ( "open all mask/dir", "close all mask/dir", "busy must ignore", "OPEN_DIR(0)", "CLOSE_DIR(1)", "z_door_cmd", ): self.assertIn(marker, dispatcher) def test_startup_and_runtime_limit_cover_close_backoff_and_hard_stop(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_hard_stop_formal.v") self.assertIn("post-done limit toggle", startup) self.assertIn("runtime LS stop failed", runtime) self.assertIn("runtime HS stop failed", runtime) self.assertRegex(formal, r"assert\s*\(\s*motor_stop_mask\s*==\s*3'b000\s*\)") self.assertRegex(formal, r"assert\s*\(\s*startup_done\s*\)") self.assertIn("startup_done ? runtime_limit_stop_mask", mux) self.assertRegex(mux, r"assert\s*\(\s*motor_stop_mask\s*==\s*runtime_limit_stop_mask\s*\)") def test_cocotb_checks_dir_semantics_six_limits_and_en_permanent_high(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.assertIn("z_limit_ls", source) self.assertIn("motor_en_n.value) == 1", source) def test_manifest_lists_new_contract_targets(self): 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", "output_target_ram_tb", "door_motor_engine_tb", "door_action_dispatcher_tb", "frame_rx_tb", "startup_close_seek_tb", "prod2u_p4_normal_v3_top_runtime_limit_tb", "temperature_reader_tb", "host_watchdog_tb", } gaps: list[str] = [] missing_icarus = sorted(required_icarus - icarus) if missing_icarus: gaps.append(f"icarus:{missing_icarus}") if "runtime_limit_hard_stop" not in formal_names: gaps.append("formal:runtime_limit_hard_stop") 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_three_axis_door_contract" not in unit: gaps.append("unit:tests.unit.test_three_axis_door_contract") forbidden_icarus = { "intake_solenoid_adapter_tb", "project_adapter_motor_compat_tb", "door_action_dispatcher_axis3_tb", "door_motor_engine_axis3_tb", "startup_close_seek_axis3_tb", } if icarus & forbidden_icarus: gaps.append(f"icarus:obsolete:{sorted(icarus & forbidden_icarus)}") formal_by_name = {entry["name"]: entry for entry in manifest["layers"]["formal"]} for cover_name in ( "startup_close_seek", "runtime_limit_hard_stop", "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") self.assertEqual(gaps, [], "verification manifest gaps: " + "; ".join(gaps)) def test_dispatcher_formal_covers_z_axis_and_busy_ignore(self): formal = _read("tests/formal/door_action_dispatcher_axis3_formal.v") self.assertIn("cover", formal) self.assertIn("AXIS_COUNT(3)", formal) self.assertIn("z_door_cmd", formal) self.assertIn("motor_busy", formal) if __name__ == "__main__": unittest.main()