Projects / city /ย test_demo.py

git clone https://molodetz.nl/retoor/city.git

Raw source file available here .

#!/usr/bin/env python3
"""
Quick test demo showing the working test framework functionality
"""
import asyncio
import sys
import os

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from tests.test_client import test_clients


async def demo_test():
"""Demo of the testing framework functionality"""
print("๐ŸŽฎ City Builder - Test Framework Demo")
print("=" * 50)

# Test 1: Single client connection
print("\nโœ… Test 1: Single client connection")
async with test_clients("demo_player") as [client]:
print(f" Connected: {client.connected}")
print(f" Player: {client.player_data['nickname']}")
print(f" Money: ${client.get_money():,}")
print(f" Population: {client.get_population()}")

# Test 2: Building placement
print("\nโœ… Test 2: Building placement")
async with test_clients("builder") as [client]:
# Place a road
await client.place_building("road", 50, 50)
message = await client.receive_message(timeout=2.0)
if message and message["type"] == "building_placed":
print(f" Successfully placed: {message['building']['type']}")
print(f" At coordinates: ({message['building']['x']}, {message['building']['y']})")
else:
print(f" Error or unexpected message: {message}")

# Test 3: Chat functionality
print("\nโœ… Test 3: Chat functionality")
async with test_clients("sender", "receiver") as [sender, receiver]:
# Clear any initial messages
receiver.clear_messages()

# Send chat message
test_message = "Hello from test framework!"
await sender.send_chat(test_message)

# Receive chat message
message = await receiver.receive_message(timeout=2.0)
if message and message["type"] == "chat":
print(f" Message sent: '{test_message}'")
print(f" Message received: '{message['message']}'")
print(f" From: {message['nickname']}")
else:
print(f" Error or unexpected message: {message}")

# Test 4: Multiple client interaction
print("\nโœ… Test 4: Multiple client interaction")
async with test_clients("alice", "bob", "charlie") as [alice, bob, charlie]:
print(f" Connected clients: {len([alice, bob, charlie])}")
print(f" Alice: ${alice.get_money():,}")
print(f" Bob: ${bob.get_money():,}")
print(f" Charlie: ${charlie.get_money():,}")

# All clients send cursor movements simultaneously
await asyncio.gather(
alice.send_cursor_move(10, 20),
bob.send_cursor_move(30, 40),
charlie.send_cursor_move(50, 60)
)
print(" All cursor movements sent successfully")

print("\n๐ŸŽ‰ Test framework demo completed!")
print("\n๐Ÿ“Š Summary:")
print(" โœ… WebSocket connections work")
print(" โœ… Building placement works")
print(" โœ… Chat system works")
print(" โœ… Multiple clients work")
print(" โœ… Async operations work")


if __name__ == "__main__":
asyncio.run(demo_test())