-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_syncchat.py
More file actions
188 lines (140 loc) Β· 6.68 KB
/
Copy pathtest_syncchat.py
File metadata and controls
188 lines (140 loc) Β· 6.68 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
#!/usr/bin/env python3
import sys
import time
import threading
from typing import Dict, Any, List
from pathlib import Path
# Add pixide to path
sys.path.insert(0, str(Path(__file__).parent / "pixide"))
from chat import SyncChat
class TestSyncChat:
def __init__(self):
self.received_messages: List[Dict[str, Any]] = []
self.message_lock = threading.Lock()
def message_handler(self, message: Dict[str, Any]):
"""Handle incoming messages during testing"""
with self.message_lock:
self.received_messages.append(message)
print(f"π¨ Received: {message.get('type')} - {message.get('content', message.get('message', ''))}")
def wait_for_message(self, timeout: float = 3.0, message_type: str = None) -> Dict[str, Any]:
"""Wait for a specific type of message or any message"""
start_time = time.time()
while time.time() - start_time < timeout:
with self.message_lock:
if self.received_messages:
if message_type is None:
return self.received_messages.pop(0)
for i, msg in enumerate(self.received_messages):
if msg.get('type') == message_type:
return self.received_messages.pop(i)
time.sleep(0.1)
raise TimeoutError(f"No {'message' if message_type is None else message_type} received within {timeout}s")
def clear_messages(self):
"""Clear received messages buffer"""
with self.message_lock:
self.received_messages.clear()
def test_connection(self):
"""Test basic connection to localhost:8080"""
print("\nπ Testing connection...")
chat = SyncChat("ws://localhost:8080")
chat.set_message_handler(self.message_handler)
# Test connection
connected = chat.connect("test-room")
assert connected, "Failed to connect to server"
print("β
Connected successfully")
# Wait for join confirmation
join_msg = self.wait_for_message(timeout=3.0, message_type='joined')
assert join_msg['roomId'] == 'test-room', f"Expected room 'test-room', got {join_msg['roomId']}"
print(f"β
Joined room: {join_msg['roomId']}")
chat.disconnect()
time.sleep(0.5) # Allow clean disconnect
print("β
Disconnected cleanly")
return chat
def test_messaging(self):
"""Test sending and receiving messages"""
print("\n㪠Testing messaging...")
chat = SyncChat("ws://localhost:8080")
chat.set_message_handler(self.message_handler)
# Connect and join room
connected = chat.connect("messaging-test")
assert connected, "Failed to connect"
# Wait for join confirmation and clear messages
self.wait_for_message(message_type='joined')
self.clear_messages()
# Send a test message
test_message = "Hello from SyncChat test!"
chat.send_message(test_message)
# Wait for the message to be echoed back
echo_msg = self.wait_for_message(timeout=3.0, message_type='chat_message')
assert echo_msg['content'] == test_message, f"Message content mismatch: {echo_msg['content']}"
assert echo_msg['userId'] == chat.get_user_id(), "User ID mismatch in echoed message"
print(f"β
Message sent and received: '{test_message}'")
chat.disconnect()
time.sleep(0.5)
def test_room_switching(self):
"""Test switching between rooms"""
print("\nπ Testing room switching...")
chat = SyncChat("ws://localhost:8080")
chat.set_message_handler(self.message_handler)
# Connect to initial room
connected = chat.connect("room1")
assert connected, "Failed to connect"
# Wait for join confirmation
join_msg = self.wait_for_message(message_type='joined')
assert join_msg['roomId'] == 'room1', "Failed to join room1"
print(f"β
Joined initial room: {join_msg['roomId']}")
# Switch to different room
self.clear_messages()
chat.switch_room("room2")
# Wait for new join confirmation
join_msg2 = self.wait_for_message(timeout=3.0, message_type='joined')
assert join_msg2['roomId'] == 'room2', f"Expected room2, got {join_msg2['roomId']}"
print(f"β
Switched to room: {join_msg2['roomId']}")
# Verify current room is updated
assert chat.get_current_room() == 'room2', "Current room not updated after switch"
chat.disconnect()
time.sleep(0.5)
def test_user_id_persistence(self):
"""Test that user ID is consistent"""
print("\nπ€ Testing user ID...")
chat1 = SyncChat("ws://localhost:8080")
user_id1 = chat1.get_user_id()
print(f"β
User ID 1: {user_id1}")
chat2 = SyncChat("ws://localhost:8080")
user_id2 = chat2.get_user_id()
print(f"β
User ID 2: {user_id2}")
# User IDs should be the same (loaded from file)
assert user_id1 == user_id2, f"User IDs should match: {user_id1} != {user_id2}"
print("β
User ID persistence confirmed")
def test_error_handling(self):
"""Test error handling for invalid operations"""
print("\nβ Testing error handling...")
chat = SyncChat("ws://localhost:8080")
chat.set_message_handler(self.message_handler)
# Try sending message without connecting
chat.send_message("This should not work")
print("β
Handled disconnected send gracefully")
# Try switching room without connecting
chat.switch_room("invalid")
print("β
Handled disconnected room switch gracefully")
def run_all_tests(self):
"""Run all tests"""
print("π§ͺ Starting SyncChat tests...")
print(f"π― Target server: ws://localhost:8080")
try:
self.test_connection()
self.test_messaging()
self.test_room_switching()
self.test_user_id_persistence()
self.test_error_handling()
print("\nπ All SyncChat tests passed!")
return True
except Exception as e:
print(f"\nπ₯ Test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
tester = TestSyncChat()
success = tester.run_all_tests()
sys.exit(0 if success else 1)