"""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