#!/usr/bin/env python3 """Formal / sim contract checks for door-percent motion stack.""" from __future__ import annotations import unittest from pathlib import Path from tools.prod2u_p4_harness import load_target_manifest ROOT = Path(__file__).resolve().parents[2] def _read(relative: str) -> str: return (ROOT / relative).read_text(encoding="utf-8", errors="replace") class Phase3FormalContractTests(unittest.TestCase): def test_manifest_lists_percent_formal_and_sim_targets(self): manifest = load_target_manifest() icarus = set(manifest["layers"]["icarus"]) formal_names = {entry["name"] for entry in manifest["layers"]["formal"]} self.assertIn("door_percent_planner_tb", icarus) self.assertIn("door_motion_controller_tb", icarus) self.assertIn("door_axis_watchdog_drain_tb", icarus) self.assertIn("door_axis_request_limit_race_tb", icarus) self.assertIn("door_axis_request_planner_collision_tb", icarus) self.assertIn("door_axis_source_limit_linger_tb", icarus) self.assertIn("prod2u_runtime_step_limit_gate_reopen_tb", icarus) self.assertIn("door_motion_status_payload_tb", icarus) self.assertIn("command_startup_commit_tb", icarus) self.assertIn("door_axis_target_valid_tb", icarus) self.assertIn("door_planner_grant_once_tb", icarus) self.assertNotIn("door_motor_engine_tb", icarus) self.assertNotIn("startup_close_seek_tb", icarus) self.assertIn("door_percent_planner", formal_names) self.assertIn("door_axis_cal", formal_names) self.assertIn("door_axis_watchdog_drain", formal_names) self.assertIn("door_axis_request_limit_race", formal_names) self.assertIn("door_axis_request_planner_collision", formal_names) self.assertIn("door_axis_motion_contract", formal_names) self.assertIn("door_motion_status_snapshot", formal_names) self.assertIn("command_startup_commit", formal_names) self.assertIn("runtime_limit_hard_stop", formal_names) self.assertIn("door_axis_source_limit_lingering", formal_names) self.assertIn("runtime_step_gate_hold", formal_names) self.assertNotIn("startup_close_seek", formal_names) self.assertNotIn("door_action_dispatcher_axis3", formal_names) blocked = manifest["layers"].get("formal_blocked", []) blocked_names = {entry["name"] for entry in blocked} self.assertNotIn("door_axis_source_limit_lingering", blocked_names) self.assertNotIn("runtime_step_gate_hold", blocked_names) def test_runtime_limit_formal_uses_production_gate_module(self): source = _read("tests/formal/runtime_limit_hard_stop_formal.v") sby = _read("tests/formal/runtime_limit_hard_stop.sby") self.assertIn("truncate_current_high_pulse_immediately_after_synchronized_limit", source) self.assertIn("prod2u_runtime_step_limit_gate", source) self.assertIn("prod2u_runtime_step_limit_gate.v", sby) self.assertNotIn("door_motor_engine", source) def test_cal_formal_instantiates_real_axis_dut(self): source = _read("tests/formal/door_axis_cal_formal.v") sby = _read("tests/formal/door_axis_cal.sby") self.assertIn("door_axis_controller", source) self.assertIn("door_axis_controller.v", sby) # Vacuous shell: assume terminal==(cal||fault) then assert same — forbidden. self.assertNotIn("assume (terminal == (faulted || calibrated))", source) def test_planner_formal_checks_half_up_result(self): self.assertTrue((ROOT / "tests/formal/door_percent_planner_formal.v").is_file()) self.assertTrue((ROOT / "tests/formal/door_percent_planner.sby").is_file()) formal = _read("tests/formal/door_percent_planner_formal.v") self.assertIn("door_percent_planner", formal) self.assertIn("expected_half_up", formal) self.assertIn("assert (result == expected_half_up", formal) def test_watchdog_drain_formal_uses_axis_dut(self): source = _read("tests/formal/door_axis_watchdog_drain_formal.v") sby = _read("tests/formal/door_axis_watchdog_drain.sby") self.assertIn("door_axis_controller", source) self.assertIn("watchdog_stop", source) self.assertIn("door_axis_controller.v", sby) def test_request_limit_race_formal_uses_axis_dut(self): source = _read("tests/formal/door_axis_request_limit_race_formal.v") sby = _read("tests/formal/door_axis_request_limit_race.sby") rtl = _read("src/modules/door_axis_controller.v") self.assertIn("door_axis_controller", source) self.assertIn("limit_pos_mismatch", rtl) self.assertIn("current_position <= travel_steps", rtl) self.assertIn("door_axis_controller.v", sby) self.assertTrue((ROOT / "tests/sim/door_axis_request_limit_race_tb.v").is_file()) def test_request_planner_collision_formal_and_sim(self): source = _read("tests/formal/door_axis_request_planner_collision_formal.v") sby = _read("tests/formal/door_axis_request_planner_collision.sby") rtl = _read("src/modules/door_axis_controller.v") self.assertIn("door_axis_controller", source) self.assertIn("request_requires_replan", rtl) self.assertIn("effective_current_position", rtl) self.assertIn("request_seen", rtl) self.assertIn("door_axis_controller.v", sby) self.assertTrue( (ROOT / "tests/sim/door_axis_request_planner_collision_tb.v").is_file() ) def test_source_limit_lingering_formal_uses_axis_dut(self): source = _read("tests/formal/door_axis_source_limit_lingering_formal.v") sby = _read("tests/formal/door_axis_source_limit_lingering.sby") self.assertIn("door_axis_controller", source) self.assertIn("gated_step_rise", source) self.assertIn("ST_MOVE_OPEN", source) self.assertNotIn("$fatal", source) self.assertIn("door_axis_controller.v", sby) def test_runtime_step_gate_hold_formal_uses_production_gate(self): source = _read("tests/formal/runtime_step_gate_hold_formal.v") sby = _read("tests/formal/runtime_step_gate_hold.sby") self.assertIn("prod2u_runtime_step_limit_gate", source) self.assertIn("phase_truncated", source) self.assertIn(".clk(clk)", source) self.assertIn(".rst_n(rst_n)", source) self.assertNotIn("$fatal", source) self.assertIn("prod2u_runtime_step_limit_gate.v", sby) def test_startup_commit_formal_uses_command_controller(self): source = _read("tests/formal/command_startup_commit_formal.v") sby = _read("tests/formal/command_startup_commit.sby") self.assertIn("command_controller", source) self.assertIn("motor_request_valid", source) self.assertIn("command_controller.v", sby) def test_status_snapshot_formal_uses_motion_and_tx(self): source = _read("tests/formal/door_motion_status_snapshot_formal.v") sby = _read("tests/formal/door_motion_status_snapshot.sby") self.assertIn("door_motion_controller", source) self.assertIn("frame_tx", source) self.assertIn("door_motion_controller.v", sby) def test_watchdog_formal_has_enable_and_fan10(self): source = _read("tests/formal/host_watchdog_contract_formal.v") sby = _read("tests/formal/host_watchdog_contract.sby") self.assertIn(".enable(enable)", source) self.assertIn("WATCHDOG_FAN_TARGET = 8'd10", source) self.assertNotIn("door_motor_engine", sby) def test_top_sim_targets_use_startup_complete(self): startup = _read("tests/sim/prod2u_p4_normal_v3_top_startup_tb.v") runtime = _read("tests/sim/prod2u_p4_normal_v3_top_runtime_limit_tb.v") self.assertIn("startup_complete", startup) self.assertNotIn("startup_done", startup) self.assertIn("gated STEP", runtime.lower() + runtime) self.assertIn("engine_step", runtime) def test_unit_manifest_includes_exhaustive_planner(self): manifest = load_target_manifest() self.assertIn( "tests.unit.test_door_percent_planner_exhaustive", manifest["layers"]["unit"], ) if __name__ == "__main__": unittest.main()