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.

11.1 KB
Raw Blame
"""Pin the shared replay grammar and deliberate citation/transport differences.

Citation recognition preserves a target's spelling. Transport additionally
requires a valid capture date and an HTTPS wrapper inside the capture guard.
"""

from urllib.parse import urlsplit

import pytest

from local_deep_research.utilities.search_utilities import (
    count_distinct_sources,
    format_links_to_markdown,
)
from local_deep_research.utilities.url_utils import (
    _is_wayback_replay_url,
    canonical_url_key,
)
from local_deep_research.web_search_engines import wayback_client


CAPTURE = "20240203112233"
TARGET = "https://example.com/history/?utm_source=capture#section"
REPLAY = f"https://web.archive.org/web/{CAPTURE}/{TARGET}"


@pytest.fixture(autouse=True)
def no_network(monkeypatch):
    def unexpected_request(*args, **kwargs):
        pytest.fail("Replay classification must not perform network access")

    monkeypatch.setattr(wayback_client, "safe_get", unexpected_request)
    monkeypatch.setattr("requests.sessions.Session.send", unexpected_request)
    monkeypatch.setattr("socket.getaddrinfo", unexpected_request)


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
@pytest.mark.parametrize(
    "authority",
    [
        "web.archive.org",
        "web.archive.org:443",
        "WEB.ARCHIVE.ORG",
        "WeB.ArChIvE.OrG:443",
    ],
)
@pytest.mark.parametrize(
    "target",
    [
        pytest.param("https://example.com", id="bare_target_host"),
        pytest.param("http://example.com/history/", id="http_target"),
        pytest.param(
            "https://Example.COM:443/History/%2f/%7Eauthor/",
            id="target_case_port_and_encoding",
        ),
        pytest.param(
            "https://example.com/history?%75tm_source=capture&&q=a+b&q=a%20b&",
            id="raw_query_spelling",
        ),
        pytest.param(
            "https://example.com/history/"
            "?next=https://other.example/a%2Fb/&fbclid=capture#section",
            id="nested_url_and_tracking",
        ),
        pytest.param(
            "https://example.com/history/?q=%23encoded%3F&x=1&x=2#part%2f",
            id="encoded_delimiters_and_fragment",
        ),
        pytest.param("https://example.com/history/?", id="empty_query"),
        pytest.param("https://example.com/history/#", id="empty_fragment"),
        pytest.param(
            "https://example.com/history/#/route?utm_source=fragment",
            id="fragment_route",
        ),
    ],
)
def test_supported_replays_share_grammar_and_preserve_identity(
    modifier, authority, target
):
    replay = f"https://{authority}/web/{CAPTURE}{modifier}/{target}"

    assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
    assert wayback_client.capture_metadata_from_replay_url(replay) == {
        "source_type": "archive",
        "original_url": target,
        "requested_url": target,
        "timestamp": CAPTURE,
        "formatted_date": "2024-02-03 11:22:33 UTC",
    }
    assert canonical_url_key(replay) == replay


@pytest.mark.parametrize(
    "replay,error",
    [
        # These two public #7569 families must raise, never return None.
        pytest.param(
            REPLAY.replace("web.archive.org", "archive.org"),
            "Unsupported archive replay capture URL",
            id="archive_org_redirector_7569",
        ),
        pytest.param(
            REPLAY.replace("/web/", "/WEB/"),
            "Unsupported archive replay capture URL",
            id="uppercase_web_7569",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "web.archive.org."),
            "left the selected capture",
            id="trailing_dot_host",
        ),
        pytest.param(
            REPLAY.replace("/web/", "//web/"),
            "Unsupported archive replay capture URL",
            id="double_leading_slash",
        ),
        pytest.param(
            REPLAY.replace("/web/", "/web//"),
            "Unsupported archive replay capture URL",
            id="double_capture_slash",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "%77eb.archive.org"),
            "left the selected capture",
            id="encoded_wrapper_host",
        ),
        pytest.param(
            REPLAY.replace("/web/", "/%77eb/"),
            "Unsupported archive replay capture URL",
            id="encoded_wrapper_path",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, "%32" + CAPTURE[1:]),
            "Unsupported archive replay capture URL",
            id="encoded_timestamp",
        ),
        pytest.param(
            REPLAY.replace("/web/", "/about/../web/"),
            "Unsupported archive replay capture URL",
            id="wrapper_dot_segments",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, "20240203"),
            "Unsupported archive replay capture URL",
            id="partial_timestamp",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, CAPTURE + "0"),
            "Unsupported archive replay capture URL",
            id="overlong_timestamp",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, "*"),
            "Unsupported archive replay capture URL",
            id="wildcard_timestamp",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, "٢٠٢٤٠٢٠٣١١٢٢٣٣"),
            "Unsupported archive replay capture URL",
            id="non_ascii_timestamp",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, CAPTURE + "im_"),
            "Unsupported archive replay capture URL",
            id="unsupported_modifier",
        ),
        pytest.param(
            REPLAY.replace(CAPTURE, CAPTURE + "ID_"),
            "Unsupported archive replay capture URL",
            id="uppercase_modifier",
        ),
        pytest.param(
            REPLAY.replace("https://", "https://user:password@", 1),
            "left the selected capture",
            id="wrapper_credentials",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "web.archive.org:8443"),
            "left the selected capture",
            id="wrapper_nondefault_port",
        ),
        pytest.param(
            REPLAY.replace(TARGET, "https://user:[email protected]/history"),
            "without credentials",
            id="target_credentials",
        ),
        pytest.param(
            REPLAY.replace(TARGET, "https:///history"),
            "without credentials",
            id="missing_target_host",
        ),
        pytest.param(
            REPLAY.replace(TARGET, "https://example.com:65536/history"),
            "Port out of range",
            id="invalid_target_port",
        ),
        pytest.param(
            REPLAY.replace(TARGET, "ftp://example.com/history"),
            "Unsupported archive replay capture URL",
            id="unsupported_target_scheme",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "ARCHIVE.ORG"),
            "Unsupported archive replay capture URL",
            id="archive_org_redirector_uppercase_host",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "archive.org:443"),
            "Unsupported archive replay capture URL",
            id="archive_org_redirector_default_port",
        ),
        pytest.param(
            REPLAY.replace("web.archive.org", "archive.org."),
            "Unsupported archive replay capture URL",
            id="archive_org_redirector_trailing_dot",
        ),
    ],
)
def test_unsupported_replay_spellings_are_not_citations_or_live_fetches(
    replay, error
):
    assert _is_wayback_replay_url(replay, urlsplit(replay)) is False
    with pytest.raises(ValueError, match=error):
        wayback_client.capture_metadata_from_replay_url(replay)


@pytest.mark.parametrize(
    "wrapper",
    [
        "http://web.archive.org",
        "HTTP://WEB.ARCHIVE.ORG:80",
        "http://web.archive.org:443",
        "https://web.archive.org:80",
    ],
)
def test_citation_spelling_survives_transport_https_and_port_restrictions(
    wrapper,
):
    replay = f"{wrapper}/web/{CAPTURE}/{TARGET}"

    assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
    assert canonical_url_key(replay) == replay
    with pytest.raises(ValueError, match="left the selected capture"):
        wayback_client.capture_metadata_from_replay_url(replay)


@pytest.mark.parametrize(
    "timestamp",
    ["20240230112233", "20230229112233", "20241303112233", "00000203112233"],
)
def test_citation_grammar_preserves_dates_that_transport_calendar_rejects(
    timestamp,
):
    replay = REPLAY.replace(CAPTURE, timestamp)

    assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
    assert canonical_url_key(replay) == replay
    # Calendar error messages come from datetime and vary by Python version.
    with pytest.raises(ValueError):
        wayback_client.capture_metadata_from_replay_url(replay)


@pytest.mark.parametrize(
    "url",
    [
        TARGET,
        "https://web.archive.org/about/",
        "https://archive.org/wayback/available?url=" + TARGET,
        "https://web.archive.org/about/?next=" + REPLAY,
        "https://archive.org/about/" + TARGET + "/web/" + CAPTURE,
        REPLAY.replace("web.archive.org", "other.example"),
        REPLAY.replace("web.archive.org", "web.archive.org.evil.example"),
        REPLAY.removeprefix("https:"),
    ],
)
def test_non_replays_are_recognized_by_neither_grammar(url):
    assert _is_wayback_replay_url(url, urlsplit(url)) is False
    assert wayback_client.capture_metadata_from_replay_url(url) is None


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
@pytest.mark.parametrize(
    "first,second",
    [
        ("https://example.com/history", "https://example.com/history/"),
        (
            "https://example.com/history?utm_source=first",
            "https://example.com/history?utm_source=second",
        ),
        (
            "https://example.com/history?q=a+b",
            "https://example.com/history?q=a%20b",
        ),
        (
            "https://example.com/history?x=1&y=2",
            "https://example.com/history?y=2&x=1",
        ),
        (
            "https://example.com/history#first",
            "https://example.com/history#second",
        ),
        ("https://example.com/history", "https://example.com/history?"),
        ("https://example.com/history", "https://example.com/history#"),
    ],
)
def test_public_citation_entry_points_keep_replay_targets_distinct(
    modifier, first, second
):
    replays = [
        f"https://web.archive.org/web/{CAPTURE}{modifier}/{target}"
        for target in (first, second)
    ]
    links = []
    for index, (replay, target) in enumerate(zip(replays, (first, second)), 1):
        assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
        metadata = wayback_client.capture_metadata_from_replay_url(replay)
        assert metadata["original_url"] == target
        assert canonical_url_key(replay) == replay
        links.append({"index": str(index), "title": "Capture", "link": replay})

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