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 advanced_search_system base strategy pattern

LearningCircuita8360e345885
diff --git a/tests/advanced_search_system/strategies/__init__.py b/tests/advanced_search_system/strategies/__init__.py
new file mode 100644
index 0000000000..3d15968ff0
--- /dev/null
+++ b/tests/advanced_search_system/strategies/__init__.py
@@ -0,0 +1 @@
+"""Tests for advanced search system strategies."""
diff --git a/tests/advanced_search_system/strategies/test_base_strategy.py b/tests/advanced_search_system/strategies/test_base_strategy.py
new file mode 100644
index 0000000000..f2a8aebfd0
--- /dev/null
+++ b/tests/advanced_search_system/strategies/test_base_strategy.py
@@ -0,0 +1,291 @@
+"""
+Tests for the BaseSearchStrategy class.
+
+Tests cover:
+- Initialization
+- Settings access
+- Progress callbacks
+- Error handling
+"""
+
+from unittest.mock import Mock
+
+
+class ConcreteTestStrategy:
+ """Concrete implementation for testing the base strategy pattern."""
+
+ def __init__(
+ self,
+ all_links_of_system=None,
+ settings_snapshot=None,
+ questions_by_iteration=None,
+ search_original_query=True,
+ ):
+ """Initialize the test strategy."""
+ self.progress_callback = None
+ self.questions_by_iteration = (
+ questions_by_iteration if questions_by_iteration is not None else {}
+ )
+ self.all_links_of_system = (
+ all_links_of_system if all_links_of_system is not None else []
+ )
+ self.settings_snapshot = settings_snapshot or {}
+ self.search_original_query = search_original_query
+ self.search = None
+
+ def get_setting(self, key, default=None):
+ """Get a setting value from the snapshot."""
+ if key in self.settings_snapshot:
+ value = self.settings_snapshot[key]
+ if isinstance(value, dict) and "value" in value:
+ return value["value"]
+ return value
+ return default
+
+ def set_progress_callback(self, callback):
+ """Set a callback function to receive progress updates."""
+ self.progress_callback = callback
+
+ def _update_progress(self, message, progress_percent=None, metadata=None):
+ """Send a progress update via the callback if available."""
+ if self.progress_callback:
+ self.progress_callback(message, progress_percent, metadata or {})
+
+ def _validate_search_engine(self):
+ """Validate that the search engine is available."""
+ if not hasattr(self, "search") or self.search is None:
+ self._update_progress(
+ "Error: No search engine available",
+ 100,
+ {"phase": "error", "error": "No search engine available"},
+ )
+ return False
+ return True
+
+ def _handle_search_error(self, error, question, progress_base):
+ """Handle errors during search execution."""
+ self._update_progress(
+ f"Error during search: {error!s}",
+ progress_base + 2,
+ {"phase": "search_error", "error": str(error)},
+ )
+ return []
+
+ def analyze_topic(self, query):
+ """Test implementation of analyze_topic."""
+ return {"findings": [], "iterations": 0}
+
+
+class TestBaseStrategyInit:
+ """Tests for BaseSearchStrategy initialization."""
+
+ def test_init_with_defaults(self):
+ """Strategy initializes with default values."""
+ strategy = ConcreteTestStrategy()
+
+ assert strategy.all_links_of_system == []
+ assert strategy.questions_by_iteration == {}
+ assert strategy.settings_snapshot == {}
+ assert strategy.search_original_query is True
+ assert strategy.progress_callback is None
+
+ def test_init_with_all_links(self):
+ """Strategy initializes with provided links list."""
+ links = [{"url": "https://example.com"}]
+
+ strategy = ConcreteTestStrategy(all_links_of_system=links)
+
+ assert strategy.all_links_of_system == links
+
+ def test_init_with_settings_snapshot(self):
+ """Strategy initializes with settings snapshot."""
+ settings = {"search.iterations": {"value": 5}}
+
+ strategy = ConcreteTestStrategy(settings_snapshot=settings)
+
+ assert strategy.settings_snapshot == settings
+
+ def test_init_with_questions_by_iteration(self):
+ """Strategy initializes with questions by iteration."""
+ questions = {1: ["Q1", "Q2"], 2: ["Q3"]}
+
+ strategy = ConcreteTestStrategy(questions_by_iteration=questions)
+
+ assert strategy.questions_by_iteration == questions
+
+ def test_init_search_original_query_false(self):
+ """Strategy respects search_original_query=False."""
+ strategy = ConcreteTestStrategy(search_original_query=False)
+
+ assert strategy.search_original_query is False
+
+ def test_init_does_not_share_mutable_defaults(self):
+ """Each strategy instance gets its own mutable containers."""
+ strategy1 = ConcreteTestStrategy()
+ strategy2 = ConcreteTestStrategy()
+
+ strategy1.all_links_of_system.append({"url": "test"})
+
+ assert len(strategy2.all_links_of_system) == 0
+
+
+class TestGetSetting:
+ """Tests for get_setting method."""
+
+ def test_get_setting_with_value_dict(self):
+ """get_setting extracts value from dict structure."""
+ settings = {"test.key": {"value": "test_value", "ui_element": "text"}}
+ strategy = ConcreteTestStrategy(settings_snapshot=settings)
+
+ result = strategy.get_setting("test.key")
+
+ assert result == "test_value"
+
+ def test_get_setting_with_raw_value(self):
+ """get_setting returns raw value if not dict."""
+ settings = {"test.key": "raw_value"}
+ strategy = ConcreteTestStrategy(settings_snapshot=settings)
+
+ result = strategy.get_setting("test.key")
+
+ assert result == "raw_value"
+
+ def test_get_setting_returns_default_for_unknown(self):
+ """get_setting returns default for unknown key."""
+ strategy = ConcreteTestStrategy(settings_snapshot={})
+
+ result = strategy.get_setting("unknown.key", default="default_value")
+
+ assert result == "default_value"
+
+ def test_get_setting_returns_none_default(self):
+ """get_setting returns None as default if not specified."""
+ strategy = ConcreteTestStrategy(settings_snapshot={})
+
+ result = strategy.get_setting("unknown.key")
+
+ assert result is None
+
+
+class TestProgressCallback:
+ """Tests for progress callback functionality."""
+
+ def test_set_progress_callback(self):
+ """set_progress_callback stores the callback."""
+ strategy = ConcreteTestStrategy()
+ callback = Mock()
+
+ strategy.set_progress_callback(callback)
+
+ assert strategy.progress_callback is callback
+
+ def test_update_progress_calls_callback(self):
+ """_update_progress calls the callback with correct args."""
+ strategy = ConcreteTestStrategy()
+ callback = Mock()
+ strategy.set_progress_callback(callback)
+
+ strategy._update_progress("Test message", 50, {"phase": "test"})
+
+ callback.assert_called_once_with("Test message", 50, {"phase": "test"})
+
+ def test_update_progress_without_callback(self):
+ """_update_progress does nothing without callback."""
+ strategy = ConcreteTestStrategy()
+
+ # Should not raise
+ strategy._update_progress("Test message", 50)
+
+ def test_update_progress_with_empty_metadata(self):
+ """_update_progress uses empty dict for None metadata."""
+ strategy = ConcreteTestStrategy()
+ callback = Mock()
+ strategy.set_progress_callback(callback)
+
+ strategy._update_progress("Test message", 50, None)
+
+ callback.assert_called_once_with("Test message", 50, {})
+
+
+class TestValidateSearchEngine:
+ """Tests for _validate_search_engine method."""
+
+ def test_validate_returns_false_when_no_search(self):
+ """_validate_search_engine returns False when search is None."""
+ strategy = ConcreteTestStrategy()
+ strategy.search = None
+
+ result = strategy._validate_search_engine()
+
+ assert result is False
+
+ def test_validate_returns_true_when_search_exists(self):
+ """_validate_search_engine returns True when search exists."""
+ strategy = ConcreteTestStrategy()
+ strategy.search = Mock()
+
+ result = strategy._validate_search_engine()
+
+ assert result is True
+
+ def test_validate_sends_progress_on_failure(self):
+ """_validate_search_engine sends progress update on failure."""
+ strategy = ConcreteTestStrategy()
+ strategy.search = None
+ callback = Mock()
+ strategy.set_progress_callback(callback)
+
+ strategy._validate_search_engine()
+
+ callback.assert_called_once()
+ call_args = callback.call_args
+ assert "error" in call_args[0][2]
+
+
+class TestHandleSearchError:
+ """Tests for _handle_search_error method."""
+
+ def test_returns_empty_list(self):
+ """_handle_search_error returns empty list."""
+ strategy = ConcreteTestStrategy()
+
+ result = strategy._handle_search_error(
+ Exception("Test error"), "Test question", 50
+ )
+
+ assert result == []
+
+ def test_sends_progress_with_error(self):
+ """_handle_search_error sends progress with error info."""
+ strategy = ConcreteTestStrategy()
+ callback = Mock()
+ strategy.set_progress_callback(callback)
+
+ strategy._handle_search_error(
+ Exception("Test error"), "Test question", 50
+ )
+
+ callback.assert_called_once()
+ call_args = callback.call_args
+ assert "search_error" in str(call_args)
+
+
+class TestAnalyzeTopic:
+ """Tests for analyze_topic abstract method."""
+
+ def test_analyze_topic_returns_dict(self):
+ """analyze_topic returns a dictionary."""
+ strategy = ConcreteTestStrategy()
+
+ result = strategy.analyze_topic("test query")
+
+ assert isinstance(result, dict)
+
+ def test_analyze_topic_has_expected_keys(self):
+ """analyze_topic returns dict with expected keys."""
+ strategy = ConcreteTestStrategy()
+
+ result = strategy.analyze_topic("test query")
+
+ assert "findings" in result
+ assert "iterations" in result