~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Three news routes the Flask->FastAPI migration left with no Python test.
WHAT WAS DELETED
----------------
14 files under ``tests/news`` went away with the Flask blueprints they drove
(~9.5k lines): ``test_flask_api*.py`` (9 files, against
``news/flask_api.py``), ``test_web_blueprint*.py`` +
``test_web_routes_comprehensive.py`` (against ``news/web.py``),
``test_news_rate_limiting.py`` and ``test_safe_error_message_behavior.py``.
The two modules under test no longer exist; the routes live on in
``web/routers/news_flask_api.py`` and ``web/routers/news_pages.py``.
Most of what those files guarded is either dead on the branch or already
re-covered elsewhere, and is deliberately NOT ported here:
* rate limits -> ``tests/security/test_scheduler_control_and_news_limits_
fastapi.py`` reads the amount/granularity/scope straight off the registered
slowapi ``Limit`` objects, which is strictly stronger than the deleted
file's grep for ``'"5 per minute"'`` in the source text.
* ``safe_error_message`` -> ``tests/security/test_news_scheduler_isolation_
fastapi.py`` and ``tests/security/test_news_error_scrub_wiring.py``.
* the news page routes and their template names ->
``tests/web/routers/test_news_strategy_dropdown.py``,
``test_endpoint_coverage.py``, ``tests/security/test_auth_security.py``.
* ``get_news_feed``'s ``if "error" in result`` triage ("must be between" ->
400, else 500) and ``get_current_user_subscriptions``'s "Failed to retrieve
subscriptions" 500: both branches are UNREACHABLE. ``news/api.py``'s
``get_news_feed`` and ``get_subscriptions`` never return an ``error`` key
(the only such return, api.py:123, is commented out) — they raise
``NewsAPIException``, whose path is covered by
``tests/web/routers/test_news_subscribe_exception_contract.py``.
Same for ``submit_feedback``'s ``"not found"``/``"must be"`` ValueError
arms: ``api.submit_feedback`` only ever raises ``ValueError("Invalid vote
type: ...")`` / ``ValueError("No username available ...")``, neither of
which contains either substring.
WHAT IS RESTORED HERE, AND WHY
------------------------------
``POST /news/api/check-overdue`` (``check_overdue_subscriptions``) had NO
Python test on the branch — not a route-table snapshot entry, an actual test.
It is the per-user overdue sweep, and its error handling is load-bearing in a
way that is invisible from the response shape:
* every subscription in the sweep shares ONE DB session, and
``_start_research_in_process``'s failure path does not roll back. Without
the ``db.rollback()`` in each error branch the session is left in
``PendingRollbackError`` and every REMAINING overdue subscription in the
sweep dies too — one bad subscription silently stops the user's news from
refreshing at all, with a 200 and a "success" status on the way out.
* the per-result ``error`` text is scrubbed through ``safe_error_message``.
This is the one news response that embeds an exception-derived string per
item rather than one per request, so an unscrubbed ``str(e)`` here leaks
whatever the research startup raised (DSNs, paths) into a 200 body.
* the post-run compare-and-set (``db.refresh(sub)`` then advance only if
``next_refresh`` is untouched) exists so a fast-failing worker thread that
already reset the schedule is not clobbered and the subscription re-hidden.
* a non-success (as opposed to raising) result reports ``result["message"]``
in preference to ``result["error"]``, because that is the key
``start_research`` actually populates on failure. Preferring ``error``
yields "Failed to start research" for every real failure.
``GET /news/api/subscription/subscriptions/organized``
(``get_subscriptions_organized``) had no test either. It flattens
``FolderManager.get_subscriptions_by_folder``'s ``{"folders": [...],
"uncategorized": [...]}`` into the ``{folder_name: [sub, ...]}`` map the
subscriptions UI consumes. ``FolderManager.create_folder`` applies no name
validation, so a user folder can literally be named "uncategorized"; the
``setdefault(...).extend(...)`` is what stops that folder's subscriptions
being dropped on the floor. That is a data-loss-shaped bug in the UI, and it
is one character away from returning (``= ...`` instead of ``.extend(...)``).
``GET /news/api/subscription/stats`` (``get_subscription_stats``) is a
passthrough, tested here only because its sibling above shipped a real 500
from re-serialising a value that was already JSON-friendly — a one-line test
keeps the same mistake from landing on the sibling.
WHY DIRECT CALLS, NOT HTTP
--------------------------
All three handlers are plain ``def`` and read nothing off ``request`` (the
sweep only forwards it to ``_start_research_in_process``, which is patched),
so HTTP would add a registration + login round trip and hide the assertions
behind auth. The premise guard below pins that the functions under test are
still the ones mounted at these paths, which is the only thing HTTP would
have proven. Same pattern as
``tests/web/routers/test_research_status_error_guidance.py``.
"""
from contextlib import contextmanager
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.database.models.news import NewsSubscription
from local_deep_research.web.routers import news_flask_api
SESSION_CONTEXT = "local_deep_research.database.session_context"
SUBSCRIPTION_RUNNER = "local_deep_research.news.subscription_runner"
NEWS_CORE_UTILS = "local_deep_research.news.core.utils"
SETTINGS_MANAGER = "local_deep_research.settings.manager"
CHECK_OVERDUE = "/news/api/check-overdue"
ORGANIZED = "/news/api/subscription/subscriptions/organized"
STATS = "/news/api/subscription/stats"
#: Exactly what ``safe_error_message(e, "running subscription")`` returns for
#: any non-ValueError/KeyError/TypeError. Spelled out so a reworded scrubber
#: is a deliberate update here rather than a silent one.
SCRUBBED_RUN_ERROR = "An error occurred while running subscription"
# ===========================================================================
# Premise guard
# ===========================================================================
@pytest.mark.parametrize(
"path,endpoint,method",
[
(CHECK_OVERDUE, "check_overdue_subscriptions", "POST"),
(ORGANIZED, "get_subscriptions_organized", "GET"),
(STATS, "get_subscription_stats", "GET"),
],
)
def test_the_functions_under_test_are_the_ones_mounted_at_these_paths(
path, endpoint, method
):
"""Everything below calls the handlers directly. If a route were renamed,
remounted or dropped, those calls would keep passing against a function no
request can reach any more. Pin the wiring once, here."""
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; "
f"found {len(matches)}. The tests below would then exercise an "
f"unreachable function."
)
assert matches[0].endpoint is getattr(news_flask_api, endpoint), (
f"{method} {path} no longer resolves to news_flask_api.{endpoint}"
)
# ===========================================================================
# POST /news/api/check-overdue -- the per-user overdue sweep
# ===========================================================================
def _subscription(sub_id, name, next_refresh="2026-08-25T00:00:00+00:00"):
"""A NewsSubscription row stand-in carrying the fields the sweep reads."""
sub = MagicMock(name=f"subscription-{sub_id}")
sub.id = sub_id
sub.name = name
sub.query_or_topic = f"topic for {sub_id}"
sub.next_refresh = next_refresh
sub.refresh_interval_minutes = 60
sub.model_provider = "ollama"
sub.model = "llama3"
sub.search_strategy = "source-based"
sub.search_engine = "auto"
sub.custom_endpoint = None
return sub
def _sweep(subscriptions, outcomes, on_refresh=None):
"""Run the real ``check_overdue_subscriptions`` over ``subscriptions``.
``outcomes`` is consumed one per subscription: a dict is returned by
``_start_research_in_process``, an Exception instance is raised by it.
Returns ``(response, session, advance_refresh_schedule_mock)``.
"""
session = MagicMock(name="user_db_session")
session.query.return_value.filter.return_value.all.return_value = list(
subscriptions
)
if on_refresh is not None:
session.refresh.side_effect = on_refresh
@contextmanager
def fake_db_session(*args, **kwargs):
yield session
remaining = list(outcomes)
def fake_start(request, request_data, username):
outcome = remaining.pop(0)
if isinstance(outcome, Exception):
raise outcome
return outcome
with (
patch(
f"{SESSION_CONTEXT}.get_user_db_session",
side_effect=fake_db_session,
),
patch(
f"{SUBSCRIPTION_RUNNER}.build_subscription_request_data",
side_effect=lambda **kwargs: dict(kwargs),
),
patch(f"{SUBSCRIPTION_RUNNER}.advance_refresh_schedule") as advance,
patch(
f"{NEWS_CORE_UTILS}.get_local_date_string",
return_value="2026-08-25",
),
patch(f"{SETTINGS_MANAGER}.SettingsManager", return_value=MagicMock()),
patch.object(
news_flask_api,
"_start_research_in_process",
side_effect=fake_start,
),
):
response = news_flask_api.check_overdue_subscriptions(
None, username="alice"
)
assert not remaining, (
f"the sweep stopped early: {len(remaining)} of {len(outcomes)} "
f"subscriptions were never started"
)
return response, session, advance
class TestOverdueSweepHappyPath:
"""Positive control for every negative below. Without it, a handler that
started nothing at all -- or returned its 500 branch -- would satisfy
'the second subscription still ran' vacuously."""
def test_every_overdue_subscription_is_started_and_counted(self):
response, session, advance = _sweep(
[_subscription("sub-a", "Alpha"), _subscription("sub-b", "Beta")],
[
{"status": "success", "research_id": "res-1"},
{"status": "queued", "research_id": "res-2"},
],
)
assert response["status"] == "success"
assert response["overdue_found"] == 2
assert response["started"] == 2
assert response["results"] == [
{"id": "sub-a", "name": "Alpha", "research_id": "res-1"},
{"id": "sub-b", "name": "Beta", "research_id": "res-2"},
]
def test_a_clean_sweep_never_rolls_back_and_always_advances(self):
"""The counterpart of the two rollback assertions below: rollback is
an error-path action, so seeing it on the happy path would mean the
negative tests are measuring something unconditional."""
_, session, advance = _sweep(
[_subscription("sub-a", "Alpha"), _subscription("sub-b", "Beta")],
[
{"status": "success", "research_id": "res-1"},
{"status": "queued", "research_id": "res-2"},
],
)
assert session.rollback.call_count == 0
assert advance.call_count == 2
def test_the_sweep_really_queried_the_subscription_model(self):
"""Premise guard for the mocked session: the ``.all()`` list above is
only meaningful if the handler asked for NewsSubscription rows. A
model rename would otherwise leave every test here green against
rows the route no longer reads."""
_, session, _ = _sweep(
[_subscription("sub-a", "Alpha")],
[{"status": "success", "research_id": "res-1"}],
)
assert session.query.call_args[0][0] is NewsSubscription
def test_no_overdue_subscriptions_is_an_empty_success(self):
response, session, advance = _sweep([], [])
assert response == {
"status": "success",
"overdue_found": 0,
"started": 0,
"results": [],
}
assert advance.call_count == 0
class TestOneBadSubscriptionDoesNotStopTheSweep:
"""The shared session is the whole problem: ``_start_research_in_process``
does not roll back on failure, so without the handler's own
``db.rollback()`` the session is poisoned and every later subscription in
the sweep fails too -- inside a response that still says "success"."""
def test_a_raising_subscription_is_recorded_and_the_next_one_still_runs(
self,
):
response, session, advance = _sweep(
[_subscription("sub-a", "Alpha"), _subscription("sub-b", "Beta")],
[
RuntimeError("psql://ldr:[email protected]/news exploded"),
{"status": "success", "research_id": "res-2"},
],
)
assert response["overdue_found"] == 2
assert response["started"] == 1, (
"the second subscription did not start: one failing subscription "
"collapsed the whole sweep"
)
assert response["results"][1] == {
"id": "sub-b",
"name": "Beta",
"research_id": "res-2",
}
def test_the_raised_exception_text_never_reaches_the_response(self):
secret = "psql://ldr:[email protected]/news exploded"
response, _, _ = _sweep(
[_subscription("sub-a", "Alpha")], [RuntimeError(secret)]
)
assert response["results"][0] == {
"id": "sub-a",
"name": "Alpha",
"error": SCRUBBED_RUN_ERROR,
}
assert secret not in str(response)
def test_the_session_is_rolled_back_after_a_raising_subscription(self):
_, session, _ = _sweep(
[_subscription("sub-a", "Alpha"), _subscription("sub-b", "Beta")],
[RuntimeError("boom"), {"status": "success", "research_id": "r2"}],
)
assert session.rollback.call_count == 1, (
"the sweep must reset the shared session after a failed "
"subscription; without it the remaining subscriptions hit "
"PendingRollbackError"
)
def test_a_non_success_result_also_rolls_back_and_continues(self):
"""``_start_research_in_process`` reports some failures by RETURNING a
non-success dict rather than raising. That path commits nothing, but
it can still have left the shared session dirty."""
response, session, _ = _sweep(
[_subscription("sub-a", "Alpha"), _subscription("sub-b", "Beta")],
[
{"status": "error", "message": "LLM unavailable"},
{"status": "success", "research_id": "res-2"},
],
)
assert response["started"] == 1
assert response["results"][1]["research_id"] == "res-2"
assert session.rollback.call_count == 1
def test_a_failed_start_is_reported_from_message_not_error(self):
"""``start_research`` populates ``message`` on failure. Reading
``error`` first would show the fallback text for every real failure,
which is exactly the case a user needs the detail for."""
response, _, _ = _sweep(
[_subscription("sub-a", "Alpha")],
[
{
"status": "error",
"message": "LLM unavailable",
"error": "unused",
}
],
)
assert response["results"][0] == {
"id": "sub-a",
"name": "Alpha",
"error": "LLM unavailable",
}
def test_a_failed_start_falls_back_to_error_then_to_a_fixed_string(self):
from_error, _, _ = _sweep(
[_subscription("sub-a", "Alpha")],
[{"status": "error", "error": "no such model"}],
)
assert from_error["results"][0]["error"] == "no such model"
from_neither, _, _ = _sweep(
[_subscription("sub-a", "Alpha")], [{"status": "error"}]
)
assert from_neither["results"][0]["error"] == "Failed to start research"
def test_an_unnamed_subscription_is_labelled_by_its_topic(self):
"""The sweep snapshots ``sub.name or sub.query_or_topic[:50]`` BEFORE
running, precisely so the error branches never touch a row that a
failed run may have expired."""
response, _, _ = _sweep(
[_subscription("sub-a", None)], [RuntimeError("boom")]
)
assert response["results"][0]["name"] == "topic for sub-a"
class TestRefreshScheduleCompareAndSet:
"""A fast-failing run on the worker thread resets ``next_refresh`` itself.
Advancing unconditionally would clobber that reset and re-hide the
subscription until the next interval."""
def test_the_schedule_advances_when_nothing_else_touched_it(self):
_, _, advance = _sweep(
[_subscription("sub-a", "Alpha", next_refresh="T0")],
[{"status": "success", "research_id": "res-1"}],
)
assert advance.call_count == 1
def test_the_schedule_is_left_alone_when_the_run_already_reset_it(self):
def worker_reset_it(sub):
sub.next_refresh = "T1"
response, _, advance = _sweep(
[_subscription("sub-a", "Alpha", next_refresh="T0")],
[{"status": "success", "research_id": "res-1"}],
on_refresh=worker_reset_it,
)
assert advance.call_count == 0, (
"advance_refresh_schedule clobbered a next_refresh that the run "
"had already reset"
)
assert response["started"] == 1, (
"skipping the advance must not skip counting the run"
)
# ===========================================================================
# GET /news/api/subscription/{subscriptions/organized,stats}
# ===========================================================================
@contextmanager
def _folder_manager(**returns):
"""Patch the route's session + FolderManager, yielding the manager mock."""
session = MagicMock(name="user_db_session")
manager = MagicMock(name="FolderManager")
for attr, value in returns.items():
getattr(manager, attr).return_value = value
@contextmanager
def fake_db_session(*args, **kwargs):
yield session
with (
patch.object(
news_flask_api,
"get_user_db_session",
side_effect=fake_db_session,
),
patch.object(news_flask_api, "FolderManager", return_value=manager),
):
yield manager
def _organized(payload):
with _folder_manager(get_subscriptions_by_folder=payload) as manager:
response = news_flask_api.get_subscriptions_organized(
None, username="alice"
)
manager.get_subscriptions_by_folder.assert_called_once_with("alice")
return response
class TestSubscriptionsOrganized:
def test_folders_are_flattened_to_a_name_keyed_map(self):
"""Positive control for the collision test below, and a regression
pin in its own right: this route once called ``.to_dict()`` on the
plain dicts ``get_subscriptions_by_folder`` returns and 500'd."""
response = _organized(
{
"folders": [
{
"folder": {"id": "f1", "name": "General"},
"subscriptions": [{"id": "s1", "name": "one"}],
},
{
"folder": {"id": "f2", "name": "Science"},
"subscriptions": [{"id": "s2", "name": "two"}],
},
],
"uncategorized": [{"id": "s3", "name": "three"}],
}
)
assert response == {
"General": [{"id": "s1", "name": "one"}],
"Science": [{"id": "s2", "name": "two"}],
"uncategorized": [{"id": "s3", "name": "three"}],
}
def test_a_folder_named_uncategorized_keeps_its_subscriptions(self):
"""``FolderManager.create_folder`` validates nothing, so a user CAN
name a folder "uncategorized". Overwriting the key instead of
extending it makes that folder's subscriptions vanish from the UI
with no error anywhere."""
response = _organized(
{
"folders": [
{
"folder": {"id": "f1", "name": "uncategorized"},
"subscriptions": [{"id": "in_the_folder"}],
}
],
"uncategorized": [{"id": "ungrouped"}],
}
)
assert {sub["id"] for sub in response["uncategorized"]} == {
"in_the_folder",
"ungrouped",
}, (
"a user folder named 'uncategorized' was merged destructively "
"with the ungrouped bucket -- subscriptions were dropped"
)
def test_a_folder_without_a_name_falls_back_to_its_id(self):
"""``folder.to_dict()`` can carry a null name; keying on ``None``
would collapse every unnamed folder into one bucket."""
response = _organized(
{
"folders": [
{
"folder": {"id": "f9", "name": None},
"subscriptions": [{"id": "s9"}],
}
],
"uncategorized": [],
}
)
assert response == {"f9": [{"id": "s9"}], "uncategorized": []}
def test_the_uncategorized_key_is_always_present(self):
"""The UI reads ``data.uncategorized`` unconditionally."""
response = _organized({"folders": [], "uncategorized": []})
assert response == {"uncategorized": []}
class TestSubscriptionStats:
def test_the_managers_stats_are_returned_unwrapped(self):
"""``FolderManager.get_subscription_stats`` already returns a
JSON-friendly dict. Wrapping or re-serialising it is the exact
mistake that shipped on the sibling route above."""
stats = {"total": 5, "active": 3, "folders": 2}
with _folder_manager(get_subscription_stats=stats) as manager:
response = news_flask_api.get_subscription_stats(
None, username="alice"
)
assert response == stats
manager.get_subscription_stats.assert_called_once_with("alice")