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.

16.2 KB
Raw Blame
"""Archive citation identity and actual Sources display regression controls."""

import pytest

from local_deep_research.utilities import search_utilities
from local_deep_research.utilities.search_utilities import (
    count_distinct_sources,
    extract_cited_indices_and_urls,
    filter_cited_links,
    format_links_to_markdown,
)
from local_deep_research.utilities.url_utils import canonical_url_key
from local_deep_research.web_search_engines.wayback_client import (
    validate_capture_fetch_url,
)


CAPTURE = "20240203112233"


def replay_url(target, *, timestamp=CAPTURE, modifier="", scheme="https"):
    return f"{scheme}://web.archive.org/web/{timestamp}{modifier}/{target}"


@pytest.mark.parametrize("scheme", ["http", "https"])
@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
@pytest.mark.parametrize(
    "target",
    [
        "https://example.com/history/",
        "http://example.com/history/?utm_source=first&fbclid=capture#section",
        "https://example.com/history?%75tm_source=first&&q=a+b&q=a%20b&",
        "https://example.com/page?next=https://other.example/path/&version=1",
    ],
)
def test_replay_canonical_key_preserves_complete_spelling(
    scheme, modifier, target
):
    replay = replay_url(target, scheme=scheme, modifier=modifier)

    assert canonical_url_key(replay) == replay


@pytest.mark.parametrize(
    "first,second",
    [
        (
            replay_url("https://example.com/history"),
            replay_url("https://example.com/history/"),
        ),
        (
            replay_url("https://example.com/history?utm_source=first"),
            replay_url("https://example.com/history?utm_source=second"),
        ),
        (
            replay_url("https://example.com/history?fbclid=first"),
            replay_url("https://example.com/history?fbclid=second"),
        ),
        (
            replay_url("https://example.com/history?x=1&y=2"),
            replay_url("https://example.com/history?y=2&x=1"),
        ),
        (
            replay_url("https://example.com/history"),
            replay_url(
                "https://example.com/history", timestamp="20250304123456"
            ),
        ),
        (
            replay_url("https://example.com/history"),
            replay_url("https://example.com/history", modifier="id_"),
        ),
    ],
)
def test_distinct_capture_targets_have_distinct_canonical_keys(first, second):
    assert canonical_url_key(first) != canonical_url_key(second)


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
@pytest.mark.parametrize("with_metadata", [False, True])
def test_sources_renderer_preserves_guard_valid_replay_targets(
    modifier, with_metadata
):
    targets = [
        "https://example.com/history/?utm_source=first&fbclid=one#section",
        "https://example.com/history?utm_source=second&fbclid=two#section",
    ]
    links = []
    for index, target in enumerate(targets, 1):
        link = {
            "title": "Historical policy",
            "link": replay_url(target, modifier=modifier),
            "index": str(index),
        }
        if with_metadata:
            link.update(
                source_type="archive",
                original_url=target,
                timestamp=CAPTURE,
            )
        links.append(link)

    rendered = format_links_to_markdown(links, uncited_mode="disabled")
    displayed = [
        line.removeprefix("   URL: ")
        for line in rendered.splitlines()
        if line.startswith("   URL: ")
    ]

    assert displayed == [link["link"] for link in links]
    assert count_distinct_sources(links) == 2
    for replay, target in zip(displayed, targets):
        validate_capture_fetch_url(replay, CAPTURE, target)


def test_repeated_exact_replay_still_groups_citation_indices():
    replay = replay_url("https://example.com/history/?utm_source=first")
    links = [
        {"link": replay, "title": "Historical policy", "index": "1"},
        {"link": replay, "title": "Historical policy", "index": "2"},
    ]

    rendered = format_links_to_markdown(links, uncited_mode="disabled")

    assert rendered.count("   URL: ") == 1
    assert f"   URL: {replay}\n" in rendered
    assert "[1, 2]" in rendered
    assert count_distinct_sources(links) == 1


@pytest.mark.parametrize(
    "url,expected",
    [
        (
            "HTTPS://EXAMPLE.COM:443/history/?utm_source=x&v=1#part",
            "https://example.com/history?v=1",
        ),
        (
            "http://user:[email protected]:80/history/?fbclid=x",
            "http://example.com/history",
        ),
        (
            "https://web.archive.org/about/?utm_source=x#part",
            "https://web.archive.org/about",
        ),
        (
            "https://web.archive.org/web/20240203/https://example.com/history/?utm_source=x",
            "https://web.archive.org/web/20240203/https://example.com/history",
        ),
        (
            "https://web.archive.org.evil.example/web/20240203112233/https://example.com/history/?utm_source=x",
            "https://web.archive.org.evil.example/web/20240203112233/https://example.com/history",
        ),
        (
            "https://user:[email protected]/web/20240203112233/https://example.com/history/?utm_source=x",
            "https://web.archive.org/web/20240203112233/https://example.com/history",
        ),
    ],
)
def test_live_and_non_capture_urls_keep_existing_canonical_behavior(
    url, expected
):
    assert canonical_url_key(url) == expected


def test_sources_renderer_keeps_existing_live_url_normalization():
    rendered = format_links_to_markdown(
        [
            {
                "link": "HTTPS://EXAMPLE.COM:443/history/?utm_source=x&v=1#part",
                "title": "Current policy",
                "index": "1",
            }
        ],
        uncited_mode="disabled",
    )

    assert "   URL: https://example.com/history?v=1\n" in rendered
    assert "utm_source" not in rendered


@pytest.mark.parametrize(
    "suffix",
    [
        "20240203112233im_/https://example.com/history/",
        "٢٠٢٤٠٢٠٣١١٢٢٣٣/https://example.com/history/",
        "20240203112233/https://example.com:65536/history/",
        "20240203112233/https://example.com/his\ntory/",
        "20240203112233/https://example.com/his\ttory/",
    ],
)
def test_invalid_replay_shapes_use_existing_normalization(suffix):
    replay = f"https://web.archive.org/web/{suffix}?utm_source=x"
    expected_path = suffix.replace("\n", "").replace("\t", "").rstrip("/")

    assert canonical_url_key(replay) == (
        f"https://web.archive.org/web/{expected_path}"
    )


@pytest.mark.parametrize(
    "replay",
    [
        "HTTP://WEB.ARCHIVE.ORG:80/web/20240203112233/https://example.com/history/?utm_source=x",
        "https://web.archive.org:443/web/20240203112233/https://example.com/history/?fbclid=x",
        replay_url("https://example.com/history/?"),
        replay_url("https://example.com/history/#"),
    ],
)
def test_replay_wrapper_and_empty_delimiters_stay_verbatim(replay):
    assert canonical_url_key(replay) == replay


def test_sources_renderer_preserves_legacy_http_replay_spelling():
    replay = replay_url(
        "https://example.com/history/?utm_source=first#section", scheme="http"
    )

    rendered = format_links_to_markdown(
        [{"link": replay, "title": "Historical policy", "index": "1"}],
        uncited_mode="disabled",
    )

    assert f"   URL: {replay}\n" in rendered


@pytest.mark.parametrize(
    "target",
    [
        "http://[::ffff:8.8.8.8]:80/history/?utm_source=first#part",
        "https://[2001:4860:4860::8888]:8443/owner's/history/"
        "?next=https://other.example/a/&fbclid=one#part",
        "https://example.com/history/"
        "?next=http://[::ffff:8.8.8.8]:8080/a/&q=owner's#part",
        "https://example.com/history/[Part]/"
        "?next=https://[2001:4860:4860::8888]:443/a/&q[range]=[alpha]#part",
        "https://[2606:4700:4700::1111]",
        "https://example.org/a[b[c]]?values=[x[y]]",
        "https://[2001:4860:4860::8888]:8443/a[b[c]]"
        "?values=[x[y]]&next=https://[2606:4700:4700::1111]/a/#part",
    ],
)
@pytest.mark.parametrize(
    "template",
    [
        "Read {url}. See [4].",
        "Read [History]({url}). See [4].",
        "Read [History](<{url}>). See [4].",
        "Read <{url}>. See [4].",
        "Read [{url}]. See [4].",
        "Read {url}[4].",
    ],
)
def test_replay_url_citations_preserve_brackets_and_select_one_capture(
    target, template
):
    replay = replay_url(target)
    cited = {"index": "7", "title": "Archived", "url": replay}
    other_capture = {
        "index": "8",
        "title": "Other capture",
        "url": replay_url(target, timestamp="20250304123456"),
    }
    truncated = {
        "index": "9",
        "title": "Truncated target",
        "url": f"https://web.archive.org/web/{CAPTURE}/https:",
    }
    other = {"index": "4", "title": "Other", "url": "https://other.example/a"}
    prose = template.format(url=replay)
    links = [cited, other_capture, truncated, other]

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {4}
    assert replay in urls
    assert not indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == [cited, other]
    rendered = format_links_to_markdown(
        links, prose=prose, uncited_mode="strict"
    )
    assert f"   URL: {replay}\n" in rendered
    assert "Other capture" not in rendered
    assert "Truncated target" not in rendered
    assert rendered.count("   URL: ") == 2
    validate_capture_fetch_url(replay, CAPTURE, target)


@pytest.mark.parametrize(
    "template",
    [
        "Reference [1, {url},bad]. See [4].",
        "Reference [1,\n{url} bad]. See [4].",
        "Read {url} and [1,abc,3]. See [4].",
    ],
)
def test_replay_ip_masking_preserves_malformed_body_reference_fallback(
    template,
):
    replay = replay_url("https://[2001:4860:4860::8888]/history/")
    links = [
        {"index": "7", "url": replay},
        {"index": "8", "url": replay_url("https://other.example/a")},
        {"index": "4", "url": "https://other.example/four"},
    ]
    prose = template.format(url=replay)

    assert extract_cited_indices_and_urls(prose)[2]
    assert filter_cited_links(links, prose, mode="strict") == links


def test_bare_replay_preserves_consecutive_numeric_reference_boundaries():
    target = "http://[::ffff:8.8.8.8]:80/history/[Part]/?values=[x[y]]"
    replay = replay_url(target)
    archived = {"index": "7", "url": replay, "title": "Archived"}
    other_capture = {
        "index": "8",
        "url": replay_url(target, timestamp="20250304123456"),
        "title": "Other capture",
    }
    fourth = {"index": "4", "url": "https://other.example/four"}
    third = {"index": "3", "url": "https://other.example/three"}
    links = [archived, other_capture, fourth, third]
    prose = f"Read {replay}[4][3]."

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {3, 4}
    assert replay in urls
    assert not indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == [
            archived,
            fourth,
            third,
        ]
    rendered = format_links_to_markdown(
        links, prose=prose, uncited_mode="strict"
    )
    assert f"   URL: {replay}\n" in rendered
    assert rendered.count("   URL: ") == 3
    assert "Other capture" not in rendered
    validate_capture_fetch_url(replay, CAPTURE, target)


@pytest.mark.parametrize(
    "template", ["[old]({first})[new]({second})", "({first})({second})"]
)
def test_adjacent_wrapped_replays_keep_both_captures_and_balanced_parentheses(
    template,
):
    target = "https://[2001:4860:4860::8888]:8443/a_(history)[b[c]]?values=[x[y]]&doc=(a)"
    first = replay_url(target)
    second = replay_url(target, timestamp="20250304123456")
    links = [
        {"index": "7", "url": first, "title": "Old"},
        {"index": "8", "url": second, "title": "New"},
        {"index": "9", "url": replay_url(target, timestamp="20260101000000")},
    ]
    prose = template.format(first=first, second=second)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert not indices
    assert {first, second} <= urls
    assert not indeterminate
    assert filter_cited_links(links, prose, mode="strict") == links[:2]
    rendered = format_links_to_markdown(
        links, prose=prose, uncited_mode="strict"
    )
    assert f"   URL: {first}\n" in rendered
    assert f"   URL: {second}\n" in rendered
    assert rendered.count("   URL: ") == 2
    validate_capture_fetch_url(first, CAPTURE, target)
    validate_capture_fetch_url(second, "20250304123456", target)


@pytest.mark.parametrize("remaining", [-1, 0, 1])
def test_replay_candidate_work_budget_boundary_includes_both_passes(
    monkeypatch, remaining
):
    replay = replay_url("https://example.com/history")
    # One exact candidate is inspected by IP masking and URL extraction.
    monkeypatch.setattr(
        search_utilities,
        "_MAX_URL_CANDIDATE_CHARS",
        2 * len(replay) + remaining,
    )
    _, urls, indeterminate = extract_cited_indices_and_urls(replay)

    assert indeterminate is (remaining < 0)
    if not indeterminate:
        assert urls == {replay}


def test_replay_candidate_work_budget_is_shared_across_urls_and_passes(
    monkeypatch,
):
    monkeypatch.setattr(search_utilities, "_MAX_URL_CANDIDATE_CHARS", 50_000)
    first = replay_url("https://example.com/first") + "[1]" * 80
    second = replay_url("https://example.com/other") + "[2]" * 80
    assert not extract_cited_indices_and_urls(first)[2]
    assert not extract_cited_indices_and_urls(second)[2]

    prose = f"{first} {second}"
    _, partial_urls, indeterminate = extract_cited_indices_and_urls(prose)
    assert partial_urls  # The first URL was already parsed successfully.
    assert indeterminate
    links = [
        {"index": "7", "url": first},
        {"index": "8", "url": second},
        {"index": "9", "url": "https://uncited.example/kept"},
    ]
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) is links


def test_bare_replay_candidates_keep_intermediate_literal_bracket_suffixes():
    base = replay_url("https://example.com/history")
    middle = base + "[1]"
    full = base + "[1][2][3]"
    links = [
        {"index": "10", "url": base},
        {"index": "20", "url": middle},
        {"index": "30", "url": full},
        {"index": "40", "url": "https://uncited.example/omitted"},
    ]

    indices, urls, indeterminate = extract_cited_indices_and_urls(full)

    assert indices == {1, 2, 3}
    assert not indeterminate
    assert {base, middle, base + "[1][2]", full} <= urls
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, full, mode=mode) == links[:3]


@pytest.mark.parametrize("template", ["<{url}>", "[capture]({url})"])
def test_exact_replay_destination_does_not_gain_prefix_interpretations(
    template,
):
    base = replay_url("https://example.com/history")
    full = base + "[1][2][3]"
    links = [
        {"index": "10", "url": base},
        {"index": "20", "url": base + "[1]"},
        {"index": "30", "url": full},
    ]
    prose = template.format(url=full)

    _, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert not indeterminate
    assert urls == {full}
    assert filter_cited_links(links, prose, mode="strict") == links[2:]


@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_long_replay_reference_run_keeps_complete_bibliography(mode):
    base = replay_url("https://example.com/history")
    middle = base + "[1]" * 512
    full = base + "[1]" * 1024
    links = [
        {"index": "10", "title": "Base", "url": base},
        {"index": "20", "title": "Middle", "url": middle},
        {"index": "30", "title": "Full", "url": full},
        {
            "index": "40",
            "title": "Uncited",
            "url": "https://uncited.example/kept",
        },
    ]

    assert extract_cited_indices_and_urls(full)[2]
    assert filter_cited_links(links, full, mode=mode) is links
    rendered = format_links_to_markdown(links, prose=full, uncited_mode=mode)
    for link in links:
        assert f"   URL: {link['url']}\n" in rendered
    assert rendered.count("   URL: ") == len(links)