import hashlib import json import tempfile import unittest from pathlib import Path from tools.generate_project import generate_project ROOT = Path(__file__).resolve().parents[2] PROJECT = ROOT / "projects" / "prod2u-door-temp" / "project.json" class ProjectGenerationTests(unittest.TestCase): def test_generation_is_deterministic_and_emits_compile_time_parameters(self): with tempfile.TemporaryDirectory() as directory: first = Path(directory) / "first" second = Path(directory) / "second" generate_project(PROJECT, first) generate_project(PROJECT, second) first_files = sorted(path.relative_to(first) for path in first.rglob("*" ) if path.is_file()) second_files = sorted(path.relative_to(second) for path in second.rglob("*" ) if path.is_file()) self.assertEqual(first_files, second_files) for relative in first_files: self.assertEqual( hashlib.sha256((first / relative).read_bytes()).digest(), hashlib.sha256((second / relative).read_bytes()).digest(), ) params = (first / "generated" / "prod2u_door_temp_params.vh").read_text(encoding="ascii") self.assertIn("P4_PTC_COUNT 24", params) self.assertIn("P4_UART_CLKS_PER_BIT 217", params) self.assertIn("P4_TEMP_DEVICE_ADDR_0 7'h49", params) self.assertIn("P4_TEMP_DEVICE_ADDR_1 7'h48", params) self.assertIn("P4_TEMP_DEVICE_ADDR_2 7'h4B", params) self.assertIn("P4_TEMP_REGISTER_8 8'h08", params) self.assertIn("P4_TEMP_MAX_CELSIUS 150", params) self.assertIn("P4_PTC_ACTIVE_HIGH 1", params) self.assertIn("P4_FAN_ACTIVE_HIGH 0", params) self.assertIn("P4_PWM_FREQUENCY_HZ 20", params) self.assertIn("P4_PWM_SLOT_CLKS 174", params) self.assertIn("P4_PTC_DEFAULT_TARGET 8'h00", params) self.assertIn("P4_FAN_DEFAULT_TARGET 8'h1A", params) contract = json.loads((first / "generated" / "host-contract.json").read_text(encoding="utf-8")) self.assertEqual(contract["commands"]["set_all"]["payload_length"], 30) golden = json.loads((first / "generated" / "golden-vectors.json").read_text(encoding="utf-8")) self.assertEqual(golden["get_temperature"], "A55A02006D7B") self.assertEqual(golden["all_invalid_temperature"], "A55A821B" + "FF" * 27 + "A745") source_manifest = json.loads( (first / "generated" / "td-source-manifest.json").read_text(encoding="utf-8") ) self.assertIn("src/common/frame_rx.v", source_manifest["sources"]) self.assertNotIn("src/rtl/binary921_crc16.v", source_manifest["sources"]) summary = json.loads((first / "generated" / "project-summary.json").read_text(encoding="utf-8")) self.assertEqual(summary["pwm_slot_clks"], 174) self.assertEqual(summary["pwm_period_clks"], 1_247_232) self.assertAlmostEqual(summary["actual_frequency_hz"], 20.044386289, places=9) if __name__ == "__main__": unittest.main()