PCB_agentic_ai / backend /tests /test_api.py
mindchainai's picture
Deploy PCB AI Studio: FastAPI + React studio at /, Gradio KiCad check at /gradio
8604d66 verified
Raw History Blame Contribute Delete
6.9 kB
"""End-to-end API tests for every endpoint (sync + background jobs + errors)."""
import time
import pytest
from fastapi.testclient import TestClient
from app.main import app
from conftest import EXAMPLE
client = TestClient(app)
@pytest.fixture(scope="module")
def project_id():
r = client.post("/api/projects", json={"name": "ESP32 Temp Monitor", "spec": {"description": EXAMPLE}})
assert r.status_code == 201
return r.json()["id"]
def _wait(job_id, timeout=120):
t0 = time.time()
while time.time() - t0 < timeout:
j = client.get(f"/api/jobs/{job_id}").json()
if j["status"] in ("succeeded", "failed"):
return j
time.sleep(0.2)
raise AssertionError("job timed out")
def test_health_and_library():
h = client.get("/api/health").json()
assert h["status"] == "ok" and h["ai"]["provider"] == "none"
parts = client.get("/api/library").json()["parts"]
assert any(p["key"] == "esp32_wroom_32" for p in parts)
def test_project_crud():
r = client.post("/api/projects", json={"name": "tmp"})
pid = r.json()["id"]
assert client.get(f"/api/projects/{pid}").json()["name"] == "tmp"
r = client.put(f"/api/projects/{pid}", json={"name": "renamed", "spec": {"description": "ESP32 with LED"}})
assert r.json()["name"] == "renamed" and r.json()["spec"]["description"] == "ESP32 with LED"
assert any(p["id"] == pid for p in client.get("/api/projects").json()["projects"])
assert client.delete(f"/api/projects/{pid}").status_code == 204
assert client.get(f"/api/projects/{pid}").status_code == 404
def test_full_pipeline(project_id):
pid = project_id
r = client.post("/api/pcb/generate", json={"project_id": pid})
assert r.status_code == 200, r.text
body = r.json()
for key in ("board", "components", "nets", "routes", "metrics", "validation", "steps"):
assert key in body
assert [s["status"] for s in body["steps"]] == ["done"] * 8
assert body["metrics"]["routing_percent"] == 100.0 and body["validation"]["errors"] == 0
rev = body["revision"]
r = client.post("/api/pcb/place", json={"project_id": pid})
assert r.status_code == 200 and r.json()["routes"] == [] and r.json()["metrics"]["routed_nets"] == 0
assert r.json()["revision"] == rev + 1
r = client.post("/api/pcb/route", json={"project_id": pid})
assert r.status_code == 200 and r.json()["metrics"]["routing_percent"] > 90
r = client.post("/api/pcb/validate", json={"project_id": pid})
assert r.status_code == 200 and {c["check"] for c in r.json()["validation"]["checks"]} >= {"overlap", "clearance"}
r = client.post("/api/pcb/optimize", json={"project_id": pid, "settings": {"effort": "fast", "seed": 7}})
assert r.status_code == 200
rep = r.json()["report"]
assert rep["rounds"] >= 1 and "total_trace_length_mm" in rep["before"]
if rep["improved"]:
assert rep["after"]["routing_percent"] >= rep["before"]["routing_percent"]
r = client.post("/api/pcb/fix-drc", json={"project_id": pid})
assert r.status_code == 200 and "actions" in r.json()["report"]
def test_component_edit_and_background_route(project_id):
pid = project_id
d = client.get(f"/api/projects/{pid}").json()["design"]
cur = next(c for c in d["components"] if c["part"] == "lm75b")
ref, new_rot = cur["ref"], (cur["rotation"] + 90) % 360
r = client.patch(f"/api/projects/{pid}/components/{ref}", json={"rotation": new_rot, "locked": True})
assert r.status_code == 200
comp = next(c for c in r.json()["components"] if c["ref"] == ref)
assert comp["rotation"] == new_rot and comp["locked"] is True
assert r.json()["report"]["ripped_nets"]
r45 = client.patch(f"/api/projects/{pid}/components/{ref}", json={"rotation": 45, "locked": False})
assert r45.status_code == 200
assert next(c for c in r45.json()["components"] if c["ref"] == ref)["rotation"] == 45
bad = client.patch(f"/api/projects/{pid}/components/{ref}", content=b'{"x": Infinity}',
headers={"content-type": "application/json"})
assert bad.status_code == 400 and bad.json()["error"]["code"] == "INVALID_MOVE"
assert client.patch(f"/api/projects/{pid}/components/NOPE", json={"x": 1}).status_code == 404
r = client.post("/api/pcb/route", json={"project_id": pid, "mode": "unrouted", "background": True})
assert r.status_code == 202
job = _wait(r.json()["job_id"])
assert job["status"] == "succeeded", job
assert job["progress"] == 1.0 and job["result"]["metrics"]["unrouted_nets"] >= 0
def test_exports(project_id):
for fmt, marker in [("json", b'"format"'), ("layout", b'"tracks"'), ("kicad", b"(kicad_pcb"), ("bom", b"References")]:
r = client.get(f"/api/projects/{project_id}/export/{fmt}")
assert r.status_code == 200 and marker in r.content, fmt
assert "attachment" in r.headers["content-disposition"]
assert client.get(f"/api/projects/{project_id}/export/gerber").status_code == 422
def test_error_responses():
r = client.post("/api/pcb/generate", json={"project_id": "missing"})
assert r.status_code == 404 and r.json()["error"]["code"] == "PROJECT_NOT_FOUND"
assert client.post("/api/projects", json={"name": ""}).json()["error"]["code"] == "VALIDATION_ERROR"
pid = client.post("/api/projects", json={"name": "bad"}).json()["id"]
r = client.post("/api/pcb/route", json={"project_id": pid})
assert r.status_code == 409 and r.json()["error"]["code"] == "NO_DESIGN"
r = client.post("/api/pcb/generate", json={"project_id": pid, "spec": {"description": ""}})
assert r.status_code == 422 and r.json()["error"]["code"] == "EMPTY_SPEC"
r = client.post("/api/pcb/generate", json={"project_id": pid, "spec": {"description": "a board with a warp core"}})
assert r.status_code == 422 and r.json()["error"]["code"] == "NO_COMPONENTS"
r = client.post("/api/pcb/generate", json={"project_id": pid, "spec": {
"description": EXAMPLE, "board": {"width_mm": 12, "height_mm": 12}}})
assert r.status_code == 422 and r.json()["error"]["code"] == "BOARD_TOO_SMALL"
r = client.post("/api/pcb/generate", json={"project_id": pid, "spec": {
"description": EXAMPLE, "rules": {"clearance": 0.01}}})
assert r.status_code == 422 and r.json()["error"]["code"] == "INVALID_RULES"
# failed background job surfaces the error
r = client.post("/api/pcb/generate", json={"project_id": pid, "background": True,
"spec": {"description": "warp core only"}})
job = _wait(r.json()["job_id"])
assert job["status"] == "failed" and job["error"]["code"] == "NO_COMPONENTS"
assert client.get("/api/jobs/doesnotexist").status_code == 404