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.

19.3 KB
Raw Blame
"""Tests for the centralised chunk-anchor helpers.

These tests exercise the single source of truth that the LangGraph
result collector and the RAG producer search engines all share for
deciding whether a ``chunk_index`` / ``chunk_id`` metadata value is a
safe anchor target. They pin the contract so a producer cannot
interpolate a malformed value into a citation and the collector cannot
skip validation whenever ``#chunk-`` is already present in the URL.
"""

import pytest

from local_deep_research.utilities.chunk_anchor import (
    build_chunk_anchor_url,
    extract_chunk_index,
    extract_document_id,
    is_library_chunk_result,
)


class TestExtractChunkIndex:
    """``extract_chunk_index`` must reject any value that cannot be safely
    interpolated into a ``#chunk-<n>`` URL fragment."""

    @pytest.mark.parametrize(
        "raw,expected",
        [
            (0, 0),
            (1, 1),
            (42, 42),
            (999, 999),
        ],
    )
    def test_accepts_non_negative_int(self, raw, expected):
        assert extract_chunk_index({"chunk_index": raw}) == expected

    @pytest.mark.parametrize(
        "raw",
        [True, False],
    )
    def test_rejects_bool_even_though_bool_subclasses_int(self, raw):
        # True / False are technically ``int`` in Python but never valid
        # chunk indices — previously slipped through as ``#chunk-True``.
        assert extract_chunk_index({"chunk_index": raw}) is None

    @pytest.mark.parametrize(
        "raw",
        [-1, -42],
    )
    def test_rejects_negative_int(self, raw):
        assert extract_chunk_index({"chunk_index": raw}) is None

    @pytest.mark.parametrize(
        "raw,expected",
        [
            ("0", 0),
            ("1", 1),
            ("42", 42),
        ],
    )
    def test_accepts_int_like_string(self, raw, expected):
        assert extract_chunk_index({"chunk_id": raw}) == expected

    @pytest.mark.parametrize(
        "raw",
        [
            "550e8400-e29b-41d4-a716-446655440000",  # UUID
            "abc",  # non-numeric
            "1.5",  # not int-like
            "  ",  # whitespace-only
            "1abc",  # mixed
        ],
    )
    def test_rejects_non_int_like_string(self, raw):
        assert extract_chunk_index({"chunk_id": raw}) is None

    def test_chunk_index_takes_precedence_over_chunk_id(self):
        # chunk_index wins when both are present; chunk_id is ignored.
        assert extract_chunk_index({"chunk_index": 5, "chunk_id": "999"}) == 5

    def test_falls_back_to_chunk_id_when_chunk_index_absent(self):
        assert extract_chunk_index({"chunk_id": 7}) == 7

    def test_returns_none_when_neither_key_present(self):
        assert extract_chunk_index({}) is None
        assert extract_chunk_index({"other": 1}) is None

    def test_returns_none_for_non_mapping_metadata(self):
        assert extract_chunk_index(None) is None
        assert extract_chunk_index("not a mapping") is None
        assert extract_chunk_index([1, 2, 3]) is None

    def test_float_with_integer_value_accepted(self):
        assert extract_chunk_index({"chunk_index": 5.0}) == 5

    def test_float_with_fractional_value_rejected(self):
        assert extract_chunk_index({"chunk_index": 5.5}) is None

    def test_negative_float_rejected(self):
        assert extract_chunk_index({"chunk_index": -5.0}) is None

    def test_none_chunk_index_falls_back_to_chunk_id(self):
        # ``chunk_index: None`` should NOT shadow ``chunk_id``.
        assert extract_chunk_index({"chunk_index": None, "chunk_id": 7}) == 7

    @pytest.mark.parametrize("value", [{}, [], object(), set()])
    def test_rejects_arbitrary_non_numeric_types(self, value):
        assert extract_chunk_index({"chunk_index": value}) is None


class TestExtractDocumentId:
    """``extract_document_id`` must sanitise for safe URL interpolation."""

    @pytest.mark.parametrize(
        "metadata,expected",
        [
            ({"doc_id": "doc1"}, "doc1"),
            ({"source_id": "doc1"}, "doc1"),
            ({"document_id": "doc1"}, "doc1"),
            ({"doc_id": "abc-123_xyz"}, "abc-123_xyz"),  # UUID-like
            ({"doc_id": 123}, "123"),  # int coerced to str
        ],
    )
    def test_accepts_valid_ids(self, metadata, expected):
        assert extract_document_id(metadata) == expected

    def test_doc_id_takes_precedence(self):
        # doc_id is the legacy key, checked first.
        assert (
            extract_document_id({"doc_id": "first", "source_id": "second"})
            == "first"
        )

    def test_top_level_fallback(self):
        # When metadata doesn't carry the id, check the top-level result.
        top = {"source_id": "top-level"}
        assert extract_document_id({}, top) == "top-level"

    def test_top_level_document_id_fallback(self):
        top = {"document_id": "top-doc-id"}
        assert extract_document_id({}, top) == "top-doc-id"

    def test_top_level_doc_id_fallback(self):
        top = {"doc_id": "top-doc-id"}
        assert extract_document_id({}, top) == "top-doc-id"

    @pytest.mark.parametrize(
        "bad_id",
        [
            "../../etc/passwd",
            "/etc/passwd",
            "doc with space",
            "doc\nnewline",
            "doc;injected",
        ],
    )
    def test_rejects_unsafe_strings(self, bad_id):
        # Path traversal / control chars must NOT make it through.
        assert extract_document_id({"doc_id": bad_id}) is None

    def test_returns_none_when_no_candidates(self):
        assert extract_document_id({}) is None

    def test_returns_none_for_empty_string(self):
        assert extract_document_id({"doc_id": ""}) is None
        assert extract_document_id({"doc_id": "   "}) is None  # strip → empty

    def test_none_metadata_returns_none(self):
        assert extract_document_id(None) is None

    def test_bool_rejected(self):
        # bool subclasses int but is never a valid doc id.
        assert extract_document_id({"doc_id": True}) is None

    def test_metadata_with_non_mapping_top_level_safe(self):
        # The top-level fallback is type-checked — non-mapping values
        # don't crash and don't leak raw content into the doc id.
        assert extract_document_id({}, "raw string", None, 42) is None


class TestIsLibraryChunkResult:
    """``is_library_chunk_result`` must agree with the collector's
    eligibility check for chunk URL rewriting."""

    def test_source_library_true(self):
        assert is_library_chunk_result({"source": "library"}) is True

    def test_source_type_library_true(self):
        assert is_library_chunk_result({"source_type": "library"}) is True

    def test_link_to_library_document_true(self):
        assert (
            is_library_chunk_result(
                {"link": "/library/document/doc1/chunks#chunk-5"}
            )
            is True
        )

    def test_unrelated_result_false(self):
        assert is_library_chunk_result({"link": "http://example.com"}) is False
        assert is_library_chunk_result({}) is False

    def test_non_mapping_returns_false(self):
        assert is_library_chunk_result(None) is False
        assert is_library_chunk_result("string") is False
        assert is_library_chunk_result([1, 2]) is False


class TestBuildChunkAnchorUrl:
    """``build_chunk_anchor_url`` must return ``None`` rather than
    mutate the input when its arguments aren't safe."""

    def test_valid_inputs_produce_url(self):
        assert (
            build_chunk_anchor_url("http://x.com", "doc1", 5)
            == "/library/document/doc1/chunks#chunk-5"
        )

    def test_missing_doc_id_returns_none(self):
        # Missing doc_id → ``None``, caller leaves link unchanged.
        assert build_chunk_anchor_url("http://x.com", None, 5) is None

    def test_missing_chunk_index_returns_none(self):
        assert build_chunk_anchor_url("http://x.com", "doc1", None) is None

    @pytest.mark.parametrize("bad_chunk", [-1, True, False, "1.5", "uuid"])
    def test_invalid_chunk_index_returns_none(self, bad_chunk):
        assert build_chunk_anchor_url("http://x.com", "doc1", bad_chunk) is None

    def test_zero_chunk_index_accepted(self):
        # 0 is a valid 0-indexed chunk anchor.
        assert (
            build_chunk_anchor_url("http://x.com", "doc1", 0)
            == "/library/document/doc1/chunks#chunk-0"
        )

    def test_document_chunk_id_preserves_ui_anchor(self):
        assert (
            build_chunk_anchor_url("", "doc1", 0, document_chunk_id=42)
            == "/library/document/doc1/chunks?document_chunk_id=42#chunk-0"
        )


class TestProducerCollectorTemplateContract:
    """Shared CI contract test for the producer→collector→template path.

    A single assertion sweep that covers malformed-chunk, missing-doc-id,
    and wrong-route cases. If a producer ships a malformed chunk anchor,
    the collector must NOT propagate it into the citation that the
    template will render.
    """

    @pytest.fixture
    def collector(self):
        from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
            SearchResultsCollector,
        )

        return SearchResultsCollector([])

    @pytest.mark.parametrize(
        "metadata",
        [
            # UUID chunk_id → must not produce #chunk-<uuid>
            {
                "chunk_id": "550e8400-e29b-41d4-a716-446655440000",
                "doc_id": "doc1",
            },
            # Boolean chunk_index → must not produce #chunk-True
            {"chunk_index": True, "doc_id": "doc1"},
            # Negative chunk_index → must not produce #chunk--1
            {"chunk_index": -1, "doc_id": "doc1"},
            # String chunk_index that isn't pure digits
            {"chunk_index": "abc", "doc_id": "doc1"},
            # Missing doc_id but valid chunk_index
            {"chunk_index": 5},
        ],
    )
    def test_malformed_inputs_do_not_produce_fragment(
        self, collector, metadata
    ):
        """For every malformed input the URL fragment must NOT appear in
        the citation — the collector must defend against a producer
        interpolating a bad value verbatim."""
        result = {
            "title": "Doc",
            "link": "/library/document/doc1",
            "source": "library",
            "metadata": metadata,
        }
        collector.add_results([result])
        link = collector.results[0]["link"]
        assert "#chunk-" not in link, (
            f"malformed chunk anchor leaked into citation: {link!r}"
        )

    def test_valid_inputs_round_trip_to_template_anchor(self, collector):
        """Valid inputs must produce the exact anchor id the template
        renders, so a citation click scrolls to a real chunk."""
        collector.add_results(
            [
                {
                    "title": "Doc",
                    "link": "/library/document/doc1",
                    "source": "library",
                    "metadata": {"chunk_index": 5, "doc_id": "doc1"},
                }
            ]
        )
        link = collector.results[0]["link"]
        assert link == "/library/document/doc1/chunks#chunk-5"
        # The fragment is exactly what document_chunks.html renders as
        # ``id="chunk-{{ chunk.index }}"`` for the matching chunk row.
        assert link.endswith("#chunk-5")

    def test_producer_prebuilt_malformed_fragment_is_stripped(self, collector):
        """Even when the producer already interpolated a bad fragment into
        the URL, the collector must strip it rather than trust it."""
        collector.add_results(
            [
                {
                    "title": "Doc",
                    "link": "/library/document/doc1/chunks#chunk-True",
                    "source": "library",
                    # Metadata has no usable chunk_index — only the
                    # prebuilt fragment. The collector must strip it.
                    "metadata": {"doc_id": "doc1"},
                }
            ]
        )
        link = collector.results[0]["link"]
        assert "#chunk-" not in link
        assert link == "/library/document/doc1/chunks"


def test_build_chunk_anchor_url_rejects_unsafe_document_ids():
    """The function BUILDS the URL, so it must enforce the doc-id contract
    itself rather than trusting the caller. Every in-tree caller happens to
    pre-sanitise, which is why this went uncovered — a future one may not."""
    from local_deep_research.utilities.chunk_anchor import (
        build_chunk_anchor_url,
    )

    for bad in ("../../secrets", "doc#1", "a/b", "a b", "", "a?q=1"):
        assert build_chunk_anchor_url("/x", bad, 5) is None, bad
    # Leading/trailing whitespace is stripped, not smuggled into the URL.
    assert (
        build_chunk_anchor_url("/x", "\r\nok-id_9  ", 0)
        == "/library/document/ok-id_9/chunks#chunk-0"
    )


def test_build_chunk_anchor_url_accepts_the_same_types_as_extract():
    """The two share ``is_safe_document_id`` precisely so they cannot
    disagree; bool and non-str/int objects must be rejected by both."""
    import uuid

    from local_deep_research.utilities.chunk_anchor import (
        build_chunk_anchor_url,
        extract_document_id,
    )

    for value in (True, False, uuid.uuid4(), 1.5, ["x"]):
        assert build_chunk_anchor_url("/x", value, 0) is None, value
        assert extract_document_id({"doc_id": value}) is None, value
    # int (not bool) is accepted by both.
    assert extract_document_id({"doc_id": 7}) == "7"
    assert (
        build_chunk_anchor_url("/x", 7, 0)
        == "/library/document/7/chunks#chunk-0"
    )


def test_extract_chunk_index_requires_ascii_digits_and_a_sane_bound():
    """``str.isdigit()`` is Unicode-wide and ``str.strip()`` undid the
    "whitespace is rejected" promise, so " 5 ", the Arabic-Indic "٧"
    and the mathematical "\U0001d7dd" all produced anchors. Unbounded
    magnitudes did too — including ``1e30``, which ``int()`` silently
    rounds to 1000000000000000019884624838656."""
    from local_deep_research.utilities.chunk_anchor import (
        MAX_CHUNK_INDEX,
        extract_chunk_index,
    )

    for bad in (
        " 5 ",
        "5 ",
        "\t5",
        "٧",
        "\U0001d7dd",
        "5",
        10**30,
        1e30,
        MAX_CHUNK_INDEX + 1,
        float(MAX_CHUNK_INDEX + 1),
    ):
        assert extract_chunk_index({"chunk_index": bad}) is None, repr(bad)

    # Everything previously accepted still is — including the 0 boundary,
    # which must stay distinct from "no anchor".
    assert extract_chunk_index({"chunk_index": 0}) == 0
    assert extract_chunk_index({"chunk_index": "0"}) == 0
    assert extract_chunk_index({"chunk_index": 0.0}) == 0
    assert extract_chunk_index({"chunk_index": 5}) == 5
    assert extract_chunk_index({"chunk_index": "5"}) == 5
    assert extract_chunk_index({"chunk_index": MAX_CHUNK_INDEX})
    assert extract_chunk_index({"chunk_id": "12"}) == 12


def test_is_safe_document_id_is_ascii_only():
    """``str.isalnum()`` accepts every Unicode letter/digit, so a fullwidth
    id was emitted unencoded into a URL path and then keyed separately from
    its ``%EF%BC%97`` form — one document, two bibliography entries."""
    from local_deep_research.utilities.chunk_anchor import (
        build_chunk_anchor_url,
        is_safe_document_id,
    )

    for bad in ("7", "١", "café", "²", "a b", "a/b", ""):
        assert is_safe_document_id(bad) is False, repr(bad)
        assert build_chunk_anchor_url("/x", bad, 0) is None, repr(bad)

    for good in ("7", "abc-123", "a_b", "0123456789abcdef" * 2):
        assert is_safe_document_id(good) is True, good


def test_is_library_chunk_result_is_anchored_at_the_url_start():
    """An unanchored ``"/library/document/" in link`` also matched
    ``https://evil.example/library/document/7/chunks`` — and the collector
    reacts by REPLACING the link with a local route, silently relabelling
    an external result as a document in the user's own library."""
    from local_deep_research.utilities.chunk_anchor import (
        is_library_chunk_result,
    )

    for external in (
        "https://evil.example/library/document/7/chunks",
        "https://evil.example/?next=/library/document/7",
        "http://library.document.evil.example/7/chunks",
    ):
        assert (
            is_library_chunk_result({"link": external, "chunk_index": 3})
            is False
        ), external

    for internal in (
        "/library/document/7/chunks",
        "/lib/document/7",
        "https://library.document/7/chunks#chunk-3",
    ):
        assert is_library_chunk_result({"link": internal}) is True, internal
    # The explicit source markers still win regardless of the link.
    assert is_library_chunk_result({"source": "library", "link": ""}) is True


def test_external_library_lookalike_is_not_rewritten_by_the_collector():
    from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
        SearchResultsCollector,
    )

    all_links = []
    collector = SearchResultsCollector(all_links)
    collector.add_results(
        [
            {
                "title": "Evil",
                "link": "https://evil.example/library/document/7/chunks",
                "metadata": {"doc_id": "7", "chunk_index": 3},
            }
        ],
        engine_name="web",
    )

    assert (
        all_links[0]["link"] == "https://evil.example/library/document/7/chunks"
    )


class TestLibraryDocumentLinkAliasHost:
    """``is_library_document_link`` matches the alias on HOST, not on a
    literal ``https://library.document/`` prefix.

    The prefix form missed the ``:443`` and userinfo spellings that
    ``_normalize_library_alias`` accepts as the same document. Combined
    with a control character — which makes the PARSER refuse the string
    too — such a URL missed both arms of ``_is_library_citation``, so its
    unvalidated fragment rode through all three collector ingest paths
    into the DB and the MCP payload.
    """

    def test_alias_spellings_that_name_the_same_document_are_accepted(self):
        from local_deep_research.utilities.chunk_anchor import (
            is_library_document_link,
        )

        for url in (
            "https://library.document/abc/chunks",
            "https://library.document:443/abc/chunks",
            "https://[email protected]/abc/chunks",
            "https://u:[email protected]:443/abc/chunks",
            "HTTPS://LIBRARY.DOCUMENT/abc",
            "  https://library.document/abc  ",
        ):
            assert is_library_document_link(url), url

    def test_look_alike_hosts_are_still_refused(self):
        """The caller REPLACES a matching link with a local route, so a
        false positive relabels an external page as the user's own
        document."""
        from local_deep_research.utilities.chunk_anchor import (
            is_library_document_link,
        )

        for url in (
            "https://library.document.evil.test/abc/chunks",
            "https://xlibrary.document/abc",
            "https://evil.example/library/document/7/chunks",
            "http://library.document/abc",  # scheme matters
            "https://library.document",  # authority with no path
            # urlsplit DELETES these, which would let a smuggled host
            # answer for the real one; the manual parse must not.
            "https://library.doc\tument/abc",
            "https://library.doc\nument/abc",
            "https://[email protected]/abc",
        ):
            assert not is_library_document_link(url), url