| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- """Multi-pass resistor index acceptance (A–H structural / search gates)."""
- from __future__ import annotations
- import json
- import sys
- from pathlib import Path
- import pytest
- ROOT = Path(__file__).resolve().parents[2]
- VIEWER_ROOT = ROOT / "tools" / "board-viewer"
- GENERATED = VIEWER_ROOT / "generated" / "board-data.json"
- CACHED_PCB = Path("/data/kicad/work/board.kicad_pcb")
- sys.path.insert(0, str(VIEWER_ROOT))
- from board_viewer.data import ( # noqa: E402
- annotation_resistor_refs_from_board_data,
- parse_resistance_ohms,
- resistance_value_tokens,
- validate_resistor_index,
- )
- from board_viewer.pcb import parse_kicad_pcb, select_resistors # noqa: E402
- @pytest.fixture(scope="module")
- def board_data() -> dict:
- payload = json.loads(GENERATED.read_text(encoding="utf-8"))
- assert isinstance(payload, dict)
- return payload
- @pytest.mark.skipif(not CACHED_PCB.is_file(), reason="cached KiCad PCB not available")
- def test_pass_a_pcb_resistor_set_equals_board_data(board_data: dict) -> None:
- board = parse_kicad_pcb(CACHED_PCB)
- pcb_refs = {footprint.reference for footprint in select_resistors(board)}
- json_refs = {item["reference"] for item in board_data["resistors"]}
- assert pcb_refs == json_refs
- validate_resistor_index(board_data, pcb_references=pcb_refs)
- def test_pass_b_resistor_structure_and_counts(board_data: dict) -> None:
- validate_resistor_index(board_data)
- assert board_data["counts"]["resistors"] == 644
- assert board_data["counts"]["resistor_pads"] == 1288
- assert board_data["counts"]["categories"]["R"] == 644
- assert len(board_data["resistors"]) == 644
- def test_pass_c_resistance_value_roundtrip() -> None:
- assert parse_resistance_ohms("4.7k") == 4700
- assert parse_resistance_ohms("4k7") == 4700
- assert parse_resistance_ohms("4700") == 4700
- assert parse_resistance_ohms("22R") == 22
- assert parse_resistance_ohms("0R") == 0
- assert parse_resistance_ohms("") is None
- assert parse_resistance_ohms("mystery") is None
- tokens = set(resistance_value_tokens("4.7k", 4700))
- assert {"4.7k", "4k7", "4700", "4700r"} <= tokens
- # Unparseable raw string remains searchable via tokens.
- assert "mystery" in resistance_value_tokens("mystery", None)
- def test_pass_g_annotation_refs_subset_of_index(board_data: dict) -> None:
- annotated = annotation_resistor_refs_from_board_data(board_data)
- index = {item["reference"] for item in board_data["resistors"]}
- assert annotated <= index
- def test_pass_h_expected_counts_locked(board_data: dict) -> None:
- assert board_data["counts"]["resistors"] == 644
- assert board_data["counts"]["resistor_pads"] == 1288
- for required in ("R7", "R12", "R63", "R674", "R1225"):
- assert any(item["reference"] == required for item in board_data["resistors"])
- def test_select_resistors_excludes_rn_and_touchpoints() -> None:
- from board_viewer.pcb import Footprint, ParsedBoard, select_resistors as select
- footprints = [
- Footprint("R12", "22R", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
- Footprint("RN1", "4x10k", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
- Footprint("CON1", "X", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
- Footprint("R1225", "10k", 0, 0, 0, "front", "F.Cu", [], {"x": 0, "y": 0, "width": 1, "height": 1}),
- ]
- parsed = ParsedBoard(footprints=footprints, footprint_count=4, bounds={"x": 0, "y": 0, "width": 10, "height": 10})
- assert [item.reference for item in select(parsed)] == ["R12", "R1225"]
- def test_pass_c_rejects_dnp_and_package_sizes() -> None:
- assert parse_resistance_ohms("DNP") is None
- assert parse_resistance_ohms("NC") is None
- assert parse_resistance_ohms("0805") is None
- assert parse_resistance_ohms("R / 1% / 0805") is None
- assert parse_resistance_ohms("120R/1%/0805") == 120
- def test_pass_b_schema_v2_and_pad_geometry(board_data: dict) -> None:
- assert board_data["schema"] == "board-viewer-data-v2"
- assert board_data["counts"]["resistor_electrical_pads"] == 1288
- r674 = next(item for item in board_data["resistors"] if item["reference"] == "R674")
- assert r674["side"] == "back"
- for pad in r674["pads"]:
- for key in ("id", "bounds", "view_bounds", "rotation", "shape", "layers"):
- assert key in pad
- conflicts = board_data.get("evidence_conflicts") or []
- assert any(item.get("subject_reference") == "R674" and item.get("status") == "unresolved" for item in conflicts)
- def test_pass_g4_r674_conflict_does_not_pollute_primary_nets(board_data: dict) -> None:
- r674 = next(item for item in board_data["resistors"] if item["reference"] == "R674")
- nets = {pad["net"] for pad in r674["pads"]}
- assert nets == {"BANK0_GPIO15", "PUL1"}
- assert "BANK1_GPIO18" not in nets
- def test_pass_c_structured_zero_and_nc() -> None:
- from board_viewer.data import detect_assembly_status
- assert parse_resistance_ohms("R / 0 /1%/0805") == 0
- assert parse_resistance_ohms("R/0/1%/1210") == 0
- assert parse_resistance_ohms("R / 0 /1%/0805/NC") == 0
- assert detect_assembly_status("R / 0 /1%/0805/NC") == "not_fitted"
- assert detect_assembly_status("R / 0 /1%/0805") == "unknown"
- assert "0r" in set(resistance_value_tokens("R / 0 /1%/0805", 0))
- def test_pass_b_side_counts_and_coverage(board_data: dict) -> None:
- sides = board_data["counts"]["resistor_sides"]
- assert sides["front"] + sides["back"] == 644
- assert sides["front"] == 435
- assert sides["back"] == 209
- assert board_data["counts"]["touchpoint_sides"]["front"] == board_data["counts"]["sides"]["front"]
- cov = board_data["value_coverage"]
- assert cov["resistors_total"] == 644
- assert cov["value_parsed"] >= 1
- assert cov["component_value_source_status"] == "brd_footprint_field_limited_coverage"
- r806 = next(item for item in board_data["resistors"] if item["reference"] == "R806")
- assert r806["value_ohms"] == 120
- zeros = [item for item in board_data["resistors"] if item.get("value_ohms") == 0]
- assert zeros
- assert any(item["assembly_status"] == "not_fitted" for item in zeros)
- def test_pass_g_structured_resistor_links_r674(board_data: dict) -> None:
- links = board_data.get("resistor_links") or []
- assert links
- pul = next(item for item in links if item["resistor_reference"] == "R674" and str(item["pad_number"]) == "2")
- assert pul["claimed_net"] == "PUL1"
- assert pul["pcb_net"] == "PUL1"
- assert pul["status"] == "resolved"
- def test_pass_g_wrong_claimed_net_fail_closed() -> None:
- from board_viewer.data import _attach_structured_resistor_links
- resistors = [
- {
- "reference": "R674",
- "pads": [{"number": "2", "net": "PUL1"}],
- }
- ]
- annotations = {
- "resistor_links": [
- {
- "source_pin_id": "CON104.pad-2",
- "resistor_reference": "R674",
- "pad_number": "2",
- "claimed_net": "WRONG_NET",
- "evidence_source": "fixture",
- }
- ]
- }
- links = _attach_structured_resistor_links(annotations, resistors, [])
- assert links[0]["status"] == "net_conflict"
- assert links[0]["pcb_net"] == "PUL1"
|