-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpertise_update_test.py
More file actions
322 lines (270 loc) · 11.2 KB
/
expertise_update_test.py
File metadata and controls
322 lines (270 loc) · 11.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/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}")
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")
user_data = data.get("user", {})
user_id = user_data.get("id")
print(f"Login successful. Token: {token[:10]}...")
print(f"User ID: {user_id}")
return token
else:
print(f"Login failed: {response.text}")
return None
except Exception as e:
print(f"Error during login: {e}")
return None
def create_test_agent(auth_token):
"""Create a test agent for update testing"""
print("\n=== Creating Test Agent ===")
# Create agent data
agent_data = {
"name": f"Test Agent {uuid.uuid4().hex[:8]}",
"archetype": "scientist",
"personality": {
"extroversion": 5,
"optimism": 7,
"curiosity": 9,
"cooperativeness": 6,
"energy": 8
},
"goal": "Test the expertise field update",
"expertise": "Initial Expertise Value",
"background": "Created for testing expertise update",
"memory_summary": "",
"avatar_prompt": "A robot testing software"
}
url = f"{API_URL}/agents"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"POST {url}")
print(f"Headers: {headers}")
print(f"Request data: {json.dumps(agent_data, indent=2)}")
try:
response = requests.post(url, json=agent_data, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
# Store the created agent ID for later tests
agent_id = response_data.get("id")
if agent_id:
print(f"✅ Successfully created agent with ID: {agent_id}")
return agent_id, response_data
else:
print("❌ No agent ID in response")
return None, None
else:
print(f"❌ Request failed: {response.text}")
return None, None
except Exception as e:
print(f"❌ Error during test: {e}")
return None, None
def test_update_expertise(auth_token, agent_id):
"""Test updating the expertise field"""
print("\n=== Testing Expertise Field Update ===")
# Prepare update data with only expertise field
update_data = {
"expertise": f"Updated Expertise {uuid.uuid4().hex[:8]}"
}
url = f"{API_URL}/agents/{agent_id}"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"PUT {url}")
print(f"Headers: {headers}")
print(f"Request data: {json.dumps(update_data, indent=2)}")
try:
response = requests.put(url, json=update_data, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
# Verify the expertise field was updated correctly
if response_data.get("expertise") == update_data.get("expertise"):
print(f"✅ Expertise field updated successfully")
print(f"Expected: {update_data.get('expertise')}")
print(f"Actual: {response_data.get('expertise')}")
return True, response_data
else:
print(f"❌ Expertise field not updated correctly")
print(f"Expected: {update_data.get('expertise')}")
print(f"Actual: {response_data.get('expertise')}")
return False, response_data
else:
print(f"❌ Request failed: {response.text}")
return False, None
except Exception as e:
print(f"❌ Error during test: {e}")
return False, None
def test_update_all_fields(auth_token, agent_id):
"""Test updating all fields including expertise"""
print("\n=== Testing All Fields Update ===")
# Prepare update data with all fields
update_data = {
"name": f"Updated Agent {uuid.uuid4().hex[:8]}",
"archetype": "leader",
"personality": {
"extroversion": 8,
"optimism": 9,
"curiosity": 7,
"cooperativeness": 8,
"energy": 9
},
"goal": "To verify all fields update correctly",
"expertise": f"Comprehensive Expertise {uuid.uuid4().hex[:8]}",
"background": "Updated background information",
"memory_summary": "Updated memory information"
}
url = f"{API_URL}/agents/{agent_id}"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"PUT {url}")
print(f"Headers: {headers}")
print(f"Request data: {json.dumps(update_data, indent=2)}")
try:
response = requests.put(url, json=update_data, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
# Verify all fields were updated correctly
all_fields_updated = True
for key, value in update_data.items():
if key == "personality":
for p_key, p_value in value.items():
if response_data["personality"][p_key] != p_value:
print(f"❌ Personality mismatch for {p_key}: expected {p_value}, got {response_data['personality'][p_key]}")
all_fields_updated = False
elif response_data.get(key) != value:
print(f"❌ Data mismatch for {key}: expected {value}, got {response_data.get(key)}")
all_fields_updated = False
if all_fields_updated:
print("✅ All fields updated successfully")
return True, response_data
else:
print("❌ Some fields not updated correctly")
return False, response_data
else:
print(f"❌ Request failed: {response.text}")
return False, None
except Exception as e:
print(f"❌ Error during test: {e}")
return False, None
def test_invalid_agent_id(auth_token):
"""Test PUT and DELETE with invalid agent ID"""
print("\n=== Testing Invalid Agent ID Handling ===")
# Generate a random invalid ID
invalid_id = str(uuid.uuid4())
# Test PUT with invalid ID
print("\n--- Testing PUT with Invalid ID ---")
update_data = {
"name": "This update should fail"
}
url = f"{API_URL}/agents/{invalid_id}"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"PUT {url}")
print(f"Headers: {headers}")
print(f"Request data: {json.dumps(update_data, indent=2)}")
try:
response = requests.put(url, json=update_data, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 404:
print(f"✅ Correctly returned 404 for invalid agent ID")
put_success = True
else:
print(f"❌ Expected 404, got {response.status_code}: {response.text}")
put_success = False
except Exception as e:
print(f"❌ Error during test: {e}")
put_success = False
# Test DELETE with invalid ID
print("\n--- Testing DELETE with Invalid ID ---")
url = f"{API_URL}/agents/{invalid_id}"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"DELETE {url}")
print(f"Headers: {headers}")
try:
response = requests.delete(url, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 404:
print(f"✅ Correctly returned 404 for invalid agent ID")
delete_success = True
else:
print(f"❌ Expected 404, got {response.status_code}: {response.text}")
delete_success = False
except Exception as e:
print(f"❌ Error during test: {e}")
delete_success = False
return put_success and delete_success
def cleanup(auth_token, agent_id):
"""Delete the test agent"""
print("\n=== Cleaning Up ===")
url = f"{API_URL}/agents/{agent_id}"
headers = {"Authorization": f"Bearer {auth_token}"}
print(f"DELETE {url}")
print(f"Headers: {headers}")
try:
response = requests.delete(url, headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code == 200:
print(f"✅ Successfully deleted test agent")
return True
else:
print(f"❌ Failed to delete test agent: {response.text}")
return False
except Exception as e:
print(f"❌ Error during cleanup: {e}")
return False
def main():
"""Run the expertise update test"""
print("=== TESTING AGENT EXPERTISE UPDATE ===")
# Login to get auth token
auth_token = test_login()
if not auth_token:
print("❌ Login failed. Cannot proceed with tests.")
return
# Create a test agent
agent_id, agent_data = create_test_agent(auth_token)
if not agent_id:
print("❌ Failed to create test agent. Cannot proceed with tests.")
return
# Test updating only the expertise field
expertise_update_success, _ = test_update_expertise(auth_token, agent_id)
# Test updating all fields including expertise
all_fields_update_success, _ = test_update_all_fields(auth_token, agent_id)
# Test invalid agent ID handling
invalid_id_success = test_invalid_agent_id(auth_token)
# Clean up
cleanup(auth_token, agent_id)
# Print summary
print("\n=== TEST SUMMARY ===")
print(f"Expertise Field Update: {'✅ PASSED' if expertise_update_success else '❌ FAILED'}")
print(f"All Fields Update: {'✅ PASSED' if all_fields_update_success else '❌ FAILED'}")
print(f"Invalid Agent ID Handling: {'✅ PASSED' if invalid_id_success else '❌ FAILED'}")
if expertise_update_success and all_fields_update_success and invalid_id_success:
print("\n✅ ALL TESTS PASSED")
else:
print("\n❌ SOME TESTS FAILED")
if __name__ == "__main__":
main()