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.

10.4 KB
Raw Blame
"""LaTeX export of a report whose citations are already links.

``research_service`` citation-formats a report before storing it, and the
export route hands that stored markdown to ``LaTeXExporter``. So in every
hyperlinked citation mode the exporter receives ``[[label]](url)`` links
rather than the ``[N]`` brackets it turns into ``\\cite{N}``. These
contracts pin that each such link becomes the ``\\cite`` of the source it
points at, whatever its label.

They pin the generated ``.tex`` only and do not need a TeX installation.
"""

from __future__ import annotations

import random
import time

import pytest

from local_deep_research.text_optimization import citation_formatter
from local_deep_research.text_optimization.citation_formatter import (
    CitationFormatter,
    CitationMode,
    LaTeXExporter,
    _link_destination_ends,
)

SOURCES = (
    "[1] Alpha Paper\nURL: https://arxiv.org/abs/2401.00001\n\n"
    "[2] Beta Paper\nURL: https://arxiv.org/abs/2401.00002\n\n"
    "[3] Gamma Blog\nURL: https://gamma.example/post\n\n"
    "[4] Foo (bar)\nURL: https://en.wikipedia.org/wiki/Foo_(bar)\n"
)

LINKED_MODES = [
    mode for mode in CitationMode if mode is not CitationMode.NO_HYPERLINKS
]


def report(body: str, sources: str = SOURCES) -> str:
    """Assemble the report shape the formatter and the exporter expect."""
    return f"# Research Report\n\n{body}\n\n## Sources\n\n{sources}"


def exported_answer(markdown: str) -> str:
    """Return the first body line of the export, the answer paragraph."""
    latex = LaTeXExporter().export_to_latex(markdown)
    body = latex[latex.index("\\section{Research Report}") :]
    return body.splitlines()[2]


class TestSavedReportCitations:
    """A formatted citation link must come out as ``\\cite{N}``."""

    @pytest.mark.parametrize("mode", LINKED_MODES, ids=lambda mode: mode.value)
    def test_every_linked_mode_exports_its_citations_as_cite(self, mode):
        saved = CitationFormatter(mode).format_document(
            report("Claims [1] and [2, 3]. Wikipedia agrees [4].")
        )
        assert "]](https://" in saved  # the stored report carries links

        assert exported_answer(saved) == (
            "Claims \\cite{1} and \\cite{2}\\cite{3}. "
            "Wikipedia agrees \\cite{4}."
        )

    def test_a_number_label_missing_from_sources_stays_literal(self):
        markdown = "# Research Report\n\nSee [[7]](https://seven.example/a).\n"

        assert exported_answer(markdown) == (
            "See [[7]](https://seven.example/a)."
        )

    def test_a_number_label_declared_in_sources_is_cited(self):
        markdown = report(
            "See [[7]](https://seven.example/a).",
            "[7] Seven Paper\nURL: https://seven.example/a\n",
        )

        assert exported_answer(markdown) == "See \\cite{7}."

    def test_two_sources_sharing_a_url_cite_the_lower_index(self):
        sources = (
            "[5] Beta Paper again\nURL: https://beta.example/p\n\n"
            "[2] Beta Paper\nURL: https://beta.example/p\n"
        )
        markdown = report(
            "See [[beta.example]](https://beta.example/p).", sources
        )

        assert exported_answer(markdown) == "See \\cite{2}."

    def test_a_link_that_names_no_source_is_left_as_written(self):
        markdown = report(
            "See [[elsewhere.example]](https://elsewhere.example/p)."
        )

        assert exported_answer(markdown) == (
            "See [[elsewhere.example]](https://elsewhere.example/p)."
        )

    def test_an_empty_destination_names_no_source(self):
        markdown = report("See [[doc]]().", "[1] Real Local Document\n")

        assert exported_answer(markdown) == "See [[doc]]()."

    def test_a_link_resolves_only_through_the_sources_section(self):
        # The bibliography is built from the Sources section alone, so a
        # Sources-shaped line in the answer must not bind a link to key 1.
        markdown = (
            "# Research Report\n\n"
            "See [[evil.example]](https://evil.example/poison).\n\n"
            "[1] Attacker Controlled Entry\n"
            "URL: https://evil.example/poison\n\n"
            "## Sources\n\n"
            "[1] Real Local Document\n"
        )

        assert exported_answer(markdown) == (
            "See [[evil.example]](https://evil.example/poison)."
        )

    def test_a_url_holding_a_citation_shaped_fragment_is_read_once(self):
        sources = "[1] Search\nURL: https://search.example/?q=[[2]](x)\n"
        markdown = report(
            "See [[search.example]](https://search.example/?q=[[2]](x)).",
            sources,
        )

        assert exported_answer(markdown) == "See \\cite{1}."

    def test_brackets_before_parenthesised_prose_are_not_a_link(self):
        # A destination cannot contain a space, so this is text, not a link.
        markdown = report("Table [[3]](see below) lists them.")

        assert exported_answer(markdown) == (
            "Table [\\cite{3}](see below) lists them."
        )

    def test_a_prose_link_is_left_as_written(self):
        markdown = report("See [the paper](https://arxiv.org/abs/2401.00001).")

        assert exported_answer(markdown) == (
            "See [the paper](https://arxiv.org/abs/2401.00001)."
        )

    def test_the_bibliography_is_the_same_as_for_the_plain_report(self):
        plain = report("Claims [1] and [2, 3]. Wikipedia agrees [4].")
        saved = CitationFormatter(
            CitationMode.DOMAIN_ID_HYPERLINKS
        ).format_document(plain)

        def bibliography(markdown: str) -> str:
            latex = LaTeXExporter().export_to_latex(markdown)
            return latex[latex.index("\\begin{thebibliography}") :]

        assert bibliography(saved) == bibliography(plain)


class TestLinkDestinationScan:
    """Where a link destination ends, found in linear time."""

    @staticmethod
    def scan_from(text: str, start: int) -> int:
        """The per-start scan the one-pass table replaces, as a reference."""
        depth = 0
        for position in range(start, len(text)):
            character = text[position]
            if character.isspace():
                return -1
            if character == "(":
                depth += 1
            elif character == ")":
                if depth == 0:
                    return position
                depth -= 1
        return -1

    def test_the_table_agrees_with_a_scan_from_every_start(self):
        rng = random.Random(42)
        for _ in range(300):
            text = "".join(
                rng.choice("()ab ") for _ in range(rng.randrange(40))
            )
            ends = _link_destination_ends(text)
            assert [ends[i] for i in range(len(text))] == [
                self.scan_from(text, i) for i in range(len(text))
            ], text

    @pytest.mark.parametrize("where", ["answer", "sources"])
    def test_unclosed_link_starts_stay_fast(self, where):
        # A destination that never closes, 20 000 times, with no whitespace
        # to end the search: a scan per start took about 40 seconds.
        unclosed = "[[a]](b" * 20000
        if where == "answer":
            markdown = report(unclosed)
        else:
            markdown = report(
                "See [1].", f"[1] Title\nURL: https://x.example/{unclosed}\n"
            )

        started = time.perf_counter()
        LaTeXExporter().export_to_latex(markdown)
        elapsed = time.perf_counter() - started

        assert elapsed < 2.0, f"export took {elapsed:.2f}s"

    def test_a_report_without_citation_links_never_builds_the_table(
        self, monkeypatch
    ):
        # The table costs memory and time for every character of the report,
        # so only a report that holds a citation link may pay for it.
        def refuse(text):
            raise AssertionError("destination table built without a link")

        monkeypatch.setattr(
            citation_formatter, "_link_destination_ends", refuse
        )

        assert exported_answer(report("Claims [1] and [2, 3].")) == (
            "Claims \\cite{1} and \\cite{2,3}."
        )


class TestCommaGroups:
    """A group of indices in one bracket is one ``\\cite`` with a key list."""

    @pytest.mark.parametrize("group", ["[2, 3]", "[2,3]", "【2, 3】"])
    def test_a_comma_group_becomes_one_cite(self, group):
        markdown = report(f"Both agree {group}.")

        assert exported_answer(markdown) == "Both agree \\cite{2,3}."

    @pytest.mark.parametrize("group", ["[4, 5]", "[1, 9]", "[7, 8]"])
    def test_a_group_with_a_member_the_sources_lack_is_left_as_written(
        self, group
    ):
        # Prose such as "versions [4, 5]": citing it would print [?] for 5.
        markdown = report(f"Supported in versions {group}.")

        assert exported_answer(markdown) == f"Supported in versions {group}."

    def test_sources_without_urls_are_members_too(self):
        # Local documents have no URL; being listed is what makes a member.
        sources = "[1] Local Document A\n\n[2] Local Document B\n"
        markdown = report("Both agree [1, 2].", sources)

        assert exported_answer(markdown) == "Both agree \\cite{1,2}."

    def test_a_group_label_cites_every_member(self):
        # Its one URL names a single source, so looking that up drops the
        # rest of the group.
        markdown = report(
            "Both agree [[2, 3]](https://arxiv.org/abs/2401.00002)."
        )

        assert exported_answer(markdown) == "Both agree \\cite{2,3}."

    def test_a_padded_group_label_cites_every_member(self):
        # A hand-edited label; the formatter itself never pads one.
        markdown = report(
            "Both agree [[ 2, 3 ]](https://arxiv.org/abs/2401.00002)."
        )

        assert exported_answer(markdown) == "Both agree \\cite{2,3}."

    def test_a_group_label_with_space_before_the_comma_cites_every_member(
        self,
    ):
        # #6944: a hand-edited label with the space before the comma.
        # The pattern allowed whitespace after the comma only, so this
        # shape failed the group match, fell to single-citation/URL
        # lookup, and dropped the second member from the \cite.
        markdown = report(
            "Both agree [[2 ,3]](https://arxiv.org/abs/2401.00002)."
        )

        assert exported_answer(markdown) == "Both agree \\cite{2,3}."

    def test_a_group_label_with_a_member_the_sources_lack_is_left_as_written(
        self,
    ):
        markdown = report("See [[4, 5]](https://elsewhere.example/p).")

        assert exported_answer(markdown) == (
            "See [[4, 5]](https://elsewhere.example/p)."
        )