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.

4.2 KB
Raw Blame
"""Measure actual normalization and require complete independent CSL results."""

from copy import deepcopy

import pytest

from local_deep_research.utilities import citation_normalizer as normalizer

from .corpus import corpus, verify_batch


def measure(
    citation_workload, monkeypatch, name, size, sources, expected, authors
):
    calls = {"authors": 0, "names": 0}
    parse_authors, parse_name = (
        normalizer._parse_authors_list,
        normalizer._parse_name,
    )

    def counted_authors(*args):
        calls["authors"] += 1
        return parse_authors(*args)

    def counted_name(*args):
        calls["names"] += 1
        return parse_name(*args)

    with monkeypatch.context() as patch:
        patch.setattr(normalizer, "_parse_authors_list", counted_authors)
        patch.setattr(normalizer, "_parse_name", counted_name)

        def prepare():
            calls.update(authors=0, names=0)
            return deepcopy(sources)

        def verify(state, output):
            verify_batch(state, output, sources, expected)
            academic = sum(item is not None for item in expected)
            assert calls["authors"] <= academic
            assert calls["names"] <= academic * authors
            return dict(
                sources=len(sources),
                academic=academic,
                authors_per_source=authors,
                author_parses=calls["authors"],
                name_parses=calls["names"],
            )

        citation_workload.measure(
            name,
            size,
            prepare,
            lambda state: [
                normalizer.normalize_citation(source) for source in state
            ],
            verify,
            units=len(sources) * max(authors, 1),
            unit="source_author",
            parameters=dict(
                sources=len(sources),
                authors_per_source=authors,
                instrumentation=True,
                input_copy_timed=False,
            ),
        )


@pytest.mark.parametrize(
    "kind", ["web", "openalex_strings", "openalex_csl", "arxiv", "pubmed"]
)
def test_source_growth_preserves_complete_metadata(
    citation_workload, monkeypatch, kind
):
    for size in (
        citation_workload.settings["size"],
        citation_workload.settings["size"] * 2,
    ):
        sources, expected = corpus(size, kind)
        measure(
            citation_workload,
            monkeypatch,
            "sources_" + kind,
            size,
            sources,
            expected,
            4 if kind != "web" else 0,
        )


@pytest.mark.parametrize("kind", ["openalex_strings", "openalex_csl"])
def test_author_growth_at_fixed_source_count(
    citation_workload, monkeypatch, kind
):
    for size in (
        citation_workload.settings["size"],
        citation_workload.settings["size"] * 2,
    ):
        sources, expected = corpus(4, kind, size)
        measure(
            citation_workload,
            monkeypatch,
            "authors_" + kind,
            size,
            sources,
            expected,
            size,
        )


@pytest.mark.parametrize("fault", ["authors", "date", "doi"])
def test_full_output_oracle_rejects_lost_citation_fields(fault):
    sources, expected = corpus(2, "openalex_csl")
    inputs = deepcopy(sources)
    output = [normalizer.normalize_citation(source) for source in inputs]
    verify_batch(inputs, output, sources, expected)
    if fault == "authors":
        output[0]["authors"] = []
    elif fault == "date":
        output[0]["publication_date"] = "1900-01-01"
    else:
        output[0]["doi"] = "10.1234/incorrect"
    with pytest.raises(AssertionError, match="Citation output mismatch"):
        verify_batch(inputs, output, sources, expected)


def test_repeated_normalization_observes_changed_input_without_sharing_names():
    sources, _ = corpus(1, "openalex_csl")
    source = sources[0]
    first = normalizer.normalize_citation(source)
    source["title"] = "Updated title"
    source["metadata"]["authors_csl"][0]["given"] = "Changed"
    second = normalizer.normalize_citation(source)
    assert first["csl_json"]["title"] == "Synthetic paper 0"
    assert first["authors"][0]["given"] == "Given0"
    assert second["csl_json"]["title"] == "Updated title"
    assert second["authors"][0]["given"] == "Changed"