test_project_structure.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python3
  2. """Top-level project structure contract tests for the converged CPLD project."""
  3. from __future__ import annotations
  4. import re
  5. import unittest
  6. from pathlib import Path
  7. ROOT = Path(__file__).resolve().parents[1]
  8. THIS_FILE = Path(__file__).resolve()
  9. WORKSPACE_PATH_MARKER = re.compile(r"(?<![A-Za-z0-9_])workspace[\\/]")
  10. LEGACY_CHAIN_MARKERS = [
  11. "bringup",
  12. "raw_cr",
  13. "raw-cr",
  14. "cr_raw",
  15. "cr-raw",
  16. "sensor_raw",
  17. "sensor-raw",
  18. "host_control_pwm",
  19. "host-control-pwm",
  20. "host_sensor_pwm_debug921",
  21. "host-sensor-pwm-debug921",
  22. "j6_system_debug",
  23. "j6-system-debug",
  24. "j6_system_top",
  25. "j6-system-top",
  26. "j6sys",
  27. "uart_rs232_system",
  28. ]
  29. class ProjectStructureTests(unittest.TestCase):
  30. def test_converged_engineering_layout_has_single_user_bitstream_top(self) -> None:
  31. required_dirs = [
  32. "src/rtl",
  33. "src/top",
  34. "tests/sim",
  35. "tests/formal",
  36. "tools",
  37. "td/syn",
  38. "td/phy",
  39. "config",
  40. "constraints",
  41. "docs",
  42. "schemas",
  43. "reference/original/schematics",
  44. "test_vectors",
  45. ]
  46. for rel in required_dirs:
  47. self.assertTrue((ROOT / rel).is_dir(), rel)
  48. required_files = [
  49. "src/rtl/pid_core.v",
  50. "src/rtl/uart_rx_8n1.v",
  51. "src/rtl/uart_tx_8n1.v",
  52. "src/rtl/bmc_uart_raw48_poll_lite.v",
  53. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v",
  54. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921_top.v",
  55. "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921_top.v",
  56. "tests/sim/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top_tb.v",
  57. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.adc",
  58. "constraints/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.adc",
  59. "td/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.prj",
  60. "td/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.prj",
  61. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  62. "td/syn/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  63. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.tcl",
  64. "td/phy/uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921.tcl",
  65. "tools/pid_harness.py",
  66. "tools/uart_receipts.py",
  67. "tools/pin_inventory.py",
  68. "tools/package_j6_host_sensor_real_fan_ptc_debug921.ps1",
  69. "tools/remote/collect-j6-host-sensor-real-fan-ptc.ps1",
  70. "schemas/final_gate_v2_receipt.schema.json",
  71. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  72. "schemas/cpld_bmc_acceptance_v1_receipt.schema.json",
  73. "schemas/real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt.schema.json",
  74. "docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md",
  75. "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
  76. ]
  77. for rel in required_files:
  78. self.assertTrue((ROOT / rel).is_file(), rel)
  79. self.assertEqual(
  80. sorted(path.name for path in (ROOT / "tools" / "remote").glob("*.ps1")),
  81. ["collect-j6-host-sensor-real-fan-ptc.ps1"],
  82. )
  83. def test_old_standalone_j6_diagnostic_entrypoints_are_removed(self) -> None:
  84. active_roots = ["src/top", "tests/sim", "td", "td/syn", "td/phy", "constraints", "tools", "tools/remote", "schemas", "docs"]
  85. offenders: list[str] = []
  86. for root_name in active_roots:
  87. root = ROOT / root_name
  88. if not root.exists():
  89. continue
  90. for path in root.rglob("*"):
  91. if not path.is_file():
  92. continue
  93. rel = path.relative_to(ROOT).as_posix()
  94. if rel in {
  95. "tools/uart_receipts.py",
  96. "tools/pid_harness.py",
  97. "docs/verification-gates-v2.zh-CN.md",
  98. "schemas/real_bmc_j6_windows_gate_v2_receipt.schema.json",
  99. }:
  100. continue
  101. lowered = path.name.lower()
  102. if any(marker in lowered for marker in LEGACY_CHAIN_MARKERS):
  103. offenders.append(rel)
  104. self.assertEqual(offenders, [])
  105. def test_workspace_is_not_an_active_project_entrypoint(self) -> None:
  106. self.assertFalse((ROOT / "workspace").exists(), "deprecated workspace tree must not remain active")
  107. def test_active_text_files_do_not_reference_workspace_entrypoints(self) -> None:
  108. active_roots = ["src", "tests", "tools", "td", "config", "constraints", "docs", "schemas"]
  109. offenders: list[str] = []
  110. for root_name in active_roots:
  111. root = ROOT / root_name
  112. if not root.exists():
  113. continue
  114. for path in root.rglob("*"):
  115. if not path.is_file() or path.suffix.lower() in {".png", ".jpg", ".jpeg", ".pdf", ".pyc"}:
  116. continue
  117. if "__pycache__" in path.parts or path.resolve() == THIS_FILE or path.name == "verify_pid_study.py":
  118. continue
  119. text = path.read_text(encoding="utf-8", errors="replace")
  120. if WORKSPACE_PATH_MARKER.search(text):
  121. offenders.append(path.relative_to(ROOT).as_posix())
  122. self.assertEqual(offenders, [])
  123. def test_latest_top_matches_locked_serial_and_real_fan_ptc_interface(self) -> None:
  124. top = (ROOT / "src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v").read_text(
  125. encoding="utf-8",
  126. errors="replace",
  127. )
  128. module_header = top.split(");", 1)[0]
  129. for marker in [
  130. "bmc_uart_rx",
  131. "bmc_uart_tx",
  132. "debug_uart_rx",
  133. "debug_uart_tx",
  134. "host_uart_rx",
  135. "host_uart_tx",
  136. "ptc_pwm_out",
  137. "fan_pwm_out",
  138. "fan_fg_in",
  139. "fan_install_in",
  140. "SET PTC 01..24",
  141. "SET FAN 01..04",
  142. "GET SENSOR",
  143. "SET PID ENABLE",
  144. "pid_active=0",
  145. "J6CMD",
  146. "J6RAW",
  147. "J6TEMP",
  148. ]:
  149. self.assertIn(marker, top)
  150. self.assertIn("output wire [23:0] ptc_pwm_out", module_header)
  151. self.assertIn("output wire [3:0] fan_pwm_out", module_header)
  152. self.assertIn("input wire [7:0] fan_fg_in", module_header)
  153. self.assertIn("input wire [3:0] fan_install_in", module_header)
  154. self.assertNotIn("PROGRAM_FLASH", top)
  155. self.assertNotIn("ERASE_FLASH", top)
  156. def test_two_user_td_profiles_elaborate_distinct_wrapper_tops(self) -> None:
  157. profiles = {
  158. "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921": 0,
  159. "uart_rs232_j6_host_sensor_real_fan_ptc_formal_uart_debug921": 1,
  160. }
  161. for stem, pin_profile in profiles.items():
  162. with self.subTest(stem=stem):
  163. wrapper_path = ROOT / "src" / "top" / f"{stem}_top.v"
  164. wrapper = wrapper_path.read_text(encoding="utf-8", errors="replace")
  165. self.assertIn(f"module {stem}_top", wrapper)
  166. self.assertIn("uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top #(", wrapper)
  167. self.assertIn(f".PIN_PROFILE({pin_profile})", wrapper)
  168. prj_text = (ROOT / "td" / f"{stem}.prj").read_text(encoding="utf-8")
  169. self.assertIn("../src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v", prj_text)
  170. self.assertIn(f"../src/top/{stem}_top.v", prj_text)
  171. self.assertIn(f"<MODULE>{stem}_top</MODULE>", prj_text)
  172. syn_text = (ROOT / "td" / "syn" / f"{stem}.tcl").read_text(encoding="utf-8")
  173. self.assertIn("analyze -file {src/top/uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top.v}", syn_text)
  174. self.assertIn(f"analyze -file {{src/top/{stem}_top.v}}", syn_text)
  175. self.assertIn(f"elaborate -top {{{stem}_top}}", syn_text)
  176. if __name__ == "__main__":
  177. unittest.main()