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.

22.3 KB
Raw Blame
"""``web/routers/news_pages.py`` — the settings loader and the form defaults.

Ported from ``tests/news/test_web_blueprint.py`` and
``tests/news/test_web_blueprint_extended.py``, both deleted with the
``news/web.py`` Flask blueprint they drove. The blueprint's page routes
survive verbatim in ``web/routers/news_pages.py``.

WHAT IS *NOT* RE-ASSERTED HERE
------------------------------
``tests/news/test_news_router_contracts.py::TestSubscriptionFormPages`` and
``::TestNewsHealth`` already cover the two form pages rendering (200 +
``text/html``), the not-found and error branches rendering rather than
500ing, the error branch leaking no exception text, the anonymous 302 to
``/auth/login?next=``, and the whole health-check body.
``tests/web/routers/test_news_strategy_dropdown.py`` covers the strategy
dicts' shape. ``tests/web/test_route_table_parity.py`` covers the page set.

WHAT WAS DROPPED FROM THE DELETED FILES, AND WHY
------------------------------------------------
* every ``create_news_blueprint()`` / ``bp.name`` / ``bp.url_prefix`` /
  ``bp.deferred_functions`` / ``isinstance(bp, Blueprint)`` test —
  Flask implementation details with no FastAPI meaning.
* ``test_web_blueprint_extended.py::TestDefaultSettings`` and
  ``::TestStrategiesConfiguration`` — tautologies: each built a dict or
  list literal *in the test body* and asserted about that literal,
  never importing ``news/web.py``. Proof they never touched the source:
  ``test_default_model_provider`` asserted ``"OLLAMA"`` while the real
  default was lowercase ``"ollama"``, and the strategy names they listed
  ("topic_based", "news_aggregation") do not exist — ``test_news_strategy_
  dropdown.py`` asserts their *absence*. Their subjects are re-pinned
  below as real assertions against the router.
* ``test_health_check_calls_get_user_feed(..., "health_check", limit=1)``
  — deliberately inverted by the port (the probe is now scoped to the
  authenticated caller); pinned in that form by test_news_router_contracts.
* ``test_edit_subscription_page_logs_subscription_id`` — the port removed
  that ``logger.info``, which wrote the whole subscription body to the log.
  A deliberate improvement; not restored.

WHY CONTEXT CAPTURE RATHER THAN HTTP
------------------------------------
``default_settings`` and the ``error`` string are consumed by the template
but not all of them are rendered into the HTML — ``error`` in particular is
never emitted by ``news-subscription-form.html``, so the not-found branch
and the exception branch are byte-identical in the response body. The
property is invisible in the output, so it is pinned structurally, at the
``TemplateResponse`` call. Same technique as
``tests/web/routers/test_news_strategy_dropdown.py``.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from starlette.requests import Request

from local_deep_research.constants import (
    AVAILABLE_STRATEGIES,
    DEFAULT_SEARCH_TOOL,
)
from local_deep_research.web.routers import news_pages

SETTINGS_MANAGER = "local_deep_research.utilities.db_utils.get_settings_manager"
SESSION_CONTEXT = (
    "local_deep_research.database.session_context.get_user_db_session"
)
NEWS_API = "local_deep_research.news.api"

USERNAME = "alice"

# The fallbacks the two subscription-form pages hand the template when the
# user has no saved value. Asserted as a whole dict so a DROPPED key is a
# failure too: ``custom_endpoint`` is rendered through ``| tojson`` and
# Jinja's Undefined is not JSON-serialisable, so losing it turns the page
# into a 500.
EXPECTED_DEFAULT_SETTINGS = {
    "iterations": 3,
    "questions_per_iteration": 5,
    "search_engine": DEFAULT_SEARCH_TOOL,
    "model_provider": "ollama",
    "model": "",
    "search_strategy": "source-based",
    "egress_scope": "adaptive",
    "custom_endpoint": "",
}


def _dummy_request(path="/news/subscriptions/new"):
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "headers": [],
            "session": {"username": USERNAME},
        }
    )


@contextmanager
def _captured_render():
    """Run a page handler with the template engine and DB stubbed out,
    yielding the kwargs of the single TemplateResponse call it makes."""
    captured = {}

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

    @contextmanager
    def fake_db_session(*args, **kwargs):
        yield MagicMock(name="user_db_session")

    with (
        patch.object(
            news_pages.templates,
            "TemplateResponse",
            side_effect=fake_template_response,
        ),
        patch(SESSION_CONTEXT, side_effect=fake_db_session),
    ):
        yield captured


# ===========================================================================
# Premise guard
# ===========================================================================


@pytest.mark.parametrize(
    "path,endpoint",
    [
        ("/news/", "news_page"),
        ("/news/subscriptions", "subscriptions_page"),
        ("/news/subscriptions/new", "new_subscription_page"),
        (
            "/news/subscriptions/{subscription_id}/edit",
            "edit_subscription_page",
        ),
    ],
)
def test_the_page_handlers_are_the_ones_mounted(path, endpoint):
    matches = [
        route for route in news_pages.router.routes if route.path == path
    ]
    assert len(matches) == 1, f"GET {path} is not mounted exactly once"
    assert matches[0].endpoint is getattr(news_pages, endpoint)


# ===========================================================================
# The subscription-form default settings
# ===========================================================================


class TestSubscriptionFormDefaults:
    """The fallbacks used when the user has saved nothing.

    They are duplicated between the two handlers (news_pages.py:53 and
    :97), so a change to one is silently a divergence from the other —
    which is why both are asserted against the same table.
    """

    def test_new_page_defaults(self):
        with patch.object(news_pages, "_load_user_settings"):
            with _captured_render() as captured:
                news_pages.new_subscription_page(
                    _dummy_request(), username=USERNAME
                )

        assert captured["context"]["default_settings"] == (
            EXPECTED_DEFAULT_SETTINGS
        )
        assert captured["context"]["subscription"] is None
        assert captured["name"] == "pages/news-subscription-form.html"

    def test_edit_page_defaults(self):
        with patch.object(news_pages, "_load_user_settings"):
            with patch(
                f"{NEWS_API}.get_subscription",
                return_value={"id": "s1", "name": "n"},
            ):
                with _captured_render() as captured:
                    news_pages.edit_subscription_page(
                        _dummy_request(), "s1", username=USERNAME
                    )

        assert captured["context"]["default_settings"] == (
            EXPECTED_DEFAULT_SETTINGS
        )
        assert captured["name"] == "pages/news-subscription-form.html"

    def test_the_two_pages_agree(self):
        """A defensive equality between the two duplicated literals: they
        are the same dict in two places, and drift between them means the
        create and edit forms disagree about what a fresh field means."""
        with patch.object(news_pages, "_load_user_settings"):
            with _captured_render() as new_page:
                news_pages.new_subscription_page(
                    _dummy_request(), username=USERNAME
                )
            with patch(
                f"{NEWS_API}.get_subscription", return_value={"id": "s"}
            ):
                with _captured_render() as edit_page:
                    news_pages.edit_subscription_page(
                        _dummy_request(), "s", username=USERNAME
                    )

        assert (
            new_page["context"]["default_settings"]
            == edit_page["context"]["default_settings"]
        )


class TestFormPagesConsultSavedSettings:
    """The pages must actually read the user's settings, not always serve
    the hardcoded table above. Nothing on the branch pinned that: a handler
    that dropped the ``_load_user_settings`` call renders an identical 200."""

    def test_new_page_opens_the_user_db_and_loads_settings(self):
        with patch.object(news_pages, "_load_user_settings") as load:
            with _captured_render():
                news_pages.new_subscription_page(
                    _dummy_request(), username=USERNAME
                )

        assert load.call_count == 1
        assert load.call_args.args[2] == USERNAME, (
            "settings were loaded for the wrong user"
        )
        assert load.call_args.args[0] is not None

    def test_edit_page_loads_settings_for_a_found_subscription(self):
        with patch.object(news_pages, "_load_user_settings") as load:
            with patch(
                f"{NEWS_API}.get_subscription", return_value={"id": "s1"}
            ):
                with _captured_render():
                    news_pages.edit_subscription_page(
                        _dummy_request(), "s1", username=USERNAME
                    )

        assert load.call_count == 1
        assert load.call_args.args[2] == USERNAME

    def test_edit_page_forwards_the_session_username_to_the_service(self):
        with patch.object(news_pages, "_load_user_settings"):
            with patch(
                f"{NEWS_API}.get_subscription", return_value={"id": "s1"}
            ) as spy:
                with _captured_render():
                    news_pages.edit_subscription_page(
                        _dummy_request(), "s1", username=USERNAME
                    )

        assert spy.call_args.kwargs["username"] == USERNAME


class TestEditPageErrorBranches:
    """``test_news_router_contracts.py`` pins that both branches render a
    200 and leak nothing. Neither pins WHICH branch was taken — and the
    template never emits ``error``, so the two are indistinguishable in the
    response body and from the happy path. Pinned at the context instead."""

    def test_absent_subscription_sets_the_not_found_error(self):
        with patch(f"{NEWS_API}.get_subscription", return_value=None):
            with _captured_render() as captured:
                news_pages.edit_subscription_page(
                    _dummy_request(), "missing", username=USERNAME
                )

        context = captured["context"]
        assert context["error"] == "Subscription not found"
        assert context["subscription"] is None
        assert context["default_settings"] == EXPECTED_DEFAULT_SETTINGS

    def test_a_raising_service_sets_the_load_error(self):
        with patch(
            f"{NEWS_API}.get_subscription",
            side_effect=RuntimeError("no such table: news_subscriptions"),
        ):
            with _captured_render() as captured:
                news_pages.edit_subscription_page(
                    _dummy_request(), "s1", username=USERNAME
                )

        context = captured["context"]
        assert context["error"] == "Error loading subscription"
        assert context["subscription"] is None
        assert "no such table" not in str(context)

    def test_the_happy_path_sets_no_error(self):
        """Control: without this, a handler that always reports an error
        would satisfy both rows above."""
        with patch.object(news_pages, "_load_user_settings"):
            with patch(
                f"{NEWS_API}.get_subscription", return_value={"id": "s1"}
            ):
                with _captured_render() as captured:
                    news_pages.edit_subscription_page(
                        _dummy_request(), "s1", username=USERNAME
                    )

        assert "error" not in captured["context"]
        assert captured["context"]["subscription"] == {"id": "s1"}


class TestTemplateNames:
    """Which page each route renders. A swap to another *existing*
    template is a 200 either way."""

    def test_news_page_renders_the_news_template(self):
        with _captured_render() as captured:
            news_pages.news_page(_dummy_request("/news/"), username=USERNAME)

        assert captured["name"] == "pages/news.html"

    def test_subscriptions_page_renders_the_subscriptions_template(self):
        with _captured_render() as captured:
            news_pages.subscriptions_page(
                _dummy_request("/news/subscriptions"), username=USERNAME
            )

        assert captured["name"] == "pages/subscriptions.html"


class TestNewsPageStrategies:
    """``test_news_strategy_dropdown.py`` pins the dict shape and that
    ``source-based`` is present. Nothing pins that the OTHER four survive —
    a truncated list renders a perfectly valid dropdown with one option."""

    def test_every_available_strategy_reaches_the_page(self):
        with _captured_render() as captured:
            news_pages.news_page(_dummy_request("/news/"), username=USERNAME)

        names = [s["name"] for s in captured["context"]["strategies"]]
        assert names == [s["name"] for s in AVAILABLE_STRATEGIES]
        assert {
            "source-based",
            "focused-iteration",
            "focused-iteration-standard",
            "topic-organization",
            "langgraph-agent",
        } <= set(names)

    def test_the_form_pages_offer_the_same_strategies(self):
        with patch.object(news_pages, "_load_user_settings"):
            with _captured_render() as captured:
                news_pages.new_subscription_page(
                    _dummy_request(), username=USERNAME
                )

        names = [s["name"] for s in captured["context"]["strategies"]]
        assert names == [s["name"] for s in AVAILABLE_STRATEGIES]


# ===========================================================================
# _load_user_settings -- the settings-key mapping
# ===========================================================================

# request setting key -> (template key, value the fake manager returns)
# Three of these are non-obvious renames. A typo in any key silently
# degrades the subscription form to the hardcoded defaults above, with no
# error anywhere: the page still renders 200 with plausible values.
SETTING_KEY_MAP = [
    ("search.iterations", "iterations", 10),
    ("search.questions_per_iteration", "questions_per_iteration", 8),
    ("search.tool", "search_engine", "google"),
    ("llm.provider", "model_provider", "openai"),
    ("llm.model", "model", "gpt-4"),
    ("search.search_strategy", "search_strategy", "focused-iteration"),
    ("llm.openai_endpoint.url", "custom_endpoint", "http://box:1234/v1"),
    ("policy.egress_scope", "egress_scope", "closed"),
]

# The per-key fallback passed as ``get_setting``'s second argument.
SETTING_FALLBACKS = {
    "search.iterations": 3,
    "search.questions_per_iteration": 5,
    "search.tool": DEFAULT_SEARCH_TOOL,
    "llm.provider": "ollama",
    "llm.model": "",
    "search.search_strategy": "source-based",
    "llm.openai_endpoint.url": "",
    "policy.egress_scope": "adaptive",
}


def _load_with(manager_side_effect):
    """Run ``_load_user_settings`` over a fresh copy of the defaults."""
    settings = dict(EXPECTED_DEFAULT_SETTINGS)
    manager = MagicMock()
    manager.get_setting.side_effect = manager_side_effect
    with patch(SETTINGS_MANAGER, return_value=manager) as get_manager:
        news_pages._load_user_settings(
            settings, MagicMock(name="db_session"), USERNAME
        )
    return settings, manager, get_manager


class TestLoadUserSettings:
    """Zero tests referenced this function on the branch."""

    @pytest.mark.parametrize(
        "setting_key,template_key,value",
        SETTING_KEY_MAP,
        ids=[k for k, _, _ in SETTING_KEY_MAP],
    )
    def test_each_setting_lands_under_its_template_key(
        self, setting_key, template_key, value
    ):
        settings, _, _ = _load_with(
            lambda key, default: value if key == setting_key else default
        )

        assert settings[template_key] == value, (
            f"{setting_key!r} did not reach default_settings[{template_key!r}]"
        )

    @pytest.mark.parametrize(
        "setting_key,template_key,value",
        SETTING_KEY_MAP,
        ids=[k for k, _, _ in SETTING_KEY_MAP],
    )
    def test_one_setting_does_not_disturb_the_others(
        self, setting_key, template_key, value
    ):
        """Guards against a mapping that writes every value to one key, or
        cross-wires two keys — both of which pass the test above."""
        settings, _, _ = _load_with(
            lambda key, default: value if key == setting_key else default
        )

        for _, other_key, _ in SETTING_KEY_MAP:
            if other_key == template_key:
                continue
            assert (
                settings[other_key] == EXPECTED_DEFAULT_SETTINGS[other_key]
            ), f"setting {setting_key!r} also overwrote {other_key!r}"

    @pytest.mark.parametrize(
        "setting_key,fallback",
        sorted(SETTING_FALLBACKS.items()),
    )
    def test_the_fallback_passed_to_get_setting_is_pinned(
        self, setting_key, fallback
    ):
        """The second argument of each ``get_setting`` call: what an
        unconfigured user actually gets. ``policy.egress_scope`` defaulting
        to ``adaptive`` rather than an open scope is the safe-by-default
        half of #5204."""
        _, manager, _ = _load_with(lambda key, default: default)

        seen = {
            call.args[0]: call.args[1]
            for call in manager.get_setting.call_args_list
            if len(call.args) > 1
        }
        assert setting_key in seen, f"{setting_key!r} is no longer read at all"
        assert seen[setting_key] == fallback

    def test_the_settings_manager_is_scoped_to_the_session_and_user(self):
        db_session = MagicMock(name="db_session")
        settings = dict(EXPECTED_DEFAULT_SETTINGS)
        manager = MagicMock()
        manager.get_setting.side_effect = lambda key, default: default
        with patch(SETTINGS_MANAGER, return_value=manager) as get_manager:
            news_pages._load_user_settings(settings, db_session, USERNAME)

        get_manager.assert_called_once_with(db_session, USERNAME)

    def test_no_db_session_returns_without_touching_the_defaults(self):
        settings = dict(EXPECTED_DEFAULT_SETTINGS)

        with patch(SETTINGS_MANAGER) as get_manager:
            news_pages._load_user_settings(settings, None, USERNAME)

        assert settings == EXPECTED_DEFAULT_SETTINGS
        assert not get_manager.called, (
            "a missing session still went looking for a settings manager"
        )

    def test_a_raising_settings_manager_leaves_the_defaults_intact(self):
        """The broad ``except`` is the reason an unreadable settings table
        renders the form with defaults instead of 500ing the page."""
        settings = dict(EXPECTED_DEFAULT_SETTINGS)

        with patch(SETTINGS_MANAGER, side_effect=RuntimeError("db locked")):
            news_pages._load_user_settings(settings, MagicMock(), USERNAME)

        assert settings == EXPECTED_DEFAULT_SETTINGS

    def test_a_partial_failure_does_not_half_apply_the_update(self):
        """``update()`` is called once with the whole dict, so a manager
        that raises on the last key must leave NOTHING applied — not an
        incoherent mix of saved and default values."""
        settings = dict(EXPECTED_DEFAULT_SETTINGS)

        def explode_on_egress(key, default):
            if key == "policy.egress_scope":
                raise RuntimeError("db locked")
            return "changed" if key == "search.tool" else default

        manager = MagicMock()
        manager.get_setting.side_effect = explode_on_egress
        with patch(SETTINGS_MANAGER, return_value=manager):
            news_pages._load_user_settings(settings, MagicMock(), USERNAME)

        assert settings == EXPECTED_DEFAULT_SETTINGS

    def test_the_dict_is_mutated_in_place_not_rebound(self):
        """The callers pass their own dict and read it back afterwards; a
        rebinding implementation would silently serve pure defaults."""
        settings = dict(EXPECTED_DEFAULT_SETTINGS)
        original = settings
        manager = MagicMock()
        manager.get_setting.side_effect = lambda key, default: (
            99 if key == "search.iterations" else default
        )

        with patch(SETTINGS_MANAGER, return_value=manager):
            news_pages._load_user_settings(settings, MagicMock(), USERNAME)

        assert original is settings
        assert original["iterations"] == 99

    def test_saved_settings_survive_into_the_rendered_page(self):
        """End-to-end control tying the loader back to the page: without
        it, every assertion above holds for a loader nothing calls."""
        manager = MagicMock()
        manager.get_setting.side_effect = lambda key, default: (
            "google" if key == "search.tool" else default
        )

        with patch(SETTINGS_MANAGER, return_value=manager):
            with _captured_render() as captured:
                news_pages.new_subscription_page(
                    _dummy_request(), username=USERNAME
                )

        assert (
            captured["context"]["default_settings"]["search_engine"] == "google"
        )


class TestSubscriptionsPageRenders(object):
    """``GET /news/subscriptions`` is the one news page with no
    authenticated render test anywhere on the branch — every existing
    reference to it drives the anonymous-302 direction, and two of those
    run against a synthetic stub app rather than the real router. A
    template that no longer parses would be a 500 nothing notices."""

    def test_the_page_renders_for_a_logged_in_user(self, authenticated_client):
        resp = authenticated_client.get(
            "/news/subscriptions", follow_redirects=False
        )

        assert resp.status_code == 200, resp.text[:400]
        assert "text/html" in resp.headers["content-type"]

    def test_an_anonymous_caller_is_bounced_to_login(self, client):
        """Explicitly ``follow_redirects=False``: with the default the login
        page's own 200 comes back and the assertion passes for the wrong
        reason."""
        resp = client.get("/news/subscriptions", follow_redirects=False)

        assert resp.status_code == 302, resp.text[:200]
        assert resp.headers["location"].startswith("/auth/login")