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.

3.8 KB
Raw Blame
"""Explicit Source separators reuse the existing citation parser (Refs #7527)."""

import pytest

from local_deep_research.utilities.search_utilities import (
    _MAX_RANGE_ENDPOINT_DIGITS,
    _MAX_RANGE_SPAN,
    _MAX_TOTAL_EXPANSION,
    extract_cited_indices_and_urls,
    filter_cited_links,
)


@pytest.fixture
def links():
    return [
        {"index": str(i), "url": f"https://source{i}.example/Article"}
        for i in range(1, 6)
    ]


@pytest.mark.parametrize("word", ["Source", "Sources", "source", "SOURCES"])
@pytest.mark.parametrize(
    "separator", [":", "#", ": ", "# ", " :", " #", " : ", " # "]
)
@pytest.mark.parametrize(
    "numbers", ["2, 3", "2 and 3", "2 & 3", "2-3", "2–3", "2—3"]
)
def test_punctuation_extraction(word, separator, numbers):
    assert extract_cited_indices_and_urls(f"{word}{separator}{numbers}.") == (
        {2, 3},
        set(),
        False,
    )


@pytest.mark.parametrize("phrase", ["Sources: 2, 3", "Source #2 and 3"])
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_punctuation_mixed_filter(links, phrase, mode):
    prose = f"Claim [1]. Further evidence {phrase}."
    kept = filter_cited_links(links, prose, mode=mode)
    assert kept == links[:3]
    assert all(actual is original for actual, original in zip(kept, links))


@pytest.mark.parametrize("phrase", ["Sources: 2, 3", "Source #2"])
def test_disabled_preserves_original_object(links, phrase):
    assert filter_cited_links(links, phrase, mode="disabled") is links


@pytest.mark.parametrize(
    "prose", ["Source 2", "Sources 2 and 3", "[2, 3]", "[2–3]"]
)
def test_control_existing_signals(links, prose):
    expected = {2} if prose == "Source 2" else {2, 3}
    assert extract_cited_indices_and_urls(prose) == (expected, set(), False)
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == [
            link for link in links if int(link["index"]) in expected
        ]


@pytest.mark.parametrize(
    "phrase",
    [
        "Source2",
        "Sources2",
        "resource2",
        "resource:2",
        "Source/2",
        "Source=2",
        "Source;2",
        "Source-2",
        "Source::2",
        "Source##2",
        "Source :#2",
    ],
)
def test_control_requires_explicit_delimiter(links, phrase):
    prose = f"[1] alongside {phrase}."
    assert extract_cited_indices_and_urls(prose) == ({1}, set(), False)
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == links[:1]


@pytest.mark.parametrize("fence", ["```", "~~~"])
def test_control_fences_and_url_identity(links, fence):
    prose = (
        f"{fence}\nSources: 2, 3; Source #4\n{fence}\n"
        "[1] https://SOURCE2.example/Article"
    )
    assert extract_cited_indices_and_urls(prose) == (
        {1},
        {links[1]["url"]},
        False,
    )
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == links[:2]


@pytest.mark.parametrize("separator", [" ", ": ", " #"])
@pytest.mark.parametrize("budget", ["range", "digits", "aggregate"])
def test_shared_limits_fail_safe(links, separator, budget):
    if budget == "range":
        numbers = f"2-{2 + _MAX_RANGE_SPAN + 1}"
    elif budget == "digits":
        numbers = "9" * (_MAX_RANGE_ENDPOINT_DIGITS + 1)
    else:
        # Repeated legal ranges still consume the shared expansion budget.
        numbers = ", ".join(
            [f"2-{2 + _MAX_RANGE_SPAN}"]
            * (_MAX_TOTAL_EXPANSION // (_MAX_RANGE_SPAN + 1) + 1)
        )
    prose = f"[1] Sources{separator}{numbers}"
    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)
    assert 1 in indices
    assert urls == set()
    assert indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) is links