~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
"""POST /news/api/scheduler/check-now — manual subscription-check trigger.
The FastAPI port of ``routers/news_flask_api.py`` was fixed to actually
enqueue the per-user overdue-subscription check on APScheduler (matching
main's Flask behavior) instead of only counting due subscriptions.
These tests pin that contract:
- auth is required (401 before the scheduler is ever consulted);
- the global scheduler-control gate (``news.scheduler.allow_api_control``
via ``LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL``) returns 403 when disabled
and never touches the scheduler singleton;
- 503 when the scheduler singleton is missing or not running (and nothing
is enqueued);
- on success the endpoint calls
``scheduler.scheduler.add_job(func=_wrap_job(
_check_user_overdue_subscriptions, username=username), args=[username],
id=f"manual_check_{username}", replace_existing=True, trigger="date")``
and reports the number of due subscriptions read from the requesting
user's own database.
The APScheduler singleton is the one true boundary mocked here
(``local_deep_research.scheduler.background.get_background_job_scheduler``
— the route imports it lazily inside the handler, so patching the source
module is authoritative). Auth, CSRF, the env-setting gate, and the
due-subscription DB query all run for real.
"""
import uuid
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, patch
CHECK_NOW = "/news/api/scheduler/check-now"
GATE_ENV = "LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL"
SCHEDULER_TARGET = (
"local_deep_research.scheduler.background.get_background_job_scheduler"
)
def _running_scheduler_mock():
"""A mock BackgroundJobScheduler that reports running.
``_wrap_job`` returns a distinct sentinel so tests can assert the
endpoint enqueues the *wrapped* callable, not the bare method.
"""
scheduler = MagicMock(name="background_job_scheduler")
scheduler.is_running = True
wrapped = MagicMock(name="wrapped_overdue_check")
scheduler._wrap_job.return_value = wrapped
return scheduler, wrapped
def _current_username(client) -> str:
resp = client.get("/auth/check")
assert resp.status_code == 200, resp.text
return resp.json()["username"]
class TestAuthAndGate:
def test_unauthenticated_returns_401(self, client, monkeypatch):
"""require_auth rejects anonymous callers before the gate/scheduler."""
monkeypatch.setenv(GATE_ENV, "true")
# Mint an anonymous-session CSRF token so the request reaches
# require_auth instead of being bounced by CSRFMiddleware (403).
token = client.get("/auth/csrf-token").json()["csrf_token"]
with patch(SCHEDULER_TARGET) as get_sched:
resp = client.post(CHECK_NOW, headers={"X-CSRFToken": token})
assert resp.status_code == 401
get_sched.assert_not_called()
def test_gate_disabled_returns_403_without_touching_scheduler(
self, authenticated_client, monkeypatch
):
"""Default (env var unset) means API control is off: 403 for
authenticated users, and the scheduler singleton is never even
resolved."""
monkeypatch.delenv(GATE_ENV, raising=False)
with patch(SCHEDULER_TARGET) as get_sched:
resp = authenticated_client.post(CHECK_NOW)
assert resp.status_code == 403
assert "disabled" in resp.json()["detail"].lower()
get_sched.assert_not_called()
class TestSchedulerUnavailable:
def test_missing_scheduler_returns_503(
self, authenticated_client, monkeypatch
):
monkeypatch.setenv(GATE_ENV, "true")
with patch(SCHEDULER_TARGET, return_value=None):
resp = authenticated_client.post(CHECK_NOW)
assert resp.status_code == 503
assert resp.json()["error"] == "Scheduler not initialized"
def test_stopped_scheduler_returns_503_and_enqueues_nothing(
self, authenticated_client, monkeypatch
):
monkeypatch.setenv(GATE_ENV, "true")
scheduler, _ = _running_scheduler_mock()
scheduler.is_running = False
with patch(SCHEDULER_TARGET, return_value=scheduler):
resp = authenticated_client.post(CHECK_NOW)
assert resp.status_code == 503
assert resp.json()["error"] == "Scheduler is not running"
scheduler.scheduler.add_job.assert_not_called()
scheduler._wrap_job.assert_not_called()
class TestCheckNowEnqueues:
def test_enqueues_wrapped_overdue_check_for_current_user(
self, authenticated_client, monkeypatch
):
"""The fix under test: a running scheduler gets a one-shot
APScheduler job — wrapped worker callable, username arg, per-user
dedup id, replace_existing — and the caller sees success."""
monkeypatch.setenv(GATE_ENV, "true")
username = _current_username(authenticated_client)
scheduler, wrapped = _running_scheduler_mock()
with patch(SCHEDULER_TARGET, return_value=scheduler):
resp = authenticated_client.post(CHECK_NOW)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["status"] == "success"
assert body["count"] == 0 # fresh user: no subscriptions due
assert body["message"] == "Checking 0 due subscriptions"
# The bare method must be wrapped for worker-side context handling.
scheduler._wrap_job.assert_called_once_with(
scheduler._check_user_overdue_subscriptions,
username=username,
)
scheduler.scheduler.add_job.assert_called_once()
kwargs = scheduler.scheduler.add_job.call_args.kwargs
assert kwargs["func"] is wrapped
assert kwargs["args"] == [username]
assert kwargs["id"] == f"manual_check_{username}"
assert kwargs["replace_existing"] is True
assert kwargs["trigger"] == "date"
# One-shot run scheduled for (roughly) now, timezone-aware.
run_date = kwargs["run_date"]
assert run_date.tzinfo is not None
drift = abs((run_date - datetime.now(timezone.utc)).total_seconds())
assert drift < 60
def test_due_count_reflects_users_database(
self, authenticated_client, monkeypatch
):
"""The reported count comes from the real due_filter query against
the requesting user's DB: one overdue subscription counts, a
future-dated one does not."""
monkeypatch.setenv(GATE_ENV, "true")
username = _current_username(authenticated_client)
from local_deep_research.database.models.news import NewsSubscription
from local_deep_research.database.session_context import (
get_user_db_session,
)
now = datetime.now(timezone.utc)
with get_user_db_session(username) as session:
session.add(
NewsSubscription(
id=str(uuid.uuid4()),
subscription_type="search",
query_or_topic="overdue query",
status="active",
next_refresh=now - timedelta(hours=1),
)
)
session.add(
NewsSubscription(
id=str(uuid.uuid4()),
subscription_type="search",
query_or_topic="not due yet",
status="active",
next_refresh=now + timedelta(days=1),
)
)
session.commit()
scheduler, _ = _running_scheduler_mock()
with patch(SCHEDULER_TARGET, return_value=scheduler):
resp = authenticated_client.post(CHECK_NOW)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["count"] == 1
assert body["message"] == "Checking 1 due subscriptions"
# The job is still enqueued exactly once regardless of count.
scheduler.scheduler.add_job.assert_called_once()