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.

19.0 KB
Raw Blame
"""Exercise journal DB compilation without relying on downloaded snapshots.

The legacy build tests skip when the large bundled database is absent, which
leaves the writer, compact-input loaders, and atomic replacement path dark in
CI. These tests create the smallest representative snapshots under pytest's
temporary directory and compile them through the production build entrypoint.
"""

from __future__ import annotations

import gzip
import json
import os
import sqlite3
import stat
from contextlib import closing
from pathlib import Path
from typing import Iterator, Mapping

import pytest
from sqlalchemy import text
from sqlalchemy.exc import OperationalError

from local_deep_research.journal_quality import db as db_module
from local_deep_research.journal_quality.db import (
    JOURNAL_DB_VALID,
    JOURNAL_QUALITY_SCHEMA_VERSION,
    JournalQualityDB,
    _load_abbreviations,
    _load_doaj,
    _load_institutions,
    _load_openalex,
    _load_predatory,
    build_db,
    journal_db_file_status,
)


type JsonValue = (
    None | bool | int | float | str | list[JsonValue] | dict[str, JsonValue]
)


def _write_gzip_json(path: Path, payload: Mapping[str, JsonValue]) -> None:
    with gzip.open(path, "wt", encoding="utf-8") as stream:
        json.dump(payload, stream)


@pytest.fixture()
def compact_snapshots(tmp_path: Path) -> Path:
    _write_gzip_json(
        tmp_path / "openalex_sources.json.gz",
        {
            "s": {
                "S1": {
                    "n": "Listed Trap",
                    "i": "1111-1111",
                    "p": "Ordinary Press",
                    "h": 5,
                    "cb": 10,
                    "t": "j",
                },
                "S2": {
                    "n": "Publisher Victim",
                    "i": "2222-2222",
                    "p": "Dubious Publishing Group",
                    "h": 7,
                    "cb": 20,
                    "t": "c",
                },
                "S3": {
                    "n": "Hijacked Clone",
                    "i": "3333-3333",
                    "h": 9,
                    "cb": 30,
                },
                "S4": {
                    "n": "Duplicate Venue",
                    "i": "4444-4444",
                    "h": 2,
                    "cb": 40,
                },
                "S5": {
                    "n": "Duplicate Venue",
                    "i": "4444-4444",
                    "h": 20,
                    "cb": 50,
                },
                "S6": {"n": "  ", "i": "5555-5555"},
            }
        },
    )
    (tmp_path / "doaj_journals.json").write_text(
        json.dumps(
            {
                "journals": {
                    "11111111": {
                        "name": "Listed Trap",
                        "publisher": "Ordinary Press",
                    },
                    "66666666": {
                        "name": "DOAJ Only",
                        "publisher": "Open Press",
                    },
                    "77777777": {"name": "   "},
                }
            }
        ),
        encoding="utf-8",
    )
    (tmp_path / "predatory.json").write_text(
        json.dumps(
            {
                "journals": [{"name": "Listed Trap"}, {"name": ""}],
                "publishers": [
                    {"name": "Dubious Publishing Group"},
                    {"name": "Tiny"},
                ],
                "hijacked": [{"hijacked_name": "Hijacked Clone"}],
            }
        ),
        encoding="utf-8",
    )
    _write_gzip_json(
        tmp_path / "openalex_institutions.json.gz",
        {
            "i": {
                "I1": {
                    "n": "Example University",
                    "r": "01example",
                    "c": "US",
                    "t": "education",
                    "h": 500,
                    "w": 1000,
                    "cb": 2000,
                },
                "I2": {"n": " "},
            }
        },
    )
    _write_gzip_json(
        tmp_path / "jabref_abbreviations.json.gz",
        {
            "abbrev_to_full": {
                "J. Test.": "Journal of Testing",
                "J. Test.": "Duplicate Normalized Abbreviation",
                "": "Ignored",
            }
        },
    )
    return tmp_path


def test_build_compiles_compact_snapshots_and_replaces_existing_file(
    compact_snapshots: Path,
) -> None:
    output = compact_snapshots / "compiled.db"
    output.write_text("old database", encoding="utf-8")

    build_db(data_dir=compact_snapshots, output_path=output)

    assert stat.S_IMODE(output.stat().st_mode) == 0o444
    with closing(
        sqlite3.connect(f"file:{output}?mode=ro", uri=True)
    ) as connection:
        assert connection.execute("PRAGMA user_version").fetchone()[0] == (
            JOURNAL_QUALITY_SCHEMA_VERSION
        )
        rows = connection.execute(
            "SELECT name, h_index, is_in_doaj, is_predatory, predatory_source "
            "FROM sources ORDER BY name"
        ).fetchall()
        assert rows == [
            ("DOAJ Only", None, 1, 0, None),
            ("Duplicate Venue", 20, 0, 0, None),
            ("Hijacked Clone", 9, 0, 1, "stop-predatory-hijacked"),
            ("Listed Trap", 5, 1, 0, None),
            (
                "Publisher Victim",
                7,
                0,
                1,
                "stop-predatory-publishers",
            ),
        ]
        assert (
            connection.execute("SELECT COUNT(*) FROM institutions").fetchone()[
                0
            ]
            == 1
        )
        assert (
            connection.execute("SELECT COUNT(*) FROM abbreviations").fetchone()[
                0
            ]
            == 1
        )


def test_built_database_opens_through_read_only_accessor(
    compact_snapshots: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    output = compact_snapshots / "compiled.db"
    build_db(data_dir=compact_snapshots, output_path=output)
    database = JournalQualityDB()
    monkeypatch.setattr(database, "_resolve_db_path", lambda: output)

    result = database.lookup_openalex(source_id="https://openalex.org/S5")

    assert result is not None
    assert result["name"] == "Duplicate Venue"

    # The module's headline invariant (db.py:3-6: "this class never
    # writes") — this is the one fail-on-revert coverage of it that
    # runs in clean CI (the sibling in test_db.py uses `ref_db`, which
    # skips there). Drive a real write through the same `mode=ro&
    # immutable=1` connection `_make_ro_conn` builds, against a real
    # table in the just-built file, and confirm SQLite itself refuses.
    with pytest.raises(OperationalError, match="readonly"):
        with database.session() as session:
            session.execute(
                text(
                    "INSERT INTO sources (name, name_lower, score_source) "
                    "VALUES ('x', 'x', 'openalex')"
                )
            )
            session.commit()

    database.reset()


@pytest.mark.parametrize(
    "dir_name",
    ["x?y", "x#y", "x%41y", "a b"],
    ids=["question-mark", "hash", "percent-escape", "space"],
)
def test_build_and_engine_open_under_uri_hostile_directory(
    compact_snapshots: Path, monkeypatch: pytest.MonkeyPatch, dir_name: str
) -> None:
    """Every SQLite URI the module builds must survive the data dir name.

    ``build_db``'s post-build count and ``_ensure_engine``'s
    ``_make_ro_conn`` used to interpolate the raw path into
    ``f"file:{path}?mode=ro&immutable=1"``. A ``?`` or ``#`` in a parent
    directory then truncated the URI — dropping ``mode=ro``/``immutable``,
    creating a stray 0-byte file at the truncated path and failing "no
    such table" — while the validity probe (already percent-encoded) said
    VALID. A literal ``%41`` was decoded to ``A``, opening the wrong path.
    Build and engine now share ``sqlite_readonly_uri``.
    """
    hostile = compact_snapshots / dir_name / "inner"
    hostile.mkdir(parents=True)
    output = hostile / "compiled.db"

    def files() -> set[str]:
        return {
            str(p.relative_to(compact_snapshots))
            for p in compact_snapshots.rglob("*")
            if p.is_file()
        }

    before_build = files()
    build_db(data_dir=compact_snapshots, output_path=output)
    after_build = files()
    assert after_build - before_build == {
        str(output.relative_to(compact_snapshots))
    }, "build_db left a file other than its output behind"

    database = JournalQualityDB()
    monkeypatch.setattr(database, "_resolve_db_path", lambda: output)
    try:
        result = database.lookup_openalex(source_id="https://openalex.org/S5")
        assert result is not None
        assert result["name"] == "Duplicate Venue"
        # Still mode=ro through the hostile path: SQLite refuses a write.
        with pytest.raises(OperationalError, match="readonly"):
            with database.session() as session:
                session.execute(
                    text(
                        "INSERT INTO sources (name, name_lower, "
                        "score_source) VALUES ('x', 'x', 'openalex')"
                    )
                )
                session.commit()
    finally:
        database.reset()

    assert files() == after_build, (
        "opening the engine created or removed a file — a truncated "
        "file: URI opened a stray path instead of the built DB"
    )
    stray = [
        p
        for p in compact_snapshots.rglob("*")
        if p.is_file() and p.stat().st_size == 0
    ]
    assert stray == [], f"stray 0-byte file(s) created: {stray}"


@pytest.mark.skipif(
    os.name == "nt",
    reason="non-UTF-8 bytes in a directory name are a POSIX-only shape",
)
def test_build_probe_and_engine_open_under_non_utf8_directory(
    compact_snapshots: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """A data dir whose name is not valid UTF-8 must still be served.

    On POSIX ``os.fsdecode(b"data\\xe9")`` is ``"data\\udce9"`` (a lone
    surrogate). ``sqlite_readonly_uri`` used to ``quote()`` that ``str``,
    which raises ``UnicodeEncodeError``; the probe then reported the
    valid DB as UNOPENABLE on every access and the engine could not
    open it at all — while a plain ``sqlite3.connect(str_path)`` (and
    the pre-URI code) opened it fine. The URI now carries the exact
    filesystem bytes.
    """
    hostile = compact_snapshots / os.fsdecode(b"data\xe9")
    try:
        hostile.mkdir()
    except (OSError, UnicodeEncodeError):
        pytest.skip("filesystem rejects non-UTF-8 directory names")
    output = hostile / "compiled.db"

    def files() -> set[bytes]:
        return {
            os.fsencode(p.relative_to(compact_snapshots))
            for p in compact_snapshots.rglob("*")
            if p.is_file()
        }

    before_build = files()
    build_db(data_dir=compact_snapshots, output_path=output)
    after_build = files()
    assert after_build - before_build == {
        os.fsencode(output.relative_to(compact_snapshots))
    }, "build_db left a file other than its output behind"

    assert journal_db_file_status(output) == JOURNAL_DB_VALID

    database = JournalQualityDB()
    monkeypatch.setattr(database, "_resolve_db_path", lambda: output)
    try:
        result = database.lookup_openalex(source_id="https://openalex.org/S5")
        assert result is not None
        assert result["name"] == "Duplicate Venue"
    finally:
        database.reset()

    assert files() == after_build, (
        "probing or opening the engine created or removed a file"
    )


def test_optional_loaders_return_empty_when_snapshots_are_absent(
    tmp_path: Path,
) -> None:
    with pytest.raises(
        FileNotFoundError, match="OpenAlex source file not found"
    ):
        _load_openalex(tmp_path)

    assert _load_doaj(tmp_path) == {}
    assert _load_predatory(tmp_path) == {
        "journals": set(),
        "publishers": set(),
        "hijacked": set(),
        "long_pubs": [],
    }
    assert _load_institutions(tmp_path) == {}
    assert _load_abbreviations(tmp_path) == {}


@pytest.fixture()
def loguru_sink() -> Iterator[list]:
    """Capture loguru log records — caplog doesn't see loguru output.

    Same technique as the sibling fixture in
    ``TestStaleDataVersionWarning`` (test_db.py:754), duplicated here
    since pytest fixtures defined on a class in another module aren't
    importable across files.
    """
    from loguru import logger as loguru_logger

    records: list = []

    def _sink(message):
        records.append(message.record)

    handler_id = loguru_logger.add(_sink, level="WARNING", diagnose=False)
    loguru_logger.enable("local_deep_research")
    yield records
    loguru_logger.disable("local_deep_research")
    loguru_logger.remove(handler_id)


@pytest.mark.parametrize(
    ("filename", "loader"),
    [
        ("openalex_sources.json.gz", _load_openalex),
        ("openalex_institutions.json.gz", _load_institutions),
        ("jabref_abbreviations.json.gz", _load_abbreviations),
    ],
)
def test_loaders_refuse_a_snapshot_that_expands_past_its_bound(
    tmp_path: Path, filename: str, loader
) -> None:
    # An empty object followed by 64 MiB of whitespace is valid JSON and
    # gzips to about 64 KiB, far past 32 times its compressed size.
    with gzip.open(tmp_path / filename, "wt", encoding="utf-8") as stream:
        stream.write("{}")
        for _ in range(64):
            stream.write(" " * 1024 * 1024)

    with pytest.raises(ValueError, match="decompresses past"):
        loader(tmp_path)


@pytest.mark.parametrize(
    ("filename", "loader"),
    [
        ("openalex_sources.json.gz", _load_openalex),
        ("openalex_institutions.json.gz", _load_institutions),
        ("jabref_abbreviations.json.gz", _load_abbreviations),
    ],
)
def test_loaders_refuse_a_snapshot_that_is_not_a_json_object(
    tmp_path: Path, filename: str, loader
) -> None:
    with gzip.open(tmp_path / filename, "wt", encoding="utf-8") as stream:
        stream.write('["not", "an", "object"]')

    with pytest.raises(ValueError, match="is not a JSON object"):
        loader(tmp_path)


@pytest.mark.parametrize(
    ("ratio", "slack", "ceiling"),
    [
        (0, 20, db_module._GZIP_SNAPSHOT_MAX_CHARS),
        (10**6, 0, 20),
    ],
    ids=["size-derived-bound", "fixed-ceiling"],
)
def test_snapshot_bound_admits_exactly_its_cap(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
    ratio: int,
    slack: int,
    ceiling: int,
) -> None:
    monkeypatch.setattr(db_module, "_GZIP_SNAPSHOT_RATIO", ratio)
    monkeypatch.setattr(db_module, "_GZIP_SNAPSHOT_SLACK_CHARS", slack)
    monkeypatch.setattr(db_module, "_GZIP_SNAPSHOT_MAX_CHARS", ceiling)
    path = tmp_path / "openalex_sources.json.gz"
    payload = '{"s": {"S1": {}}}   '
    assert len(payload) == 20

    with gzip.open(path, "wt", encoding="utf-8") as stream:
        stream.write(payload)
    assert _load_openalex(tmp_path) == {"S1": {}}

    with gzip.open(path, "wt", encoding="utf-8") as stream:
        stream.write(payload + " ")
    with pytest.raises(ValueError, match="decompresses past 20 characters"):
        _load_openalex(tmp_path)


class _ReadRecorder:
    """A gzip text handle that records the sizes it is asked to read."""

    def __init__(self, handle) -> None:
        self.handle = handle
        self.sizes: list[int] = []

    def __enter__(self) -> "_ReadRecorder":
        return self

    def __exit__(self, *exc_info) -> None:
        self.handle.close()

    def read(self, size: int = -1) -> str:
        self.sizes.append(size)
        return self.handle.read(size)


def test_snapshot_read_stops_one_character_past_the_cap(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    # The refusal alone would still decompress the whole file first; the
    # read itself has to stop.
    monkeypatch.setattr(db_module, "_GZIP_SNAPSHOT_RATIO", 0)
    monkeypatch.setattr(db_module, "_GZIP_SNAPSHOT_SLACK_CHARS", 20)
    with gzip.open(
        tmp_path / "openalex_sources.json.gz", "wt", encoding="utf-8"
    ) as stream:
        stream.write("{}" + " " * 10_000)
    recorders: list[_ReadRecorder] = []
    real_open = gzip.open

    def recording_open(*args, **kwargs) -> _ReadRecorder:
        recorders.append(_ReadRecorder(real_open(*args, **kwargs)))
        return recorders[-1]

    monkeypatch.setattr(db_module.gzip, "open", recording_open)

    with pytest.raises(ValueError, match="decompresses past"):
        _load_openalex(tmp_path)
    assert [recorder.sizes for recorder in recorders] == [[21]]


def test_malformed_version_metadata_is_ignored(
    tmp_path: Path, loguru_sink: list
) -> None:
    (tmp_path / "version.json").write_text("{broken", encoding="utf-8")
    database = JournalQualityDB()

    database._warn_on_stale_data_version(tmp_path)

    assert database._stale_version_warned is False
    # Like its siblings (test_db.py:779-835): a malformed version.json
    # must fail silently, not emit a spurious WARNING.
    assert not loguru_sink


def test_reset_db_forwards_to_existing_singleton(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    calls: list[bool] = []
    database = JournalQualityDB()
    monkeypatch.setattr(database, "reset", lambda: calls.append(True))
    monkeypatch.setattr(db_module, "_db", database)

    db_module.reset_db()

    assert calls == [True]


def test_deprecated_build_alias_forwards_paths(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    calls: list[tuple[Path, Path]] = []
    output = tmp_path / "alias.db"
    monkeypatch.setattr(
        db_module,
        "build_db",
        lambda *, data_dir, output_path: calls.append((data_dir, output_path)),
    )

    db_module.build_reference_db(data_dir=tmp_path, output_path=output)

    assert calls == [(tmp_path, output)]


def test_failed_build_removes_its_unique_temporary_database(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    _write_gzip_json(tmp_path / "openalex_sources.json.gz", {"s": ["invalid"]})
    output = tmp_path / "failed.db"

    # Record (rather than fix) what secrets.token_hex(4) actually
    # returns, so the test still observes real per-build uniqueness
    # instead of asserting against a value it dictated itself. This
    # also stays a passthrough — it does not stub out the module-level
    # `secrets` import build_db relies on for its next real caller.
    real_token_hex = db_module.secrets.token_hex
    generated: list[str] = []

    def recording_token_hex(length: int) -> str:
        value = real_token_hex(length)
        generated.append(value)
        return value

    monkeypatch.setattr(db_module.secrets, "token_hex", recording_token_hex)

    for _ in range(2):
        with pytest.raises(ValueError, match="dictionary update sequence"):
            build_db(data_dir=tmp_path, output_path=output)

    assert not output.exists()
    assert not list(tmp_path.glob("failed.db.tmp-*"))
    # Two independent failed builds must not reuse the same temp name —
    # this is what secrets.token_hex(4) buys over os.getpid() alone
    # (which is constant across both calls in this one test process).
    assert len(generated) == 2
    assert generated[0] != generated[1]