test_resistor_index.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """Multi-pass resistor index acceptance (A–H structural / search gates)."""
  2. from __future__ import annotations
  3. import json
  4. import sys
  5. from pathlib import Path
  6. import pytest
  7. ROOT = Path(__file__).resolve().parents[2]
  8. VIEWER_ROOT = ROOT / "tools" / "board-viewer"
  9. GENERATED = VIEWER_ROOT / "generated" / "board-data.json"
  10. CACHED_PCB = Path("/data/kicad/work/board.kicad_pcb")
  11. sys.path.insert(0, str(VIEWER_ROOT))
  12. from board_viewer.data import ( # noqa: E402
  13. annotation_resistor_refs_from_board_data,
  14. parse_resistance_ohms,
  15. resistance_value_tokens,
  16. validate_resistor_index,
  17. )
  18. from board_viewer.pcb import parse_kicad_pcb, select_resistors # noqa: E402
  19. @pytest.fixture(scope="module")
  20. def board_data() -> dict:
  21. payload = json.loads(GENERATED.read_text(encoding="utf-8"))
  22. assert isinstance(payload, dict)
  23. return payload
  24. @pytest.mark.skipif(not CACHED_PCB.is_file(), reason="cached KiCad PCB not available")
  25. def test_pass_a_pcb_resistor_set_equals_board_data(board_data: dict) -> None:
  26. board = parse_kicad_pcb(CACHED_PCB)
  27. pcb_refs = {footprint.reference for footprint in select_resistors(board)}
  28. json_refs = {item["reference"] for item in board_data["resistors"]}
  29. assert pcb_refs == json_refs
  30. validate_resistor_index(board_data, pcb_references=pcb_refs)
  31. def test_pass_b_resistor_structure_and_counts(board_data: dict) -> None:
  32. validate_resistor_index(board_data)
  33. assert board_data["counts"]["resistors"] == 644
  34. assert board_data["counts"]["resistor_pads"] == 1288
  35. assert board_data["counts"]["categories"]["R"] == 644
  36. assert len(board_data["resistors"]) == 644
  37. def test_pass_c_resistance_value_roundtrip() -> None:
  38. assert parse_resistance_ohms("4.7k") == 4700
  39. assert parse_resistance_ohms("4k7") == 4700
  40. assert parse_resistance_ohms("4700") == 4700
  41. assert parse_resistance_ohms("22R") == 22
  42. assert parse_resistance_ohms("0R") == 0
  43. assert parse_resistance_ohms("") is None
  44. assert parse_resistance_ohms("mystery") is None
  45. tokens = set(resistance_value_tokens("4.7k", 4700))
  46. assert {"4.7k", "4k7", "4700", "4700r"} <= tokens
  47. # Unparseable raw string remains searchable via tokens.
  48. assert "mystery" in resistance_value_tokens("mystery", None)
  49. def test_pass_g_annotation_refs_subset_of_index(board_data: dict) -> None:
  50. annotated = annotation_resistor_refs_from_board_data(board_data)
  51. index = {item["reference"] for item in board_data["resistors"]}
  52. assert annotated <= index
  53. def test_pass_h_expected_counts_locked(board_data: dict) -> None:
  54. assert board_data["counts"]["resistors"] == 644
  55. assert board_data["counts"]["resistor_pads"] == 1288
  56. for required in ("R7", "R12", "R63", "R674", "R1225"):
  57. assert any(item["reference"] == required for item in board_data["resistors"])
  58. def test_select_resistors_excludes_rn_and_touchpoints() -> None:
  59. from board_viewer.pcb import Footprint, ParsedBoard, select_resistors as select
  60. footprints = [
  61. Footprint("R12", "22R", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
  62. Footprint("RN1", "4x10k", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
  63. Footprint("CON1", "X", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
  64. Footprint("R1225", "10k", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
  65. ]
  66. parsed = ParsedBoard(footprints=footprints, footprint_count=4, bounds={"x": 0, "y": 0, "width": 10, "height": 10})
  67. assert [item.reference for item in select(parsed)] == ["R12", "R1225"]
  68. def test_pass_c_rejects_dnp_and_package_sizes() -> None:
  69. assert parse_resistance_ohms("DNP") is None
  70. assert parse_resistance_ohms("NC") is None
  71. assert parse_resistance_ohms("0805") is None
  72. assert parse_resistance_ohms("R / 1% / 0805") is None
  73. assert parse_resistance_ohms("120R/1%/0805") == 120
  74. def test_pass_b_schema_v2_and_pad_geometry(board_data: dict) -> None:
  75. assert board_data["schema"] == "board-viewer-data-v2"
  76. assert board_data["counts"]["resistor_electrical_pads"] == 1288
  77. r674 = next(item for item in board_data["resistors"] if item["reference"] == "R674")
  78. assert r674["side"] == "back"
  79. for pad in r674["pads"]:
  80. for key in ("id", "bounds", "view_bounds", "rotation", "shape", "layers"):
  81. assert key in pad
  82. conflicts = board_data.get("evidence_conflicts") or []
  83. assert any(item.get("subject_reference") == "R674" and item.get("status") == "unresolved" for item in conflicts)
  84. def test_pass_g4_r674_conflict_does_not_pollute_primary_nets(board_data: dict) -> None:
  85. r674 = next(item for item in board_data["resistors"] if item["reference"] == "R674")
  86. nets = {pad["net"] for pad in r674["pads"]}
  87. assert nets == {"BANK0_GPIO15", "PUL1"}
  88. assert "BANK1_GPIO18" not in nets
  89. def test_pass_c_structured_zero_and_nc() -> None:
  90. from board_viewer.data import detect_assembly_status
  91. assert parse_resistance_ohms("R / 0 /1%/0805") == 0
  92. assert parse_resistance_ohms("R/0/1%/1210") == 0
  93. assert parse_resistance_ohms("R / 0 /1%/0805/NC") == 0
  94. assert detect_assembly_status("R / 0 /1%/0805/NC") == "not_fitted"
  95. assert detect_assembly_status("R / 0 /1%/0805") == "unknown"
  96. assert "0r" in set(resistance_value_tokens("R / 0 /1%/0805", 0))
  97. def test_pass_b_side_counts_and_coverage(board_data: dict) -> None:
  98. sides = board_data["counts"]["resistor_sides"]
  99. assert sides["front"] + sides["back"] == 644
  100. assert sides["front"] == 435
  101. assert sides["back"] == 209
  102. assert board_data["counts"]["touchpoint_sides"]["front"] == board_data["counts"]["sides"]["front"]
  103. cov = board_data["value_coverage"]
  104. assert cov["resistors_total"] == 644
  105. assert cov["value_parsed"] >= 1
  106. assert cov["component_value_source_status"] == "brd_footprint_field_limited_coverage"
  107. r806 = next(item for item in board_data["resistors"] if item["reference"] == "R806")
  108. assert r806["value_ohms"] == 120
  109. zeros = [item for item in board_data["resistors"] if item.get("value_ohms") == 0]
  110. assert zeros
  111. assert any(item["assembly_status"] == "not_fitted" for item in zeros)
  112. def test_pass_g_structured_resistor_links_r674(board_data: dict) -> None:
  113. links = board_data.get("resistor_links") or []
  114. assert links
  115. pul = next(item for item in links if item["resistor_reference"] == "R674" and str(item["pad_number"]) == "2")
  116. assert pul["claimed_net"] == "PUL1"
  117. assert pul["pcb_net"] == "PUL1"
  118. assert pul["status"] == "resolved"
  119. def test_pass_g_wrong_claimed_net_fail_closed() -> None:
  120. from board_viewer.data import _attach_structured_resistor_links
  121. resistors = [
  122. {
  123. "reference": "R674",
  124. "pads": [{"number": "2", "net": "PUL1"}],
  125. }
  126. ]
  127. annotations = {
  128. "resistor_links": [
  129. {
  130. "source_pin_id": "CON104.pad-2",
  131. "resistor_reference": "R674",
  132. "pad_number": "2",
  133. "claimed_net": "WRONG_NET",
  134. "evidence_source": "fixture",
  135. }
  136. ]
  137. }
  138. links = _attach_structured_resistor_links(annotations, resistors, [])
  139. assert links[0]["status"] == "net_conflict"
  140. assert links[0]["pcb_net"] == "PUL1"