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.

71.6 KB
Raw Blame
"""Route branches of ``web/routers/news_flask_api.py`` with no successor.

Ported from the news test files the Flask->FastAPI migration deleted:
``tests/news/test_flask_api.py``, ``test_flask_api_coverage.py``,
``test_flask_api_coverage_gaps.py``, ``test_flask_api_extra_coverage.py``,
``test_flask_api_scheduler_coverage.py`` and
``test_safe_error_message_behavior.py`` (all against
``news/flask_api.py``, whose successor this module is).

WHY THESE AND NOT THE OTHER ~90 DELETED TESTS
---------------------------------------------
Most of what those files pinned is already covered, often more strongly,
by ``tests/security/test_news_scheduler_isolation_fastapi.py``,
``tests/security/test_scheduler_control_and_news_limits_fastapi.py``,
``tests/news/test_news_router_contracts.py``,
``tests/news/test_news_input_validation.py``,
``tests/news/test_news_api_contract_restored.py``,
``tests/web/routers/test_news_scheduler_check_now.py`` and
``tests/web/test_rate_limit_census.py``.  What is here is the residue: the
route branches for which deleting the guard from the router leaves every
existing test green.

Three whole routes had NO Python test on the branch before this file:
``POST /news/api/vote``, ``POST /news/api/feedback/batch`` and the
folders ``GET``/``POST``-duplicate/``PUT``-success/``DELETE`` family.

DELIBERATELY NOT PORTED (and why)
---------------------------------
* ``update_subscription`` / ``delete_subscription`` / ``get_subscription_
  history`` "error-dict" branches — the port removed them; not-found now
  arrives as a ``NewsAPIException`` and is pinned in
  ``test_news_router_contracts.py``.
* the ``search-history`` "no current_user" branches — ``Depends(require_auth)``
  replaced them, so there is no branch left to enter.
* the blueprint ``@errorhandler(400/404/500)`` trio — Flask-only; the
  app-level successors (with deliberately different strings) are pinned by
  ``tests/web/test_exception_handler_contract.py``.
* every rate-limit test — superseded by ``tests/web/test_rate_limit_census.py``
  and ``test_scheduler_control_and_news_limits_fastapi.py``, which read the
  amount/granularity/scope off the live slowapi ``Limit`` objects.

CONVENTIONS
-----------
``authenticated_client`` (tests/conftest.py) is a real registered+logged-in
user with a real per-user encrypted DB; only the ``news.api`` service
boundary and the APScheduler singleton are patched.  Every assertion that
is redirect-adjacent passes ``follow_redirects=False`` explicitly — httpx's
TestClient follows by default and Flask's did not.
"""

import json
import re
import uuid
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.web.routers import news_flask_api

# The router imports ``from ...news import api`` at module scope, so the
# module attribute is the patch target for the service boundary.
API = "local_deep_research.web.routers.news_flask_api.api"
FOLDER_MANAGER = "local_deep_research.web.routers.news_flask_api.FolderManager"
SESSION_CTX = (
    "local_deep_research.web.routers.news_flask_api.get_user_db_session"
)
SETTINGS_MANAGER_AT_ROUTER = (
    "local_deep_research.web.routers.news_flask_api.get_settings_manager"
)
SCHEDULER_TARGET = (
    "local_deep_research.scheduler.background.get_background_job_scheduler"
)
GATE_ENV = "LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL"

VOTE = "/news/api/vote"
BATCH = "/news/api/feedback/batch"
SUBSCRIBE = "/news/api/subscribe"
CURRENT = "/news/api/subscriptions/current"
FOLDERS = "/news/api/subscription/folders"
SUB_STATS = "/news/api/subscription/stats"
STATUS = "/news/api/scheduler/status"
STATS = "/news/api/scheduler/stats"
START = "/news/api/scheduler/start"
STOP = "/news/api/scheduler/stop"

CARD = "card-42"

REFRESH_RANGE_ERROR = (
    "refresh_minutes must be an integer between "
    f"{news_flask_api.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES} and "
    f"{news_flask_api.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES}"
)
ITERATIONS_RANGE_ERROR = (
    "search_iterations must be an integer between 1 and "
    f"{news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS}"
)
QUESTIONS_RANGE_ERROR = (
    "questions_per_iteration must be an integer between 1 and "
    f"{news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION}"
)
MODEL_PROVIDER_TYPE_ERROR = "model_provider must be a string or null"

INVALID_SUBSCRIPTION_PAYLOADS = [
    pytest.param({"query": None}, "query must be a string", id="query-null"),
    pytest.param({"query": 1}, "query must be a string", id="query-int"),
    pytest.param({"query": True}, "query must be a string", id="query-bool"),
    pytest.param({"query": []}, "query must be a string", id="query-list"),
    pytest.param({"query": {}}, "query must be a string", id="query-object"),
    pytest.param({"query": ""}, "query is required", id="query-empty"),
    pytest.param({"query": "   "}, "query is required", id="query-blank"),
    pytest.param(
        {
            "query": "q"
            * (news_flask_api.NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH + 1)
        },
        "query exceeds maximum length of "
        f"{news_flask_api.NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH} characters",
        id="query-over-limit",
    ),
    pytest.param({"name": 1}, "name must be a string or null", id="name-int"),
    pytest.param(
        {"name": True}, "name must be a string or null", id="name-bool"
    ),
    pytest.param({"name": []}, "name must be a string or null", id="name-list"),
    pytest.param(
        {"name": {}}, "name must be a string or null", id="name-object"
    ),
    pytest.param(
        {"folder_id": 1},
        "folder_id must be a string or null",
        id="folder-int",
    ),
    pytest.param(
        {"folder_id": True},
        "folder_id must be a string or null",
        id="folder-bool",
    ),
    pytest.param(
        {"folder_id": []},
        "folder_id must be a string or null",
        id="folder-list",
    ),
    pytest.param(
        {"folder_id": {}},
        "folder_id must be a string or null",
        id="folder-object",
    ),
    *[
        pytest.param(
            {"model_provider": value},
            MODEL_PROVIDER_TYPE_ERROR,
            id=f"model-provider-{label}",
        )
        for label, value in (
            ("int", 1),
            ("bool", True),
            ("list", []),
            ("object", {}),
        )
    ],
    *[
        pytest.param(
            {"refresh_minutes": value},
            REFRESH_RANGE_ERROR,
            id=f"refresh-{label}",
        )
        for label, value in (
            ("zero", 0),
            ("negative", -1),
            (
                "over-limit",
                news_flask_api.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES + 1,
            ),
            ("string", "60"),
            ("float", 60.0),
            ("bool", True),
            ("null", None),
            ("huge", 2**63),
        )
    ],
    *[
        pytest.param(
            {"search_iterations": value},
            ITERATIONS_RANGE_ERROR,
            id=f"iterations-{label}",
        )
        for label, value in (
            ("zero", 0),
            ("negative", -1),
            (
                "over-limit",
                news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS + 1,
            ),
            ("string", "2"),
            ("float", 2.0),
            ("bool", True),
            ("null", None),
        )
    ],
    *[
        pytest.param(
            {"questions_per_iteration": value},
            QUESTIONS_RANGE_ERROR,
            id=f"questions-{label}",
        )
        for label, value in (
            ("zero", 0),
            ("negative", -1),
            (
                "over-limit",
                news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION
                + 1,
            ),
            ("string", "2"),
            ("float", 2.0),
            ("bool", True),
            ("null", None),
        )
    ],
]

# One representative for every shared field, driven through each HTTP route
# below. The full type/range matrix above stays direct and fast; these prove
# the route wiring cannot bypass it before endpoint/service work.
SUBSCRIPTION_ROUTE_REJECTION_CASES = [
    pytest.param("query", {}, "query must be a string", id="query"),
    pytest.param("name", {}, "name must be a string or null", id="name"),
    pytest.param(
        "folder_id",
        {},
        "folder_id must be a string or null",
        id="folder-id",
    ),
    *[
        pytest.param(
            "model_provider",
            value,
            MODEL_PROVIDER_TYPE_ERROR,
            id=f"model-provider-{label}",
        )
        for label, value in (
            ("int", 1),
            ("bool", True),
            ("list", []),
            ("object", {}),
        )
    ],
    pytest.param("refresh_minutes", 0, REFRESH_RANGE_ERROR, id="refresh"),
    pytest.param(
        "search_iterations",
        news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS + 1,
        ITERATIONS_RANGE_ERROR,
        id="iterations",
    ),
    pytest.param(
        "questions_per_iteration",
        "2",
        QUESTIONS_RANGE_ERROR,
        id="questions",
    ),
]


def _feedback(card_id=CARD):
    return f"/news/api/feedback/{card_id}"


def _research(card_id=CARD):
    return f"/news/api/research/{card_id}"


def _username(client) -> str:
    resp = client.get("/auth/check")
    assert resp.status_code == 200, resp.text
    return resp.json()["username"]


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


@pytest.mark.parametrize(
    "path,method,endpoint",
    [
        ("/news/api/vote", "POST", "vote_on_news"),
        ("/news/api/feedback/batch", "POST", "get_batch_feedback"),
        ("/news/api/feedback/{card_id}", "POST", "submit_feedback"),
        ("/news/api/research/{card_id}", "POST", "research_news_item"),
        ("/news/api/subscribe", "POST", "create_subscription"),
        (
            "/news/api/subscriptions/{subscription_id}",
            "GET",
            "get_subscription",
        ),
        (
            "/news/api/subscriptions/current",
            "GET",
            "get_current_user_subscriptions",
        ),
        ("/news/api/subscription/folders", "GET", "get_folders"),
        ("/news/api/subscription/folders", "POST", "create_folder"),
        ("/news/api/subscription/folders/{folder_id}", "PUT", "update_folder"),
        (
            "/news/api/subscription/folders/{folder_id}",
            "DELETE",
            "delete_folder",
        ),
        ("/news/api/subscription/stats", "GET", "get_subscription_stats"),
        ("/news/api/scheduler/status", "GET", "get_scheduler_status"),
        ("/news/api/scheduler/stats", "GET", "scheduler_stats"),
        ("/news/api/scheduler/start", "POST", "start_scheduler"),
        ("/news/api/scheduler/stop", "POST", "stop_scheduler"),
    ],
)
def test_the_routes_under_test_are_still_mounted_here(path, method, endpoint):
    """A rename or remount would leave every test below asserting against a
    URL nothing serves — 404s that read like ordinary failures."""
    matches = [
        route
        for route in news_flask_api.router.routes
        if route.path == path and method in route.methods
    ]
    assert len(matches) == 1, (
        f"{method} {path} is not registered exactly once on the news router"
    )
    # The rate-limit decorator wraps the handler, so compare by name.
    assert matches[0].endpoint.__name__ == endpoint, (
        f"{method} {path} no longer resolves to {endpoint}"
    )


# ===========================================================================
# Shared subscription request validation
# ===========================================================================


class TestSubscriptionPayloadValidator:
    """Full JSON-type/range matrix for the validator all write paths use."""

    @pytest.mark.parametrize(
        "payload,message",
        INVALID_SUBSCRIPTION_PAYLOADS,
    )
    def test_invalid_values_have_a_stable_400_shape(self, payload, message):
        response = news_flask_api._validate_subscription_payload(payload)

        assert response.status_code == 400
        assert json.loads(response.body) == {"error": message}

    def test_create_requires_a_query_without_type_confusing_null(self):
        for payload in ({}, {"query": None}):
            response = news_flask_api._validate_subscription_payload(
                payload, require_query=True
            )

            assert response.status_code == 400
            assert json.loads(response.body) == {"error": "query is required"}

    @pytest.mark.parametrize(
        "payload",
        [
            pytest.param(
                {
                    "query": "q"
                    * news_flask_api.NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH,
                    "name": None,
                    "folder_id": None,
                    "model_provider": None,
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES
                    ),
                    "search_iterations": 1,
                    "questions_per_iteration": 1,
                },
                id="minimums-and-null-optionals",
            ),
            pytest.param(
                {
                    "query": "q",
                    "name": "Digest",
                    "folder_id": "folder-1",
                    "model_provider": "OLLAMA",
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES
                    ),
                    "search_iterations": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS
                    ),
                    "questions_per_iteration": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION
                    ),
                },
                id="maximums-and-string-optionals",
            ),
        ],
    )
    def test_inclusive_boundaries_and_nullable_strings_are_accepted(
        self, payload
    ):
        assert (
            news_flask_api._validate_subscription_payload(
                payload, require_query=True
            )
            is None
        )

    def test_questions_per_iteration_public_limit_is_the_forms_20(self):
        """Cross-layer pin of the questions-per-iteration boundary.

        The shipped subscription form advertises ``max="20"``, so the API
        must accept 20 or form submissions and previously saved values
        11-20 get a 400 (#6066). Every other expectation in this file is
        derived from the server constant and would silently follow it
        anywhere, so both layers are pinned to the literal number here.
        """
        template = (
            Path(news_flask_api.__file__).parents[1]
            / "templates"
            / "pages"
            / "news-subscription-form.html"
        )
        form_max = re.search(
            r'id="subscription-questions"[^>]*max="(\d+)"',
            template.read_text(encoding="utf-8"),
        )
        assert form_max is not None, (
            "the form no longer declares a questions-per-iteration max"
        )
        assert int(form_max.group(1)) == 20
        assert (
            news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION == 20
        )
        assert (
            news_flask_api._validate_subscription_payload(
                {"questions_per_iteration": 20}
            )
            is None
        )
        assert (
            news_flask_api._validate_subscription_payload(
                {"questions_per_iteration": 21}
            ).status_code
            == 400
        )


# ===========================================================================
# POST /news/api/vote -- the route had no Python test at all
# ===========================================================================


class TestVote:
    def test_success_forwards_card_id_user_and_vote(self, authenticated_client):
        """Positive control for every rejection below."""
        username = _username(authenticated_client)
        with patch(f"{API}.submit_feedback", return_value={"ok": True}) as spy:
            resp = authenticated_client.post(
                VOTE, json={"card_id": CARD, "vote": "up"}
            )

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"ok": True}
        assert spy.call_args.kwargs == {
            "card_id": CARD,
            "user_id": username,
            "vote": "up",
        }

    @pytest.mark.parametrize(
        "body",
        [
            {"vote": "up"},
            {"card_id": CARD},
            {"card_id": "", "vote": "up"},
            {"card_id": CARD, "vote": ""},
            {},
        ],
        ids=["no_card_id", "no_vote", "empty_card_id", "empty_vote", "neither"],
    )
    def test_missing_field_is_a_400_before_the_service(
        self, authenticated_client, body
    ):
        with patch(f"{API}.submit_feedback") as spy:
            resp = authenticated_client.post(VOTE, json=body)

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "card_id and vote are required"}
        assert not spy.called, (
            "the required-field guard let an incomplete vote reach the service"
        )

    def test_value_error_saying_not_found_is_a_404(self, authenticated_client):
        with patch(
            f"{API}.submit_feedback",
            side_effect=ValueError("Card card-42 not found in /db/alice.db"),
        ):
            resp = authenticated_client.post(
                VOTE, json={"card_id": CARD, "vote": "up"}
            )

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json() == {"error": "Resource not found"}
        assert "alice.db" not in resp.text

    def test_any_other_value_error_is_a_scrubbed_400(
        self, authenticated_client
    ):
        """Control for the test above: the 404 arm must be selected by the
        message, not returned for every ValueError."""
        with patch(
            f"{API}.submit_feedback",
            side_effect=ValueError("Invalid vote type: sideways"),
        ):
            resp = authenticated_client.post(
                VOTE, json={"card_id": CARD, "vote": "sideways"}
            )

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "Invalid input provided"}
        assert "sideways" not in resp.text

    def test_a_non_value_error_is_a_scrubbed_500(self, authenticated_client):
        with patch(
            f"{API}.submit_feedback",
            side_effect=RuntimeError("sqlcipher: file is not a database"),
        ):
            resp = authenticated_client.post(
                VOTE, json={"card_id": CARD, "vote": "up"}
            )

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {
            "error": "An error occurred while submitting vote"
        }
        assert "sqlcipher" not in resp.text


# ===========================================================================
# POST /news/api/feedback/batch -- also untested on the branch
# ===========================================================================


class TestBatchFeedback:
    def test_votes_are_returned_unchanged(self, authenticated_client):
        username = _username(authenticated_client)
        votes = {"c1": "up", "c2": "down"}
        with patch(
            f"{API}.get_votes_for_cards", return_value={"votes": votes}
        ) as spy:
            resp = authenticated_client.post(
                BATCH, json={"card_ids": ["c1", "c2"]}
            )

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"votes": votes}
        assert spy.call_args.kwargs == {
            "card_ids": ["c1", "c2"],
            "user_id": username,
        }

    @pytest.mark.parametrize(
        "card_ids",
        [
            "c1",
            {"c1": "up"},
            1,
            True,
            None,
            [1],
            [""],
            ["   "],
            ["c1", {}],
        ],
        ids=[
            "string",
            "object",
            "integer",
            "boolean",
            "null",
            "non-string-item",
            "empty-item",
            "blank-item",
            "mixed-items",
        ],
    )
    def test_malformed_card_ids_are_rejected_before_the_service(
        self, authenticated_client, card_ids
    ):
        with patch(f"{API}.get_votes_for_cards") as spy:
            resp = authenticated_client.post(BATCH, json={"card_ids": card_ids})

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {
            "error": "card_ids must be a list of non-empty strings"
        }
        spy.assert_not_called()

    def test_batch_limit_is_inclusive(self, authenticated_client):
        card_ids = [
            f"card-{index}"
            for index in range(news_flask_api.NEWS_BATCH_FEEDBACK_MAX_CARD_IDS)
        ]
        with patch(
            f"{API}.get_votes_for_cards", return_value={"votes": {}}
        ) as spy:
            resp = authenticated_client.post(BATCH, json={"card_ids": card_ids})

        assert resp.status_code == 200, resp.text[:300]
        assert spy.call_args.kwargs["card_ids"] == card_ids

    def test_oversized_batch_is_rejected_before_the_service(
        self, authenticated_client
    ):
        card_ids = [
            f"card-{index}"
            for index in range(
                news_flask_api.NEWS_BATCH_FEEDBACK_MAX_CARD_IDS + 1
            )
        ]
        with patch(f"{API}.get_votes_for_cards") as spy:
            resp = authenticated_client.post(BATCH, json={"card_ids": card_ids})

        assert resp.status_code == 400, resp.text[:300]
        assert "exceeds maximum" in resp.json()["error"]
        spy.assert_not_called()

    @pytest.mark.parametrize(
        "body", [{"card_ids": []}, {}], ids=["empty_list", "absent"]
    )
    def test_no_card_ids_short_circuits_without_touching_the_service(
        self, authenticated_client, body
    ):
        """The short-circuit is the point: an empty batch must not open a
        per-user encrypted DB session just to return nothing."""
        with patch(f"{API}.get_votes_for_cards") as spy:
            resp = authenticated_client.post(BATCH, json=body)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"votes": {}}
        assert not spy.called, (
            "an empty card_ids batch still called get_votes_for_cards"
        )

    def test_value_error_saying_not_found_is_a_404(self, authenticated_client):
        with patch(
            f"{API}.get_votes_for_cards",
            side_effect=ValueError("card c9 not found"),
        ):
            resp = authenticated_client.post(BATCH, json={"card_ids": ["c9"]})

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json() == {"error": "Resource not found"}

    def test_a_non_value_error_is_a_scrubbed_500(self, authenticated_client):
        with patch(
            f"{API}.get_votes_for_cards",
            side_effect=RuntimeError("/home/victim/.config/ldr/victim.db"),
        ):
            resp = authenticated_client.post(BATCH, json={"card_ids": ["c1"]})

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {"error": "An error occurred while getting votes"}
        assert "victim" not in resp.text


# ===========================================================================
# POST /news/api/feedback/{card_id}
# ===========================================================================


class TestSubmitFeedback:
    def test_success_forwards_card_id_from_the_path(self, authenticated_client):
        username = _username(authenticated_client)
        with patch(f"{API}.submit_feedback", return_value={"ok": 1}) as spy:
            resp = authenticated_client.post(_feedback(), json={"vote": "down"})

        assert resp.status_code == 200, resp.text[:300]
        assert spy.call_args.kwargs == {
            "card_id": CARD,
            "user_id": username,
            "vote": "down",
        }

    @pytest.mark.parametrize(
        "body",
        [{}, {"vote": ""}, {"vote": None}],
        ids=["absent", "empty", "null"],
    )
    def test_missing_vote_is_a_400_before_the_service(
        self, authenticated_client, body
    ):
        with patch(f"{API}.submit_feedback") as spy:
            resp = authenticated_client.post(_feedback(), json=body)

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "vote is required"}
        assert not spy.called

    def test_value_error_saying_not_found_is_a_404(self, authenticated_client):
        with patch(
            f"{API}.submit_feedback", side_effect=ValueError("Card not found")
        ):
            resp = authenticated_client.post(_feedback(), json={"vote": "up"})

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json() == {"error": "Resource not found"}

    def test_value_error_saying_must_be_is_a_distinct_400(
        self, authenticated_client
    ):
        """``"must be"`` gets its own body -- ``{"error": "Invalid input
        value"}`` -- not the scrubbed generic 400 below it. Two arms that
        return the same status code but different bodies collapse into one
        the moment nothing pins the body."""
        with patch(
            f"{API}.submit_feedback",
            side_effect=ValueError("vote must be 'up' or 'down'"),
        ):
            resp = authenticated_client.post(_feedback(), json={"vote": "x"})

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "Invalid input value"}

    def test_any_other_value_error_is_the_scrubbed_400(
        self, authenticated_client
    ):
        with patch(
            f"{API}.submit_feedback",
            side_effect=ValueError("Invalid vote type: sideways"),
        ):
            resp = authenticated_client.post(_feedback(), json={"vote": "x"})

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "Invalid input provided"}

    def test_a_non_value_error_is_a_scrubbed_500(self, authenticated_client):
        with patch(
            f"{API}.submit_feedback",
            side_effect=RuntimeError("postgresql://ldr:[email protected]/ldr"),
        ):
            resp = authenticated_client.post(_feedback(), json={"vote": "up"})

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {
            "error": "An error occurred while submitting feedback"
        }
        assert "S3cr3t" not in resp.text


# ===========================================================================
# POST /news/api/research/{card_id}
# ===========================================================================


class TestResearchNewsItem:
    """``api.research_news_item`` always raises NotImplementedException today,
    so the 501 is what ``test_full_surface_smoke.py`` sees. These pin the
    argument forwarding and the generic-error arm underneath it, which that
    501 hides completely."""

    def test_depth_from_the_body_is_forwarded(self, authenticated_client):
        with patch(f"{API}.research_news_item", return_value={"ok": 1}) as spy:
            resp = authenticated_client.post(
                _research(), json={"depth": "detailed"}
            )

        assert resp.status_code == 200, resp.text[:300]
        spy.assert_called_once_with(CARD, "detailed")

    def test_absent_depth_defaults_to_quick(self, authenticated_client):
        with patch(f"{API}.research_news_item", return_value={"ok": 1}) as spy:
            resp = authenticated_client.post(_research(), json={})

        assert resp.status_code == 200, resp.text[:300]
        spy.assert_called_once_with(CARD, "quick")

    def test_a_json_null_body_is_treated_as_an_empty_body(
        self, authenticated_client
    ):
        """The handler's ``await request.json() or {}`` exists so a client
        that sends a literal ``null`` gets the "quick" default rather than
        an AttributeError -> scrubbed 500. This is the ONE route in the
        module whose body is optional."""
        with patch(f"{API}.research_news_item", return_value={"ok": 1}) as spy:
            resp = authenticated_client.post(
                _research(),
                content=b"null",
                headers={"Content-Type": "application/json"},
            )

        assert resp.status_code == 200, resp.text[:300]
        spy.assert_called_once_with(CARD, "quick")

    def test_a_generic_failure_is_a_scrubbed_500(self, authenticated_client):
        with patch(
            f"{API}.research_news_item",
            side_effect=RuntimeError("LLM unavailable at 10.1.2.3:11434"),
        ):
            resp = authenticated_client.post(_research(), json={})

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {
            "error": "An error occurred while researching news item"
        }
        assert "10.1.2.3" not in resp.text


# ===========================================================================
# POST /news/api/subscribe -- the guards ahead of the service call
# ===========================================================================


class TestCreateSubscription:
    @pytest.mark.parametrize(
        "body",
        [
            {},
            {"query": ""},
            {"query": "   "},
            {"query": None},
            {"name": "x"},
        ],
        ids=["empty", "empty-query", "blank-query", "null-query", "no-query"],
    )
    def test_missing_query_is_a_400_before_the_service(
        self, authenticated_client, body
    ):
        with patch(f"{API}.create_subscription") as spy:
            resp = authenticated_client.post(SUBSCRIBE, json=body)

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "query is required"}
        assert not spy.called

    @pytest.mark.parametrize(
        "is_active",
        ["false", "true", 0, 1, None, [], {}],
        ids=[
            "string-false",
            "string-true",
            "zero",
            "one",
            "null",
            "list",
            "object",
        ],
    )
    def test_non_boolean_is_active_is_rejected_before_the_service(
        self, authenticated_client, is_active
    ):
        with patch(f"{API}.create_subscription") as spy:
            resp = authenticated_client.post(
                SUBSCRIBE,
                json={"query": "fusion energy", "is_active": is_active},
            )

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "is_active must be a boolean"}
        spy.assert_not_called()

    @pytest.mark.parametrize(
        "field,value,message", SUBSCRIPTION_ROUTE_REJECTION_CASES
    )
    def test_shared_fields_are_rejected_before_endpoint_or_service_work(
        self, authenticated_client, field, value, message
    ):
        payload = {"query": "fusion energy", field: value}
        with (
            patch.object(
                news_flask_api, "_reject_custom_endpoint_async"
            ) as endpoint_check,
            patch(f"{API}.create_subscription") as service,
        ):
            resp = authenticated_client.post(SUBSCRIBE, json=payload)

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": message}
        endpoint_check.assert_not_awaited()
        service.assert_not_called()

    @pytest.mark.parametrize(
        "controls",
        [
            pytest.param(
                {
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES
                    ),
                    "search_iterations": 1,
                    "questions_per_iteration": 1,
                },
                id="minimums",
            ),
            pytest.param(
                {
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES
                    ),
                    "search_iterations": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS
                    ),
                    "questions_per_iteration": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION
                    ),
                },
                id="maximums",
            ),
        ],
    )
    def test_numeric_boundaries_reach_the_service_exactly(
        self, authenticated_client, controls
    ):
        with (
            patch.object(
                news_flask_api,
                "_reject_custom_endpoint_async",
                return_value=None,
            ),
            patch(
                f"{API}.create_subscription", return_value={"id": "s1"}
            ) as service,
        ):
            resp = authenticated_client.post(
                SUBSCRIBE, json={"query": "fusion energy", **controls}
            )

        assert resp.status_code == 200, resp.text[:300]
        for field, value in controls.items():
            assert service.call_args.kwargs[field] == value

    def test_folder_name_and_is_active_reach_the_service(
        self, authenticated_client
    ):
        """Regression #4489: these three body fields were silently dropped,
        so every subscription landed unfoldered, unnamed and active."""
        folder_id = str(uuid.uuid4())
        with patch(
            f"{API}.create_subscription", return_value={"id": "s1"}
        ) as spy:
            resp = authenticated_client.post(
                SUBSCRIBE,
                json={
                    "query": "fusion energy",
                    "folder_id": folder_id,
                    "name": "My subscription",
                    "is_active": False,
                },
            )

        assert resp.status_code == 200, resp.text[:300]
        kwargs = spy.call_args.kwargs
        assert kwargs["folder_id"] == folder_id
        assert kwargs["name"] == "My subscription"
        assert kwargs["is_active"] is False

    def test_a_bare_value_error_is_a_scrubbed_400(self, authenticated_client):
        """``api.create_subscription`` normally raises a NewsAPIException
        (covered in test_news_subscribe_exception_contract.py). The plain
        ValueError arm underneath it is what a validation helper raising
        directly would hit."""
        with patch(
            f"{API}.create_subscription",
            side_effect=ValueError("refresh_minutes must be >= 5 (got 0)"),
        ):
            resp = authenticated_client.post(SUBSCRIBE, json={"query": "q"})

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json()["error"] == "Invalid input provided"

    def test_a_generic_failure_is_a_scrubbed_500(self, authenticated_client):
        with patch(
            f"{API}.create_subscription",
            side_effect=RuntimeError("/home/victim/.config/ldr/victim.db"),
        ):
            resp = authenticated_client.post(SUBSCRIBE, json={"query": "q"})

        assert resp.status_code == 500, resp.text[:300]
        assert "victim" not in resp.text


# ===========================================================================
# GET /news/api/subscriptions/{id} -- the "null"/"undefined" string guard
# ===========================================================================


class TestSubscriptionIdSentinels:
    """A separate guard from ``_is_valid_uuid``.

    The UUID check is wired to the feed and history routes only (pinned in
    ``test_news_scheduler_isolation_fastapi.py``); this route instead
    special-cases the two literal strings a JS client sends when it
    interpolates a missing id. Nothing on the branch covers it, and a
    ``null`` reaching ``api.get_subscription`` is a LIKE-pattern query on
    an attacker-shaped string.
    """

    @pytest.mark.parametrize("sentinel", ["null", "undefined"])
    def test_sentinel_id_is_rejected_before_the_service(
        self, authenticated_client, sentinel
    ):
        with patch(f"{API}.get_subscription") as spy:
            resp = authenticated_client.get(
                f"/news/api/subscriptions/{sentinel}"
            )

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "Invalid subscription ID"}
        assert not spy.called, f"{sentinel!r} reached api.get_subscription"

    def test_a_real_id_is_not_rejected(self, authenticated_client):
        """Positive control -- otherwise a handler that 400s on everything
        would satisfy both rows above."""
        sub_id = str(uuid.uuid4())
        with patch(
            f"{API}.get_subscription", return_value={"id": sub_id}
        ) as spy:
            resp = authenticated_client.get(f"/news/api/subscriptions/{sub_id}")

        assert resp.status_code == 200, resp.text[:300]
        assert spy.called


# ===========================================================================
# GET /news/api/subscriptions/current -- the error-dict branch
# ===========================================================================


def test_an_error_dict_from_get_subscriptions_is_a_scrubbed_500(
    authenticated_client,
):
    """A distinct branch from the raised-DatabaseAccessException path that
    ``test_news_subscribe_exception_contract.py`` covers: here the service
    RETURNS ``{"error": ...}``, and the handler must substitute its own
    fixed message rather than reflecting the service's."""
    with patch(
        f"{API}.get_subscriptions",
        return_value={"error": "no such table: news_subscriptions in alice.db"},
    ):
        resp = authenticated_client.get(CURRENT)

    assert resp.status_code == 500, resp.text[:300]
    assert resp.json() == {"error": "Failed to retrieve subscriptions"}
    assert "alice.db" not in resp.text
    assert "no such table" not in resp.text


# ===========================================================================
# Subscription folders -- GET / duplicate POST / PUT success / DELETE
# ===========================================================================


def _folder(folder_id="f1", name="Tech"):
    folder = MagicMock(name=f"folder-{folder_id}")
    folder.to_dict.return_value = {"id": folder_id, "name": name}
    return folder


class _FakeSession:
    """Enough of a Session for the folder handlers' direct queries."""

    def __init__(self, existing=None):
        self._existing = existing

    def query(self, *args, **kwargs):
        return self

    def filter_by(self, **kwargs):
        return self

    def first(self):
        return self._existing


def _patched_folders(manager, existing=None):
    """Patch the two boundaries the folder handlers touch."""
    from contextlib import contextmanager, ExitStack

    @contextmanager
    def fake_session(*args, **kwargs):
        yield _FakeSession(existing)

    stack = ExitStack()
    stack.enter_context(patch(SESSION_CTX, side_effect=fake_session))
    stack.enter_context(patch(FOLDER_MANAGER, return_value=manager))
    return stack


class TestFolders:
    def test_get_returns_a_bare_array_of_folder_dicts(
        self, authenticated_client
    ):
        """The response is a JSON ARRAY, not an object with a "folders" key.
        The subscriptions UI indexes it positionally."""
        manager = MagicMock()
        manager.get_user_folders.return_value = [_folder("f1", "Tech")]

        with _patched_folders(manager):
            resp = authenticated_client.get(FOLDERS)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == [{"id": "f1", "name": "Tech"}]
        manager.get_user_folders.assert_called_once_with(
            _username(authenticated_client)
        )

    def test_a_duplicate_name_is_a_409_and_creates_nothing(
        self, authenticated_client
    ):
        manager = MagicMock()

        with _patched_folders(manager, existing=_folder("f1", "Tech")):
            resp = authenticated_client.post(FOLDERS, json={"name": "Tech"})

        assert resp.status_code == 409, resp.text[:300]
        assert resp.json() == {"error": "Folder already exists"}
        assert not manager.create_folder.called, (
            "the duplicate-name guard still created a second folder"
        )

    def test_a_fresh_name_is_created_with_201(self, authenticated_client):
        """Positive control for the 409 above."""
        manager = MagicMock()
        manager.create_folder.return_value = _folder("f2", "Science")

        with _patched_folders(manager, existing=None):
            resp = authenticated_client.post(
                FOLDERS, json={"name": "Science", "description": "d"}
            )

        assert resp.status_code == 201, resp.text[:300]
        assert resp.json() == {"id": "f2", "name": "Science"}
        manager.create_folder.assert_called_once_with(
            name="Science", description="d"
        )

    def test_update_returns_the_updated_folder(self, authenticated_client):
        """Only the 404 arm of this route is covered on the branch
        (test_scheduler_control_and_news_limits_fastapi.py drives it against
        folder 999999); the success arm returns ``folder.to_dict()``."""
        manager = MagicMock()
        manager.update_folder.return_value = _folder("f1", "Renamed")

        with _patched_folders(manager):
            resp = authenticated_client.put(
                f"{FOLDERS}/f1", json={"name": "Renamed"}
            )

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"id": "f1", "name": "Renamed"}
        manager.update_folder.assert_called_once_with("f1", name="Renamed")

    def test_delete_of_an_unknown_folder_is_a_404(self, authenticated_client):
        manager = MagicMock()
        manager.delete_folder.return_value = False

        with _patched_folders(manager):
            resp = authenticated_client.delete(f"{FOLDERS}/999999")

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json() == {"error": "Folder not found"}

    def test_delete_success_reports_deleted(self, authenticated_client):
        manager = MagicMock()
        manager.delete_folder.return_value = True

        with _patched_folders(manager):
            resp = authenticated_client.delete(f"{FOLDERS}/f1")

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"status": "deleted"}

    def test_delete_forwards_move_to(self, authenticated_client):
        """``?move_to=`` is where the folder's subscriptions are re-homed.
        Dropping it orphans every subscription in the deleted folder, and
        the response is a 200 either way -- invisible without this."""
        manager = MagicMock()
        manager.delete_folder.return_value = True

        with _patched_folders(manager):
            resp = authenticated_client.delete(
                f"{FOLDERS}/f1", params={"move_to": "f2"}
            )

        assert resp.status_code == 200, resp.text[:300]
        manager.delete_folder.assert_called_once_with("f1", "f2")

    def test_delete_without_move_to_passes_none(self, authenticated_client):
        manager = MagicMock()
        manager.delete_folder.return_value = True

        with _patched_folders(manager):
            authenticated_client.delete(f"{FOLDERS}/f1")

        manager.delete_folder.assert_called_once_with("f1", None)


def test_subscription_stats_failure_is_a_scrubbed_500(authenticated_client):
    """Its sibling ``/subscriptions/organized`` shipped a real 500 from
    re-serialising an already-JSON-friendly value; the passthrough success is
    pinned in test_news_api_contract_restored.py, the failure arm was not."""
    with patch(SESSION_CTX, side_effect=RuntimeError("DB locked at /db/x.db")):
        resp = authenticated_client.get(SUB_STATS)

    assert resp.status_code == 500, resp.text[:300]
    assert resp.json() == {"error": "An error occurred while getting stats"}
    assert "/db/x.db" not in resp.text


# ===========================================================================
# Scheduler routes
# ===========================================================================


def _scheduler_mock(is_running=True, user_sessions=None, jobs=()):
    scheduler = MagicMock(name="background_job_scheduler")
    scheduler.is_running = is_running
    scheduler.user_sessions = user_sessions if user_sessions is not None else {}
    scheduler.config = {}
    scheduler.scheduler.get_jobs.return_value = list(jobs)
    scheduler._credential_store.retrieve.return_value = None
    return scheduler


def _job(job_id, owner):
    job = MagicMock(name=f"job-{job_id}")
    job.id = job_id
    job.name = job_id
    job.args = [owner]
    job.next_run_time = None
    return job


class TestSchedulerReadEndpointsHaveNoSideEffects:
    def test_stats_never_schedules_anything(
        self, authenticated_client, monkeypatch
    ):
        """A GET that re-registers the caller's subscriptions on the global
        APScheduler is a write disguised as a read -- and one that a status
        page polling every few seconds would perform continuously."""
        monkeypatch.delenv(GATE_ENV, raising=False)
        scheduler = _scheduler_mock()

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATS)

        assert resp.status_code == 200, resp.text[:300]
        assert not scheduler._schedule_user_subscriptions.called
        assert not scheduler.start.called
        assert not scheduler.scheduler.add_job.called

    def test_status_never_schedules_anything(
        self, authenticated_client, monkeypatch
    ):
        monkeypatch.delenv(GATE_ENV, raising=False)
        scheduler = _scheduler_mock()

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATUS)

        assert resp.status_code == 200, resp.text[:300]
        assert not scheduler._schedule_user_subscriptions.called
        assert not scheduler.scheduler.add_job.called


class TestSchedulerStatusDegradation:
    def test_an_apscheduler_failure_reports_zero_jobs_not_a_500(
        self, authenticated_client, monkeypatch
    ):
        """``get_jobs()`` raising (a locked job store) must degrade the job
        count to 0, not take down the whole status page."""
        monkeypatch.delenv(GATE_ENV, raising=False)
        scheduler = _scheduler_mock()
        scheduler.scheduler.get_jobs.side_effect = RuntimeError("lock error")

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATUS)

        assert resp.status_code == 200, resp.text[:300]
        body = resp.json()
        assert body["apscheduler_job_count"] == 0
        assert "lock error" not in resp.text

    def test_the_happy_path_still_counts_jobs(
        self, authenticated_client, monkeypatch
    ):
        """Control: the degradation above must not be indistinguishable from
        'this endpoint always reports zero'."""
        monkeypatch.delenv(GATE_ENV, raising=False)
        username = _username(authenticated_client)
        scheduler = _scheduler_mock(
            user_sessions={username: {"scheduled_jobs": {"j1"}}},
            jobs=[_job("j1", username)],
        )

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATUS)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json()["apscheduler_job_count"] == 1


class TestSchedulerStatsSerialisation:
    def test_a_session_with_no_last_activity_serialises_as_null(
        self, authenticated_client, monkeypatch
    ):
        """``last_activity`` is None for a session that has never been used.
        Dropping the ternary makes this an AttributeError -> scrubbed 500,
        i.e. the stats page dies for exactly the users it should show."""
        monkeypatch.delenv(GATE_ENV, raising=False)
        username = _username(authenticated_client)
        scheduler = _scheduler_mock(
            user_sessions={
                username: {"last_activity": None, "scheduled_jobs": set()}
            }
        )

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATS)

        assert resp.status_code == 200, resp.text[:300]
        session = resp.json()["user_sessions"][username]
        assert session["last_activity"] is None
        assert session["scheduled_jobs_count"] == 0


class TestSchedulerStartStopIdempotence:
    """The four no-op / not-found arms of start and stop.

    ``test_scheduler_control_and_news_limits_fastapi.py`` only ever drives
    start against a stopped scheduler and stop against a running one, so
    every branch below is currently unreachable by any test.
    """

    def test_start_on_a_running_scheduler_is_a_no_op_200(
        self, authenticated_client, monkeypatch
    ):
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _scheduler_mock(is_running=True)

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.post(START)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"message": "Scheduler is already running"}
        assert not scheduler.start.called, (
            "starting an already-running scheduler restarted it"
        )

    def test_start_on_a_stopped_scheduler_starts_it(
        self, authenticated_client, monkeypatch
    ):
        """Control for the no-op above."""
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _scheduler_mock(is_running=False)

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.post(START)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json()["status"] == "success"
        scheduler.start.assert_called_once()

    def test_stop_on_a_stopped_scheduler_is_a_no_op_200(
        self, authenticated_client, monkeypatch
    ):
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _scheduler_mock(is_running=False)

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.post(STOP)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {"message": "Scheduler is not running"}
        assert not scheduler.stop.called

    def test_stop_with_no_scheduler_instance_is_a_404(
        self, authenticated_client, monkeypatch
    ):
        """The port changed the mechanism here -- Flask read
        ``current_app.news_scheduler``, FastAPI checks the singleton getter's
        truthiness -- and nothing checks the new one."""
        monkeypatch.setenv(GATE_ENV, "true")

        with patch(SCHEDULER_TARGET, return_value=None):
            resp = authenticated_client.post(STOP)

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json() == {"message": "No scheduler instance found"}


@pytest.mark.parametrize("path", [START, STOP])
def test_the_403_body_names_no_environment_variable(
    authenticated_client, monkeypatch, path
):
    """The gate's 403 tells the caller to contact an administrator. Naming
    ``LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL`` in it would hand an
    unprivileged user the exact knob to ask for -- and disclose the
    deployment's configuration surface."""
    monkeypatch.delenv(GATE_ENV, raising=False)

    with patch(SCHEDULER_TARGET) as get_sched:
        resp = authenticated_client.post(path)

    assert resp.status_code == 403, resp.text[:300]
    detail = resp.json()["detail"]
    assert "LDR_NEWS_SCHEDULER" not in detail
    assert "allow_api_control" not in detail
    assert "administrator" in detail.lower()
    assert not get_sched.called


def test_the_gate_fails_closed_when_the_session_has_no_username(monkeypatch):
    """``require_scheduler_control`` indexes ``session["username"]`` rather
    than defaulting it, so a broken dependency order raises instead of
    writing an unattributable "unknown" audit line (#5549). A
    ``.get(..., "unknown")`` "fix" silently merges every blocked privileged
    attempt into one entry."""
    monkeypatch.delenv(GATE_ENV, raising=False)

    request = MagicMock()
    request.session = {}
    request.client.host = "10.0.0.9"

    with pytest.raises(KeyError):
        news_flask_api.require_scheduler_control(request)


# ===========================================================================
# PUT /news/api/subscriptions/{id} -- the whole field_mapping
# ===========================================================================


class TestUpdateSubscriptionFieldMapping:
    """``test_news_router_contracts.py`` pins ONE row of the table
    (``name`` -> ``name``, an identity mapping) and that unmapped keys are
    dropped. The three real renames -- ``query`` -> ``query_or_topic``,
    ``refresh_minutes`` -> ``refresh_interval_minutes``, and the nine
    identity rows -- are unpinned, so a dropped or mistyped row silently
    stops applying that field. The API answers 200 either way.
    """

    # request field -> (storage field, value)
    FIELDS = [
        ("query", "query_or_topic", "fusion energy"),
        ("name", "name", "My digest"),
        ("refresh_minutes", "refresh_interval_minutes", 30),
        ("is_active", "is_active", False),
        ("folder_id", "folder_id", "folder-9"),
        ("model_provider", "model_provider", "openai"),
        ("model", "model", "gpt-4"),
        ("search_strategy", "search_strategy", "focused-iteration"),
        ("search_engine", "search_engine", "bing"),
        ("search_iterations", "search_iterations", 4),
        ("questions_per_iteration", "questions_per_iteration", 7),
    ]

    def _update(self, client, body):
        sub_id = str(uuid.uuid4())
        with patch(
            f"{API}.update_subscription", return_value={"status": "success"}
        ) as spy:
            resp = client.put(f"/news/api/subscriptions/{sub_id}", json=body)
        assert resp.status_code == 200, resp.text[:300]
        return spy.call_args.args[1]

    @pytest.mark.parametrize(
        "request_field,storage_field,value",
        FIELDS,
        ids=[f for f, _, _ in FIELDS],
    )
    def test_each_field_lands_under_its_storage_name(
        self, authenticated_client, request_field, storage_field, value
    ):
        update_data = self._update(authenticated_client, {request_field: value})

        assert update_data == {storage_field: value}, (
            f"{request_field!r} did not map to {storage_field!r}"
        )

    def test_the_whole_table_applies_at_once(self, authenticated_client):
        body = {f: v for f, _, v in self.FIELDS}
        expected = {s: v for _, s, v in self.FIELDS}

        assert self._update(authenticated_client, body) == expected

    def test_an_unmapped_field_is_dropped(self, authenticated_client):
        """The mapping is an allow-list, not a passthrough: it is the only
        thing stopping a body key from being written to an arbitrary
        column."""
        update_data = self._update(
            authenticated_client, {"name": "n", "user_id": "mallory", "id": "x"}
        )

        assert update_data == {"name": "n"}

    def test_absent_fields_are_not_written_as_none(self, authenticated_client):
        """``if request_field in data`` rather than ``data.get(...)``: an
        absent field must be left alone, not overwritten with None."""
        assert self._update(authenticated_client, {"name": "n"}) == {
            "name": "n"
        }

    @pytest.mark.parametrize(
        "is_active",
        ["false", "true", 0, 1, None, [], {}],
        ids=[
            "string-false",
            "string-true",
            "zero",
            "one",
            "null",
            "list",
            "object",
        ],
    )
    def test_non_boolean_is_active_is_rejected_before_the_service(
        self, authenticated_client, is_active
    ):
        sub_id = str(uuid.uuid4())
        with patch(f"{API}.update_subscription") as spy:
            resp = authenticated_client.put(
                f"/news/api/subscriptions/{sub_id}",
                json={"is_active": is_active},
            )

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "is_active must be a boolean"}
        spy.assert_not_called()

    @pytest.mark.parametrize(
        "field,value,message", SUBSCRIPTION_ROUTE_REJECTION_CASES
    )
    def test_shared_fields_are_rejected_before_endpoint_or_service_work(
        self, authenticated_client, field, value, message
    ):
        sub_id = str(uuid.uuid4())
        with (
            patch.object(
                news_flask_api, "_reject_custom_endpoint_async"
            ) as endpoint_check,
            patch(f"{API}.update_subscription") as service,
        ):
            resp = authenticated_client.put(
                f"/news/api/subscriptions/{sub_id}", json={field: value}
            )

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": message}
        endpoint_check.assert_not_awaited()
        service.assert_not_called()

    @pytest.mark.parametrize(
        "controls",
        [
            pytest.param(
                {
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES
                    ),
                    "search_iterations": 1,
                    "questions_per_iteration": 1,
                },
                id="minimums",
            ),
            pytest.param(
                {
                    "refresh_minutes": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES
                    ),
                    "search_iterations": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS
                    ),
                    "questions_per_iteration": (
                        news_flask_api.NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION
                    ),
                },
                id="maximums",
            ),
        ],
    )
    def test_numeric_boundaries_map_to_storage_exactly(
        self, authenticated_client, controls
    ):
        expected = {
            "refresh_interval_minutes": controls["refresh_minutes"],
            "search_iterations": controls["search_iterations"],
            "questions_per_iteration": controls["questions_per_iteration"],
        }

        assert self._update(authenticated_client, controls) == expected


# ===========================================================================
# POST /news/api/subscribe -- the rest of the forwarded body
# ===========================================================================


def test_every_subscription_field_reaches_the_service(authenticated_client):
    """13 body fields are forwarded. ``test_news_router_contracts.py`` pins
    ``user_id``; ``folder_id``/``name``/``is_active`` are pinned above as
    regression #4489. The remainder -- the model and search configuration a
    subscription runs with -- had nothing."""
    body = {
        "query": "fusion energy",
        "subscription_type": "topic",
        "refresh_minutes": 120,
        "model_provider": "openai",
        "model": "gpt-4",
        "search_strategy": "focused-iteration",
        "search_engine": "bing",
        "search_iterations": 4,
        "questions_per_iteration": 7,
    }
    with patch(f"{API}.create_subscription", return_value={"id": "s1"}) as spy:
        resp = authenticated_client.post(SUBSCRIBE, json=body)

    assert resp.status_code == 200, resp.text[:300]
    kwargs = spy.call_args.kwargs
    assert kwargs["query"] == "fusion energy"
    assert kwargs["subscription_type"] == "topic"
    assert kwargs["refresh_minutes"] == 120
    assert kwargs["model_provider"] == "openai"
    assert kwargs["model"] == "gpt-4"
    assert kwargs["search_strategy"] == "focused-iteration"
    assert kwargs["search_engine"] == "bing"
    assert kwargs["search_iterations"] == 4
    assert kwargs["questions_per_iteration"] == 7


# ===========================================================================
# POST /news/api/preferences -- positional contract
# ===========================================================================


class TestSavePreferences:
    """``api.save_news_preferences`` always raises NotImplementedException
    today, so ``test_full_surface_smoke.py`` only ever sees the 501. With
    the service mocked, the call itself is observable: the two arguments
    are POSITIONAL, so swapping them sends the preferences dict as the
    user id."""

    def test_user_id_and_preferences_are_passed_positionally(
        self, authenticated_client
    ):
        username = _username(authenticated_client)
        with patch(
            f"{API}.save_news_preferences", return_value={"ok": True}
        ) as spy:
            resp = authenticated_client.post(
                "/news/api/preferences",
                json={"preferences": {"theme": "dark"}},
            )

        assert resp.status_code == 200, resp.text[:300]
        spy.assert_called_once_with(username, {"theme": "dark"})

    def test_an_absent_preferences_key_defaults_to_an_empty_dict(
        self, authenticated_client
    ):
        """Not None: the service indexes it."""
        username = _username(authenticated_client)
        with patch(
            f"{API}.save_news_preferences", return_value={"ok": True}
        ) as spy:
            resp = authenticated_client.post("/news/api/preferences", json={})

        assert resp.status_code == 200, resp.text[:300]
        spy.assert_called_once_with(username, {})


# ===========================================================================
# GET|DELETE /news/api/search-history -- neither had any Python test
# ===========================================================================


class TestSearchHistory:
    """These run against the caller's REAL per-user encrypted database --
    ``authenticated_client`` provides one -- because what is being pinned
    is that rows are actually written, ordered, capped and deleted."""

    HISTORY = "/news/api/search-history"

    def test_a_saved_search_comes_back(self, authenticated_client):
        added = authenticated_client.post(
            self.HISTORY,
            json={"query": "fusion energy", "type": "filter", "resultCount": 3},
        )
        assert added.status_code == 200, added.text[:300]
        assert added.json()["status"] == "success"

        listed = authenticated_client.get(self.HISTORY)
        assert listed.status_code == 200, listed.text[:300]
        entries = listed.json()["search_history"]
        assert [e["query"] for e in entries] == ["fusion energy"]
        # The wire keys are camelCase/short -- ``type``/``resultCount``, not
        # the column names ``search_type``/``result_count``. news.js indexes
        # them by these names.
        assert entries[0]["type"] == "filter"
        assert entries[0]["resultCount"] == 3
        assert entries[0]["timestamp"]

    def test_a_missing_type_and_count_take_their_defaults(
        self, authenticated_client
    ):
        """The client-visible contract. Note the ``"filter"`` half is
        belt-and-braces: the router passes ``data.get("type", "filter")``
        AND the column declares ``default="filter"``, so removing the
        router's default alone is invisible here. ``resultCount`` has only
        the router's ``0``, and is genuinely load-bearing."""
        authenticated_client.post(self.HISTORY, json={"query": "bare"})

        entry = authenticated_client.get(self.HISTORY).json()["search_history"][
            0
        ]
        assert entry["type"] == "filter"
        assert entry["resultCount"] == 0

    def test_history_is_newest_first_and_capped_at_twenty(
        self, authenticated_client
    ):
        """The cap is a hard 20 in the handler, and the order is
        ``created_at DESC``. Losing either turns the "recent searches"
        dropdown into an unbounded oldest-first dump."""
        for i in range(22):
            resp = authenticated_client.post(
                self.HISTORY, json={"query": f"q{i:02d}"}
            )
            assert resp.status_code == 200, resp.text[:300]

        entries = authenticated_client.get(self.HISTORY).json()[
            "search_history"
        ]
        assert len(entries) == 20, (
            f"the 20-row cap is gone: {len(entries)} rows returned"
        )
        ids = [e["id"] for e in entries]
        assert ids == sorted(ids, reverse=True), (
            "search history is no longer newest-first"
        )
        assert "q21" in {e["query"] for e in entries}
        assert "q00" not in {e["query"] for e in entries}

    def test_clear_removes_every_row(self, authenticated_client):
        authenticated_client.post(self.HISTORY, json={"query": "to be cleared"})
        assert authenticated_client.get(self.HISTORY).json()["search_history"]

        cleared = authenticated_client.delete(self.HISTORY)

        assert cleared.status_code == 200, cleared.text[:300]
        assert cleared.json() == {"status": "success"}
        assert (
            authenticated_client.get(self.HISTORY).json()["search_history"]
            == []
        ), "clear reported success without deleting anything"

    def test_a_dict_body_with_no_query_is_a_400(self, authenticated_client):
        resp = authenticated_client.post(self.HISTORY, json={"type": "filter"})

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json() == {"error": "query is required"}


# ===========================================================================
# Scheduler status/stop -- the assertions the existing tests stop short of
# ===========================================================================


def test_scheduler_status_reports_availability_and_run_state(
    authenticated_client, monkeypatch
):
    """``is_running`` and ``scheduler_available`` are what the status widget
    renders; the isolation tests assert only the per-user counts, so a
    handler hardcoding ``is_running: True`` passes all of them."""
    monkeypatch.delenv(GATE_ENV, raising=False)

    for is_running in (True, False):
        scheduler = _scheduler_mock(is_running=is_running)
        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = authenticated_client.get(STATUS)

        assert resp.status_code == 200, resp.text[:300]
        body = resp.json()
        assert body["is_running"] is is_running
        assert body["scheduler_available"] is True


def test_stop_on_a_running_scheduler_actually_stops_it(
    authenticated_client, monkeypatch
):
    """Control for the no-op arms above, and for the existing gate tests,
    which assert the success body without checking anything happened."""
    monkeypatch.setenv(GATE_ENV, "true")
    scheduler = _scheduler_mock(is_running=True)

    with patch(SCHEDULER_TARGET, return_value=scheduler):
        resp = authenticated_client.post(STOP)

    assert resp.status_code == 200, resp.text[:300]
    assert resp.json() == {"status": "success", "message": "Scheduler stopped"}
    scheduler.stop.assert_called_once()


# ===========================================================================
# Remaining forwarding gaps on feed / history / subscribe defaults
# ===========================================================================


def test_a_valid_subscription_id_reaches_the_feed_service(
    authenticated_client,
):
    """``test_news_scheduler_isolation_fastapi.py`` proves a well-formed UUID
    is not REJECTED, but only by asserting 200 -- a handler that validated the
    id and then dropped it would pass. The whole point of the parameter is the
    filter it applies."""
    sub_id = str(uuid.uuid4())
    with patch(f"{API}.get_news_feed", return_value={"news_items": []}) as spy:
        resp = authenticated_client.get(
            "/news/api/feed", params={"subscription_id": sub_id}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert spy.call_args.kwargs["subscription_id"] == sub_id


def test_the_all_sentinel_reaches_the_feed_service_unchanged(
    authenticated_client,
):
    """``news/api.py`` implements an "all" branch; the UUID guard exempts the
    sentinel so it can get there. Normalising it to None here would silently
    turn "every subscription" into "no filter" -- the same result today, but
    not the same call."""
    with patch(f"{API}.get_news_feed", return_value={"news_items": []}) as spy:
        resp = authenticated_client.get(
            "/news/api/feed", params={"subscription_id": "all"}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert spy.call_args.kwargs["subscription_id"] == "all"


class TestSubscriptionHistoryLimit:
    """``test_news_input_validation.py`` pins the clamp (a supplied limit is
    bounded). It never sends a request WITHOUT ``?limit``, so the configured
    default -- the value every UI request actually uses -- is unpinned on
    this route. Its twin on ``/feed`` does have that test."""

    def test_an_absent_limit_uses_the_configured_default(
        self, authenticated_client
    ):
        """The setting, not a hardcoded number. Pinned with a distinctive
        value so a handler that ignores the setting and hardcodes 20 -- the
        shipped default, and therefore indistinguishable in a normal run --
        goes red."""
        sub_id = str(uuid.uuid4())
        manager = MagicMock()
        manager.get_setting.return_value = 37

        with patch(SETTINGS_MANAGER_AT_ROUTER, return_value=manager):
            with patch(
                f"{API}.get_subscription_history", return_value={"history": []}
            ) as spy:
                resp = authenticated_client.get(
                    f"/news/api/subscriptions/{sub_id}/history"
                )

        assert resp.status_code == 200, resp.text[:300]
        assert spy.call_args.args[1] == 37
        manager.get_setting.assert_called_with("news.feed.default_limit")

    def test_the_configured_default_is_still_clamped(
        self, authenticated_client
    ):
        """A setting above the ceiling must not bypass the cap."""
        sub_id = str(uuid.uuid4())
        manager = MagicMock()
        manager.get_setting.return_value = 5000

        with patch(SETTINGS_MANAGER_AT_ROUTER, return_value=manager):
            with patch(
                f"{API}.get_subscription_history", return_value={"history": []}
            ) as spy:
                authenticated_client.get(
                    f"/news/api/subscriptions/{sub_id}/history"
                )

        assert spy.call_args.args[1] == 100

    def test_the_subscription_id_is_forwarded_verbatim(
        self, authenticated_client
    ):
        sub_id = str(uuid.uuid4())
        with patch(
            f"{API}.get_subscription_history", return_value={"history": []}
        ) as spy:
            authenticated_client.get(
                f"/news/api/subscriptions/{sub_id}/history", params={"limit": 7}
            )

        assert spy.call_args.args[0] == sub_id
        assert spy.call_args.args[1] == 7


class TestSubscribeDefaults:
    """What a subscription gets when the form omits a field. These are the
    values the scheduler then runs with, and nothing pinned them."""

    def test_defaults_for_the_omitted_fields(self, authenticated_client):
        with patch(
            f"{API}.create_subscription", return_value={"id": "s1"}
        ) as spy:
            resp = authenticated_client.post(SUBSCRIBE, json={"query": "q"})

        assert resp.status_code == 200, resp.text[:300]
        kwargs = spy.call_args.kwargs
        assert kwargs["subscription_type"] == "search"
        assert kwargs["search_strategy"] == "news_aggregation"
        assert kwargs["is_active"] is True

    def test_the_optional_fields_default_to_none_not_empty_string(
        self, authenticated_client
    ):
        """``news/api.py`` distinguishes "unset" (fall back to the user's
        settings) from "set to empty". Sending "" would pin the subscription
        to an empty provider."""
        with patch(
            f"{API}.create_subscription", return_value={"id": "s1"}
        ) as spy:
            authenticated_client.post(SUBSCRIBE, json={"query": "q"})

        kwargs = spy.call_args.kwargs
        for field in (
            "refresh_minutes",
            "model_provider",
            "model",
            "custom_endpoint",
            "name",
            "folder_id",
            "search_engine",
            "search_iterations",
            "questions_per_iteration",
        ):
            assert kwargs[field] is None, (
                f"{field} defaulted to {kwargs[field]!r}"
            )