from __future__ import annotations import copy import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] VIEWER_ROOT = ROOT / "tools" / "board-viewer" PCB_FIXTURE = Path(__file__).parent / "fixtures" / "parser_edge_cases.kicad_pcb" D356_FIXTURE = Path(__file__).parent / "fixtures" / "parser_edge_cases.d356" sys.path.insert(0, str(VIEWER_ROOT)) from board_viewer.data import build_board_data # noqa: E402 from board_viewer.ipcd356 import IpCd356AlignmentError, validate_ipcd356_alignment # noqa: E402 from board_viewer.pcb import parse_kicad_pcb # noqa: E402 def fixture_payload() -> dict[str, object]: return build_board_data( parse_kicad_pcb(PCB_FIXTURE), {"schema": "fixture", "board_id": "fixture", "pins": []}, { "schema": "board-viewer-annotations-v1", "board_id": "fixture", "connector_profiles": {}, "touchpoints": {}, "interfaces": [], "photo_overlay": None, }, board_name="Fixture", ) def test_d356_alignment_independently_matches_every_numbered_pad() -> None: result = validate_ipcd356_alignment(fixture_payload(), D356_FIXTURE) assert result == { "numbered_pads": 2, "matched_pads": 2, "record_types": {"317": 1, "327": 1}, "tolerance_mm": 0.02, } def test_d356_alignment_reports_zero_for_an_unused_record_type() -> None: payload = fixture_payload() payload["touchpoints"] = [item for item in payload["touchpoints"] if item["reference"] == "CON1"] result = validate_ipcd356_alignment(payload, D356_FIXTURE) assert result["record_types"] == {"317": 1, "327": 0} @pytest.mark.parametrize("mutation", ["coordinate", "net"]) def test_d356_alignment_fails_closed_on_coordinate_or_net_drift(tmp_path: Path, mutation: str) -> None: text = D356_FIXTURE.read_text(encoding="ascii") if mutation == "coordinate": text = text.replace("X+004331Y+002756", "X+004731Y+002756") else: text = text.replace("RS232_TX2", "WRONG_NET ") path = tmp_path / "drifted.d356" path.write_text(text, encoding="ascii") with pytest.raises(IpCd356AlignmentError, match="D356 alignment failed"): validate_ipcd356_alignment(fixture_payload(), path)