"""Round-10: TD resource parser and production-source hash compare.""" from __future__ import annotations import json from pathlib import Path from tools import package_provenance as pkg_prov ROOT = Path(__file__).resolve().parents[2] def test_parse_td_resources_keeps_explicit_mslice_1089(): compact = { "lut": 3901, "mslice": 1089, "dsp": 1, "resources": { "utilization": { "lut": {"used": 4320}, "dsp": {"used": 1}, }, "packing": {"mslice": 1089, "lslice": 1089, "total_slices": 2178}, }, } parsed = pkg_prov.parse_td_resource_fields(compact) assert parsed["lut"] == 4320 assert parsed["mslice"] == 1089 assert parsed["dsp"] == 1 def test_parse_td_resources_never_falls_back_lut_to_mslice(): compact = { "resources": { "utilization": { "lut": {"used": 4320}, "le": {"used": 4320}, "dsp": {"used": 1}, } } } parsed = pkg_prov.parse_td_resource_fields(compact) assert parsed["lut"] == 4320 assert parsed["mslice"] is None assert parsed["dsp"] == 1 def test_canonical_identity_mslice_is_1089_not_4320(): td = pkg_prov.load_canonical_td_a1_fields(ROOT) ident = pkg_prov.build_shared_release_identity( delivery_commit="d91a7b46a37371d9a03ae47c5245da2eb4561772", package_stage="intermediate-review", matrix_verification_tip="d126c3df0ceef3de4d559b60253fb5336da11922", software_verification_run_id="mse-r9-20260812T163229Z", config_fingerprint="x", reference_set_fingerprint="y", repo_root=ROOT, claim_bound_bitstream=True, ) assert td["mslice"] == 1089 assert ident["mslice"] == 1089 assert ident["lut"] == 4320 def test_artifact_to_delivery_hash_compare_not_commit_equality(): artifact = "ff44128a540b7ffd9d82314093b242ac3aee92d7" delivery = "d91a7b46a37371d9a03ae47c5245da2eb4561772" assert artifact != delivery cmp_ = pkg_prov.compare_production_source_commits( from_commit=artifact, to_commit=delivery, repo_root=ROOT, ) assert cmp_["unchanged"] is True assert cmp_["production_changed_paths"] == [] rel = pkg_prov.build_source_closure_relations( old_baseline_commit=pkg_prov.BASE_DEV_COMMIT_DEFAULT, artifact_commit=artifact, delivery_commit=delivery, old_to_artifact_unchanged=False, artifact_to_delivery_unchanged=True, old_to_artifact_changed_paths=["projects/prod2u-door-temp/project.json"], artifact_to_delivery_changed_paths=[], comparison_mode="snapshot_hash", ) check = pkg_prov.validate_source_closure_relations(rel) assert check["status"] == "pass", check def test_snapshot_hash_false_empty_paths_fails(): rel = pkg_prov.build_source_closure_relations( old_baseline_commit=pkg_prov.BASE_DEV_COMMIT_DEFAULT, artifact_commit="aa", delivery_commit="bb", old_to_artifact_unchanged=False, artifact_to_delivery_unchanged=False, old_to_artifact_changed_paths=[], artifact_to_delivery_changed_paths=[], comparison_mode="snapshot_hash", ) check = pkg_prov.validate_source_closure_relations(rel) assert check["status"] == "fail" assert any("empty_paths" in f for f in check["failures"]) def test_load_td_identity_fields_fresh_not_canonical(tmp_path: Path): bitstream = "a" * 64 bound = "c9e7e2b90d60b5018d859b67a55fb162ad0eca39" receipt = tmp_path / "td-receipt.json" doc = { "status": "pass", "run_id": "018969d8b4194ba285ca6dd18c1180bf", "verification_run_id": "e10d2026081301474900000000000000", "commit": bound, "commit_full": bound, "bitstream_sha256": bitstream, "mslice": 1089, "synthesis_input_manifest": {"inputs": {f"f{i}.v": "b" * 64 for i in range(37)}}, "resources": { "utilization": { "lut": {"used": 4320}, "dsp": {"used": 1}, }, "packing": {"mslice": 1089, "lslice": 1089, "total_slices": 2178}, }, } receipt.write_text(json.dumps(doc), encoding="utf-8") fields = pkg_prov.load_td_identity_fields(receipt, repo_root=ROOT) assert fields["bitstream_sha256"] == bitstream assert fields["bitstream_sha256"] != pkg_prov.CANONICAL_TD_A1_BITSTREAM assert fields["td_bound_commit"] == bound assert fields["td_artifact_run_id"] == "018969d8b4194ba285ca6dd18c1180bf" assert fields["lut"] == 4320 assert fields["mslice"] == 1089 assert fields["dsp"] == 1 assert fields["td_policy"] == "fresh_td" assert fields["production_binding"] == "37/37/0/0" ident = pkg_prov.build_shared_release_identity( delivery_commit="d" * 40, package_stage="final-review", matrix_verification_tip="e" * 40, software_verification_run_id="mse-r10-test", config_fingerprint="x", reference_set_fingerprint="y", repo_root=ROOT, claim_bound_bitstream=True, td_fields=fields, ) assert ident["bitstream_sha256"] == bitstream assert ident["artifact_source_commit"] == bound assert ident["lut"] == 4320 assert ident["mslice"] == 1089 assert ident["td_policy"] == "fresh_td" canonical = pkg_prov.load_canonical_td_a1_fields(ROOT) assert ident["release_set_id"] != pkg_prov.build_shared_release_identity( delivery_commit="d" * 40, package_stage="final-review", matrix_verification_tip="e" * 40, software_verification_run_id="mse-r10-test", config_fingerprint="x", reference_set_fingerprint="y", repo_root=ROOT, claim_bound_bitstream=True, )["release_set_id"] assert canonical["bitstream_sha256"] == pkg_prov.CANONICAL_TD_A1_BITSTREAM