from __future__ import annotations import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] VIEWER_ROOT = ROOT / "tools" / "board-viewer" sys.path.insert(0, str(VIEWER_ROOT)) from board_viewer.annotations import AnnotationError, validate_annotation_references # noqa: E402 from board_viewer.eligibility import evaluate_recommendation # noqa: E402 TOUCHPOINTS = { "CON78": { "component_value": "CON_11251W00_NPX_SX_4P", "pins": [ {"number": "1"}, {"number": "2"}, {"number": "3"}, {"number": "4"}, {"number": "5"}, {"number": "6"}, {"number": ""}, ], }, } @pytest.mark.parametrize( "annotations, expected_message", [ ( {"touchpoints": {"CON99": {"pins": {}}}}, "unknown touchpoint CON99", ), ( {"touchpoints": {"CON78": {"pins": {"99": {"summary": "invalid"}}}}}, "unknown pin CON78.99", ), ], ) def test_annotations_must_reference_real_touchpoints_and_pins( annotations: dict[str, object], expected_message: str ) -> None: with pytest.raises(AnnotationError, match=expected_message): validate_annotation_references(annotations, TOUCHPOINTS) @pytest.mark.parametrize( "profile, expected_message", [ ( { "references": ["CON99"], "contact_pins": ["1"], "shield_pads": [], }, "unknown connector CON99", ), ( { "references": ["CON78"], "contact_pins": ["1", "2", "3", "4"], "shield_pads": ["5"], }, "unclassified numbered pads CON78: 6", ), ( { "references": ["CON78"], "contact_pins": ["1", "2", "3", "4", "5"], "shield_pads": ["5", "6"], }, "overlapping contact and shield pads", ), ( { "references": ["CON78"], "contact_pins": ["1", "2", "3", "4", "99"], "shield_pads": ["5", "6"], }, "unknown classified pads CON78: 99", ), ], ) def test_connector_profiles_fail_closed_on_invalid_pad_classification( profile: dict[str, object], expected_message: str ) -> None: annotations = { "touchpoints": {}, "connector_profiles": {"CON_11251W00_NPX_SX_4P": profile}, } with pytest.raises(AnnotationError, match=expected_message): validate_annotation_references(annotations, TOUCHPOINTS) def test_connector_profile_component_value_must_match_board_database() -> None: annotations = { "touchpoints": {}, "connector_profiles": { "WRONG_VALUE": { "references": ["CON78"], "contact_pins": ["1", "2", "3", "4"], "shield_pads": ["5", "6"], } }, } with pytest.raises(AnnotationError, match="component value mismatch CON78"): validate_annotation_references(annotations, TOUCHPOINTS) def test_connector_reference_may_only_appear_in_one_profile() -> None: profile = { "references": ["CON78"], "contact_pins": ["1", "2", "3", "4"], "shield_pads": ["5", "6"], } annotations = { "touchpoints": {}, "connector_profiles": {"FIRST": profile, "SECOND": profile}, } with pytest.raises(AnnotationError, match="connector CON78 appears in multiple profiles"): validate_annotation_references(annotations, TOUCHPOINTS) def eligible_pin(**overrides: object) -> dict[str, object]: pin: dict[str, object] = { "number": "1", "pad_role": "contact", "mechanical": False, "electrical": True, "net": "UART2_TX", "function_category": "uart_tx", "reserved": False, "fixed_load_conflict": False, "external_level": "RS232", "evidence_tier": "cross_verified", "fpga": { "ball": "M10", "io_standard": "LVCMOS33", "capabilities": ["gpio", "uart_tx"], "evidence_tier": "cross_verified", }, } pin.update(overrides) return pin def test_verified_capability_match_is_recommendable_with_reasons() -> None: result = evaluate_recommendation(eligible_pin(), "uart_tx") assert result.eligible is True assert result.reasons == ["capability_match", "verified_fpga_path", "external_level_known"] @pytest.mark.parametrize( "pin, capability, reason", [ (eligible_pin(evidence_tier="name_inferred"), "uart_tx", "pin_evidence_not_verified"), (eligible_pin(evidence_tier="unknown"), "uart_tx", "pin_evidence_not_verified"), (eligible_pin(net="GND"), "uart_tx", "power_or_ground"), (eligible_pin(mechanical=True, electrical=False), "uart_tx", "mechanical_or_non_electrical"), (eligible_pin(pad_role="shield"), "uart_tx", "not_connector_contact"), (eligible_pin(pad_role="unknown"), "uart_tx", "not_connector_contact"), (eligible_pin(reserved=True), "uart_tx", "reserved"), (eligible_pin(function_category="power", net="+3V3"), "uart_tx", "power_or_ground"), (eligible_pin(fixed_load_conflict=True), "uart_tx", "fixed_load_conflict"), (eligible_pin(external_level="unknown"), "uart_tx", "external_level_unknown"), (eligible_pin(), "pwm", "capability_mismatch"), ], ) def test_ineligible_pin_classes_are_rejected( pin: dict[str, object], capability: str, reason: str ) -> None: result = evaluate_recommendation(pin, capability) assert result.eligible is False assert reason in result.reasons