#!/usr/bin/env python3 """Static routing contracts for the minimal package PowerShell wrappers.""" from __future__ import annotations import re import subprocess import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] TEMPLATES = ROOT / "tools" / "package_templates" DISABLE_HEX = "A55A02040500000000000000000426000000C4AE" class Prod2UP4WrapperV3Tests(unittest.TestCase): def test_run_wrapper_exposes_only_four_package_operations(self) -> None: source = (TEMPLATES / "run.ps1").read_text(encoding="utf-8") validate_set = re.search(r'\[ValidateSet\((.*?)\)\]', source, flags=re.DOTALL) self.assertIsNotNone(validate_set) commands = set(re.findall(r'"([^"]+)"', validate_set.group(1))) self.assertEqual( commands, {"selftest", "send-hex", "initialize-disable", "program-sram"}, ) for retired in ( "normal-actuator-trace-gate", "set-all-outputs", "jog", "mark-closed", "read-temperatures", ): self.assertNotIn(f'"{retired}"', source) def test_run_wrapper_rejects_direct_motion_before_any_child_script(self) -> None: completed = subprocess.run( [ "powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(TEMPLATES / "run.ps1"), "jog", ], cwd=ROOT, capture_output=True, text=True, check=False, ) self.assertNotEqual(completed.returncode, 0) self.assertIn("ValidateSet", completed.stderr + completed.stdout) def test_initialize_disable_uses_the_frozen_request_through_send_hex(self) -> None: source = (TEMPLATES / "INITIALIZE_DISABLE.ps1").read_text(encoding="utf-8") compact = re.sub(r"\s+", "", source).upper() self.assertIn("SEND_HEX.PS1", compact) self.assertIn(DISABLE_HEX, compact) self.assertIn('"COM5"', source) self.assertIn("921600", source) def test_programmer_is_one_attempt_sram_only_at_200_khz(self) -> None: source = (TEMPLATES / "PROGRAM_SRAM.ps1").read_text(encoding="utf-8") self.assertIn('[ValidateSet("PROGRAM_SRAM")]', source) self.assertEqual(source.count("download -bit"), 1) self.assertIn("-mode jtag -spd 2", source) self.assertIn("attempt_count = 1", source) self.assertIn("speed_hz = 200000", source) self.assertIn("no retry was attempted", source) self.assertIn("$info.commit -cnotmatch '^[0-9a-f]{8}$'", source) self.assertIn('$stage = Join-Path "C:\\P2U" ([string]$info.commit)', source) self.assertNotRegex(source, r"(?i)build[_-]?id") for forbidden in ( "PROGRAM_FLASH", "ERASE_FLASH", "PROGRAM_SPIBIN", "DIRECT_FLASH_WRITE", ): self.assertNotIn(forbidden, source) if __name__ == "__main__": unittest.main()