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.

13.3 KB
Raw Blame
"""Socket properties main pinned that no successor on this branch does.

Ported from the three real socket test files the Flask->FastAPI migration
deleted -- ``tests/web/services/test_socket_service.py``,
``test_socket_service_coverage.py`` and ``test_socket_service_concurrency.py``
-- reduced to what the branch's own eight socket modules leave uncovered.
Everything else in those files is either already pinned (the connect gate, the
teardown race, owner-scoped emit and removal, the ``run_coroutine_threadsafe``
seam, the logging-suppression ContextVar, the snapshot-copy that lets a
mid-fanout disconnect through) or was Flask-only plumbing with no FastAPI
meaning (the ``SocketIOService`` singleton, ``join_room``/``leave_room``,
``run()``'s werkzeug import, the ``__log_*`` gate methods).

What is left is four things:

1. **Per-subscriber failure isolation in the fanout.** The branch has
   ``test_a_failing_transport_does_not_escape_into_the_dropped_future``, which
   drives ``emit_to_subscribers`` with an sio whose every call raises and
   asserts nothing escaped -- but it seeds a single sid, so it cannot tell
   "caught and continued" from "caught and returned". Replacing the loop's
   ``except`` body with a ``return`` keeps that test green while every
   subscriber after the first broken socket stops receiving the run.
   ``test_a_tab_closing_mid_fanout...`` covers a *disconnect* interleaving,
   not an emit that raises. Main pinned it as
   ``test_one_subscriber_fails_others_succeed``.

2. **The same isolation in ``_disconnect_matching``.** One socket refusing to
   close must not leave the rest of a logged-out session connected -- that is
   the whole point of the teardown. Main: ``TestDisconnectUser::
   test_disconnect_user_continues_when_one_disconnect_fails``.

3. **The catch-up snapshot replay on subscribe.** ``on_subscribe`` ends by
   replaying the latest progress frame to the newly-subscribed sid. That is
   what makes the "drop events with no subscribers" policy safe: a client that
   subscribes after the run started would otherwise see a frozen bar until the
   next frame. ``get_active_research_snapshot`` appears in no socket test on
   the branch, so the whole replay -- and its ``if latest_log`` guard -- is
   unpinned. Main: ``TestHandleSubscribeEdgeCases``.

4. **``remove_subscriptions_for_research`` logging only when it removed
   something.** ``_async_remove_subscriptions`` pops with a ``None`` default
   and guards the log on it; without the guard the cleanup raises
   ``TypeError: object of type 'NoneType' has no len()`` on every completed
   run that had no subscribers. Main: ``TestRemoveSubscriptionsNoneBranch``.
"""

import asyncio
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.web.services import socketio_asgi as sio_mod


@pytest.fixture
def socket_state(monkeypatch):
    """Isolated socket routing state plus a capturing ``sio.emit``."""
    sio_mod.init_lock()
    monkeypatch.setattr(sio_mod, "_subscriptions", {})
    monkeypatch.setattr(sio_mod, "_sid_users", {})
    monkeypatch.setattr(sio_mod, "_sid_sessions", {})

    delivered = []

    async def _capture(event, data, room=None):
        delivered.append((room, event, data))

    monkeypatch.setattr(sio_mod.sio, "emit", _capture)
    return delivered


def _subscribe(username, research_id, sid, session_id=None):
    sio_mod._sid_users[sid] = username
    sio_mod._sid_sessions[sid] = session_id or f"session-{username}"
    sio_mod._subscriptions.setdefault(
        sio_mod._subscription_key(username, research_id), set()
    ).add(sid)


# ---------------------------------------------------------------------------
# 1. One broken socket must not strand the rest of the fanout.
# ---------------------------------------------------------------------------


class TestOneFailingSubscriberDoesNotStopTheOthers:
    def test_every_subscriber_is_attempted_when_one_raises(
        self, socket_state, monkeypatch
    ):
        """A dead socket in the middle of the set is skipped, not fatal.

        Without the per-sid ``try/except`` -- or with an ``except`` that
        aborts rather than continues -- the user's remaining tabs never learn
        the run progressed past the frame that hit the broken socket.
        """
        attempted = []

        async def _flaky(event, data, room=None):
            attempted.append(room)
            if room == "sid-2":
                raise RuntimeError("connection lost")

        monkeypatch.setattr(sio_mod.sio, "emit", _flaky)

        for sid in ("sid-1", "sid-2", "sid-3"):
            _subscribe("alice", "r4", sid)

        asyncio.run(
            sio_mod._async_emit_to_subscribers(
                "research_progress", "r4", {"progress": 40}, "alice"
            )
        )

        assert sorted(attempted) == ["sid-1", "sid-2", "sid-3"], (
            "the fanout stopped at the broken socket; the remaining "
            f"subscribers were never attempted: {attempted}"
        )

    def test_the_healthy_subscribers_still_receive_the_payload(
        self, monkeypatch
    ):
        """Positive control for the test above: "attempted" is not enough --
        the surviving sockets must actually get the frame."""
        delivered = []

        async def _flaky(event, data, room=None):
            if room == "sid-2":
                raise RuntimeError("connection lost")
            delivered.append((room, event, data))

        sio_mod.init_lock()
        monkeypatch.setattr(sio_mod, "_subscriptions", {})
        monkeypatch.setattr(sio_mod, "_sid_users", {})
        monkeypatch.setattr(sio_mod, "_sid_sessions", {})
        monkeypatch.setattr(sio_mod.sio, "emit", _flaky)

        for sid in ("sid-1", "sid-2", "sid-3"):
            _subscribe("alice", "r4", sid)

        asyncio.run(
            sio_mod._async_emit_to_subscribers(
                "research_progress", "r4", {"progress": 40}, "alice"
            )
        )

        assert sorted(room for room, _, _ in delivered) == ["sid-1", "sid-3"]
        assert all(data == {"progress": 40} for _, _, data in delivered)


# ---------------------------------------------------------------------------
# 2. The same isolation on the teardown side.
# ---------------------------------------------------------------------------


class TestOneFailingDisconnectDoesNotStopTheTeardown:
    def test_every_socket_of_a_dead_session_is_still_severed(
        self, socket_state, monkeypatch
    ):
        """Logout severs a session's sockets. If the first socket refuses to
        close, the rest must still be disconnected -- otherwise one wedged
        tab keeps a logged-out session receiving that user's events,
        including ``settings_changed``, which carries plaintext secrets."""
        attempted = []

        async def _flaky_disconnect(sid):
            attempted.append(sid)
            if sid == "sid-b":
                raise RuntimeError("socket wedged")

        monkeypatch.setattr(sio_mod.sio, "disconnect", _flaky_disconnect)

        for sid in ("sid-a", "sid-b", "sid-c"):
            sio_mod._sid_sessions[sid] = "sess-A"

        # Drive the coroutine the sync wrapper schedules, on this thread.
        select = [
            sid for sid, s in sio_mod._sid_sessions.items() if s == "sess-A"
        ]
        captured = {}

        def _fake_schedule(coro, loop):
            captured["coro"] = coro
            return MagicMock()

        loop = MagicMock()
        loop.is_running.return_value = True
        with (
            patch.object(sio_mod, "_get_main_loop", return_value=loop),
            patch.object(
                sio_mod.asyncio, "run_coroutine_threadsafe", _fake_schedule
            ),
        ):
            assert sio_mod.disconnect_session("sess-A") is True

        asyncio.run(captured["coro"])

        assert sorted(attempted) == sorted(select), (
            "the teardown stopped at the wedged socket; the rest of the "
            f"session stayed connected: {attempted}"
        )


# ---------------------------------------------------------------------------
# 3. The catch-up snapshot replayed on subscribe.
# ---------------------------------------------------------------------------


class TestSubscribeCatchUpSnapshot:
    """``on_subscribe``'s replay of the latest progress frame.

    Delivery is dropped when nobody is subscribed (never broadcast), so this
    replay is what stops a client that subscribes mid-run from sitting on a
    stale bar until the next frame happens to arrive.
    """

    @staticmethod
    def _subscribe(data, snapshot):
        with (
            patch.object(
                sio_mod,
                "_socket_session_still_valid",
                new=_async_return(("sess-A", True)),
            ),
            patch.object(
                sio_mod, "_user_owns_research", new=_async_return(True)
            ),
            patch.object(
                sio_mod, "get_active_research_snapshot", return_value=snapshot
            ),
        ):
            asyncio.run(sio_mod.on_subscribe("sid-1", data))

    def test_a_missing_research_id_subscribes_to_nothing(self, socket_state):
        """Falsy id => immediate return, before the auth gate even runs. A
        subscription keyed on ``None`` would collect sids that no emit can
        ever reach."""
        sio_mod._sid_users["sid-1"] = "alice"

        asyncio.run(sio_mod.on_subscribe("sid-1", {}))

        assert sio_mod._subscriptions == {}
        assert socket_state == []

    def test_no_active_snapshot_replays_nothing(self, socket_state):
        """A finished (or never-registered) run has no snapshot; subscribing
        must not synthesise a frame."""
        sio_mod._sid_users["sid-1"] = "alice"
        sio_mod._sid_sessions["sid-1"] = "sess-A"

        self._subscribe({"research_id": "r1"}, None)

        assert sio_mod._subscriptions[
            sio_mod._subscription_key("alice", "r1")
        ] == {"sid-1"}
        assert socket_state == []

    def test_a_snapshot_with_an_empty_log_replays_nothing(self, socket_state):
        """``latest_log`` is falsy, so there is no message to replay. Dropping
        the guard emits ``log_entry: None`` and a "Processing..." placeholder
        over whatever the client already had."""
        sio_mod._sid_users["sid-1"] = "alice"
        sio_mod._sid_sessions["sid-1"] = "sess-A"

        self._subscribe({"research_id": "r1"}, {"progress": 42, "log": []})

        assert socket_state == []

    def test_a_snapshot_with_a_log_replays_the_latest_frame_to_that_sid(
        self, socket_state
    ):
        sio_mod._sid_users["sid-1"] = "alice"
        sio_mod._sid_sessions["sid-1"] = "sess-A"
        latest = {"message": "Searching...", "type": "info"}

        self._subscribe(
            {"research_id": "r1"},
            {"progress": 75, "log": [{"message": "older"}, latest]},
        )

        assert len(socket_state) == 1
        room, event, payload = socket_state[0]
        # Addressed to the subscribing socket only, never the room-less
        # broadcast that would hand one user's progress to everyone.
        assert room == "sid-1"
        # The per-research event name the client listens on.
        assert event == "research_progress_r1"
        assert payload["progress"] == 75
        # The LAST log entry, not the first.
        assert payload["message"] == "Searching..."
        assert payload["log_entry"] == latest
        assert payload["status"] == "in_progress"

    def test_a_log_entry_without_a_message_gets_the_placeholder(
        self, socket_state
    ):
        sio_mod._sid_users["sid-1"] = "alice"
        sio_mod._sid_sessions["sid-1"] = "sess-A"

        self._subscribe(
            {"research_id": "r1"}, {"progress": 10, "log": [{"type": "info"}]}
        )

        assert socket_state[0][2]["message"] == "Processing..."


def _async_return(value):
    async def _coro(*args, **kwargs):
        return value

    return _coro


# ---------------------------------------------------------------------------
# 4. Subscription cleanup for a run nobody was watching.
# ---------------------------------------------------------------------------


class TestRemoveSubscriptionsForAnUnwatchedRun:
    def test_removing_a_key_that_was_never_stored_is_silent_and_safe(
        self, socket_state
    ):
        """Every completed run calls this, and most have no subscribers left
        by then. The ``removed is not None`` guard is what keeps that from
        raising ``TypeError`` on ``len(None)`` inside the cleanup path."""
        recorded = []

        with patch.object(
            sio_mod.logger, "info", lambda *a, **k: recorded.append(a)
        ):
            asyncio.run(
                sio_mod._async_remove_subscriptions("never-seen", "alice")
            )

        assert recorded == []

    def test_removing_a_watched_run_reports_how_many_it_dropped(
        self, socket_state
    ):
        """Positive control: the silence above is the guard, not a broken
        logger patch."""
        _subscribe("alice", "r10", "sid-a")
        _subscribe("alice", "r10", "sid-b")

        recorded = []

        with patch.object(
            sio_mod.logger, "info", lambda *a, **k: recorded.append(a)
        ):
            asyncio.run(sio_mod._async_remove_subscriptions("r10", "alice"))

        assert len(recorded) == 1
        assert "2 subscription(s)" in recorded[0][0]
        assert (
            sio_mod._subscription_key("alice", "r10")
            not in sio_mod._subscriptions
        )