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.

62.9 KB
Raw Blame
"""Port of ``origin/main:tests/web/services/test_research_service_coverage.py``.

That module was deleted by the Flask -> FastAPI migration. The module it
covered — ``web/services/research_service.py`` — survived the migration
essentially intact (68 insertions / 22 deletions, identical set of
top-level functions), so almost every assertion here is main's assertion
verbatim. Only the plumbing moved:

* ``local_deep_research.web.routes.globals`` -> ``local_deep_research.web.research_state``.
  ``run_research_process`` / ``cleanup_research_resources`` / ``cancel_research``
  each do ``from ..research_state import ...`` at call time, so the
  ``research_state`` module is the live patch target; the surviving
  ``routes.globals`` shim re-exported its names at import time and patching
  it no longer reaches the worker (see ``tests/web/services/helpers.py``).
* The Flask ``SocketIOService`` class is gone. ``research_service`` now emits
  through module-level ``_sio_emit`` / ``_sio_remove`` (aliases of
  ``socketio_asgi``) plus the ``_socket_emitter`` object-API adapter, so
  ``patch(f"{RS}.SocketIOService")`` becomes patches of those three names.
  They are ``create_autospec``'d, not bare mocks, so a signature drift (the
  required ``owner`` kwarg) fails loudly instead of being swallowed by the
  ``except`` around every emit call site.
* ``start_research_process`` no longer wraps the callback in
  ``thread_with_app_context`` / prepends ``thread_context()``; it copies
  contextvars instead. The two patches main needed for that are dropped.

Tests from the source file that an existing branch test already pins are
NOT re-ported; the coordinator's report lists each successor. What is here
is what nothing on the branch covers.
"""

import threading
from contextlib import contextmanager, ExitStack
from datetime import datetime, UTC
from unittest.mock import create_autospec, MagicMock, patch

import pytest

from loguru import logger

from local_deep_research.web.services import research_service

# Register MILESTONE log level used by progress_callback (normally done in
# log_utils.init_loguru, which tests do not run).
try:
    logger.level("MILESTONE")
except ValueError:
    logger.level("MILESTONE", no=26)

RS = "local_deep_research.web.services.research_service"
STATE = "local_deep_research.web.research_state"
QUEUE_PROC = "local_deep_research.web.queue.processor_v2.queue_processor"


# ---------------------------------------------------------------------------
# Local harness (kept in this module on purpose — see the porting brief)
# ---------------------------------------------------------------------------
def _fake_session_ctx(session):
    @contextmanager
    def ctx(username=None):
        yield session

    return ctx


def _get_raw_run_research_process():
    """Use the shared raw-worker wrapper that restores egress context."""
    from tests.web.services.helpers import (
        _get_raw_run_research_process as get_isolated_raw_worker,
    )

    return get_isolated_raw_worker()


def _make_research_mock(
    status="in_progress", research_meta=None, report_content=""
):
    r = MagicMock()
    r.status = status
    r.research_meta = research_meta if research_meta is not None else {}
    r.report_content = report_content
    r.created_at = datetime.now(UTC).isoformat()
    r.completed_at = None
    r.duration_seconds = None
    return r


def _socket_patches():
    """Replacement for main's single ``patch(f"{RS}.SocketIOService")``.

    autospec, not bare mocks: every emit call site in research_service sits
    inside a swallowing ``except``, so a mock that accepts any signature
    hides a real break (this is exactly how the required ``owner`` kwarg
    regressed unnoticed once already).
    """
    return [
        patch(
            f"{RS}._sio_emit",
            create_autospec(research_service._sio_emit, spec_set=True),
        ),
        patch(
            f"{RS}._sio_remove",
            create_autospec(research_service._sio_remove, spec_set=True),
        ),
        patch(
            f"{RS}._socket_emitter",
            create_autospec(research_service._socket_emitter, spec_set=True),
        ),
    ]


def _state_patches(terminated=False, active=True, progress=(50, True)):
    """main patched ``web.routes.globals``; the worker binds
    ``web.research_state``."""
    return [
        patch(f"{STATE}.is_termination_requested", return_value=terminated),
        patch(f"{STATE}.is_research_active", return_value=active),
        patch(
            f"{STATE}.update_progress_and_check_active", return_value=progress
        ),
    ]


# ---------------------------------------------------------------------------
# save_research_strategy / get_research_strategy — exception paths
#
# The happy paths (create, update, found, not-found) are superseded by
# tests/security/test_research_service_isolation_fastapi.py::
# TestStrategyUsernameBarrier::test_save_and_get_use_the_callers_own_database,
# which does the same round trip against real per-user databases. Nothing
# on the branch covers the swallow-and-continue behaviour below.
# ---------------------------------------------------------------------------
class TestResearchStrategyExceptionPaths:
    """Ported from ``TestSaveResearchStrategy::test_exception_handled`` and
    ``TestGetResearchStrategy::test_exception_returns_none``.

    If the try/except around the session were removed, a transient DB error
    while recording the chosen strategy would kill the research worker
    thread (save) or blow up the history/status readers (get) instead of
    degrading to "strategy unknown".
    """

    @patch(f"{RS}.get_user_db_session")
    def test_save_swallows_db_errors(self, mock_sess):
        mock_sess.side_effect = RuntimeError("fail")
        from local_deep_research.web.services.research_service import (
            save_research_strategy,
        )

        save_research_strategy(1, "x", username="u")  # should not raise

    @patch(f"{RS}.get_user_db_session")
    def test_get_returns_none_on_db_error(self, mock_sess):
        mock_sess.side_effect = RuntimeError("fail")
        from local_deep_research.web.services.research_service import (
            get_research_strategy,
        )

        assert get_research_strategy(1, username="u") is None


# ---------------------------------------------------------------------------
# start_research_process
# ---------------------------------------------------------------------------
class TestStartResearchProcessRegistration:
    """Ported from ``TestStartResearchProcess::test_starts_thread_and_registers``.

    ``tests/web/services/test_research_service_start_process.py::
    test_success_registers_thread_and_returns_it`` already pins the thread
    object, the daemon flag and ``data["thread"]`` / ``data["settings"]``.
    What it does NOT pin, and what is re-ported here, is the initial
    ``progress``/``status`` the registration entry carries: the research
    detail page and ``/api/research/{id}/status`` read those straight back
    out of the shared registry, so a registration that omitted them would
    show a freshly started research as having no progress and no state.
    """

    @patch(f"{RS}._global_research_semaphore")
    def test_registers_initial_progress_and_status(self, mock_sem):
        with patch(
            f"{STATE}.check_and_start_research",
            return_value=True,
        ) as mock_check_start:
            from local_deep_research.web.services.research_service import (
                start_research_process,
            )

            callback = MagicMock()
            thread = start_research_process(
                research_id="r1",
                query="test",
                mode="quick",
                run_research_callback=callback,
            )
            assert isinstance(thread, threading.Thread)
            assert thread.daemon is True
            mock_check_start.assert_called_once()
            args = mock_check_start.call_args
            assert args[0][0] == "r1"
            info = args[0][1]
            assert info["progress"] == 0
            assert info["status"] == "in_progress"


# ---------------------------------------------------------------------------
# cleanup_research_resources
# ---------------------------------------------------------------------------
class TestCleanupResearchResources:
    """Ported from ``TestCleanupResearchResources``.

    Nothing on the branch asserts what ``cleanup_research_resources``
    actually does: it must tell the queue processor the run finished (so the
    user's active-research slot is released and the row's terminal status is
    written from the main thread) and drop the id from the shared registry.
    If either call were lost the run would look "still running" forever and
    the user's concurrency slot would leak.
    """

    @patch(f"{RS}._last_emit_lock", threading.Lock())
    @patch(f"{RS}._last_emit_times", {"r1": 1.0})
    @patch(f"{STATE}.cleanup_research")
    @patch(
        "local_deep_research.settings.env_registry.is_test_mode",
        return_value=False,
    )
    def test_cleanup_notifies_and_emits(self, mock_test_mode, mock_cleanup):
        mock_qp = MagicMock()
        with ExitStack() as stack:
            for cm in _socket_patches():
                stack.enter_context(cm)
            stack.enter_context(patch(QUEUE_PROC, mock_qp))
            from local_deep_research.web.services.research_service import (
                cleanup_research_resources,
            )

            cleanup_research_resources("r1", username="alice")

        mock_qp.notify_research_completed.assert_called_once_with(
            "alice", "r1", user_password=None
        )
        mock_cleanup.assert_called_once_with("r1")

    @patch(f"{RS}._last_emit_lock", threading.Lock())
    @patch(f"{RS}._last_emit_times", {})
    @patch(f"{STATE}.cleanup_research")
    @patch(
        "local_deep_research.settings.env_registry.is_test_mode",
        return_value=False,
    )
    def test_cleanup_without_username(self, mock_test_mode, mock_cleanup):
        """No username -> no queue notification (there is no database to
        open); the cleanup must still not raise."""
        mock_qp = MagicMock()
        with ExitStack() as stack:
            for cm in _socket_patches():
                stack.enter_context(cm)
            stack.enter_context(patch(QUEUE_PROC, mock_qp))
            from local_deep_research.web.services.research_service import (
                cleanup_research_resources,
            )

            cleanup_research_resources("r2", username=None)

        mock_qp.notify_research_completed.assert_not_called()

    @patch(f"{RS}._last_emit_lock", threading.Lock())
    @patch(f"{RS}._last_emit_times", {})
    @patch(f"{STATE}.cleanup_research")
    @patch(
        "local_deep_research.settings.env_registry.is_test_mode",
        return_value=False,
    )
    def test_cleanup_socket_error_handled(self, mock_test_mode, mock_cleanup):
        """Socket errors in cleanup should not raise.

        main raised from ``SocketIOService().emit_to_subscribers``; the
        FastAPI equivalent is the module-level ``_sio_emit`` alias.
        """
        failing_emit = create_autospec(
            research_service._sio_emit, spec_set=True
        )
        failing_emit.side_effect = RuntimeError("socket fail")
        mock_qp = MagicMock()
        with (
            patch(f"{RS}._sio_emit", failing_emit),
            patch(
                f"{RS}._sio_remove",
                create_autospec(research_service._sio_remove, spec_set=True),
            ),
            patch(QUEUE_PROC, mock_qp),
        ):
            from local_deep_research.web.services.research_service import (
                cleanup_research_resources,
            )

            # Should not raise
            cleanup_research_resources("r3", username="u")


# ---------------------------------------------------------------------------
# handle_termination
# ---------------------------------------------------------------------------
class TestHandleTermination:
    """Ported from ``TestHandleTermination``.

    ``handle_termination`` is the only thing standing between "user pressed
    Stop" and a row that stays ``in_progress`` forever: it pushes a
    ``suspended`` error-update into the queue processor and then cleans up
    with ``final_status="suspended"`` (so the final socket frame does not
    claim "completed"). The branch has an HTTP-level test that the row ends
    up suspended after a queue drain
    (``tests/web/test_research_lifecycle_states.py``), but nothing pins the
    ``final_status`` argument or the swallow-and-still-clean-up behaviour.
    """

    @patch(f"{RS}.cleanup_research_resources")
    def test_queues_suspension_update(self, mock_cleanup):
        mock_qp = MagicMock()
        with patch(QUEUE_PROC, mock_qp):
            from local_deep_research.web.services.research_service import (
                handle_termination,
            )

            handle_termination("r1", username="alice")

        mock_qp.queue_error_update.assert_called_once()
        kwargs = mock_qp.queue_error_update.call_args[1]
        assert kwargs["username"] == "alice"
        assert kwargs["research_id"] == "r1"
        assert kwargs["status"] == "suspended"
        mock_cleanup.assert_called_once_with(
            "r1", "alice", final_status="suspended"
        )

    @patch(f"{RS}.cleanup_research_resources")
    def test_exception_in_queue_handled(self, mock_cleanup):
        """A queue failure must not skip the resource cleanup."""
        mock_qp = MagicMock()
        mock_qp.queue_error_update.side_effect = RuntimeError("fail")
        with patch(QUEUE_PROC, mock_qp):
            from local_deep_research.web.services.research_service import (
                handle_termination,
            )

            # Should not raise
            handle_termination("r1", username="u")
        mock_cleanup.assert_called_once()


# ---------------------------------------------------------------------------
# cancel_research
# ---------------------------------------------------------------------------
class TestCancelResearch:
    """Ported from ``TestCancelResearch``.

    The cross-user isolation half is superseded by
    ``tests/security/test_research_service_isolation_fastapi.py::
    TestCancelResearchIsolation``. Re-ported here: the active-research
    branch (flag + handle_termination), the "already terminal -> True but
    do NOT re-terminate" branch, and the two failure branches, none of
    which any branch test asserts.
    """

    @patch(f"{RS}.handle_termination")
    def test_active_research_cancelled(self, mock_handle):
        with (
            # Ownership gate reads the caller's DB first; a mock session
            # makes the ownership query return a row so the owner is
            # authorized.
            patch(f"{RS}.get_user_db_session", _fake_session_ctx(MagicMock())),
            patch(f"{STATE}.set_termination_flag") as mock_flag,
            patch(f"{STATE}.is_research_active", return_value=True),
        ):
            from local_deep_research.web.services.research_service import (
                cancel_research,
            )

            result = cancel_research("r1", "alice")
        assert result is True
        mock_flag.assert_called_once_with("r1")
        mock_handle.assert_called_once_with(
            "r1", "alice", preserve_termination_flag=True
        )

    @patch(f"{RS}.get_user_db_session")
    @patch(f"{RS}.handle_termination")
    def test_inactive_completed_research(self, mock_handle, mock_sess):
        """Already in a terminal state -> report success WITHOUT running the
        termination path again (which would re-queue a suspension update for
        a research that already finished)."""
        session = MagicMock()
        research = MagicMock()
        research.status = "completed"
        session.query.return_value.filter_by.return_value.first.return_value = (
            research
        )
        mock_sess.side_effect = _fake_session_ctx(session)

        with (
            patch(f"{STATE}.set_termination_flag"),
            patch(f"{STATE}.is_research_active", return_value=False),
        ):
            from local_deep_research.web.services.research_service import (
                cancel_research,
            )

            result = cancel_research("r1", "alice")
        assert result is True
        mock_handle.assert_not_called()

    @patch(f"{RS}.get_user_db_session")
    @patch(f"{RS}.handle_termination")
    def test_db_exception_returns_false(self, mock_handle, mock_sess):
        mock_sess.side_effect = RuntimeError("db fail")

        with (
            patch(f"{STATE}.set_termination_flag"),
            patch(f"{STATE}.is_research_active", return_value=False),
        ):
            from local_deep_research.web.services.research_service import (
                cancel_research,
            )

            result = cancel_research("r1", "alice")
        assert result is False

    def test_outer_exception_returns_false(self):
        """The outermost try/except: any unexpected failure reports False
        rather than propagating a 500 out of the terminate endpoint."""
        with (
            patch(f"{RS}.get_user_db_session", _fake_session_ctx(MagicMock())),
            patch(
                f"{STATE}.set_termination_flag",
                side_effect=RuntimeError("boom"),
            ),
        ):
            from local_deep_research.web.services.research_service import (
                cancel_research,
            )

            result = cancel_research("r1", "alice")
        assert result is False


# ---------------------------------------------------------------------------
# run_research_process — termination before start
# ---------------------------------------------------------------------------
class TestRunResearchProcessTerminatedBeforeStart:
    """Ported from ``TestRunResearchProcessTerminatedBeforeStart``.

    A research that was already cancelled before its worker thread got
    scheduled must report SUSPENDED. If ``final_status`` were dropped here
    the cleanup would default to COMPLETED and the UI would show a research
    the user cancelled as having finished successfully.
    """

    @patch(f"{RS}.cleanup_research_resources")
    def test_terminated_early(self, mock_cleanup):
        with ExitStack() as stack:
            for cm in _state_patches(terminated=True, progress=(0, True)):
                stack.enter_context(cm)
            raw_fn = _get_raw_run_research_process()
            raw_fn("r1", "query", "quick", username="alice")
        mock_cleanup.assert_called_once_with(
            "r1", "alice", user_password=None, final_status="suspended"
        )


# ---------------------------------------------------------------------------
# run_research_process — quick mode
# ---------------------------------------------------------------------------
class TestRunResearchProcessQuickMode:
    """Ported from ``TestRunResearchProcessQuickMode``.

    The synthesis/fallback cases from this class are superseded by
    ``test_research_service_synthesis.py::TestQuickModeSynthesis`` (which
    drives the same code through the shared harness and asserts the actual
    persisted markdown rather than only ``status == "completed"``). Kept
    here: the storage-abstraction assertion, the subscription-retry guard,
    and the all-error-findings emergency fallback, none of which those
    successors reach.
    """

    def _run_quick(
        self, results, search_error=None, research_meta=None, **extra_kwargs
    ):
        mock_session = MagicMock()
        research = _make_research_mock(research_meta=research_meta)
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        if search_error:
            mock_system.analyze_topic.side_effect = search_error
        else:
            mock_system.analyze_topic.return_value = results

        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted content"

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 3

        mock_qp = MagicMock()

        kwargs = dict(
            username="alice",
            settings_snapshot={"search.tool": "searxng"},
            **extra_kwargs,
        )

        with ExitStack() as stack:
            for cm in _state_patches():
                stack.enter_context(cm)
            for cm in _socket_patches():
                stack.enter_context(cm)
            for cm in (
                patch(f"{RS}.get_llm", return_value=MagicMock()),
                patch(f"{RS}.get_search", return_value=MagicMock()),
                patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
                patch(
                    f"{RS}.get_citation_formatter", return_value=mock_formatter
                ),
                patch(
                    f"{RS}.get_user_db_session",
                    side_effect=_fake_session_ctx(mock_session),
                ),
                patch(f"{RS}.cleanup_research_resources"),
                patch(f"{RS}.set_search_context"),
                patch(f"{RS}.calculate_duration", return_value=10.0),
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                ),
                patch(
                    f"{RS}.extract_links_from_search_results",
                    return_value=[{"url": "http://example.com", "title": "Ex"}],
                ),
                patch(
                    "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
                    return_value=mock_sources_service,
                ),
                patch(QUEUE_PROC, mock_qp),
                patch("local_deep_research.settings.logger.log_settings"),
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                ),
            ):
                stack.enter_context(cm)

            raw_fn = _get_raw_run_research_process()
            raw_fn("r1", "test query", "quick", **kwargs)

        return research, mock_storage, mock_system

    def test_quick_mode_success(self):
        """A completed quick run persists through the storage abstraction.

        ``get_report_storage().save_report`` is the only path that honours
        ``report.enable_file_backup``; a raw ORM write would still mark the
        row completed and pass every synthesis test, but silently lose the
        user's file backups.
        """
        results = {
            "findings": [
                {
                    "phase": "search",
                    "content": "found stuff",
                    "search_results": [{"url": "http://a.com"}],
                }
            ],
            "formatted_findings": "# Summary\nGreat results",
            "iterations": 3,
        }
        research, mock_storage, _ = self._run_quick(
            results, model="test-model", search_engine="google"
        )
        assert research.status == "completed"
        mock_storage.save_report.assert_called_once()

    def test_failed_subscription_run_is_reset_to_due(self):
        """A FAILED subscription-triggered run resets next_refresh to due.

        Integration guard for the headline fix #1: the failure handler in
        run_research_process must call mark_subscription_due_by_id so a
        failed run is retried by the scheduler instead of being hidden a
        full interval.
        """
        with patch(
            "local_deep_research.news.subscription_runner.mark_subscription_due_by_id",
            return_value=True,
        ) as mock_reset:
            self._run_quick(
                results=None,
                search_error=RuntimeError("provider unavailable"),
                research_meta={
                    "subscription_id": "sub_x",
                    "is_news_search": True,
                },
            )

        mock_reset.assert_called_once()
        # called as mark_subscription_due_by_id(db_session, subscription_id)
        assert mock_reset.call_args.args[1] == "sub_x"

    def test_failed_non_subscription_run_does_not_reset(self):
        """A FAILED run that is NOT subscription-triggered never calls the
        reset (the guard must require a subscription_id)."""
        with patch(
            "local_deep_research.news.subscription_runner.mark_subscription_due_by_id",
            return_value=True,
        ) as mock_reset:
            self._run_quick(
                results=None,
                search_error=RuntimeError("boom"),
                research_meta={},  # no subscription_id
            )

        mock_reset.assert_not_called()

    def test_quick_mode_error_no_valid_fallback_all_errors(self):
        """All findings have error content -> emergency fallback.

        Distinct from ``test_quick_mode_synthesis_all_fallbacks_exhausted``
        (findings=[] -> the run is reported FAILED): here there ARE findings,
        they are merely all error-shaped, and the run must still land on a
        completed report rather than being abandoned.
        """
        results = {
            "findings": [
                {"phase": "s1", "content": "Error: fail1"},
                {"phase": "s2", "content": "Error: fail2"},
            ],
            "formatted_findings": "Error: synthesis failed",
            "iterations": 1,
        }
        research, _, _ = self._run_quick(results)
        assert research.status == "completed"


# ---------------------------------------------------------------------------
# run_research_process — no on-disk output directory
# ---------------------------------------------------------------------------
class TestRunResearchProcessNoOutputDir:
    """Ported from ``TestRunResearchProcessNoOutputDir``.

    Regression: run_research_process must not create an empty
    ``research_outputs/research_<id>`` directory on disk. Reports are
    persisted via ``storage.save_report`` into the encrypted per-user
    database; the on-disk output directory is not used by the
    encrypted-database version, so the worker must not create it.
    """

    def test_no_research_output_dir_created(self, tmp_path):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_system.analyze_topic.return_value = {
            "findings": [],
            "formatted_findings": "# Summary",
            "iterations": 1,
        }

        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted content"

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 0

        with ExitStack() as stack:
            # Point the module's output directory at an empty tmp_path so we
            # can assert nothing gets written under it.
            stack.enter_context(patch(f"{RS}.OUTPUT_DIR", tmp_path))
            for cm in _state_patches():
                stack.enter_context(cm)
            for cm in _socket_patches():
                stack.enter_context(cm)
            for cm in (
                patch(f"{RS}.get_llm", return_value=MagicMock()),
                patch(f"{RS}.get_search", return_value=MagicMock()),
                patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
                patch(
                    f"{RS}.get_citation_formatter", return_value=mock_formatter
                ),
                patch(
                    f"{RS}.get_user_db_session",
                    side_effect=_fake_session_ctx(mock_session),
                ),
                patch(f"{RS}.cleanup_research_resources"),
                patch(f"{RS}.set_search_context"),
                patch(f"{RS}.calculate_duration", return_value=10.0),
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                ),
                patch(
                    f"{RS}.extract_links_from_search_results", return_value=[]
                ),
                patch(
                    "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
                    return_value=mock_sources_service,
                ),
                patch(QUEUE_PROC, MagicMock()),
                patch("local_deep_research.settings.logger.log_settings"),
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                ),
            ):
                stack.enter_context(cm)

            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r_no_dir",
                "test query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
            )

        assert research.status == "completed"
        # The per-research output directory must never be created.
        assert not (tmp_path / "research_r_no_dir").exists()
        assert list(tmp_path.iterdir()) == []


# ---------------------------------------------------------------------------
# run_research_process — detailed mode
# ---------------------------------------------------------------------------
def _detailed_patches(
    mock_session,
    mock_system,
    mock_formatter,
    mock_report_gen,
    mock_sources_service,
    mock_qp,
):
    """Shared patch list for the detailed-mode ports below.

    Built as a list fed through ``ExitStack`` rather than a literal nested
    ``with (...)``: several of these tests need extra patches on top, which
    pushes a statically nested ``with`` past CPython's block limit (main hit
    the same wall and used ExitStack for one of them).
    """
    return [
        *_state_patches(),
        *_socket_patches(),
        patch(f"{RS}.get_llm", return_value=MagicMock()),
        patch(f"{RS}.get_search", return_value=MagicMock()),
        patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
        patch(f"{RS}.get_citation_formatter", return_value=mock_formatter),
        patch(
            f"{RS}.get_user_db_session",
            side_effect=_fake_session_ctx(mock_session),
        ),
        patch(f"{RS}.cleanup_research_resources"),
        patch(f"{RS}.set_search_context"),
        patch(f"{RS}.calculate_duration", return_value=20.0),
        patch(f"{RS}.IntegratedReportGenerator", return_value=mock_report_gen),
        patch(f"{RS}.extract_links_from_search_results", return_value=[]),
        patch(
            "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
            return_value=mock_sources_service,
        ),
        patch(QUEUE_PROC, mock_qp),
        patch("local_deep_research.settings.logger.log_settings"),
        patch(
            "local_deep_research.config.thread_settings.set_settings_context"
        ),
    ]


def _detailed_results(mock_search_system):
    return {
        "findings": [{"content": "data"}],
        "formatted_findings": "# Report",
        "iterations": 5,
        "search_system": mock_search_system,
    }


class TestRunResearchProcessDetailedMode:
    """Ported from ``TestRunResearchProcessDetailedMode`` (the two sentinel
    tests in that class are already ported on the branch as
    ``test_research_service_citation_sentinel.py`` and are not repeated)."""

    def test_detailed_mode_success(self):
        """Detailed completion saves the report through the storage
        abstraction (get_report_storage), exactly like the quick path.

        Guards the H2 fix: the detailed path previously did a raw ORM write
        of report_content that bypassed get_report_storage and therefore the
        report.enable_file_backup feature. If that regresses, save_report
        stops being called and this fails.
        """
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_search_system = MagicMock()
        mock_search_system.all_links_of_system = [
            {"url": "http://a.com", "title": "A"}
        ]
        mock_system.analyze_topic.return_value = _detailed_results(
            mock_search_system
        )

        mock_formatter = MagicMock()
        # Exercise the normal citation path: format_document_split returns
        # the answer body plus non-empty sources, so apply_inline_hyperlinks
        # is skipped and the over-strip safety check is not triggered for
        # this short content. format_document_split returns
        # (answer, sources, on_sentinel).
        mock_formatter.format_document_split.return_value = (
            "# Full Report",
            [{"url": "u"}],
            False,
        )

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": "# Full Report",
            "metadata": {"sections": 3},
        }

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 1
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _detailed_patches(
                mock_session,
                mock_system,
                mock_formatter,
                mock_report_gen,
                mock_sources_service,
                mock_qp,
            ):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

        assert research.status == "completed"
        # Detailed routes report-save through the storage abstraction (the
        # H2 fix), passing the DETAILED report content.
        mock_storage.save_report.assert_called_once()
        assert (
            mock_storage.save_report.call_args.kwargs["content"]
            == "# Full Report"
        )

    def test_detailed_mode_news_search_generates_headlines(self):
        """Detailed-mode news search metadata should trigger headline/topic
        generation with a settings_snapshot scoped to the run's owner.

        The quick-mode half of this is superseded on the branch by
        ``tests/security/test_research_service_isolation_fastapi.py::
        TestNewsSnapshotOwnerScoping``, but that harness only ever runs
        ``mode="quick"``; the detailed branch reaches the generators from a
        different call site. Without the scoped snapshot the LLM PEP
        silently no-ops for cloud-provider users instead of resolving a
        per-user provider.
        """
        mock_session = MagicMock()
        research = _make_research_mock(
            research_meta={"is_news_search": True, "category": "Tech"}
        )
        research.report_content = "some report"
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_search_system = MagicMock()
        mock_search_system.all_links_of_system = [
            {"url": "http://a.com", "title": "A"}
        ]
        results = _detailed_results(mock_search_system)
        results["findings"] = [{"content": "news data"}]
        results["iterations"] = 2
        mock_system.analyze_topic.return_value = results

        mock_formatter = MagicMock()
        mock_formatter.format_document_split.return_value = (
            "# Full Report",
            [{"url": "u"}],
            False,
        )

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": "# Full Report",
            "metadata": {"sections": 3},
        }

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 1
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _detailed_patches(
                mock_session,
                mock_system,
                mock_formatter,
                mock_report_gen,
                mock_sources_service,
                mock_qp,
            ):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                )
            )
            mock_headline = stack.enter_context(
                patch(
                    "local_deep_research.news.utils.headline_generator.generate_headline",
                    return_value="Breaking: Test",
                )
            )
            mock_topics = stack.enter_context(
                patch(
                    "local_deep_research.news.utils.topic_generator.generate_topics",
                    return_value=["topic1", "topic2"],
                )
            )

            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="searxng",
            )

        assert research.status == "completed"

        headline_snapshot = mock_headline.call_args.kwargs["settings_snapshot"]
        assert headline_snapshot["_username"] == "alice"
        assert headline_snapshot["search.tool"] == "searxng"

        topics_snapshot = mock_topics.call_args.kwargs["settings_snapshot"]
        assert topics_snapshot["_username"] == "alice"
        assert topics_snapshot["search.tool"] == "searxng"

    def test_detailed_mode_writes_file_backup_when_enabled(self):
        """Detailed completion writes the on-disk file backup when the
        report.enable_file_backup setting is on.

        The user-visible half of the H2 fix: because the detailed path now
        routes through get_report_storage -> DatabaseWithFileBackupStorage, a
        user who enabled file backup gets files for detailed research, not
        only quick research. On the old raw ORM write get_report_storage was
        never called for detailed, so FileReportStorage was never invoked and
        this would fail.
        """
        from local_deep_research.storage.database_with_file_backup import (
            DatabaseWithFileBackupStorage,
        )

        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_search_system = MagicMock()
        mock_search_system.all_links_of_system = [
            {"url": "http://a.com", "title": "A"}
        ]
        mock_system.analyze_topic.return_value = _detailed_results(
            mock_search_system
        )

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": "# Full Report",
            "metadata": {"sections": 3},
        }

        # Exercise the normal citation path (see test_detailed_mode_success)
        # rather than the formatter's exception fallback.
        mock_formatter = MagicMock()
        mock_formatter.format_document_split.return_value = (
            "# Full Report",
            [{"url": "u"}],
            False,
        )

        mock_file_storage = MagicMock()
        mock_file_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 1
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _detailed_patches(
                mock_session,
                mock_system,
                mock_formatter,
                mock_report_gen,
                mock_sources_service,
                mock_qp,
            ):
                stack.enter_context(cm)
            # The on-disk writer is mocked so the test touches no filesystem.
            stack.enter_context(
                patch(
                    "local_deep_research.storage.database_with_file_backup.FileReportStorage",
                    return_value=mock_file_storage,
                )
            )
            # Construct the real backup-aware storage lazily (at call time,
            # so the FileReportStorage patch above is active) with backup
            # ENABLED.
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    side_effect=lambda *a, **kw: DatabaseWithFileBackupStorage(
                        session=mock_session, enable_file_storage=True
                    ),
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

        # The file backup was written exactly once, with the detailed report
        # content (positional: research_id, content, metadata, username).
        mock_file_storage.save_report.assert_called_once()
        assert (
            mock_file_storage.save_report.call_args.args[1] == "# Full Report"
        )

    def test_detailed_mode_report_not_found_raises(self):
        """The research row disappearing between the analysis and the report
        save must land in the error handler (which queues an error update),
        not escape the worker thread."""
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.side_effect = [
            research,  # first query in report save
            None,  # research not found
        ]

        mock_system = MagicMock()
        mock_search_system = MagicMock()
        mock_search_system.all_links_of_system = []
        results = _detailed_results(mock_search_system)
        results["iterations"] = 2
        mock_system.analyze_topic.return_value = results

        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": "# Full Report",
            "metadata": {"sections": 1},
        }
        mock_sources_service = MagicMock()
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _detailed_patches(
                mock_session,
                mock_system,
                mock_formatter,
                mock_report_gen,
                mock_sources_service,
                mock_qp,
            ):
                stack.enter_context(cm)
            raw_fn = _get_raw_run_research_process()
            # This should handle the error gracefully (error handler catches
            # it).
            raw_fn(
                "r1",
                "query",
                "detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )
        # Error handler should queue error update
        mock_qp.queue_error_update.assert_called()


# ---------------------------------------------------------------------------
# run_research_process — misc branches
# ---------------------------------------------------------------------------
def _quick_run_patches(
    mock_session, mock_system, mock_formatter, mock_storage, mock_qp
):
    return [
        *_state_patches(),
        *_socket_patches(),
        patch(f"{RS}.get_llm", return_value=MagicMock()),
        patch(f"{RS}.get_search", return_value=MagicMock()),
        patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
        patch(f"{RS}.get_citation_formatter", return_value=mock_formatter),
        patch(
            f"{RS}.get_user_db_session",
            side_effect=_fake_session_ctx(mock_session),
        ),
        patch(f"{RS}.cleanup_research_resources"),
        patch(f"{RS}.set_search_context"),
        patch(f"{RS}.calculate_duration", return_value=5.0),
        patch(
            "local_deep_research.storage.get_report_storage",
            return_value=mock_storage,
        ),
        patch(f"{RS}.extract_links_from_search_results", return_value=[]),
        patch(QUEUE_PROC, mock_qp),
        patch("local_deep_research.settings.logger.log_settings"),
    ]


_SIMPLE_RESULTS = {
    "findings": [{"phase": "s", "content": "data"}],
    "formatted_findings": "# Results",
    "iterations": 1,
}


class TestRunResearchProcessResearchContext:
    """Ported from ``TestRunResearchProcessResearchContext``.

    A follow-up run carries a ``research_context`` kwarg holding the parent
    run's findings. That blob is spliced into the prompt/context handling;
    a run that chokes on it would fail every follow-up while ordinary runs
    stayed green.
    """

    @pytest.mark.parametrize(
        "research_context",
        [
            {"past_findings": "previous research data" * 100},
            # Read back from JSON, the context and its findings may be of
            # any JSON type.
            {"past_findings": 42},
            ["not", "a", "dict"],
        ],
        ids=["findings", "numeric-findings", "non-dict-context"],
    )
    def test_follow_up_research_context(self, research_context):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_system.analyze_topic.return_value = {
            "findings": [{"phase": "search", "content": "data"}],
            "formatted_findings": "# Results",
            "iterations": 1,
        }

        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"
        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _quick_run_patches(
                mock_session, mock_system, mock_formatter, mock_storage, mock_qp
            ):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "follow-up query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
                research_context=research_context,
            )
        assert research.status == "completed"


class TestRunResearchProcessSettingsContext:
    """Ported from ``TestRunResearchProcessSettingsContext``.

    ``test_research_execution_boundary.py`` pins that the installed context
    is stamped with the owner, and ``test_research_service_execution.py``
    pins ``SnapshotSettingsContext``'s own extraction rules in isolation.
    Neither pins that the context the WORKER installs unwraps full setting
    objects (``{"value": ..., "type": ...}``) — the shape a real snapshot
    taken from the settings table has. If that unwrapping regressed, every
    consumer downstream would receive the dict instead of the value.
    """

    def test_settings_context_with_setting_objects(self):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_system.analyze_topic.return_value = {
            "findings": [{"phase": "search", "content": "data"}],
            "formatted_findings": "# Results",
            "iterations": 1,
        }
        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"
        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True
        mock_qp = MagicMock()

        # Settings snapshot with both raw values and setting objects.
        # Includes search.tool so the run proceeds past the egress build
        # (which fails closed on a snapshot with no primary) to the
        # SettingsContext extraction under test.
        snapshot = {
            "report.citation_format": {
                "value": "number_hyperlinks",
                "type": "str",
            },
            "search.max_results": 10,  # raw value
            "search.tool": "searxng",
        }

        with ExitStack() as stack:
            for cm in _quick_run_patches(
                mock_session, mock_system, mock_formatter, mock_storage, mock_qp
            ):
                stack.enter_context(cm)
            mock_set_ctx = stack.enter_context(
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "quick",
                username="alice",
                settings_snapshot=snapshot,
                model="m",
                search_engine="s",
            )

        # Verify settings context was set
        mock_set_ctx.assert_called_once()
        ctx = mock_set_ctx.call_args[0][0]
        # The SettingsContext should extract values from setting objects
        assert ctx.get_setting("report.citation_format") == "number_hyperlinks"
        assert ctx.get_setting("search.max_results") == 10
        assert ctx.get_setting("nonexistent", "default") == "default"


class TestRunResearchProcessSubscription:
    """Ported from ``TestRunResearchProcessSubscription``.

    On completion a subscription-triggered run must advance the
    subscription's refresh schedule. Without it the scheduler re-fires the
    same subscription on every tick.
    """

    def test_subscription_update_on_completion(self):
        mock_session = MagicMock()
        research = _make_research_mock(
            research_meta={"subscription_id": "sub_123"}
        )
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        # On completion the service loads the NewsSubscription via ORM
        # (db_session.query(...).filter(...).first()) and advances its
        # refresh schedule. The research-row lookups above use filter_by, so
        # the subscription lookup (which uses filter) is an independent mock
        # chain.
        mock_sub = MagicMock()
        mock_sub.id = "sub_123"
        mock_sub.refresh_interval_minutes = 60
        mock_session.query.return_value.filter.return_value.first.return_value = mock_sub

        mock_system = MagicMock()
        mock_system.analyze_topic.return_value = dict(_SIMPLE_RESULTS)
        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"
        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _quick_run_patches(
                mock_session, mock_system, mock_formatter, mock_storage, mock_qp
            ):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                )
            )
            mock_advance = stack.enter_context(
                patch(
                    "local_deep_research.news.subscription_runner.advance_refresh_schedule",
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )
        mock_advance.assert_called_once()
        # Called with the loaded subscription as the first positional arg.
        assert mock_advance.call_args.args[0] is mock_sub


class TestRunResearchProcessFinallyBlock:
    """Ported from ``TestRunResearchProcessFinallyBlock``.

    The ``finally`` block closes the search engine, the search system and
    the LLM. Neither ``@thread_cleanup`` nor
    ``cleanup_research_resources`` does this, so losing it means HTTP
    connection pools and strategy thread pools leak per run until file
    descriptors run out under sustained load.
    """

    def _run(self, mock_llm, mock_search, mock_system):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system.analyze_topic.return_value = dict(_SIMPLE_RESULTS)
        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"
        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True
        mock_qp = MagicMock()

        patches = _quick_run_patches(
            mock_session, mock_system, mock_formatter, mock_storage, mock_qp
        )
        # Replace the generic llm/search patches with the instrumented ones.
        patches = [
            p
            for p in patches
            if getattr(p, "attribute", None) not in ("get_llm", "get_search")
        ]
        patches.append(patch(f"{RS}.get_llm", return_value=mock_llm))
        patches.append(patch(f"{RS}.get_search", return_value=mock_search))

        with ExitStack() as stack:
            for cm in patches:
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                )
            )
            raw_fn = _get_raw_run_research_process()
            raw_fn(
                "r1",
                "query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

    def test_finally_closes_resources(self):
        """Verify use_search, system, use_llm get closed in finally."""
        mock_llm = MagicMock()
        mock_search = MagicMock()
        mock_system = MagicMock()

        self._run(mock_llm, mock_search, mock_system)

        mock_search.close.assert_called_once()
        mock_system.close.assert_called_once()
        mock_llm.close.assert_called_once()

    def test_finally_close_exceptions_suppressed(self):
        """Exceptions in close() calls should be suppressed."""
        mock_llm = MagicMock()
        mock_llm.close.side_effect = RuntimeError("close fail")
        mock_search = MagicMock()
        mock_search.close.side_effect = RuntimeError("close fail")
        mock_system = MagicMock()
        mock_system.close.side_effect = RuntimeError("close fail")

        # Should not raise despite close errors
        self._run(mock_llm, mock_search, mock_system)


class TestRunResearchProcessSaveReportFailure:
    """Ported from ``TestRunResearchProcessSaveReportFailure``.

    ``storage.save_report`` returning False is a silent failure mode: the
    report was not persisted. The worker must treat it as an error (queue an
    error update) rather than marking the run completed with no report.
    """

    def test_save_report_failure_raises(self):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research

        mock_system = MagicMock()
        mock_system.analyze_topic.return_value = dict(_SIMPLE_RESULTS)
        mock_formatter = MagicMock()
        mock_formatter.format_document.return_value = "formatted"
        mock_storage = MagicMock()
        mock_storage.save_report.return_value = False  # Save fails
        mock_qp = MagicMock()

        with ExitStack() as stack:
            for cm in _quick_run_patches(
                mock_session, mock_system, mock_formatter, mock_storage, mock_qp
            ):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                )
            )
            raw_fn = _get_raw_run_research_process()
            # The error handler should catch this
            raw_fn(
                "r1",
                "query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )
        # Should queue error
        mock_qp.queue_error_update.assert_called_once()


class TestRunResearchProcessErrorHandlerTermination:
    """Ported from
    ``TestRunResearchProcessErrorHandler::test_error_termination_requested_marks_suspended``.

    NOTE: main's version carried no assertions at all — it is a smoke test
    that the terminated-before-start branch does not raise when the search
    system would also have failed. It is ported as written (assertion
    fidelity cuts both ways); the asserting version of this property is
    ``TestRunResearchProcessTerminatedBeforeStart::test_terminated_early``
    above.
    """

    def test_error_termination_requested_marks_suspended(self):
        mock_session = MagicMock()
        research = _make_research_mock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = research
        mock_qp = MagicMock()
        mock_system = MagicMock()
        mock_system.analyze_topic.side_effect = Exception("fail")

        with ExitStack() as stack:
            for cm in _state_patches(terminated=True):
                stack.enter_context(cm)
            for cm in _socket_patches():
                stack.enter_context(cm)
            for cm in (
                patch(f"{RS}.get_llm", return_value=MagicMock()),
                patch(f"{RS}.get_search", return_value=MagicMock()),
                patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
                patch(
                    f"{RS}.get_user_db_session",
                    side_effect=_fake_session_ctx(mock_session),
                ),
                patch(f"{RS}.cleanup_research_resources"),
                patch(f"{RS}.set_search_context"),
                patch(f"{RS}.ErrorReportGenerator"),
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=MagicMock(),
                ),
                patch(QUEUE_PROC, mock_qp),
                patch("local_deep_research.settings.logger.log_settings"),
                patch(
                    "local_deep_research.config.thread_settings.set_settings_context"
                ),
            ):
                stack.enter_context(cm)

            raw_fn = _get_raw_run_research_process()
            # is_termination_requested returns True, so it exits early
            raw_fn(
                "r1",
                "query",
                "quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )


# ---------------------------------------------------------------------------
# clamp_user_max_concurrent
# ---------------------------------------------------------------------------
class TestClampUserMaxConcurrent:
    """Ported from ``TestClampUserMaxConcurrent`` (see #5481).

    ``app.max_concurrent_researches`` is a per-user, user-editable setting
    gating how many of a user's OWN researches start immediately instead of
    queueing. Its JSON schema now caps it at 20, but a value written before
    that cap existed (or a malformed/tampered stored value) keeps its raw
    form until the schema is reconciled on next login -- this clamp is the
    read-site defense-in-depth backstop that prevents any such value from
    exceeding the server-wide semaphore ceiling.

    The branch has read-site tests (``tests/web/queue/
    test_queue_processor_contracts.py::
    test_dispatch_uses_the_clamped_per_user_max_concurrent`` and
    ``tests/chat/test_chat_concurrency_clamp.py``) but NOTHING covers the
    helper's own arithmetic: the ceiling, the floor, and the unparseable
    fallback are all unasserted on the branch.
    """

    def test_clamps_value_above_global_ceiling(self):
        from local_deep_research.web.services.research_service import (
            clamp_user_max_concurrent,
        )

        with patch(f"{RS}._MAX_GLOBAL_CONCURRENT", 10):
            assert clamp_user_max_concurrent(1000) == 10
            assert clamp_user_max_concurrent(11) == 10

    def test_value_within_ceiling_is_unchanged(self):
        from local_deep_research.web.services.research_service import (
            clamp_user_max_concurrent,
        )

        with patch(f"{RS}._MAX_GLOBAL_CONCURRENT", 10):
            assert clamp_user_max_concurrent(3) == 3
            assert clamp_user_max_concurrent(1) == 1
            assert clamp_user_max_concurrent(10) == 10

    def test_default_of_3_is_unaffected(self):
        """The documented default (3) must pass through unclamped under the
        default global ceiling (10)."""
        from local_deep_research.web.services.research_service import (
            clamp_user_max_concurrent,
        )

        assert clamp_user_max_concurrent(3) == 3

    def test_non_positive_value_floored_to_one(self):
        from local_deep_research.web.services.research_service import (
            clamp_user_max_concurrent,
        )

        with patch(f"{RS}._MAX_GLOBAL_CONCURRENT", 10):
            assert clamp_user_max_concurrent(0) == 1
            assert clamp_user_max_concurrent(-5) == 1

    def test_unparseable_value_falls_back_to_default(self):
        from local_deep_research.web.services.research_service import (
            clamp_user_max_concurrent,
        )

        with patch(f"{RS}._MAX_GLOBAL_CONCURRENT", 10):
            assert clamp_user_max_concurrent("not-a-number") == 3
            assert clamp_user_max_concurrent(None) == 3

    def test_clamp_is_applied_at_the_research_routes_read_site(self):
        """start_research reads app.max_concurrent_researches through the
        clamp helper, not the raw settings value.

        main's ``web.routes.research_routes`` is ``web.routers.research`` on
        this branch. Nothing here asserts the clamp is *called* — that is
        what the queue/chat successors do for their own sites — only that
        the research router still binds the symbol at all.
        """
        import local_deep_research.web.routers.research as rr

        assert rr.clamp_user_max_concurrent is not None
        with patch(f"{RS}._MAX_GLOBAL_CONCURRENT", 5):
            assert rr.clamp_user_max_concurrent(1000) == 5