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.

31.3 KB
Raw Blame
"""The journal-data fetch must honour the user's egress scope everywhere.

Three entry points can pull the journal-quality datasets (OpenAlex / DOAJ
/ GitHub, hundreds of MB):

* the dashboard download button (``metrics.py``), which refuses with 403
  under PRIVATE_ONLY / STRICT;
* the reputation filter's background fetch thread, skipped under those
  scopes (``_should_skip_journal_fetch_for_scope``, pinned by
  ``test_egress_pep_coverage.py``); and
* the lazy build — ``JournalQualityDB._build_or_raise`` runs implicitly
  on any read through ``_ensure_engine`` when the DB file is missing or
  fails validation, and used to decide on test mock mode alone.

The lazy build is reached from two directions, and each needs its own
gate because each sees a different signal:

* inside a research run, on a worker thread that arms the run's egress
  context: ``_build_or_raise`` reads that context and turns the network
  download off (a local build from already-present snapshots still
  proceeds — only the network is off the table); and
* from the dashboard read routes, which Starlette runs on anyio
  threadpool workers where NO context is armed, leaving the context gate
  structurally blind. Those routes resolve the scope a research
  run for the user would get — the stored ``policy.egress_scope`` with
  ADAPTIVE resolved against the user's primary engine, as the worker and
  the reputation filter resolve it — and answer "unavailable" rather than let a page load DOWNLOAD the
  datasets.

The route gate answers the same question the build does, in the same
order, because the compiled DB is not the predicate that governs a
download — the snapshot is:

1. ``openalex_sources.json.gz`` on disk → the build compiles from disk
   and never asks the network. Allowed under every scope, including a
   forbidding one, which is the whole point: an offline rebuild must not
   darken the dashboard.
2. No snapshot, but the compiled DB exists AND passes the same validity
   probe ``_ensure_engine`` applies → no build runs. Allowed.
3. Neither → a read would have to download. Only here does the
   effective scope decide, and only here is the answer "unavailable".

Case 2 is not decoration: ``_ensure_engine`` rebuilds on a FAILED
validation as well as on a missing file, and three
``JOURNAL_QUALITY_SCHEMA_VERSION`` bumps have shipped, so every install
that upgrades past one takes that branch on its next read.

These tests pin both gates plus the two branches reachable only through
the first one: the ``policy_audit`` record and the scope-specific error.
"""

import sqlite3
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, patch

import pytest
from fastapi.responses import JSONResponse
from starlette.requests import Request

from local_deep_research.journal_quality.db import (
    DB_FILENAME,
    JOURNAL_QUALITY_SCHEMA_VERSION,
    JournalQualityDB,
)
from local_deep_research.journal_quality.downloader import (
    REQUIRED_SNAPSHOT_FILENAME,
)
from local_deep_research.security.egress.audit_hook import (
    active_egress_context,
)
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)
from local_deep_research.web.routers import metrics

_DB = "local_deep_research.journal_quality.db"
_TESTING = "local_deep_research.settings.env_definitions.testing"
_DOWNLOADER = "local_deep_research.journal_quality.downloader"
_PATHS = "local_deep_research.config.paths"
_DB_UTILS = "local_deep_research.utilities.db_utils"

#: The policy_audit record `_build_or_raise` writes when it refuses.
_AUDIT_LINE = "journal-data lazy build refused the network download"
#: Branch-specific substrings of two of `_build_or_raise`'s three
#: FileNotFoundError messages. All three share "not available", so an
#: unpinned `pytest.raises` passes on whichever branch happens to run.
_SCOPE_REFUSAL = "egress scope forbids the public download"
_GENERIC_FAILURE = "Check your network connection"


def _ctx(scope: EgressScope) -> EgressContext:
    return EgressContext(
        scope=scope,
        primary_engine="library",
        require_local_llm=False,
        require_local_embeddings=False,
    )


def _lazy_build_calls_with(recorder, *, message):
    """Run the lazy build in non-mock mode; return the auto_download kwarg.

    ``message`` pins which refusal branch raised.
    """
    with (
        patch(f"{_TESTING}.testing_with_mocks", return_value=False),
        patch(f"{_DOWNLOADER}.ensure_journal_data", recorder) as rec,
    ):
        db = __import__(
            "local_deep_research.journal_quality.db",
            fromlist=["JournalQualityDB"],
        ).JournalQualityDB()
        with pytest.raises(FileNotFoundError, match=message):
            db._build_or_raise(Path("/nonexistent/journal_quality.db"))
    assert rec.call_count == 1
    return rec.call_args.kwargs["auto_download"]


def test_lazy_build_refuses_download_under_private_only(loguru_caplog_full):
    recorder = Mock(return_value=(None, False))

    with (
        loguru_caplog_full.at_level("INFO"),
        active_egress_context(_ctx(EgressScope.PRIVATE_ONLY)),
    ):
        auto_download = _lazy_build_calls_with(recorder, message=_SCOPE_REFUSAL)

    assert auto_download is False, (
        "lazy build attempted the public dataset download under a "
        "PRIVATE_ONLY egress scope — the scope forbids public fetches"
    )
    assert _AUDIT_LINE in loguru_caplog_full.text, (
        "a refused download must leave a policy_audit record"
    )


def test_lazy_build_refuses_download_under_strict(loguru_caplog_full):
    recorder = Mock(return_value=(None, False))

    with (
        loguru_caplog_full.at_level("INFO"),
        active_egress_context(_ctx(EgressScope.STRICT)),
    ):
        auto_download = _lazy_build_calls_with(recorder, message=_SCOPE_REFUSAL)

    assert auto_download is False
    assert _AUDIT_LINE in loguru_caplog_full.text


def test_lazy_build_without_active_context_still_downloads(
    loguru_caplog_full,
):
    """No active scope (a run whose snapshot failed to resolve, or a call
    from a thread that arms no context): this gate leaves the download
    alone, and nothing was refused, so no audit record is written."""
    recorder = Mock(return_value=(None, False))

    with loguru_caplog_full.at_level("INFO"):
        auto_download = _lazy_build_calls_with(
            recorder, message=_GENERIC_FAILURE
        )

    assert auto_download is True
    assert _AUDIT_LINE not in loguru_caplog_full.text


def test_lazy_build_still_builds_locally_under_private_only(
    tmp_path, loguru_caplog_full
):
    """Snapshots already on disk: the local, network-free build proceeds
    even under a forbidding scope — only the network is off the table.
    Nothing was refused, so no policy record is due either."""
    recorder = Mock(return_value=(tmp_path, True))

    with (
        loguru_caplog_full.at_level("INFO"),
        active_egress_context(_ctx(EgressScope.PRIVATE_ONLY)),
        patch(f"{_TESTING}.testing_with_mocks", return_value=False),
        patch(f"{_DOWNLOADER}.ensure_journal_data", recorder),
        patch(f"{_DB}.build_db") as build,
    ):
        db = __import__(
            "local_deep_research.journal_quality.db",
            fromlist=["JournalQualityDB"],
        ).JournalQualityDB()
        db._build_or_raise(tmp_path / "journal_quality.db")

    assert build.call_count == 1, "local build from snapshots must proceed"
    assert _AUDIT_LINE not in loguru_caplog_full.text, (
        "the local build was allowed — recording it as a refused download "
        "would put a phantom entry in the policy audit trail"
    )


# ===========================================================================
# The dashboard read routes: a page load must not start the download
# ===========================================================================

#: Every route in `web/routers/metrics.py` that reaches `_ensure_engine`
#: (and through it `_build_or_raise`): `/api/journals` via `ref.available`,
#: the two paper-rooted routes via `_get_ref_db_or_none` ->
#: `lookup_sources_batch` / `count_predatory_by_names`.
_READ_ROUTES = ("journals", "user-research", "per-research")


def _request() -> Request:
    """A real Starlette Request — slowapi's decorator rejects a Mock."""
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/metrics/api/journals",
            "headers": [],
            "query_string": b"",
            "client": ("127.0.0.1", 1),
            "server": ("test", 80),
            "scheme": "http",
        }
    )


def _call(route: str):
    if route == "journals":
        return metrics.api_journal_quality(_request(), username="alice")
    if route == "user-research":
        return metrics.api_user_research_journals(_request(), username="alice")
    return metrics.api_research_journals(
        _request(), "research-1", username="alice"
    )


def _settings_manager(scope: str, primary: str | None = "arxiv") -> Mock:
    """A settings manager whose stored scope is ``scope``.

    Answers both reads: ``get_setting`` (the download button's literal
    scope) and ``get_settings_snapshot`` (the read-route gate, which
    resolves ADAPTIVE against ``search.tool`` the way a research run
    does). ``primary`` defaults to a public engine so a literal scope
    behaves as itself; ``None`` leaves ``search.tool`` out entirely.
    """
    manager = Mock()
    manager.get_setting.return_value = scope
    snapshot = {"policy.egress_scope": scope}
    if primary is not None:
        snapshot["search.tool"] = primary
    manager.get_settings_snapshot.return_value = snapshot
    return manager


@contextmanager
def _dashboard(*, scope: str, data_dir: Path):
    """A dashboard request for a user whose persisted scope is ``scope``.

    ``data_dir`` stands in for the journal data directory, so what the
    machine happens to have on disk never decides these tests.

    ``_build_or_raise`` is stubbed because it is the assertion target:
    whether the read reached the build. ``download_journal_data`` is
    stubbed only as a backstop — with the build stubbed it is
    unreachable, so asserting on it here would be vacuous. The
    non-vacuous downloader evidence is
    ``test_forbidding_scope_is_what_stops_the_download``, which leaves
    ``_build_or_raise`` real.
    """
    manager = _settings_manager(scope)
    with (
        patch(f"{_DB_UTILS}.get_settings_manager", return_value=manager),
        patch(f"{_PATHS}.get_journal_data_directory", return_value=data_dir),
        patch(f"{_DB}.JournalQualityDB._build_or_raise") as build,
        patch(f"{_DOWNLOADER}.download_journal_data") as download,
        patch.object(
            metrics, "_get_ref_db_or_none", return_value=None
        ) as ref_db_getter,
    ):
        yield SimpleNamespace(
            build=build, download=download, ref_db_getter=ref_db_getter
        )


@contextmanager
def _one_paper_row():
    """A user DB with one Paper row, so the paper-rooted routes get past
    their ``if not rows`` early return and reach the reference DB."""
    row = SimpleNamespace(
        container_title="Journal A",
        paper_count=2,
        year_min=2020,
        year_max=2024,
    )
    query = MagicMock()
    for method in ("filter", "join", "group_by", "order_by", "limit"):
        getattr(query, method).return_value = query
    query.all.return_value = [row]
    query.first.return_value = SimpleNamespace(id="research-1")
    session = MagicMock(bind=MagicMock())
    session.query.return_value = query
    inspector = MagicMock()
    inspector.has_table.return_value = True

    @contextmanager
    def open_session(*_args):
        yield session

    with (
        patch.object(metrics, "get_user_db_session", side_effect=open_session),
        patch("sqlalchemy.inspect", return_value=inspector),
        patch.object(metrics, "_lookup_journal_llm_quality", return_value={}),
    ):
        yield


@contextmanager
def _stub_reference_db():
    """Reference DB stub for the cases where the read IS allowed through:
    the assertion is that the route got there, not what it read."""
    ref = MagicMock(available=True)
    ref.get_journals_page.return_value = ([{"name": "Journal A"}], 1)
    with patch(f"{_DB}.get_journal_reference_db", return_value=ref):
        yield ref


def _assert_reached_the_reference_db(route, env, result, ref):
    if route == "journals":
        assert result["status"] == "success"
        ref.get_journals_page.assert_called_once()
    else:
        assert result["status"] == "success"
        env.ref_db_getter.assert_called_once_with()


@pytest.mark.parametrize("scope", ["private_only", "strict"])
@pytest.mark.parametrize("route", _READ_ROUTES)
def test_dashboard_read_does_not_build_under_a_forbidding_scope(
    tmp_path, route, scope
):
    """A dashboard page load must not become a public dataset download.

    The reference DB is missing, so every one of these reads would run
    the lazy build, and `_build_or_raise`'s own gate cannot see this
    user's scope from a threadpool worker (no context is armed there).
    Remove the route-level gate and the read reaches `_build_or_raise`
    with `auto_download=True`: `env.build` records the call.
    """
    with _dashboard(scope=scope, data_dir=tmp_path) as env, _one_paper_row():
        result = _call(route)

    env.build.assert_not_called()
    if route == "journals":
        assert isinstance(result, JSONResponse)
        assert result.status_code == 503, (
            "the route must answer with its existing 'reference database "
            "not available' response, not build the database"
        )
    else:
        env.ref_db_getter.assert_not_called()
        assert result["status"] == "success"
        assert result["summary"]["predatory_blocked"] == 0


@pytest.mark.parametrize("route", _READ_ROUTES)
def test_dashboard_read_proceeds_when_the_db_file_exists(tmp_path, route):
    """An existing file is read, never built, so no download can follow:
    a forbidding scope must not degrade the dashboard. Gating on the
    scope alone (ignoring the file) turns this into the 503 / empty
    response above."""
    (tmp_path / "journal_quality.db").touch()

    with (
        _dashboard(scope="private_only", data_dir=tmp_path) as env,
        _one_paper_row(),
        _stub_reference_db() as ref,
    ):
        result = _call(route)

    _assert_reached_the_reference_db(route, env, result, ref)


@pytest.mark.parametrize("route", _READ_ROUTES)
def test_dashboard_read_is_unchanged_under_a_public_scope(tmp_path, route):
    """Positive control: with no DB file at all, a scope that permits
    public egress still reaches the reference DB (and, in production,
    still builds it). Without this, "always answer unavailable" would
    pass every other test in this section."""
    with (
        _dashboard(scope="public_only", data_dir=tmp_path) as env,
        _one_paper_row(),
        _stub_reference_db() as ref,
    ):
        result = _call(route)

    _assert_reached_the_reference_db(route, env, result, ref)


# ===========================================================================
# What the gate actually predicts: the snapshot, not the compiled DB
# ===========================================================================


def _write_snapshot(data_dir: Path) -> None:
    """Put the one REQUIRED snapshot on disk.

    Content is irrelevant here: ``ensure_journal_data`` decides on
    ``(user_dir / REQUIRED_SNAPSHOT_FILENAME).exists()`` alone and
    returns "available" without a single request, so the build that
    follows is local. Only the presence of the name matters.
    """
    (data_dir / REQUIRED_SNAPSHOT_FILENAME).write_bytes(b"")


def _write_stale_schema_db(path: Path) -> None:
    """A real SQLite file stamped with a schema version this build rejects.

    Not an exotic corner: ``JOURNAL_QUALITY_SCHEMA_VERSION`` has been
    bumped three times (1→2, 2→3, 3→4), and every install that upgrades
    past a bump finds exactly this file — valid SQLite, wrong stamp — on
    its next read, which sends ``_ensure_engine`` into ``_build_or_raise``.
    """
    conn = sqlite3.connect(str(path))
    try:
        conn.execute(
            "PRAGMA user_version = %d" % (JOURNAL_QUALITY_SCHEMA_VERSION + 1)
        )
        conn.execute("CREATE TABLE t (x)")
        conn.commit()
    finally:
        conn.close()


def _write_valid_db(path: Path) -> None:
    conn = sqlite3.connect(str(path))
    try:
        conn.execute(
            "PRAGMA user_version = %d" % JOURNAL_QUALITY_SCHEMA_VERSION
        )
        conn.execute("CREATE TABLE t (x)")
        conn.commit()
    finally:
        conn.close()


def _write_corrupt_db(path: Path) -> None:
    path.write_text("this is not a database at all", encoding="utf-8")


_DB_STATES = {
    "missing": lambda path: None,
    "valid": _write_valid_db,
    "stale_schema": _write_stale_schema_db,
    "corrupt": _write_corrupt_db,
}


@contextmanager
def _gate_env(data_dir: Path, *, scope: str, primary: str | None = "arxiv"):
    """Point the gate at ``data_dir`` with ``scope`` as the stored value
    and ``primary`` as the user's ``search.tool``."""
    manager = _settings_manager(scope, primary)
    with (
        patch(f"{_PATHS}.get_journal_data_directory", return_value=data_dir),
        patch(f"{_DB_UTILS}.get_settings_manager", return_value=manager),
    ):
        yield


@pytest.mark.parametrize(
    ("snapshot", "db_state", "scope", "expected_blocked"),
    [
        # Nothing on disk: the read would download, so the scope decides.
        (False, "missing", "private_only", True),
        (False, "missing", "strict", True),
        (False, "missing", "public_only", False),
        # ADAPTIVE (the default) with a public primary resolves
        # PUBLIC_ONLY. The private-primary half is
        # test_gate_resolves_adaptive_against_the_primary_engine.
        (False, "missing", "adaptive", False),
        # Unparseable stored value still fails closed.
        (False, "missing", "not-a-scope", True),
        # The file exists but `_ensure_engine` would REBUILD it. Gating on
        # `.exists()` alone answers "not blocked" here and lets a schema
        # bump turn one dashboard page load into the full download.
        (False, "stale_schema", "private_only", True),
        (False, "corrupt", "private_only", True),
        (False, "stale_schema", "public_only", False),
        # A valid file is read, never rebuilt: no build, no download.
        (False, "valid", "private_only", False),
        # Snapshot on disk: any build compiles from it, offline. Blocking
        # these would darken the dashboard of the very users this gate
        # exists to protect, for no egress benefit.
        (True, "missing", "private_only", False),
        (True, "stale_schema", "private_only", False),
        (True, "corrupt", "strict", False),
        (True, "valid", "private_only", False),
    ],
)
def test_gate_predicts_a_download_not_a_missing_file(
    tmp_path, snapshot, db_state, scope, expected_blocked
):
    """The gate must answer "would this read DOWNLOAD?", not "is the file
    there?".

    The two questions differ in both directions, and each direction is a
    real defect: a stale-schema file is "there" but rebuilds (and, with
    no snapshot, downloads), while a missing file with the snapshot
    beside it never touches the network.
    """
    if snapshot:
        _write_snapshot(tmp_path)
    _DB_STATES[db_state](tmp_path / DB_FILENAME)

    with _gate_env(tmp_path, scope=scope):
        blocked = metrics._journal_ref_db_read_would_download("alice")

    assert blocked is expected_blocked


@pytest.mark.parametrize(
    ("scope", "primary", "expected_blocked"),
    [
        # A public primary: ADAPTIVE resolves PUBLIC_ONLY -> allowed.
        ("adaptive", "arxiv", False),
        ("adaptive", "searxng", False),
        # A private primary: ADAPTIVE resolves PRIVATE_ONLY, exactly as
        # the research worker's armed context and the reputation filter's
        # `_should_skip_journal_fetch_for_scope` resolve it. Gating on the
        # literal stored "adaptive" let a dashboard page load start the
        # download those two refuse.
        ("adaptive", "library", True),
        ("adaptive", "collection_abc", True),
        # No primary engine: the scope cannot be resolved -> fail closed,
        # like the worker, which refuses such a run.
        ("adaptive", None, True),
        ("public_only", None, True),
    ],
)
def test_gate_resolves_adaptive_against_the_primary_engine(
    tmp_path, scope, primary, expected_blocked
):
    """The read-route gate must see the scope a run would see, not the
    literal stored value."""
    with _gate_env(tmp_path, scope=scope, primary=primary):
        blocked = metrics._journal_ref_db_read_would_download("alice")

    assert blocked is expected_blocked


def test_gate_fails_closed_when_settings_cannot_be_read(tmp_path):
    """A strict snapshot read that raises (SQLCipher session unusable)
    must answer "blocked", not fall back to a permissive default."""
    manager = _settings_manager("public_only")
    manager.get_settings_snapshot.side_effect = RuntimeError("db locked")
    with (
        patch(f"{_PATHS}.get_journal_data_directory", return_value=tmp_path),
        patch(f"{_DB_UTILS}.get_settings_manager", return_value=manager),
    ):
        assert metrics._journal_ref_db_read_would_download("alice") is True
    manager.get_settings_snapshot.assert_called_once_with(strict=True)


@pytest.mark.parametrize("db_state", ["stale_schema", "corrupt"])
def test_gate_probe_does_not_delete_the_users_database(tmp_path, db_state):
    """Asking the question must not destroy the answer.

    ``JournalQualityDB._validate_existing_db`` unlinks an unusable file
    as part of deciding it is unusable — correct for the build path,
    unacceptable on a read route, where a GET would silently delete the
    user's reference DB. The gate uses ``journal_db_file_is_valid``, the
    read-only half, so the file is still there afterwards.
    """
    db_path = tmp_path / DB_FILENAME
    _DB_STATES[db_state](db_path)

    with _gate_env(tmp_path, scope="private_only"):
        assert metrics._journal_ref_db_read_would_download("alice") is True

    assert db_path.exists(), (
        "the read-route gate deleted the user's reference DB as a side "
        "effect of probing it"
    )


@pytest.mark.parametrize("db_state", ["missing", "stale_schema"])
def test_snapshot_present_lets_the_read_reach_the_local_build(
    tmp_path, db_state
):
    """The permissive half of the gate, pinned against the build itself.

    With the snapshot on disk the gate says "not blocked" — and the read
    it lets through really does run the build. That build is local:
    ``ensure_journal_data`` returns "available" on the snapshot without a
    request (``test_lazy_build_still_builds_locally_under_private_only``
    pins that end). Gate and build have to agree, or the gate is either
    blocking an offline rebuild or waving through a download.
    """
    _write_snapshot(tmp_path)
    _DB_STATES[db_state](tmp_path / DB_FILENAME)

    with (
        _gate_env(tmp_path, scope="private_only"),
        patch(f"{_DB}.JournalQualityDB._build_or_raise") as build,
    ):
        assert metrics._journal_ref_db_read_would_download("alice") is False
        # A FRESH instance, never the module singleton: a leaked engine
        # would short-circuit `_ensure_engine` and hide the call.
        JournalQualityDB()._ensure_engine()

    assert build.call_count == 1, (
        "the read the gate allowed did not reach the build — the local "
        "rebuild from present snapshots is not happening"
    )


@pytest.mark.parametrize("route", _READ_ROUTES)
def test_invalid_db_without_snapshot_is_treated_as_missing(tmp_path, route):
    """The upgrade path this gate exists for.

    Schema bump + snapshots deleted (a configuration
    ``get_journal_data_status`` explicitly supports: "The compiled DB is
    useful even when the source JSON has been deleted") + a forbidding
    scope. Gating on ``.exists()`` answers "not blocked" here and a
    dashboard page load starts the several-hundred-MB fetch.
    """
    _write_stale_schema_db(tmp_path / DB_FILENAME)

    with _dashboard(scope="private_only", data_dir=tmp_path) as env:
        with _one_paper_row():
            result = _call(route)

    env.build.assert_not_called()
    if route == "journals":
        assert isinstance(result, JSONResponse)
        assert result.status_code == 503
    else:
        env.ref_db_getter.assert_not_called()
        assert result["status"] == "success"
        assert result["reference_data_available"] is False


@pytest.mark.parametrize("db_state", ["missing", "stale_schema"])
@pytest.mark.parametrize("route", _READ_ROUTES)
def test_snapshot_present_read_proceeds_under_a_forbidding_scope(
    tmp_path, route, db_state
):
    """Both of the limits the previous shape of this gate carried.

    ``db_state="missing"`` is a forbidding-scope user who has the
    snapshots but no compiled DB: the old gate answered "unavailable"
    where a purely local build was available.
    ``db_state="stale_schema"`` is the same user after an upgrade. Under
    both, the read proceeds.
    """
    _write_snapshot(tmp_path)
    _DB_STATES[db_state](tmp_path / DB_FILENAME)

    with (
        _dashboard(scope="private_only", data_dir=tmp_path) as env,
        _one_paper_row(),
        _stub_reference_db() as ref,
    ):
        result = _call(route)

    _assert_reached_the_reference_db(route, env, result, ref)


def test_forbidding_scope_is_what_stops_the_download(tmp_path):
    """The discriminating pair, with the real ``_build_or_raise``.

    Every other route test in this module stubs ``_build_or_raise``, the
    only production path from these routes to ``download_journal_data``,
    which makes any "the downloader was not called" assertion there
    vacuous. Here the build is REAL and only the downloader itself is
    stubbed, so the two halves differ in exactly one thing — the
    persisted scope — and only one of them reaches the network call.

    Both halves end in a 503 (the permitted half's stubbed download
    "fails"), so the 503 proves nothing on its own; the call count is
    the whole evidence.
    """
    import local_deep_research.journal_quality.downloader as dl

    def run(scope: str) -> int:
        manager = _settings_manager(scope)
        # Not in conftest's reset_all_singletons: a negative entry from
        # the first half would answer the second half from cache and
        # never call the downloader at all.
        dl._ensure_cache = None
        with (
            patch(f"{_DB_UTILS}.get_settings_manager", return_value=manager),
            patch(
                f"{_PATHS}.get_journal_data_directory", return_value=tmp_path
            ),
            patch(f"{_TESTING}.testing_with_mocks", return_value=False),
            # A fresh instance, so `_ensure_engine` cannot be satisfied
            # by an engine another test left on the module singleton.
            patch(
                f"{_DB}.get_journal_reference_db",
                return_value=JournalQualityDB(),
            ),
            patch(
                f"{_DOWNLOADER}.download_journal_data",
                return_value=(
                    False,
                    "stubbed: this test never uses the network",
                ),
            ) as download,
        ):
            result = metrics.api_journal_quality(_request(), username="alice")
        assert isinstance(result, JSONResponse)
        assert result.status_code == 503
        return download.call_count

    try:
        # tmp_path has neither the snapshot nor a compiled DB, so a read
        # that gets past the gate has nothing local to build from.
        assert run("private_only") == 0, (
            "a dashboard page load under a scope that forbids public "
            "egress reached download_journal_data"
        )
        assert run("public_only") == 1, (
            "positive control: with no snapshot and no DB, a permitted "
            "scope must still reach the downloader — otherwise 'never "
            "download' would pass the assertion above for free"
        )
    finally:
        dl._ensure_cache = None


@pytest.mark.parametrize("route", ("user-research", "per-research"))
def test_withheld_reference_db_is_flagged_in_the_response(tmp_path, route):
    """``predatory_blocked: 0`` must not read as a clean bill of health.

    With the reference DB withheld, these routes answer ``status:
    success`` and ``predatory_blocked: 0`` — a count of nothing that was
    never counted. The route's own comment calls silently undercounting
    predatory journals a no-fallbacks violation, so the response says
    which it is.
    """
    with _dashboard(scope="private_only", data_dir=tmp_path) as env:
        with _one_paper_row():
            withheld = _call(route)

    env.ref_db_getter.assert_not_called()
    assert withheld["summary"]["predatory_blocked"] == 0
    assert withheld["reference_data_available"] is False

    # Same user, same zero count, but the reference DB was consulted.
    # A 0-byte file passes the validity probe (user_version 0 is
    # grandfathered), so the gate lets the read through.
    (tmp_path / DB_FILENAME).touch()
    ref = MagicMock()
    ref.lookup_sources_batch.return_value = {}
    ref.count_predatory_by_names.return_value = 0
    with (
        _dashboard(scope="private_only", data_dir=tmp_path),
        _one_paper_row(),
        # `_dashboard` stubs `_get_ref_db_or_none` to None; hand these
        # routes a reference DB that is actually there instead.
        patch.object(metrics, "_get_ref_db_or_none", return_value=ref),
    ):
        available = _call(route)

    assert available["summary"]["predatory_blocked"] == 0
    assert available["reference_data_available"] is True, (
        "the flag must distinguish 'checked, found none' from 'never "
        "checked' — both report predatory_blocked: 0"
    )


@pytest.mark.parametrize("route", ("user-research", "per-research"))
def test_failed_lazy_build_is_flagged_in_the_response(tmp_path, route):
    """A reference DB that could not be built is "not checked" too.

    Under a permitting scope the gate lets the read through, and
    ``_get_ref_db_or_none`` hands back the singleton without opening
    anything. When the lazy build then fails — no snapshot and the
    network down, say — the lookups swallow the ``FileNotFoundError``
    into ``{}`` / ``0``. Keying the flag on the handle being non-None
    reported that as ``reference_data_available: true`` with
    ``predatory_blocked: 0``: the "checked, found none" reading the flag
    exists to rule out.
    """
    from local_deep_research.journal_quality.db import JournalQualityDB

    ref = JournalQualityDB()
    with (
        _dashboard(scope="public_only", data_dir=tmp_path),
        _one_paper_row(),
        patch(
            f"{_DB}.JournalQualityDB._build_or_raise",
            side_effect=FileNotFoundError(
                "Journal data files not available. Check your network "
                "connection or download manually from the dashboard."
            ),
        ) as build,
        patch.object(metrics, "_get_ref_db_or_none", return_value=ref),
    ):
        result = _call(route)

    build.assert_called()  # the read really reached the failing build
    assert result["status"] == "success"
    assert result["summary"]["predatory_blocked"] == 0
    assert result["reference_data_available"] is False, (
        "a reference DB whose lazy build failed was reported as checked"
    )