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.

1.8 KB
Raw Blame
"""Bibliography export of MERGED Sources lines.

``format_links_to_markdown`` collapses every citation of one source onto a
single line carrying all of its indices. Library documents cited across
several chunks hit this routinely (#5685), so an exporter that matches only
single-index lines drops those sources from the bibliography entirely.
"""

from __future__ import annotations

_CONTENT = """# Report

### SOURCES USED IN THIS SECTION:
[1, 3] Doc A (source nr: 1, 3)
   URL: /library/document/a/chunks#chunk-0

[2] Doc B
   URL: https://e.com/b

## ALL SOURCES:
[1, 3] Doc A (source nr: 1, 3)
   URL: /library/document/a/chunks#chunk-0

[2] Doc B
   URL: https://e.com/b
"""


def test_bibtex_keeps_merged_sources_without_duplicate_keys():
    from local_deep_research.text_optimization.citation_formatter import (
        QuartoExporter,
    )

    bib = QuartoExporter()._generate_bibliography(_CONTENT)
    keys = [ln for ln in bib.splitlines() if ln.startswith("@misc")]

    # The merged source survives...
    assert "Doc A" in bib
    assert keys == ["@misc{ref1,", "@misc{ref2,", "@misc{ref3,"]
    # ...and the Sources block appearing twice (per-section plus the
    # combined block) must not yield duplicate keys, which bibtex rejects.
    assert len(keys) == len(set(keys))


def test_latex_bibitems_are_ordered_by_index():
    from local_deep_research.text_optimization.citation_formatter import (
        LaTeXExporter,
    )

    tex = LaTeXExporter()._create_bibliography(_CONTENT)
    items = [
        ln.split(" ")[0]
        for ln in tex.splitlines()
        if ln.startswith("\\bibitem")
    ]

    assert "Doc A" in tex
    # The bibliography reads in index order, and a merged [1, 3] group
    # must not disturb that.
    assert items == ["\\bibitem[1]{1}", "\\bibitem[2]{2}", "\\bibitem[3]{3}"]