~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Pin destination-processing work without machine-dependent timing gates."""
import pytest
from local_deep_research.text_optimization import citation_formatter as module
@pytest.mark.parametrize(
"mode",
[
mode
for mode in module.CitationMode
if mode != module.CitationMode.NO_HYPERLINKS
],
)
def test_source_adaptation_does_not_repeat_destination_processing(
monkeypatch, mode
):
original, checked = module._safe_link_destination, []
def observe(value):
checked.append(value)
return original(value)
monkeypatch.setattr(module, "_safe_link_destination", observe)
sources = [
{"index": 1, "title": "First", "url": "https://example.com/one"},
{"index": 2, "title": "Second", "link": "https://example.org/two"},
{"index": 3, "title": "No destination"},
{"index": None, "url": "https://example.com/unindexed"},
]
formatter = module.CitationFormatter(mode)
render_name = {
module.CitationMode.NUMBER_HYPERLINKS: "_format_number_hyperlinks",
module.CitationMode.DOMAIN_HYPERLINKS: "_format_domain_hyperlinks",
module.CitationMode.DOMAIN_ID_HYPERLINKS: "_format_domain_id_hyperlinks",
module.CitationMode.DOMAIN_ID_ALWAYS_HYPERLINKS: "_format_domain_id_always_hyperlinks",
module.CitationMode.SOURCE_TAGGED_HYPERLINKS: "_format_source_tagged_hyperlinks",
}[mode]
render, adaptation_checks = getattr(formatter, render_name), []
def after_adaptation(*args, **kwargs):
adaptation_checks.append(list(checked))
return render(*args, **kwargs)
monkeypatch.setattr(formatter, render_name, after_adaptation)
output = formatter.apply_inline_hyperlinks(
"[1, 2] Source 1 [3] [9]", sources
)
assert len(adaptation_checks) == 1
adapted = adaptation_checks[0]
assert len(adapted) <= len(sources)
assert adapted.count("https://example.com/one") <= 1
assert adapted.count("https://example.org/two") <= 1
assert output.count("https://example.com/one") == 2
assert output.count("https://example.org/two") == 1
assert "[3]" in output and "[9]" in output
def test_duplicate_indices_keep_last_source():
sources = [
{"index": 1, "url": "https://example.com/old"},
{"index": 1, "link": "https://example.com/new"},
]
assert (
module.CitationFormatter().apply_inline_hyperlinks("[1]", sources)
== "[[1]](https://example.com/new)"
)
def test_destination_results_are_fresh_for_each_call():
formatter = module.CitationFormatter()
sources = [{"index": 1, "url": "https://example.com/first"}]
assert (
formatter.apply_inline_hyperlinks("[1]", sources)
== "[[1]](https://example.com/first)"
)
sources[0]["url"] = "https://example.com/second"
assert (
formatter.apply_inline_hyperlinks("[1]", sources)
== "[[1]](https://example.com/second)"
)
@pytest.mark.parametrize("case", ["no_links", "empty_body", "empty_sources"])
def test_existing_early_returns_do_no_destination_work(monkeypatch, case):
def unexpected(_value):
raise AssertionError("Early return processed a destination")
monkeypatch.setattr(module, "_safe_link_destination", unexpected)
formatter = module.CitationFormatter(
module.CitationMode.NO_HYPERLINKS
if case == "no_links"
else module.CitationMode.NUMBER_HYPERLINKS
)
body = "" if case == "empty_body" else "[1]"
sources = (
[]
if case == "empty_sources"
else [{"index": 1, "url": "https://example.com/one"}]
)
assert formatter.apply_inline_hyperlinks(body, sources) == body