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.

Add tests for findings repository and expand diversity explorer tests

LearningCircuit2891c890f49b
diff --git a/tests/advanced_search_system/candidate_exploration/test_diversity_explorer.py b/tests/advanced_search_system/candidate_exploration/test_diversity_explorer.py
index 97e85a6297..5ad4cc6412 100644
--- a/tests/advanced_search_system/candidate_exploration/test_diversity_explorer.py
+++ b/tests/advanced_search_system/candidate_exploration/test_diversity_explorer.py
@@ -393,3 +393,520 @@ class TestDiversityExplorerSimilarityDedup:
# Should detect similarity
assert isinstance(is_similar, bool)
+
+
+class TestDetermineCategoryMethod:
+ """Tests for _determine_category method."""
+
+ def test_categorizes_mountain_terms(self, mock_search_engine, mock_model):
+ """Test categorizing mountain-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ # Names must contain "mountain", "peak", "summit", or "hill" (case-insensitive)
+ for name in ["Big Mountain View", "Rocky Peak", "Grand Summit"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "mountain"
+
+ def test_categorizes_water_terms(self, mock_search_engine, mock_model):
+ """Test categorizing water-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["Lake Superior", "Colorado River", "Clear Creek"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "water"
+
+ def test_categorizes_park_terms(self, mock_search_engine, mock_model):
+ """Test categorizing park-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["National Park", "Black Forest", "Nature Reserve"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "park"
+
+ def test_categorizes_trail_terms(self, mock_search_engine, mock_model):
+ """Test categorizing trail-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidate = Candidate(name="Appalachian Trail")
+ result = explorer._determine_category(candidate)
+ assert result == "trail"
+
+ def test_categorizes_canyon_terms(self, mock_search_engine, mock_model):
+ """Test categorizing canyon-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["Grand Canyon", "Box Gorge", "Hidden Valley"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "canyon"
+
+ def test_categorizes_viewpoint_terms(self, mock_search_engine, mock_model):
+ """Test categorizing viewpoint-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["Eagle Cliff", "Sunset Bluff", "Scenic Overlook"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "viewpoint"
+
+ def test_categorizes_coastal_terms(self, mock_search_engine, mock_model):
+ """Test categorizing coastal-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["Paradise Island", "Sunny Beach", "Rocky Coast"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "coastal"
+
+ def test_categorizes_place_terms(self, mock_search_engine, mock_model):
+ """Test categorizing place-related names."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ for name in ["New York City", "Small Town", "Jefferson County"]:
+ candidate = Candidate(name=name)
+ result = explorer._determine_category(candidate)
+ assert result == "place"
+
+ def test_categorizes_other_terms(self, mock_search_engine, mock_model):
+ """Test categorizing unknown names as other."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidate = Candidate(name="Some Random Name")
+ result = explorer._determine_category(candidate)
+ assert result == "other"
+
+
+class TestCategorizeCandidatesMethod:
+ """Tests for _categorize_candidates method."""
+
+ def test_updates_category_counts(self, mock_search_engine, mock_model):
+ """Test that category counts are updated."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidates = [
+ Candidate(name="Hood Mountain"),
+ Candidate(name="Lake Tahoe"),
+ Candidate(name="Another Mountain Peak"),
+ ]
+
+ explorer._categorize_candidates(candidates)
+
+ assert explorer.category_counts["mountain"] == 2
+ assert explorer.category_counts["water"] == 1
+
+ def test_adds_metadata_to_candidates(self, mock_search_engine, mock_model):
+ """Test that metadata is added to candidates."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidate = Candidate(name="Grand Canyon")
+ explorer._categorize_candidates([candidate])
+
+ assert candidate.metadata is not None
+ assert candidate.metadata["diversity_category"] == "canyon"
+
+ def test_updates_diversity_categories_set(
+ self, mock_search_engine, mock_model
+ ):
+ """Test that diversity_categories set is updated."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidates = [
+ Candidate(name="Test Mountain"),
+ Candidate(name="Lake Test"),
+ ]
+
+ explorer._categorize_candidates(candidates)
+
+ assert "mountain" in explorer.diversity_categories
+ assert "water" in explorer.diversity_categories
+
+
+class TestFindUnderrepresentedCategories:
+ """Tests for _find_underrepresented_categories method."""
+
+ def test_empty_counts_returns_empty(self, mock_search_engine, mock_model):
+ """Test that empty counts returns empty list."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ result = explorer._find_underrepresented_categories()
+
+ assert result == []
+
+ def test_finds_underrepresented(self, mock_search_engine, mock_model):
+ """Test finding underrepresented categories."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ # Set up uneven distribution
+ explorer.category_counts["mountain"] = 10
+ explorer.category_counts["water"] = 10
+ explorer.category_counts["trail"] = 1 # Underrepresented
+
+ result = explorer._find_underrepresented_categories()
+
+ assert "trail" in result
+ assert "mountain" not in result
+ assert "water" not in result
+
+
+class TestGenerateDiversityQueries:
+ """Tests for _generate_diversity_queries method."""
+
+ def test_generates_queries_for_missing_categories(
+ self, mock_search_engine, mock_model
+ ):
+ """Test generating queries for missing categories."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ # Create candidates with only one category
+ candidate = Candidate(name="Mount Test")
+ candidate.metadata = {"diversity_category": "mountain"}
+ candidates = [candidate]
+
+ queries = explorer._generate_diversity_queries("hiking", candidates)
+
+ assert len(queries) > 0
+
+ def test_uses_entity_type_as_base(self, mock_search_engine, mock_model):
+ """Test using entity type as base for queries."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ queries = explorer._generate_diversity_queries(
+ "base query", [], entity_type="locations"
+ )
+
+ # Entity type should be used in queries
+ for query in queries:
+ assert "locations" in query.lower() or "location" in query.lower()
+
+
+class TestFilterForDiversity:
+ """Tests for _filter_for_diversity method."""
+
+ def test_filters_over_limit_category(self, mock_search_engine, mock_model):
+ """Test filtering candidates from over-limit categories."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model, category_limit=2
+ )
+ explorer.category_counts["mountain"] = 2 # At limit
+
+ new_candidates = [Candidate(name="Another Mountain Peak")]
+ existing = []
+
+ filtered = explorer._filter_for_diversity(new_candidates, existing)
+
+ assert len(filtered) == 0
+
+ def test_keeps_candidates_under_limit(self, mock_search_engine, mock_model):
+ """Test keeping candidates from under-limit categories."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine,
+ model=mock_model,
+ category_limit=10,
+ )
+ explorer.category_counts["water"] = 1 # Under limit
+
+ new_candidates = [Candidate(name="New Lake")]
+ existing = []
+
+ filtered = explorer._filter_for_diversity(new_candidates, existing)
+
+ assert len(filtered) == 1
+
+
+class TestIsSufficientlyDifferent:
+ """Tests for _is_sufficiently_different method."""
+
+ def test_similar_candidates_returns_false(
+ self, mock_search_engine, mock_model
+ ):
+ """Test that very similar candidates return False."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine,
+ model=mock_model,
+ similarity_threshold=0.3,
+ )
+
+ candidate = Candidate(name="Mount Rainier Washington")
+ existing = [Candidate(name="Mount Rainier Peak")]
+
+ result = explorer._is_sufficiently_different(candidate, existing)
+
+ assert result is False
+
+ def test_different_candidates_returns_true(
+ self, mock_search_engine, mock_model
+ ):
+ """Test that different candidates return True."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine,
+ model=mock_model,
+ similarity_threshold=0.8,
+ )
+
+ candidate = Candidate(name="Grand Canyon Arizona")
+ existing = [Candidate(name="Lake Superior Michigan")]
+
+ result = explorer._is_sufficiently_different(candidate, existing)
+
+ assert result is True
+
+ def test_empty_existing_returns_true(self, mock_search_engine, mock_model):
+ """Test that empty existing list returns True."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidate = Candidate(name="Test Place")
+
+ result = explorer._is_sufficiently_different(candidate, [])
+
+ assert result is True
+
+
+class TestFinalDiversitySelection:
+ """Tests for _final_diversity_selection method."""
+
+ def test_empty_candidates_returns_empty(
+ self, mock_search_engine, mock_model
+ ):
+ """Test that empty candidates returns empty list."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ result = explorer._final_diversity_selection([])
+
+ assert result == []
+
+ def test_selects_from_multiple_categories(
+ self, mock_search_engine, mock_model
+ ):
+ """Test selecting from multiple categories."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model, max_candidates=4
+ )
+
+ candidates = []
+ for i in range(3):
+ candidate = Candidate(name=f"Mountain {i}")
+ candidate.metadata = {"diversity_category": "mountain"}
+ candidates.append(candidate)
+
+ for i in range(3):
+ candidate = Candidate(name=f"Lake {i}")
+ candidate.metadata = {"diversity_category": "water"}
+ candidates.append(candidate)
+
+ result = explorer._final_diversity_selection(candidates)
+
+ # Should have representation from both categories
+ categories = [c.metadata.get("diversity_category") for c in result]
+ assert "mountain" in categories
+ assert "water" in categories
+
+
+class TestGenerateExplorationQueries:
+ """Tests for generate_exploration_queries method."""
+
+ def test_returns_list(self, mock_search_engine, mock_model):
+ """Test that it returns a list."""
+ from local_deep_research.advanced_search_system.candidate_exploration.diversity_explorer import (
+ DiversityExplorer,
+ )
+ from local_deep_research.advanced_search_system.candidates.base_candidate import (
+ Candidate,
+ )
+
+ explorer = DiversityExplorer(
+ search_engine=mock_search_engine, model=mock_model
+ )
+
+ candidates = [Candidate(name="Test")]
+
+ result = explorer.generate_exploration_queries("base query", candidates)
+
+ assert isinstance(result, list)
diff --git a/tests/advanced_search_system/findings/test_findings_repository.py b/tests/advanced_search_system/findings/test_findings_repository.py
new file mode 100644
index 0000000000..1fb103ca77
--- /dev/null
+++ b/tests/advanced_search_system/findings/test_findings_repository.py
@@ -0,0 +1,519 @@
+"""
+Tests for advanced_search_system/findings/repository.py
+
+Tests cover:
+- format_links function
+- FindingsRepository initialization
+- add_finding method
+- get_findings method
+- clear_findings method
+- add_documents method
+- set_questions_by_iteration method
+- format_findings_to_text method
+- synthesize_findings method
+"""
+
+from unittest.mock import Mock, patch
+
+from langchain_core.documents import Document
+
+
+class TestFormatLinks:
+ """Tests for format_links function."""
+
+ def test_formats_single_link(self):
+ """Test formatting a single link."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ format_links,
+ )
+
+ links = [{"title": "Test Page", "url": "https://example.com"}]
+
+ result = format_links(links)
+
+ assert "1. Test Page" in result
+ assert "https://example.com" in result
+
+ def test_formats_multiple_links(self):
+ """Test formatting multiple links."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ format_links,
+ )
+
+ links = [
+ {"title": "Page 1", "url": "https://example1.com"},
+ {"title": "Page 2", "url": "https://example2.com"},
+ ]
+
+ result = format_links(links)
+
+ assert "1. Page 1" in result
+ assert "2. Page 2" in result
+
+ def test_handles_empty_list(self):
+ """Test handling empty list."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ format_links,
+ )
+
+ result = format_links([])
+
+ assert result == ""
+
+
+class TestFindingsRepositoryInit:
+ """Tests for FindingsRepository initialization."""
+
+ def test_initializes_with_model(self):
+ """Test initialization with model."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ repo = FindingsRepository(mock_model)
+
+ assert repo.model is mock_model
+ assert repo.findings == {}
+ assert repo.documents == []
+ assert repo.questions_by_iteration == {}
+
+ def test_inherits_from_base_findings(self):
+ """Test inheritance from BaseFindingsRepository."""
+ from local_deep_research.advanced_search_system.findings.base_findings import (
+ BaseFindingsRepository,
+ )
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ assert issubclass(FindingsRepository, BaseFindingsRepository)
+
+
+class TestAddFinding:
+ """Tests for add_finding method."""
+
+ def test_adds_string_finding(self):
+ """Test adding a string finding."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ repo.add_finding("test query", "This is a finding")
+
+ findings = repo.findings["test query"]
+ assert len(findings) == 1
+ assert findings[0]["content"] == "This is a finding"
+ assert findings[0]["phase"] == "Synthesis"
+
+ def test_adds_dict_finding(self):
+ """Test adding a dictionary finding."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ finding = {"phase": "Research", "content": "Dict finding"}
+ repo.add_finding("test query", finding)
+
+ findings = repo.findings["test query"]
+ assert len(findings) == 1
+ assert findings[0]["phase"] == "Research"
+
+ def test_creates_synthesis_for_final_synthesis(self):
+ """Test that final synthesis creates a copy."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ finding = {"phase": "Final synthesis", "content": "Final content"}
+ repo.add_finding("test query", finding)
+
+ assert "test query_synthesis" in repo.findings
+
+ def test_appends_multiple_findings(self):
+ """Test appending multiple findings for same query."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ repo.add_finding("query", "Finding 1")
+ repo.add_finding("query", "Finding 2")
+
+ assert len(repo.findings["query"]) == 2
+
+
+class TestGetFindings:
+ """Tests for get_findings method."""
+
+ def test_returns_findings_for_query(self):
+ """Test getting findings for existing query."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ repo.add_finding("query", "Finding")
+
+ result = repo.get_findings("query")
+
+ assert len(result) == 1
+
+ def test_returns_empty_for_unknown_query(self):
+ """Test getting findings for unknown query."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+
+ result = repo.get_findings("unknown")
+
+ assert result == []
+
+
+class TestClearFindings:
+ """Tests for clear_findings method."""
+
+ def test_clears_findings_for_query(self):
+ """Test clearing findings for a query."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ repo.add_finding("query", "Finding")
+ repo.clear_findings("query")
+
+ assert "query" not in repo.findings
+
+ def test_handles_nonexistent_query(self):
+ """Test clearing findings for nonexistent query."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ # Should not raise
+ repo.clear_findings("nonexistent")
+
+
+class TestAddDocuments:
+ """Tests for add_documents method."""
+
+ def test_adds_documents(self):
+ """Test adding documents."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ docs = [Document(page_content="Doc 1"), Document(page_content="Doc 2")]
+
+ repo.add_documents(docs)
+
+ assert len(repo.documents) == 2
+
+ def test_extends_existing_documents(self):
+ """Test that documents are extended, not replaced."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ repo.add_documents([Document(page_content="Doc 1")])
+ repo.add_documents([Document(page_content="Doc 2")])
+
+ assert len(repo.documents) == 2
+
+
+class TestSetQuestionsByIteration:
+ """Tests for set_questions_by_iteration method."""
+
+ def test_sets_questions(self):
+ """Test setting questions by iteration."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ questions = {1: ["Q1", "Q2"], 2: ["Q3"]}
+
+ repo.set_questions_by_iteration(questions)
+
+ assert repo.questions_by_iteration == questions
+
+ def test_makes_a_shallow_copy(self):
+ """Test that a shallow copy is made (top-level dict is copied)."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ repo = FindingsRepository(Mock())
+ questions = {1: ["Q1"]}
+
+ repo.set_questions_by_iteration(questions)
+ # Adding a new key to original dict should not affect repo
+ questions[2] = ["Q3"]
+
+ # New key should not be in repo (shallow copy of dict)
+ assert 2 not in repo.questions_by_iteration
+
+
+class TestFormatFindingsToText:
+ """Tests for format_findings_to_text method."""
+
+ @patch(
+ "local_deep_research.advanced_search_system.findings.repository.format_findings"
+ )
+ def test_formats_findings(self, mock_format):
+ """Test formatting findings to text."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_format.return_value = "Formatted report"
+
+ repo = FindingsRepository(Mock())
+ findings_list = [{"phase": "Research", "content": "Finding"}]
+
+ result = repo.format_findings_to_text(
+ findings_list, "Synthesized content"
+ )
+
+ assert result == "Formatted report"
+ mock_format.assert_called_once()
+
+ @patch(
+ "local_deep_research.advanced_search_system.findings.repository.format_findings"
+ )
+ def test_returns_fallback_on_error(self, mock_format):
+ """Test fallback when formatting fails."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_format.side_effect = Exception("Format error")
+
+ repo = FindingsRepository(Mock())
+
+ result = repo.format_findings_to_text([], "Content")
+
+ assert "Error during final formatting" in result
+ assert "Content" in result
+
+
+class TestSynthesizeFindings:
+ """Tests for synthesize_findings method."""
+
+ def test_synthesizes_with_model(self):
+ """Test synthesizing findings with model."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Synthesized answer")
+
+ repo = FindingsRepository(mock_model)
+ findings = [{"content": "Finding 1"}, {"content": "Finding 2"}]
+
+ result = repo.synthesize_findings(
+ query="Test query", sub_queries=["Sub 1"], findings=findings
+ )
+
+ assert result == "Synthesized answer"
+
+ def test_synthesizes_string_findings(self):
+ """Test synthesizing string findings."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Synthesized")
+
+ repo = FindingsRepository(mock_model)
+ findings = ["Finding 1", "Finding 2"]
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=findings
+ )
+
+ assert result == "Synthesized"
+
+ def test_uses_accumulated_knowledge_when_provided(self):
+ """Test using provided accumulated knowledge."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Result")
+
+ repo = FindingsRepository(mock_model)
+
+ repo.synthesize_findings(
+ query="Query",
+ sub_queries=[],
+ findings=[],
+ accumulated_knowledge="Existing knowledge",
+ )
+
+ # Verify that model was invoked
+ mock_model.invoke.assert_called_once()
+
+ @patch(
+ "local_deep_research.advanced_search_system.findings.repository.format_findings"
+ )
+ def test_old_formatting_mode(self, mock_format):
+ """Test old formatting mode."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_format.return_value = "Old formatted result"
+
+ repo = FindingsRepository(Mock())
+ findings = ["Finding 1"]
+
+ result = repo.synthesize_findings(
+ query="Query",
+ sub_queries=[],
+ findings=findings,
+ old_formatting=True,
+ )
+
+ assert result == "Old formatted result"
+ mock_format.assert_called_once()
+
+ def test_handles_model_error(self):
+ """Test handling model invocation error."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.side_effect = Exception("Model error")
+
+ repo = FindingsRepository(mock_model)
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=[{"content": "Finding"}]
+ )
+
+ assert "Error" in result
+
+ def test_handles_timeout_error(self):
+ """Test handling timeout error."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.side_effect = Exception("Request timed out")
+
+ repo = FindingsRepository(mock_model)
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=[{"content": "Finding"}]
+ )
+
+ assert "timeout" in result.lower() or "Error" in result
+
+ def test_handles_token_limit_error(self):
+ """Test handling token limit error."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.side_effect = Exception("Token limit exceeded")
+
+ repo = FindingsRepository(mock_model)
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=[{"content": "Finding"}]
+ )
+
+ assert "Error" in result
+
+ def test_truncates_long_knowledge(self):
+ """Test that very long knowledge is truncated."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Result")
+
+ repo = FindingsRepository(mock_model)
+ # Create very long finding content
+ long_content = "A" * 30000
+ findings = [{"content": long_content}]
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=findings
+ )
+
+ # Should still succeed
+ assert result == "Result"
+
+ def test_handles_string_response(self):
+ """Test handling string response from model."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = "String response"
+
+ repo = FindingsRepository(mock_model)
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=[{"content": "Finding"}]
+ )
+
+ assert "String response" in result
+
+
+class TestEdgeCases:
+ """Tests for edge cases."""
+
+ def test_empty_findings_list(self):
+ """Test with empty findings list."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Empty result")
+
+ repo = FindingsRepository(mock_model)
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=[]
+ )
+
+ assert "Empty result" in result
+
+ def test_mixed_findings_types(self):
+ """Test with mixed finding types."""
+ from local_deep_research.advanced_search_system.findings.repository import (
+ FindingsRepository,
+ )
+
+ mock_model = Mock()
+ mock_model.invoke.return_value = Mock(content="Mixed result")
+
+ repo = FindingsRepository(mock_model)
+ findings = [{"content": "Dict finding"}, "String finding"]
+
+ result = repo.synthesize_findings(
+ query="Query", sub_queries=[], findings=findings
+ )
+
+ assert result == "Mixed result"