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.

7.9 KB
Raw Blame
"""High-value edge case tests for the document-scheduler surface.

Covers:
- Route registration for the document scheduler
- The status / manual-run response contracts, including the failure paths

PROVENANCE — read before adding anything back
---------------------------------------------
This module skipped itself whole after the migration because it imported
``local_deep_research.research_scheduler.routes`` (a Flask blueprint) and
``...research_scheduler.document_scheduler.DocumentSchedulerUtil``. Those two
imports are NOT equivalent losses:

* ``DocumentSchedulerUtil`` — the five tests that mocked it are not coverage
  this branch dropped. The class was deleted on **main** by #3750
  ("refactor(scheduler): inline DocumentSchedulerUtil into routes"), and
  ``git show origin/main:tests/research_scheduler/test_scheduler_edge_cases.py``
  contains only the two blueprint tests. The five reappeared here through a
  main->branch merge that resurrected the pre-#3750 file; un-skipping them
  as-is would raise ``ModuleNotFoundError`` at collection, not run. Their
  behaviour (an error dict from ``get_status``, a ``(bool, message)`` tuple
  from ``trigger_manual_run``) now lives in
  ``scheduler/background.py::BackgroundJobScheduler`` plus the HTTP contract
  of ``web/routers/scheduler.py``, which is what the route tests below pin.
* The two blueprint tests ARE a genuine loss. ``scheduler_bp.name`` and
  ``scheduler_bp.deferred_functions`` have no successor — a
  ``fastapi.APIRouter`` has no ``name`` attribute at all, and it materialises
  its routes eagerly into ``router.routes`` instead of deferring
  registration callables until ``register_blueprint``. The property those
  two were reaching for — "this feature's routes are actually wired up, not
  silently absent" — is re-expressed against the live app below.
"""

from unittest.mock import MagicMock, patch

import pytest
from fastapi.routing import APIRoute

SCHEDULER_ROUTER = "local_deep_research.web.routers.scheduler"
STATUS_URL = "/api/scheduler/status"
RUN_NOW_URL = "/api/scheduler/run-now"


def _routes(app):
    return {
        (r.path, frozenset(r.methods))
        for r in app.routes
        if isinstance(r, APIRoute)
    }


class TestSchedulerRouteRegistration:
    """Successor of the blueprint-registration tests."""

    def test_router_declares_the_document_scheduler_tag(self):
        """``APIRouter`` has no ``name``; the tag is the identity it carries.

        Direct successor of ``scheduler_bp.name == "document_scheduler"`` —
        the tag is what groups these operations in the OpenAPI schema.
        """
        from local_deep_research.web.routers import scheduler

        assert "document_scheduler" in scheduler.router.tags

    def test_router_has_routes(self):
        """Successor of ``len(scheduler_bp.deferred_functions) > 0``."""
        from local_deep_research.web.routers import scheduler

        assert len(scheduler.router.routes) > 0

    def test_scheduler_routes_are_mounted_on_the_app(self, app):
        """...and the router is actually mounted.

        A router can be perfectly well-formed and still never reach the app
        (a missing entry in ``_mount_all``), which is exactly the failure a
        blueprint-object assertion could not see: every endpoint 404s while
        the module-level test stays green.
        """
        routes = _routes(app)
        assert any(
            path == STATUS_URL and "GET" in methods for path, methods in routes
        ), f"{STATUS_URL} is not mounted"
        assert any(
            path == RUN_NOW_URL and "POST" in methods
            for path, methods in routes
        ), f"{RUN_NOW_URL} is not mounted"


class TestSchedulerStatusContract:
    """``GET /api/scheduler/status`` — successor of ``get_status``'s error dict."""

    def test_status_returns_the_schedulers_payload(self, authenticated_client):
        """Positive control for the failure case below."""
        payload = {
            "enabled": True,
            "interval_seconds": 3600,
            "has_scheduled_job": True,
            "user_active": True,
        }
        scheduler = MagicMock()
        scheduler.get_document_scheduler_status.return_value = payload

        with patch(
            f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
            return_value=scheduler,
        ):
            resp = authenticated_client.get(STATUS_URL)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == payload
        scheduler.get_document_scheduler_status.assert_called_once()

    def test_status_error_is_a_generic_500_not_a_traceback(
        self, authenticated_client
    ):
        """A scheduler blow-up must surface as a handled error.

        The Flask helper answered with an error dict; the route answers with
        a 500 whose body names no internals. Letting the exception escape
        would render a stack trace into an authenticated user's dashboard.
        """
        with patch(
            f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
            side_effect=Exception("Scheduler error"),
        ):
            resp = authenticated_client.get(STATUS_URL)

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {"error": "Failed to get scheduler status"}
        assert "Scheduler error" not in resp.text, (
            "the internal exception message leaked into the response"
        )


class TestSchedulerManualRunContract:
    """``POST /api/scheduler/run-now`` — successor of ``trigger_manual_run``'s
    ``(success, message)`` tuple, now expressed as a status code."""

    def _post(self, client):
        return client.post(RUN_NOW_URL, json={})

    def test_manual_run_success(self, authenticated_client):
        scheduler = MagicMock()
        scheduler.trigger_document_processing.return_value = True

        with patch(
            f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
            return_value=scheduler,
        ):
            resp = self._post(authenticated_client)

        assert resp.status_code == 200, resp.text[:300]
        assert "successfully" in resp.json()["message"]
        scheduler.trigger_document_processing.assert_called_once()

    def test_manual_run_refused_is_a_client_error(self, authenticated_client):
        """``trigger_document_processing`` returning False means "not
        applicable for this user" (inactive, or processing disabled). That is
        a 400, distinguishable from the 500 below — collapsing the two would
        page an operator for a user-configuration state.
        """
        scheduler = MagicMock()
        scheduler.trigger_document_processing.return_value = False

        with patch(
            f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
            return_value=scheduler,
        ):
            resp = self._post(authenticated_client)

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

    def test_manual_run_error_is_a_generic_500(self, authenticated_client):
        with patch(
            f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
            side_effect=Exception("boom"),
        ):
            resp = self._post(authenticated_client)

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {"error": "Failed to trigger manual run"}
        assert "boom" not in resp.text


@pytest.mark.parametrize("url,method", [(STATUS_URL, "get")])
def test_scheduler_status_requires_authentication(app, url, method):
    """The scheduler surface is behind the auth gate.

    Reported per-user (``get_document_scheduler_status(username)``), so an
    anonymous caller must not be able to ask at all.
    """
    from fastapi.testclient import TestClient

    client = TestClient(app, raise_server_exceptions=False)
    resp = getattr(client, method)(url, follow_redirects=False)
    assert resp.status_code == 401, (
        f"{method.upper()} {url} returned {resp.status_code} without auth"
    )