PCB_agentic_ai / backend /tests /test_validator.py
mindchainai's picture
Deploy PCB AI Studio: FastAPI + React studio at /, Gradio KiCad check at /gradio
8604d66 verified
Raw History Blame Contribute Delete
3.28 kB
"""The DRC must catch deliberately injected violations."""
import math
from app.models import Route, TracePath, Via
from app.pcb.optimizer import fix_drc
from app.pcb.validator import PCBValidator
def _checks(d):
v = PCBValidator(d).validate()
return v, {c.check: c for c in v.checks}
def test_clean_design_passes(example_design):
v, checks = _checks(example_design)
assert v.passed and all(c.status == "pass" for c in checks.values())
def test_detects_overlap_and_boundary(example_design):
d = example_design
a, b = d.components[1], d.components[2]
b.x, b.y, b.rotation = a.x, a.y, a.rotation
d.components[3].x = d.board.width + 5
v, checks = _checks(d)
assert checks["overlap"].status == "fail"
assert checks["boundary"].status == "fail"
assert not v.passed
def test_detects_duplicate_refs_invalid_coordinates_and_data(example_design):
d = example_design
d.components[4].ref = d.components[5].ref
d.components[6].x = math.nan
d.components[7].rotation = float("nan")
d.components[8].pads[0].width = 0
v, checks = _checks(d)
assert checks["duplicate_refs"].status == "fail"
assert checks["coordinates"].status == "fail"
assert checks["component_data"].status == "fail"
def test_detects_crossing_and_clearance(example_design):
d = example_design
W, H = d.board.width, d.board.height
cx, cy = W / 2, H - 3
d.routes.append(Route(net="I2C_SDA", paths=[TracePath(layer="F.Cu", width=0.2, points=[[cx - 3, cy], [cx + 3, cy]])]))
d.routes.append(Route(net="I2C_SCL", paths=[TracePath(layer="F.Cu", width=0.2, points=[[cx, cy - 2], [cx, cy + 2]])]))
d.routes.append(Route(net="EN", paths=[TracePath(layer="F.Cu", width=0.2, points=[[cx - 3, cy - 0.3], [cx - 1, cy - 0.3]])]))
_, checks = _checks(d)
assert checks["crossing"].status == "fail"
assert checks["clearance"].status == "fail"
def test_detects_unconnected_power_pin_and_width(example_design):
d = example_design
u1 = next(c for c in d.components if c.ref == "U1")
pad = next(p for p in u1.pads if p.name == "3V3")
net = next(n for n in d.nets if n.name == pad.net)
net.pins = [p for p in net.pins if not (p.ref == "U1" and p.pad == pad.number)]
pad.net = None
d.routes.append(Route(net="GND", paths=[TracePath(layer="F.Cu", width=0.05, points=[[1, 1], [1, 1.5]])],
vias=[Via(x=2, y=2, diameter=0.3, drill=0.1, net="GND")]))
_, checks = _checks(d)
assert checks["unconnected_pins"].status == "fail"
assert checks["widths"].status == "fail"
def test_fix_drc_repairs_overlap_and_crossing(example_design):
d = example_design
a, b = d.components[9], d.components[10]
b.x, b.y = a.x, a.y
cx, cy = d.board.width / 2, d.board.height - 3
d.routes.append(Route(net="I2C_SDA", paths=[TracePath(layer="F.Cu", width=0.2, points=[[cx - 3, cy], [cx + 3, cy]])]))
before = PCBValidator(d).validate()
assert before.errors > 0
rep = fix_drc(d, effort="fast", route_timeout_s=30)
assert rep["errors_after"] < rep["errors_before"]
assert d.validation.errors == 0, [i.message for i in d.validation.issues if i.severity == "error"]