kicad-mcp/tests/test_schematic.py
Ryan Malloy b7e4fc6859 Fix pin extraction, connectivity, hierarchy, and label counting
Root cause: kicad-sch-api doesn't back-populate comp.pins or sch.nets
on loaded schematics. All data is accessible through alternative APIs.

Pin extraction: use comp.get_symbol_definition().pins for metadata and
sch.list_component_pins(ref) for schematic-transformed positions.

Connectivity: new wire-walking engine using union-find on coordinates.
Walks wires, pin positions, labels, and power symbols to reconstruct
the net graph. Replaces broken ConnectivityAnalyzer/sch.nets fallbacks.
Eliminates 'unhashable type: Net' crash.

Hierarchy: use sch.sheets.get_sheet_hierarchy() instead of the broken
sheets.data.get("sheet", []) raw dict approach.

Labels: supplement sch.get_statistics() with sch.labels.get_statistics()
and sch.hierarchical_labels for accurate counts.

99 tests passing, lint clean.
2026-03-04 16:55:19 -07:00

148 lines
5.0 KiB
Python

"""Tests for schematic tools (kicad-sch-api integration)."""
import os
import pytest
from tests.conftest import requires_sch_api
@pytest.mark.unit
def test_create_schematic(tmp_output_dir):
"""create_schematic should produce a .kicad_sch file."""
from mckicad.tools.schematic import create_schematic
output_path = os.path.join(tmp_output_dir, "test.kicad_sch")
result = create_schematic(name="test_circuit", output_path=output_path)
assert result["success"] is True
assert os.path.exists(output_path)
@pytest.mark.unit
def test_create_schematic_invalid_path():
"""create_schematic should fail gracefully for invalid paths."""
from mckicad.tools.schematic import create_schematic
result = create_schematic(name="x", output_path="/nonexistent/dir/test.kicad_sch")
assert result["success"] is False
assert "error" in result
@pytest.mark.unit
def test_search_components():
"""search_components should return results for common queries."""
from mckicad.tools.schematic import search_components
result = search_components(query="resistor")
# Should succeed even if no libs installed (returns empty results)
assert "success" in result
@pytest.mark.unit
def test_list_components_empty_schematic(tmp_output_dir):
"""list_components on new empty schematic should return empty list."""
from mckicad.tools.schematic import create_schematic, list_components
path = os.path.join(tmp_output_dir, "empty.kicad_sch")
create_schematic(name="empty", output_path=path)
result = list_components(schematic_path=path)
if result["success"]:
assert result.get("count", 0) == 0
@requires_sch_api
@pytest.mark.unit
def test_list_components_single_lookup(populated_schematic):
"""list_components with reference param should return one component."""
from mckicad.tools.schematic import list_components
result = list_components(schematic_path=populated_schematic, reference="R1")
assert result["success"] is True
assert result["count"] == 1
assert result["components"][0]["reference"] == "R1"
@requires_sch_api
@pytest.mark.unit
def test_list_components_single_lookup_not_found(populated_schematic):
"""list_components with nonexistent reference should fail."""
from mckicad.tools.schematic import list_components
result = list_components(schematic_path=populated_schematic, reference="Z99")
assert result["success"] is False
@requires_sch_api
@pytest.mark.unit
def test_get_schematic_info_compact(populated_schematic):
"""get_schematic_info should return compact output."""
from mckicad.tools.schematic import get_schematic_info
result = get_schematic_info(schematic_path=populated_schematic)
assert result["success"] is True
assert "statistics" in result
assert "validation" in result
assert "unique_symbol_count" in result
@requires_sch_api
@pytest.mark.unit
def test_get_component_detail(populated_schematic):
"""get_component_detail should return full info for one component."""
from mckicad.tools.schematic import get_component_detail
result = get_component_detail(schematic_path=populated_schematic, reference="R1")
assert result["success"] is True
assert result["component"]["reference"] == "R1"
assert result["component"]["lib_id"] == "Device:R"
@requires_sch_api
@pytest.mark.unit
def test_get_component_detail_not_found(populated_schematic):
"""get_component_detail for missing component should fail."""
from mckicad.tools.schematic import get_component_detail
result = get_component_detail(schematic_path=populated_schematic, reference="Z99")
assert result["success"] is False
@requires_sch_api
@pytest.mark.unit
def test_get_schematic_hierarchy(populated_schematic):
"""get_schematic_hierarchy should return at least the root sheet."""
from mckicad.tools.schematic import get_schematic_hierarchy
result = get_schematic_hierarchy(schematic_path=populated_schematic)
assert result["success"] is True
assert result["hierarchy"]["name"] == "root"
assert result["hierarchy"]["total_sheets"] >= 1
@pytest.mark.unit
def test_file_output_infrastructure(tmp_output_dir):
"""write_detail_file should create .mckicad sidecar directory and file."""
from mckicad.utils.file_utils import write_detail_file
fake_sch = os.path.join(tmp_output_dir, "test.kicad_sch")
# File doesn't need to exist for write_detail_file
open(fake_sch, "w").close()
data = {"test": "data", "items": [1, 2, 3]}
path = write_detail_file(fake_sch, "test_output.json", data)
assert os.path.isfile(path)
assert ".mckicad" in path
assert path.endswith("test_output.json")
@pytest.mark.unit
def test_file_output_cwd_fallback(tmp_output_dir, monkeypatch):
"""write_detail_file with None path should use CWD."""
from mckicad.utils.file_utils import write_detail_file
monkeypatch.chdir(tmp_output_dir)
path = write_detail_file(None, "test_cwd.json", {"test": True})
assert os.path.isfile(path)
assert ".mckicad" in path