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: add ~354 coverage tests across 5 new files (round 3) (#2900)

LearningCircuit0a9f2eb6bde9
diff --git a/tests/utilities/test_url_thread_coverage.py b/tests/utilities/test_url_thread_coverage.py
new file mode 100644
index 0000000000..d8c82ee7f7
--- /dev/null
+++ b/tests/utilities/test_url_thread_coverage.py
@@ -0,0 +1,635 @@
+"""
+Comprehensive coverage tests for url_utils.normalize_url and
+thread_context (set/get/clear, search_context manager, preserve_research_context).
+
+This file is intentionally self-contained: every scenario listed in the task
+specification is covered with distinct, well-named tests.
+"""
+
+import sys
+import threading
+from concurrent.futures import ThreadPoolExecutor
+from types import ModuleType
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from local_deep_research.utilities.url_utils import normalize_url
+from local_deep_research.utilities.thread_context import (
+ _g_thread_data,
+ clear_search_context,
+ get_search_context,
+ preserve_research_context,
+ search_context,
+ set_search_context,
+)
+
+
+# ===================================================================
+# PART 1 -- normalize_url
+# ===================================================================
+
+
+class TestNormalizeUrlLocalhostDefault:
+ """The canonical example: localhost:11434 -> http://localhost:11434."""
+
+ def test_localhost_with_port(self):
+ assert normalize_url("localhost:11434") == "http://localhost:11434"
+
+ def test_localhost_bare(self):
+ assert normalize_url("localhost") == "http://localhost"
+
+ def test_localhost_with_path_and_port(self):
+ assert (
+ normalize_url("localhost:11434/api/generate")
+ == "http://localhost:11434/api/generate"
+ )
+
+
+class TestNormalizeUrlMalformedScheme:
+ """http:example.com (missing //) should be repaired."""
+
+ def test_http_colon_no_slashes(self):
+ assert normalize_url("http:example.com") == "http://example.com"
+
+ def test_https_colon_no_slashes(self):
+ assert normalize_url("https:example.com") == "https://example.com"
+
+ def test_http_colon_with_port(self):
+ assert (
+ normalize_url("http:example.com:8080") == "http://example.com:8080"
+ )
+
+ def test_https_colon_with_path(self):
+ assert (
+ normalize_url("https:example.com/api/v1")
+ == "https://example.com/api/v1"
+ )
+
+ def test_http_colon_localhost(self):
+ assert normalize_url("http:localhost:11434") == "http://localhost:11434"
+
+
+class TestNormalizeUrlAlreadyCorrect:
+ """URLs with proper scheme are returned unchanged."""
+
+ def test_http_url_unchanged(self):
+ assert (
+ normalize_url("http://localhost:11434") == "http://localhost:11434"
+ )
+
+ def test_https_url_unchanged(self):
+ url = "https://api.openai.com/v1/chat/completions"
+ assert normalize_url(url) == url
+
+ def test_http_with_path_query_fragment(self):
+ url = "http://example.com/path?q=1#frag"
+ assert normalize_url(url) == url
+
+ def test_https_with_userinfo(self):
+ url = "https://user:pass@host:9200/path"
+ assert normalize_url(url) == url
+
+
+class TestNormalizeUrlIPv6:
+ """IPv6 addresses in brackets."""
+
+ def test_ipv6_loopback(self):
+ assert normalize_url("[::1]") == "http://[::1]"
+
+ def test_ipv6_loopback_with_port(self):
+ assert normalize_url("[::1]:8080") == "http://[::1]:8080"
+
+ def test_ipv6_link_local(self):
+ result = normalize_url("[fe80::1]:8080")
+ assert result == "http://[fe80::1]:8080"
+
+ def test_ipv6_private_fc00(self):
+ result = normalize_url("[fc00::1]:9200")
+ assert result == "http://[fc00::1]:9200"
+
+ def test_ipv6_public(self):
+ result = normalize_url("[2001:4860:4860::8888]")
+ assert result.startswith("https://")
+
+ def test_ipv6_already_has_scheme(self):
+ url = "http://[::1]:11434"
+ assert normalize_url(url) == url
+
+
+class TestNormalizeUrlPrivateIPs:
+ """Private IPs get http://."""
+
+ @pytest.mark.parametrize(
+ "ip",
+ [
+ "10.0.0.1",
+ "10.255.255.255",
+ "172.16.0.1",
+ "172.31.255.255",
+ "192.168.0.1",
+ "192.168.1.100",
+ ],
+ )
+ def test_rfc1918_gets_http(self, ip):
+ assert normalize_url(ip) == f"http://{ip}"
+
+ def test_private_ip_with_port(self):
+ assert (
+ normalize_url("192.168.1.100:8888") == "http://192.168.1.100:8888"
+ )
+
+ def test_zero_address(self):
+ assert normalize_url("0.0.0.0") == "http://0.0.0.0"
+
+ def test_127_0_0_1(self):
+ assert normalize_url("127.0.0.1") == "http://127.0.0.1"
+
+ def test_link_local_169_254(self):
+ assert normalize_url("169.254.0.1") == "http://169.254.0.1"
+
+
+class TestNormalizeUrlExternalHosts:
+ """External / public hosts get https://."""
+
+ @pytest.mark.parametrize(
+ "host",
+ [
+ "8.8.8.8",
+ "1.1.1.1",
+ "example.com",
+ "api.openai.com",
+ "sub.domain.example.org",
+ ],
+ )
+ def test_public_host_gets_https(self, host):
+ assert normalize_url(host) == f"https://{host}"
+
+ def test_public_ip_with_port(self):
+ assert normalize_url("8.8.8.8:53") == "https://8.8.8.8:53"
+
+
+class TestNormalizeUrlEmptyAndWhitespace:
+ """Empty string and whitespace edge cases."""
+
+ def test_empty_string_raises(self):
+ with pytest.raises(ValueError, match="(?i)empty"):
+ normalize_url("")
+
+ def test_whitespace_stripped_leading_trailing(self):
+ assert normalize_url(" https://example.com ") == "https://example.com"
+
+ def test_whitespace_around_bare_host(self):
+ assert normalize_url(" localhost:8080 ") == "http://localhost:8080"
+
+ def test_tab_stripped(self):
+ assert normalize_url("\thttps://example.com\t") == "https://example.com"
+
+ def test_whitespace_only_does_not_raise(self):
+ # " " is truthy so it passes the empty check, but strips to ""
+ # which falls through to the scheme-adding logic
+ result = normalize_url(" ")
+ assert isinstance(result, str)
+
+
+class TestNormalizeUrlDoubleSlash:
+ """Protocol-relative //hostname format."""
+
+ def test_double_slash_public(self):
+ assert normalize_url("//example.com") == "https://example.com"
+
+ def test_double_slash_public_with_port(self):
+ assert normalize_url("//example.com:443") == "https://example.com:443"
+
+ def test_double_slash_localhost(self):
+ assert normalize_url("//localhost:8080") == "http://localhost:8080"
+
+ def test_double_slash_private_ip(self):
+ assert normalize_url("//192.168.1.1:9200") == "http://192.168.1.1:9200"
+
+ def test_double_slash_with_path(self):
+ assert (
+ normalize_url("//example.com/api/v1")
+ == "https://example.com/api/v1"
+ )
+
+
+class TestNormalizeUrlMockedPrivateIp:
+ """Mock-based tests to force both branches of the private-ip decision."""
+
+ def test_mock_private_gives_http(self):
+ with patch(
+ "local_deep_research.utilities.url_utils.is_private_ip",
+ return_value=True,
+ ):
+ assert normalize_url("anyhost:1234") == "http://anyhost:1234"
+
+ def test_mock_public_gives_https(self):
+ with patch(
+ "local_deep_research.utilities.url_utils.is_private_ip",
+ return_value=False,
+ ):
+ assert normalize_url("anyhost:1234") == "https://anyhost:1234"
+
+ def test_ipv6_hostname_passed_with_brackets(self):
+ """Bracketed IPv6 passes bracket-inclusive hostname to is_private_ip."""
+ with patch(
+ "local_deep_research.utilities.url_utils.is_private_ip",
+ return_value=True,
+ ) as mock_fn:
+ normalize_url("[::1]:8080")
+ mock_fn.assert_called_once_with("[::1]")
+
+ def test_bare_hostname_extracted_before_colon(self):
+ with patch(
+ "local_deep_research.utilities.url_utils.is_private_ip",
+ return_value=False,
+ ) as mock_fn:
+ normalize_url("myhost:9999")
+ mock_fn.assert_called_once_with("myhost")
+
+ def test_bare_hostname_extracted_before_slash(self):
+ with patch(
+ "local_deep_research.utilities.url_utils.is_private_ip",
+ return_value=False,
+ ) as mock_fn:
+ normalize_url("myhost/some/path")
+ mock_fn.assert_called_once_with("myhost")
+
+
+# ===================================================================
+# PART 2 -- thread_context
+# ===================================================================
+
+
+class TestSetSearchContext:
+ """set_search_context sets context and overwrites previous."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def test_sets_context(self):
+ set_search_context({"research_id": "abc"})
+ assert _g_thread_data.context == {"research_id": "abc"}
+
+ def test_overwrites_previous(self):
+ set_search_context({"first": 1})
+ set_search_context({"second": 2})
+ assert _g_thread_data.context == {"second": 2}
+
+ def test_makes_defensive_copy(self):
+ original = {"key": "value"}
+ set_search_context(original)
+ original["key"] = "mutated"
+ assert _g_thread_data.context["key"] == "value"
+
+ def test_empty_dict_accepted(self):
+ set_search_context({})
+ assert _g_thread_data.context == {}
+
+
+class TestClearSearchContext:
+ """clear_search_context removes the context attribute entirely."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def test_clears_existing(self):
+ set_search_context({"x": 1})
+ clear_search_context()
+ assert not hasattr(_g_thread_data, "context")
+
+ def test_noop_when_nothing_set(self):
+ clear_search_context() # must not raise
+
+ def test_clear_then_set_again(self):
+ set_search_context({"a": 1})
+ clear_search_context()
+ set_search_context({"b": 2})
+ assert get_search_context() == {"b": 2}
+
+
+class TestGetSearchContext:
+ """get_search_context returns a copy, or None when unset."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def test_returns_none_when_not_set(self):
+ assert get_search_context() is None
+
+ def test_returns_copy_not_reference(self):
+ set_search_context({"key": "value"})
+ result = get_search_context()
+ result["key"] = "mutated"
+ assert _g_thread_data.context["key"] == "value"
+
+ def test_two_gets_are_independent_copies(self):
+ set_search_context({"k": "v"})
+ a = get_search_context()
+ b = get_search_context()
+ assert a == b
+ assert a is not b
+
+ def test_returns_correct_data(self):
+ set_search_context({"research_id": "r1", "extra": 42})
+ ctx = get_search_context()
+ assert ctx["research_id"] == "r1"
+ assert ctx["extra"] == 42
+
+
+class TestSearchContextManager:
+ """search_context context manager sets on entry and clears on exit."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def test_sets_inside_block(self):
+ with search_context({"research_id": "cm"}):
+ assert get_search_context() == {"research_id": "cm"}
+
+ def test_clears_after_block(self):
+ with search_context({"research_id": "cm2"}):
+ pass
+ assert get_search_context() is None
+
+ def test_clears_on_exception(self):
+ with pytest.raises(RuntimeError):
+ with search_context({"research_id": "err"}):
+ raise RuntimeError("boom")
+ assert get_search_context() is None
+
+ def test_copies_input_dict(self):
+ original = {"key": "val"}
+ with search_context(original):
+ original["key"] = "changed"
+ assert get_search_context()["key"] == "val"
+
+ def test_nested_inner_clears(self):
+ """Inner context manager clears on exit (no stack)."""
+ with search_context({"level": "outer"}):
+ with search_context({"level": "inner"}):
+ assert get_search_context()["level"] == "inner"
+ # Inner exited, context is now gone
+ assert get_search_context() is None
+
+
+class TestPreserveResearchContext:
+ """preserve_research_context decorator captures and restores context."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+ sys.modules.pop(
+ "local_deep_research.database.thread_local_session", None
+ )
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+ sys.modules.pop(
+ "local_deep_research.database.thread_local_session", None
+ )
+
+ def test_captures_context_at_decoration_time(self):
+ set_search_context({"research_id": "snap"})
+
+ @preserve_research_context
+ def worker():
+ return get_search_context()
+
+ # Clear the context on the main thread after decoration
+ clear_search_context()
+
+ # The decorator captured the snapshot; running in another thread
+ # should still see it.
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ result = pool.submit(worker).result()
+
+ assert result == {"research_id": "snap"}
+
+ def test_restores_and_clears_in_worker_thread(self):
+ set_search_context({"research_id": "restore"})
+
+ context_after = []
+
+ @preserve_research_context
+ def worker():
+ return "done"
+
+ def run_then_check():
+ worker()
+ context_after.append(get_search_context())
+
+ clear_search_context()
+
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ pool.submit(run_then_check).result()
+
+ # Context should have been cleared after the worker finished
+ assert context_after[0] is None
+
+ def test_no_context_runs_without_error(self):
+ """When no context was set at decoration time, runs normally."""
+ assert get_search_context() is None
+
+ @preserve_research_context
+ def worker():
+ return "ok"
+
+ assert worker() == "ok"
+
+ def test_preserves_function_name(self):
+ @preserve_research_context
+ def my_func():
+ """Docstring."""
+ pass
+
+ assert my_func.__name__ == "my_func"
+ assert my_func.__doc__ == "Docstring."
+
+ def test_passes_args_and_kwargs(self):
+ set_search_context({"research_id": "args"})
+
+ @preserve_research_context
+ def add(a, b, offset=0):
+ return a + b + offset
+
+ clear_search_context()
+
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ result = pool.submit(add, 3, 4, offset=10).result()
+
+ assert result == 17
+
+ def test_exception_propagates(self):
+ set_search_context({"research_id": "exc"})
+
+ @preserve_research_context
+ def boom():
+ raise ValueError("kaboom")
+
+ clear_search_context()
+
+ with pytest.raises(ValueError, match="kaboom"):
+ boom()
+
+ def test_clears_context_even_on_exception(self):
+ set_search_context({"research_id": "exc2"})
+
+ context_after = []
+
+ @preserve_research_context
+ def boom():
+ raise ValueError("kaboom")
+
+ clear_search_context()
+
+ def run_then_check():
+ try:
+ boom()
+ except ValueError:
+ pass
+ context_after.append(get_search_context())
+
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ pool.submit(run_then_check).result()
+
+ assert context_after[0] is None
+
+ def test_cleanup_current_thread_called(self):
+ """The finally block should call cleanup_current_thread."""
+ set_search_context({"research_id": "cleanup"})
+
+ @preserve_research_context
+ def worker():
+ return 42
+
+ clear_search_context()
+
+ mock_cleanup = MagicMock()
+ fake_module = ModuleType(
+ "local_deep_research.database.thread_local_session"
+ )
+ fake_module.cleanup_current_thread = mock_cleanup
+
+ with patch.dict(
+ sys.modules,
+ {
+ "local_deep_research.database.thread_local_session": (
+ fake_module
+ )
+ },
+ ):
+ result = worker()
+
+ assert result == 42
+ mock_cleanup.assert_called_once()
+
+ def test_cleanup_exception_is_suppressed(self):
+ """If cleanup_current_thread raises, the error is swallowed."""
+ set_search_context({"research_id": "suppress"})
+
+ @preserve_research_context
+ def worker():
+ return "fine"
+
+ clear_search_context()
+
+ fake_module = ModuleType(
+ "local_deep_research.database.thread_local_session"
+ )
+ fake_module.cleanup_current_thread = MagicMock(
+ side_effect=RuntimeError("db engine died")
+ )
+
+ with patch.dict(
+ sys.modules,
+ {
+ "local_deep_research.database.thread_local_session": (
+ fake_module
+ )
+ },
+ ):
+ result = worker()
+
+ assert result == "fine"
+
+ def test_cleanup_import_failure_suppressed(self):
+ """If thread_local_session module cannot be imported, no crash."""
+ set_search_context({"research_id": "noimport"})
+
+ @preserve_research_context
+ def worker():
+ return "still fine"
+
+ clear_search_context()
+
+ with patch.dict(
+ sys.modules,
+ {"local_deep_research.database.thread_local_session": None},
+ ):
+ assert worker() == "still fine"
+
+
+class TestThreadIsolation:
+ """Context is truly thread-local: other threads cannot see it."""
+
+ def setup_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def teardown_method(self):
+ if hasattr(_g_thread_data, "context"):
+ del _g_thread_data.context
+
+ def test_other_thread_sees_none(self):
+ set_search_context({"main": True})
+
+ result = []
+
+ def check():
+ result.append(get_search_context())
+
+ t = threading.Thread(target=check)
+ t.start()
+ t.join()
+
+ assert result[0] is None
+
+ def test_threads_have_independent_contexts(self):
+ results = {}
+
+ def worker(tid):
+ set_search_context({"tid": tid})
+ ctx = get_search_context()
+ results[tid] = ctx["tid"]
+
+ threads = [threading.Thread(target=worker, args=(i,)) for i in range(5)]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join()
+
+ for i in range(5):
+ assert results[i] == i
diff --git a/tests/web/queue/test_queue_manager_coverage.py b/tests/web/queue/test_queue_manager_coverage.py
new file mode 100644
index 0000000000..e88fe836f1
--- /dev/null
+++ b/tests/web/queue/test_queue_manager_coverage.py
@@ -0,0 +1,236 @@
+"""Coverage tests for web/queue/manager.py — QueueManager static methods."""
+
+from contextlib import contextmanager
+from datetime import datetime
+from unittest.mock import MagicMock, patch
+
+MODULE = "local_deep_research.web.queue.manager"
+
+
+def _fake_session_ctx(session=None):
+ if session is None:
+ session = MagicMock()
+
+ @contextmanager
+ def ctx(username=None):
+ yield session
+
+ return ctx
+
+
+def _make_queued(research_id="r1", position=1, query="q", mode="quick"):
+ q = MagicMock()
+ q.research_id = research_id
+ q.query = query
+ q.mode = mode
+ q.position = position
+ q.created_at = datetime(2025, 1, 1)
+ q.is_processing = False
+ return q
+
+
+def _make_research(id="r1"):
+ r = MagicMock()
+ r.id = id
+ return r
+
+
+class TestAddToQueue:
+ def test_success(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.scalar.return_value = 3
+
+ with (
+ patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
+ patch(f"{MODULE}.queue_processor"),
+ patch("local_deep_research.notifications.send_queue_notification"),
+ patch("local_deep_research.settings.SettingsManager"),
+ ):
+ pos = QueueManager.add_to_queue("user", "r1", "query", "quick", {})
+
+ assert pos == 4
+ ms.add.assert_called_once()
+ ms.commit.assert_called_once()
+
+ def test_first_item_empty_queue(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.scalar.return_value = None
+
+ with (
+ patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
+ patch(f"{MODULE}.queue_processor"),
+ patch("local_deep_research.notifications.send_queue_notification"),
+ patch("local_deep_research.settings.SettingsManager"),
+ ):
+ assert (
+ QueueManager.add_to_queue("user", "r1", "q", "quick", {}) == 1
+ )
+
+ def test_notification_exception_swallowed(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.scalar.return_value = 0
+
+ with (
+ patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
+ patch(f"{MODULE}.queue_processor"),
+ patch(
+ "local_deep_research.notifications.send_queue_notification",
+ side_effect=RuntimeError("notify fail"),
+ ),
+ patch("local_deep_research.settings.SettingsManager"),
+ ):
+ assert (
+ QueueManager.add_to_queue("user", "r1", "q", "quick", {}) == 1
+ )
+
+ def test_queue_processor_exception_swallowed(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.scalar.return_value = 0
+ mock_qp = MagicMock()
+ mock_qp.notify_research_queued.side_effect = RuntimeError("qp fail")
+
+ with (
+ patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
+ patch(f"{MODULE}.queue_processor", mock_qp),
+ patch("local_deep_research.notifications.send_queue_notification"),
+ patch("local_deep_research.settings.SettingsManager"),
+ ):
+ assert (
+ QueueManager.add_to_queue("user", "r1", "q", "quick", {}) == 1
+ )
+
+
+class TestGetQueuePosition:
+ def test_found(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(position=5)
+ ms = MagicMock()
+ query_mock = MagicMock()
+ ms.query.return_value = query_mock
+ query_mock.filter_by.return_value.first.return_value = queued
+ query_mock.filter.return_value.count.return_value = 2
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.get_queue_position("user", "r1") == 3
+
+ def test_not_found(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.first.return_value = None
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.get_queue_position("user", "nope") is None
+
+ def test_first_in_queue(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(position=1)
+ ms = MagicMock()
+ query_mock = MagicMock()
+ ms.query.return_value = query_mock
+ query_mock.filter_by.return_value.first.return_value = queued
+ query_mock.filter.return_value.count.return_value = 0
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.get_queue_position("user", "r1") == 1
+
+
+class TestRemoveFromQueue:
+ def test_found_and_removed(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(position=2)
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.first.return_value = queued
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.remove_from_queue("user", "r1") is True
+
+ ms.delete.assert_called_once_with(queued)
+ ms.commit.assert_called_once()
+
+ def test_not_found(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.first.return_value = None
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.remove_from_queue("user", "nope") is False
+
+
+class TestGetUserQueue:
+ def test_with_items(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(research_id="r1", position=1)
+ research = _make_research(id="r1")
+
+ ms = MagicMock()
+ query_mock = MagicMock()
+ ms.query.return_value = query_mock
+ query_mock.filter_by.return_value.order_by.return_value.all.return_value = [
+ queued
+ ]
+ query_mock.filter_by.return_value.first.return_value = research
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ result = QueueManager.get_user_queue("user")
+
+ assert len(result) == 1
+ assert result[0]["research_id"] == "r1"
+
+ def test_empty_queue(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ ms = MagicMock()
+ ms.query.return_value.filter_by.return_value.order_by.return_value.all.return_value = []
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.get_user_queue("user") == []
+
+ def test_missing_research_skipped(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(research_id="r1", position=1)
+
+ ms = MagicMock()
+ query_mock = MagicMock()
+ ms.query.return_value = query_mock
+ query_mock.filter_by.return_value.order_by.return_value.all.return_value = [
+ queued
+ ]
+ query_mock.filter_by.return_value.first.return_value = None
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ assert QueueManager.get_user_queue("user") == []
+
+ def test_item_without_created_at(self):
+ from local_deep_research.web.queue.manager import QueueManager
+
+ queued = _make_queued(research_id="r1", position=1)
+ queued.created_at = None
+ research = _make_research(id="r1")
+
+ ms = MagicMock()
+ query_mock = MagicMock()
+ ms.query.return_value = query_mock
+ query_mock.filter_by.return_value.order_by.return_value.all.return_value = [
+ queued
+ ]
+ query_mock.filter_by.return_value.first.return_value = research
+
+ with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
+ result = QueueManager.get_user_queue("user")
+
+ assert result[0]["created_at"] is None