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.

5.6 KB
Raw Blame
"""Shared fixtures for citation handler tests."""

import pytest
from unittest.mock import AsyncMock, Mock


def _make_mock_llm(response):
    """A mock LLM exposing the full sync + async LangChain surface.

    ``stream``/``astream`` return REAL (async) generators rather than bare
    Mocks: a Mock return value is not iterable, so a test that sets a stream
    callback would silently land in the handler's ``except Exception``
    fallback and quietly test the wrong branch.
    """
    llm = Mock()
    llm.invoke = Mock(return_value=response)
    llm.ainvoke = AsyncMock(return_value=response)

    def _stream(_prompt):
        yield response

    async def _astream(_prompt):
        yield response

    llm.stream = Mock(side_effect=_stream)
    llm.astream = Mock(side_effect=_astream)
    return llm


@pytest.fixture
def mock_llm():
    """Create a mock LLM that returns configurable responses."""
    return _make_mock_llm(Mock(content="Test response with citation [1]."))


@pytest.fixture
def mock_llm_string_response():
    """Create a mock LLM that returns string responses directly."""
    return _make_mock_llm("Test string response with citation [1].")


@pytest.fixture
def sample_search_results():
    """Sample search results for testing."""
    return [
        {
            "title": "Test Article 1",
            "link": "https://example.com/article1",
            "snippet": "This is the first test article snippet.",
            "full_content": "This is the full content of the first test article. It contains detailed information about the topic.",
        },
        {
            "title": "Test Article 2",
            "link": "https://example.org/article2",
            "snippet": "Second article snippet with different information.",
            "full_content": "Full content of the second article with more details and context.",
        },
        {
            "title": "Test Article 3",
            "link": "https://test.com/article3",
            "snippet": "Third article providing additional context.",
            "full_content": "The third article contains supplementary information relevant to the query.",
        },
    ]


@pytest.fixture
def empty_search_results():
    """Empty search results."""
    return []


@pytest.fixture
def string_search_results():
    """Search results as a string (edge case)."""
    return "No structured results available"


@pytest.fixture
def settings_with_fact_checking():
    """Settings snapshot with fact-checking enabled."""
    return {"general.enable_fact_checking": True}


@pytest.fixture
def settings_without_fact_checking():
    """Settings snapshot with fact-checking disabled."""
    return {"general.enable_fact_checking": False}


@pytest.fixture
def settings_with_output_instructions():
    """Settings snapshot with custom output instructions."""
    return {
        "general.output_instructions": "Respond in formal academic English",
        "general.enable_fact_checking": True,
    }


@pytest.fixture
def settings_with_dict_value():
    """Settings snapshot with dict-wrapped values (legacy format)."""
    return {
        "general.enable_fact_checking": {"value": True},
        "general.output_instructions": {"value": "Be concise"},
    }


@pytest.fixture
def sample_previous_knowledge():
    """Sample previous knowledge for follow-up analysis."""
    return """Based on previous research:
- The topic was first studied in 1995 [1]
- Key findings include improved performance metrics [2]
- Recent developments show promising results [3]"""


@pytest.fixture
def name_search_results():
    """Search results containing name information."""
    return [
        {
            "title": "Biography of John Michael Smith",
            "link": "https://example.com/bio",
            "snippet": "John Smith was a renowned scientist.",
            "full_content": "John Michael Smith (1950-2020) was a renowned scientist who contributed significantly to the field. His full name was John Michael William Smith.",
        },
        {
            "title": "Awards and Recognition",
            "link": "https://example.com/awards",
            "snippet": "Dr. J. M. Smith received numerous awards.",
            "full_content": "Dr. John M. Smith received numerous awards including the Nobel Prize. Smith's work influenced many researchers.",
        },
    ]


@pytest.fixture
def dimension_search_results():
    """Search results containing dimension/measurement information."""
    return [
        {
            "title": "Building Specifications",
            "link": "https://example.com/building",
            "snippet": "The tower stands at 324 meters tall.",
            "full_content": "The Eiffel Tower stands at 324 meters (1,063 feet) tall. Its base is 125 meters wide. The structure weighs approximately 10,100 tons.",
        },
    ]


@pytest.fixture
def score_search_results():
    """Search results containing score/game information."""
    return [
        {
            "title": "Match Results",
            "link": "https://example.com/match",
            "snippet": "The final score was 3-2.",
            "full_content": "In the championship final, Team A defeated Team B with a final score of 3-2. The halftime score was 1-1.",
        },
    ]


@pytest.fixture
def temporal_search_results():
    """Search results containing date/year information."""
    return [
        {
            "title": "Historical Timeline",
            "link": "https://example.com/history",
            "snippet": "The company was founded in 1998.",
            "full_content": "The company was founded in 1998 by two engineers. It went public in 2004 and was acquired in 2015.",
        },
    ]