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.

11.6 KB
Raw Blame
"""Ported from ``tests/news/test_web_routes_comprehensive.py`` on main
(deleted by the FastAPI migration).

Old surface: ``news/web.py::create_news_blueprint`` + ``load_user_settings``.
New surface: ``web/routers/news_pages.py`` (``router`` + ``_load_user_settings``).

Successor audit
---------------
``tests/web/routers/test_news_strategy_dropdown.py`` is a PARTIAL successor:
it pins that ``news_page``'s ``strategies`` are ``{name,label}`` dicts and
that ``source-based`` is among them. It says nothing about

* which template each of the four page routes renders,
* ``focused-iteration`` / ``topic-organization`` being offered,
* the ``default_settings`` block the subscription form is built from
  (iterations / questions_per_iteration / search_engine / model_provider /
  search_strategy) -- five hardcoded values that reach the browser as form
  defaults,
* ``subscription`` being ``None`` on the create page,
* any of ``edit_subscription_page``'s three branches (loaded / not-found /
  exception), including that the lookup is scoped to the caller,
* ``_load_user_settings`` returning early rather than exploding when handed
  no DB session.

Those are ported here. Everything asserted against the RENDER CONTEXT rather
than the rendered HTML, exactly as the deleted file did (it mocked
``render_template`` and read ``call_args``): the values are template inputs,
and asserting on markup would pin the template instead of the handler.

Dropped from the original, with reasons
---------------------------------------
* ``TestCreateNewsBlueprint`` (3 tests) -- ``isinstance(bp, Blueprint)``,
  ``bp.name == "news"`` and ``bp.deferred_functions`` are Flask object model.
  An ``APIRouter`` has no ``name`` and materialises routes eagerly. The
  property behind them -- "these routes are actually wired up" -- is
  re-expressed as ``test_news_pages_are_mounted_on_the_app``, which is
  strictly stronger: a well-formed router that ``_mount_all`` forgot would
  pass the old blueprint assertions and fail this one.
* ``test_function_exists`` (``callable(load_user_settings)``) -- subsumed by
  ``test_load_user_settings_returns_early_without_session``, which calls it.
"""

from unittest.mock import MagicMock, patch

import pytest
from fastapi.routing import APIRoute
from starlette.requests import Request

NEWS_PAGES = "local_deep_research.web.routers.news_pages"
GET_SUBSCRIPTION = "local_deep_research.news.api.get_subscription"
GET_USER_DB_SESSION = (
    "local_deep_research.database.session_context.get_user_db_session"
)

USERNAME = "testuser"


def _request(path="/news/"):
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "headers": [],
            "session": {},
        }
    )


def _capture(handler, **kwargs):
    """Call a news_pages handler and return the template name + context.

    Mirrors the deleted file's ``@patch("...news.web.render_template")`` +
    ``mock_render.call_args`` idiom.
    """
    from local_deep_research.web.routers import news_pages

    captured = {}

    def fake_template_response(request, name, context, **kw):
        captured["name"] = name
        captured["context"] = context
        return "rendered"

    with patch.object(
        news_pages.templates,
        "TemplateResponse",
        side_effect=fake_template_response,
    ):
        getattr(news_pages, handler)(_request(), **kwargs)

    return captured["name"], captured["context"]


@pytest.fixture
def neutralize_settings_load():
    """The subscription-form routes ALWAYS load user settings from the DB.

    Successor of the deleted file's fixture of the same name: neutralise the
    DB path so the tests observe the pristine hardcoded defaults.
    ``TestLoadUserSettings`` deliberately does NOT use it.
    """
    from local_deep_research.web.routers import news_pages

    with (
        patch(GET_USER_DB_SESSION),
        patch.object(news_pages, "_load_user_settings"),
    ):
        yield


# ---------------------------------------------------------------- mounting


def test_news_pages_are_mounted_on_the_app(app):
    """Successor of ``bp.name == "news"`` + ``bp.deferred_functions``.

    The blueprint assertions could only see that routes had been *declared*.
    This sees that they reach the app: a router missing from ``_mount_all``
    404s every page while a router-object assertion stays green.
    """
    mounted = {
        (r.path, frozenset(r.methods))
        for r in app.routes
        if isinstance(r, APIRoute)
    }
    for path in (
        "/news/",
        "/news/subscriptions",
        "/news/subscriptions/new",
        "/news/subscriptions/{subscription_id}/edit",
    ):
        assert any(p == path and "GET" in m for p, m in mounted), (
            f"{path} is not mounted"
        )


# --------------------------------------------------------------- news page


class TestNewsPageRoute:
    def test_news_page_renders_template(self):
        name, _ = _capture("news_page", username=USERNAME)
        assert name == "pages/news.html"

    def test_news_page_passes_strategies(self):
        _, context = _capture("news_page", username=USERNAME)
        assert "strategies" in context
        assert isinstance(context["strategies"], list)

    def test_news_page_includes_expected_strategies(self):
        _, context = _capture("news_page", username=USERNAME)
        names = [s["name"] for s in context["strategies"]]
        assert "source-based" in names
        assert "focused-iteration" in names


class TestSubscriptionsPageRoute:
    def test_subscriptions_page_renders_template(self):
        name, _ = _capture("subscriptions_page", username=USERNAME)
        assert name == "pages/subscriptions.html"


# ------------------------------------------------------- new subscription


@pytest.mark.usefixtures("neutralize_settings_load")
class TestNewSubscriptionPageRoute:
    def test_new_subscription_page_renders_template(self):
        name, _ = _capture("new_subscription_page", username=USERNAME)
        assert name == "pages/news-subscription-form.html"

    def test_new_subscription_passes_default_settings(self):
        _, context = _capture("new_subscription_page", username=USERNAME)
        assert "default_settings" in context
        settings = context["default_settings"]
        assert "iterations" in settings
        assert "search_engine" in settings

    def test_new_subscription_passes_none_subscription(self):
        _, context = _capture("new_subscription_page", username=USERNAME)
        assert context["subscription"] is None


@pytest.mark.usefixtures("neutralize_settings_load")
class TestDefaultSettings:
    """The five hardcoded form defaults, each pinned by value."""

    def _settings(self):
        _, context = _capture("new_subscription_page", username=USERNAME)
        return context["default_settings"]

    def test_new_subscription_default_iterations(self):
        assert self._settings()["iterations"] == 3

    def test_new_subscription_default_questions_per_iteration(self):
        assert self._settings()["questions_per_iteration"] == 5

    def test_new_subscription_default_search_engine(self):
        assert self._settings()["search_engine"] == "searxng"

    def test_new_subscription_default_model_provider(self):
        assert self._settings()["model_provider"] == "ollama"

    def test_new_subscription_default_search_strategy(self):
        assert self._settings()["search_strategy"] == "source-based"


# ------------------------------------------------------ edit subscription


@pytest.mark.usefixtures("neutralize_settings_load")
class TestEditSubscriptionPageRoute:
    def test_edit_subscription_loads_subscription(self):
        with patch(GET_SUBSCRIPTION) as mock_get:
            mock_get.return_value = {"id": "sub-123", "query": "test"}
            _capture(
                "edit_subscription_page",
                subscription_id="sub-123",
                username=USERNAME,
            )

        mock_get.assert_called_once()
        args, kwargs = mock_get.call_args
        assert args[0] == "sub-123"
        # The FastAPI port added per-user scoping the Flask route did not
        # have (``api.get_subscription(subscription_id)``). Assert the kwarg
        # is PRESENT and carries the caller -- checking only the value
        # against a default would pass if the kwarg were dropped again.
        assert "username" in kwargs, kwargs
        assert kwargs["username"] == USERNAME

    def test_edit_subscription_passes_subscription(self):
        subscription = {"id": "sub-123", "query": "test"}
        with patch(GET_SUBSCRIPTION, return_value=subscription):
            name, context = _capture(
                "edit_subscription_page",
                subscription_id="sub-123",
                username=USERNAME,
            )

        assert name == "pages/news-subscription-form.html"
        assert context["subscription"] == subscription
        # The happy path must NOT render an error banner.
        assert "error" not in context

    def test_edit_subscription_handles_not_found(self):
        with patch(GET_SUBSCRIPTION, return_value=None):
            _, context = _capture(
                "edit_subscription_page",
                subscription_id="nonexistent",
                username=USERNAME,
            )

        assert context["subscription"] is None
        assert "error" in context

    def test_edit_subscription_handles_exception(self):
        with patch(GET_SUBSCRIPTION, side_effect=Exception("Database error")):
            _, context = _capture(
                "edit_subscription_page",
                subscription_id="sub-123",
                username=USERNAME,
            )

        assert "error" in context
        assert context["subscription"] is None
        # The internal message must not reach the page.
        assert "Database error" not in str(context["error"])


# ------------------------------------------------------ load_user_settings


class TestLoadUserSettings:
    """Exercises the real function -- no neutralising fixture."""

    def test_load_user_settings_returns_early_without_session(self):
        from local_deep_research.web.routers.news_pages import (
            _load_user_settings,
        )

        default_settings = {"iterations": 3}
        _load_user_settings(default_settings, db_session=None)

        assert default_settings["iterations"] == 3
        assert default_settings == {"iterations": 3}

    def test_load_user_settings_applies_saved_values(self):
        """Positive control for the early return above.

        Without this, deleting the ``if not db_session: return`` guard's
        *body* (or the whole function body) would leave the early-return test
        green.
        """
        from local_deep_research.web.routers.news_pages import (
            _load_user_settings,
        )

        manager = MagicMock()
        manager.get_setting.side_effect = lambda key, default=None: {
            "search.iterations": 9,
            "llm.provider": "openai",
        }.get(key, default)

        default_settings = {"iterations": 3, "model_provider": "ollama"}
        with patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=manager,
        ):
            _load_user_settings(
                default_settings, db_session=MagicMock(), username=USERNAME
            )

        assert default_settings["iterations"] == 9
        assert default_settings["model_provider"] == "openai"


class TestStrategyList:
    def test_strategies_include_topic_organization(self):
        from local_deep_research.search_system_factory import (
            get_available_strategies,
        )

        names = [s["name"] for s in get_available_strategies()]
        assert "topic-organization" in names