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.

24.1 KB
Raw Blame
"""Bracketed URL hosts must not be interpreted as citation numbers."""

import pytest

from local_deep_research.utilities.search_utilities import (
    extract_cited_indices_and_urls,
    filter_cited_links,
    format_links_to_markdown,
)
from local_deep_research.utilities.url_utils import canonical_url_key


@pytest.mark.parametrize(
    "url",
    [
        "https://[2001:DB8::A]:8443/Source",
        "https://[::a]/Source",
        "http://[::FFFF:192.0.2.1]:8080/Source",
        "https://[fe80::ABCD%25Eth0]:8443/Source",
        "https://user:password@[2001:DB8::A]:8443/Source",
        "https://@[2001:DB8::A]/Source",
        "https://@[::a]/Source",
        "https://user:@[2001:DB8::A]/Source",
        "https://:password@[2001:DB8::A]/Source",
    ],
)
@pytest.mark.parametrize(
    "prose_template",
    ["Read {url}", "Read [IPv6]({url}).", "Read <{url}>."],
)
def test_ipv6_url_citation_filters_out_unrelated_sources(url, prose_template):
    cited = {"index": "1", "title": "IPv6", "url": canonical_url_key(url)}
    unrelated = {
        "index": "2",
        "title": "Unrelated",
        "url": "https://other.example/Different",
    }
    prose = prose_template.format(url=url)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == set()
    assert canonical_url_key(url) in urls
    assert not indeterminate
    assert filter_cited_links([cited, unrelated], prose, mode="strict") == [
        cited
    ]
    rendered = format_links_to_markdown(
        [cited, unrelated], prose=prose, uncited_mode="strict"
    )
    assert canonical_url_key(url) in rendered
    assert "other.example" not in rendered


@pytest.mark.parametrize(
    "different_url",
    [
        "https://[fe80::abcd%25eth0]:8443/Source?q=AbC",
        "https://[fe80::abcd%25Eth0]:8444/Source?q=AbC",
        "https://[fe80::abcd%25Eth0]:8443/source?q=AbC",
        "https://[fe80::abcd%25Eth0]:8443/Source?q=abc",
    ],
)
def test_ipv6_citation_preserves_source_identity(different_url):
    url = "https://[fe80::ABCD%25Eth0]:8443/Source?q=AbC"
    cited = {"index": "1", "url": canonical_url_key(url)}
    different = {"index": "2", "url": different_url}

    assert filter_cited_links(
        [cited, different], f"Read {url}", mode="strict"
    ) == [cited]


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        ("https://[2001:DB8::A]", "https://[2001:db8::a]"),
        ("https://[2001:DB8::A]/Source", "https://[2001:db8::a]/Source"),
        ("https://@[::A]/Source", "https://[::a]/Source"),
    ],
)
@pytest.mark.parametrize(
    "other_citation",
    ["[3]", "[[3]]", "【3】", "[^3]", "[arxiv.org-3]", "Source 3"],
)
def test_ipv6_host_does_not_hide_other_citation_styles(
    url, expected_url, other_citation
):
    cited = {"index": "1", "url": expected_url}
    other_cited = {"index": "3", "url": "https://other.example/Cited"}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    prose = f"Read {url} and {other_citation}."

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {3}
    assert cited["url"] in urls
    assert not indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links(
            [cited, unrelated, other_cited], prose, mode=mode
        ) == [cited, other_cited]


@pytest.mark.parametrize(
    "prose_template", ["Read {url}", "Read [IPv6]({url})."]
)
def test_bare_ipv6_host_keeps_its_closing_bracket(prose_template):
    url = "https://[2001:db8::a]"
    cited = {"index": "1", "url": url}
    truncated = {"index": "2", "url": "https://[2001:db8::a"}
    prose = prose_template.format(url=url)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == set()
    assert url in urls
    assert truncated["url"] not in urls
    assert not indeterminate
    assert filter_cited_links([cited, truncated], prose, mode="strict") == [
        cited
    ]


@pytest.mark.parametrize(
    "prose",
    [
        "Read https://[2001:DB8::A]/Source and [1,abc,3].",
        "Read https://[1:broken]/Source and [3].",
        "Read https://[2001:DB8::A]/Source[1,abc,3].",
        "Read https://[192.0.2.1]/Source and [3].",
        "Read https://[2001:db8::a/Source and [3].",
        "Read https://[1-1002]/Source and [3].",
        "Read https://[2001:DB8::A]suffix and [3].",
        "Read https://[2001:DB8::A]:broken/Source and [3].",
        "Read https://[2001:DB8::A]:65536/Source and [3].",
        "Read https://[2001:DB8::A]/Source and [1-1002].",
        "Read https://[2001:DB8::A]/Source and Sources 1-1002.",
        "Read https://[2001:DB8::A]/Source and [1-99999999999999999999].",
        "Read https://[2001:DB8::A]/Source.\n```\nunfinished code [3]",
    ],
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_malformed_citations_still_preserve_the_fallback(prose, mode):
    links = [
        {"index": "1", "url": "https://[2001:db8::a]/Source"},
        {"index": "2", "url": "https://other.example/Unrelated"},
    ]

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


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        ("https://[2001:DB8::A]", "https://[2001:db8::a]"),
        ("https://[2001:DB8::A]:443", "https://[2001:db8::a]"),
        (
            "https://[2001:DB8::A]:8443/Source",
            "https://[2001:db8::a]:8443/Source",
        ),
        (
            "https://[2001:DB8::A]/Source_(IPv6)",
            "https://[2001:db8::a]/Source_(IPv6)",
        ),
        (
            "https://[2001:DB8::A]?q=AbC",
            "https://[2001:db8::a]?q=AbC",
        ),
        (
            "https://[2001:DB8::A]/Source%E3%80%91",
            "https://[2001:db8::a]/Source%E3%80%91",
        ),
        ("https://EXAMPLE.com/Source", "https://example.com/Source"),
        ("/library/document/DocA", "/library/document/DocA"),
    ],
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_fullwidth_url_wrapper_does_not_drop_a_cited_source(
    url, expected_url, mode
):
    cited = {"index": "1", "url": expected_url}
    other_cited = {"index": "3", "url": "https://other.example/Cited"}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    prose = f"Read 【{url}】。 See [3]."

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {3}
    assert expected_url in urls
    assert not indeterminate
    assert filter_cited_links(
        [cited, unrelated, other_cited], prose, mode=mode
    ) == [cited, other_cited]


@pytest.mark.parametrize("host", ["v1.source-1", "vF.arxiv-2"])
@pytest.mark.parametrize(
    "prose_template",
    ["Read {url}", "Read [Future IP]({url}).", "Read <{url}>."],
)
def test_valid_ip_literal_is_not_a_tagged_citation(host, prose_template):
    url = f"https://[{host}]/Source"
    cited = {"index": "7", "url": f"https://[{host.lower()}]/Source"}
    unrelated = [
        {"index": "1", "url": "https://other.example/One"},
        {"index": "2", "url": "https://other.example/Two"},
    ]
    prose = prose_template.format(url=url)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == set()
    assert cited["url"] in urls
    assert not indeterminate
    assert filter_cited_links([*unrelated, cited], prose, mode="strict") == [
        cited
    ]


@pytest.mark.parametrize("fence", ["```", "~~~"])
@pytest.mark.parametrize("prefix", ["", "> ", "- "])
def test_fenced_ipv6_urls_cannot_select_a_source_or_force_fallback(
    fence, prefix
):
    cited = {"index": "1", "url": "https://[2001:db8::a]/Cited"}
    hidden = {"index": "2", "url": "https://[2001:db8::b]/Hidden"}
    unrelated = {"index": "3", "url": "https://other.example/Unrelated"}
    prose = (
        f"{prefix}{fence}text\n"
        f"{prefix}https://[2001:DB8::B]/Hidden [3] [1,abc,3]\n"
        f"{prefix}{fence}\n"
        "Read https://[2001:DB8::A]/Cited."
    )

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == set()
    assert cited["url"] in urls
    assert hidden["url"] not in urls
    assert not indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links(
            [cited, hidden, unrelated], prose, mode=mode
        ) == [cited]


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        ("HTTP://[2001:DB8::A]:80/Source", "http://[2001:db8::a]/Source"),
        ("HTTPS://[2001:DB8::A]:443/Source", "https://[2001:db8::a]/Source"),
        (
            "https://[2001:DB8::A]:443/Source?q=AbC&utm_source=test#section",
            "https://[2001:db8::a]/Source?q=AbC",
        ),
    ],
)
def test_ipv6_citation_matches_normalized_source_urls(url, expected_url):
    cited = {"index": "1", "url": expected_url}
    unrelated = {
        "index": "2",
        "url": expected_url.replace("/Source", "/source"),
    }
    prose = f"Read [IPv6](<{url}>)."

    assert filter_cited_links([cited, unrelated], prose, mode="strict") == [
        cited
    ]


def test_ipv6_source_credentials_do_not_appear_in_bibliography():
    url = "https://user:password@[2001:DB8::A]:8443/Source"
    cited = {"index": "1", "title": "IPv6", "url": url}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}

    rendered = format_links_to_markdown(
        [cited, unrelated], prose=f"Read {url}", uncited_mode="strict"
    )

    assert "https://[2001:db8::a]:8443/Source" in rendered
    assert "user" not in rendered
    assert "password" not in rendered
    assert "other.example" not in rendered


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        ("https://[2001:DB8::A]", "https://[2001:db8::a]"),
        ("https://@[::A]", "https://[::a]"),
        ("https://[v1.source-1]", "https://[v1.source-1]"),
        ("https://[fe80::1%25Sources]", "https://[fe80::1%25Sources]"),
    ],
)
@pytest.mark.parametrize(
    "prose_template",
    [
        "Sources\n{url}\n3 experiments were reproduced.",
        "Source {url} 3 experiments were reproduced.",
        "{url} 3 experiments were reproduced.",
    ],
)
def test_masking_host_brackets_cannot_manufacture_source_phrases(
    url, expected_url, prose_template
):
    cited = {"index": "1", "url": expected_url}
    unrelated = {"index": "3", "url": "https://other.example/Unrelated"}
    prose = prose_template.format(url=url)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == set()
    assert expected_url in urls
    assert not indeterminate
    for mode in ("strict", "fallback"):
        assert filter_cited_links([cited, unrelated], prose, mode=mode) == [
            cited
        ]


@pytest.mark.parametrize(
    "url",
    ["https://[2001:DB8::A]", "https://[::A]", "https://@[::A]"],
)
@pytest.mark.parametrize(
    "prose_template",
    ["Reference [1 {url}].", "Reference [1-{url}3].", "Reference [1,2{url}]."],
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_ip_literal_inside_malformed_citation_keeps_fallback(
    url, prose_template, mode
):
    links = [
        {"index": "1", "url": "https://other.example/One"},
        {"index": "2", "url": canonical_url_key(url)},
        {"index": "4", "url": "https://other.example/Four"},
    ]
    prose = prose_template.format(url=url)

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


@pytest.mark.parametrize(
    "url",
    [
        "https://example.com/redirect?next=https://[2001:DB8::A]/Source",
        "https://example.com/path/https://[2001:DB8::A]/Source",
    ],
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_embedded_ip_literal_does_not_hide_an_ambiguous_url(url, mode):
    links = [
        {"index": "1", "url": url},
        {"index": "2", "url": "https://other.example/Unrelated"},
        {"index": "3", "url": "https://other.example/Cited"},
    ]
    prose = f"Read {url} and [3]."

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


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        ("https://[2001:DB8::A]", "https://[2001:db8::a]"),
        ("https://[2001:DB8::A]/Source", "https://[2001:db8::a]/Source"),
        (
            "https://[2001:DB8::A]:8443/Source",
            "https://[2001:db8::a]:8443/Source",
        ),
        (
            "https://a)b@[2001:DB8::A]/Source_(IPv6)",
            "https://[2001:db8::a]/Source_(IPv6)",
        ),
        (
            "https://[fe80::ABCD%25Eth0]/Source?q=AbC",
            "https://[fe80::abcd%25Eth0]/Source?q=AbC",
        ),
        (
            "https://[2001:DB8::A]/Source%E2%80%9D",
            "https://[2001:db8::a]/Source%E2%80%9D",
        ),
        ("https://EXAMPLE.com/Source", "https://example.com/Source"),
        ("/library/document/DocA", "/library/document/DocA"),
    ],
)
@pytest.mark.parametrize(
    "quote_pair", [("“", "”"), ("‘", "’"), ("«", "»"), ("《", "》")]
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_typographic_quotes_preserve_cited_sources(
    url, expected_url, quote_pair, mode
):
    cited = {"index": "1", "url": expected_url}
    other_cited = {"index": "4", "url": "https://other.example/Four"}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    prose = f"Read {quote_pair[0]}{url}{quote_pair[1]}. See [4]."

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {4}
    assert expected_url in urls
    assert not indeterminate
    assert filter_cited_links(
        [cited, unrelated, other_cited], prose, mode=mode
    ) == [cited, other_cited]


@pytest.mark.parametrize(
    ("url", "expected_url", "prose_template"),
    [
        (
            "https://a(b@[2001:DB8::A]/Source",
            "https://[2001:db8::a]/Source",
            "Read ({url}). See [4].",
        ),
        (
            "https://a)b@[2001:DB8::A]/Source_(IPv6)",
            "https://[2001:db8::a]/Source_(IPv6)",
            "Read {url}. See [4].",
        ),
        (
            "https://a)b@[2001:DB8::A]/Source_(IPv6)",
            "https://[2001:db8::a]/Source_(IPv6)",
            "Read [IPv6](<{url}>). See [4].",
        ),
        (
            "https://user:pa(ss@[fe80::ABCD%25Eth0]:8443/Source_(IPv6)",
            "https://[fe80::abcd%25Eth0]:8443/Source_(IPv6)",
            "Read ({url}). See [4].",
        ),
        (
            "https://user:pa)ss@[2001:DB8::A]/Source?q=Value_(A)",
            "https://[2001:db8::a]/Source?q=Value_(A)",
            "Read {url}. See [4].",
        ),
        (
            "https://[v1.a(b]/Source",
            "https://[v1.a(b]/Source",
            "Read ({url}). See [4].",
        ),
        (
            "https://[v1.a)b]/Source_(IPv6)",
            "https://[v1.a)b]/Source_(IPv6)",
            "Read {url}. See [4].",
        ),
    ],
)
@pytest.mark.parametrize("mode", ["strict", "fallback"])
def test_authority_parentheses_do_not_change_the_cited_path(
    url, expected_url, prose_template, mode
):
    cited = {"index": "1", "url": expected_url}
    truncated = {"index": "2", "url": expected_url[:-1]}
    wrapped = {"index": "3", "url": expected_url + ")"}
    other_cited = {"index": "4", "url": "https://other.example/Four"}
    prose = prose_template.format(url=url)

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indices == {4}
    assert canonical_url_key(expected_url) in urls
    assert canonical_url_key(truncated["url"]) not in urls
    assert not indeterminate
    assert filter_cited_links(
        [cited, truncated, wrapped, other_cited], prose, mode=mode
    ) == [cited, other_cited]


def test_authority_parentheses_do_not_render_a_different_source():
    cited = {
        "index": "1",
        "title": "Cited",
        "url": "https://[2001:db8::a]/Source_(IPv6)",
    }
    truncated = {
        "index": "2",
        "title": "Wrong",
        "url": "https://[2001:db8::a]/Source_(IPv6",
    }
    prose = "Read https://a)b@[2001:DB8::A]/Source_(IPv6). See [4]."

    rendered = format_links_to_markdown([cited, truncated], prose=prose)

    assert cited["url"] in rendered
    assert "Wrong" not in rendered


@pytest.mark.parametrize("mode", ["strict", "fallback"])
@pytest.mark.parametrize(
    ("path", "wrong_path"),
    [("/O'Reilly", "/O"), ("/Source?q=O'Reilly", "/Source?q=O")],
)
@pytest.mark.parametrize(
    "template",
    [
        "Read {url}. See [4].",
        "Read [Book](<{url}>). See [4].",
        "Read <{url}>. See [4].",
    ],
)
def test_apostrophe_does_not_select_a_truncated_source(
    mode, path, wrong_path, template
):
    url = "https://[2001:DB8::A]" + path
    cited = {"index": "1", "url": canonical_url_key(url)}
    wrong = {"index": "2", "url": "https://[2001:db8::a]" + wrong_path}
    other = {"index": "4", "url": "https://other.example/Four"}
    links = [cited, wrong, other]
    prose = template.format(url=url)
    assert filter_cited_links(links, prose, mode=mode) == [cited, other]


@pytest.mark.parametrize("mode", ["strict", "fallback"])
@pytest.mark.parametrize(
    ("path", "wrong_path"),
    [
        ("/Article)", "/Article"),
        ("/Article.", "/Article"),
        ("/Article!", "/Article"),
        ("/Article;", "/Article"),
        ("/Source_", "/Source"),
        ("/Source_*", "/Source"),
        ("/Source?q=Value)", "/Source?q=Value"),
    ],
)
def test_sentence_punctuation_does_not_remove_url_content(
    mode, path, wrong_path
):
    url = "https://[2001:DB8::A]" + path
    cited = {"index": "1", "url": canonical_url_key(url)}
    wrong = {"index": "2", "url": "https://[2001:db8::a]" + wrong_path}
    other = {"index": "4", "url": "https://other.example/Four"}
    links = [cited, wrong, other]
    prose = f"Read {url}. See [4]."
    kept = filter_cited_links(links, prose, mode=mode)
    if path.endswith(")"):
        assert kept in ([cited, other], [cited, wrong, other])
    else:
        assert kept == [cited, other]


@pytest.mark.parametrize("mode", ["strict", "fallback"])
@pytest.mark.parametrize(
    ("path", "wrong_path"),
    [
        ("/Article.", "/Article"),
        ("/Article!", "/Article"),
        ("/Article;", "/Article"),
        ("/Source_", "/Source"),
        ("/Source_*", "/Source"),
    ],
)
def test_markdown_destination_keeps_its_last_url_character(
    mode, path, wrong_path
):
    url = "https://[2001:DB8::A]" + path
    cited = {"index": "1", "url": canonical_url_key(url)}
    wrong = {"index": "2", "url": "https://[2001:db8::a]" + wrong_path}
    other = {"index": "4", "url": "https://other.example/Four"}
    links = [cited, wrong, other]
    prose = f"Read [Article]({url}). See [4]."
    assert filter_cited_links(links, prose, mode=mode) == [cited, other]


@pytest.mark.parametrize("mode", ["strict", "fallback"])
@pytest.mark.parametrize(
    "quotes", [("„", "“"), ("‚", "‘"), ("»", "«"), ("‹", "›")]
)
def test_additional_quote_styles_keep_the_cited_source(mode, quotes):
    cited = {"index": "1", "url": "https://[2001:db8::a]/Source"}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    other = {"index": "4", "url": "https://other.example/Four"}
    links = [cited, unrelated, other]
    prose = f"Read {quotes[0]}https://[2001:DB8::A]/Source{quotes[1]}. See [4]."
    assert filter_cited_links(links, prose, mode=mode) == [cited, other]


@pytest.mark.parametrize("mode", ["strict", "fallback"])
@pytest.mark.parametrize(
    "prose",
    [
        "Reference [1,\nhttps://[2001:DB8::A]/Source 3]. See [4].",
        "Reference [1, https://[2001:DB8::A]/Source "
        + "x" * 300
        + ",3]. See [4].",
        "Reference [[1,\nhttps://[2001:DB8::A]/Source 3]]. See [4].",
    ],
)
def test_masking_does_not_remove_long_or_multiline_citation_fallback(
    mode, prose
):
    links = [
        {"index": "1", "url": "https://other.example/One"},
        {"index": "2", "url": "https://other.example/Two"},
        {"index": "3", "url": "https://[2001:db8::a]/Source"},
        {"index": "4", "url": "https://other.example/Four"},
    ]
    assert extract_cited_indices_and_urls(prose)[2]
    assert filter_cited_links(links, prose, mode=mode) == links


def test_apostrophe_source_is_not_replaced_in_the_bibliography():
    cited = {
        "index": "1",
        "title": "Cited",
        "url": "https://[2001:db8::a]/O'Reilly",
    }
    wrong = {"index": "2", "title": "Wrong", "url": "https://[2001:db8::a]/O"}
    prose = "Read [Book](<https://[2001:DB8::A]/O'Reilly>)."
    rendered = format_links_to_markdown([cited, wrong], prose=prose)
    assert cited["url"] in rendered
    assert "Wrong" not in rendered


@pytest.mark.parametrize("path", ["/Article.", "/Source_", "/O'Reilly"])
@pytest.mark.parametrize(
    "template",
    [
        'Read "{url}". See [4].',
        "Read '{url}'. See [4].",
        'Read [Article]({url} "A title (with parentheses)"). See [4].',
        "Read [Article]({url} 'A title'). See [4].",
        "Read **{url}**. See [4].",
        "Read __{url}__. See [4].",
        "Read `{url}`. See [4].",
    ],
)
def test_url_content_survives_quotes_titles_and_emphasis(path, template):
    cited = {"index": "1", "url": "https://[2001:db8::a]" + path}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    other = {"index": "4", "url": "https://other.example/Four"}
    prose = template.format(url="https://[2001:DB8::A]" + path)

    for mode in ("strict", "fallback"):
        assert filter_cited_links(
            [cited, unrelated, other], prose, mode=mode
        ) == [cited, other]


@pytest.mark.parametrize(
    ("url", "expected_url"),
    [
        (
            "https://user:pa'ss@[2001:DB8::A]/Source",
            "https://[2001:db8::a]/Source",
        ),
        ("https://[v1.a'b]/Source", "https://[v1.a'b]/Source"),
        (
            "https://[2001:DB8::A]/Source\\_(IPv6\\)",
            "https://[2001:db8::a]/Source_(IPv6)",
        ),
        ("/library/document/DocA_(B)", "/library/document/DocA_(B)"),
    ],
)
def test_markdown_destination_preserves_authority_and_escaped_content(
    url, expected_url
):
    cited = {"index": "1", "url": expected_url}
    unrelated = {"index": "2", "url": "https://other.example/Unrelated"}
    other = {"index": "4", "url": "https://other.example/Four"}
    prose = f"Read [Source](<{url}>). See [4]."

    for mode in ("strict", "fallback"):
        assert filter_cited_links(
            [cited, unrelated, other], prose, mode=mode
        ) == [cited, other]


def test_invalid_markdown_destination_cannot_mask_a_truncated_authority():
    links = [
        {"index": "1", "url": "https://[2001:db8::a]/Source"},
        {"index": "2", "url": "https://a"},
        {"index": "4", "url": "https://other.example/Four"},
    ]
    prose = "Read [Source](https://a)b@[2001:DB8::A]/Source). See [4]."

    assert extract_cited_indices_and_urls(prose)[2]
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == links


def test_malformed_multiline_footnote_retains_the_fallback():
    links = [
        {"index": "1", "url": "https://other.example/One"},
        {"index": "2", "url": "https://[2001:db8::a]/Source"},
        {"index": "4", "url": "https://other.example/Four"},
    ]
    prose = "Reference [^1,\nhttps://[2001:DB8::A]/Source 3]. See [4]."

    assert extract_cited_indices_and_urls(prose)[2]
    for mode in ("strict", "fallback"):
        assert filter_cited_links(links, prose, mode=mode) == links


def test_long_nested_malformed_citation_is_checked_in_linear_time():
    import time

    prose = (
        "[" * 100_000
        + "1, https://[2001:DB8::A]/Source "
        + "x" * 100_000
        + "] See [4]."
    )
    started = time.process_time()

    _, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert indeterminate
    assert "https://[2001:db8::a]/Source" in urls
    assert time.process_time() - started < 3.0


def test_many_angle_destinations_are_processed_in_linear_time():
    import time

    prose = " ".join(
        f"<https://[2001:DB8::A]/Source_{index}>" for index in range(3000)
    )
    started = time.process_time()

    indices, urls, indeterminate = extract_cited_indices_and_urls(prose)

    assert not indices
    assert len(urls) == 3000
    assert not indeterminate
    assert time.process_time() - started < 3.0