-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
162 lines (136 loc) · 4.79 KB
/
Copy pathtest_api.py
File metadata and controls
162 lines (136 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
"""
API tests for noema-agent.
Uses FastAPI TestClient — no live server required.
Covers: root, health, echo task, unsupported task.
"""
import uuid
import pytest
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
"""Health endpoint returns 200 with executor=ready."""
response = client.get("/health")
assert response.status_code == 200
body = response.json()
assert body["status"] == "healthy"
assert body["executor"] == "ready"
assert "echo" in body["supported_tasks"]
def test_root():
"""Root endpoint returns service identity."""
response = client.get("/")
assert response.status_code == 200
body = response.json()
assert body["service"] == "noema-agent"
assert body["status"] == "ready"
def test_echo_task():
"""Echo task returns payload unchanged with success status."""
request_id = str(uuid.uuid4())
payload = {
"session_id": "test-123",
"request_id": request_id,
"task_type": "echo",
"payload": {"message": "Hello, Noema!", "data": [1, 2, 3]},
}
response = client.post("/invoke", json=payload)
assert response.status_code == 200
body = response.json()
assert body["status"] == "success"
assert body["session_id"] == "test-123"
assert body["request_id"] == request_id
assert body["result"] == payload["payload"]
assert body["error"] is None
assert isinstance(body["execution_time_ms"], int)
assert body["execution_time_ms"] >= 0
def test_route_contract_minimal_request():
"""/v1/route accepts minimal valid request and returns planned stub metadata."""
payload = {
"intent": "answer_question",
"input": "What is Noema?",
}
response = client.post("/v1/route", json=payload)
assert response.status_code == 200
body = response.json()
assert body["route"] == "local_echo"
assert body["status"] == "planned"
assert body["confidence"] == 0.5
assert body["requires_approval"] is False
assert body["approval_reason"] is None
assert "Route Contract v0 stub" in body["reason"]
assert "no route was executed" in body["reason"]
assert isinstance(body["requires_approval"], bool)
assert uuid.UUID(body["trace_id"])
assert uuid.UUID(body["request_id"])
assert uuid.UUID(body["audit_id"])
def test_route_contract_preserves_request_id_and_accepts_object_input():
"""/v1/route accepts object input and echoes caller request_id."""
request_id = str(uuid.uuid4())
payload = {
"intent": "summarize",
"input": {"text": "Short input"},
"session_id": "session-route-123",
"request_id": request_id,
"available_models": ["local-test-model"],
"available_tools": ["search"],
"context": {"privacy_level": "local"},
"metadata": {"caller": "test"},
}
response = client.post("/v1/route", json=payload)
assert response.status_code == 200
body = response.json()
required_fields = {
"route",
"reason",
"confidence",
"requires_approval",
"approval_reason",
"audit_id",
"trace_id",
"request_id",
"status",
}
assert required_fields.issubset(body.keys())
assert body["request_id"] == request_id
assert body["status"] == "planned"
assert isinstance(body["requires_approval"], bool)
assert uuid.UUID(body["trace_id"])
assert uuid.UUID(body["audit_id"])
def test_unsupported_task():
"""Unsupported task_type returns structured E-EXEC-001 error."""
request_id = str(uuid.uuid4())
payload = {
"session_id": "test-456",
"request_id": request_id,
"task_type": "unsupported_operation",
"payload": {},
}
response = client.post("/invoke", json=payload)
assert response.status_code == 200
body = response.json()
assert body["status"] == "error"
assert body["error"] is not None
assert body["error"]["code"] == "E-EXEC-001"
assert body["error"]["recoverable"] is False
assert body["error"]["trace_id"] == body["trace_id"]
if __name__ == "__main__":
# Convenience: run as script and print results
import json as _json
_client = TestClient(app)
tests = [
("Root endpoint", test_root),
("Health check", test_health),
("Echo task (success)", test_echo_task),
("Unsupported task (error)", test_unsupported_task),
]
passed = 0
for name, test_func in tests:
try:
test_func()
print(f"✅ {name} PASSED")
passed += 1
except AssertionError as exc:
print(f"❌ {name} FAILED: {exc}")
except Exception as exc:
print(f"❌ {name} ERROR: {exc}")
print(f"\nResults: {passed}/{len(tests)} tests passed")