Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.7 KB
Raw Blame
"""Unit tests for socketio_asgi.emit_to_user's per-user scoping.

emit_to_user is the FastAPI-native mechanism behind the #4437 cross-user-leak
fix: events like settings_changed (which carry raw setting values, including
plaintext API keys) must reach only the sockets authenticated as the owning
user — never every connected client. The Flask implementation used a per-user
Socket.IO room; here the connect handler records sid→username in _sid_users
and emit_to_user emits per matching sid. These tests pin that selection logic
with a real event loop and a stubbed AsyncServer.
"""

import asyncio
import threading
import time
from unittest.mock import AsyncMock, Mock, patch

import pytest

from local_deep_research.web.services import socketio_asgi
from local_deep_research.web.services.socketio_asgi import emit_to_user


@pytest.fixture
def loop_thread():
    """A running event loop on a background thread, installed as the module's
    main loop with a fresh lock (the real ones are created at app lifespan
    startup, which never ran in this unit-test process)."""
    loop = asyncio.new_event_loop()
    thread = threading.Thread(target=loop.run_forever, daemon=True)
    thread.start()

    old_lock = socketio_asgi._lock
    socketio_asgi._lock = asyncio.Lock()
    try:
        with patch.object(socketio_asgi, "_get_main_loop", return_value=loop):
            yield loop
    finally:
        socketio_asgi._lock = old_lock
        loop.call_soon_threadsafe(loop.stop)
        thread.join(timeout=5)
        loop.close()


@pytest.fixture
def sid_users():
    """Seed _sid_users and restore the previous contents afterwards."""
    saved = dict(socketio_asgi._sid_users)
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_users.update(
        {"sid-alice-1": "alice", "sid-alice-2": "alice", "sid-bob": "bob"}
    )
    yield socketio_asgi._sid_users
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_users.update(saved)


def _barrier(loop):
    """Wait until everything already scheduled on the loop has run."""

    async def _noop():
        return None

    asyncio.run_coroutine_threadsafe(_noop(), loop).result(timeout=5)


def _wait_for(predicate, timeout=5.0):
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        if predicate():
            return True
        time.sleep(0.01)
    return False


class TestEmitToUserScoping:
    def test_emits_only_to_target_users_sids(self, loop_thread, sid_users):
        """Both of alice's sockets get the event; bob's never does."""
        mock_sio = Mock()
        mock_sio.emit = AsyncMock()

        with patch.object(socketio_asgi, "sio", mock_sio):
            assert emit_to_user("settings_changed", "alice", {"k": "v"}) is True
            _barrier(loop_thread)
            assert _wait_for(lambda: mock_sio.emit.await_count == 2)

        targeted_rooms = {
            call.kwargs["room"] for call in mock_sio.emit.await_args_list
        }
        assert targeted_rooms == {"sid-alice-1", "sid-alice-2"}
        for call in mock_sio.emit.await_args_list:
            assert call.args[0] == "settings_changed"
            assert call.args[1] == {"k": "v"}

    def test_no_sockets_for_user_emits_nothing(self, loop_thread, sid_users):
        """A user with no connected sockets gets no emit — and crucially the
        event is not broadcast to anyone else's sockets."""
        mock_sio = Mock()
        mock_sio.emit = AsyncMock()

        with patch.object(socketio_asgi, "sio", mock_sio):
            emit_to_user("settings_changed", "carol", {"k": "v"})
            _barrier(loop_thread)

        mock_sio.emit.assert_not_awaited()

    def test_one_failing_socket_does_not_stop_the_others(
        self, loop_thread, sid_users
    ):
        """An emit error on one sid must not prevent delivery to the user's
        remaining sockets."""
        mock_sio = Mock()
        mock_sio.emit = AsyncMock(
            side_effect=[RuntimeError("socket gone"), None]
        )

        with patch.object(socketio_asgi, "sio", mock_sio):
            assert emit_to_user("settings_changed", "alice", {}) is True
            _barrier(loop_thread)
            assert _wait_for(lambda: mock_sio.emit.await_count == 2)


class TestEmitToUserNoLoop:
    def test_returns_false_when_no_main_loop(self):
        with patch.object(socketio_asgi, "_get_main_loop", return_value=None):
            assert emit_to_user("settings_changed", "alice", {}) is False

    def test_returns_false_when_loop_not_running(self):
        loop = asyncio.new_event_loop()
        try:
            with patch.object(
                socketio_asgi, "_get_main_loop", return_value=loop
            ):
                assert emit_to_user("settings_changed", "alice", {}) is False
        finally:
            loop.close()