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.

38.8 KB
Raw Blame
"""Wayback uses supplied archive targets without another search provider."""

import gc
import time
from unittest.mock import Mock

import pytest
import requests

from local_deep_research.web_search_engines.engines.search_engine_wayback import (
    WaybackSearchEngine,
)


@pytest.fixture
def discovery_guards(monkeypatch):
    import langchain_community.utilities as utilities

    discovery = Mock()
    discovery.return_value.results.return_value = [
        {"link": "https://unexpected.example"}
    ]
    monkeypatch.setattr(utilities, "DuckDuckGoSearchAPIWrapper", discovery)
    request = Mock(side_effect=AssertionError("Unexpected outbound request"))
    monkeypatch.setattr(requests.sessions.Session, "request", request)
    return discovery, request


@pytest.mark.parametrize(
    ("query", "expected"),
    [
        ("https://example.com", ["https://example.com"]),
        ("example.com", ["http://example.com"]),
        ("example.com/page", ["http://example.com/page"]),
        (
            "https://example.com:8443/page?x=1#section",
            ["https://example.com:8443/page?x=1#section"],
        ),
        (
            "example.com:8080/page?x=1#section",
            ["http://example.com:8080/page?x=1#section"],
        ),
        pytest.param(
            "example.com:8080/page?next=https://other.example/a",
            ["http://example.com:8080/page?next=https://other.example/a"],
            id="nested_bare_target",
        ),
        pytest.param(
            "[source](https://example.com:8443/page?x=1)",
            ["https://example.com:8443/page?x=1"],
            id="markdown_link_target",
        ),
        pytest.param(
            "[source](https://example.com:8443/page?x=1).",
            ["https://example.com:8443/page?x=1"],
            id="markdown_sentence_target",
        ),
        (
            "(https://example.com/wiki/Foo_(bar)).",
            ["https://example.com/wiki/Foo_(bar)"],
        ),
        (
            "(https://example.com/wiki/Foo_(bar))",
            ["https://example.com/wiki/Foo_(bar)"],
        ),
        (
            "https://example.com/wiki/Foo_(bar)",
            ["https://example.com/wiki/Foo_(bar)"],
        ),
        ("https://example.com/page)", ["https://example.com/page)"]),
        (
            "https://example.com/page?next=https://other.example/a",
            ["https://example.com/page?next=https://other.example/a"],
        ),
        (
            "https://web.archive.org/web/20240101000000/https://example.com/page",
            [
                "https://web.archive.org/web/20240101000000/https://example.com/page"
            ],
        ),
        ("This sentence has a period.", []),
        ("topic.with.dots and words", []),
        ("topic/with.a.path and words", []),
        ("historical information about a topic", []),
        ("", []),
        # The documented contract: a scheme in any case, and a bare domain
        # only as the whole query. A combining mark is not a bare-domain
        # character, but a full URL keeps it.
        ("sub.example.co.uk", ["http://sub.example.co.uk"]),
        ("Find example.com", []),
        ("https://", []),
        ("HTTPS://EXAMPLE.COM/page", ["HTTPS://EXAMPLE.COM/page"]),
        ("bu\u0308cher.de", []),
        ("https://bu\u0308cher.de/x", ["https://bu\u0308cher.de/x"]),
        # Running text: a closing bracket (ASCII, fullwidth or CJK) without
        # its opener, straight and curly closing quotes, emphasis, and CJK
        # or fullwidth stops end a URL; a balanced bracket pair stays in
        # it. Inside a parenthesis or square bracket, a list separator
        # before the closer or the next space ends it too.
        # Fullwidth commas and semicolons and the ideographic comma
        # separate compact lists like ASCII ones.
        pytest.param(
            "[source: https://example.com] for details",
            ["https://example.com"],
            id="prose_unmatched_square_closer",
        ),
        pytest.param(
            "Citations: [1: https://example.com], [2: https://example.org]",
            ["https://example.com", "https://example.org"],
            id="prose_bracketed_citations",
        ),
        pytest.param(
            "Archived versions of [https://a.example, https://b.example]",
            ["https://a.example", "https://b.example"],
            id="bracketed_root_list",
        ),
        pytest.param(
            "[https://a.example/x, https://b.example/y]",
            ["https://a.example/x", "https://b.example/y"],
            id="bracketed_path_list",
        ),
        pytest.param(
            "(https://a.example/x; https://b.example/y)",
            ["https://a.example/x", "https://b.example/y"],
            id="parenthesized_semicolon_path_list",
        ),
        pytest.param(
            "Pages (https://a.example/about, https://b.example/about) in 2010",
            ["https://a.example/about", "https://b.example/about"],
            id="prose_parenthesized_path_list",
        ),
        pytest.param(
            "The bot said 'see https://example.com' twice",
            ["https://example.com"],
            id="prose_closing_quote_without_opener",
        ),
        pytest.param(
            "He said \u201chttps://a.example/x\u201d twice",
            ["https://a.example/x"],
            id="curly_double_quotes",
        ),
        pytest.param(
            "Find it at \u2018https://a.example/x\u2019 now",
            ["https://a.example/x"],
            id="curly_single_quotes",
        ),
        pytest.param(
            "Read **https://a.example/x** first",
            ["https://a.example/x"],
            id="markdown_emphasis",
        ),
        pytest.param(
            "See {https://a.example/x} now",
            ["https://a.example/x"],
            id="braces",
        ),
        pytest.param(
            "\u53c2\u89c1https://example.com\u3002",
            ["https://example.com"],
            id="cjk_full_stop",
        ),
        pytest.param(
            "\uff08https://a.example/x\uff09",
            ["https://a.example/x"],
            id="fullwidth_parentheses",
        ),
        pytest.param(
            "see https://ja.wikipedia.org/wiki/"
            "\u3010\u63a8\u3057\u306e\u5b50\u3011 for details",
            [
                "https://ja.wikipedia.org/wiki/"
                "\u3010\u63a8\u3057\u306e\u5b50\u3011"
            ],
            id="balanced_lenticular_brackets",
        ),
        pytest.param(
            "see https://zh.wikipedia.org/wiki/"
            "\u82f9\u679c\uff08\u6c34\u679c\uff09 ok",
            [
                "https://zh.wikipedia.org/wiki/\u82f9\u679c\uff08\u6c34\u679c\uff09"
            ],
            id="balanced_fullwidth_parentheses",
        ),
        pytest.param(
            "see https://ja.wikipedia.org/wiki/\u300e\u30c6\u30b9\u30c8\u300f now",
            ["https://ja.wikipedia.org/wiki/\u300e\u30c6\u30b9\u30c8\u300f"],
            id="balanced_white_corner_brackets",
        ),
        pytest.param(
            "\uff08\u53c2\u89c1 https://zh.wikipedia.org/wiki/"
            "\u82f9\u679c\uff08\u6c34\u679c\uff09\uff09\u3002",
            [
                "https://zh.wikipedia.org/wiki/\u82f9\u679c\uff08\u6c34\u679c\uff09"
            ],
            id="balanced_pair_inside_fullwidth_parenthetical",
        ),
        pytest.param(
            "see https://a.example/x\u3011 now",
            ["https://a.example/x"],
            id="unmatched_cjk_closer",
        ),
        pytest.param(
            "\u300chttps://a.example/x\u300d",
            ["https://a.example/x"],
            id="cjk_corner_quotes",
        ),
        pytest.param(
            "see https://a.example/x\u2026 and",
            ["https://a.example/x"],
            id="ellipsis",
        ),
        pytest.param(
            "https://example.com\uff0chttps://example.org",
            ["https://example.com", "https://example.org"],
            id="fullwidth_comma_roots",
        ),
        pytest.param(
            "https://a.example\u3001https://b.example",
            ["https://a.example", "https://b.example"],
            id="ideographic_comma_roots",
        ),
        pytest.param(
            "https://a.example/x\uff1bhttps://b.example/y",
            ["https://a.example/x", "https://b.example/y"],
            id="fullwidth_semicolon_paths",
        ),
        # Documented limits (docs/search-engines.md): what follows a URL
        # without a space stays in it, because the same characters can
        # belong to an IRI path.
        pytest.param(
            "What did https://example.com's homepage say in 2005?",
            ["https://example.com's"],
            id="limit_possessive",
        ),
        pytest.param(
            "\u8bf7\u67e5\u770bhttps://example.com/about\u7684\u5386\u53f2\u7248\u672c",
            ["https://example.com/about\u7684\u5386\u53f2\u7248\u672c"],
            id="limit_cjk_text_after_path",
        ),
        pytest.param(
            "https://example.com(see note)",
            ["https://example.com(see"],
            id="limit_parenthesis_after_url",
        ),
        pytest.param(
            "see https://example.com/a(x)https://example.org/b now",
            ["https://example.com/a(x)https://example.org/b"],
            id="limit_urls_written_together",
        ),
        # A URL that itself ends in a character that also ends prose loses
        # it in running text; alone, in straight quotes, in angle brackets
        # or in backticks it keeps it. Of the quote styles, only straight
        # quotes delimit a URL.
        pytest.param(
            "see https://ja.wikipedia.org/wiki/\u541b\u306e\u540d\u306f\u3002 now",
            ["https://ja.wikipedia.org/wiki/\u541b\u306e\u540d\u306f"],
            id="limit_url_ending_in_cjk_full_stop",
        ),
        pytest.param(
            "see https://web.archive.org/web/*/example.com/* now",
            ["https://web.archive.org/web/*/example.com/"],
            id="limit_url_ending_in_asterisk",
        ),
        pytest.param(
            "see <https://ja.wikipedia.org/wiki/\u541b\u306e\u540d\u306f\u3002> now",
            ["https://ja.wikipedia.org/wiki/\u541b\u306e\u540d\u306f\u3002"],
            id="angle_brackets_keep_url_final_stop",
        ),
        pytest.param(
            "see `https://web.archive.org/web/*/example.com/*` now",
            ["https://web.archive.org/web/*/example.com/*"],
            id="backticks_keep_url_final_asterisk",
        ),
        pytest.param(
            'see "https://example.com/search?a=*" now',
            ["https://example.com/search?a=*"],
            id="straight_quotes_keep_url_final_asterisk",
        ),
        pytest.param(
            "Er schrieb \u201ehttps://example.com/a\u201c dazu",
            ["https://example.com/a\u201c"],
            id="limit_german_closing_quote",
        ),
        pytest.param(
            "[read https://example.com/label-url][1]",
            ["https://example.com/label-url][1"],
            id="limit_reference_style_link",
        ),
    ],
)
def test_extract_archive_targets_without_discovery(
    query, expected, discovery_guards
):
    discovery, request = discovery_guards
    engine = WaybackSearchEngine()

    assert engine._extract_urls_from_query(query) == expected
    discovery.assert_not_called()
    request.assert_not_called()


def _fastest(extract, query, fastest):
    started = time.perf_counter()
    extract(query)
    return min(fastest, time.perf_counter() - started)


@pytest.mark.parametrize(
    ("entry", "joiner", "target"),
    [
        pytest.param(
            "https://h{i}.example/p{i}",
            ",",
            "https://h{i}.example/p{i}",
            id="comma_paths",
        ),
        pytest.param(
            "https://h{i}.example/p{i}",
            ";",
            "https://h{i}.example/p{i}",
            id="semicolon_paths",
        ),
        pytest.param(
            "https://h{i}.example",
            ",",
            "https://h{i}.example",
            id="comma_roots",
        ),
        pytest.param(
            "(https://h{i}.example/p{i})",
            ",",
            "https://h{i}.example/p{i}",
            id="parenthesized",
        ),
        pytest.param(
            "[https://h{i}.example/p{i}]",
            ",",
            "https://h{i}.example/p{i}",
            id="bracketed",
        ),
        pytest.param(
            "'https://h{i}.example/p{i}'",
            ",",
            "https://h{i}.example/p{i}",
            id="single_quoted",
        ),
        pytest.param(
            "(https://h{i}.example",
            ",",
            "https://h{i}.example",
            id="unclosed_parentheses",
        ),
        pytest.param(
            "[x](https://h{i}.example/p{i})",
            "",
            "https://h{i}.example/p{i}",
            id="adjacent_markdown",
        ),
        pytest.param(
            "[x](https://h{i}.example/p{i})",
            " ",
            "https://h{i}.example/p{i}",
            id="spaced_markdown",
        ),
    ],
)
def test_compact_target_lists_scale_linearly(entry, joiner, target):
    """Each URL costs work in proportion to itself, not to the list.

    A compact list is one run with no whitespace. Rescanning the rest of
    the run for every URL, or every Markdown label for every URL, made
    16 times the URLs take hundreds of times as long. The bound compares
    growth on one machine instead of fixed milliseconds, so a loaded
    runner slows both sizes alike; linear growth is about 16 times.
    """
    extract = WaybackSearchEngine()._extract_urls_from_query
    small = joiner.join(entry.format(i=i) for i in range(250))
    large = joiner.join(entry.format(i=i) for i in range(4000))
    assert extract(small) == [target.format(i=i) for i in range(250)]
    assert extract(large) == [target.format(i=i) for i in range(4000)]

    gc.collect()
    gc_was_enabled = gc.isenabled()
    gc.disable()
    try:
        small_time = large_time = float("inf")
        for _attempt in range(5):
            small_time = _fastest(extract, small, small_time)
            large_time = _fastest(extract, large, large_time)
            if large_time < 64 * small_time:
                break
    finally:
        if gc_was_enabled:
            gc.enable()
    assert large_time < 64 * small_time, (
        f"16x the URLs took {large_time / small_time:.0f}x as long"
    )


@pytest.mark.parametrize(
    "query",
    [
        "historical information about a topic",
        "This sentence has a period.",
        "topic/with.a.path and words",
        "",
    ],
)
def test_plain_text_previews_do_not_search_or_request_archives(
    query, discovery_guards
):
    discovery, request = discovery_guards
    engine = WaybackSearchEngine()
    engine._get_wayback_snapshots = Mock(return_value=[])

    assert engine._get_previews(query) == []
    discovery.assert_not_called()
    engine._get_wayback_snapshots.assert_not_called()
    request.assert_not_called()


@pytest.mark.parametrize("closest_only", [False, True])
@pytest.mark.parametrize(
    ("query", "targets"),
    [
        pytest.param(
            "See [https://example.org] for history.",
            ["https://example.org"],
            id="bracket_followup_root_prose",
        ),
        pytest.param(
            "See [https://example.org/page?v=2] for history.",
            ["https://example.org/page?v=2"],
            id="bracket_followup_path_prose",
        ),
        pytest.param(
            "See [https://[2001:db8::1]] for history.",
            ["https://[2001:db8::1]"],
            id="bracket_followup_ipv6_root",
        ),
        pytest.param(
            "See [https://[2001:db8::1]:8443/a[b]?values=[x,y]"
            "&next=https://nested.example/c[d]#part] for history.",
            [
                "https://[2001:db8::1]:8443/a[b]?values=[x,y]"
                "&next=https://nested.example/c[d]#part"
            ],
            id="bracket_followup_ipv6_complete",
        ),
        pytest.param(
            "See [https://example.org/a[b[c]]?values=[x[y]]].",
            ["https://example.org/a[b[c]]?values=[x[y]]"],
            id="bracket_followup_nested_literal_pairs",
        ),
        pytest.param(
            "See [https://example.org/O'Reilly?x=[1]"
            "&next=https://nested.example/a] for history.",
            [
                "https://example.org/O'Reilly?x=[1]"
                "&next=https://nested.example/a"
            ],
            id="bracket_followup_apostrophe_nested_query",
        ),
        pytest.param(
            "[https://example.org/a[b]][https://second.example/c[d]?x=[1]]",
            [
                "https://example.org/a[b]",
                "https://second.example/c[d]?x=[1]",
            ],
            id="bracket_followup_adjacent_wrappers",
        ),
        pytest.param(
            "See [https://example.org/a[b]?v=[2]",
            ["https://example.org/a[b]?v=[2]"],
            id="bracket_followup_unclosed_wrapper",
        ),
        pytest.param(
            "See ``https://example.org/article?v=2`` for history.",
            ["https://example.org/article?v=2"],
            id="bracket_followup_double_backtick_control",
        ),
        pytest.param(
            "See ```https://example.org/a[b]?values=[x,]"
            "&next=https://nested.example/a#part``` for history.",
            [
                "https://example.org/a[b]?values=[x,]"
                "&next=https://nested.example/a#part"
            ],
            id="bracket_followup_triple_backtick_control",
        ),
        pytest.param(
            "example.org:8080/a[b]?values=[x]&next=https://nested.example/p[q]",
            [
                "http://example.org:8080/a[b]?values=[x]"
                "&next=https://nested.example/p[q]"
            ],
            id="bracket_followup_bare_literal_pairs",
        ),
        pytest.param(
            "example.org/a]?values=[x]]",
            ["http://example.org/a]?values=[x]]"],
            id="bracket_followup_bare_literal_closers",
        ),
        pytest.param(
            "[https://[2001:db8::1]/a[b]](https://target.example/p[q]?x=[v])",
            ["https://target.example/p[q]?x=[v]"],
            id="bracket_followup_url_label_control",
        ),
        *[
            pytest.param(
                template.format(target=target),
                [target],
                id=f"bracket_followup_{wrapper}_{case}",
            )
            for wrapper, template in [
                ("raw", "{target}"),
                ("single_quote", "See '{target}' for history."),
                ("double_quote", 'See "{target}" for history.'),
                ("angle", "See <{target}> for history."),
            ]
            for case, target in [
                (
                    "ipv6_literal_pairs",
                    "https://[2001:db8::1]:8443/a[b]?x=[v]"
                    "&next=https://other.example/p[q]#part",
                ),
                ("literal_closers", "https://example.org/a]?x=[v]]"),
                ("empty_literal_pairs", "https://example.org/a[]?values=[]"),
            ]
        ],
        pytest.param(
            "https://example.com/O'Reilly",
            ["https://example.com/O'Reilly"],
            id="regression_apostrophe_path",
        ),
        pytest.param(
            "https://example.com/page?author=O'Reilly&version=1",
            ["https://example.com/page?author=O'Reilly&version=1"],
            id="regression_apostrophe_query",
        ),
        pytest.param(
            "example.com/page?author=O'Reilly&version=1",
            ["http://example.com/page?author=O'Reilly&version=1"],
            id="regression_bare_apostrophe_query",
        ),
        pytest.param(
            "example.com:8080/O'Reilly?next=https://other.example/a#part",
            [
                "http://example.com:8080/O'Reilly"
                "?next=https://other.example/a#part"
            ],
            id="regression_bare_apostrophe_nested_query",
        ),
        pytest.param(
            "[source](https://example.com/O'Reilly)",
            ["https://example.com/O'Reilly"],
            id="regression_markdown_apostrophe",
        ),
        pytest.param(
            "<https://example.com/O'Reilly>",
            ["https://example.com/O'Reilly"],
            id="regression_angle_apostrophe",
        ),
        pytest.param(
            "'https://example.com/O'Reilly?value=a,'",
            ["https://example.com/O'Reilly?value=a,"],
            id="regression_single_quote_apostrophe",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly",
            ["https://example.com/O'Reilly"],
            id="quote_boundary_regression_unclosed_path",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly.",
            ["https://example.com/O'Reilly"],
            id="quote_boundary_regression_unclosed_sentence",
        ),
        pytest.param(
            "Compare 'https://example.com/page?author=O'Reilly&version=1",
            ["https://example.com/page?author=O'Reilly&version=1"],
            id="quote_boundary_regression_unclosed_query",
        ),
        pytest.param(
            "Compare 'https://example.com:8443/O'Reilly"
            "?next=https://other.example/a#part",
            [
                "https://example.com:8443/O'Reilly"
                "?next=https://other.example/a#part"
            ],
            id="quote_boundary_regression_unclosed_nested_query",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly and 'https://example.org/a'",
            ["https://example.com/O'Reilly", "https://example.org/a"],
            id="quote_boundary_regression_unclosed_first_target",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly'.",
            ["https://example.com/O'Reilly"],
            id="quote_boundary_control_closed_sentence",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly',"
            "'https://example.org/O'Brien'",
            ["https://example.com/O'Reilly", "https://example.org/O'Brien"],
            id="quote_boundary_control_comma_separated_targets",
        ),
        pytest.param(
            "Compare 'https://example.com/O'Reilly' "
            "and 'https://example.org/O'Brien'",
            ["https://example.com/O'Reilly", "https://example.org/O'Brien"],
            id="quote_boundary_control_separated_targets",
        ),
        pytest.param(
            '"https://example.com/O\'Reilly?value=a,"',
            ["https://example.com/O'Reilly?value=a,"],
            id="regression_double_quote_apostrophe",
        ),
        pytest.param(
            "Read https://example.com/O'Reilly.",
            ["https://example.com/O'Reilly"],
            id="regression_prose_apostrophe",
        ),
        pytest.param(
            "https://example.com/path'",
            ["https://example.com/path'"],
            id="regression_raw_trailing_apostrophe",
        ),
        pytest.param(
            "'https://example.com/path''",
            ["https://example.com/path'"],
            id="regression_quoted_trailing_apostrophe",
        ),
        pytest.param(
            "[https://example.com](https://example.org)",
            ["https://example.org"],
            id="regression_markdown_url_label",
        ),
        pytest.param(
            "[https://example.com/a]"
            "(https://example.org:8443/Foo_(bar)?version=1#part)",
            ["https://example.org:8443/Foo_(bar)?version=1#part"],
            id="regression_markdown_url_label_complete_target",
        ),
        pytest.param(
            "[https://example.com/a](<https://example.org/b?version=1>)",
            ["https://example.org/b?version=1"],
            id="regression_markdown_url_label_angle_target",
        ),
        pytest.param(
            "[read https://example.com/a](https://example.org/b?version=1)",
            ["https://example.org/b?version=1"],
            id="regression_markdown_url_label_with_text",
        ),
        pytest.param(
            "[https://[2001:db8::1]:8443/a](https://example.org/b)",
            ["https://example.org/b"],
            id="regression_markdown_ipv6_url_label",
        ),
        pytest.param(
            "[https://example.com/a](https://example.org/b)"
            "[source](https://third.example/c)",
            ["https://example.org/b", "https://third.example/c"],
            id="regression_adjacent_markdown_url_label",
        ),
        pytest.param(
            "`https://example.com/page?version=1`",
            ["https://example.com/page?version=1"],
            id="regression_code_span_complete_target",
        ),
        pytest.param(
            "`https://example.org`",
            ["https://example.org"],
            id="regression_code_span_root",
        ),
        pytest.param(
            "``https://example.com:8443/O'Reilly"
            "?next=https://other.example/a#part``",
            [
                "https://example.com:8443/O'Reilly"
                "?next=https://other.example/a#part"
            ],
            id="regression_multibacktick_span_nested_query",
        ),
        pytest.param(
            "`https://example.com/page?value=a,`",
            ["https://example.com/page?value=a,"],
            id="regression_code_span_literal_punctuation",
        ),
        pytest.param(
            "Read 'https://example.com/page?value=a,'.",
            ["https://example.com/page?value=a,"],
            id="control_single_quote_literal_punctuation",
        ),
        pytest.param(
            "'https://example.com/a','https://example.org/b'",
            ["https://example.com/a", "https://example.org/b"],
            id="control_adjacent_single_quoted_targets",
        ),
        pytest.param(
            "example.com:8080/page?next=https://other.example/a",
            ["http://example.com:8080/page?next=https://other.example/a"],
            id="nested_bare_target",
        ),
        pytest.param(
            "[source](https://example.com:8443/page?x=1)",
            ["https://example.com:8443/page?x=1"],
            id="markdown_link_target",
        ),
        pytest.param(
            "[source](https://example.com:8443/page?x=1).",
            ["https://example.com:8443/page?x=1"],
            id="markdown_sentence_target",
        ),
        pytest.param(
            "https://example.com:8443/page?version=1",
            ["https://example.com:8443/page?version=1"],
            id="complete_absolute_target",
        ),
        pytest.param(
            "Compare https://example.com, https://example.org",
            ["https://example.com", "https://example.org"],
            id="prose_comma",
        ),
        pytest.param(
            "See https://example.com; then compare https://example.org",
            ["https://example.com", "https://example.org"],
            id="prose_semicolon",
        ),
        pytest.param(
            "https://example.com,https://example.org",
            ["https://example.com", "https://example.org"],
            id="compact_comma_roots",
        ),
        pytest.param(
            "https://example.com/history,https://example.org/archive",
            ["https://example.com/history", "https://example.org/archive"],
            id="compact_comma_paths",
        ),
        pytest.param(
            "https://example.com/history;https://example.org/archive",
            ["https://example.com/history", "https://example.org/archive"],
            id="compact_semicolon_paths",
        ),
        pytest.param(
            "Compare https://example.com/history,https://example.org/archive",
            ["https://example.com/history", "https://example.org/archive"],
            id="prose_compact_comma_paths",
        ),
        pytest.param(
            "Compare https://example.com/history;https://example.org/archive",
            ["https://example.com/history", "https://example.org/archive"],
            id="prose_compact_semicolon_paths",
        ),
        pytest.param(
            "'https://example.com/history,https://example.org/archive'",
            ["https://example.com/history,https://example.org/archive"],
            id="quoted_literal_comma_http_path",
        ),
        pytest.param(
            '"https://example.com/history;https://example.org/archive"',
            ["https://example.com/history;https://example.org/archive"],
            id="quoted_literal_semicolon_http_path",
        ),
        pytest.param(
            "https://example.com,'https://example.org/a'",
            ["https://example.com", "https://example.org/a"],
            id="compact_root_then_quoted_target",
        ),
        pytest.param(
            "(https://example.com,(https://example.org",
            ["https://example.com", "https://example.org"],
            id="compact_unclosed_parenthesized_roots",
        ),
        pytest.param(
            "[https://example.com,[https://example.org",
            ["https://example.com", "https://example.org"],
            id="compact_unclosed_bracketed_roots",
        ),
        pytest.param(
            "[https://a.example](https://b.example) "
            "[x](https://c.example) https://d.example",
            ["https://b.example", "https://c.example", "https://d.example"],
            id="markdown_url_label_then_links_and_raw_url",
        ),
        pytest.param(
            "[first](https://example.com)[second](https://example.org)",
            ["https://example.com", "https://example.org"],
            id="adjacent_markdown",
        ),
        pytest.param(
            "[first](https://example.com/wiki/Foo_(bar))"
            "[second](https://example.org/wiki/Baz_(qux))",
            [
                "https://example.com/wiki/Foo_(bar)",
                "https://example.org/wiki/Baz_(qux)",
            ],
            id="adjacent_markdown_balanced_paths",
        ),
        pytest.param(
            "[first](https://example.com/page?next=https://other.example/a)"
            "[second](https://example.org/page?next=https://third.example/b)",
            [
                "https://example.com/page?next=https://other.example/a",
                "https://example.org/page?next=https://third.example/b",
            ],
            id="adjacent_markdown_nested_queries",
        ),
        pytest.param(
            "xn--e1afmkfd.xn--p1ai",
            ["http://xn--e1afmkfd.xn--p1ai"],
            id="bare_punycode_host",
        ),
        pytest.param(
            "xn--e1afmkfd.xn--p1ai/page",
            ["http://xn--e1afmkfd.xn--p1ai/page"],
            id="bare_punycode_path",
        ),
        pytest.param(
            "xn--e1afmkfd.xn--p1ai:8080",
            ["http://xn--e1afmkfd.xn--p1ai:8080"],
            id="bare_punycode_port",
        ),
        pytest.param(
            "xn--e1afmkfd.xn--p1ai?x=1",
            ["http://xn--e1afmkfd.xn--p1ai?x=1"],
            id="bare_punycode_query",
        ),
        pytest.param(
            "xn--e1afmkfd.xn--p1ai:8080/page?next=https://other.example/a#part",
            [
                "http://xn--e1afmkfd.xn--p1ai:8080/page"
                "?next=https://other.example/a#part"
            ],
            id="bare_punycode_complete_nested_target",
        ),
        pytest.param(
            "https://example.com/page?next=https://other.example/a",
            ["https://example.com/page?next=https://other.example/a"],
            id="nested_absolute_target",
        ),
        pytest.param(
            "(https://example.com/wiki/Foo_(bar)).",
            ["https://example.com/wiki/Foo_(bar)"],
            id="parenthesized_balanced_path_sentence",
        ),
        pytest.param(
            "(https://example.com/wiki/Foo_(bar))",
            ["https://example.com/wiki/Foo_(bar)"],
            id="parenthesized_balanced_path",
        ),
        pytest.param(
            "https://example.com/wiki/Foo_(bar)",
            ["https://example.com/wiki/Foo_(bar)"],
            id="unwrapped_balanced_path",
        ),
        pytest.param(
            "https://example.com/page)",
            ["https://example.com/page)"],
            id="unwrapped_literal_closing_parenthesis",
        ),
        pytest.param(
            "https://example.com/path,segment;version?values=a,b;c",
            ["https://example.com/path,segment;version?values=a,b;c"],
            id="literal_path_and_query_punctuation",
        ),
        pytest.param(
            "https://example.com/path,",
            ["https://example.com/path,"],
            id="literal_trailing_path_comma",
        ),
        pytest.param(
            "https://example.com/path;",
            ["https://example.com/path;"],
            id="literal_trailing_path_semicolon",
        ),
        pytest.param(
            "https://example.com/page?value=a,",
            ["https://example.com/page?value=a,"],
            id="literal_trailing_query_comma",
        ),
        pytest.param(
            "https://example.com/page?value=a;",
            ["https://example.com/page?value=a;"],
            id="literal_trailing_query_semicolon",
        ),
        pytest.param(
            "https://example.com/page"
            "?next=https://other.example/a,https://third.example/b",
            [
                "https://example.com/page"
                "?next=https://other.example/a,https://third.example/b"
            ],
            id="literal_nested_query_comma",
        ),
        pytest.param(
            "https://example.com/page"
            "?next=https://other.example/a;https://third.example/b",
            [
                "https://example.com/page"
                "?next=https://other.example/a;https://third.example/b"
            ],
            id="literal_nested_query_semicolon",
        ),
        *[
            pytest.param(
                template.format(target=target),
                [target],
                id=f"{wrapper}_{case}",
            )
            for wrapper, template in [
                ("angle_markdown", "[source](<{target}>)"),
                ("autolink", "<{target}>"),
            ]
            for case, target in [
                ("literal_path_period", "https://example.com/a."),
                ("literal_path_comma", "https://example.com/a,"),
                ("literal_path_semicolon", "https://example.com/a;"),
                ("literal_query_period", "https://example.com/page?value=a."),
                ("literal_query_comma", "https://example.com/page?value=a,"),
                (
                    "literal_query_semicolon",
                    "https://example.com/page?value=a;",
                ),
            ]
        ],
    ],
)
def test_archive_previews_keep_the_complete_target(
    query, targets, closest_only, discovery_guards, monkeypatch
):
    from local_deep_research.web_search_engines.engines import (
        search_engine_wayback as wayback,
    )

    discovery, request = discovery_guards
    timestamp = "20240101000000"
    replay_urls = [
        f"https://web.archive.org/web/{timestamp}/{target}"
        for target in targets
    ]

    def archive_response(_endpoint, *, params, **kwargs):
        if closest_only:
            assert kwargs == {"timeout": 30, "allow_redirects": False}
        target = params["url"]
        # Only intended URLs have captures: malformed targets must not
        # receive an archive response that could hide the parser defect.
        captured = target in targets
        response = Mock(status_code=200, headers={})
        if closest_only:
            response.json.return_value = {
                "archived_snapshots": {
                    "closest": {
                        "available": True,
                        "timestamp": timestamp,
                        "url": f"https://web.archive.org/web/{timestamp}/{target}",
                    }
                }
                if captured
                else {}
            }
        else:
            response.json.return_value = [
                ["timestamp", "original", "statuscode", "mimetype"],
            ]
            if captured:
                response.json.return_value.append(
                    [timestamp, target, "200", "text/html"]
                )
        return response

    archive_lookup = Mock(side_effect=archive_response)
    monkeypatch.setattr(wayback, "safe_get", archive_lookup)
    engine = WaybackSearchEngine(
        closest_only=closest_only, settings_snapshot={}
    )
    engine._configure_programmatic_mode(True)

    previews = engine._get_previews(query)

    calls = archive_lookup.call_args_list
    endpoint = engine.available_api if closest_only else engine.cdx_api
    assert [call.args[0] for call in calls] == [endpoint] * len(targets)
    assert [call.kwargs["params"]["url"] for call in calls] == targets
    assert [preview["original_url"] for preview in previews] == targets
    assert [preview["link"] for preview in previews] == replay_urls
    assert [preview["timestamp"] for preview in previews] == [timestamp] * len(
        targets
    )
    discovery.assert_not_called()
    request.assert_not_called()


def _no_capture(_endpoint, *, params, **_kwargs):
    response = Mock(status_code=200, headers={})
    if params.get("output") == "json":
        response.json.return_value = [
            ["timestamp", "original", "statuscode", "mimetype"]
        ]
    else:
        response.json.return_value = {"archived_snapshots": {}}
    return response


@pytest.mark.parametrize("closest_only", [False, True])
def test_previews_skip_targets_that_fetch_archive_refuses(
    closest_only, discovery_guards, monkeypatch, loguru_caplog
):
    """A user name or password never reaches the archive or the logs."""
    from local_deep_research.web_search_engines.engines import (
        search_engine_wayback as wayback,
    )

    discovery, request = discovery_guards
    secret = "pw-" + "x" * 8
    archive_lookup = Mock(side_effect=_no_capture)
    monkeypatch.setattr(wayback, "safe_get", archive_lookup)
    engine = WaybackSearchEngine(
        closest_only=closest_only, settings_snapshot={}
    )
    engine._configure_programmatic_mode(True)

    with loguru_caplog.at_level("INFO"):
        engine._get_previews(
            f"Compare https://user:{secret}@example.com/page, "
            "https://example.com:99999/old and https://example.org/a"
        )

    calls = archive_lookup.call_args_list
    assert [call.kwargs["params"]["url"] for call in calls] == [
        "https://example.org/a"
    ]
    assert secret not in loguru_caplog.text
    assert "Skipping Wayback target https://example.com:" in loguru_caplog.text
    discovery.assert_not_called()
    request.assert_not_called()


def test_previews_look_up_at_most_max_results_targets(
    discovery_guards, monkeypatch
):
    from local_deep_research.web_search_engines.engines import (
        search_engine_wayback as wayback,
    )

    discovery, request = discovery_guards
    archive_lookup = Mock(side_effect=_no_capture)
    monkeypatch.setattr(wayback, "safe_get", archive_lookup)
    engine = WaybackSearchEngine(max_results=2, settings_snapshot={})
    engine._configure_programmatic_mode(True)

    engine._get_previews(" ".join(f"https://h{i}.example/p" for i in range(5)))

    assert [
        call.kwargs["params"]["url"] for call in archive_lookup.call_args_list
    ] == ["https://h0.example/p", "https://h1.example/p"]
    discovery.assert_not_called()
    request.assert_not_called()