Skip to content

Commit 67bf4ab

Browse files
committed
feat: added comprehensive and quick tests for a2a, improved a2a testing in gradio
1 parent 99f1dc7 commit 67bf4ab

File tree

2 files changed

+323
-0
lines changed

2 files changed

+323
-0
lines changed

agentic_rag/test_a2a_final.py

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Final A2A Test Script
4+
5+
This script performs a final test of the A2A protocol to verify all fixes.
6+
"""
7+
8+
import requests
9+
import json
10+
import time
11+
12+
def test_a2a_final():
13+
"""Final A2A test"""
14+
print("🔍 Final A2A Test")
15+
print("=" * 40)
16+
17+
# Test health check
18+
print("1. Testing Health Check...")
19+
try:
20+
response = requests.get("http://localhost:8000/a2a/health", timeout=5)
21+
if response.status_code == 200:
22+
health_data = response.json()
23+
if "result" in health_data and health_data["result"].get("status") == "healthy":
24+
print("✅ Health Check: PASSED")
25+
else:
26+
print(f"❌ Health Check: FAILED - {health_data}")
27+
return False
28+
else:
29+
print(f"❌ Health Check: FAILED - HTTP {response.status_code}")
30+
return False
31+
except Exception as e:
32+
print(f"❌ Health Check: ERROR - {str(e)}")
33+
return False
34+
35+
# Test agent discovery
36+
print("\n2. Testing Agent Discovery...")
37+
try:
38+
discover_payload = {
39+
"jsonrpc": "2.0",
40+
"method": "agent.discover",
41+
"params": {"capability": "document.query"},
42+
"id": "test-discover"
43+
}
44+
45+
response = requests.post(
46+
"http://localhost:8000/a2a",
47+
json=discover_payload,
48+
headers={"Content-Type": "application/json"},
49+
timeout=10
50+
)
51+
52+
if response.status_code == 200:
53+
discover_data = response.json()
54+
print(f"Discovery response: {json.dumps(discover_data, indent=2)}")
55+
56+
if "error" in discover_data:
57+
print(f"❌ Agent Discovery: FAILED - {discover_data['error']}")
58+
return False
59+
else:
60+
result = discover_data.get("result", {})
61+
agents = result.get("agents", [])
62+
if agents:
63+
print(f"✅ Agent Discovery: PASSED - Found {len(agents)} agents")
64+
for i, agent in enumerate(agents, 1):
65+
agent_id = agent.get('agent_id', 'unknown')
66+
agent_name = agent.get('name', 'unknown')
67+
print(f" Agent {i}: {agent_id} - {agent_name}")
68+
else:
69+
print("❌ Agent Discovery: FAILED - No agents found")
70+
print(f" Result: {result}")
71+
return False
72+
else:
73+
print(f"❌ Agent Discovery: FAILED - HTTP {response.status_code}")
74+
return False
75+
except Exception as e:
76+
print(f"❌ Agent Discovery: ERROR - {str(e)}")
77+
return False
78+
79+
# Test document query
80+
print("\n3. Testing Document Query...")
81+
try:
82+
query_payload = {
83+
"jsonrpc": "2.0",
84+
"method": "document.query",
85+
"params": {
86+
"query": "What is artificial intelligence?",
87+
"collection": "General",
88+
"use_cot": False,
89+
"max_results": 3
90+
},
91+
"id": "test-query"
92+
}
93+
94+
response = requests.post(
95+
"http://localhost:8000/a2a",
96+
json=query_payload,
97+
headers={"Content-Type": "application/json"},
98+
timeout=30
99+
)
100+
101+
if response.status_code == 200:
102+
query_data = response.json()
103+
if "error" in query_data:
104+
print(f"❌ Document Query: FAILED - {query_data['error']}")
105+
return False
106+
else:
107+
result = query_data.get("result", {})
108+
answer = result.get("answer", "")
109+
if answer and answer != "No answer provided" and answer.strip():
110+
print(f"✅ Document Query: PASSED")
111+
print(f" Answer: {answer[:100]}...")
112+
else:
113+
print(f"❌ Document Query: FAILED - No valid answer")
114+
print(f" Result: {result}")
115+
return False
116+
else:
117+
print(f"❌ Document Query: FAILED - HTTP {response.status_code}")
118+
return False
119+
except Exception as e:
120+
print(f"❌ Document Query: ERROR - {str(e)}")
121+
return False
122+
123+
# Test task creation
124+
print("\n4. Testing Task Creation...")
125+
try:
126+
task_payload = {
127+
"jsonrpc": "2.0",
128+
"method": "task.create",
129+
"params": {
130+
"task_type": "document_processing",
131+
"params": {
132+
"command": "test_command",
133+
"description": "Test task for A2A testing",
134+
"priority": "high"
135+
}
136+
},
137+
"id": "test-task"
138+
}
139+
140+
response = requests.post(
141+
"http://localhost:8000/a2a",
142+
json=task_payload,
143+
headers={"Content-Type": "application/json"},
144+
timeout=10
145+
)
146+
147+
if response.status_code == 200:
148+
task_data = response.json()
149+
if "error" in task_data:
150+
print(f"❌ Task Creation: FAILED - {task_data['error']}")
151+
return False
152+
else:
153+
result = task_data.get("result", {})
154+
task_id = result.get("task_id")
155+
if task_id:
156+
print(f"✅ Task Creation: PASSED - Created task {task_id}")
157+
158+
# Test task status
159+
time.sleep(1)
160+
status_payload = {
161+
"jsonrpc": "2.0",
162+
"method": "task.status",
163+
"params": {"task_id": task_id},
164+
"id": "test-status"
165+
}
166+
167+
status_response = requests.post(
168+
"http://localhost:8000/a2a",
169+
json=status_payload,
170+
headers={"Content-Type": "application/json"},
171+
timeout=10
172+
)
173+
174+
if status_response.status_code == 200:
175+
status_data = status_response.json()
176+
if "error" not in status_data:
177+
status_result = status_data.get("result", {})
178+
task_status = status_result.get("status", "unknown")
179+
print(f"✅ Task Status: PASSED - Status: {task_status}")
180+
else:
181+
print(f"❌ Task Status: FAILED - {status_data['error']}")
182+
return False
183+
else:
184+
print(f"❌ Task Status: FAILED - HTTP {status_response.status_code}")
185+
return False
186+
else:
187+
print(f"❌ Task Creation: FAILED - No task ID returned")
188+
return False
189+
else:
190+
print(f"❌ Task Creation: FAILED - HTTP {response.status_code}")
191+
return False
192+
except Exception as e:
193+
print(f"❌ Task Operations: ERROR - {str(e)}")
194+
return False
195+
196+
return True
197+
198+
def main():
199+
"""Main function"""
200+
print("🤖 Final A2A Test")
201+
print("Make sure A2A server is running: python main.py")
202+
print()
203+
204+
success = test_a2a_final()
205+
206+
if success:
207+
print("\n🎉 All A2A tests passed!")
208+
print("✅ OK")
209+
else:
210+
print("\n❌ Some A2A tests failed.")
211+
print("❌ NOT OK")
212+
213+
if __name__ == "__main__":
214+
main()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test Agent Registration
4+
5+
This script tests the agent registration functionality directly.
6+
"""
7+
8+
import sys
9+
import os
10+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
11+
12+
from a2a_handler import A2AHandler
13+
from agent_registry import AgentRegistry
14+
from agent_card import get_agent_card
15+
from a2a_models import AgentCard, AgentCapability, AgentEndpoints
16+
17+
def test_agent_registration():
18+
"""Test agent registration directly"""
19+
print("🔍 Testing Agent Registration...")
20+
21+
# Create a mock RAG agent
22+
class MockRAGAgent:
23+
def process_query(self, query):
24+
return {"answer": "Test answer", "context": []}
25+
26+
# Create A2A handler
27+
handler = A2AHandler(MockRAGAgent())
28+
29+
# Check registry state
30+
print(f"Registry has {len(handler.agent_registry.registered_agents)} agents")
31+
print(f"Available capabilities: {list(handler.agent_registry.capability_index.keys())}")
32+
33+
# Test discovery
34+
agents = handler.agent_registry.discover_agents("document.query")
35+
print(f"Found {len(agents)} agents with document.query capability")
36+
37+
for i, agent in enumerate(agents, 1):
38+
print(f" Agent {i}: {agent.agent_id} - {agent.name}")
39+
40+
return len(agents) > 0
41+
42+
def test_agent_card_conversion():
43+
"""Test agent card conversion"""
44+
print("\n🔍 Testing Agent Card Conversion...")
45+
46+
try:
47+
agent_card_data = get_agent_card()
48+
print(f"Agent card data type: {type(agent_card_data)}")
49+
print(f"Agent ID: {agent_card_data.get('agent_id')}")
50+
print(f"Capabilities count: {len(agent_card_data.get('capabilities', []))}")
51+
52+
# Test conversion to AgentCard object
53+
capabilities = []
54+
for cap_data in agent_card_data.get("capabilities", []):
55+
capability = AgentCapability(
56+
name=cap_data["name"],
57+
description=cap_data["description"],
58+
input_schema=cap_data.get("input_schema", {}),
59+
output_schema=cap_data.get("output_schema", {})
60+
)
61+
capabilities.append(capability)
62+
63+
endpoints_data = agent_card_data.get("endpoints", {})
64+
endpoints = AgentEndpoints(
65+
base_url=endpoints_data.get("base_url", "http://localhost:8000"),
66+
authentication=endpoints_data.get("authentication", {})
67+
)
68+
69+
agent_card = AgentCard(
70+
agent_id=agent_card_data["agent_id"],
71+
name=agent_card_data["name"],
72+
version=agent_card_data["version"],
73+
description=agent_card_data["description"],
74+
capabilities=capabilities,
75+
endpoints=endpoints,
76+
metadata=agent_card_data.get("metadata", {})
77+
)
78+
79+
print(f"Created AgentCard: {agent_card.agent_id}")
80+
print(f"Capabilities: {[cap.name for cap in agent_card.capabilities]}")
81+
82+
return True
83+
84+
except Exception as e:
85+
print(f"❌ Agent Card Conversion: ERROR - {str(e)}")
86+
import traceback
87+
print(f"Traceback: {traceback.format_exc()}")
88+
return False
89+
90+
def main():
91+
"""Main test function"""
92+
print("🤖 Agent Registration Test")
93+
print("=" * 40)
94+
95+
# Test agent card conversion
96+
card_success = test_agent_card_conversion()
97+
98+
# Test agent registration
99+
reg_success = test_agent_registration()
100+
101+
if card_success and reg_success:
102+
print("\n🎉 Agent registration is working!")
103+
print("✅ OK")
104+
else:
105+
print("\n❌ Agent registration has issues.")
106+
print("❌ NOT OK")
107+
108+
if __name__ == "__main__":
109+
main()

0 commit comments

Comments
 (0)