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.

24.9 KB
Raw Blame
"""``save_research_sources`` against engine dicts carrying non-str values.

Nine commits guarded one read at a time — url, then snippet, then
ct_matched, then title — while their siblings kept raising inside the
broad per-source ``except``, which rolls back and drops the citation with
only a counter to show for it. Every one of those guards was pinned by a
test that re-implemented the caller's expression instead of calling it, so
the suite stayed green while the drop continued.

These tests call the real function against a real in-memory session. The
assertion is the one that matters: a row exists.
"""

from __future__ import annotations

from unittest.mock import patch

from decimal import Decimal
from fractions import Fraction

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models.citation import (
    Base as CitationBase,
)
from local_deep_research.database.models.library import Base
from local_deep_research.database.models.research import (
    Base as ResearchBase,
)
from local_deep_research.web.services.research_sources_service import (
    ResearchSourcesService,
)


@pytest.fixture
def session():
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    CitationBase.metadata.create_all(engine)
    ResearchBase.metadata.create_all(engine)
    maker = sessionmaker(bind=engine)
    db = maker()
    yield db
    db.close()


def _save(session, sources):
    """Run the real service against *session*."""

    class _Ctx:
        def __enter__(self):
            return session

        def __exit__(self, *exc):
            return False

    with patch(
        "local_deep_research.web.services.research_sources_service"
        ".get_user_db_session",
        return_value=_Ctx(),
    ):
        return ResearchSourcesService.save_research_sources(
            "research-1", sources, username="tester"
        )


@pytest.mark.parametrize(
    "bad_url",
    [["/library/document/doc1"], {"href": "x"}, 123, True, b"x"],
)
def test_non_str_url_still_saves_the_citation(session, bad_url):
    """The guard added for this moved the exception from ``flush()`` to
    ``normalize_citation``'s re-read of the same field, 50 lines later —
    same ``try``, same rollback, same silent drop."""
    saved = _save(
        session,
        [{"url": bad_url, "title": "Paper", "source_engine": "retriever"}],
    )

    assert saved == 1


@pytest.mark.parametrize(
    "engine, field, value",
    [
        # Each engine is the one whose extractor actually reads that
        # field. An arxiv_id under source_engine="pubmed" is never
        # extracted, so such a case cannot fail under any mutation.
        ("pubmed", "pmid", ["12345678"]),
        ("arxiv", "arxiv_id", ["2401.00001"]),
        ("pubmed", "title", ["Nature paper"]),
        ("pubmed", "snippet", 123),
        ("pubmed", "source_type", ["web"]),
        ("pubmed", "journal", ["Nature", "Nature Physics"]),
        ("pubmed", "volume", {1, 2}),
        ("pubmed", "metadata", "not-a-dict"),
        # The tree, not just the top level: both reach the same JSON
        # column and the same normalize_citation call.
        ("pubmed", "metadata", {"journal": {1, 2}}),
        ("pubmed", "authors", [{"family": {1, 2}}]),
        ("pubmed", "authors", [{"name": 12345}]),
    ],
)
def test_non_str_sibling_fields_still_save(session, engine, field, value):
    """Each of these reaches a slice, a dict key, a regex, a Text column or
    ``json.dumps`` in the same ``try`` as the url."""
    saved = _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": engine,
                field: value,
            }
        ],
    )

    assert saved == 1


@pytest.mark.parametrize(
    "engine, field, value, expected",
    [
        ("pubmed", "pmid", ["12345678"], "12345678"),
        ("arxiv", "arxiv_id", ["2401.00001"], "2401.00001"),
        ("nasa_ads", "doi", ["10.1234/test", "10.5678/x"], "10.1234/test"),
    ],
)
def test_identifier_columns_are_not_stringified(
    session, engine, field, value, expected
):
    """These feed ``unique`` columns and the dedup SELECT. Writing a repr
    into one is worse than dropping the citation: it is permanent, and it
    defeats the dedup the Paper table exists for.

    Asserting the stored VALUE, not just that a row appeared — the earlier
    version of this file only counted rows, so a poisoned key was
    invisible to it.
    """
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": engine,
                field: value,
            }
        ],
    )

    stored = session.query(Paper).all()
    assert stored, "no Paper row was created"
    assert getattr(stored[0], field) == expected


def test_same_paper_from_two_engines_still_dedups(session):
    """A list identifier from one engine and a str from another are the
    same paper. Stringifying the list made them two rows."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "Paper",
                "source_engine": "pubmed",
                "pmid": ["12345678"],
            },
            {
                "url": "https://b.test/p",
                "title": "Paper",
                "source_engine": "pubmed",
                "pmid": "12345678",
            },
        ],
    )

    assert session.query(Paper).count() == 1


def test_falsy_non_str_url_is_still_skipped(session):
    """Coercion must not turn an empty list into the truthy string
    ``"[]"``, which would persist a row that previously did not exist."""
    saved = _save(session, [{"link": [], "title": "Paper"}])

    assert saved == 0


def test_ordinary_source_is_unaffected(session):
    saved = _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "snippet": "text",
            }
        ],
    )

    assert saved == 1


@pytest.mark.parametrize(
    "engine, field, value",
    [
        # authors_csl is read BEFORE authors by normalize_citation and is
        # NASA ADS's primary author channel.
        ("pubmed", "authors_csl", [{"family": {1, 2}}]),
        ("pubmed", "authors_csl", [{"suffix": {1, 2}}]),
        ("pubmed", "authors_csl", [{"name": 12345}]),
        # display_name is the sub-field _parse_name actually strips.
        ("pubmed", "authors", [{"display_name": 12345}]),
        # Author dicts also arrive nested under metadata, where a
        # structural walk alone left .strip() to raise.
        ("pubmed", "metadata", {"authors": [{"name": 12345}]}),
        ("pubmed", "metadata", {"authors_csl": [{"name": 12345}]}),
    ],
)
def test_author_channels_and_nesting_still_save(session, engine, field, value):
    saved = _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": engine,
                field: value,
            }
        ],
    )

    assert saved == 1


def test_external_ids_doi_is_not_stringified(session):
    """``external_ids`` is the second DOI source ``_extract_doi``
    documents. Normalizing only ``source["doi"]`` left a list DOI reaching
    the unique ``Paper.doi`` by the other channel."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": "semantic_scholar",
                "external_ids": {"DOI": ["10.1234/test"]},
            }
        ],
    )

    stored = session.query(Paper).all()
    assert stored, "no Paper row was created"
    assert stored[0].doi == "10.1234/test"


def test_same_doi_via_different_channels_dedups(session):
    """One engine reporting it under external_ids and another under doi is
    the same paper."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "Paper",
                "source_engine": "semantic_scholar",
                "external_ids": {"DOI": ["10.1234/test"]},
            },
            {
                "url": "https://b.test/p",
                "title": "Paper",
                "source_engine": "nasa_ads",
                "doi": "10.1234/test",
            },
        ],
    )

    assert session.query(Paper).count() == 1


@pytest.mark.parametrize("falsy", [0, False, {}, "", []])
def test_falsy_identifier_does_not_suppress_the_doi_fallback(session, falsy):
    """``_extract_doi`` treats a falsy doi as missing and falls through to
    external_ids. Coercing it to a truthy repr both suppressed that and let
    two DIFFERENT papers match on the same string."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": "semantic_scholar",
                "doi": falsy,
                "external_ids": {"DOI": "10.9999/real"},
            }
        ],
    )

    stored = session.query(Paper).all()
    assert stored, "no Paper row was created"
    assert stored[0].doi == "10.9999/real"


def test_two_distinct_papers_with_falsy_dois_do_not_merge(session):
    """Both coerced to the same repr, ``_find_existing_paper`` matched them
    — one row for two papers, worse than the duplicate it replaced."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "First",
                "source_engine": "pubmed",
                "doi": {},
                "pmid": "111",
            },
            {
                "url": "https://b.test/p",
                "title": "Second",
                "source_engine": "pubmed",
                "doi": {},
                "pmid": "222",
            },
        ],
    )

    assert session.query(Paper).count() == 2


@pytest.mark.parametrize("key", ["name", "display_name"])
@pytest.mark.parametrize("field", ["authors", "authors_csl"])
def test_null_author_field_still_saves(session, key, field):
    """``{"name": null}`` is the ordinary JSON shape for a missing author
    field, and ``_parse_name`` calls ``.strip()`` on it unguarded."""
    saved = _save(
        session,
        [
            {
                "url": "https://example.test/paper",
                "title": "Paper",
                "source_engine": "pubmed",
                field: [{key: None}],
            }
        ],
    )

    assert saved == 1


@pytest.mark.parametrize("bad", [{"a": 1}, {1, 2}, [["10.1/x"]], True, 0.5])
def test_unparseable_identifier_is_dropped_not_stringified(session, bad):
    """A repr in a unique column is worse than absence twice over: it is
    permanent, and two DIFFERENT papers carrying the same unparseable
    value match each other and collapse into one row."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "First",
                "source_engine": "pubmed",
                "doi": bad,
                "pmid": "111",
            },
            {
                "url": "https://b.test/p",
                "title": "Second",
                "source_engine": "pubmed",
                "doi": bad,
                "pmid": "222",
            },
        ],
    )

    assert session.query(Paper).count() == 2


@pytest.mark.parametrize("bad_year", [10**30, 1e30, "99999999999999999999"])
def test_unstorable_year_does_not_lose_the_batch(session, bad_year):
    """``int()`` accepts these and SQLite's 64-bit INTEGER does not, so the
    OverflowError lands at flush — which is not an IntegrityError, so the
    per-source SAVEPOINT retry does not apply and the Session is left
    rolled back. Every LATER source is lost and nothing commits.
    """
    saved = _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "Bad year",
                "source_engine": "pubmed",
                "year": bad_year,
            },
            {
                "url": "https://b.test/p",
                "title": "Good",
                "source_engine": "pubmed",
                "year": 2024,
            },
        ],
    )

    assert saved == 2


def test_absent_identifiers_are_not_materialized(session):
    """A plain web result has no academic identity; writing four nulls into
    its stored ``original_data`` changes what that column contains."""
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )

    normalized = normalize_source_fields(
        {"url": "https://example.test/page", "title": "Page"}
    )

    assert set(normalized) == {"url", "title"}


class _IntLike:
    """Stands in for a numpy scalar: ``int()``-able, not an ``int``."""

    def __init__(self, value):
        self._value = value

    def __int__(self):
        return self._value

    def __eq__(self, other):
        return self._value == other

    def __hash__(self):
        return hash(self._value)


@pytest.mark.parametrize(
    "bad_year",
    [_IntLike(2**64 - 1), Decimal("1E+400"), Fraction(10**30, 1)],
)
def test_int_like_year_does_not_lose_the_batch(session, bad_year):
    """The hazard is ``int()``-acceptability, not membership of
    ``(int, float, str)``. ``_parse_date`` calls ``int()``, which takes
    anything with ``__int__`` — numpy scalars and Decimal among them, and
    this service's own comments name numpy as an expected producer."""
    saved = _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "Bad year",
                "source_engine": "pubmed",
                "year": bad_year,
            },
            {
                "url": "https://b.test/p",
                "title": "Good",
                "source_engine": "pubmed",
                "year": 2024,
            },
        ],
    )

    assert saved == 2


@pytest.mark.parametrize("pmid", [_IntLike(12345), Decimal("12345"), 12345])
def test_integral_identifier_is_kept_not_dropped(session, pmid):
    """Integral-ness, not ``isinstance(int)``. numpy.int64 and Decimal are
    integers that are not ``int`` subclasses; dropping them loses a real
    identifier and breaks dedup — the same harm as stringifying, in the
    other direction."""
    from local_deep_research.database.models.citation import Paper

    _save(
        session,
        [
            {
                "url": "https://a.test/p",
                "title": "Paper",
                "source_engine": "pubmed",
                "pmid": pmid,
            },
            {
                "url": "https://b.test/p",
                "title": "Paper",
                "source_engine": "pubmed",
                "pmid": "12345",
            },
        ],
    )

    assert session.query(Paper).count() == 1


def test_null_author_name_creates_no_empty_author(session):
    """``_parse_name("")`` synthesises ``{"literal": ""}``, defeating
    ``_parse_authors_list``'s own "no authors -> None" contract."""
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )
    from local_deep_research.utilities.citation_normalizer import (
        normalize_citation,
    )

    normalized = normalize_source_fields(
        {
            "url": "https://a.test/p",
            "source_engine": "pubmed",
            "authors": [{"name": None}, {"given": "Jane", "family": "Doe"}],
        }
    )

    authors = (normalize_citation(normalized) or {}).get("authors")
    assert authors == [{"family": "Doe", "given": "Jane"}]


def test_unprintable_url_does_not_lose_sibling_sources(session):
    """The handler that reports a failed source read ``url`` unguarded, so
    an exception there escaped the handler and took the siblings with it."""

    class _Unprintable:
        def __str__(self):
            raise RuntimeError("boom")

    saved = _save(
        session,
        [
            {"url": _Unprintable(), "title": "Bad"},
            {"url": "https://b.test/p", "title": "Good"},
        ],
    )

    assert saved >= 1


def test_flush_failure_does_not_lose_the_rest_of_the_batch(session):
    """The per-source SAVEPOINT only isolated ``IntegrityError``.

    Any other flush-time exception deactivates the savepoint, so the
    ``is_active`` guard skipped the rollback in exactly the case that
    needed it. The Session then stayed in its failed state, every later
    source raised ``PendingRollbackError``, and the final ``commit()``
    raised out of the function — one bad source lost the whole batch and
    the caller got an exception instead of a reduced count.

    The failure is injected at the DB cursor, NOT by patching
    ``ResearchResource.__init__``: that raises before any DB work, leaves
    ``sp.is_active`` True, and so passes even against the pre-fix code —
    it would not exercise the branch this test exists for.
    """
    from sqlalchemy import event

    from local_deep_research.database.models.research import ResearchResource

    engine = session.get_bind()

    def explode_on_bad_insert(
        conn, cursor, statement, parameters, context, executemany
    ):
        if "research_resources" in statement.lower() and any(
            "Bad" == p for p in (parameters or ())
        ):
            raise RuntimeError("simulated non-integrity flush failure")

    event.listen(engine, "before_cursor_execute", explode_on_bad_insert)
    try:
        saved = _save(
            session,
            [
                {"url": "https://a.test/p", "title": "First"},
                {"url": "https://b.test/p", "title": "Bad"},
                {"url": "https://c.test/p", "title": "Third"},
                {"url": "https://d.test/p", "title": "Fourth"},
            ],
        )
    finally:
        event.remove(engine, "before_cursor_execute", explode_on_bad_insert)

    assert saved == 3
    assert session.query(ResearchResource).count() == 3


@pytest.mark.parametrize(
    "key", ["name", "display_name", "family", "given", "suffix"]
)
@pytest.mark.parametrize("empty", [None, "", "   "])
def test_empty_author_field_is_dropped(key, empty):
    """``_parse_authors_list``'s CSL branch copies family/given/suffix
    verbatim and checks only ``"family" in author``, so an empty one lands
    as a junk author in the stored csl_json — the same harm the name keys
    were guarded against. Whitespace counts as empty because
    ``_parse_name`` strips before its own emptiness test.
    """
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )

    # Through a real author list: the rule is scoped to those, since that
    # is the only subtree _parse_authors_list turns into author records.
    normalized = normalize_source_fields({"authors": [{key: empty}]})

    assert normalized["authors"] == [{}]


def test_author_name_is_coerced_only_once():
    """The filter and the value expression each called ``_as_text``, so a
    non-deterministic ``__str__`` could pass the filter on one call and
    write "" from the next."""
    from local_deep_research.web.services.research_sources_service import (
        _json_text_safe,
    )

    class _Flaky:
        def __init__(self):
            self.calls = 0

        def __str__(self):
            self.calls += 1
            if self.calls % 2 == 1:
                return "Real Author Name"
            raise RuntimeError("boom")

    from local_deep_research.web.services.research_sources_service import (
        _AUTHORS_RECORD,
    )

    assert _json_text_safe({"name": _Flaky()}, _authors=_AUTHORS_RECORD) == {
        "name": "Real Author Name"
    }


def test_numpy_boolean_is_not_an_identifier():
    """A numpy bool is neither a Python ``bool`` nor a
    ``numbers.Real``/``Integral``, so it slipped both gates and a True in a
    pmid field was stored as the identifier "1". Matched on dtype kind
    because numpy 1.x names the scalar ``bool_`` and 2.x names it ``bool``.
    """
    numpy = pytest.importorskip("numpy")

    from local_deep_research.web.services.research_sources_service import (
        _coerce_identifier,
    )

    assert _coerce_identifier(numpy.bool_(True)) is None
    assert _coerce_identifier(numpy.bool_(False)) is None
    # ...while real numpy integers are still accepted.
    assert _coerce_identifier(numpy.int64(12345)) == "12345"
    assert _coerce_identifier(numpy.float32(12345.0)) is None


def test_empty_author_keys_are_dropped_only_inside_author_lists():
    """The drop rule exists because ``_parse_authors_list`` turns these
    fields into author records. It must not fire on arbitrary metadata,
    where a key named ``given``/``family``/``suffix`` carries no such
    hazard and the producer may want it preserved.
    """
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )

    normalized = normalize_source_fields(
        {
            "metadata": {
                "contributor": {"given": "", "family": "Smith", "suffix": ""},
                # ...but a nested author list IS still guarded.
                "authors": [{"family": ""}, {"family": "Real"}],
            }
        }
    )

    assert normalized["metadata"]["contributor"] == {
        "given": "",
        "family": "Smith",
        "suffix": "",
    }
    assert normalized["metadata"]["authors"] == [{}, {"family": "Real"}]


def test_hostile_dtype_property_does_not_escape_the_coercion():
    """Every other step in ``_coerce_identifier`` is total; ``getattr``
    only swallows a MISSING attribute, so a ``.kind`` that raises would
    propagate out of a helper the rest of the file treats as safe."""
    from local_deep_research.web.services.research_sources_service import (
        _coerce_identifier,
    )

    class _ExplodingDtype:
        @property
        def kind(self):
            raise RuntimeError("boom")

    class _Hostile:
        @property
        def dtype(self):
            return _ExplodingDtype()

        def __int__(self):
            return 5

    assert _coerce_identifier(_Hostile()) is None


def test_empty_keys_nested_inside_an_author_record_are_preserved():
    """``_parse_authors_list`` reads only an author record's OWN
    ``name``/``display_name``/``family``/``given``/``suffix``. A key of the
    same name nested in a sub-object of that record — an ``affiliation``,
    or OpenAlex's ``institutions[].display_name`` — is inert data with no
    junk-author hazard, so it must survive.

    A boolean "inside authors" flag could not express this: once set it
    stayed set for every descendant.
    """
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )

    normalized = normalize_source_fields(
        {
            "authors": [
                {
                    "name": "Marie Curie",
                    "affiliation": {"institution": "Sorbonne", "given": ""},
                    "institutions": [{"id": "I1", "display_name": ""}],
                }
            ]
        }
    )

    record = normalized["authors"][0]
    assert record["affiliation"] == {"institution": "Sorbonne", "given": ""}
    assert record["institutions"] == [{"id": "I1", "display_name": ""}]
    # ...and the record's own name is untouched because it is non-empty.
    assert record["name"] == "Marie Curie"


def test_authors_nested_under_an_author_record_still_guarded():
    """The flag RE-ARMS on a further ``authors`` key rather than being
    inherited.

    Asserting the nested drop alone does not discriminate: the inherited
    boolean this replaced was strictly over-inclusive, so it dropped
    everything the fix drops and more, and a test that only checks "the
    nested empty field went away" passes under both. The sibling
    ``affiliation`` in the same record is what separates them — inherited
    state deletes its empty ``given``, re-armed state leaves it alone.
    """
    from local_deep_research.web.services.research_sources_service import (
        normalize_source_fields,
    )

    normalized = normalize_source_fields(
        {
            "authors": [
                {
                    "name": "A",
                    "authors": [{"family": ""}],
                    "affiliation": {"given": ""},
                }
            ]
        }
    )

    assert normalized["authors"] == [
        {
            "name": "A",
            # re-armed: this IS an author list, so the empty key goes
            "authors": [{}],
            # not re-armed: inert data, preserved
            "affiliation": {"given": ""},
        }
    ]