Files
google-labs-jules[bot] 90fdb8f57b feat(core): add real-time online user count to chat (#79)
- Update `ChatConsumer` to track connected users using Redis cache
- Broadcast `user_count` events to WebSocket group on connect/disconnect
- Update `player.html` to display the online user count
- Distinguish between chat messages and system events in WebSocket payload
- Add pytest `core/tests/test_chat_count.py` for verification

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-01-20 17:29:05 +00:00

81 lines
2.5 KiB
Python

import pytest
from channels.testing import WebsocketCommunicator
from aniscrap_core.asgi import application
from django.core.cache import cache
@pytest.mark.asyncio
@pytest.mark.django_db
async def test_chat_user_count():
# Setup
room_name = "test_room_count"
# Clean cache key
cache_key = f"chat_count_{room_name}"
cache.delete(cache_key)
# 1. Connect first user
communicator1 = WebsocketCommunicator(application, f"/ws/chat/{room_name}/")
connected1, _ = await communicator1.connect()
assert connected1
# User 1 should receive count = 1
# We loop to find the 'user_count' message as other messages (history) might arrive
found_count_1 = False
for _ in range(10):
try:
response = await communicator1.receive_json_from(timeout=1)
if response.get('type') == 'user_count' and response['count'] == 1:
found_count_1 = True
break
except:
break
assert found_count_1, "User 1 did not receive count=1"
# 2. Connect second user
communicator2 = WebsocketCommunicator(application, f"/ws/chat/{room_name}/")
connected2, _ = await communicator2.connect()
assert connected2
# User 1 should receive count = 2
found_count_2_u1 = False
for _ in range(10):
try:
response = await communicator1.receive_json_from(timeout=1)
if response.get('type') == 'user_count' and response['count'] == 2:
found_count_2_u1 = True
break
except:
break
assert found_count_2_u1, "User 1 did not receive count=2"
# User 2 should receive count = 2
found_count_2_u2 = False
for _ in range(10):
try:
response = await communicator2.receive_json_from(timeout=1)
if response.get('type') == 'user_count' and response['count'] == 2:
found_count_2_u2 = True
break
except:
break
assert found_count_2_u2, "User 2 did not receive count=2"
# 3. Disconnect User 2
await communicator2.disconnect()
# User 1 should receive count = 1
found_count_1_u1 = False
for _ in range(10):
try:
response = await communicator1.receive_json_from(timeout=1)
if response.get('type') == 'user_count' and response['count'] == 1:
found_count_1_u1 = True
break
except:
break
assert found_count_1_u1, "User 1 did not receive count=1 (disconnect)"
# Cleanup
await communicator1.disconnect()