test_annotations_and_eligibility.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from __future__ import annotations
  2. import sys
  3. from pathlib import Path
  4. import pytest
  5. ROOT = Path(__file__).resolve().parents[2]
  6. VIEWER_ROOT = ROOT / "tools" / "board-viewer"
  7. sys.path.insert(0, str(VIEWER_ROOT))
  8. from board_viewer.annotations import AnnotationError, validate_annotation_references # noqa: E402
  9. from board_viewer.eligibility import evaluate_recommendation # noqa: E402
  10. TOUCHPOINTS = {
  11. "CON78": {
  12. "component_value": "CON_11251W00_NPX_SX_4P",
  13. "pins": [
  14. {"number": "1"},
  15. {"number": "2"},
  16. {"number": "3"},
  17. {"number": "4"},
  18. {"number": "5"},
  19. {"number": "6"},
  20. {"number": ""},
  21. ],
  22. },
  23. }
  24. @pytest.mark.parametrize(
  25. "annotations, expected_message",
  26. [
  27. (
  28. {"touchpoints": {"CON99": {"pins": {}}}},
  29. "unknown touchpoint CON99",
  30. ),
  31. (
  32. {"touchpoints": {"CON78": {"pins": {"99": {"summary": "invalid"}}}}},
  33. "unknown pin CON78.99",
  34. ),
  35. ],
  36. )
  37. def test_annotations_must_reference_real_touchpoints_and_pins(
  38. annotations: dict[str, object], expected_message: str
  39. ) -> None:
  40. with pytest.raises(AnnotationError, match=expected_message):
  41. validate_annotation_references(annotations, TOUCHPOINTS)
  42. @pytest.mark.parametrize(
  43. "profile, expected_message",
  44. [
  45. (
  46. {
  47. "references": ["CON99"],
  48. "contact_pins": ["1"],
  49. "shield_pads": [],
  50. },
  51. "unknown connector CON99",
  52. ),
  53. (
  54. {
  55. "references": ["CON78"],
  56. "contact_pins": ["1", "2", "3", "4"],
  57. "shield_pads": ["5"],
  58. },
  59. "unclassified numbered pads CON78: 6",
  60. ),
  61. (
  62. {
  63. "references": ["CON78"],
  64. "contact_pins": ["1", "2", "3", "4", "5"],
  65. "shield_pads": ["5", "6"],
  66. },
  67. "overlapping contact and shield pads",
  68. ),
  69. (
  70. {
  71. "references": ["CON78"],
  72. "contact_pins": ["1", "2", "3", "4", "99"],
  73. "shield_pads": ["5", "6"],
  74. },
  75. "unknown classified pads CON78: 99",
  76. ),
  77. ],
  78. )
  79. def test_connector_profiles_fail_closed_on_invalid_pad_classification(
  80. profile: dict[str, object], expected_message: str
  81. ) -> None:
  82. annotations = {
  83. "touchpoints": {},
  84. "connector_profiles": {"CON_11251W00_NPX_SX_4P": profile},
  85. }
  86. with pytest.raises(AnnotationError, match=expected_message):
  87. validate_annotation_references(annotations, TOUCHPOINTS)
  88. def test_connector_profile_component_value_must_match_board_database() -> None:
  89. annotations = {
  90. "touchpoints": {},
  91. "connector_profiles": {
  92. "WRONG_VALUE": {
  93. "references": ["CON78"],
  94. "contact_pins": ["1", "2", "3", "4"],
  95. "shield_pads": ["5", "6"],
  96. }
  97. },
  98. }
  99. with pytest.raises(AnnotationError, match="component value mismatch CON78"):
  100. validate_annotation_references(annotations, TOUCHPOINTS)
  101. def test_connector_reference_may_only_appear_in_one_profile() -> None:
  102. profile = {
  103. "references": ["CON78"],
  104. "contact_pins": ["1", "2", "3", "4"],
  105. "shield_pads": ["5", "6"],
  106. }
  107. annotations = {
  108. "touchpoints": {},
  109. "connector_profiles": {"FIRST": profile, "SECOND": profile},
  110. }
  111. with pytest.raises(AnnotationError, match="connector CON78 appears in multiple profiles"):
  112. validate_annotation_references(annotations, TOUCHPOINTS)
  113. def eligible_pin(**overrides: object) -> dict[str, object]:
  114. pin: dict[str, object] = {
  115. "number": "1",
  116. "pad_role": "contact",
  117. "mechanical": False,
  118. "electrical": True,
  119. "net": "UART2_TX",
  120. "function_category": "uart_tx",
  121. "reserved": False,
  122. "fixed_load_conflict": False,
  123. "external_level": "RS232",
  124. "evidence_tier": "cross_verified",
  125. "fpga": {
  126. "ball": "M10",
  127. "io_standard": "LVCMOS33",
  128. "capabilities": ["gpio", "uart_tx"],
  129. "evidence_tier": "cross_verified",
  130. },
  131. }
  132. pin.update(overrides)
  133. return pin
  134. def test_verified_capability_match_is_recommendable_with_reasons() -> None:
  135. result = evaluate_recommendation(eligible_pin(), "uart_tx")
  136. assert result.eligible is True
  137. assert result.reasons == ["capability_match", "verified_fpga_path", "external_level_known"]
  138. @pytest.mark.parametrize(
  139. "pin, capability, reason",
  140. [
  141. (eligible_pin(evidence_tier="name_inferred"), "uart_tx", "pin_evidence_not_verified"),
  142. (eligible_pin(evidence_tier="unknown"), "uart_tx", "pin_evidence_not_verified"),
  143. (eligible_pin(net="GND"), "uart_tx", "power_or_ground"),
  144. (eligible_pin(mechanical=True, electrical=False), "uart_tx", "mechanical_or_non_electrical"),
  145. (eligible_pin(pad_role="shield"), "uart_tx", "not_connector_contact"),
  146. (eligible_pin(pad_role="unknown"), "uart_tx", "not_connector_contact"),
  147. (eligible_pin(reserved=True), "uart_tx", "reserved"),
  148. (eligible_pin(function_category="power", net="+3V3"), "uart_tx", "power_or_ground"),
  149. (eligible_pin(fixed_load_conflict=True), "uart_tx", "fixed_load_conflict"),
  150. (eligible_pin(external_level="unknown"), "uart_tx", "external_level_unknown"),
  151. (eligible_pin(), "pwm", "capability_mismatch"),
  152. ],
  153. )
  154. def test_ineligible_pin_classes_are_rejected(
  155. pin: dict[str, object], capability: str, reason: str
  156. ) -> None:
  157. result = evaluate_recommendation(pin, capability)
  158. assert result.eligible is False
  159. assert reason in result.reasons