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.

20.9 KB
Raw Blame
"""Saved subscription intervals.

An interval that cannot be scheduled never dispatches research or becomes an
APScheduler job. Every interval the released version accepted does keep
running: v1.10.7 had no server-side cap and documented up to 30 days, so the
one-week cap on newly written values must not stop the rows it already saved.
"""

import json
from contextlib import contextmanager
from datetime import datetime, timedelta, UTC
from types import SimpleNamespace
from unittest.mock import Mock

import pytest
from sqlalchemy import create_engine, update
from sqlalchemy.orm import sessionmaker
from starlette.requests import Request

from local_deep_research.database import session_context
from local_deep_research.database.models.news import NewsSubscription
from local_deep_research.news import api, constants
from local_deep_research.news.exceptions import NewsAPIException
from local_deep_research.news.subscription_runner import (
    advance_refresh_schedule,
    advance_refresh_schedule_by_id,
)
from local_deep_research.scheduler import background
from local_deep_research.scheduler.background import BackgroundJobScheduler
from local_deep_research.web.routers import news_flask_api as routes

# Intervals above the write cap that v1.10.7 could save: one past the cap,
# biweekly, the documented 30-day maximum, and the saved-row ceiling.
LEGACY_INTERVALS = [10_081, 20_160, 43_200, 525_600]

REPAIR_MESSAGE = "Edit this subscription's refresh interval before running it"


@pytest.fixture
def world(monkeypatch):
    engine = create_engine("sqlite:///:memory:")
    NewsSubscription.__table__.create(engine)
    make_session = sessionmaker(bind=engine, expire_on_commit=False)

    @contextmanager
    def session(*_args, **_kwargs):
        with make_session() as db:
            yield db

    def seed(sub_id, interval):
        with session() as db:
            db.add(
                NewsSubscription(
                    id=sub_id,
                    name=sub_id,
                    query_or_topic="A saved topic",
                    subscription_type="topic",
                    status="active",
                    refresh_interval_minutes=60,
                    next_refresh=datetime.now(UTC) - timedelta(hours=1),
                )
            )
            db.flush()
            # A legacy/imported value, bypassing today's service validation.
            db.execute(
                update(NewsSubscription)
                .where(NewsSubscription.id == sub_id)
                .values(refresh_interval_minutes=interval)
            )
            db.commit()

    notify = Mock()
    monkeypatch.setattr(session_context, "get_user_db_session", session)
    monkeypatch.setattr(
        api, "_validate_subscription_policy", Mock(return_value=None)
    )
    monkeypatch.setattr(
        api, "_notify_scheduler_about_subscription_change", notify
    )
    # Which rows were already reported is process state; every test starts
    # with none reported.
    monkeypatch.setattr(
        background, "_reported_invalid_intervals", set(), raising=False
    )
    scheduler = BackgroundJobScheduler()
    scheduler._credential_store = Mock()
    scheduler._credential_store.retrieve.return_value = "test-password"
    scheduler.user_sessions["user"] = {"scheduled_jobs": set()}
    scheduler.config["max_jitter_seconds"] = 0
    monkeypatch.setattr(scheduler, "_schedule_document_processing", Mock())
    trigger = Mock()
    monkeypatch.setattr(
        scheduler, "_trigger_subscription_research_sync", trigger
    )
    yield SimpleNamespace(
        session=session,
        seed=seed,
        scheduler=scheduler,
        trigger=trigger,
        notify=notify,
    )
    scheduler.scheduler.remove_all_jobs()
    engine.dispose()


def web_run_harness(monkeypatch):
    """Patch the research start and settings seams the web run paths use."""
    start = Mock(return_value={"status": "success", "research_id": "research"})
    monkeypatch.setattr(routes, "_start_research_in_process", start)
    monkeypatch.setattr(
        "local_deep_research.settings.manager.SettingsManager", Mock()
    )
    monkeypatch.setattr(
        "local_deep_research.news.core.utils.get_local_date_string",
        Mock(return_value="2026-09-27"),
    )
    request = Request(
        {"type": "http", "method": "POST", "path": "/", "headers": []}
    )
    return start, request


@pytest.mark.parametrize(
    ("value", "writable", "runnable"),
    [
        (1, True, True),
        (10_080, True, True),
        (10_081, False, True),
        (43_200, False, True),
        (525_600, False, True),
        (525_601, False, False),
        (10**12, False, False),
        (0, False, False),
        (-1, False, False),
        (1.5, False, False),
        (60.0, False, False),
        (float("nan"), False, False),
        (float("inf"), False, False),
        (True, False, False),
        ("60", False, False),
        (None, False, False),
    ],
)
def test_interval_bounds_separate_new_values_from_saved_rows(
    value, writable, runnable
):
    assert constants.is_valid_refresh_interval(value) is writable
    assert constants.is_runnable_refresh_interval(value) is runnable


def test_saved_row_ceiling_covers_the_released_documented_range():
    assert constants.NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES >= 30 * 24 * 60
    assert (
        constants.NEWS_SUBSCRIPTION_MAX_SAVED_REFRESH_MINUTES
        > constants.NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES
    )


@pytest.mark.parametrize(
    "interval", [0, -100, 525_601, 10**12, None, "bad", 1.5]
)
def test_invalid_saved_interval_is_not_scheduled_but_valid_rows_are(
    world, interval
):
    world.seed("bad", interval)
    valid = (1, 60, 240, 10_080, 43_200)
    for minutes in valid:
        world.seed(str(minutes), minutes)
    world.scheduler._schedule_user_subscriptions("user")
    jobs = {job.id: job for job in world.scheduler.scheduler.get_jobs()}
    assert set(jobs) == {f"user_{minutes}" for minutes in valid}
    assert jobs["user_1"].trigger.interval == timedelta(minutes=1)
    assert jobs["user_60"].trigger.interval == timedelta(minutes=60)
    for minutes in (240, 10_080, 43_200):
        assert type(jobs[f"user_{minutes}"].trigger).__name__ == "DateTrigger"
    world.trigger.assert_not_called()


def test_invalid_overdue_row_does_not_prevent_valid_overdue_job(world):
    world.seed("bad", 0)
    world.seed("good", 60)
    world.scheduler._check_user_overdue_subscriptions("user")
    jobs = world.scheduler.scheduler.get_jobs()
    assert len(jobs) == 1
    assert jobs[0].args == ("user", "good")


@pytest.mark.parametrize("interval", [0, -100, None, "bad", 525_601])
def test_preexisting_job_is_removed_before_dispatching_invalid_row(
    world, interval
):
    world.seed("bad", interval)
    scheduler = world.scheduler
    scheduler.scheduler.add_job(
        lambda: None, "interval", minutes=1, id="user_bad"
    )
    scheduler.user_sessions["user"]["scheduled_jobs"].add("user_bad")
    with world.session() as db:
        before = db.get(NewsSubscription, "bad").next_refresh
    scheduler._check_subscription("user", "bad")
    world.trigger.assert_not_called()
    assert scheduler.scheduler.get_job("user_bad") is None
    assert scheduler.user_sessions["user"]["scheduled_jobs"] == set()
    with world.session() as db:
        assert db.get(NewsSubscription, "bad").next_refresh == before


def test_valid_scheduled_job_still_dispatches_and_advances(world):
    world.seed("good", 60)
    world.scheduler._check_subscription("user", "good")
    world.trigger.assert_called_once()
    with world.session() as db:
        sub = db.get(NewsSubscription, "good")
        assert sub.next_refresh - sub.last_refresh == timedelta(minutes=60)


@pytest.mark.parametrize("interval", LEGACY_INTERVALS)
def test_saved_interval_above_the_write_cap_keeps_running(world, interval):
    """The cap limits what is written now; rows v1.10.7 saved still run."""
    world.seed("legacy", interval)
    scheduler = world.scheduler

    scheduler._schedule_user_subscriptions("user")
    job = scheduler.scheduler.get_job("user_legacy")
    assert type(job.trigger).__name__ == "DateTrigger"

    scheduler._check_user_overdue_subscriptions("user")
    overdue = [
        job.args
        for job in scheduler.scheduler.get_jobs()
        if job.id.startswith("overdue_")
    ]
    assert overdue == [("user", "legacy")]

    scheduler._check_subscription("user", "legacy")
    world.trigger.assert_called_once()
    with world.session() as db:
        sub = db.get(NewsSubscription, "legacy")
        assert sub.next_refresh - sub.last_refresh == timedelta(
            minutes=interval
        )


@pytest.mark.parametrize("interval", LEGACY_INTERVALS)
def test_advance_accepts_a_saved_interval_above_the_write_cap(interval):
    now = datetime(2026, 10, 16, tzinfo=UTC)
    row = SimpleNamespace(
        refresh_interval_minutes=interval, last_refresh=None, next_refresh=None
    )
    advance_refresh_schedule(row, now)
    assert row.last_refresh == now
    assert row.next_refresh == now + timedelta(minutes=interval)


@pytest.mark.parametrize(
    "interval", [0, -100, 525_601, True, "60", 1.5, float("nan")]
)
def test_advance_rejects_an_unrunnable_interval_without_moving_the_schedule(
    world, interval
):
    world.seed("good", 60)
    with world.session() as db:
        row = db.get(NewsSubscription, "good")
        before = (row.last_refresh, row.next_refresh)
        row.refresh_interval_minutes = interval
        with pytest.raises(NewsAPIException) as rejected:
            advance_refresh_schedule(row)
        assert rejected.value.status_code == 400
        assert (row.last_refresh, row.next_refresh) == before
    with world.session() as db:
        assert db.get(NewsSubscription, "good").refresh_interval_minutes == 60


@pytest.mark.parametrize("operation", ["create", "update"])
@pytest.mark.parametrize(
    "interval", [0, -100, 10_081, 43_200, True, "60", 1.5, float("nan")]
)
def test_new_intervals_above_the_cap_or_malformed_are_rejected(
    world, operation, interval
):
    world.seed("good", 60)
    with pytest.raises(NewsAPIException) as rejected:
        if operation == "create":
            api.create_subscription("user", "Topic", refresh_minutes=interval)
        else:
            api.update_subscription(
                "good",
                {"name": "changed", "refresh_interval_minutes": interval},
                username="user",
            )
    assert rejected.value.status_code == 400
    with world.session() as db:
        assert db.query(NewsSubscription).count() == 1
        unchanged = db.get(NewsSubscription, "good")
        assert unchanged.name == "good"
        assert unchanged.refresh_interval_minutes == 60


def test_editing_a_row_above_the_write_cap_must_lower_the_interval(world):
    """A legacy row keeps running, but an edit that sends its interval is a write.

    Both editors send the interval with every save, so saving one needs 10080
    or less (the docs say so). Pausing sends no interval and leaves it as saved.
    """
    world.seed("legacy", 43_200)

    with pytest.raises(NewsAPIException) as rejected:
        api.update_subscription(
            "legacy",
            {"name": "renamed", "refresh_interval_minutes": 43_200},
            username="user",
        )
    assert rejected.value.status_code == 400
    assert rejected.value.error_code == "INVALID_REFRESH_INTERVAL"
    full_put = routes._validate_subscription_payload(
        {"refresh_minutes": 43_200}
    )
    assert full_put.status_code == 400
    assert (
        constants.compact_subscription_update_error(
            {"refresh_interval_minutes": 43_200}
        )
        == "refresh_interval_minutes must be an integer between 1 and 10080"
    )
    with world.session() as db:
        row = db.get(NewsSubscription, "legacy")
        assert (row.name, row.refresh_interval_minutes) == ("legacy", 43_200)

    api.update_subscription("legacy", {"is_active": False}, username="user")
    with world.session() as db:
        row = db.get(NewsSubscription, "legacy")
        assert (row.status, row.refresh_interval_minutes) == ("paused", 43_200)

    api.update_subscription(
        "legacy",
        {"name": "renamed", "refresh_interval_minutes": 10_080},
        username="user",
    )
    with world.session() as db:
        row = db.get(NewsSubscription, "legacy")
        assert (row.name, row.refresh_interval_minutes) == ("renamed", 10_080)


@pytest.mark.parametrize("default_interval", [0, None, "bad", 10_081])
def test_invalid_saved_default_cannot_create_a_subscription(
    world, monkeypatch, default_interval
):
    settings = Mock()
    settings.get_setting.return_value = default_interval
    monkeypatch.setattr(
        "local_deep_research.utilities.db_utils.get_settings_manager",
        Mock(return_value=settings),
    )
    with pytest.raises(NewsAPIException) as rejected:
        api.create_subscription("user", "Topic")
    assert rejected.value.status_code == 400
    with world.session() as db:
        assert db.query(NewsSubscription).count() == 0


@pytest.mark.parametrize("operation", ["manual", "overdue"])
def test_web_run_paths_skip_invalid_rows_before_starting_research(
    world, monkeypatch, operation
):
    world.seed("bad", 0)
    world.seed("good", 60)
    start, request = web_run_harness(monkeypatch)
    if operation == "manual":
        response = routes.run_subscription_now("bad", request, "user")
        assert response.status_code == 400
        assert json.loads(response.body) == {"error": REPAIR_MESSAGE}
        start.assert_not_called()
    else:
        response = routes.check_overdue_subscriptions(request, "user")
        assert response["started"] == 1
        assert len(response["results"]) == 2
        assert any(
            row["id"] == "bad" and row["error"] == REPAIR_MESSAGE
            for row in response["results"]
        )
        start.assert_called_once()
        assert start.call_args.args[1]["metadata"]["subscription_id"] == "good"


@pytest.mark.parametrize("interval", LEGACY_INTERVALS)
@pytest.mark.parametrize("operation", ["manual", "overdue"])
def test_web_run_paths_run_a_saved_interval_above_the_write_cap(
    world, monkeypatch, operation, interval
):
    world.seed("legacy", interval)
    start, request = web_run_harness(monkeypatch)
    if operation == "manual":
        response = routes.run_subscription_now("legacy", request, "user")
        assert response["status"] == "success"
    else:
        response = routes.check_overdue_subscriptions(request, "user")
        assert response["started"] == 1
    start.assert_called_once()
    assert start.call_args.args[1]["metadata"]["subscription_id"] == "legacy"
    with world.session() as db:
        sub = db.get(NewsSubscription, "legacy")
        assert sub.next_refresh - sub.last_refresh == timedelta(
            minutes=interval
        )


def test_compact_edit_that_repairs_an_interval_reschedules_the_row(
    world, monkeypatch
):
    """The compact PUT must reach the scheduler like the full update does."""
    monkeypatch.setattr(routes, "get_user_db_session", world.session)
    # What the real notification does for a running scheduler.
    world.notify.side_effect = lambda *_args: (
        world.scheduler._schedule_user_subscriptions("user")
    )
    world.seed("bad", 0)
    world.scheduler._schedule_user_subscriptions("user")
    assert world.scheduler.scheduler.get_job("user_bad") is None

    result = routes._update_subscription_folder_sync(
        {"refresh_interval_minutes": 120}, "bad", "user"
    )

    assert result["refresh_interval_minutes"] == 120
    world.notify.assert_called_once_with("updated", "user")
    assert world.scheduler.scheduler.get_job("user_bad") is not None


def test_compact_edit_that_pauses_a_row_notifies_the_scheduler(
    world, monkeypatch
):
    monkeypatch.setattr(routes, "get_user_db_session", world.session)
    world.seed("running", 60)

    result = routes._update_subscription_folder_sync(
        {"is_active": False}, "running", "user"
    )

    assert result["status"] == "paused"
    world.notify.assert_called_once_with("updated", "user")


@pytest.mark.parametrize(
    ("sub_id", "payload", "status_code"),
    [
        ("bad", {"refresh_interval_minutes": 10_081}, 400),
        ("bad", {"refresh_interval_minutes": 0}, 400),
        ("missing", {"name": "renamed"}, 404),
    ],
)
def test_compact_edit_that_saves_nothing_does_not_notify_the_scheduler(
    world, monkeypatch, sub_id, payload, status_code
):
    monkeypatch.setattr(routes, "get_user_db_session", world.session)
    world.seed("bad", 0)

    response = routes._update_subscription_folder_sync(payload, sub_id, "user")

    assert response.status_code == status_code
    world.notify.assert_not_called()


def test_direct_start_research_refuses_a_link_to_an_unrunnable_subscription(
    world, monkeypatch
):
    """POST /api/start_research must not run research for a row /run refuses."""
    from local_deep_research.web.routers import research

    world.seed("bad", 0)
    world.seed("empty", None)
    world.seed("good", 60)
    world.seed("legacy", 43_200)
    monkeypatch.setattr(research, "get_user_db_session", world.session)
    monkeypatch.setattr("local_deep_research.settings.SettingsManager", Mock())
    # Parameter extraction is the next step after the guard; stopping it with
    # a validation error tells "passed the guard" from "refused by it".
    extract = Mock(side_effect=ValueError("iterations"))
    monkeypatch.setattr(research, "_extract_research_params", extract)

    def start(metadata):
        response = research._start_research_sync(
            {"query": "A saved topic", "metadata": metadata},
            "user",
            "http://testserver",
            None,
        )
        return response.status_code, json.loads(response.body)

    for sub_id in ("bad", "empty"):
        assert start({"subscription_id": sub_id}) == (
            400,
            {"status": "error", "message": REPAIR_MESSAGE},
        )
    extract.assert_not_called()

    passed = (
        {"subscription_id": "good"},
        {"subscription_id": "legacy"},
        {"subscription_id": "no-such-row"},
        {"subscription_id": 7},
        {},
    )
    for metadata in passed:
        status_code, body = start(metadata)
        assert status_code == 400
        assert body["message"].startswith("iterations must be")
    assert extract.call_count == len(passed)


def test_direct_start_research_guards_a_number_id_like_the_completion_advance(
    world, monkeypatch
):
    """A JSON-number id reaches the numeric-string row at completion.

    The completion advance looks the row up as ``str(id)``, so the guard must
    not treat ``{"subscription_id": 7}`` as "not an id" and let the run start.
    """
    from local_deep_research.web.routers import research

    world.seed("7", 0)
    world.seed("8", 60)
    # The premise: a number finds the numeric-string row in the completion
    # advance (the unrunnable row refuses to move, the runnable one moves).
    with world.session() as db:
        assert advance_refresh_schedule_by_id(db, 8) is True
        with pytest.raises(NewsAPIException):
            advance_refresh_schedule_by_id(db, 7)

    monkeypatch.setattr(research, "get_user_db_session", world.session)
    monkeypatch.setattr("local_deep_research.settings.SettingsManager", Mock())
    # As above: stopping parameter extraction tells "passed the guard" from
    # "refused by it".
    extract = Mock(side_effect=ValueError("iterations"))
    monkeypatch.setattr(research, "_extract_research_params", extract)

    def start(subscription_id):
        response = research._start_research_sync(
            {
                "query": "A saved topic",
                "metadata": {"subscription_id": subscription_id},
            },
            "user",
            "http://testserver",
            None,
        )
        return response.status_code, json.loads(response.body)

    assert start(7) == (
        400,
        {"status": "error", "message": REPAIR_MESSAGE},
    )
    extract.assert_not_called()

    # A runnable row, an id that matches no row and falsy ids all pass.
    passed = (8, 9, 0, "")
    for subscription_id in passed:
        status_code, body = start(subscription_id)
        assert status_code == 400
        assert body["message"].startswith("iterations must be")
    assert extract.call_count == len(passed)


def test_unusable_interval_is_reported_once_per_row_not_on_every_pass(
    world, monkeypatch
):
    logged = Mock()
    monkeypatch.setattr(background, "logger", logged)
    world.seed("bad", 0)
    world.seed("also-bad", None)
    world.seed("good", 60)

    for _ in range(3):
        world.scheduler._schedule_user_subscriptions("user")
    for _ in range(2):
        world.scheduler._check_user_overdue_subscriptions("user")

    def reported_ids(level):
        return [
            call.args[1]
            for call in getattr(logged, level).call_args_list
            if "invalid refresh interval" in call.args[0]
        ]

    # Each bad row is met five times: one warning, then four debug lines.
    assert sorted(reported_ids("warning")) == ["also-bad", "bad"]
    assert sorted(reported_ids("debug")) == ["also-bad"] * 4 + ["bad"] * 4