-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
268 lines (229 loc) · 7.96 KB
/
test_api.py
File metadata and controls
268 lines (229 loc) · 7.96 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
#!/usr/bin/env python
"""
Quick demo script to test the API endpoints
Run: python test_api.py
"""
import requests
import json
from typing import Dict, Any
BASE_URL = "http://localhost:8000"
# Colors for terminal output
GREEN = '\033[92m'
BLUE = '\033[94m'
YELLOW = '\033[93m'
RED = '\033[91m'
RESET = '\033[0m'
def print_header(title: str):
"""Print a formatted header"""
print(f"\n{BLUE}{'='*60}")
print(f" {title}")
print(f"{'='*60}{RESET}\n")
def print_success(msg: str):
"""Print success message"""
print(f"{GREEN}✓ {msg}{RESET}")
def print_error(msg: str):
"""Print error message"""
print(f"{RED}✗ {msg}{RESET}")
def print_response(response: Dict[Any, Any]):
"""Pretty print JSON response"""
print(json.dumps(response, indent=2))
def test_health_check():
"""Test health endpoint"""
print_header("1. Health Check")
try:
response = requests.get(f"{BASE_URL}/health")
print_success("Health check passed")
print_response(response.json())
return True
except Exception as e:
print_error(f"Health check failed: {e}")
return False
def test_create_task():
"""Test task creation"""
print_header("2. Create a Task")
try:
payload = {
"title": "Review API documentation",
"description": "Check the new endpoints for Slack integration"
}
response = requests.post(f"{BASE_URL}/tasks/", json=payload)
if response.status_code == 200:
print_success("Task created")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
print_response(response.json())
return False
except Exception as e:
print_error(f"Task creation failed: {e}")
return False
def test_list_tasks():
"""Test listing tasks"""
print_header("3. List All Tasks")
try:
response = requests.get(f"{BASE_URL}/tasks/")
if response.status_code == 200:
print_success("Tasks retrieved")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
return False
except Exception as e:
print_error(f"Listing tasks failed: {e}")
return False
def test_summarize():
"""Test text summarization"""
print_header("4. Summarize Text")
try:
payload = {
"text": """
In today's meeting we discussed the Q1 roadmap.
John mentioned we need to prioritize the API refactoring.
Sarah will handle the frontend redesign.
The deployment is planned for end of month.
We also need to schedule a code review session.
""",
"store_tasks": True
}
response = requests.post(f"{BASE_URL}/summarize", json=payload)
if response.status_code == 200:
print_success("Text summarized")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
print_response(response.json())
return False
except Exception as e:
print_error(f"Summarization failed: {e}")
return False
def test_ask_question():
"""Test asking a question"""
print_header("5. Ask a Question (with context)")
try:
payload = {
"user_id": "U123456789",
"question": "What's on my plate today? What should I prioritize?",
"thread_ts": "1234567.890123"
}
response = requests.post(f"{BASE_URL}/chat/ask", json=payload)
if response.status_code == 200:
print_success("Question answered")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
print_response(response.json())
return False
except Exception as e:
print_error(f"Question answering failed: {e}")
return False
def test_pending_summary():
"""Test getting pending summary"""
print_header("6. Get Pending Summary for User")
try:
response = requests.get(
f"{BASE_URL}/chat/pending-summary/U123456789"
)
if response.status_code == 200:
print_success("Pending summary retrieved")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
print_response(response.json())
return False
except Exception as e:
print_error(f"Pending summary failed: {e}")
return False
def test_mark_thread():
"""Test marking a thread"""
print_header("7. Mark Slack Thread for Analysis")
try:
payload = {
"thread_ts": "1234567.890123",
"is_marked": True
}
response = requests.post(
f"{BASE_URL}/memory/slack-threads/mark",
json=payload
)
if response.status_code == 200:
print_success("Thread marked")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
print_response(response.json())
return False
except Exception as e:
print_error(f"Marking thread failed: {e}")
return False
def test_get_marked_threads():
"""Test getting marked threads"""
print_header("8. Get All Marked Threads")
try:
response = requests.get(
f"{BASE_URL}/memory/slack-threads/marked"
)
if response.status_code == 200:
print_success("Marked threads retrieved")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
return False
except Exception as e:
print_error(f"Getting marked threads failed: {e}")
return False
def test_get_user_threads():
"""Test getting user threads"""
print_header("9. Get User Slack Threads")
try:
response = requests.get(
f"{BASE_URL}/memory/slack-threads/user/U123456789"
)
if response.status_code == 200:
print_success("User threads retrieved")
print_response(response.json())
return True
else:
print_error(f"Failed with status {response.status_code}")
return False
except Exception as e:
print_error(f"Getting user threads failed: {e}")
return False
def main():
"""Run all tests"""
print(f"\n{YELLOW}{'='*60}")
print(" AI Assistant API - Test Suite")
print(f"{'='*60}{RESET}")
print(f"\nTesting API at: {BASE_URL}")
print("Make sure the server is running: uvicorn api.main:app --reload\n")
results = []
# Run tests
results.append(("Health Check", test_health_check()))
results.append(("Create Task", test_create_task()))
results.append(("List Tasks", test_list_tasks()))
results.append(("Summarize Text", test_summarize()))
results.append(("Ask Question", test_ask_question()))
results.append(("Pending Summary", test_pending_summary()))
results.append(("Mark Thread", test_mark_thread()))
results.append(("Get Marked Threads", test_get_marked_threads()))
results.append(("Get User Threads", test_get_user_threads()))
# Print summary
print_header("Test Summary")
passed = sum(1 for _, result in results if result)
total = len(results)
for test_name, result in results:
status = f"{GREEN}PASSED{RESET}" if result else f"{RED}FAILED{RESET}"
print(f" {test_name}: {status}")
print(f"\n{BLUE}Total: {passed}/{total} tests passed{RESET}\n")
if passed == total:
print_success("All tests passed! ✨")
else:
print_error(f"{total - passed} test(s) failed")
if __name__ == "__main__":
main()