from __future__ import annotations import math import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] VIEWER_ROOT = ROOT / "tools" / "board-viewer" FIXTURE = Path(__file__).parent / "fixtures" / "parser_edge_cases.kicad_pcb" sys.path.insert(0, str(VIEWER_ROOT)) from board_viewer.pcb import parse_kicad_pcb, select_touchpoints # noqa: E402 def test_parser_ignores_hidden_reference_placeholder_and_non_footprint_text() -> None: board = parse_kicad_pcb(FIXTURE) references = {footprint.reference for footprint in board.footprints} touchpoints = {footprint.reference for footprint in select_touchpoints(board)} assert references == {"CON1", "TP1"} assert "${REFERENCE}" not in references assert touchpoints == {"CON1", "TP1"} assert "CON5" not in touchpoints def test_board_outline_bounds_are_extracted_from_edge_cuts() -> None: board = parse_kicad_pcb(FIXTURE) assert board.bounds == {"x": 0.0, "y": -10.0, "width": 30.0, "height": 10.0} def test_front_pad_coordinates_include_footprint_rotation_and_negative_y() -> None: board = parse_kicad_pcb(FIXTURE) connector = board.footprint_by_reference("CON1") pin = connector.pad_by_number("1") assert connector.component_value == "Fixture:Front" assert connector.x == 10 assert connector.y == -5 assert connector.rotation == 90 assert connector.side == "front" assert pin.x == pytest.approx(11) assert pin.y == pytest.approx(-7) # Pad at-angle is footprint-relative; board-absolute = local + footprint. assert pin.rotation == pytest.approx(120) assert pin.net == "RS232_TX2" def test_back_pad_coordinates_follow_kicad_board_transform_without_extra_mirroring() -> None: board = parse_kicad_pcb(FIXTURE) testpoint = board.footprint_by_reference("TP1") pin = testpoint.pad_by_number("1") assert testpoint.side == "back" assert pin.x == pytest.approx(23) assert pin.y == pytest.approx(8) assert pin.rotation == pytest.approx(120) def test_unnumbered_zero_size_np_thru_hole_is_mechanical_and_finite() -> None: board = parse_kicad_pcb(FIXTURE) connector = board.footprint_by_reference("CON1") hole = next(pad for pad in connector.pads if pad.number == "") assert hole.mechanical is True assert hole.electrical is False assert hole.width == 0 assert hole.height == 0 assert hole.drill_width == 1 assert hole.drill_height == 1 assert all(math.isfinite(value) for value in connector.bounds.values()) assert connector.bounds["width"] > 0 assert connector.bounds["height"] > 0