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.

6.3 KB
Raw Blame
"""Regression test: several news_flask_api.py routes must surface a
NewsAPIException's structured body (error_code, details) instead of
collapsing it into a bare ``{"error": "..."}`` when the underlying
``news.api.*`` call fails.

The app registers ``@app.exception_handler(NewsAPIException)``
(fastapi_app.py) which renders ``exc.to_dict()`` -- ``error``, ``error_code``,
``status_code``, and (when present) ``details`` -- but only if the exception
actually reaches it. Most routes in this file guard against their own broad
``except Exception`` swallowing it with an ``except NewsAPIException: raise``
clause placed ahead of it (see ``get_subscription``, ``update_subscription``,
``delete_subscription``, ``get_subscription_history``, ``research_news_item``,
``save_preferences``). Three routes were missing that clause even though
their backing ``news.api`` function can raise a ``NewsAPIException``
subclass on a real, reachable path:

* ``POST /news/api/subscribe`` (``create_subscription``) --
  ``api.create_subscription()`` always raises
  ``SubscriptionCreationException`` on failure (e.g. the N14 egress-policy
  rejection, or any wrapped unexpected error).
* ``GET /news/api/feed`` (``get_news_feed``) -- ``api.get_news_feed()``
  raises ``DatabaseAccessException`` / ``NewsFeedGenerationException`` on a
  DB error or other unexpected failure while building the feed.
* ``GET /news/api/subscriptions/current`` (``get_current_user_subscriptions``)
  -- ``api.get_subscriptions()`` raises ``DatabaseAccessException`` on any
  failure reading the user's subscriptions.

Each failing-path test below was verified RED (via an in-place ``Edit``
revert of the corresponding ``except NewsAPIException: raise`` clause, never
``git``) before the fix, and GREEN after re-applying it.
"""

from local_deep_research.news.exceptions import (
    DatabaseAccessException,
    SubscriptionCreationException,
)

SUBSCRIBE = "/news/api/subscribe"
FEED = "/news/api/feed"
SUBSCRIPTIONS_CURRENT = "/news/api/subscriptions/current"

CREATE_SUBSCRIPTION_TARGET = "local_deep_research.news.api.create_subscription"
GET_NEWS_FEED_TARGET = "local_deep_research.news.api.get_news_feed"
GET_SUBSCRIPTIONS_TARGET = "local_deep_research.news.api.get_subscriptions"


def test_create_subscription_failure_carries_error_code_and_details(
    authenticated_client, monkeypatch
):
    """When api.create_subscription() raises SubscriptionCreationException,
    the client must see the structured exc.to_dict() body -- error_code and
    details -- not a bare {"error": "..."}.

    This is the assertion that fails without the fix (see
    test_manual_revert_reproduces_the_bug below for the falsification run)
    and passes with it.
    """

    def _raise(*args, **kwargs):
        raise SubscriptionCreationException(
            "egress policy rejected engine 'brave'",
            {"query": "test query", "type": "search"},
        )

    monkeypatch.setattr(CREATE_SUBSCRIPTION_TARGET, _raise)

    resp = authenticated_client.post(SUBSCRIBE, json={"query": "test query"})

    assert resp.status_code == 500, resp.text
    body = resp.json()

    # The bug: a bare {"error": "..."} with no error_code/details/status_code
    # (what safe_error_message() + the generic except Exception produces).
    # The fix: exc.to_dict() -- error_code, details, status_code all present.
    assert "error_code" in body, (
        f"error_code missing from response body -- NewsAPIException was "
        f"swallowed by a broad `except Exception` instead of reaching the "
        f"registered exception handler. Body: {body}"
    )
    assert body["error_code"] == "SUBSCRIPTION_CREATE_FAILED"
    assert body.get("details") == {"query": "test query", "type": "search"}
    assert body.get("status_code") == 500


def test_create_subscription_success_unchanged(authenticated_client):
    """A normal, successful create is unaffected by the exception-handling
    fix -- same 200 + subscription_id shape as before."""
    resp = authenticated_client.post(
        SUBSCRIBE, json={"query": "unit test subscription query"}
    )

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["status"] == "success"
    assert body["query"] == "unit test subscription query"
    assert "subscription_id" in body


def test_get_news_feed_failure_carries_error_code(
    authenticated_client, monkeypatch
):
    """When api.get_news_feed() raises DatabaseAccessException, the client
    must see error_code -- not a bare {"error": "...", "news_items": []}."""

    def _raise(*args, **kwargs):
        raise DatabaseAccessException(
            "research_history_query", "An error occurred"
        )

    monkeypatch.setattr(GET_NEWS_FEED_TARGET, _raise)

    resp = authenticated_client.get(FEED)

    assert resp.status_code == 500, resp.text
    body = resp.json()
    assert "error_code" in body, (
        f"error_code missing -- NewsAPIException swallowed by the broad "
        f"except Exception. Body: {body}"
    )
    assert body["error_code"] == "DATABASE_ERROR"


def test_get_news_feed_success_unchanged(authenticated_client):
    """A normal feed request (no subscriptions/history yet) still returns
    200 with the usual empty-feed shape."""
    resp = authenticated_client.get(FEED)

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert "news_items" in body


def test_get_current_subscriptions_failure_carries_error_code(
    authenticated_client, monkeypatch
):
    """When api.get_subscriptions() raises DatabaseAccessException, the
    client must see error_code -- not a bare {"error": "..."}."""

    def _raise(*args, **kwargs):
        raise DatabaseAccessException("get_subscriptions", "An error occurred")

    monkeypatch.setattr(GET_SUBSCRIPTIONS_TARGET, _raise)

    resp = authenticated_client.get(SUBSCRIPTIONS_CURRENT)

    assert resp.status_code == 500, resp.text
    body = resp.json()
    assert "error_code" in body, (
        f"error_code missing -- NewsAPIException swallowed by the broad "
        f"except Exception. Body: {body}"
    )
    assert body["error_code"] == "DATABASE_ERROR"


def test_get_current_subscriptions_success_unchanged(authenticated_client):
    """A normal (empty) subscriptions list still returns 200 unchanged."""
    resp = authenticated_client.get(SUBSCRIPTIONS_CURRENT)

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["subscriptions"] == []
    assert body["total"] == 0