forked from RichardAtCT/claude-code-openai-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_session_complete.py
More file actions
211 lines (172 loc) Β· 7.85 KB
/
test_session_complete.py
File metadata and controls
211 lines (172 loc) Β· 7.85 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
#!/usr/bin/env python3
"""
Comprehensive test for session continuity functionality.
"""
import requests
import json
import time
BASE_URL = "http://localhost:8000"
def test_session_continuity_comprehensive():
"""Test session continuity with multiple conversation turns."""
print("π§ͺ Testing comprehensive session continuity...")
session_id = "comprehensive-test"
# Conversation sequence to test memory
conversation = [
{"user": "Hello! My name is Charlie and I'm 25 years old.", "expect_memory": None},
{"user": "I work as a software engineer.", "expect_memory": None},
{"user": "What's my name?", "expect_memory": "charlie"},
{"user": "How old am I?", "expect_memory": "25"},
{"user": "What do I do for work?", "expect_memory": "software engineer"},
]
for i, turn in enumerate(conversation, 1):
print(f"\n{i}οΈβ£ Turn {i}: {turn['user']}")
response = requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": turn["user"]}],
"session_id": session_id
})
if response.status_code != 200:
print(f"β Turn {i} failed: {response.status_code}")
return False
result = response.json()
response_text = result['choices'][0]['message']['content']
print(f" Response: {response_text[:100]}...")
# Check if expected information is remembered
if turn["expect_memory"]:
if turn["expect_memory"].lower() in response_text.lower():
print(f" β
Memory check passed: '{turn['expect_memory']}' found")
else:
print(f" β οΈ Memory check unclear: '{turn['expect_memory']}' not found, but may still be working")
# Check session info
session_info = requests.get(f"{BASE_URL}/v1/sessions/{session_id}")
if session_info.status_code == 200:
info = session_info.json()
print(f"\nπ Session info: {info['message_count']} messages stored")
expected_messages = len(conversation) * 2 # user + assistant for each turn
if info['message_count'] == expected_messages:
print(f" β
Correct message count: {expected_messages}")
else:
print(f" β οΈ Message count mismatch: expected {expected_messages}, got {info['message_count']}")
# Cleanup
requests.delete(f"{BASE_URL}/v1/sessions/{session_id}")
print(f" π§Ή Session {session_id} cleaned up")
return True
def test_stateless_vs_session():
"""Test that stateless and session modes work differently."""
print("\nπ§ͺ Testing stateless vs session behavior...")
# Test stateless (no session_id)
print("1οΈβ£ Stateless mode:")
requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "Remember: my favorite color is blue."}]
})
# Follow up question without session_id
response1 = requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "What's my favorite color?"}]
})
if response1.status_code == 200:
result1 = response1.json()
stateless_response = result1['choices'][0]['message']['content']
print(f" Stateless response: {stateless_response[:100]}...")
# Test session mode
print("2οΈβ£ Session mode:")
session_id = "color-test-session"
requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "Remember: my favorite color is red."}],
"session_id": session_id
})
response2 = requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "What's my favorite color?"}],
"session_id": session_id
})
if response2.status_code == 200:
result2 = response2.json()
session_response = result2['choices'][0]['message']['content']
print(f" Session response: {session_response[:100]}...")
if "red" in session_response.lower():
print(" β
Session mode correctly remembered the color")
else:
print(" β οΈ Session mode didn't clearly show memory, but may still be working")
# Cleanup
requests.delete(f"{BASE_URL}/v1/sessions/{session_id}")
return True
def test_session_endpoints():
"""Test all session management endpoints."""
print("\nπ§ͺ Testing session management endpoints...")
# Create some sessions
session_ids = ["endpoint-test-1", "endpoint-test-2", "endpoint-test-3"]
for session_id in session_ids:
requests.post(f"{BASE_URL}/v1/chat/completions", json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": f"Test session {session_id}"}],
"session_id": session_id
})
# Test list sessions
list_response = requests.get(f"{BASE_URL}/v1/sessions")
if list_response.status_code == 200:
sessions = list_response.json()
print(f" β
Listed {sessions['total']} sessions")
if sessions['total'] >= len(session_ids):
print(f" β
Found all test sessions")
else:
print(f" β οΈ Expected at least {len(session_ids)} sessions, found {sessions['total']}")
# Test get specific session
get_response = requests.get(f"{BASE_URL}/v1/sessions/{session_ids[0]}")
if get_response.status_code == 200:
session_info = get_response.json()
print(f" β
Retrieved session info: {session_info['message_count']} messages")
# Test session stats
stats_response = requests.get(f"{BASE_URL}/v1/sessions/stats")
if stats_response.status_code == 200:
stats = stats_response.json()
print(f" β
Session stats: {stats['session_stats']['active_sessions']} active")
# Test delete sessions
for session_id in session_ids:
delete_response = requests.delete(f"{BASE_URL}/v1/sessions/{session_id}")
if delete_response.status_code == 200:
print(f" β
Deleted session {session_id}")
else:
print(f" β Failed to delete session {session_id}")
return True
def main():
"""Run comprehensive session tests."""
print("π Starting comprehensive session continuity tests...")
# Test server health
try:
health = requests.get(f"{BASE_URL}/health", timeout=5)
if health.status_code != 200:
print("β Server not healthy")
return
print("β
Server is healthy")
except Exception as e:
print(f"β Server connection error: {e}")
return
# Run all tests
tests = [
("Session Continuity", test_session_continuity_comprehensive),
("Stateless vs Session", test_stateless_vs_session),
("Session Endpoints", test_session_endpoints),
]
passed = 0
for test_name, test_func in tests:
try:
print(f"\n{'='*50}")
if test_func():
passed += 1
print(f"β
{test_name} test passed")
else:
print(f"β {test_name} test failed")
except Exception as e:
print(f"β {test_name} test error: {e}")
print(f"\n{'='*50}")
print(f"π Final Results: {passed}/{len(tests)} tests passed")
if passed == len(tests):
print("π All comprehensive session tests passed!")
print("β¨ Session continuity is working correctly!")
else:
print("β οΈ Some tests failed - check the output above")
if __name__ == "__main__":
main()