|
|
@@ -38,8 +38,8 @@ async def _make_client(path, timeout, script): |
|
|
|
elif kind == 'send': |
|
|
|
await send_json(websocket, **message) |
|
|
|
|
|
|
|
while True: |
|
|
|
if state['users'][0] == state['username']: |
|
|
|
while 'users' in state: |
|
|
|
if min(state['users']) == state['username']: |
|
|
|
break |
|
|
|
message = await recv_json(websocket) |
|
|
|
message.pop('ts') |
|
|
@@ -59,52 +59,48 @@ async def _test(*clients): |
|
|
|
start_server('localhost', 8642, 'TestServer'), |
|
|
|
*clients, |
|
|
|
) |
|
|
|
server, *_ = await asyncio.wait_for(gather, timeout=2) |
|
|
|
server, *results = await asyncio.wait_for(gather, timeout=2) |
|
|
|
server.close() |
|
|
|
except asyncio.TimeoutError: |
|
|
|
return |
|
|
|
|
|
|
|
return results |
|
|
|
|
|
|
|
|
|
|
|
def test_happy_path(): |
|
|
|
client = _make_client('ws://localhost:8642/', 0.1, Script() |
|
|
|
+ {'action': 'login', 'username': 'TestUser'} |
|
|
|
- {'kind': 'update', 'username': 'TestUser'} |
|
|
|
- {'kind': 'update', 'users': ['TestUser'], 'info': 'TestUser joined'} |
|
|
|
client = _make_client('ws://TestUser@localhost:8642/A', 0.1, Script() |
|
|
|
- {'kind': 'update', 'users': ['TestUser'], 'info': 'Welcome to /A', 'username': 'TestUser'} |
|
|
|
+ {'action': 'post', 'text': 'Hello World!'} |
|
|
|
- {'kind': 'post', 'source': 'TestUser', 'text': 'Hello World!'} |
|
|
|
) |
|
|
|
return _test(client) |
|
|
|
|
|
|
|
|
|
|
|
def test_post_before_login(): |
|
|
|
client = _make_client('ws://localhost:8642/', 0.1, Script() |
|
|
|
+ {} |
|
|
|
- {'kind': 'error', 'info': 'Message without action is invalid'} |
|
|
|
+ {'action': 'post', 'text': ''} |
|
|
|
- {'kind': 'error', 'info': 'Not logged in'} |
|
|
|
+ {'action': 'login', 'username': ''} |
|
|
|
- {'kind': 'error', 'info': 'Invalid username'} |
|
|
|
+ {'action': 'login', 'username': 'Joe'} |
|
|
|
- {'kind': 'update', 'username': 'Joe'} |
|
|
|
- {'kind': 'update', 'users': ['Joe'], 'info': 'Joe joined'} |
|
|
|
def test_name_taken(): |
|
|
|
client1 = _make_client('ws://A@localhost:8642/', 0.10, Script() |
|
|
|
- {'kind': 'update', 'users': ['A'], 'info': 'Welcome to /', 'username': 'A'} |
|
|
|
- {'kind': 'update', 'users': ['A', 'B'], 'info': 'B joined'} |
|
|
|
) |
|
|
|
return _test(client) |
|
|
|
client2 = _make_client('ws://A@localhost:8642/', 0.11, Script() |
|
|
|
- {'kind': 'error', 'info': 'Username taken'} |
|
|
|
) |
|
|
|
client3 = _make_client('ws://B@localhost:8642/', 0.12, Script() |
|
|
|
- {'kind': 'update', 'users': ['A', 'B'], 'info': 'Welcome to /', 'username': 'B'} |
|
|
|
- {'kind': 'update', 'users': ['B'], 'info': 'A left'} |
|
|
|
) |
|
|
|
return _test(client1, client2, client3) |
|
|
|
|
|
|
|
|
|
|
|
def test_interact(): |
|
|
|
client1 = _make_client('ws://localhost:8642/', 0.10, Script() |
|
|
|
+ {'action': 'login', 'username': 'Alice'} |
|
|
|
- {'kind': 'update', 'username': 'Alice'} |
|
|
|
- {'kind': 'update', 'users': ['Alice'], 'info': 'Alice joined'} |
|
|
|
client1 = _make_client('ws://Alice@localhost:8642/', 0.10, Script() |
|
|
|
- {'kind': 'update', 'users': ['Alice'], 'info': 'Welcome to /', 'username': 'Alice'} |
|
|
|
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Bob joined'} |
|
|
|
+ {'action': 'post', 'text': 'Hey Bob!'} |
|
|
|
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'} |
|
|
|
- {'kind': 'post', 'source': 'Bob', 'text': 'Howdy!'} |
|
|
|
) |
|
|
|
client2 = _make_client('ws://localhost:8642/', 0.11, Script() |
|
|
|
+ {'action': 'login', 'username': 'Bob'} |
|
|
|
- {'kind': 'update', 'username': 'Bob'} |
|
|
|
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Bob joined'} |
|
|
|
client2 = _make_client('ws://Bob@localhost:8642/', 0.11, Script() |
|
|
|
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Welcome to /', 'username': 'Bob'} |
|
|
|
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'} |
|
|
|
+ {'action': 'post', 'text': 'Howdy!'} |
|
|
|
- {'kind': 'post', 'source': 'Bob', 'text': 'Howdy!'} |
|
|
@@ -115,46 +111,36 @@ def test_interact(): |
|
|
|
|
|
|
|
|
|
|
|
def test_party(): |
|
|
|
client1 = _make_client('ws://localhost:8642/', 0.10, Script() |
|
|
|
+ {'action': 'login', 'username': 'Norman'} |
|
|
|
- {'kind': 'update', 'username': 'Norman'} |
|
|
|
- {'kind': 'update', 'users': ['Norman'], 'info': 'Norman joined'} |
|
|
|
client1 = _make_client('ws://Norman@localhost:8642/', 0.10, Script() |
|
|
|
- {'kind': 'update', 'users': ['Norman'], 'info': 'Welcome to /', 'username': 'Norman'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray', 'Emma'], 'info': 'Emma joined'} |
|
|
|
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'} |
|
|
|
+ {'action': 'post', 'text': 'なに?'} |
|
|
|
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'} |
|
|
|
) |
|
|
|
client2 = _make_client('ws://localhost:8642/', 0.11, Script() |
|
|
|
+ {'action': 'login', 'username': 'Ray'} |
|
|
|
- {'kind': 'update', 'username': 'Ray'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray', 'Emma'], 'info': 'Emma joined'} |
|
|
|
client2 = _make_client('ws://Ray@localhost:8642/', 0.11, Script() |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Ray'} |
|
|
|
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'} |
|
|
|
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} |
|
|
|
- {'kind': 'update', 'users': ['Ray', 'Emma'], 'info': 'Norman left'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'} |
|
|
|
- {'kind': 'update', 'users': ['Ray'], 'info': 'Norman left'} |
|
|
|
) |
|
|
|
client3 = _make_client('ws://localhost:8642/', 0.12, Script() |
|
|
|
+ {'action': 'login', 'username': 'Emma'} |
|
|
|
- {'kind': 'update', 'username': 'Emma'} |
|
|
|
- {'kind': 'update', 'users': ['Norman', 'Ray', 'Emma'], 'info': 'Emma joined'} |
|
|
|
client3 = _make_client('ws://Emma@localhost:8642/', 0.12, Script() |
|
|
|
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Emma'} |
|
|
|
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} |
|
|
|
- {'kind': 'update', 'users': ['Ray', 'Emma'], 'info': 'Norman left'} |
|
|
|
- {'kind': 'update', 'users': ['Emma'], 'info': 'Ray left'} |
|
|
|
) |
|
|
|
return _test(client1, client2, client3) |
|
|
|
|
|
|
|
|
|
|
|
def test_rooms(): |
|
|
|
client1 = _make_client('ws://localhost:8642/A', 0.10, Script() |
|
|
|
+ {'action': 'login', 'username': 'Dandy'} |
|
|
|
- {'kind': 'update', 'username': 'Dandy'} |
|
|
|
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Dandy joined'} |
|
|
|
client1 = _make_client('ws://Dandy@localhost:8642/A', 0.10, Script() |
|
|
|
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /A', 'username': 'Dandy'} |
|
|
|
+ {'action': 'post', 'text': 'Hi'} |
|
|
|
- {'kind': 'post', 'source': 'Dandy', 'text': 'Hi'} |
|
|
|
) |
|
|
|
client2 = _make_client('ws://localhost:8642/B', 0.10, Script() |
|
|
|
+ {'action': 'login', 'username': 'Dandy'} |
|
|
|
- {'kind': 'update', 'username': 'Dandy'} |
|
|
|
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Dandy joined'} |
|
|
|
client2 = _make_client('ws://Dandy@localhost:8642/B', 0.10, Script() |
|
|
|
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /B', 'username': 'Dandy'} |
|
|
|
+ {'action': 'post', 'text': 'Howdy'} |
|
|
|
- {'kind': 'post', 'source': 'Dandy', 'text': 'Howdy'} |
|
|
|
) |