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.

test: fix disconnect tests to use correct subscription schema (#2416)

LearningCircuit31241cd1b281
diff --git a/tests/web/services/test_socket_service.py b/tests/web/services/test_socket_service.py
index 84c86c7815..5aaf566463 100644
--- a/tests/web/services/test_socket_service.py
+++ b/tests/web/services/test_socket_service.py
@@ -585,7 +585,11 @@ class TestSocketServiceDisconnectCleanup:
@patch("local_deep_research.web.services.socket_service.SocketIO")
def test_disconnect_removes_subscriptions(self, mock_socketio_class):
- """Test that disconnect removes client from subscriptions dict."""
+ """Test that disconnect removes client from all research subscriptions.
+
+ Schema: __socket_subscriptions is research_id → set of sids.
+ Disconnect must discard the sid from every research_id's set.
+ """
from local_deep_research.web.services.socket_service import (
SocketIOService,
)
@@ -603,12 +607,10 @@ class TestSocketServiceDisconnectCleanup:
# Access the private subscriptions dict
subscriptions = service._SocketIOService__socket_subscriptions
- # Add a test subscription
+ # Set up correct schema: research_id → {sids}
test_sid = "test_client_123"
- subscriptions[test_sid] = {"research_1", "research_2"}
-
- # Verify it was added
- assert test_sid in subscriptions
+ subscriptions["research_1"] = {test_sid, "other_client"}
+ subscriptions["research_2"] = {test_sid}
# Create a mock request
mock_request = MagicMock()
@@ -619,8 +621,119 @@ class TestSocketServiceDisconnectCleanup:
mock_request, "test reason"
)
- # Verify subscription was removed
- assert test_sid not in subscriptions
+ # sid should be removed from research_1, leaving other_client
+ assert test_sid not in subscriptions.get("research_1", set())
+ assert "other_client" in subscriptions["research_1"]
+ # research_2 had only this sid, so the key should be cleaned up
+ assert "research_2" not in subscriptions
+ finally:
+ SocketIOService._instance = original_instance
+
+ @patch("local_deep_research.web.services.socket_service.SocketIO")
+ def test_disconnect_leaves_other_clients_intact(self, mock_socketio_class):
+ """Test that disconnect only removes the disconnecting client."""
+ from local_deep_research.web.services.socket_service import (
+ SocketIOService,
+ )
+
+ original_instance = SocketIOService._instance
+ SocketIOService._instance = None
+
+ mock_socketio = MagicMock()
+ mock_socketio_class.return_value = mock_socketio
+
+ try:
+ app = MockFlaskApp()
+ service = SocketIOService(app=app)
+
+ subscriptions = service._SocketIOService__socket_subscriptions
+ subscriptions["research_1"] = {"client_A", "client_B", "client_C"}
+
+ mock_request = MagicMock()
+ mock_request.sid = "client_B"
+
+ service._SocketIOService__handle_disconnect(
+ mock_request, "test reason"
+ )
+
+ assert subscriptions["research_1"] == {"client_A", "client_C"}
+ finally:
+ SocketIOService._instance = original_instance
+
+ @patch("local_deep_research.web.services.socket_service.SocketIO")
+ def test_disconnect_noop_when_sid_not_subscribed(self, mock_socketio_class):
+ """Test that disconnect is a no-op when sid has no subscriptions."""
+ from local_deep_research.web.services.socket_service import (
+ SocketIOService,
+ )
+
+ original_instance = SocketIOService._instance
+ SocketIOService._instance = None
+
+ mock_socketio = MagicMock()
+ mock_socketio_class.return_value = mock_socketio
+
+ try:
+ app = MockFlaskApp()
+ service = SocketIOService(app=app)
+
+ subscriptions = service._SocketIOService__socket_subscriptions
+ subscriptions["research_1"] = {"other_client_1", "other_client_2"}
+
+ mock_request = MagicMock()
+ mock_request.sid = "unknown_client"
+
+ service._SocketIOService__handle_disconnect(
+ mock_request, "test reason"
+ )
+
+ # Nothing should change
+ assert subscriptions["research_1"] == {
+ "other_client_1",
+ "other_client_2",
+ }
+ finally:
+ SocketIOService._instance = original_instance
+
+ @patch("local_deep_research.web.services.socket_service.SocketIO")
+ def test_subscribe_then_disconnect_round_trip(self, mock_socketio_class):
+ """Test full subscribe → disconnect cycle uses consistent schema."""
+ from local_deep_research.web.services.socket_service import (
+ SocketIOService,
+ )
+
+ original_instance = SocketIOService._instance
+ SocketIOService._instance = None
+
+ mock_socketio = MagicMock()
+ mock_socketio_class.return_value = mock_socketio
+
+ try:
+ app = MockFlaskApp()
+ service = SocketIOService(app=app)
+
+ mock_request = MagicMock()
+ mock_request.sid = "round_trip_client"
+
+ # Subscribe to two research IDs
+ service._SocketIOService__handle_subscribe(
+ {"research_id": "r1"}, mock_request
+ )
+ service._SocketIOService__handle_subscribe(
+ {"research_id": "r2"}, mock_request
+ )
+
+ subscriptions = service._SocketIOService__socket_subscriptions
+ assert "round_trip_client" in subscriptions["r1"]
+ assert "round_trip_client" in subscriptions["r2"]
+
+ # Disconnect should clean up both
+ service._SocketIOService__handle_disconnect(
+ mock_request, "test reason"
+ )
+
+ assert "round_trip_client" not in subscriptions.get("r1", set())
+ assert "round_trip_client" not in subscriptions.get("r2", set())
finally:
SocketIOService._instance = original_instance
diff --git a/tests/web_services/test_socket_service.py b/tests/web_services/test_socket_service.py
index 5f1f6ddec2..1691e0e513 100644
--- a/tests/web_services/test_socket_service.py
+++ b/tests/web_services/test_socket_service.py
@@ -441,12 +441,17 @@ class TestSocketIOServiceSubscriptionManagement:
def test_disconnect_removes_subscription(
self, service_with_mocks, mock_request
):
- """Test that disconnecting removes client subscription."""
+ """Test that disconnecting removes client from all research subscriptions.
+
+ Schema: __socket_subscriptions is research_id → set of sids.
+ Disconnect must discard the sid from every research_id's set.
+ """
svc, _ = service_with_mocks
- # Add a subscription first
+ # Set up correct schema: research_id → {sids}
svc._SocketIOService__socket_subscriptions = {
- mock_request.sid: {"research-1", "research-2"}
+ "research-1": {mock_request.sid, "other-sid"},
+ "research-2": {mock_request.sid},
}
svc._SocketIOService__handle_disconnect(
@@ -454,7 +459,11 @@ class TestSocketIOServiceSubscriptionManagement:
)
subscriptions = svc._SocketIOService__socket_subscriptions
- assert mock_request.sid not in subscriptions
+ # sid should be removed from research-1, leaving only other-sid
+ assert mock_request.sid not in subscriptions.get("research-1", set())
+ assert "other-sid" in subscriptions["research-1"]
+ # research-2 had only this sid, so the key should be cleaned up
+ assert "research-2" not in subscriptions
def test_disconnect_handles_no_subscription(
self, service_with_mocks, mock_request
@@ -470,6 +479,67 @@ class TestSocketIOServiceSubscriptionManagement:
mock_request, "client disconnect"
)
+ def test_disconnect_leaves_other_clients_intact(
+ self, service_with_mocks, mock_request
+ ):
+ """Test that disconnect only removes the disconnecting client."""
+ svc, _ = service_with_mocks
+
+ svc._SocketIOService__socket_subscriptions = {
+ "research-1": {mock_request.sid, "sid-A", "sid-B"},
+ }
+
+ svc._SocketIOService__handle_disconnect(
+ mock_request, "client disconnect"
+ )
+
+ subscriptions = svc._SocketIOService__socket_subscriptions
+ assert subscriptions["research-1"] == {"sid-A", "sid-B"}
+
+ def test_disconnect_noop_when_sid_not_subscribed(
+ self, service_with_mocks, mock_request
+ ):
+ """Test that disconnect is a no-op when sid has no subscriptions."""
+ svc, _ = service_with_mocks
+
+ svc._SocketIOService__socket_subscriptions = {
+ "research-1": {"other-sid-1", "other-sid-2"},
+ }
+
+ svc._SocketIOService__handle_disconnect(
+ mock_request, "client disconnect"
+ )
+
+ # Nothing should change
+ subscriptions = svc._SocketIOService__socket_subscriptions
+ assert subscriptions["research-1"] == {"other-sid-1", "other-sid-2"}
+
+ def test_subscribe_then_disconnect_round_trip(
+ self, service_with_mocks, mock_request
+ ):
+ """Test full subscribe → disconnect cycle uses consistent schema."""
+ svc, _ = service_with_mocks
+
+ # Subscribe to two research IDs
+ svc._SocketIOService__handle_subscribe(
+ {"research_id": "r1"}, mock_request
+ )
+ svc._SocketIOService__handle_subscribe(
+ {"research_id": "r2"}, mock_request
+ )
+
+ subscriptions = svc._SocketIOService__socket_subscriptions
+ assert mock_request.sid in subscriptions["r1"]
+ assert mock_request.sid in subscriptions["r2"]
+
+ # Disconnect should clean up both
+ svc._SocketIOService__handle_disconnect(
+ mock_request, "client disconnect"
+ )
+
+ assert mock_request.sid not in subscriptions.get("r1", set())
+ assert mock_request.sid not in subscriptions.get("r2", set())
+
class TestSocketIOServiceErrorHandling:
"""Tests for error handling methods.