Files
romm/backend/tests/handler/test_socket_handler.py
zurdi bb5769e606 refactor: apply the simplify pass
- One home for pushing to a user's room: `SocketHandler.emit_to_user`, on a
  write-only manager kept per event loop and on the server's own channel.
  Streaming's push_to_user and the sync and scan emitters use it too.
- Notifications are stored in one transaction per send, so a broadcast is one
  commit, and pushed concurrently. Recipients are resolved from id and
  enabled only, and the actor is loaded with just the fields it shows.
- notify_user_or_admins holds the "whoever started it, else the admins"
  rule both the scan and the task runner applied by hand.
- A scan notifies from finish(), so no exit path can forget it.
- `data` is never null, dropping its guards; the toast host, the inbox and
  the admin form share one tone icon map; the inbox icon uses RAvatar.
- The store keeps its list when a socket echo changes nothing, and small
  dead parameters and duplicated lists are gone.
- The socket reconnect after a logout moved from the inbox to AppLayout,
  since every v2 feature depends on the socket's identity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 13:04:18 +02:00

52 lines
2.1 KiB
Python

from unittest.mock import AsyncMock, MagicMock
from handler import socket_handler as socket_handler_module
from handler.socket_handler import SocketHandler, netplay_socket_handler, socket_handler
def test_netplay_cannot_join_the_main_servers_rooms():
# Netplay clients are unauthenticated and name their own rooms, so an emit to
# `user:{id}` or `admin` must not reach a netplay socket that picked that name.
netplay = netplay_socket_handler.socket_server.manager
main = socket_handler.socket_server.manager
assert netplay.channel != main.channel
class TestEmitToUser:
async def test_targets_the_users_room_on_the_servers_channel(self, mocker):
manager = MagicMock(emit=AsyncMock())
make = mocker.patch.object(
socket_handler_module.socketio, "AsyncRedisManager", return_value=manager
)
handler = SocketHandler(path="/test", channel="test-channel")
await handler.emit_to_user(5, "notifications:read", {"ids": None})
assert make.call_args.kwargs == {"channel": "test-channel", "write_only": True}
manager.emit.assert_awaited_once_with(
"notifications:read", {"ids": None}, room="user:5"
)
async def test_reuses_one_client_on_the_same_loop(self, mocker):
manager = MagicMock(emit=AsyncMock())
make = mocker.patch.object(
socket_handler_module.socketio, "AsyncRedisManager", return_value=manager
)
handler = SocketHandler(path="/test")
make.reset_mock()
await handler.emit_to_user(5, "notifications:read", {"ids": None})
await handler.emit_to_user(6, "notifications:read", {"ids": None})
make.assert_called_once()
assert manager.emit.await_count == 2
async def test_swallows_a_broker_failure(self, mocker):
manager = MagicMock(emit=AsyncMock(side_effect=ConnectionError("redis")))
mocker.patch.object(
socket_handler_module.socketio, "AsyncRedisManager", return_value=manager
)
handler = SocketHandler(path="/test")
await handler.emit_to_user(5, "notifications:read", {"ids": None})