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.

13.6 KB
Raw Blame
"""
Regression tests for issue #4615.

Several call sites consumed ``response.content`` directly and called string
methods (``.strip()``, ``.split()``, ``len()``) on it. When a provider returns
content in LIST form (Anthropic content blocks from extended-thinking/tool-use),
this raised AttributeError/TypeError. The fix routes those reads through
``get_llm_response_text``, which coerces non-string content to a string.

These tests build fake responses whose ``.content`` is a LIST (simulating
Anthropic content blocks) and confirm the corrected sites no longer raise and
return the expected string.
"""

from unittest.mock import Mock

import pytest


def _list_content_response(text):
    """Build a fake LLM response with Anthropic-style list content blocks."""
    response = Mock()
    response.content = [{"type": "text", "text": text}]
    return response


class TestGetLLMResponseTextBlocks:
    """Root-cause regression for #4615: get_llm_response_text must EXTRACT the
    text from list-type content blocks, not stringify the list to its repr.

    These assert *clean* extraction (exact equality), so they fail against the
    old ``str(raw)`` behavior — which returned ``"[{'type': 'text', ...}]"``.
    """

    def _msg(self, content):
        response = Mock()
        response.content = content
        return response

    def test_single_text_block_extracted_clean(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        out = get_llm_response_text(
            self._msg([{"type": "text", "text": "Paris"}])
        )
        assert out == "Paris"
        assert "'type'" not in out and "[{" not in out  # no repr leakage

    def test_newlines_preserved_so_split_works(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        out = get_llm_response_text(
            self._msg([{"type": "text", "text": "A\nB\nC"}])
        )
        # The #4615 corruption turned "\n" into literal "\\n", collapsing splits.
        assert out.split("\n") == ["A", "B", "C"]

    def test_multiple_text_blocks_joined(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        out = get_llm_response_text(
            self._msg(
                [
                    {"type": "text", "text": "Hello "},
                    {"type": "text", "text": "world"},
                ]
            )
        )
        assert out == "Hello world"

    def test_tool_use_block_skipped(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        out = get_llm_response_text(
            self._msg(
                [
                    {"type": "text", "text": "answer"},
                    {"type": "tool_use", "name": "search", "input": {"q": "x"}},
                ]
            )
        )
        assert out == "answer"

    def test_empty_list_returns_empty_string(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        assert get_llm_response_text(self._msg([])) == ""

    def test_plain_string_content_unchanged(self):
        from local_deep_research.utilities.json_utils import (
            get_llm_response_text,
        )

        # String content must pass straight through (block coercion only
        # applies to list content); no leading/trailing space to avoid the
        # helper's existing strip behavior masking the check.
        assert get_llm_response_text(self._msg("hello world")) == "hello world"


class TestFollowUpContextManagerListContent:
    """followup_context_manager.py sites (issue #4615)."""

    def _handler(self, text):
        from local_deep_research.advanced_search_system.knowledge.followup_context_manager import (
            FollowUpContextHandler,
        )

        model = Mock()
        model.invoke.return_value = _list_content_response(text)
        return FollowUpContextHandler(model)

    def test_extract_entities_does_not_raise_on_list_content(self):
        handler = self._handler("Paris")
        research_data = {
            "formatted_findings": "Some prior findings about Paris."
        }

        entities = handler._extract_entities(research_data)

        assert isinstance(entities, list)
        # Coerced content preserves the underlying text — cleanly, not as the
        # list's repr (the #4615 data-corruption regression).
        assert "Paris" in entities
        assert not any("'type'" in e or "[{" in e for e in entities)

    def test_generate_summary_does_not_raise_on_list_content(self):
        handler = self._handler("A concise summary.")

        summary = handler._generate_summary(
            findings="x" * 5000,  # long enough to trigger an LLM call
            query="follow-up question",
        )

        assert isinstance(summary, str)
        assert "A concise summary." in summary
        assert "'type'" not in summary and "[{" not in summary

    def test_identify_gaps_does_not_raise_on_list_content(self):
        handler = self._handler("Missing detail about funding.")
        research_data = {"formatted_findings": "Some prior findings."}

        gaps = handler.identify_gaps(research_data, "follow-up question")

        assert isinstance(gaps, list)
        assert any("funding" in gap for gap in gaps)
        assert not any("'type'" in g or "[{" in g for g in gaps)


class TestDecompositionQuestionListContent:
    """decomposition_question.py sites (issue #4615)."""

    def test_generate_questions_does_not_raise_on_list_content(self):
        from local_deep_research.advanced_search_system.questions.decomposition_question import (
            DecompositionQuestionGenerator,
        )

        model = Mock()
        model.invoke.return_value = _list_content_response(
            "What is X?\nHow does X work?\nWhy is X important?"
        )
        generator = DecompositionQuestionGenerator(model)

        questions = generator.generate_questions("X technology", context="")

        assert isinstance(questions, list)
        # Non-tautological: the pre-fix path crashes and falls back to generic
        # default questions, so the LLM's actual questions would be absent and
        # the repr ("'type'") would leak. Both checks fail on the old behavior.
        assert any("How does X work?" in q for q in questions)
        assert not any("'type'" in q or "[{" in q for q in questions)


class TestBaseExplorerListContent:
    """base_explorer.py sites (issue #4615)."""

    def _explorer(self, text):
        from local_deep_research.advanced_search_system.candidate_exploration.base_explorer import (
            BaseCandidateExplorer,
        )

        class _TestExplorer(BaseCandidateExplorer):
            def explore(
                self, initial_query, constraints=None, entity_type=None
            ):
                pass

            def generate_exploration_queries(
                self, base_query, found_candidates, constraints=None
            ):
                return []

        model = Mock()
        model.invoke.return_value = _list_content_response(text)
        return _TestExplorer(
            model=model,
            search_engine=Mock(),
            max_candidates=50,
            max_search_time=60.0,
        )

    def test_generate_answer_candidates_does_not_raise_on_list_content(self):
        explorer = self._explorer("Alice Smith")

        answers = explorer._generate_answer_candidates(
            "Who discovered X?", "search content"
        )

        assert isinstance(answers, list)
        assert "Alice Smith" in answers
        assert not any("'type'" in a or "[{" in a for a in answers)

    def test_extract_entity_names_does_not_raise_on_list_content(self):
        explorer = self._explorer("Marie Curie")

        names = explorer._extract_entity_names("Some text with a name.")

        assert isinstance(names, list)
        assert "Marie Curie" in names
        assert not any("'type'" in n or "[{" in n for n in names)


class TestCandidateQueryGenerationListContent:
    """Candidate explorer query-generation sites missed by #4644."""

    @staticmethod
    def _adaptive_explorer(text):
        from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
            AdaptiveExplorer,
        )

        model = Mock()
        model.invoke.return_value = _list_content_response(text)
        return AdaptiveExplorer(model=model, search_engine=Mock())

    def test_adaptive_synonym_query_extracts_text_block(self):
        explorer = self._adaptive_explorer("alternative terminology")

        query = explorer._synonym_expansion_query("original term")

        assert query == "alternative terminology"

    def test_adaptive_related_query_extracts_text_block(self):
        explorer = self._adaptive_explorer("adjacent topic")

        query = explorer._related_terms_query("original term", [])

        assert query == "adjacent topic"

    def test_parallel_variations_extract_text_block(self):
        from local_deep_research.advanced_search_system.candidate_exploration.parallel_explorer import (
            ParallelExplorer,
        )

        model = Mock()
        model.invoke.return_value = _list_content_response(
            "1. first query\n2. second query"
        )
        explorer = ParallelExplorer(model=model, search_engine=Mock())

        assert explorer._generate_query_variations("original") == [
            "first query",
            "second query",
        ]


class TestTopicOrganizationStrategyListContent:
    """topic_organization_strategy.py sites (issue #4615).

    The file already imported ``get_llm_response_text`` and used it at one
    site; the seven others still read ``.content`` directly.
    """

    @pytest.fixture(autouse=True)
    def milestone_level(self):
        # config_logger() registers MILESTONE at app startup; these tests call
        # the strategy directly, so register it the same way it does.
        from loguru import logger

        try:
            logger.level("MILESTONE", no=26, color="<magenta><bold>")
        except ValueError:
            pass

    @staticmethod
    def _strategy(blocks, **kwargs):
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        response = Mock()
        response.content = blocks
        model = Mock()
        model.invoke.return_value = response
        return TopicOrganizationStrategy(
            search=Mock(), model=model, citation_handler=Mock(), **kwargs
        )

    @staticmethod
    def _source(i=1):
        return {
            "title": f"T{i}",
            "link": f"https://example.com/{i}",
            "snippet": f"S{i}",
        }

    def _topic(self, sources=2):
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        topic = Topic(id="t1", title="Topic1", lead_source=self._source(1))
        for j in range(sources - 1):
            topic.add_supporting_source(self._source(100 + j))
        return topic

    def test_extract_topics_honours_irrelevant_marker(self):
        # "d" marks the source irrelevant. Reading the block list raw makes
        # response_text the list's repr, which is neither "d" nor an int, so
        # the source was kept as a topic instead of being skipped.
        strategy = self._strategy([{"type": "text", "text": "d"}])

        topics = strategy._extract_topics_from_sources(
            [self._source(1)], "query"
        )

        assert topics == []

    def test_reselect_lead_for_single_topic_parses_index(self):
        strategy = self._strategy([{"type": "text", "text": "1"}])
        topic = self._topic(sources=2)
        original_lead = topic.lead_source["title"]

        changed = strategy._reselect_lead_for_single_topic(topic, [topic])

        assert changed is True
        assert topic.lead_source["title"] != original_lead

    def test_reselect_lead_sources_parses_index(self):
        strategy = self._strategy([{"type": "text", "text": "1"}])
        topic = self._topic(sources=2)
        original_lead = topic.lead_source["title"]

        strategy._reselect_lead_sources([topic])

        assert topic.lead_source["title"] != original_lead

    def test_filter_topics_by_relevance_ignores_thinking_blocks(self):
        # Extended thinking puts the model's reasoning in a separate block.
        # The raw read stringified every block, so "yes" appearing anywhere in
        # the reasoning kept a topic the model had actually rejected.
        strategy = self._strategy(
            [
                {
                    "type": "thinking",
                    "thinking": "Could be yes, but it is off topic.",
                },
                {"type": "text", "text": "no"},
            ]
        )

        assert strategy._filter_topics_by_relevance([self._topic()], "q") == []

    def test_generate_refinement_question_returns_text(self):
        strategy = self._strategy(
            [{"type": "text", "text": "What about funding?"}],
            enable_refinement=True,
        )

        question = strategy._generate_refinement_question(
            [self._topic()], "query"
        )

        assert question == "What about funding?"

    def test_generate_topic_based_text_does_not_raise(self):
        # These two sites did not wrap the value in str(), so the list reached
        # "\n".join() and raised TypeError rather than degrading.
        strategy = self._strategy([{"type": "text", "text": "Body text."}])

        text = strategy._generate_topic_based_text([self._topic()], "query")

        assert "Body text." in text
        assert "'type'" not in text and "[{" not in text