"""Helpers shared by the server tests.""" SECRET = "test-secret" # Tests play much faster than humans, so the message rate limit is raised. TEST_SETTINGS = dict( ai_shared_secret=SECRET, ai_move_delay=0, trick_pause=0, ai_decision_timeout=2, message_rate=10_000, message_burst=10_000, ) def receive_until(ws, predicate, limit=5000): for _ in range(limit): message = ws.receive_json() if predicate(message): return message raise AssertionError("expected message not received") def is_state(message): return message["type"] == "state" def connect(client, token=None): ws = client.sockets.enter_context(client.websocket_connect("/ws")) ws.send_json({"type": "hello", "token": token}) welcome = receive_until(ws, lambda m: m["type"] == "welcome") receive_until(ws, is_state) return ws, welcome