fix(realtime): restore websocket phase event type
All checks were successful
CI / test-and-quality (push) Successful in 2m52s
CI / test-and-quality (pull_request) Successful in 2m53s

This commit is contained in:
2026-03-15 10:32:10 +00:00
parent 62174135b8
commit 5c9d29a3a7
2 changed files with 25 additions and 2 deletions

View File

@@ -58,4 +58,6 @@ class GameConsumer(AsyncJsonWebsocketConsumer):
async def phase_event(self, event):
"""Forward any phase_event broadcast to the WebSocket client."""
await self.send_json(event["payload"])
payload = dict(event.get("payload") or {})
payload["type"] = event["event_type"]
await self.send_json(payload)

View File

@@ -1,5 +1,5 @@
import unittest
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, Mock, patch
from channels.exceptions import InvalidChannelLayerError
from django.contrib.auth import get_user_model
@@ -13,6 +13,7 @@ except Exception: # pragma: no cover - optional test dependency
from fupogfakta.models import GameSession, Player
from partyhub.asgi import application
from realtime.broadcast import broadcast_phase_event, sync_broadcast_phase_event
from realtime.consumers import GameConsumer
User = get_user_model()
@@ -38,6 +39,26 @@ class BroadcastPhaseEventTests(SimpleTestCase):
sender.assert_called_once_with("ABCD", "phase.scoreboard", {"phase": "scoreboard"})
class GameConsumerPhaseEventTests(SimpleTestCase):
async def test_phase_event_restores_external_type_field(self):
consumer = GameConsumer()
consumer.send_json = AsyncMock()
await consumer.phase_event(
{
"event_type": "phase.test_event",
"payload": {"hello": "world"},
}
)
consumer.send_json.assert_awaited_once_with(
{
"type": "phase.test_event",
"hello": "world",
}
)
@unittest.skipIf(WebsocketCommunicator is None, "channels.testing dependencies unavailable")
class GameConsumerConnectTest(TestCase):
def setUp(self):