-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_test.py
More file actions
280 lines (234 loc) · 8.91 KB
/
agent_test.py
File metadata and controls
280 lines (234 loc) · 8.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
import requests
import json
import os
import sys
from dotenv import load_dotenv
import uuid
# Load environment variables from frontend/.env
load_dotenv('/app/frontend/.env')
# Get the backend URL from environment variables
BACKEND_URL = os.environ.get('REACT_APP_BACKEND_URL')
if not BACKEND_URL:
print("Error: REACT_APP_BACKEND_URL not found in environment variables")
sys.exit(1)
# Ensure the URL ends with /api
API_URL = f"{BACKEND_URL}/api"
print(f"Using API URL: {API_URL}")
# Test login to get auth token
def test_login():
"""Login with test endpoint to get auth token"""
print("\n=== Testing Login ===")
# Try the test login endpoint
test_login_url = f"{API_URL}/auth/test-login"
print(f"POST {test_login_url}")
try:
response = requests.post(test_login_url)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
token = data.get("access_token")
print(f"Login successful. Token: {token[:10]}...")
return token
else:
print(f"Login failed: {response.text}")
return None
except Exception as e:
print(f"Error during login: {e}")
return None
# Test GET /api/agents
def test_get_agents(token=None):
"""Test the GET /api/agents endpoint"""
print("\n=== Testing GET /api/agents ===")
url = f"{API_URL}/agents"
print(f"GET {url}")
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
try:
response = requests.get(url, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Found {len(data)} agents")
if data:
print("\nSample agent structure:")
sample_agent = data[0]
for key, value in sample_agent.items():
if isinstance(value, dict):
print(f"- {key}: {type(value)}")
else:
print(f"- {key}: {value}")
else:
print("No agents found")
return data
else:
print(f"Request failed: {response.text}")
return None
except Exception as e:
print(f"Error during request: {e}")
return None
# Test GET /api/saved-agents
def test_get_saved_agents(token=None):
"""Test the GET /api/saved-agents endpoint"""
print("\n=== Testing GET /api/saved-agents ===")
url = f"{API_URL}/saved-agents"
print(f"GET {url}")
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
try:
response = requests.get(url, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Found {len(data)} saved agents")
if data:
print("\nSample saved agent structure:")
sample_agent = data[0]
for key, value in sample_agent.items():
if isinstance(value, dict):
print(f"- {key}: {type(value)}")
else:
print(f"- {key}: {value}")
else:
print("No saved agents found")
return data
else:
print(f"Request failed: {response.text}")
return None
except Exception as e:
print(f"Error during request: {e}")
return None
# Test POST /api/agents
def test_create_agent(token=None):
"""Test the POST /api/agents endpoint"""
print("\n=== Testing POST /api/agents ===")
url = f"{API_URL}/agents"
print(f"POST {url}")
# Create test agent data
agent_data = {
"name": f"Test Agent {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"goal": "Test the agent creation endpoint",
"expertise": "API Testing",
"background": "Created for testing purposes",
"memory_summary": "This agent was created to test the API",
"avatar_prompt": "A robot scientist in a lab coat"
}
print(f"Request data: {json.dumps(agent_data, indent=2)}")
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
headers["Content-Type"] = "application/json"
try:
response = requests.post(url, json=agent_data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Agent created successfully with ID: {data.get('id')}")
print("\nCreated agent structure:")
for key, value in data.items():
if isinstance(value, dict):
print(f"- {key}: {type(value)}")
else:
print(f"- {key}: {value}")
return data
else:
print(f"Request failed: {response.text}")
return None
except Exception as e:
print(f"Error during request: {e}")
return None
# Test GET /api/archetypes
def test_get_archetypes(token=None):
"""Test the GET /api/archetypes endpoint"""
print("\n=== Testing GET /api/archetypes ===")
url = f"{API_URL}/archetypes"
print(f"GET {url}")
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
try:
response = requests.get(url, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Found {len(data)} archetypes")
print("\nAvailable archetypes:")
for archetype, details in data.items():
print(f"- {archetype}: {details.get('name')} - {details.get('description')}")
return data
else:
print(f"Request failed: {response.text}")
return None
except Exception as e:
print(f"Error during request: {e}")
return None
# Main function
# Test POST /api/saved-agents
def test_save_agent(token=None):
"""Test the POST /api/saved-agents endpoint"""
print("\n=== Testing POST /api/saved-agents ===")
url = f"{API_URL}/saved-agents"
print(f"POST {url}")
# Create test saved agent data
agent_data = {
"name": f"Saved Test Agent {uuid.uuid4().hex[:8]}",
"archetype": "researcher",
"goal": "Test the saved agent creation endpoint",
"expertise": "API Testing",
"background": "Created for testing saved agents",
"avatar_prompt": "A researcher with a clipboard"
}
print(f"Request data: {json.dumps(agent_data, indent=2)}")
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
headers["Content-Type"] = "application/json"
try:
response = requests.post(url, json=agent_data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Saved agent created successfully with ID: {data.get('id')}")
print("\nCreated saved agent structure:")
for key, value in data.items():
if isinstance(value, dict):
print(f"- {key}: {type(value)}")
else:
print(f"- {key}: {value}")
return data
else:
print(f"Request failed: {response.text}")
return None
except Exception as e:
print(f"Error during request: {e}")
return None
def main():
print("=== Agent Library API Testing ===")
# Login to get auth token
token = test_login()
# Test GET /api/agents
agents = test_get_agents(token)
# Test GET /api/saved-agents
saved_agents = test_get_saved_agents(token)
# Test GET /api/archetypes
archetypes = test_get_archetypes(token)
# Test POST /api/agents
created_agent = test_create_agent(token)
# Test POST /api/saved-agents
saved_agent = test_save_agent(token)
# Test GET /api/saved-agents again to see if the saved agent appears
if saved_agent:
print("\n=== Testing GET /api/saved-agents (After Saving) ===")
updated_saved_agents = test_get_saved_agents(token)
# Print summary
print("\n=== Test Summary ===")
print(f"GET /api/agents: {'Success' if agents is not None else 'Failed'}")
print(f"GET /api/saved-agents: {'Success' if saved_agents is not None else 'Failed'}")
print(f"GET /api/archetypes: {'Success' if archetypes is not None else 'Failed'}")
print(f"POST /api/agents: {'Success' if created_agent is not None else 'Failed'}")
print(f"POST /api/saved-agents: {'Success' if saved_agent is not None else 'Failed'}")
if __name__ == "__main__":
main()