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.

40.5 KB
Raw Blame
"""Unit tests for ``advanced_search_system.tools.fetch.library_resolver``.

The library resolver powers the A3 fix: a ``fetch_content`` call on a
``/library/document/<uuid>[/pdf]`` URL or a bare ``[N]`` citation marker
short-circuits the egress policy and reads the document / rewrites the
URL locally. Tests here pin the parsing, the lookup shape, and the
``None``-fall-through contract so a regression can't silently break the
fix (which previously turned 26 of 26 fetches into ``unsupported_scheme``
denials).
"""

from __future__ import annotations

from unittest.mock import MagicMock, call, patch

import pytest


# ---------------------------------------------------------------------------
# Pure parsing
# ---------------------------------------------------------------------------


def test_parse_library_url_root_form():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    assert parse_library_url(f"/library/document/{doc_id}") == (doc_id, None)


def test_parse_library_url_pdf_suffix():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    assert parse_library_url(f"/library/document/{doc_id}/pdf") == (
        doc_id,
        "pdf",
    )


def test_parse_library_url_chunks_suffix_with_anchor():
    """Regression for #5381: RAG citations are emitted as
    ``/library/document/<id>/chunks#chunk-<n>``. Before the ``chunks``
    suffix and fragment stripping existed, these failed to match and the
    fetch tool fell through to the egress gate, which rejects a relative
    path as ``unsupported_scheme`` — i.e. the agent could not read the
    library documents it had just cited.
    """
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-5"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        "chunks",
    )
    # Chunk 0 is a real chunk — the anchor must not be treated as falsy.
    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-0"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        "chunks",
    )
    # Bare chunks route, and the ``/lib/`` abbreviation.
    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        "chunks",
    )
    assert parse_library_url(
        "/lib/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-2"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        "chunks",
    )


def test_parse_library_url_strips_fragment_on_root_form():
    """A fragment addresses a position inside the page, never a different
    document, so it is dropped before matching."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000#chunk-2"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        None,
    )
    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000/pdf#page=4"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        "pdf",
    )


def test_parse_library_url_absolute_alias_with_chunk_anchor():
    """The fragment is stripped BEFORE the ``://`` branch, so the
    ``https://library.document/...`` alias the agent sometimes emits also
    resolves when it carries a chunk anchor. Stripping after that branch
    left this shape unresolvable."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url(
        "https://library.document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3"
    ) == ("123e4567-e89b-12d3-a456-426614174000", "chunks")


@pytest.mark.parametrize(
    "suffix",
    [
        "?",
        "?document_chunk_id=0#chunk-0",
        "?document_chunk_id=-1#chunk-0",
        "?document_chunk_id=9223372036854775808#chunk-0",
        "?document_chunk_id=1&other=2#chunk-0",
        "?document_chunk_id=1",
        "?document_chunk_id=1#invalid",
        "?document_chunk_id=" + "9" * 5000 + "#chunk-0",
    ],
    ids=[
        "empty",
        "zero",
        "negative",
        "over_int64",
        "extra_parameter",
        "missing_fragment",
        "bad_fragment",
        "very_long_id",
    ],
)
def test_parse_library_url_rejects_invalid_chunk_id_query(suffix):
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    assert (
        parse_library_url(f"/library/document/{doc_id}/chunks{suffix}") is None
    )


def test_parse_library_url_fragment_containing_scheme_not_misrouted():
    """A relative path whose fragment contains ``://`` must not be pushed
    into the absolute branch by the ``\"://\" in candidate`` test."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url(
        "/library/document/123e4567-e89b-12d3-a456-426614174000#http://anything"
    ) == (
        "123e4567-e89b-12d3-a456-426614174000",
        None,
    )


def test_parse_library_url_chunk_anchor_still_rejects_bad_doc_ids():
    """Fragment stripping must not widen what counts as a document id."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    for bad in (
        "/library/document//chunks#chunk-1",
        "/library/document/abc%00def/chunks#chunk-1",
        "/library/document/abc/chunks/extra#chunk-1",
        "/library/document/abc/txt#chunk-1",
        "#chunk-1",
    ):
        assert parse_library_url(bad) is None, bad


def test_resolve_library_document_fetches_chunk_content_when_present():
    """A chunk-anchored citation resolves specifically to that chunk's text content."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.database.models.library import (
        Document,
        DocumentChunk,
    )

    document = MagicMock()
    document.id = "123e4567-e89b-12d3-a456-426614174000"
    document.title = "My Paper"
    document.text_content = "full document body"

    chunk = MagicMock()
    chunk.chunk_index = 3
    chunk.chunk_text = "specific chunk 3 content"

    session = MagicMock()

    def query_side_effect(model):
        q = MagicMock()
        if model == Document:
            q.filter_by.return_value.first.return_value = document
        elif model == DocumentChunk:
            q.filter.return_value.order_by.return_value.first.return_value = (
                chunk
            )
        return q

    session.query.side_effect = query_side_effect
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    ):
        result = resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3",
            "alice",
        )

    assert result is not None
    assert result["title"] == "My Paper"
    assert result["content"] == "specific chunk 3 content"
    assert result["snippet"] == "specific chunk 3 content"
    assert (
        result["url"]
        == "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3"
    )


def test_resolve_library_document_uses_exact_hit_chunk_across_index_versions():
    """A citation with a row id fetches its hit, not an older ordinal twin."""
    from contextlib import contextmanager

    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.database.models.library import (
        Base,
        Document,
        DocumentChunk,
        DocumentStatus,
        RAGIndex,
        SourceType,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    with Session() as session:
        session.add(
            SourceType(id="source", name="document", display_name="Document")
        )
        session.add(
            Document(
                id=doc_id,
                source_type_id="source",
                title="Paper",
                text_content="document body",
                document_hash="hash",
                file_size=13,
                file_type="txt",
                status=DocumentStatus.COMPLETED,
            )
        )
        for owner, text in (
            ("old-index", "old text"),
            ("new-index", "new text"),
        ):
            session.add(
                DocumentChunk(
                    chunk_hash=owner,
                    source_type="document",
                    source_id=doc_id,
                    collection_name="collection_one",
                    chunk_text=text,
                    chunk_index=0,
                    start_char=0,
                    end_char=len(text),
                    word_count=2,
                    embedding_id=owner,
                    embedding_model="model",
                    embedding_model_type="openai",
                    vector_index_key=owner,
                )
            )
            session.add(
                RAGIndex(
                    collection_name="collection_one",
                    embedding_model="model",
                    embedding_model_type="openai",
                    embedding_dimension=2,
                    index_path=f"/{owner}.faiss",
                    index_hash=owner,
                    chunk_size=500,
                    chunk_overlap=50,
                    is_current=owner == "new-index",
                )
            )
        session.commit()
        current_id = (
            session.query(DocumentChunk.id)
            .filter_by(vector_index_key="new-index")
            .scalar()
        )
        old_id = (
            session.query(DocumentChunk.id)
            .filter_by(vector_index_key="old-index")
            .scalar()
        )

    @contextmanager
    def user_session(username):
        assert username == "alice"
        with Session() as session:
            yield session

    url = f"/library/document/{doc_id}/chunks?document_chunk_id={current_id}#chunk-0"
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        side_effect=user_session,
    ):
        result = resolve_library_document(url, "alice")
        legacy_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks#chunk-0", "alice"
        )
        old_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks?document_chunk_id={old_id}#chunk-0",
            "alice",
        )
        rebuilt_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks?document_chunk_id={current_id + 1000}#chunk-0",
            "alice",
        )

    assert result is not None
    assert result["content"] == "new text"
    assert result["url"] == url
    assert legacy_result is not None
    assert legacy_result["content"] == "new text"
    assert old_result is not None
    assert old_result["content"] == "old text"
    assert rebuilt_result is not None
    assert rebuilt_result["content"] == "new text"
    # Migration 0042 cannot identify an owner when several index rows match.
    # A stored, ID-less citation must still resolve a legacy NULL-key row.
    with Session() as session:
        session.query(DocumentChunk).delete()
        session.query(RAGIndex).update({"is_current": False})
        session.add(
            DocumentChunk(
                chunk_hash="legacy-null",
                source_type="document",
                source_id=doc_id,
                collection_name="collection_one",
                chunk_text="legacy text",
                chunk_index=0,
                start_char=0,
                end_char=11,
                word_count=2,
                embedding_id="legacy-null",
                embedding_model="model",
                embedding_model_type="openai",
                vector_index_key=None,
            )
        )
        session.commit()
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        side_effect=user_session,
    ):
        null_key_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks#chunk-0", "alice"
        )
    assert null_key_result is not None
    assert null_key_result["content"] == "legacy text"
    with Session() as session:
        session.query(DocumentChunk).update({"embedding_model": "unrelated"})
        session.commit()
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        side_effect=user_session,
    ):
        unrelated_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks#chunk-0", "alice"
        )
    assert unrelated_result is not None
    assert (
        unrelated_result["content"]
        == f"Chunk 0 not found for document {doc_id}."
    )
    with Session() as session:
        session.query(DocumentChunk).update(
            {"embedding_model": "model", "vector_index_key": "old-index"}
        )
        session.commit()
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        side_effect=user_session,
    ):
        retired_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks#chunk-0", "alice"
        )
    assert retired_result is not None
    assert retired_result["content"] == "legacy text"
    with Session() as session:
        session.add(
            DocumentChunk(
                chunk_hash="second-retired",
                source_type="document",
                source_id=doc_id,
                collection_name="collection_one",
                chunk_text="ambiguous text",
                chunk_index=0,
                start_char=0,
                end_char=14,
                word_count=2,
                embedding_id="second-retired",
                embedding_model="model",
                embedding_model_type="openai",
                vector_index_key="new-index",
            )
        )
        session.commit()
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        side_effect=user_session,
    ):
        ambiguous_result = resolve_library_document(
            f"/library/document/{doc_id}/chunks#chunk-0", "alice"
        )
    assert ambiguous_result is not None
    assert (
        ambiguous_result["content"]
        == f"Chunk 0 not found for document {doc_id}."
    )
    engine.dispose()


def test_resolve_library_document_reports_missing_chunk_when_chunk_missing():
    """When a chunk anchor is present but the DocumentChunk row is missing,
    it reports that the chunk was not found instead of silently falling back
    to the full document body."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.database.models.library import (
        Document,
        DocumentChunk,
    )

    document = MagicMock()
    document.id = "123e4567-e89b-12d3-a456-426614174000"
    document.title = "My Paper"
    document.text_content = "full document body"

    session = MagicMock()

    def query_side_effect(model):
        q = MagicMock()
        if model == Document:
            q.filter_by.return_value.first.return_value = document
        elif model == DocumentChunk:
            q.filter.return_value.order_by.return_value.first.return_value = (
                None
            )
        return q

    session.query.side_effect = query_side_effect
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    ):
        result = resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3",
            "alice",
        )

    assert result is not None
    assert result["title"] == "My Paper"
    assert "Chunk 3 not found for document" in result["content"]
    assert result["content"] != "full document body"
    assert (
        result["url"]
        == "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3"
    )


def test_resolve_library_document_missing_chunk_not_cached_and_retries_successfully():
    """Missing chunks must not be cached as source text (PR #6759).

    A chunk-anchored citation whose chunk row is missing returns a
    "Chunk N not found" message, but that failure must NOT be written to
    the content cache. A later call after the chunk is indexed must query
    the database and return the real text, while a successfully resolved
    chunk must be cached and reused on retry without querying the database.
    """
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.database.models.library import (
        Document,
        DocumentChunk,
    )
    from local_deep_research.utilities.research_content_cache import (
        ResearchContentCache,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    url = f"/library/document/{doc_id}/chunks#chunk-3"

    document = MagicMock()
    document.id = doc_id
    document.title = "My Paper"
    document.text_content = "full document body"

    chunk = MagicMock()
    chunk.id = 1
    chunk.chunk_index = 3
    chunk.chunk_text = "Real chunk 3 text content"
    chunk.collection_name = "default"

    chunk_available = False
    session = MagicMock()

    def query_side_effect(model):
        q = MagicMock()
        if model == Document:
            q.filter_by.return_value.first.return_value = document
        elif model == DocumentChunk:
            q.filter.return_value.order_by.return_value.first.return_value = (
                chunk if chunk_available else None
            )
        return q

    session.query.side_effect = query_side_effect
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False

    cache = ResearchContentCache()

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    ):
        # 1. First attempt: chunk does not exist yet
        res1 = resolve_library_document(url, "alice", content_cache=cache)
        assert res1 is not None
        assert "Chunk 3 not found for document" in res1["content"]
        # Must NOT be stored in cache
        assert cache.get(url) is None

        # 2. Chunk becomes available; retry through the SAME cache
        chunk_available = True
        session.query.reset_mock()
        res2 = resolve_library_document(url, "alice", content_cache=cache)
        assert res2 is not None
        assert res2["content"] == "Real chunk 3 text content"
        # Must hit DB because failure was not cached
        assert session.query.call_count > 0
        # Now the successful resolution IS cached
        assert cache.get(url) is not None

        # 3. Control: repeat with chunk available -> served from cache, no DB lookup
        session.query.reset_mock()
        res3 = resolve_library_document(url, "alice", content_cache=cache)
        assert res3 is not None
        assert res3["content"] == "Real chunk 3 text content"
        assert session.query.call_count == 0


def test_resolve_library_document_returns_empty_content_when_chunk_text_empty():
    """When a chunk is present but its chunk_text is empty, returns content=""
    rather than falling back to the full document."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.database.models.library import (
        Document,
        DocumentChunk,
    )

    document = MagicMock()
    document.id = "123e4567-e89b-12d3-a456-426614174000"
    document.title = "My Paper"
    document.text_content = "full document body"

    chunk = MagicMock()
    chunk.chunk_index = 3
    chunk.chunk_text = ""

    session = MagicMock()

    def query_side_effect(model):
        q = MagicMock()
        if model == Document:
            q.filter_by.return_value.first.return_value = document
        elif model == DocumentChunk:
            q.filter.return_value.order_by.return_value.first.return_value = (
                chunk
            )
        return q

    session.query.side_effect = query_side_effect
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    ):
        result = resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3",
            "alice",
        )

    assert result is not None
    assert result["title"] == "My Paper"
    assert result["content"] == ""
    assert result["snippet"] == "My Paper"


def test_parse_library_url_oversized_chunk_fragment_does_not_raise():
    """Regression for digit-length guard: an oversized chunk fragment (e.g. 5000 digits)
    must not raise ValueError during integer parsing."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
        resolve_library_document,
    )

    huge_fragment = "chunk-" + ("9" * 5000)
    url = f"/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#{huge_fragment}"

    parsed = parse_library_url(url)
    assert parsed == ("123e4567-e89b-12d3-a456-426614174000", "chunks")

    res = resolve_library_document(url, username=None)
    assert res is None


def test_parse_library_url_trailing_slash():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    assert parse_library_url(f"/library/document/{doc_id}/") == (
        doc_id,
        None,
    )


def test_parse_library_url_accepts_observed_document_aliases():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    doc_id = "123e4567-e89b-12d3-a456-426614174000"
    assert parse_library_url(f"/lib/document/{doc_id}") == (doc_id, None)
    assert parse_library_url(f"https://library.document/{doc_id}") == (
        doc_id,
        None,
    )
    assert parse_library_url(f"[{doc_id}]") == (doc_id, None)


def test_parse_library_url_rejects_guessable_filename_forms():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url("/legacy/document/report%202024.txt") is None
    assert parse_library_url("report 2024.txt") is None
    # The path form is bound to the canonical document-ID pattern (32/64-hex
    # or UUID), so a guessable filename under /library/document/ or
    # /lib/document/ is rejected at the regex, not by a downstream DB miss.
    assert parse_library_url("/library/document/report.pdf") is None
    assert parse_library_url("/lib/document/secret.key") is None


def test_parse_library_url_rejects_encoded_path_traversal():
    """Encoded path traversal cannot escape the document segment.

    Rejected by ``_LIBRARY_PATH_RE`` itself, before any decoding happens: the
    doc_id group is hex/UUID only, so a ``%`` cannot appear in it and the match
    fails. ``_decode_segment``'s own ``/`` check is never reached on this input
    -- verified by instrumenting it -- so do not read this test as evidence
    that decoding is what stops traversal here.

    That matters if the doc_id group is ever loosened: ``_decode_segment`` is
    not currently a live second layer behind it, because the charset the regex
    admits can never contain a percent-escape for it to decode.
    """
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    assert parse_library_url("/library/document/%2e%2e%2fetc%2fpasswd") is None
    assert parse_library_url("/lib/document/%2e%2e%2fetc%2fpasswd") is None


def test_parse_library_url_rejects_non_matching_shapes():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        parse_library_url,
    )

    for bad in (
        "http://example.com/library/document/abc",
        "/library/document/",
        "/library/document",
        "/library/abc",  # not under document/
        "/library/document/abc/txt",  # txt suffix is not library-DB route
        "library/document/abc",  # missing leading slash
        "https://library.document.evil.test/abc",
        "https://[email protected]/abc",
        "https://library.document/abc?download=1",
        "https://library.document:443/abc",
        "https://library.document./abc",
        "http://library.document/abc",
        "/library/document/abc%00def",
        "/library/document/abc%1fdef",
        "/library/document/abc%7fdef",
        "[42]",  # numeric markers remain collector citation references
        "[not-a-document-id]",
        "bare words without an extension",
        "",
        None,
        42,
    ):
        assert parse_library_url(bad) is None, f"expected None for {bad!r}"


def test_is_citation_reference_matches_well_formed_marker():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        is_citation_reference,
    )

    assert is_citation_reference("[1]") == 1
    assert is_citation_reference("[1062]") == 1062
    # Marker is strict: no whitespace tolerance — a malformed marker
    # must NOT be silently treated as a valid citation reference.
    assert is_citation_reference("[ 42 ]") is None


def test_is_citation_reference_rejects_lookalikes():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        is_citation_reference,
    )

    for bad in (
        "[1, 2]",  # list marker
        "[]",
        "[abc]",  # non-digit
        "1",  # no brackets
        "[1",  # missing close
        "1]",
        "",
        None,
    ):
        assert is_citation_reference(bad) is None, f"expected None for {bad!r}"


# ---------------------------------------------------------------------------
# resolve_library_document — DB interaction
# ---------------------------------------------------------------------------


def _fake_document(text_content="Body text", title="Doc Title"):
    doc = MagicMock()
    doc.id = "abc-123"
    doc.title = title
    doc.text_content = text_content
    return doc


def test_resolve_library_document_returns_shape_for_full_text():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    document = _fake_document(text_content="full body", title="T")

    # Patch both the model import and the session so the test runs without
    # SQLAlchemy fixtures.
    with (
        patch(
            "local_deep_research.database.models.library.Document",
            return_value=document,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session"
        ) as session_cm,
    ):
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            document
        )
        session_cm.return_value.__enter__.return_value = session

        result = resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000",
            username="alice",
        )

    assert result is not None
    assert result["title"] == "T"
    assert result["content"] == "full body"
    assert (
        result["url"]
        == "/library/document/123e4567-e89b-12d3-a456-426614174000"
    )
    # Snippet is the first ~200 chars of the content.
    assert result["snippet"] == "full body"


def test_resolve_library_document_falls_back_to_document_hash():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    doc_hash = "d" * 64
    document = _fake_document(text_content="hash body", title="Hash Doc")

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        session = MagicMock()
        id_query = MagicMock()
        id_query.first.return_value = None
        hash_query = MagicMock()
        hash_query.first.return_value = document
        session.query.return_value.filter_by.side_effect = [
            id_query,
            hash_query,
        ]
        session_cm.return_value.__enter__.return_value = session

        result = resolve_library_document(
            f"https://library.document/{doc_hash}", username="alice"
        )

    assert result is not None
    assert result["content"] == "hash body"
    assert session.query.return_value.filter_by.call_args_list == [
        call(id=doc_hash),
        call(document_hash=doc_hash),
    ]


def test_resolve_library_document_normalizes_32_hex_uuid():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    compact_id = "fc524319abee46deb8b9415425fd77ec"  # DevSkim: ignore DS173237 — test-fixture UUID (canonical form on next line), not a secret
    canonical_id = "fc524319-abee-46de-b8b9-415425fd77ec"
    document = _fake_document(text_content="uuid body", title="UUID Doc")

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        session = MagicMock()
        missing_id = MagicMock()
        missing_id.first.return_value = None
        missing_hash = MagicMock()
        missing_hash.first.return_value = None
        canonical_query = MagicMock()
        canonical_query.first.return_value = document
        session.query.return_value.filter_by.side_effect = [
            missing_id,
            missing_hash,
            canonical_query,
        ]
        session_cm.return_value.__enter__.return_value = session

        result = resolve_library_document(
            f"/library/document/{compact_id}", username="alice"
        )

    assert result is not None
    assert result["content"] == "uuid body"
    assert session.query.return_value.filter_by.call_args_list == [
        call(id=compact_id),
        call(document_hash=compact_id),
        call(id=canonical_id),
    ]


def test_resolve_library_document_handles_empty_text():
    """An empty ``text_content`` is still a successful resolve — the
    fetch tool's existing NOT RELEVANT guard handles the empty case."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    document = _fake_document(text_content="", title="T")

    with (
        patch(
            "local_deep_research.database.models.library.Document",
            return_value=document,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session"
        ) as session_cm,
    ):
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            document
        )
        session_cm.return_value.__enter__.return_value = session

        result = resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000",
            username="alice",
        )

    assert result is not None
    assert result["content"] == ""
    assert result["snippet"] == "T"  # falls back to title


def test_resolve_library_document_returns_none_for_unknown_doc():
    """An unknown document UUID returns ``None`` so the caller falls
    through to the egress policy and produces the standard
    ``unsupported_scheme`` denial."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )
        session_cm.return_value.__enter__.return_value = session

        assert (
            resolve_library_document(
                "/library/document/123e4567-e89b-12d3-a456-426614174000",
                username="alice",
            )
            is None
        )


def test_resolve_library_document_returns_none_when_no_username():
    """No username (programmatic mode, benchmarks) means no library —
    return ``None`` so the call falls through to the egress policy."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    assert (
        resolve_library_document(
            "/library/document/123e4567-e89b-12d3-a456-426614174000",
            username=None,
        )
        is None
    )


def test_resolve_library_document_swallows_db_errors():
    """A per-document DB hiccup is non-fatal (mirrors the
    LibraryRAGSearchEngine behaviour). Return ``None`` so the caller
    falls through."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        session_cm.return_value.__enter__.side_effect = RuntimeError("db down")

        assert (
            resolve_library_document(
                "/library/document/123e4567-e89b-12d3-a456-426614174000",
                username="alice",
            )
            is None
        )


def test_resolve_library_document_handles_import_error():
    """An import failure when loading DB models returns None and logs an exception."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    with patch.dict(
        "sys.modules", {"local_deep_research.database.models.library": None}
    ):
        assert (
            resolve_library_document(
                "/library/document/123e4567-e89b-12d3-a456-426614174000",
                username="alice",
            )
            is None
        )


def test_resolve_library_document_returns_none_for_non_matching_url():
    """A URL that isn't ``/library/document/...`` returns ``None``
    immediately (no DB call)."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        assert (
            resolve_library_document("http://example.com/", username="alice")
            is None
        )
        session_cm.assert_not_called()


# ---------------------------------------------------------------------------
# resolve_citation_reference — collector interaction
# ---------------------------------------------------------------------------


def test_resolve_citation_reference_returns_dict_for_known_index():
    """A bare ``[N]`` marker returns the citation dict from the
    collector (so the fetch tool can extract its URL and recurse)."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_citation_reference,
    )

    citation = {"link": "http://example.com/", "title": "T", "index": "1"}
    collector = MagicMock()
    collector.find_by_index.return_value = citation

    result = resolve_citation_reference("[1]", collector)
    assert result == citation
    collector.find_by_index.assert_called_once_with(1)


def test_resolve_citation_reference_returns_none_for_unknown():
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_citation_reference,
    )

    collector = MagicMock()
    collector.find_by_index.return_value = None

    assert resolve_citation_reference("[42]", collector) is None


def test_resolve_citation_reference_returns_none_for_non_marker():
    """A non-marker input doesn't even hit the collector."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_citation_reference,
    )

    collector = MagicMock()
    assert resolve_citation_reference("http://example.com/", collector) is None
    collector.find_by_index.assert_not_called()


def test_resolve_citation_reference_returns_none_without_find_by_index():
    """A duck-typed collector without ``find_by_index`` doesn't crash
    (mirrors the langgraph module's own defensive getattr in
    ``SearchResultsCollector``)."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_citation_reference,
    )

    collector = MagicMock(spec=[])  # no find_by_index attribute
    assert resolve_citation_reference("[1]", collector) is None


# ---------------------------------------------------------------------------
# make_library_resolver — closure binding
# ---------------------------------------------------------------------------


def test_make_library_resolver_binds_username():
    """The closure captures the username so subagent threads can resolve
    library URLs without inheriting Flask session state."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        make_library_resolver,
    )

    document = _fake_document(text_content="hello")

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as session_cm:
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            document
        )
        session_cm.return_value.__enter__.return_value = session

        resolver = make_library_resolver("alice")
        result = resolver(
            "/library/document/123e4567-e89b-12d3-a456-426614174000"
        )

    assert result is not None
    assert result["content"] == "hello"


def test_make_library_resolver_returns_none_for_no_username():
    """``make_library_resolver(None)`` returns a resolver that always
    returns ``None`` — used in programmatic mode where no user library
    exists."""
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        make_library_resolver,
    )

    resolver = make_library_resolver(None)
    assert (
        resolver("/library/document/123e4567-e89b-12d3-a456-426614174000")
        is None
    )


def test_bracket_alias_is_recited_as_the_canonical_route():
    """The bare ``[<uuid>]`` alias is not a URL, so registering it verbatim
    gave the document a THIRD bibliography entry (alongside its
    ``/library/document/<id>`` and ``https://library.document/<id>``
    citations) that also rendered as a dead link. It is re-emitted as the
    RESOLVED row's canonical route — the lookup also accepts a
    document_hash and a dash-less UUID, neither of which is a live route.
    """
    from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
        resolve_library_document,
    )
    from local_deep_research.utilities.url_utils import canonical_url_key

    doc_id = "550e8400-e29b-41d4-a716-446655440000"
    document = MagicMock()
    document.id = doc_id
    document.title = "My Paper"
    document.text_content = "full document body"

    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        document
    )
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False

    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    ):
        result = resolve_library_document(f"[{doc_id}]", "alice")

    assert result is not None
    assert result["url"] == f"/library/document/{doc_id}"
    # ... which shares a bibliography entry with the document's other
    # citation spellings.
    assert canonical_url_key(result["url"]) == canonical_url_key(
        f"/library/document/{doc_id}/chunks#chunk-3"
    )