Spaces:
Sleeping
Sleeping
Commit ·
17e56f5
1
Parent(s): edd81ef
test: add FastAPI route tests for chat, report, and agents endpoints
Browse files- tests/test_api_routes.py +127 -0
tests/test_api_routes.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for server/routes.py — FastAPI chat API endpoints."""
|
| 2 |
+
|
| 3 |
+
from fastapi.testclient import TestClient
|
| 4 |
+
from server.web import app
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
client = TestClient(app)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestChatEndpoint:
|
| 11 |
+
def test_basic_chat(self):
|
| 12 |
+
resp = client.post("/api/chat", json={
|
| 13 |
+
"message": "I need a bracket",
|
| 14 |
+
"history": [],
|
| 15 |
+
"backend": "mock",
|
| 16 |
+
})
|
| 17 |
+
assert resp.status_code == 200
|
| 18 |
+
data = resp.json()
|
| 19 |
+
assert "responses" in data
|
| 20 |
+
assert len(data["responses"]) > 0
|
| 21 |
+
|
| 22 |
+
def test_chat_with_mentions(self):
|
| 23 |
+
resp = client.post("/api/chat", json={
|
| 24 |
+
"message": "What do you think?",
|
| 25 |
+
"history": [],
|
| 26 |
+
"mentions": ["cnc"],
|
| 27 |
+
"backend": "mock",
|
| 28 |
+
})
|
| 29 |
+
assert resp.status_code == 200
|
| 30 |
+
data = resp.json()
|
| 31 |
+
agent_ids = [r["agent_id"] for r in data["responses"]]
|
| 32 |
+
assert "cnc" in agent_ids
|
| 33 |
+
|
| 34 |
+
def test_chat_with_history(self):
|
| 35 |
+
resp = client.post("/api/chat", json={
|
| 36 |
+
"message": "Make it wider",
|
| 37 |
+
"history": [
|
| 38 |
+
{"role": "user", "content": "I need a bracket"},
|
| 39 |
+
{"role": "agent", "agent_id": "design", "content": "L-bracket suggestion."},
|
| 40 |
+
],
|
| 41 |
+
"backend": "mock",
|
| 42 |
+
})
|
| 43 |
+
assert resp.status_code == 200
|
| 44 |
+
data = resp.json()
|
| 45 |
+
assert "responses" in data
|
| 46 |
+
|
| 47 |
+
def test_chat_empty_message_rejected(self):
|
| 48 |
+
resp = client.post("/api/chat", json={
|
| 49 |
+
"message": "",
|
| 50 |
+
"history": [],
|
| 51 |
+
"backend": "mock",
|
| 52 |
+
})
|
| 53 |
+
assert resp.status_code == 422
|
| 54 |
+
|
| 55 |
+
def test_chat_returns_design_state(self):
|
| 56 |
+
resp = client.post("/api/chat", json={
|
| 57 |
+
"message": "60mm wide aluminum bracket",
|
| 58 |
+
"history": [],
|
| 59 |
+
"backend": "mock",
|
| 60 |
+
})
|
| 61 |
+
assert resp.status_code == 200
|
| 62 |
+
data = resp.json()
|
| 63 |
+
assert "design_state" in data
|
| 64 |
+
|
| 65 |
+
def test_chat_at_mention_in_message(self):
|
| 66 |
+
resp = client.post("/api/chat", json={
|
| 67 |
+
"message": "@engineering what thickness?",
|
| 68 |
+
"history": [],
|
| 69 |
+
"backend": "mock",
|
| 70 |
+
})
|
| 71 |
+
assert resp.status_code == 200
|
| 72 |
+
data = resp.json()
|
| 73 |
+
agent_ids = [r["agent_id"] for r in data["responses"]]
|
| 74 |
+
assert "engineering" in agent_ids
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class TestReportEndpoint:
|
| 78 |
+
def test_basic_report(self):
|
| 79 |
+
resp = client.post("/api/report", json={
|
| 80 |
+
"part_name": "test_bracket",
|
| 81 |
+
"history": [
|
| 82 |
+
{"role": "agent", "agent_id": "design", "content": "L-bracket design."},
|
| 83 |
+
{"role": "agent", "agent_id": "engineering", "content": "3mm aluminum."},
|
| 84 |
+
{"role": "agent", "agent_id": "cnc", "content": "3-axis OK."},
|
| 85 |
+
],
|
| 86 |
+
"backend": "mock",
|
| 87 |
+
})
|
| 88 |
+
assert resp.status_code == 200
|
| 89 |
+
data = resp.json()
|
| 90 |
+
assert "report" in data
|
| 91 |
+
assert "test_bracket" in data["report"]
|
| 92 |
+
assert "Design Decisions" in data["report"]
|
| 93 |
+
assert "Engineering Specifications" in data["report"]
|
| 94 |
+
assert "Manufacturing Notes" in data["report"]
|
| 95 |
+
|
| 96 |
+
def test_empty_history(self):
|
| 97 |
+
resp = client.post("/api/report", json={
|
| 98 |
+
"part_name": "empty_part",
|
| 99 |
+
"history": [],
|
| 100 |
+
"backend": "mock",
|
| 101 |
+
})
|
| 102 |
+
assert resp.status_code == 200
|
| 103 |
+
data = resp.json()
|
| 104 |
+
assert "report" in data
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class TestAgentsEndpoint:
|
| 108 |
+
def test_list_agents(self):
|
| 109 |
+
resp = client.get("/api/agents")
|
| 110 |
+
assert resp.status_code == 200
|
| 111 |
+
data = resp.json()
|
| 112 |
+
assert "agents" in data
|
| 113 |
+
agent_ids = [a["id"] for a in data["agents"]]
|
| 114 |
+
assert "design" in agent_ids
|
| 115 |
+
assert "engineering" in agent_ids
|
| 116 |
+
assert "cnc" in agent_ids
|
| 117 |
+
assert "cad" in agent_ids
|
| 118 |
+
|
| 119 |
+
def test_agent_has_metadata(self):
|
| 120 |
+
resp = client.get("/api/agents")
|
| 121 |
+
data = resp.json()
|
| 122 |
+
agent = data["agents"][0]
|
| 123 |
+
assert "id" in agent
|
| 124 |
+
assert "name" in agent
|
| 125 |
+
assert "role" in agent
|
| 126 |
+
assert "color" in agent
|
| 127 |
+
assert "avatar" in agent
|