Skip to content

Commit e6561b1

Browse files
committed
add pytests
1 parent 190a39e commit e6561b1

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

tests/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest
2+
pytest-asyncio
3+
aiohttp

tests/tests.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import pytest, asyncio, aiohttp, json, pytest_asyncio
2+
from typing import Dict, Any
3+
4+
BASE_URL = "https://modal-labs-vishy-dev--seamless-chat-seamlessm4t-asgi-app.modal.run"
5+
6+
@pytest_asyncio.fixture
7+
async def session():
8+
async with aiohttp.ClientSession() as session:
9+
yield session
10+
11+
@pytest.mark.asyncio
12+
async def test_create_room(session):
13+
async with session.post(f"{BASE_URL}/create-room") as response:
14+
assert response.status == 200
15+
data = await response.json()
16+
assert "roomId" in data
17+
assert isinstance(data["roomId"], str)
18+
19+
@pytest.mark.asyncio
20+
async def test_join_room(session):
21+
# First, create a room
22+
async with session.post(f"{BASE_URL}/create-room") as response:
23+
room_data = await response.json()
24+
room_id = room_data["roomId"]
25+
26+
# Now, join the room
27+
join_data = {
28+
"user_name": "TestUser",
29+
"lang": "eng",
30+
"room_id": room_id
31+
}
32+
async with session.post(f"{BASE_URL}/join-room", data=join_data) as response:
33+
assert response.status == 200
34+
data = await response.json()
35+
assert "userId" in data
36+
assert isinstance(data["userId"], str)
37+
38+
@pytest.mark.asyncio
39+
async def test_get_rooms(session):
40+
async with session.get(f"{BASE_URL}/rooms") as response:
41+
assert response.status == 200
42+
data = await response.json()
43+
assert isinstance(data, dict)
44+
for room_id, room_info in data.items():
45+
assert isinstance(room_id, str)
46+
assert "name" in room_info
47+
assert "members" in room_info
48+
49+
@pytest.mark.asyncio
50+
async def test_get_room_info(session):
51+
# First, create a room
52+
async with session.post(f"{BASE_URL}/create-room") as response:
53+
room_data = await response.json()
54+
room_id = room_data["roomId"]
55+
56+
# Now, get room info
57+
async with session.get(f"{BASE_URL}/room-info?room_id={room_id}") as response:
58+
assert response.status == 200
59+
data = await response.json()
60+
assert "name" in data
61+
assert "members" in data
62+
assert isinstance(data["members"], dict)
63+
64+
@pytest.mark.asyncio
65+
async def test_websocket_chat():
66+
async def connect_and_chat(user_name: str, lang: str) -> Dict[str, Any]:
67+
async with aiohttp.ClientSession() as session:
68+
# Create and join a room
69+
async with session.post(f"{BASE_URL}/create-room") as response:
70+
room_data = await response.json()
71+
room_id = room_data["roomId"]
72+
73+
join_data = {
74+
"user_name": user_name,
75+
"lang": lang,
76+
"room_id": room_id
77+
}
78+
async with session.post(f"{BASE_URL}/join-room", data=join_data) as response:
79+
user_data = await response.json()
80+
user_id = user_data["userId"]
81+
82+
# Connect to WebSocket
83+
async with session.ws_connect(f"{BASE_URL}/chat") as ws:
84+
await ws.send_json({"user_id": user_id, "room_id": room_id})
85+
86+
# Send a message
87+
await ws.send_json({
88+
"message_type": "text",
89+
"content": "Hello, World!"
90+
})
91+
92+
# Wait for a response
93+
response = await ws.receive_json(timeout=10)
94+
return response
95+
96+
# Test with two users
97+
results = await asyncio.gather(
98+
connect_and_chat("User1", "eng"),
99+
connect_and_chat("User2", "fra")
100+
)
101+
102+
for result in results:
103+
assert "messageId" in result
104+
assert "userId" in result
105+
assert "userName" in result
106+
assert "lang" in result
107+
assert "text" in result
108+
assert "audio" in result
109+
assert isinstance(result["audio"], list)
110+
111+
print(results)
112+
113+
114+
if __name__ == "__main__":
115+
pytest.main([__file__])

0 commit comments

Comments
 (0)