~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
"""Contract tests for the news SERVICE layer -- everything beneath the
router: ``news/api.py``, ``news/subscription_runner.py`` and ``news/core/``.
The router itself (ownership forwarding, response envelopes, the
``cleanup-now`` job registration) is covered by
``tests/news/test_news_router_contracts.py`` and is NOT re-asserted here.
Two facts established by that router audit are taken as given and built
on rather than repeated:
* ``news/api.py`` filters every subscription query by ``id`` alone
(``.filter_by(id=subscription_id)``), so the entire isolation boundary
is *which encrypted database gets opened*. What is new here is the
service-side half of that claim, proved with TWO real on-disk
databases holding the SAME subscription id -- something the router
tests could not do (their fixture ``rmtree``s the whole encrypted-DB
directory when building a user, so two users cannot coexist).
* ``news/core/search_integration.py`` imports a module that does not
exist. Rather than re-assert that one line, ``TestImportSweep`` sweeps
EVERY import in the package against what is actually on disk, at both
module and imported-name granularity, and pins the broken set exactly
-- so a second dead import added later fails here.
NO NETWORK, NO LLM. Nothing in this file reaches a search engine or a
model: the feed is built from rows seeded directly into a real on-disk
SQLite database, and the scheduler/egress boundaries are patched objects.
WHY ON-DISK SQLITE: ``news/api.py`` opens and closes its session inside
each function via ``get_user_db_session``; an in-memory database would be
a different database per connection for the multi-session tests (delete
in one session, assert in another). Every engine below is a real file
under ``tmp_path``.
EXECUTION-VERIFIED vs STATIC
----------------------------
``TestImportSweep`` is pure static analysis (``ast`` over the source
tree) plus one live ``importlib`` probe. Everything else runs the real
service functions against a real SQLite file.
DEFECTS PINNED AS CHARACTERIZATION (each named in the test's docstring):
D1 ``news/core/search_integration.py:69`` imports
``news.preference_manager.search_tracker``, which does not exist.
D2 Resolved: the service rejects invalid refresh intervals before saving
them, and the scheduler rejects invalid legacy rows before dispatch.
D3 ``delete_subscription`` / ``update_subscription`` do not pass their
known ``username`` as the scheduler-notify fallback, though
``create_subscription`` does -- so with no request context the
scheduler is never told the subscription changed.
D4 A failure of the source-links batch fetch aborts the ENTIRE feed
instead of degrading, unlike a per-row failure which is contained.
D5 The feed's ``limit * 2`` overfetch window silently under-delivers.
D6 Deleting a subscription cleans up nothing: its research history
survives on disk but becomes unreachable through the API.
D7 ``SQLCardStorage.create`` writes the enum VALUE (``"news"``) into a
column typed by enum NAME, so every card it writes is unreadable --
``get``/``list`` raise ``LookupError`` forever after.
D8 ``SQLCardStorage.create`` silently drops ``subscription_id``, so a
subscription's cards can never be found (and the un-cascaded FK on
that column is a latent delete-blocker if it ever is populated).
FALSIFICATION
-------------
Every assertion class here was shown RED against a mutated copy of the
source: a hard-linked (``cp -al``) tree with only the mutated files
unlinked and rewritten, loaded via ``PYTHONPATH``; the checked-in tree
was never touched, and each scratch tree was deleted afterwards. The
mutations, and the single test that caught each:
* dead import repointed at an existing module ->
``test_broken_module_imports_are_exactly_the_known_set``
* ``due_filter`` losing its ``active_filter()`` conjunct ->
``test_due_filter_selects_only_active_and_past_due``
* ``SQLCardStorage.create`` passing ``CardType(card_type_str)`` ->
``test_card_storage_writes_an_unreadable_card_type``
* ``SQLCardStorage.create`` assigning ``subscription_id`` ->
``test_card_storage_drops_the_subscription_link``
* ``delete_subscription`` forwarding ``username`` to the notify hook ->
``test_delete_and_update_lose_the_notify_when_context_is_absent``
* the source-links fetch wrapped in its own ``try/except`` ->
``test_a_failing_source_links_fetch_aborts_the_whole_feed``
* the overfetch widened to ``limit * 20`` ->
``test_overfetch_window_hides_reachable_news_items``
* the per-row ``except`` re-raising instead of continuing ->
``test_a_corrupt_row_is_dropped_and_the_rest_survive``
* ``delete_subscription`` also purging the tagged research history ->
``test_research_history_survives_deletion_but_becomes_unreachable``
* ``create_subscription`` minting sequential ids ->
``test_subscription_ids_are_unguessable_uuid4``
The static sweep additionally carries its own positive control
(``test_sweep_detects_a_broken_import``), which builds a throwaway
package on ``tmp_path`` and asserts the checker flags both a missing
module and a missing name.
"""
import ast
import importlib
import pathlib
import uuid
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, patch
import pytest
from sqlalchemy import create_engine, event, text
from sqlalchemy.orm import sessionmaker
import local_deep_research
from local_deep_research.database.models import Base, ResearchHistory
from local_deep_research.database.models.news import NewsCard, NewsSubscription
from local_deep_research.news import api as news_api
from local_deep_research.news.core.card_storage import SQLCardStorage
from local_deep_research.news.exceptions import (
DatabaseAccessException,
InvalidLimitException,
NewsAPIException,
SubscriptionDeletionException,
SubscriptionNotFoundException,
)
from local_deep_research.news.subscription_runner import (
advance_refresh_schedule,
advance_refresh_schedule_by_id,
mark_subscription_due_by_id,
)
SESSION_CONTEXT = "local_deep_research.database.session_context"
PKG_ROOT = pathlib.Path(local_deep_research.__file__).parent
SRC_ROOT = PKG_ROOT.parent
NEWS_DIR = PKG_ROOT / "news"
# The one dead import in the package, as of this test being written.
# (path relative to the package root, line number, dotted module)
KNOWN_BROKEN_IMPORTS = {
(
"news/core/search_integration.py",
69,
"local_deep_research.news.preference_manager.search_tracker",
)
}
# --------------------------------------------------------------------------
# Static import sweep (no imports executed)
# --------------------------------------------------------------------------
def _module_path(dotted, src_root=SRC_ROOT):
"""Filesystem path backing ``dotted``, or None if nothing backs it."""
parts = dotted.split(".")
candidate = pathlib.Path(src_root).joinpath(*parts)
if (candidate / "__init__.py").exists():
return candidate / "__init__.py"
if candidate.with_suffix(".py").exists():
return candidate.with_suffix(".py")
return None
def _resolve(dotted_self, is_package, level, module):
"""Turn a relative ``from .. import`` into an absolute dotted name."""
base = list(dotted_self) if is_package else list(dotted_self[:-1])
for _ in range(level - 1):
base = base[:-1]
return ".".join(base + ([module] if module else []))
def _bound_names(path):
"""Top-level names a module binds (defs, classes, assignments, imports).
Walks ``if``/``try`` bodies too, since conditional definitions are
still importable names.
"""
names = set()
tree = ast.parse(path.read_text(encoding="utf-8"), str(path))
def collect(node):
if isinstance(
node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
):
names.add(node.name)
elif isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
names.add(target.id)
elif isinstance(node, ast.AnnAssign) and isinstance(
node.target, ast.Name
):
names.add(node.target.id)
elif isinstance(node, (ast.Import, ast.ImportFrom)):
for alias in node.names:
names.add(alias.asname or alias.name.split(".")[0])
for node in tree.body:
collect(node)
if isinstance(node, (ast.If, ast.Try)):
for child in ast.walk(node):
collect(child)
return names
def _sweep(root, src_root, top_package):
"""Every ``from X import y`` edge under ``root`` that names a module
inside ``top_package``.
Returns ``(edges, broken_modules, broken_names)`` where an edge is
``(relative_path, lineno, dotted_module, imported_name)``.
"""
edges, broken_modules, broken_names = [], set(), set()
for file in sorted(root.rglob("*.py")):
rel_to_src = file.relative_to(src_root).with_suffix("")
is_package = rel_to_src.parts[-1] == "__init__"
self_parts = (
list(rel_to_src.parts[:-1])
if is_package
else list(rel_to_src.parts)
)
tree = ast.parse(file.read_text(encoding="utf-8"), str(file))
for node in ast.walk(tree):
if not isinstance(node, ast.ImportFrom):
continue
dotted = (
_resolve(self_parts, is_package, node.level, node.module)
if node.level
else (node.module or "")
)
if not dotted.startswith(top_package):
continue # third-party / stdlib: not this sweep's business
rel = file.relative_to(src_root / top_package).as_posix()
target = _module_path(dotted, src_root)
if target is None:
broken_modules.add((rel, node.lineno, dotted))
continue
available = _bound_names(target)
for alias in node.names:
if alias.name == "*":
continue
edges.append((rel, node.lineno, dotted, alias.name))
if alias.name in available:
continue
# ``from pkg import submodule`` is legal too.
if _module_path(f"{dotted}.{alias.name}", src_root):
continue
broken_names.add((rel, node.lineno, dotted, alias.name))
return edges, broken_modules, broken_names
class TestImportSweep:
"""STATIC. Every import in ``news/`` checked against what exists."""
@staticmethod
@pytest.fixture(scope="class")
def sweep():
return _sweep(NEWS_DIR, SRC_ROOT, "local_deep_research")
def test_sweep_actually_covered_the_package(self, sweep):
"""Guard against a vacuously-passing sweep.
A sweep that resolved nothing would report zero broken imports and
pass. Pin a floor on what it saw and name specific modules that
MUST resolve, so a broken resolver shows up as a failure here
rather than as silent all-clear below.
"""
edges, _, _ = sweep
modules = sorted(
p for p in NEWS_DIR.rglob("*.py") if p.name != "__init__.py"
)
assert len(modules) >= 20, (
f"only {len(modules)} modules found under {NEWS_DIR}; "
"the sweep is not looking at the real package"
)
assert len(edges) >= 50, (
f"only {len(edges)} intra-project import edges resolved; "
"expected the news package to have far more"
)
# A hand-picked spread: a sibling module, a cross-package model,
# a deep relative import, and the package __init__'s own re-export
# source. Each is a real import that appears in news/.
for dotted, name in [
("local_deep_research.news.core.utils", "utc_now"),
("local_deep_research.news.core.base_card", "NewsCard"),
("local_deep_research.database.models.news", "NewsSubscription"),
("local_deep_research.news.api", "get_news_feed"),
(
"local_deep_research.scheduler.background",
"BackgroundJobScheduler",
),
("local_deep_research.utilities.sql_utils", "escape_like"),
]:
path = _module_path(dotted)
assert path is not None, f"{dotted} should resolve to a file"
assert name in _bound_names(path), f"{dotted} should define {name}"
def test_sweep_detects_a_broken_import(self, tmp_path):
"""POSITIVE CONTROL for the checker itself.
Builds a throwaway package on ``tmp_path`` (``src/`` is never
touched) containing one import of a module that isn't there and
one import of a name that isn't there, and asserts the same sweep
function flags both.
"""
pkg = tmp_path / "ctlpkg"
(pkg / "sub").mkdir(parents=True)
(pkg / "__init__.py").write_text("", encoding="utf-8")
(pkg / "real.py").write_text("VALUE = 1\n", encoding="utf-8")
(pkg / "sub" / "__init__.py").write_text("", encoding="utf-8")
(pkg / "sub" / "mod.py").write_text(
"from ..real import VALUE\n"
"from ..ghost import Thing\n"
"from ..real import ABSENT\n",
encoding="utf-8",
)
edges, broken_modules, broken_names = _sweep(pkg, tmp_path, "ctlpkg")
assert ("sub/mod.py", 2, "ctlpkg.ghost") in broken_modules
assert ("sub/mod.py", 3, "ctlpkg.real", "ABSENT") in broken_names
assert ("sub/mod.py", 1, "ctlpkg.real", "VALUE") in edges
assert ("sub/mod.py", 1, "ctlpkg.real", "VALUE") not in broken_names
def test_broken_module_imports_are_exactly_the_known_set(self, sweep):
"""D1. The dead-import blast radius across ``news/`` is ONE line.
Pinned as an exact set: a new dead import anywhere in the package
fails here, and fixing this one fails here too (update the set).
"""
_, broken_modules, _ = sweep
assert broken_modules == KNOWN_BROKEN_IMPORTS
def test_no_import_names_are_missing_from_their_module(self, sweep):
"""Name-level sweep: every ``from X import y`` has a real ``y``."""
_, _, broken_names = sweep
assert broken_names == set()
def test_dead_import_raises_module_not_found_at_runtime(self):
"""D1. It is a ModuleNotFoundError, not a soft/optional import."""
with pytest.raises(ModuleNotFoundError):
importlib.import_module(
"local_deep_research.news.preference_manager.search_tracker"
)
def test_dead_import_is_swallowed_by_a_catch_all_handler(self):
"""D1. What swallows it: ``except Exception`` + ``logger.exception``.
Asserted against the AST of the enclosing ``try`` so a change from
broad-catch to a targeted one shows up here.
"""
source = (NEWS_DIR / "core" / "search_integration.py").read_text(
encoding="utf-8"
)
tree = ast.parse(source)
handlers = [
handler
for node in ast.walk(tree)
if isinstance(node, ast.Try)
for handler in node.handlers
if any(
isinstance(sub, ast.ImportFrom)
and sub.module == "preference_manager.search_tracker"
for sub in ast.walk(node)
)
]
assert len(handlers) == 1, "expected exactly one guarding try/except"
(handler,) = handlers
assert isinstance(handler.type, ast.Name)
assert handler.type.id == "Exception"
# It is logged, not silently discarded.
assert any(
isinstance(sub, ast.Attribute) and sub.attr == "exception"
for sub in ast.walk(handler)
)
def test_dead_import_branch_is_unreachable_anyway(self):
"""D1. The swallow never even fires on the normal path.
``tracking_enabled`` is hardcoded ``False``, so ``__call__`` never
calls ``_track_user_search``. Calling it directly does hit the dead
import -- and returns None instead of propagating.
"""
from local_deep_research.news.core.search_integration import (
NewsSearchCallback,
)
callback = NewsSearchCallback()
assert callback.tracking_enabled is False
with patch.object(
NewsSearchCallback,
"_track_user_search",
side_effect=AssertionError("tracking path should be unreachable"),
):
callback(
"some query",
{"findings": [{"content": "x"}]},
{"is_user_search": True, "user_id": "alice"},
)
# Reached directly, the ModuleNotFoundError is absorbed.
assert (
callback._track_user_search(
search_id="s", user_id="alice", query="q", result={}
)
is None
)
# --------------------------------------------------------------------------
# Shared database harness (real on-disk SQLite)
# --------------------------------------------------------------------------
def _make_engine(path, foreign_keys=False):
engine = create_engine(f"sqlite:///{path}")
if foreign_keys:
# Production turns this on for every user database
# (database/sqlcipher_utils.py: "PRAGMA foreign_keys = ON").
@event.listens_for(engine, "connect")
def _fk_on(dbapi_conn, _record):
dbapi_conn.execute("PRAGMA foreign_keys = ON")
Base.metadata.create_all(engine)
return engine
class UserDatabases:
"""A username -> on-disk SQLite database map, standing in for the
per-user encrypted databases that ``get_user_db_session`` opens."""
def __init__(self, tmp_path, foreign_keys=False):
self._tmp = tmp_path
self._fk = foreign_keys
self._engines = {}
self._factories = {}
self.statements = []
def factory(self, username):
if username not in self._factories:
engine = _make_engine(
self._tmp / f"{username}.sqlite", foreign_keys=self._fk
)
@event.listens_for(engine, "before_cursor_execute")
def _record(conn, cursor, statement, parameters, ctx, many):
self.statements.append((statement, parameters))
self._engines[username] = engine
self._factories[username] = sessionmaker(bind=engine)
return self._factories[username]
@contextmanager
def session(self, username):
db = self.factory(username)()
try:
yield db
finally:
db.close()
def install(self):
"""Patch ``get_user_db_session`` to hand out these databases."""
outer = self
@contextmanager
def _fake(username=None, password=None, session_id=None):
if not username:
raise AssertionError(
"news service opened a DB session without a username"
)
with outer.session(username) as db:
yield db
return patch(f"{SESSION_CONTEXT}.get_user_db_session", _fake)
def dispose(self):
for engine in self._engines.values():
engine.dispose()
@pytest.fixture
def userdbs(tmp_path):
dbs = UserDatabases(tmp_path)
with dbs.install():
yield dbs
dbs.dispose()
@pytest.fixture(autouse=True)
def _no_real_scheduler():
"""Keep the scheduler-notify hook off the real APScheduler singleton.
``create/update/delete_subscription`` all call
``_notify_scheduler_about_subscription_change``, which builds the
process-wide ``BackgroundJobScheduler``. Tests that care about the
notify contract patch this themselves; everything else just needs it
inert.
"""
scheduler = MagicMock()
scheduler.is_running = False
with patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
return_value=scheduler,
):
yield scheduler
@pytest.fixture(autouse=True)
def _skip_egress_policy_precheck():
"""The N14 egress pre-check is not this file's subject.
It is already covered by
``tests/security/test_egress_news_subscription_callsite.py``, and left
live it would reach for a settings manager that these bare databases
do not have. Neutralised to its documented "settings unavailable"
outcome (``None`` == allow) so the subscription paths under test run.
"""
with patch.object(
news_api, "_validate_subscription_policy", return_value=None
):
yield
def _news_row(
session,
research_id,
subscription_id=None,
created_at=None,
query="latest news about widgets",
content="Answer body.",
):
session.add(
ResearchHistory(
id=research_id,
query=query,
title=f"Headline {research_id}",
mode="quick",
status="completed",
created_at=created_at or datetime.now(timezone.utc).isoformat(),
completed_at=datetime.now(timezone.utc).isoformat(),
report_content=content,
research_meta={
"is_news_search": True,
**(
{"subscription_id": subscription_id}
if subscription_id
else {}
),
},
)
)
def _non_news_row(session, research_id, created_at):
session.add(
ResearchHistory(
id=research_id,
query="how do i package a python wheel",
title=f"Wheel {research_id}",
mode="quick",
status="completed",
created_at=created_at,
completed_at=created_at,
report_content="Answer body.",
research_meta={},
)
)
# --------------------------------------------------------------------------
# Subscription scheduling
# --------------------------------------------------------------------------
class TestOverdueDetection:
"""EXECUTION-VERIFIED against a real SQLite file."""
def _seed(self, session, **overrides):
fields = {
"id": str(uuid.uuid4()),
"query_or_topic": "widgets",
"subscription_type": "search",
"refresh_interval_minutes": 60,
"status": "active",
"created_at": datetime.now(timezone.utc),
}
fields.update(overrides)
sub = NewsSubscription(**fields)
session.add(sub)
return sub
def test_due_filter_selects_only_active_and_past_due(self, userdbs):
"""``NewsSubscription.due_filter`` is the single definition of
"needs running now" -- the scheduler and the overdue sweep both
key off it. Exercised as real SQL, not as a re-implementation."""
now = datetime.now(timezone.utc)
with userdbs.session("alice") as db:
self._seed(
db, id="past-active", next_refresh=now - timedelta(minutes=1)
)
self._seed(db, id="exactly-now", next_refresh=now)
self._seed(db, id="future", next_refresh=now + timedelta(hours=1))
self._seed(db, id="null-refresh", next_refresh=None)
self._seed(
db,
id="past-paused",
status="paused",
next_refresh=now - timedelta(hours=5),
)
db.commit()
with userdbs.session("alice") as db:
due = {
s.id
for s in db.query(NewsSubscription)
.filter(NewsSubscription.due_filter(now))
.all()
}
assert due == {"past-active", "exactly-now"}
def test_advance_refresh_schedule_moves_the_window_forward(self):
"""Pure arithmetic helper shared by all three run paths."""
now = datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc)
sub = NewsSubscription(refresh_interval_minutes=90)
advance_refresh_schedule(sub, now)
assert sub.last_refresh == now
assert sub.next_refresh == now + timedelta(minutes=90)
def test_advance_and_mark_due_by_id_report_missing_subscriptions(
self, userdbs
):
"""The by-id wrappers must distinguish "advanced" from "no such
subscription" -- the research-completion hooks branch on it."""
now = datetime.now(timezone.utc)
with userdbs.session("alice") as db:
sub = self._seed(
db,
id="s1",
refresh_interval_minutes=30,
next_refresh=now + timedelta(hours=3),
)
db.commit()
sub_id = sub.id
assert advance_refresh_schedule_by_id(db, sub_id) is True
assert advance_refresh_schedule_by_id(db, "nope") is False
db.commit()
with userdbs.session("alice") as db:
sub = db.query(NewsSubscription).filter_by(id=sub_id).one()
assert sub.last_refresh is not None
# Advanced to ~now + 30min, i.e. pulled BACK from now+3h.
assert sub.next_refresh < now + timedelta(minutes=45)
with userdbs.session("alice") as db:
assert mark_subscription_due_by_id(db, sub_id) is True
assert mark_subscription_due_by_id(db, "nope") is False
db.commit()
with userdbs.session("alice") as db:
sub = db.query(NewsSubscription).filter_by(id=sub_id).one()
assert sub.next_refresh <= datetime.now(timezone.utc)
# A failed run resets the due time but not the last SUCCESS.
assert sub.last_refresh is not None
@pytest.mark.parametrize("refresh_minutes", [0, -1440])
def test_nonpositive_refresh_interval_is_rejected_before_persisting(
self, userdbs, refresh_minutes
):
"""Service callers receive the same interval protection as HTTP callers."""
with pytest.raises(NewsAPIException) as rejected:
news_api.create_subscription(
user_id="alice",
query="widgets",
refresh_minutes=refresh_minutes,
)
assert rejected.value.status_code == 400
with userdbs.session("alice") as db:
assert db.query(NewsSubscription).count() == 0
def test_zero_interval_becomes_a_one_second_apscheduler_trigger(self):
"""APScheduler itself does not reject zero; our boundary must do so."""
from apscheduler.triggers.interval import IntervalTrigger
assert IntervalTrigger(minutes=0).interval == timedelta(seconds=1)
def test_update_rejects_a_nonpositive_interval(self, userdbs):
"""A rejected update preserves the usable saved schedule."""
created = news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=240
)
sub_id = created["subscription_id"]
with pytest.raises(NewsAPIException) as rejected:
news_api.update_subscription(
sub_id, {"refresh_interval_minutes": 0}, username="alice"
)
assert rejected.value.status_code == 400
with userdbs.session("alice") as db:
sub = db.query(NewsSubscription).filter_by(id=sub_id).one()
assert sub.refresh_interval_minutes == 240
assert sub.next_refresh > datetime.now(timezone.utc)
class TestSchedulingForALoggedOutUser:
"""Can a subscription still be scheduled once credentials are gone?"""
@contextmanager
def _notify_harness(self, *, username, session_id, password):
"""Drive ``_notify_scheduler_about_subscription_change`` with a
controlled request context and password store."""
scheduler = MagicMock()
scheduler.is_running = True
store = MagicMock()
store.get_session_password.return_value = password
with (
patch(
"local_deep_research.scheduler.background."
"get_background_job_scheduler",
return_value=scheduler,
),
patch(
"local_deep_research.database.session_passwords."
"session_password_store",
store,
),
patch.object(
news_api, "get_current_username", return_value=username
),
patch.object(
news_api, "get_current_session_id", return_value=session_id
),
):
yield scheduler, store
def test_logged_out_user_is_not_rescheduled(self, userdbs):
"""After logout the session password is gone, so the notify hook
must NOT hand the scheduler a user it can no longer decrypt for --
and must not raise, since it runs inside the create transaction."""
with self._notify_harness(
username="alice", session_id="sess-1", password=None
) as (scheduler, _store):
result = news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=60
)
assert result["status"] == "success"
scheduler.update_user_info.assert_not_called()
def test_live_session_is_rescheduled(self, userdbs):
"""The positive half: with a live session the scheduler IS told,
with the credentials it needs to open the encrypted database."""
with self._notify_harness(
username="alice", session_id="sess-1", password="pw"
) as (scheduler, store):
news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=60
)
store.get_session_password.assert_called_with("alice", "sess-1")
scheduler.update_user_info.assert_called_once_with("alice", "pw")
def test_delete_and_update_lose_the_notify_when_context_is_absent(
self, userdbs
):
"""D3 (DEFECT). Only ``create`` passes a username fallback.
``_notify_scheduler_about_subscription_change`` takes an optional
``user_id`` used when ``get_current_username()`` is empty.
``create_subscription`` passes it; ``update_subscription`` and
``delete_subscription`` call the hook with no fallback even though
both were handed an explicit ``username``. Off the request thread
(the scheduler's own run paths, ``run_db_sync`` worker context)
the username contextvar is empty, so a delete never reaches the
scheduler and its APScheduler job keeps firing for a subscription
that no longer exists.
"""
# Baseline: create DOES notify with no request context, via fallback.
with self._notify_harness(
username=None, session_id="sess-1", password="pw"
) as (scheduler, _store):
created = news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=60
)
assert scheduler.update_user_info.call_args_list == [
(("alice", "pw"), {})
]
sub_id = created["subscription_id"]
with self._notify_harness(
username=None, session_id="sess-1", password="pw"
) as (scheduler, _store):
news_api.update_subscription(
sub_id, {"name": "renamed"}, username="alice"
)
update_calls = scheduler.update_user_info.call_args_list
with self._notify_harness(
username=None, session_id="sess-1", password="pw"
) as (scheduler, _store):
news_api.delete_subscription(sub_id, username="alice")
delete_calls = scheduler.update_user_info.call_args_list
assert update_calls == [], (
"update_subscription now forwards a username fallback; D3 fixed"
)
assert delete_calls == [], (
"delete_subscription now forwards a username fallback; D3 fixed"
)
def test_overdue_sweep_opens_no_database_without_credentials(self):
"""Scheduler side of logout: ``_check_user_overdue_subscriptions``
must bail BEFORE touching the encrypted database once the
credential store has been cleared."""
from local_deep_research.scheduler.background import (
BackgroundJobScheduler,
SchedulerCredentialStore,
)
store = SchedulerCredentialStore(ttl_hours=1)
store.store("alice", "pw")
assert store.retrieve("alice") == "pw"
store.clear("alice")
assert store.retrieve("alice") is None
stub = MagicMock()
stub.user_sessions = {"alice": {"scheduled_jobs": set()}}
stub._credential_store = store
method = BackgroundJobScheduler._check_user_overdue_subscriptions
method = getattr(method, "__wrapped__", method)
def _explode(*args, **kwargs):
raise AssertionError("opened a user database for a logged-out user")
with patch(f"{SESSION_CONTEXT}.get_user_db_session", _explode):
method(stub, "alice")
stub.scheduler.add_job.assert_not_called()
def test_overdue_sweep_ignores_users_with_no_session(self):
"""A user the scheduler is not tracking is never swept, even if a
stale credential somehow survives."""
from local_deep_research.scheduler.background import (
BackgroundJobScheduler,
SchedulerCredentialStore,
)
store = SchedulerCredentialStore(ttl_hours=1)
store.store("alice", "pw")
stub = MagicMock()
stub.user_sessions = {}
stub._credential_store = store
method = BackgroundJobScheduler._check_user_overdue_subscriptions
method = getattr(method, "__wrapped__", method)
def _explode(*args, **kwargs):
raise AssertionError("opened a DB for an unregistered user")
with patch(f"{SESSION_CONTEXT}.get_user_db_session", _explode):
method(stub, "alice")
stub.scheduler.add_job.assert_not_called()
# --------------------------------------------------------------------------
# Feed generation
# --------------------------------------------------------------------------
class TestFeedGeneration:
"""EXECUTION-VERIFIED: real rows, real SQL, real ordering."""
def test_feed_is_bounded_by_limit_and_deterministic(self, userdbs):
"""Two identical calls over an unchanged database return the same
items in the same order, capped at ``limit``."""
base = datetime(2026, 1, 1, tzinfo=timezone.utc)
with userdbs.session("alice") as db:
for i in range(8):
_news_row(
db,
f"r{i}",
created_at=(base + timedelta(minutes=i)).isoformat(),
)
db.commit()
first = news_api.get_news_feed(user_id="alice", limit=3)
second = news_api.get_news_feed(user_id="alice", limit=3)
assert len(first["news_items"]) == 3
assert first["total_items"] == 3
ids = [item["id"] for item in first["news_items"]]
assert ids == [item["id"] for item in second["news_items"]]
# Newest first: r7, r6, r5.
assert ids == ["news-r7", "news-r6", "news-r5"]
def test_limit_below_one_is_rejected(self, userdbs):
for bad in (0, -1):
with pytest.raises(InvalidLimitException) as excinfo:
news_api.get_news_feed(user_id="alice", limit=bad)
assert excinfo.value.status_code == 400
def test_service_layer_applies_no_upper_bound_on_limit(self, userdbs):
"""The feed's only cap lives in the ROUTER. A programmatic caller
(the scheduler, an embedding application) can ask ``news.api`` for
an arbitrarily large page and the overfetched SQL LIMIT goes
straight to the database. Read off the emitted statement rather
than inferred."""
with userdbs.session("alice") as db:
_news_row(db, "r0")
db.commit()
userdbs.statements.clear()
news_api.get_news_feed(user_id="alice", limit=1_000_000)
limited = [
(statement, params)
for statement, params in userdbs.statements
if "research_history" in statement and "LIMIT" in statement
]
assert limited, "expected a LIMITed research_history query"
def _carries_overfetch(statement, params):
values = (
tuple(params.values())
if isinstance(params, dict)
else tuple(params or ())
)
return 2_000_000 in values or "2000000" in statement
assert any(_carries_overfetch(s, p) for s, p in limited), (
f"expected the overfetched LIMIT 2*1_000_000 in {limited}"
)
def test_a_corrupt_row_is_dropped_and_the_rest_survive(self, userdbs):
"""Per-row degradation. ``_format_time_ago`` raises on a
non-timestamp ``created_at``; the documented contract is that the
row is skipped and the feed still renders."""
good = datetime(2026, 1, 1, tzinfo=timezone.utc)
with userdbs.session("alice") as db:
_news_row(db, "ok-1", created_at=good.isoformat())
_news_row(db, "corrupt", created_at="not-a-timestamp")
_news_row(
db,
"ok-2",
created_at=(good - timedelta(minutes=1)).isoformat(),
)
db.commit()
feed = news_api.get_news_feed(user_id="alice", limit=10)
ids = {item["id"] for item in feed["news_items"]}
assert ids == {"news-ok-1", "news-ok-2"}
def test_a_failing_source_links_fetch_aborts_the_whole_feed(self, userdbs):
"""D4 (DEFECT). Sources do NOT degrade uniformly.
A bad ROW is contained by the per-row try/except above. The
batched source-links fetch, by contrast, sits inside the outer
try whose handler raises ``DatabaseAccessException`` -- so one
failing auxiliary source takes the entire feed down, including
every item whose headline and body were already loaded and which
needed no links at all.
"""
with userdbs.session("alice") as db:
_news_row(db, "r0")
_news_row(db, "r1")
db.commit()
# Sanity: without the failure the feed has content to lose.
assert (
len(news_api.get_news_feed(user_id="alice", limit=10)["news_items"])
== 2
)
with patch(
"local_deep_research.web.services.report_assembly_service."
"get_research_source_links_batch",
side_effect=RuntimeError("resources table unavailable"),
):
with pytest.raises(DatabaseAccessException) as excinfo:
news_api.get_news_feed(user_id="alice", limit=10)
# And the client is told nothing useful about why.
assert excinfo.value.to_dict()["error"].endswith(
"an internal error occurred"
)
def test_overfetch_window_hides_reachable_news_items(self, userdbs):
"""D5 (DEFECT). The feed under-delivers, silently.
``get_news_feed`` reads ``limit * 2`` rows and only then filters
for news-shaped ones. Fill that window with newer non-news
research and the news items below it vanish -- ``total_items``
reports 0 with no indication that more exist. Raising the limit
brings the very same rows back, proving they were reachable.
"""
base = datetime(2026, 1, 1, tzinfo=timezone.utc)
with userdbs.session("alice") as db:
# 3 news rows, oldest.
for i in range(3):
_news_row(
db,
f"news-{i}",
created_at=(base + timedelta(minutes=i)).isoformat(),
)
# 4 newer non-news rows == the whole limit*2 window for limit=2.
for i in range(4):
_non_news_row(
db,
f"other-{i}",
(base + timedelta(hours=1, minutes=i)).isoformat(),
)
db.commit()
narrow = news_api.get_news_feed(user_id="alice", limit=2)
assert narrow["news_items"] == []
assert narrow["total_items"] == 0
wide = news_api.get_news_feed(user_id="alice", limit=10)
assert len(wide["news_items"]) == 3
# --------------------------------------------------------------------------
# The unsubscribe path
# --------------------------------------------------------------------------
class TestUnsubscribePath:
"""``delete_subscription`` IS the unsubscribe path -- there is no
token, no signed link, no separate route (established by the router
audit). These tests pin what the capability actually is."""
def test_no_signed_token_machinery_exists_in_the_package(self):
"""There is nothing to forge because there is nothing signed.
A grep of the whole package for the usual token primitives. This
is the premise the rest of the class rests on: the capability is
the subscription id plus whichever database is open.
"""
hits = []
for file in NEWS_DIR.rglob("*.py"):
source = file.read_text(encoding="utf-8")
for marker in (
"itsdangerous",
"import hmac",
"URLSafeSerializer",
"TimedJSONWebSignature",
"secrets.compare_digest",
):
if marker in source:
hits.append((file.name, marker))
assert hits == []
def test_subscription_ids_are_unguessable_uuid4(self, userdbs):
"""The id is the entire capability, so it had better not be
sequential. ``create_subscription`` mints ``uuid.uuid4()``."""
ids = [
news_api.create_subscription(
user_id="alice", query=f"q{i}", refresh_minutes=60
)["subscription_id"]
for i in range(25)
]
assert len(set(ids)) == 25
for value in ids:
parsed = uuid.UUID(value)
assert parsed.version == 4
assert str(parsed) == value
def test_replaying_a_delete_reports_not_found(self, userdbs):
"""A replayed unsubscribe is not silently "successful" -- the
second call raises 404 rather than reporting another deletion."""
created = news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=60
)
sub_id = created["subscription_id"]
first = news_api.delete_subscription(sub_id, username="alice")
assert first == {"status": "success", "deleted": sub_id}
with pytest.raises(SubscriptionNotFoundException) as excinfo:
news_api.delete_subscription(sub_id, username="alice")
assert excinfo.value.status_code == 404
def test_the_database_choice_is_the_only_ownership_check(self, userdbs):
"""The service-side half of the isolation claim, with two real
databases holding the SAME subscription id.
There is no user predicate in the query, so passing the id to a
DIFFERENT user's database is not "denied" -- it either finds a row
with that id there and deletes it, or finds nothing and 404s.
Whoever calls ``delete_subscription`` picks the victim database by
the ``username`` argument alone.
"""
shared_id = str(uuid.uuid4())
alice_only = str(uuid.uuid4())
now = datetime.now(timezone.utc)
for user in ("alice", "bob"):
with userdbs.session(user) as db:
db.add(
NewsSubscription(
id=shared_id,
query_or_topic=f"{user} widgets",
subscription_type="search",
refresh_interval_minutes=60,
status="active",
created_at=now,
)
)
if user == "alice":
db.add(
NewsSubscription(
id=alice_only,
query_or_topic="alice private",
subscription_type="search",
refresh_interval_minutes=60,
status="active",
created_at=now,
)
)
db.commit()
# Alice's id, aimed at Bob's database: nothing there, so 404 --
# the isolation that holds is purely "wrong file, no such row".
with pytest.raises(SubscriptionNotFoundException):
news_api.delete_subscription(alice_only, username="bob")
# The shared id exists in BOTH. The username argument alone
# decides which one dies; no ownership column is consulted.
news_api.delete_subscription(shared_id, username="bob")
with userdbs.session("bob") as db:
assert db.query(NewsSubscription).count() == 0
with userdbs.session("alice") as db:
remaining = {s.id for s in db.query(NewsSubscription).all()}
assert remaining == {shared_id, alice_only}
# --------------------------------------------------------------------------
# What deletion leaves behind
# --------------------------------------------------------------------------
class TestDeletionCleanup:
def test_research_history_survives_deletion_but_becomes_unreachable(
self, userdbs
):
"""D6 (DEFECT). Deleting a subscription cleans up nothing.
``delete_subscription`` removes exactly one row. The research runs
it produced stay in ``research_history`` tagged with the dead
subscription's id -- still returned by ``get_news_feed`` when
filtered on that id, but no longer reachable through
``get_subscription_history``, which 404s on the missing parent
before it ever reads them. Storage grows without bound and the
user has no way to clear it.
"""
created = news_api.create_subscription(
user_id="alice", query="widgets", refresh_minutes=60
)
sub_id = created["subscription_id"]
with userdbs.session("alice") as db:
_news_row(db, "run-1", subscription_id=sub_id)
_news_row(db, "run-2", subscription_id=sub_id)
db.commit()
history = news_api.get_subscription_history(sub_id, username="alice")
assert history["total_runs"] == 2
news_api.delete_subscription(sub_id, username="alice")
with userdbs.session("alice") as db:
assert db.query(NewsSubscription).count() == 0
assert db.query(ResearchHistory).count() == 2
# Still served by the feed, keyed on the dead subscription's id.
orphans = news_api.get_news_feed(
user_id="alice", limit=10, subscription_id=sub_id
)
assert len(orphans["news_items"]) == 2
# But unreachable through the history endpoint.
with pytest.raises(SubscriptionNotFoundException):
news_api.get_subscription_history(sub_id, username="alice")
def test_card_storage_drops_the_subscription_link(self, tmp_path):
"""D8 (DEFECT). ``news_cards.subscription_id`` is never written.
``SQLCardStorage``'s own module docstring lists ``subscription_id``
among the fields it maps, and the model carries a foreign key to
``news_subscriptions.id``, but ``create()`` never assigns it. Every
card is born orphaned, so there is no query that could find a
subscription's cards in order to delete them.
"""
engine = _make_engine(tmp_path / "cards.sqlite")
factory = sessionmaker(bind=engine)
try:
storage = SQLCardStorage(factory())
card_id = storage.create(
{
"topic": "Widget prices",
"subscription_id": "sub-123",
"user_id": "alice",
}
)
with factory() as db:
row = db.execute(
text(
"SELECT id, subscription_id, card_type "
"FROM news_cards WHERE id = :i"
),
{"i": card_id},
).one()
assert row.subscription_id is None
finally:
engine.dispose()
def test_card_storage_writes_an_unreadable_card_type(self, tmp_path):
"""D7 (DEFECT). Cards are written in a form that cannot be read.
``card_type`` is a ``sqlalchemy.Enum(CardType)`` column, which
round-trips by member NAME (``"NEWS"``). ``SQLCardStorage.create``
passes the member VALUE (``"news"``). SQLAlchemy does not validate
strings on the way in, so the INSERT succeeds and the row is
poisoned: every later ``get``/``list`` over it raises
``LookupError``. ``StorageManager.get_user_feed`` wraps its whole
body in ``except Exception: return []``, so the card feed degrades
to permanently empty with nothing surfaced to the user.
"""
engine = _make_engine(tmp_path / "cards2.sqlite")
factory = sessionmaker(bind=engine)
try:
card_id = SQLCardStorage(factory()).create(
{"topic": "Widget prices", "user_id": "alice"}
)
with factory() as db:
stored = db.execute(
text("SELECT card_type FROM news_cards WHERE id = :i"),
{"i": card_id},
).scalar_one()
assert stored == "news", "the enum VALUE was persisted"
with pytest.raises(LookupError):
SQLCardStorage(factory()).get(card_id)
with pytest.raises(LookupError):
SQLCardStorage(factory()).list()
finally:
engine.dispose()
def test_a_linked_card_would_block_deletion_outright(self, tmp_path):
"""D8, latent half. The FK has no ``ondelete`` and no cascade.
Production enables ``PRAGMA foreign_keys = ON`` on every user
database (``database/sqlcipher_utils.py``). ``delete_subscription``
does a bare ``session.delete``, so the moment anything DOES
populate ``news_cards.subscription_id`` -- the field D8 currently
drops -- unsubscribing stops working entirely, and the user is
told only "an internal error occurred".
"""
dbs = UserDatabases(tmp_path, foreign_keys=True)
try:
with dbs.install():
sub_id = str(uuid.uuid4())
with dbs.session("alice") as db:
db.add(
NewsSubscription(
id=sub_id,
query_or_topic="widgets",
subscription_type="search",
refresh_interval_minutes=60,
status="active",
created_at=datetime.now(timezone.utc),
)
)
db.commit()
# Separate transaction: the two tables have no ORM
# ``relationship``, so a single flush does not order the
# parent insert before the child.
with dbs.session("alice") as db:
db.add(
NewsCard(
id="card-1",
title="Widget prices",
subscription_id=sub_id,
)
)
db.commit()
with pytest.raises(SubscriptionDeletionException) as excinfo:
news_api.delete_subscription(sub_id, username="alice")
assert excinfo.value.to_dict()["error"].endswith(
"an internal error occurred"
)
with dbs.session("alice") as db:
assert db.query(NewsSubscription).count() == 1
finally:
dbs.dispose()