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.

79.5 KB
Raw Blame
"""
Tests for search_utilities.py — format_links_to_markdown and format_findings
edge cases not covered by the existing test_search_utilities.py and
test_search_utilities_extended.py files.

Tests cover:
- format_links_to_markdown: None/empty URL handling, special characters, index
  aggregation for 'link' key fallback
- format_findings: iteration numbering, question_by_iteration edge cases,
  findings with search results that have errors, malformed finding structures
"""


# ---------------------------------------------------------------------------
# format_links_to_markdown — additional edge cases
# ---------------------------------------------------------------------------


class TestFormatLinksToMarkdownNoneUrls:
    """Tests for format_links_to_markdown with None/empty URLs."""

    def test_none_url_skipped(self):
        """Links with None url are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Valid", "url": "https://valid.com", "index": "1"},
            {"title": "None URL", "url": None, "index": "2"},
        ]

        result = format_links_to_markdown(links)

        assert "https://valid.com" in result
        assert "None URL" not in result

    def test_empty_string_url_skipped(self):
        """Links with empty string url are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Empty URL", "url": "", "index": "1"},
            {"title": "Good", "url": "https://good.com", "index": "2"},
        ]

        result = format_links_to_markdown(links)

        assert "https://good.com" in result
        assert "Empty URL" not in result

    def test_no_url_or_link_key_skipped(self):
        """Links with neither 'url' nor 'link' key are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "No URL key", "index": "1"},
        ]

        result = format_links_to_markdown(links)

        assert "No URL key" not in result


class TestFormatLinksToMarkdownSpecialChars:
    """Tests for format_links_to_markdown with special characters."""

    def test_url_with_query_params(self):
        """URLs with query parameters are preserved."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Search",
                "url": "https://example.com/search?q=hello+world&lang=en",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "https://example.com/search?q=hello+world&lang=en" in result

    def test_url_with_fragment(self):
        """URLs with fragments are stripped in the Sources display.

        Fragments are anchor-only and have no effect on click-through
        landing pages; the canonical form drops them for a cleaner UI.
        """
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Section",
                "url": "https://example.com/page#section-2",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "URL: https://example.com/page" in result
        assert "#section-2" not in result

    def test_title_with_special_markdown_chars(self):
        """Titles with markdown-special chars are included."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Results [2024] — *Important*",
                "url": "https://example.com",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "Results [2024]" in result


class TestFormatLinksToMarkdownIndexAggregation:
    """Tests for index aggregation in format_links_to_markdown."""

    def test_link_key_fallback_aggregates_indices(self):
        """'link' key fallback still aggregates indices for same URL."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Same", "link": "https://same.com", "index": "1"},
            {"title": "Same", "link": "https://same.com", "index": "4"},
        ]

        result = format_links_to_markdown(links)
        assert result.count("https://same.com") == 1
        assert "1" in result
        assert "4" in result

    def test_mixed_url_and_link_keys(self):
        """Handles mix of 'url' and 'link' keys for same actual URL."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "First", "url": "https://a.com", "index": "1"},
            {"title": "Second", "link": "https://b.com", "index": "2"},
        ]

        result = format_links_to_markdown(links)
        assert "https://a.com" in result
        assert "https://b.com" in result


# ---------------------------------------------------------------------------
# format_findings — additional edge cases
# ---------------------------------------------------------------------------


class TestFormatFindingsIterationNumbering:
    """Tests for iteration numbering in format_findings."""

    def test_single_iteration_with_questions(self):
        """Single iteration with questions formatted correctly."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {1: ["Q1", "Q2", "Q3"]}

        result = format_findings([], "Summary content", questions)

        assert "Iteration 1" in result
        assert "1. Q1" in result
        assert "2. Q2" in result
        assert "3. Q3" in result

    def test_non_sequential_iteration_numbers(self):
        """Non-sequential iteration numbers (e.g., 1, 3, 5) handled correctly."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {
            1: ["First iter Q"],
            3: ["Third iter Q"],
            5: ["Fifth iter Q"],
        }

        result = format_findings([], "Summary", questions)

        assert "Iteration 1" in result
        assert "Iteration 3" in result
        assert "Iteration 5" in result
        assert "Iteration 2" not in result

    def test_zero_iteration_key(self):
        """Iteration 0 (used by IterDRAG) is formatted."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {0: ["Sub-query A", "Sub-query B"]}

        result = format_findings([], "Summary", questions)

        assert "Iteration 0" in result
        assert "Sub-query A" in result


class TestFormatFindingsMalformedStructures:
    """Tests for malformed finding structures."""

    def test_finding_missing_content_key(self):
        """Finding without 'content' key uses default."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [{"phase": "Phase 1", "search_results": []}]

        result = format_findings(findings, "Summary", {})

        assert "No content available" in result

    def test_finding_missing_phase_key(self):
        """Finding without 'phase' key uses default."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [{"content": "Some content", "search_results": []}]

        result = format_findings(findings, "Summary", {})

        assert "Unknown Phase" in result
        assert "Some content" in result

    def test_finding_with_only_question_field(self):
        """Finding with only 'question' field displays it."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Custom",
                "content": "Answer text",
                "question": "How does X work?",
                "search_results": [],
            }
        ]

        result = format_findings(findings, "Summary", {})

        assert "How does X work?" in result
        assert "SEARCH QUESTION" in result

    def test_empty_questions_dict_shows_warning_section(self):
        """Empty questions dict (no iterations) omits section."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        result = format_findings([], "Summary", {})

        assert "SEARCH QUESTIONS BY ITERATION" not in result

    def test_findings_with_search_results_error(self):
        """Findings with non-list search_results handled gracefully."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Search",
                "content": "Content",
                "search_results": None,  # None instead of list
            }
        ]

        # Should not crash
        result = format_findings(findings, "Summary", {})
        assert "Content" in result


# ---------------------------------------------------------------------------
# _format_quality_tag — journal quality tags in source lists
# ---------------------------------------------------------------------------


class TestFormatQualityTag:
    """Test the _format_quality_tag helper for source list quality indicators."""

    def test_none_returns_empty(self):
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert _format_quality_tag(None) == ""

    def test_elite_tier(self):
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert "Q1" in _format_quality_tag(10)
        assert "Q1" in _format_quality_tag(9)

    def test_strong_tier(self):
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert "Q1" in _format_quality_tag(7)
        assert "Q1" in _format_quality_tag(8)

    def test_moderate_tier(self):
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert "Q2" in _format_quality_tag(5)
        assert "Q2" in _format_quality_tag(6)

    def test_default_unknown_tier(self):
        """Score 4 (JOURNAL_QUALITY_DEFAULT) is rendered as [Unranked ★]."""
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert _format_quality_tag(4) == " [Unranked ★]"

    def test_predatory_tier(self):
        """Score 1 (predatory — normally auto-removed) falls back to Q4."""
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert "Q4" in _format_quality_tag(1)

    def test_score_boundary_5_is_q2_not_unranked(self):
        """Score 5 crosses the Q2 threshold; must not fall through to Unranked."""
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert _format_quality_tag(5) == " [Q2 ★★★]"

    def test_score_3_renders_unranked_not_q4(self):
        """Score 3 is the filter's 'no scoring data' fallback — rendered as
        Unranked rather than Q4 so it reads as "we don't know this venue"
        rather than "we know it's low-quality"."""
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        assert _format_quality_tag(3) == " [Unranked ★]"

    def test_preprint_sentinel(self):
        """QUALITY_PREPRINT marks results with no journal_ref at all."""
        from local_deep_research.utilities.search_utilities import (
            QUALITY_PREPRINT,
            _format_quality_tag,
        )

        tag = _format_quality_tag(QUALITY_PREPRINT)
        assert "preprint" in tag
        assert "not in journal catalog" in tag

    def test_pending_sentinel(self):
        from local_deep_research.utilities.search_utilities import (
            QUALITY_PENDING,
            _format_quality_tag,
        )

        assert "downloading" in _format_quality_tag(QUALITY_PENDING)

    def test_out_of_range_value_surfaces_raw(self):
        """Out-of-set inputs must not silently bucket into Q4 — the
        catch-all renders ``[quality=<value>]`` so bad scoring logic is
        visible in the output.
        """
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        tag = _format_quality_tag(99)
        assert "quality=" in tag
        assert "99" in tag

    def test_every_valid_score_has_explicit_branch(self):
        """Every value in VALID_QUALITY_SCORES must map to a real tier
        tag — none should fall through to the debug catch-all.
        """
        from local_deep_research.constants import VALID_QUALITY_SCORES
        from local_deep_research.utilities.search_utilities import (
            _format_quality_tag,
        )

        for score in VALID_QUALITY_SCORES:
            tag = _format_quality_tag(score)
            assert "quality=" not in tag, (
                f"score {score} fell through to the debug catch-all: {tag!r}"
            )


# ---------------------------------------------------------------------------
# format_links_to_markdown — dedup via canonical URL key
# ---------------------------------------------------------------------------


class TestFormatLinksDedupCanonical:
    """Canonicalized dedup: slight URL variants collapse to one Sources entry."""

    def setup_method(self):
        # canonical_url_key is cached across tests; clear so each test starts
        # from a clean slate (otherwise earlier test inputs can pollute the
        # expected-output behavior if we ever depend on cache-miss timing).
        from local_deep_research.utilities.url_utils import canonical_url_key

        canonical_url_key.cache_clear()

    def test_dedup_trailing_slash(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Page", "url": "https://x.com/p", "index": "1"},
            {"title": "Page", "url": "https://x.com/p/", "index": "2"},
        ]
        result = format_links_to_markdown(links)
        # One entry, two indices.
        assert result.count("URL: https://x.com/p") == 1
        assert "[1, 2]" in result

    def test_dedup_tracking_params(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Vox",
                "url": "https://www.vox.com/a?utm_source=g&utm_medium=s",
                "index": "1",
            },
            {"title": "Vox", "url": "https://www.vox.com/a", "index": "2"},
        ]
        result = format_links_to_markdown(links)
        assert "[1, 2]" in result
        # Canonical (clean) URL is displayed — tracking params stripped.
        assert "utm_source" not in result
        assert "utm_medium" not in result
        assert "URL: https://www.vox.com/a" in result

    def test_dedup_fragment_variants(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Wiki", "url": "https://x.com/p", "index": "1"},
            {
                "title": "Wiki",
                "url": "https://x.com/p#section",
                "index": "2",
            },
        ]
        result = format_links_to_markdown(links)
        assert "[1, 2]" in result

    def test_dedup_default_port(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Site", "url": "https://x.com/p", "index": "1"},
            {"title": "Site", "url": "https://x.com:443/p", "index": "2"},
        ]
        result = format_links_to_markdown(links)
        assert "[1, 2]" in result

    def test_distinct_urls_stay_distinct(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "A", "url": "https://x.com/a", "index": "1"},
            {"title": "B", "url": "https://x.com/b", "index": "2"},
        ]
        result = format_links_to_markdown(links)
        assert "https://x.com/a" in result
        assert "https://x.com/b" in result
        assert "[1, 2]" not in result

    def test_display_shows_canonical_url_not_raw(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        # Even when the utm-bearing variant comes first, the Sources
        # section shows the canonical (tracking-stripped) URL — cleaner
        # UI, identical click-through behavior.
        links = [
            {
                "title": "Article",
                "url": "https://x.com/a?utm_source=g",
                "index": "1",
            },
            {"title": "Article", "url": "https://x.com/a", "index": "2"},
        ]
        result = format_links_to_markdown(links)
        assert "URL: https://x.com/a\n" in result
        assert "utm_source" not in result

    def test_display_strips_userinfo(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        # Userinfo in a URL would leak credentials if displayed. Canonical
        # form drops it, so the Sources section is always safe.
        links = [
            {
                "title": "Internal",
                "url": "https://user:[email protected]/p",
                "index": "1",
            }
        ]
        result = format_links_to_markdown(links)
        assert "secret" not in result
        assert "user:" not in result
        assert "URL: https://internal.example.com/p" in result

    def test_ref_param_not_stripped(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        # ?ref= on GitHub is a branch selector, not a tracker.
        links = [
            {
                "title": "Repo on main",
                "url": "https://github.com/o/r?ref=main",
                "index": "1",
            },
            {
                "title": "Repo on dev",
                "url": "https://github.com/o/r?ref=dev",
                "index": "2",
            },
        ]
        result = format_links_to_markdown(links)
        # Must remain two separate entries.
        assert "ref=main" in result
        assert "ref=dev" in result
        assert "[1, 2]" not in result


# ---------------------------------------------------------------------------
# format_findings — exact character-by-character output
# ---------------------------------------------------------------------------


class TestFormatFindingsExactOutput:
    """Character-by-character tests of the markdown produced by
    ``format_findings`` for predetermined inputs. Locks in the exact
    layout (indentation, blank lines, ``[1]`` citation markers, the
    80-char ``_`` separator) so a renderer regression — even a single
    extra newline or a moved citation — fails loudly.
    """

    def test_exact_output_minimal_findings(self):
        """One finding, one source, one iteration, one question."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings_list = [
            {
                "phase": "Initial",
                "content": "Qubits enable quantum computing [1].",
                "search_results": [
                    {
                        "title": "Qubit Basics",
                        "link": "https://example.com/q",
                        "index": "1",
                    },
                ],
            },
        ]
        synthesized_content = "Quantum computing uses qubits [1]."
        questions_by_iteration = {1: ["What is a qubit?"]}

        # Expected output is the verbatim string format_findings produces
        # for the inputs above. Anchored to today's exact layout — any
        # change (added blank line, moved citation, renamed section
        # header) will fail this test, which is the point.
        expected = (
            "Quantum computing uses qubits [1].\n"
            "\n"
            "[1] Qubit Basics (source nr: 1)\n"
            "   URL: https://example.com/q\n"
            "\n"
            "\n"
            "\n"
            "\n"
            "## SEARCH QUESTIONS BY ITERATION\n"
            "\n"
            "\n"
            " #### Iteration 1:\n"
            "1. What is a qubit?\n"
            "\n"
            "\n"
            "\n"
            "## DETAILED FINDINGS\n"
            "\n"
            "\n"
            "### Initial\n"
            "\n"
            "\n"
            "\n"
            "\n"
            "Qubits enable quantum computing [1].\n"
            "\n"
            "### SOURCES USED IN THIS SECTION:\n"
            "[1] Qubit Basics (source nr: 1)\n"
            "   URL: https://example.com/q\n"
            "\n"
            "\n"
            "\n"
            "\n" + ("_" * 80) + "\n"
            "\n"
            "## ALL SOURCES:\n"
            "[1] Qubit Basics (source nr: 1)\n"
            "   URL: https://example.com/q\n"
            "\n"
            "\n"
        )

        actual = format_findings(
            findings_list, synthesized_content, questions_by_iteration
        )
        assert actual == expected, (
            f"format_findings output drifted from the locked-in layout.\n"
            f"--- expected ---\n{expected!r}\n"
            f"--- actual ---\n{actual!r}"
        )

    def test_citations_in_synthesized_content_preserved(self):
        """``[1]`` and ``[2]`` markers in the synthesized content reach
        the final output unchanged. Independent narrower assertion to
        catch citation-handling regressions even when the broader exact-
        match test is in flux for unrelated layout reasons."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        synthesized = "First fact [1]. Second fact [2]. Third fact [1]."
        result = format_findings(
            findings_list=[],
            synthesized_content=synthesized,
            questions_by_iteration={},
        )
        # The synthesized content is the very first thing written.
        assert result.startswith(synthesized + "\n\n"), (
            f"Synthesized content (with citations) was rewritten or "
            f"prefixed by format_findings: result starts with "
            f"{result[: len(synthesized) + 20]!r}"
        )
        assert result.count("[1]") == 2
        assert result.count("[2]") == 1

    def test_source_indices_match_citation_markers(self):
        """When a finding's search_results have indices ``"1"`` and
        ``"2"``, format_links_to_markdown emits ``[1]`` and ``[2]`` —
        these should align with the citation markers in the synthesized
        content. Tests the citation→source alignment that makes the
        markdown navigable."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings_list = [
            {
                "phase": "Initial",
                "content": "Body referencing [1] and [2].",
                "search_results": [
                    {
                        "title": "Source One",
                        "link": "https://example.com/one",
                        "index": "1",
                    },
                    {
                        "title": "Source Two",
                        "link": "https://example.com/two",
                        "index": "2",
                    },
                ],
            },
        ]
        result = format_findings(
            findings_list,
            synthesized_content="See [1] and [2].",
            questions_by_iteration={},
        )
        # Both [1] and [2] appear in source listings, attached to the
        # right titles and URLs.
        assert "[1] Source One" in result
        assert "https://example.com/one" in result
        assert "[2] Source Two" in result
        assert "https://example.com/two" in result
        # And the synthesized [1]/[2] citations at the top survive.
        assert result.startswith("See [1] and [2].\n\n")


class TestFormatLinksToMarkdownCollections:
    """Tests for the optional ``Collection:`` line surfaced for
    RAG / library results so the source-tagged citation mode can read
    the collection name back from the rendered sources block."""

    def test_emits_collection_line_when_metadata_present(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Local Paper",
                "url": "/library/document/abc123",
                "index": "1",
                "metadata": {"collection_name": "My Papers"},
            },
        ]
        result = format_links_to_markdown(links)
        assert "URL: /library/document/abc123" in result
        assert "Collection: My Papers" in result

    def test_no_collection_line_when_metadata_absent(self):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        # Plain web hit, no metadata.
        links = [
            {
                "title": "Web Page",
                "url": "https://example.com/page",
                "index": "1",
            },
        ]
        result = format_links_to_markdown(links)
        assert "URL: https://example.com/page" in result
        assert "Collection:" not in result

    def test_no_collection_line_when_metadata_lacks_collection_name(self):
        """metadata may exist for other reasons (engine_name, score, etc.)
        without carrying a collection name. Don't emit the line then."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Web Page",
                "url": "https://example.com/page",
                "index": "1",
                "metadata": {"engine_name": "web", "score": 0.91},
            },
        ]
        result = format_links_to_markdown(links)
        assert "Collection:" not in result

    def test_first_non_empty_collection_wins_per_url(self):
        """Two hits for the same canonical URL — the first source with a
        non-empty collection name sets it; later hits don't overwrite.
        Mirrors how title and journal_quality work."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Doc",
                "url": "/library/document/abc",
                "index": "1",
                "metadata": {"collection_name": "first"},
            },
            {
                "title": "Doc",
                "url": "/library/document/abc",
                "index": "2",
                "metadata": {"collection_name": "second"},
            },
        ]
        result = format_links_to_markdown(links)
        # Single entry (URLs deduped), carries the first collection.
        assert "Collection: first" in result
        assert "Collection: second" not in result

    def test_sentinel_stripped_from_title(self):
        """LDR_APPENDED_SOURCES_SENTINEL in link title is stripped."""
        from local_deep_research.text_optimization.citation_formatter import (
            LDR_APPENDED_SOURCES_SENTINEL,
        )
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": f"Title with {LDR_APPENDED_SOURCES_SENTINEL} sentinel",
                "url": "https://example.com",
                "index": "1",
            }
        ]
        result = format_links_to_markdown(links)
        assert LDR_APPENDED_SOURCES_SENTINEL not in result
        assert "Title with  sentinel" in result

    def test_sentinel_stripped_from_collection_name(self):
        """A collection name cannot shift the appended-sources boundary."""
        from local_deep_research.text_optimization.citation_formatter import (
            LDR_APPENDED_SOURCES_SENTINEL,
            find_sources_section,
        )
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Local Paper",
                "url": "/library/document/abc123",
                "index": "1",
                "metadata": {
                    "collection_name": (
                        f"My Papers\n{LDR_APPENDED_SOURCES_SENTINEL}\nArchive"
                    )
                },
            }
        ]
        sources = format_links_to_markdown(links)
        content = (
            "Answer\n\n"
            f"{LDR_APPENDED_SOURCES_SENTINEL}\n\n"
            f"## Sources\n\n{sources}"
        )

        assert LDR_APPENDED_SOURCES_SENTINEL not in sources
        # The name is also flattened onto one line: newlines in a rendered
        # Sources field are what let a crafted value forge an extra entry,
        # so they are replaced with spaces rather than preserved. That is
        # strictly stronger than this test's own premise — the name can no
        # longer introduce line structure at all.
        assert "Collection: My Papers  Archive" in sources
        assert "\n" not in sources.split("Collection: ", 1)[1].split("\n")[0]
        assert find_sources_section(content) == (
            content.index(LDR_APPENDED_SOURCES_SENTINEL),
            True,
        )


def test_format_links_preserves_distinct_library_chunks_as_separate_entries():
    """Distinct cited chunks of a library document render as separate bibliography
    entries, each with its own citation index and chunk URL. Duplicate citations
    of the SAME chunk merge under that chunk's entry."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    links = [
        {
            "title": f"My Paper (chunk {i})",
            "url": f"/library/document/doc1/chunks#chunk-{i}",
            "index": i + 1,
        }
        for i in range(8)
    ]
    # Repeat citation of chunk 0
    links.append(
        {
            "title": "My Paper (chunk 0)",
            "url": "/library/document/doc1/chunks#chunk-0",
            "index": 10,
        }
    )
    links.append(
        {
            "title": "Other Paper",
            "url": "/library/document/doc2/chunks#chunk-0",
            "index": 9,
        }
    )

    out = format_links_to_markdown(links)

    # doc1's 8 distinct chunks each get their own entry; chunk 0 merges indices 1 and 10
    assert "[1, 10]" in out
    assert "[2]" in out
    assert "[8]" in out
    assert "[9]" in out
    assert out.count("/library/document/doc1/chunks#chunk-") == 8
    assert "/library/document/doc2/chunks#chunk-0" in out


def test_format_links_keeps_pdf_and_chunk_view_distinct_and_merges_same_chunk():
    """A document cited with a chunk anchor and an unanchored whole-document /pdf
    are distinct citation entries. Citations to the same chunk merge together."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    out = format_links_to_markdown(
        [
            {
                "title": "My Paper",
                "url": "/library/document/doc1/chunks#chunk-0",
                "index": 1,
            },
            {
                "title": "My Paper",
                "url": "/library/document/doc1/pdf",
                "index": 2,
            },
            {
                "title": "My Paper",
                "url": "/library/document/doc1/chunks#chunk-0",
                "index": 3,
            },
        ]
    )

    assert "[1, 3]" in out
    assert "/library/document/doc1/chunks#chunk-0" in out
    assert "[2]" in out
    assert "/library/document/doc1/pdf" in out


def test_format_links_merges_absolute_library_alias_with_relative_route():
    """A chunk cited once by the RAG engine (relative route) and once via
    fetch_content on the absolute alias is ONE source and merges indices."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    out = format_links_to_markdown(
        [
            {
                "title": "My Paper",
                "url": "/library/document/abc123/chunks#chunk-7",
                "index": 1,
            },
            {
                "title": "My Paper",
                "url": "https://library.document/abc123/chunks#chunk-7",
                "index": 2,
            },
        ]
    )

    assert "[1, 2]" in out
    assert out.count("My Paper") == 1
    assert "/library/document/abc123/chunks#chunk-7" in out
    assert "https://library.document" not in out


def test_format_links_merges_unanchored_views_and_keeps_chunks_distinct():
    """Unanchored views (/pdf and base) share one canonical key and merge,
    while anchored chunks remain distinct."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    out = format_links_to_markdown(
        [
            {
                "title": "My Paper",
                "url": "/library/document/doc1/pdf",
                "index": 1,
            },
            {
                "title": "My Paper",
                "url": "/library/document/doc1",
                "index": 2,
            },
            {
                "title": "My Paper",
                "url": "/library/document/doc1/chunks#chunk-4",
                "index": 3,
            },
        ]
    )

    assert "[1, 2]" in out
    assert "[3]" in out
    assert "/library/document/doc1/chunks#chunk-4" in out


def test_format_links_survives_non_ascii_digit_index():
    """``\"²\".isdigit()`` is True while ``int(\"²\")`` raises, which took down
    the whole bibliography. The repo's Hypothesis fuzzer only finds this
    from a cached example database, so pin it deterministically."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    # Only superscripts qualify: int("١٢٣") == 123 (Arabic-Indic digits are
    # decimal), so that input never hit the old int() failure.
    for index in ("²", "¹"):
        out = format_links_to_markdown(
            [{"title": "t", "url": "https://x.test/p", "index": index}]
        )
        assert index in out


def test_format_links_prefers_the_chunk_anchor_over_any_other_fragment():
    """The display preference tested for ``"#" in display`` rather than
    ``"#chunk-" in display``, so an empty or unrelated fragment counted as
    \"anchored\", won, and permanently locked out the real chunk anchor."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    for decoy in ("/library/document/9#", "/library/document/9#section-intro"):
        canonical_url_key.cache_clear()
        out = format_links_to_markdown(
            [
                {"title": "Doc", "url": decoy, "index": 1},
                {
                    "title": "Doc",
                    "url": "/library/document/9/chunks#chunk-4",
                    "index": 2,
                },
            ]
        )
        assert "[1]" in out, decoy
        assert "[2]" in out, decoy
        assert "/library/document/9/chunks#chunk-4" in out, decoy


def test_format_links_merges_lib_abbreviation_and_encoded_id():
    """``/lib/document/<id>`` and ``/library/document/<id>`` are the same
    document to the fetch resolver, and ``a%2Db`` resolves to ``a-b``."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    out = format_links_to_markdown(
        [
            {"title": "Doc", "url": "/library/document/a-b/pdf", "index": 1},
            {"title": "Doc", "url": "/lib/document/a-b", "index": 2},
            {
                "title": "Doc",
                "url": "/library/document/a%2Db/chunks#chunk-2",
                "index": 3,
            },
            {
                "title": "Doc",
                "url": "/lib/document/a-b/chunks#chunk-2",
                "index": 4,
            },
        ]
    )

    assert "[1, 2]" in out
    assert "[3, 4]" in out
    assert "/library/document/a-b/chunks#chunk-2" in out
    assert "/lib/document" not in out


def test_format_links_does_not_let_a_traversal_url_take_over_a_document():
    """The crafted entry used to merge into the real document's entry,
    inherit its title and citation indices, and REPLACE its link — the
    anchor-preference rule actively favoured it over the real ``/pdf``
    view."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    out = format_links_to_markdown(
        [
            {
                "title": "Real Paper",
                "url": "/library/document/7/pdf",
                "index": 1,
            },
            {
                "title": "Forged",
                "url": "/library/document/7/../../../admin/wipe#chunk-1",
                "index": 2,
            },
        ]
    )

    assert "[1, 2]" not in out
    assert "   URL: /library/document/7/pdf\n" in out
    assert "Real Paper" in out
    # The crafted URL keeps its own separate entry rather than taking over
    # the real document's line.
    assert out.index("Real Paper") < out.index("Forged")


def test_format_links_cannot_be_line_injected_via_a_library_route():
    """End-to-end guard on the library-route entry point.

    ``library_display_url`` refuses a control character and the canonical
    key it falls back to keeps the raw string, so the last line of defence
    is ``_sanitize_sources_field`` flattening the payload where the block
    is rendered. This exercises that through a library route rather than
    through a title."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    canonical_url_key.cache_clear()
    forged = (
        "/library/document/doc1/chunks#chunk-0\n"
        "   Collection: admin-secrets\n"
        "[99] Forged Source (source nr: 99)\n"
        "   URL: https://evil.example/pwn"
    )
    out = format_links_to_markdown(
        [{"title": "Doc", "url": forged, "index": 1}]
    )

    lines = out.splitlines()
    assert "   Collection: admin-secrets" not in lines
    assert not any(line.startswith("[99]") for line in lines)
    assert "   URL: https://evil.example/pwn" not in lines


def test_non_string_url_is_skipped_not_stringified():
    """Rendering ``str(raw)`` would put a Python repr in front of the user
    as a clickable URL, and would disagree with ``_citation_dedup_key``,
    which refuses a non-str link outright."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    out = format_links_to_markdown(
        [{"url": ["/library/document/doc1"], "title": "T", "index": 1}]
    )

    assert "[" not in out.replace("\n", "").strip() or "doc1" not in out


def test_non_string_urls_do_not_merge_distinct_sources():
    """``True`` and ``"True"`` must not collapse onto one citation."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    out = format_links_to_markdown(
        [
            {"url": True, "title": "First", "index": 1},
            {"url": "True", "title": "Other", "index": 2},
        ]
    )

    assert "First" not in out
    assert "Other" in out


def test_non_string_title_and_metadata_do_not_crash():
    """Both sit beside the url in the same loops and raised on the same
    inputs — taking the whole Sources block with them."""
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    out = format_links_to_markdown(
        [
            {
                "url": "https://a.test/p",
                "title": ["Nature paper"],
                "metadata": "not-a-dict",
                "index": 1,
            }
        ]
    )

    assert "https://a.test/p" in out


def test_recorded_anchor_for_ANOTHER_document_is_not_rendered():
    """``_owned_chunk_display``'s ownership comparison is load-bearing.

    Its docstring calls the check "the PRIMARY control, not defence in
    depth": on every strategy except LangGraph, ``CHUNK_DISPLAY_KEY`` is
    producer-supplied by construction, and the value is PREFERRED over the
    entry's own url. So a well-formed anchor naming a DIFFERENT document
    would render — and persist — under this citation.

    The writer-side twin (``select_source_url``) has six tests; this
    reader-side sibling had none, which is the same one-instance-at-a-time
    gap the code keeps exhibiting. Removing the comparison must fail here.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import (
        CHUNK_DISPLAY_KEY,
        canonical_url_key,
    )

    canonical_url_key.cache_clear()
    foreign = "/library/document/BBBB/chunks#chunk-3"
    out = format_links_to_markdown(
        [
            {
                "title": "Doc A",
                "url": "/library/document/AAAA",
                CHUNK_DISPLAY_KEY: foreign,
                "index": 1,
            }
        ]
    )
    assert "/library/document/BBBB" not in out, out
    assert "#chunk-3" not in out, out
    assert "/library/document/AAAA" in out, out

    # The same key naming THIS document is still honoured — the check must
    # reject foreign anchors, not all recorded ones.
    canonical_url_key.cache_clear()
    own = "/library/document/AAAA/chunks#chunk-3"
    out_own = format_links_to_markdown(
        [
            {
                "title": "Doc A",
                "url": "/library/document/AAAA",
                CHUNK_DISPLAY_KEY: own,
                "index": 1,
            }
        ]
    )
    assert own in out_own, out_own


class TestCountDistinctSources:
    """``count_distinct_sources`` — the "how many sources" number.

    ``len(all_links_of_system)`` counts occurrences: the LangGraph
    collector stores one entry per distinct ``(url, snippet)`` pair since
    #5894, and ``source_based`` / ``focused_iteration`` /
    ``topic_organization`` extend the list with raw engine dicts and no
    URL dedup at all.
    """

    def test_counts_the_lines_the_bibliography_renders(self):
        from local_deep_research.utilities.search_utilities import (
            count_distinct_sources,
            format_links_to_markdown,
        )

        links = [
            {"title": "P", "link": "https://ex.test/p", "index": "1"},
            {"title": "P", "link": "https://ex.test/p", "index": "2"},
            {"title": "O", "link": "https://o.test/q", "index": "3"},
        ]

        assert count_distinct_sources(links) == 2
        # The two must agree — that is the whole point of sharing the
        # grouping expression.
        assert format_links_to_markdown(links).count("URL:") == 2

    def test_groups_by_the_canonical_key_like_the_renderer(self):
        from local_deep_research.utilities.search_utilities import (
            count_distinct_sources,
        )

        links = [
            {"link": "https://ex.test/p", "index": "1"},
            {"link": "https://EX.test/p/?utm_source=x", "index": "2"},
            {"link": "https://user:[email protected]/p#frag", "index": "3"},
        ]
        assert count_distinct_sources(links) == 1
        # Control: a genuinely different URL is a second source.
        assert (
            count_distinct_sources(links + [{"link": "https://o.test/q"}]) == 2
        )

    def test_skips_entries_the_renderer_also_skips(self):
        from local_deep_research.utilities.search_utilities import (
            count_distinct_sources,
        )

        assert count_distinct_sources([]) == 0
        assert count_distinct_sources(None) == 0
        assert (
            count_distinct_sources(
                [
                    "not a dict",
                    {"link": None},
                    {"link": ["a"]},
                    {"title": "no url at all"},
                    {"link": ""},
                ]
            )
            == 0
        )

    def test_renderer_skips_a_non_dict_entry_too(self):
        from local_deep_research.utilities.search_utilities import (
            count_distinct_sources,
            format_links_to_markdown,
        )

        links = [
            {"title": "P", "link": "https://ex.test/p", "index": "1"},
            "not a dict",
            None,
            {"title": "O", "link": "https://o.test/q", "index": "2"},
        ]

        assert count_distinct_sources(links) == 2
        assert format_links_to_markdown(links).count("URL:") == 2


class TestSourceUrlField:
    """Which field identifies a source, for grouping.

    ``SearchResultsCollector`` keys every citation on the entry's
    ``link``. The bibliography used to group on ``url or link`` —
    ``url`` first — and while at most one entry per source existed the
    disagreement could not show. It can now, so both read
    ``source_url_field``.
    """

    def test_link_wins_over_a_divergent_url(self):
        from local_deep_research.utilities.search_utilities import (
            source_url_field,
        )

        assert (
            source_url_field(
                {"link": "https://a.test/x", "url": "https://b.test/y"}
            )
            == "https://a.test/x"
        )
        # ...and ``url`` still serves the raw engine dicts that set only it.
        assert source_url_field({"url": "https://b.test/y"}) == (
            "https://b.test/y"
        )
        assert source_url_field({"link": "", "url": "https://b.test/y"}) == (
            "https://b.test/y"
        )
        assert source_url_field({}) == ""

    def test_two_entries_of_one_source_render_one_line_despite_a_divergent_url(
        self,
    ):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "P", "link": "https://ex.test/p", "index": "1"},
            {
                "title": "P",
                "link": "https://ex.test/p",
                "url": "https://elsewhere.test/mirror",
                "index": "2",
            },
        ]
        rendered = format_links_to_markdown(links)
        assert rendered.count("URL:") == 1, rendered
        assert "[1, 2]" in rendered
        assert "elsewhere.test" not in rendered


class TestFormatFindingsUncitedFilter:
    """format_findings filters global dumps, not per-section lists (#5379)."""

    def _findings(self):
        return [
            {
                "phase": "Initial",
                "content": "Section content.",
                "search_results": [
                    {
                        "title": "Cited",
                        "link": "https://cited.test/a",
                        "index": "1",
                    },
                    {
                        "title": "Uncited",
                        "link": "https://uncited.test/b",
                        "index": "2",
                    },
                ],
            },
        ]

    def test_global_dumps_filtered_per_section_kept(self):
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        out = format_findings(
            self._findings(),
            "Synthesis cites the first source [1].",
            {1: ["Q?"]},
        )

        head, _, tail = out.partition("## DETAILED FINDINGS")
        assert "https://cited.test/a" in head
        assert "https://uncited.test/b" not in head
        assert "## ALL SOURCES:" in out
        assert "https://uncited.test/b" not in tail.split("## ALL SOURCES:")[1]
        # Per-section list is scoped to its own results: unfiltered.
        assert "SOURCES USED IN THIS SECTION" in out
        assert out.count("https://uncited.test/b") == 1

    def test_disabled_mode_keeps_all(self):
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        out = format_findings(
            self._findings(),
            "Synthesis cites the first source [1].",
            {1: ["Q?"]},
            uncited_mode="disabled",
        )

        assert out.count("https://uncited.test/b") == 3

    def test_strict_mode_drops_uncited_global_sources(self):
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        out = format_findings(
            self._findings(),
            "No citations here.",
            {1: ["Q?"]},
            uncited_mode="strict",
        )

        head, _, tail = out.partition("## DETAILED FINDINGS")
        assert "https://cited.test/a" not in head
        assert "https://uncited.test/b" not in head
        assert "https://cited.test/a" not in tail.split("## ALL SOURCES:")[1]
        assert "https://uncited.test/b" not in tail.split("## ALL SOURCES:")[1]
        assert out.count("https://uncited.test/b") == 1


class TestFilterCitedLinks:
    """Uncited-source filter engine (#5379, PR1)."""

    @staticmethod
    def _filter(links, prose, mode="fallback"):
        from local_deep_research.utilities.search_utilities import (
            filter_cited_links,
        )

        return filter_cited_links(links, prose, mode=mode)

    @staticmethod
    def _format(links, prose=None, uncited_mode="fallback"):
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        return format_links_to_markdown(
            links, prose=prose, uncited_mode=uncited_mode
        )

    @staticmethod
    def _extract(prose):
        from local_deep_research.utilities.search_utilities import (
            extract_cited_indices_and_urls,
        )

        return extract_cited_indices_and_urls(prose)

    def _links(self):
        return [
            {"title": "A", "url": "https://a.test/x", "index": "1"},
            {"title": "B", "url": "https://b.test/y", "index": "2"},
            {"title": "C", "url": "https://c.test/z", "index": "3"},
        ]

    def test_bracket_and_comma_list(self):
        prose = "Claims [1] and more [1, 3]."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "3"]

    def test_range_expansion(self):
        prose = "Summary [1-3]."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "2", "3"]

    def test_en_dash_range(self):
        prose = "Summary [1–3]."
        kept = self._filter(self._links(), prose)
        assert len(kept) == 3

    def test_double_and_fullwidth_brackets(self):
        prose = "See [[1]] and 【2】."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "2"]

    def test_tagged_citation_accepted(self):
        prose = "Per [arxiv.org-2] the result holds."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["2"]

    def test_footnote_citation_accepted(self):
        prose = "See [^1] and [3]."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "3"]

    def test_uppercase_tag_rejected_with_fallback(self):
        # Bare [GPT-4] must not mark index 4 as cited; nothing else
        # cited either, so fallback keeps everything (never wipe out).
        links = self._links()
        kept = self._filter(links, "Model [GPT-4] is great.")
        assert kept == links

    def test_uppercase_tag_ignored_alongside_real_citation(self):
        prose = "Model [GPT-4] is great, see [1]."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1"]

    def test_source_phrase(self):
        prose = "Sources 1 and 2 agree."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "2"]

    def test_fenced_code_ignored(self):
        prose = "```\ndata[3]\n```\nSee [1]."
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1"]

    def test_dangling_fence_marks_parse_indeterminate(self):
        prose = "See [1].\n```\nstray [2] between\n```\nblock\n```\nafter [3]."
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is True
        assert self._filter(self._links(), prose) == self._links()

    def test_wikipedia_paren_url(self):
        links = [
            {
                "title": "Python",
                "url": "https://en.wikipedia.org/wiki/Python_(programming_language)",
                "index": "5",
            },
            {"title": "Other", "url": "https://o.test/q", "index": "6"},
        ]
        prose = (
            "See https://en.wikipedia.org/wiki/Python_(programming_language)"
            " for details."
        )
        kept = self._filter(links, prose)
        assert [link["index"] for link in kept] == ["5"]

    def test_external_paren_peeling(self):
        links = [
            {"title": "P", "url": "https://ex.test/page", "index": "1"},
            {"title": "Q", "url": "https://o.test/q", "index": "2"},
        ]
        kept = self._filter(links, "(https://ex.test/page).")
        assert [link["index"] for link in kept] == ["1"]

    def test_markdown_relative_library_url_with_mixed_citations(self):
        links = [
            {
                "title": "One",
                "url": "/library/document/d1",
                "index": "1",
            },
            {
                "title": "Two",
                "url": "/library/document/d2",
                "index": "2",
            },
            {"title": "Three", "url": "https://c.test/z", "index": "3"},
        ]
        prose = "See [[doc-two-1]](/library/document/d2) and [3]."
        kept = self._filter(links, prose)
        assert [link["index"] for link in kept] == ["1", "2", "3"]

    def test_url_peeling_handles_many_trailing_parentheses(self):
        import time

        from local_deep_research.utilities.search_utilities import (
            _peel_url_candidate,
        )

        raw = "https://e.test/" + ")" * 100_000
        started = time.process_time()
        peeled = _peel_url_candidate(raw)
        elapsed = time.process_time() - started
        assert peeled == "https://e.test/"
        assert elapsed < 1.0, f"peeling took {elapsed:.3f}s CPU"

    def test_balanced_paren_url_with_sentence_period_mixed_citations(self):
        """Balanced ")" before sentence "." must survive when another index matches.

        Reproduced from review: prose citing ``[1]`` plus
        ``https://en.wikipedia.org/wiki/Python_(programming_language).``
        dropped source 2 (only ``[1]`` kept); the same URL without the
        final period retained it. Sentence punctuation is peeled before
        balancing so the balanced paren is preserved.
        """
        links = [
            {"title": "A", "url": "https://a.test/x", "index": "1"},
            {
                "title": "Python",
                "url": "https://en.wikipedia.org/wiki/Python_(programming_language)",
                "index": "2",
            },
        ]
        prose = (
            "See [1] and "
            "https://en.wikipedia.org/wiki/Python_(programming_language)."
            " for details."
        )
        kept = self._filter(links, prose)
        assert [link["index"] for link in kept] == ["1", "2"]
        # Control: without the sentence-final period both were already kept.
        prose_no_period = (
            "See [1] and "
            "https://en.wikipedia.org/wiki/Python_(programming_language)"
            " for details."
        )
        kept_no_period = self._filter(links, prose_no_period)
        assert [link["index"] for link in kept_no_period] == ["1", "2"]

    def test_source_phrase_range_matches_comma_list(self):
        """``Sources 1-3`` must retain 1, 2 and 3 like ``Sources 1, 2 and 3``.

        The old phrase regex captured only the ``1`` prefix of ``1-3``,
        so with indices 1-3 available the filter kept just source 1 and
        — having matched something — never fell back to restore 2 and 3.
        """
        range_kept = self._filter(self._links(), "Sources 1-3 agree.")
        list_kept = self._filter(self._links(), "Sources 1, 2 and 3 agree.")
        assert [link["index"] for link in range_kept] == ["1", "2", "3"]
        assert [link["index"] for link in list_kept] == ["1", "2", "3"]

    def test_uppercase_scheme(self):
        links = [{"title": "P", "url": "https://ex.test/page", "index": "1"}]
        kept = self._filter(links, "See HTTPS://ex.test/page.")
        assert [link["index"] for link in kept] == ["1"]

    def test_cjk_trailing_punct(self):
        links = [{"title": "P", "url": "https://ex.test/page", "index": "1"}]
        kept = self._filter(links, "See https://ex.test/page。")
        assert [link["index"] for link in kept] == ["1"]

    def test_empty_parse_falls_back(self):
        links = self._links()
        assert self._filter(links, "No citations here.") == links
        assert self._filter(links, "") == links
        assert self._filter(links, None) == links

    def test_no_match_falls_back(self):
        links = self._links()
        assert self._filter(links, "See [99].") == links

    def test_strict_mode_drops_uncited(self):
        kept = self._filter(self._links(), "See [99].", mode="strict")
        assert kept == []
        kept = self._filter(self._links(), "See [1].", mode="strict")
        assert [link["index"] for link in kept] == ["1"]

    def test_strict_mode_falls_back_on_indeterminate_parse(self):
        links = self._links()
        prose = "See [1-1002] and [2]."
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is True
        assert self._filter(links, prose, mode="strict") == links

    def test_disabled_mode_keeps_all(self):
        links = self._links()
        assert self._filter(links, "See [1].", mode="disabled") == links

    def test_overcap_range_falls_back(self):
        links = self._links()
        assert self._filter(links, "See [1-1002].") == links

    def test_malformed_numeric_bracket_marks_parse_indeterminate(self):
        links = self._links()
        prose = "[1,abc,3] and [3]."
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is True
        assert self._filter(links, prose) == links

    def test_huge_endpoint_digits_falls_back(self):
        links = self._links()
        assert self._filter(links, "See [1-99999999999999999999].") == links

    def test_format_links_prose_filters(self):
        rendered = self._format(self._links(), prose="See [1].")
        assert "https://a.test/x" in rendered
        assert "https://b.test/y" not in rendered
        assert "https://c.test/z" not in rendered

    def test_format_links_no_prose_keeps_all(self):
        rendered = self._format(self._links())
        assert rendered.count("URL:") == 3

    def test_extract_indeterminate_flag(self):
        _, _, indeterminate = self._extract("See [1-1002].")
        assert indeterminate is True
        _, _, indeterminate = self._extract("See [1].")
        assert indeterminate is False

    def test_prose_singletons_do_not_force_fallback(self):
        """Ordinary prose ([2nd]/[1st]/[5x]/[100m]/[50%]) is ignored.

        Regression for review: each shape flipped the parser to
        indeterminate and returned the full bibliography even in strict
        mode, defeating the cited-only contract. Mixed with a real [1],
        only source 1 is kept in both modes.
        """
        for token in ["[2nd]", "[1st]", "[5x]", "[100m]", "[50%]"]:
            _, _, indeterminate = self._extract(token)
            assert indeterminate is False, token
        for prose in (
            "See [1] and [2nd].",
            "The 2nd trial [2nd] results, see [1].",
            "[50%] off, see [1].",
        ):
            _, _, indeterminate = self._extract(prose)
            assert indeterminate is False, prose
            assert [
                link["index"] for link in self._filter(self._links(), prose)
            ] == ["1"]
            assert [
                link["index"]
                for link in self._filter(self._links(), prose, mode="strict")
            ] == ["1"]

    def test_malformed_list_still_marks_indeterminate(self):
        """Genuinely ambiguous lists keep the safety-net fallback."""
        for prose in ["[1,abc,3]", "[1-abc]", "[1 and 2]"]:
            _, _, indeterminate = self._extract(prose)
            assert indeterminate is True, prose
            assert self._filter(self._links(), prose) == self._links()

    def test_hostile_tagged_huge_index_does_not_force_fallback(self):
        """A single [a-999…] token must not wipe strict-mode filtering."""
        _, _, indeterminate = self._extract("[a-9999999999999999999]")
        assert indeterminate is False
        prose = "[a-9999999999999999999] and [1]."
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        assert [
            link["index"]
            for link in self._filter(self._links(), prose, mode="strict")
        ] == ["1"]

    def test_bare_library_path_retained_like_markdown_destination(self):
        """Bare /library/document/… mentions retain their source."""
        links = [
            {"title": "One", "url": "/library/document/d1", "index": "1"},
            {"title": "Two", "url": "/library/document/d2", "index": "2"},
            {"title": "Three", "url": "https://c.test/z", "index": "3"},
        ]
        for prose in (
            "See /library/document/d2 and [3].",
            "See /library/document/d2. and [3].",
            "See (/library/document/d2) and [3].",
            "See /lib/document/d2 and [3].",
        ):
            for mode in ("fallback", "strict"):
                kept = self._filter(links, prose, mode=mode)
                assert [link["index"] for link in kept] == ["2", "3"], (
                    prose,
                    mode,
                )

    def test_spaced_brackets_parse(self):
        """[ 1, 2 ] keeps both indices instead of dropping silently."""
        _, _, indeterminate = self._extract("[ 1, 2 ]")
        assert indeterminate is False
        kept = self._filter(self._links(), "[ 1, 2 ]")
        assert [link["index"] for link in kept] == ["1", "2"]
        kept = self._filter(self._links(), "[ 1, 2 ]", mode="strict")
        assert [link["index"] for link in kept] == ["1", "2"]
        kept = self._filter(self._links(), "See [ 1, 2 ] and [3].")
        assert [link["index"] for link in kept] == ["1", "2", "3"]

    def test_tilde_fences_stripped_like_backtick_fences(self):
        """~~~ blocks are stripped; dangling ~~~ marks indeterminate."""
        kept = self._filter(self._links(), "~~~\ncode [1] here\n~~~\nSee [2].")
        assert [link["index"] for link in kept] == ["2"]
        _, _, indeterminate = self._extract("~~~\ndangling [1]\nSee [2].")
        assert indeterminate is True
        assert (
            self._filter(self._links(), "~~~\ndangling [1]\nSee [2].")
            == self._links()
        )

    def test_subref_single_letter_marks_indeterminate(self):
        """``[3a]``/``[4b]``/``[12c]`` stay on the safe side of the gate.

        They are regex-identical to sub-reference citations, so mixing one
        with a real citation falls back to the full bibliography in every
        mode instead of silently keeping only the numeric index.
        Controls: ordinals/units/percentages (``2nd``, ``5x``, ``50%``)
        are still ignored prose.
        """
        for token in ["[3a]", "[4b]", "[12c]", "[3A]", "[5d]"]:
            _, _, indeterminate = self._extract(token)
            assert indeterminate is True, token
        for prose in ("See [3a] and [2].", "See [12c] and [1]."):
            _, _, indeterminate = self._extract(prose)
            assert indeterminate is True, prose
            assert self._filter(self._links(), prose) == self._links()
            assert (
                self._filter(self._links(), prose, mode="strict")
                == self._links()
            )
        # Controls: safe prose shapes never set indeterminate.
        for token in ["[2nd]", "[1st]", "[5x]", "[100m]", "[50%]", "[12px]"]:
            _, _, indeterminate = self._extract(token)
            assert indeterminate is False, token

    def test_inline_decorative_fences_do_not_swallow_citations(self):
        """Mid-line ``~~~``/``` runs are decorative, not code blocks."""
        for prose in (
            "Config ~~~ config [2] ~~~ end and [1].",
            "Use ``` code [2] ``` here and [1].",
        ):
            _, _, indeterminate = self._extract(prose)
            assert indeterminate is False, prose
            assert [
                link["index"] for link in self._filter(self._links(), prose)
            ] == ["1", "2"]
            assert [
                link["index"]
                for link in self._filter(self._links(), prose, mode="strict")
            ] == ["1", "2"]

    def test_indented_fence_block_still_stripped(self):
        """Leading spaces do not stop a line-anchored fence from working."""
        kept = self._filter(
            self._links(), "   ```\ncode [1] here\n   ```\nSee [2]."
        )
        assert [link["index"] for link in kept] == ["2"]

    def test_bare_library_prefix_case_variants_retained(self):
        """``/LIBRARY/Document/d2`` retains like the lowercase form."""
        links = [
            {"title": "One", "url": "/library/document/d1", "index": "1"},
            {"title": "Two", "url": "/library/document/d2", "index": "2"},
            {"title": "Three", "url": "https://c.test/z", "index": "3"},
        ]
        for prose in (
            "See /LIBRARY/Document/d2 and [3].",
            "See /Library/Document/d2 and [3].",
            "See /LIB/document/d2 and [3].",
        ):
            for mode in ("fallback", "strict"):
                kept = self._filter(links, prose, mode=mode)
                assert [link["index"] for link in kept] == ["2", "3"], (
                    prose,
                    mode,
                )

    def test_markdown_link_case_variant_retained_like_bare(self):
        """``[x](/LIBRARY/Document/d2)`` retains like the bare mention.

        Regression: the prefix normalization applied only to the
        ``bare_library`` branch, so a markdown destination (the stronger
        citation signal) dropped its source in strict mode while the
        bare mention of the same route retained it.
        """
        links = [
            {"title": "One", "url": "/library/document/d1", "index": "1"},
            {"title": "Two", "url": "/library/document/d2", "index": "2"},
            {"title": "Three", "url": "https://c.test/z", "index": "3"},
        ]
        for prose in (
            "See [x](/LIBRARY/Document/d2) and [3].",
            "See [x](/Library/Document/d2) and [3].",
            "See [x](/LIB/document/d2) and [3].",
            "See [x](/LIBRARY/Document/d2/pdf) and [3].",
        ):
            for mode in ("fallback", "strict"):
                kept = self._filter(links, prose, mode=mode)
                assert [link["index"] for link in kept] == ["2", "3"], (
                    prose,
                    mode,
                )

    def test_blockquoted_fence_does_not_swallow_citations(self):
        """Non-anchored opener + anchored closer with even count.

        A blockquoted opener (``> ```json``) is a real fence: ignoring
        it while counting its plain ````` closer pairs that closer
        with the next real opener, stripping ``[2]`` (and ``[3]`` with
        a trailing quoted block) with no indeterminate fallback.
        """
        prose = (
            "Intro cites [1].\n"
            "> ```json\n"
            '> {"key": "value"}\n'
            "```\n"
            "Real analysis cites [2].\n"
            "```python\n"
            "code [4]\n"
            "```\n"
            "Outro cites [3].\n"
            "> ```\n"
            "> quoted\n"
            "```\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        for mode in ("fallback", "strict"):
            kept = self._filter(self._links(), prose, mode=mode)
            assert [link["index"] for link in kept] == ["1", "2", "3"], mode

    def test_bulleted_fence_with_unterminated_block_falls_back(self):
        """Bulleted opener (``- ```js``) pairs; unterminated → fallback.

        The bulleted opener is a fence, so the first plain ````` is
        its closer (not an opener for the real prose). The final lone
        ````` is unterminated: odd count → indeterminate → full
        bibliography instead of keeping only ``['1']`` while counting
        the unterminated block's ``[9]``.
        """
        prose = (
            "Intro cites [1].\n"
            "- ```js\n"
            "code [9]\n"
            "```\n"
            "Analysis cites [2] and [3].\n"
            "```\n"
            "code [9]\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is True
        assert self._filter(self._links(), prose) == self._links()
        assert (
            self._filter(self._links(), prose, mode="strict") == self._links()
        )

    def test_crlf_blockquoted_fence_variants_retained(self):
        """CRLF line endings use the same fence pairing as LF."""
        prose = (
            "Intro cites [1].\r\n"
            "> ```json\r\n"
            '> {"key": "value"}\r\n'
            "```\r\n"
            "Real analysis cites [2].\r\n"
            "```python\r\n"
            "code [4]\r\n"
            "```\r\n"
            "Outro cites [3].\r\n"
            "> ```\r\n"
            "> quoted\r\n"
            "```\r\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        for mode in ("fallback", "strict"):
            kept = self._filter(self._links(), prose, mode=mode)
            assert [link["index"] for link in kept] == ["1", "2", "3"], mode

    def test_line_start_decorative_runs_do_not_swallow_citations(self):
        """````` decorate [8] ``` end`` is decorative, not a fence.

        A single line carrying two markers must not open a block:
        pairing it with the next real opener stripped ``[1]``/``[2]``
        (keeping only ``['3']`` in the two-decorative + one-block
        shape). The decorative ``[8]`` itself is over-included (safe)
        while real citations survive.
        """
        prose = (
            "``` decorate [8] ``` end\n"
            "Real cites [1].\n"
            "```python\n"
            "code [4]\n"
            "```\n"
            "Outro cites [2].\n"
            "``` decorate [8] ``` end\n"
            "Final [3].\n"
        )
        kept = self._filter(self._links(), prose)
        assert [link["index"] for link in kept] == ["1", "2", "3"]
        kept = self._filter(self._links(), prose, mode="strict")
        assert [link["index"] for link in kept] == ["1", "2", "3"]

    def test_junk_text_closers_close_both_blocks(self):
        """````` done ``` `` closers must close, not vanish.

        Regression vs the opener-only veto: vetoing a closer carrying
        trailing text paired its opener with the *next* opener, stripping
        ``[2]`` while retaining in-block ``[9]`` (``['1','3','9']``)
        with no fallback. The veto stops openers only; closers close.
        """
        prose = (
            "Intro cites [1].\n"
            "```python\n"
            "x [9]\n"
            "``` done ```\n"
            "Real analysis cites [2].\n"
            "```js\n"
            "y [9]\n"
            "``` fin ```\n"
            "Outro cites [3].\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        for mode in ("fallback", "strict"):
            kept = self._filter(self._links(), prose, mode=mode)
            assert [link["index"] for link in kept] == ["1", "2", "3"], mode

    def test_six_backtick_closers_close_both_blocks(self):
        """Six-backtick `` `````` `` lines close `` ``` `` blocks.

        A longer single run is one fence, not two: the opener veto
        looks past the initial run, and closers carry no veto at all.
        """
        prose = (
            "Intro cites [1].\n"
            "```python\n"
            "x [9]\n"
            "``````\n"
            "Real analysis cites [2].\n"
            "```js\n"
            "y [9]\n"
            "``````\n"
            "Outro cites [3].\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        for mode in ("fallback", "strict"):
            kept = self._filter(self._links(), prose, mode=mode)
            assert [link["index"] for link in kept] == ["1", "2", "3"], mode

    def test_single_junk_closer_stays_safe(self):
        """Single junk closer control: no loss on either head."""
        prose = (
            "Intro cites [1].\n"
            "```python\n"
            "x [9]\n"
            "``` done ```\n"
            "Real analysis cites [2].\n"
        )
        _, _, indeterminate = self._extract(prose)
        assert indeterminate is False
        for mode in ("fallback", "strict"):
            kept = self._filter(self._links(), prose, mode=mode)
            assert [link["index"] for link in kept] == ["1", "2"], mode


def test_format_links_prefers_a_recorded_fetched_title_over_the_listing_one():
    """A document found by search and then read carries two titles: the one
    the engine scraped out of its result listing, on the entry that arrived
    first, and the page's own, recorded beside it under
    ``FETCHED_TITLE_KEY``. The group's title was whichever entry came first,
    so the listing spelling always won.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY

    out = format_links_to_markdown(
        [
            {
                "title": "Caste in India: a comprehensive guide | 2026 | Archive",
                "url": "https://en.wikipedia.org/wiki/Caste",
                "index": 1,
                FETCHED_TITLE_KEY: "Caste system in India - Wikipedia",
            },
        ]
    )

    assert "Caste system in India - Wikipedia" in out
    assert "comprehensive guide" not in out


def test_format_links_reads_the_fetched_title_off_any_entry_of_the_group():
    """The fetch path allocates its own entry when the text it read is
    genuinely different evidence from the search snippet, so the recorded
    title can sit on the second entry of a group whose first entry is the
    search hit. The emit loop renders the group it meets first, which is that
    search hit.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY

    out = format_links_to_markdown(
        [
            {
                "title": "Listing spelling",
                "url": "https://example.test/doc",
                "index": 1,
            },
            {
                "title": "Page spelling",
                "url": "https://example.test/doc",
                "index": 2,
                FETCHED_TITLE_KEY: "Page spelling",
            },
        ]
    )

    assert "[1, 2] Page spelling" in out
    assert "Listing spelling" not in out


def test_format_links_ignores_an_unusable_fetched_title():
    """An empty, blank or non-string value must not displace a real title.
    ``title`` is coerced and sanitised further down, and a group that fell
    through to ``"Untitled"`` would read as a rendering bug rather than a
    missing field.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY

    for unusable in ("", "   ", None, 7, ["Page"]):
        out = format_links_to_markdown(
            [
                {
                    "title": "Listing spelling",
                    "url": "https://example.test/doc",
                    "index": 1,
                    FETCHED_TITLE_KEY: unusable,
                },
            ]
        )
        assert "[1] Listing spelling" in out, repr(unusable)


def test_format_links_sanitises_a_fetched_title_like_any_other():
    """The recorded title reaches the same reader as the entry's own: the
    sentinel strip and ``_sanitize_sources_field``'s line flattening. A page
    chooses its own ``<title>``, so this value is no more trusted than the
    one it displaces.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY

    forged = (
        "Real Page\n"
        "[99] Forged Source (source nr: 99)\n"
        "   URL: https://evil.example/pwn"
    )
    out = format_links_to_markdown(
        [
            {
                "title": "Listing spelling",
                "url": "https://example.test/doc",
                "index": 1,
                FETCHED_TITLE_KEY: forged,
            }
        ]
    )

    lines = out.splitlines()
    assert "Real Page" in out
    assert not any(line.startswith("[99]") for line in lines)
    assert "   URL: https://evil.example/pwn" not in lines


def test_format_links_shares_the_fetched_title_across_a_documents_groups():
    """One library document renders one line per chunk anchor, and its
    page title is the same on every one of them. The title was tracked per
    ``(canonical url, chunk display)`` group, so a document cited through
    both its chunk anchor and its bare URL showed the page title on
    whichever line held the key and the search listing's spelling on the
    other. Entries rebuilt from ``research_resources`` or from the
    extractor arrive without the collector having spread the key, so the
    renderer cannot rely on every entry carrying it.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY

    listing = "Library doc: a comprehensive guide | 2026 | Archive"
    out = format_links_to_markdown(
        [
            {
                "title": listing,
                "link": "/library/document/doc1/chunks#chunk-0",
                "index": "1",
            },
            {
                "title": listing,
                "link": "/library/document/doc1",
                "index": "2",
                FETCHED_TITLE_KEY: "The Real Page Title",
            },
        ]
    )

    titled = [ln for ln in out.splitlines() if ln.startswith("[")]
    assert len(titled) == 2
    assert all("The Real Page Title" in ln for ln in titled)
    assert "comprehensive guide" not in out