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.

4.5 KB
Raw Blame
"""A search listing title must not stand in for a fetched page's title."""

from unittest.mock import Mock

import pytest

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    SearchResultsCollector,
)
from local_deep_research.advanced_search_system.tools.fetch import (
    build_fetch_tool,
)
from local_deep_research.content_fetcher import ContentFetcher
from local_deep_research.research_library.downloaders.base import DownloadResult
from local_deep_research.utilities.research_content_cache import (
    ResearchContentCache,
)
from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY
from local_deep_research.web_search_engines.engines.search_engine_library import (
    LibraryRAGSearchEngine,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)


URL = "https://93.184.216.34/fixture-title"
BODY = "Fully synthetic source content. " * 20
LISTING_TITLE = "Fixture search listing"
PAGE_TITLE = "Fixture page title"


class _WebEngine(BaseSearchEngine):
    def _get_previews(self, query):
        return []

    def _get_full_content(self, items):
        return items


@pytest.fixture
def downloader(monkeypatch):
    transport = Mock()
    transport.download_with_result.return_value = DownloadResult(
        content=BODY.encode("utf-8"), is_success=True, status_code=200
    )
    transport.get_metadata.return_value = {"title": PAGE_TITLE}
    monkeypatch.setattr(
        ContentFetcher, "_get_downloader", lambda self, url_type: transport
    )
    return transport


def _tool_and_collector(cache):
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"link": URL, "title": LISTING_TITLE, "snippet": "Fixture excerpt"}],
        engine_name="fixture",
    )
    tool = build_fetch_tool(
        "full",
        collector,
        settings_snapshot={},
        content_cache=cache,
        max_fetches=0,
    )
    return tool, collector


def _warm_from_search(cache):
    engine = _WebEngine(content_cache=cache)
    engine._store_full_content_in_cache(
        [
            {
                "link": URL,
                "title": LISTING_TITLE,
                "full_content": BODY,
                "full_content_is_complete": True,
            }
        ]
    )
    assert cache.get(URL)["content"] == BODY


def test_search_cache_hit_does_not_record_a_listing_as_a_page_title(downloader):
    cache = ResearchContentCache()
    _warm_from_search(cache)
    tool, collector = _tool_and_collector(cache)

    output = tool.invoke({"url": URL})

    assert BODY in output
    assert "Title: None" not in output
    downloader.download_with_result.assert_not_called()
    assert collector.find_by_index(1)["title"] == LISTING_TITLE
    assert not collector._doc_fetched_title


def test_page_title_can_be_recorded_after_a_search_cache_hit(downloader):
    cache = ResearchContentCache()
    _warm_from_search(cache)
    tool, collector = _tool_and_collector(cache)
    assert BODY in tool.invoke({"url": URL})
    downloader.download_with_result.assert_not_called()

    # Content keys retain the fragment; citation grouping shares the document.
    output = tool.invoke({"url": URL + "#fixture-section"})

    assert BODY in output and PAGE_TITLE in output
    downloader.download_with_result.assert_called_once()
    assert collector.find_by_index(1)[FETCHED_TITLE_KEY] == PAGE_TITLE


def test_real_page_metadata_survives_a_content_fetcher_cache_hit(downloader):
    cache = ResearchContentCache()
    tool, collector = _tool_and_collector(cache)

    first = tool.invoke({"url": URL})
    second = tool.invoke({"url": URL})

    assert BODY in first and BODY in second
    assert PAGE_TITLE in first and PAGE_TITLE in second
    downloader.download_with_result.assert_called_once()
    assert cache.get(URL)["title"] == PAGE_TITLE
    assert collector.find_by_index(1)[FETCHED_TITLE_KEY] == PAGE_TITLE


def test_library_cache_entries_keep_the_database_title():
    cache = ResearchContentCache()
    engine = LibraryRAGSearchEngine(
        settings_snapshot={"_username": "fixture"},
        content_cache=cache,
    )
    url = "/library/document/123e4567-e89b-12d3-a456-426614174000"
    engine._store_full_content_in_cache(
        [
            {
                "link": url,
                "title": "Fixture document title",
                "full_content": BODY,
                "full_content_is_complete": True,
            }
        ]
    )

    assert cache.get(url)["content"] == BODY
    assert cache.get(url)["title"] == "Fixture document title"