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.

test: add tests for bounded TTLCache and classification caching

LearningCircuitb91a4eed78dd
diff --git a/tests/domain_classifier/test_classifier.py b/tests/domain_classifier/test_classifier.py
index 0bc19bcbbc..aa824b585a 100644
--- a/tests/domain_classifier/test_classifier.py
+++ b/tests/domain_classifier/test_classifier.py
@@ -2,6 +2,7 @@
from unittest.mock import MagicMock, patch
+from cachetools import TTLCache
from local_deep_research.domain_classifier.classifier import (
DOMAIN_CATEGORIES,
@@ -61,6 +62,13 @@ class TestDomainClassifierInit:
classifier = DomainClassifier(username="testuser")
assert classifier.llm is None
+ def test_classification_cache_initialized(self):
+ """Should initialize bounded TTLCache for classifications."""
+ classifier = DomainClassifier(username="testuser")
+ assert isinstance(classifier._classification_cache, TTLCache)
+ assert classifier._classification_cache.maxsize == 500
+ assert classifier._classification_cache.ttl == 300
+
class TestGetLlm:
"""Tests for _get_llm method."""
@@ -253,6 +261,64 @@ class TestGetClassification:
assert result is None
+ def test_caches_result_on_db_hit(self, mock_domain_classification):
+ """Should cache DB result in _classification_cache."""
+ classifier = DomainClassifier(username="testuser")
+
+ with patch(
+ "local_deep_research.domain_classifier.classifier.get_user_db_session"
+ ) as mock_session_ctx:
+ mock_session = MagicMock()
+ mock_session_ctx.return_value.__enter__ = MagicMock(
+ return_value=mock_session
+ )
+ mock_session_ctx.return_value.__exit__ = MagicMock(
+ return_value=None
+ )
+ mock_session.query.return_value.filter_by.return_value.first.return_value = mock_domain_classification
+
+ classifier.get_classification("example.com")
+
+ assert (
+ classifier._classification_cache.get("example.com")
+ is mock_domain_classification
+ )
+
+ def test_returns_cached_without_db_query(self, mock_domain_classification):
+ """Should return cached result without hitting DB."""
+ classifier = DomainClassifier(username="testuser")
+ classifier._classification_cache["example.com"] = (
+ mock_domain_classification
+ )
+
+ with patch(
+ "local_deep_research.domain_classifier.classifier.get_user_db_session"
+ ) as mock_session_ctx:
+ result = classifier.get_classification("example.com")
+
+ mock_session_ctx.assert_not_called()
+ assert result is mock_domain_classification
+
+ def test_does_not_cache_none_result(self):
+ """Should not cache None results (domain not found)."""
+ classifier = DomainClassifier(username="testuser")
+
+ with patch(
+ "local_deep_research.domain_classifier.classifier.get_user_db_session"
+ ) as mock_session_ctx:
+ mock_session = MagicMock()
+ mock_session_ctx.return_value.__enter__ = MagicMock(
+ return_value=mock_session
+ )
+ mock_session_ctx.return_value.__exit__ = MagicMock(
+ return_value=None
+ )
+ mock_session.query.return_value.filter_by.return_value.first.return_value = None
+
+ classifier.get_classification("unknown.com")
+
+ assert classifier._classification_cache.get("unknown.com") is None
+
class TestGetAllClassifications:
"""Tests for get_all_classifications method."""
diff --git a/tests/utilities/test_search_cache_stampede.py b/tests/utilities/test_search_cache_stampede.py
index 57ef216481..26fbf59b03 100644
--- a/tests/utilities/test_search_cache_stampede.py
+++ b/tests/utilities/test_search_cache_stampede.py
@@ -12,6 +12,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
from unittest.mock import patch
import pytest
+from cachetools import TTLCache
class TestStampedeProtectionConcurrency:
@@ -647,3 +648,36 @@ class TestCacheInvalidation:
assert len(cache._memory_cache) == 0
assert len(cache._access_times) == 0
+
+
+class TestBoundedTTLCacheStampedeProtection:
+ """Tests for bounded TTLCache in stampede protection dicts."""
+
+ @pytest.fixture
+ def cache(self):
+ """Create a fresh cache instance for each test."""
+ with tempfile.TemporaryDirectory() as tmpdir:
+ from local_deep_research.utilities.search_cache import SearchCache
+
+ cache = SearchCache(
+ cache_dir=tmpdir, max_memory_items=100, default_ttl=3600
+ )
+ yield cache
+
+ def test_fetch_events_is_ttl_cache(self, cache):
+ """Stampede _fetch_events should be a bounded TTLCache."""
+ assert isinstance(cache._fetch_events, TTLCache)
+ assert cache._fetch_events.maxsize == 1000
+ assert cache._fetch_events.ttl == 60
+
+ def test_fetch_locks_is_ttl_cache(self, cache):
+ """Stampede _fetch_locks should be a bounded TTLCache."""
+ assert isinstance(cache._fetch_locks, TTLCache)
+ assert cache._fetch_locks.maxsize == 1000
+ assert cache._fetch_locks.ttl == 60
+
+ def test_fetch_results_is_ttl_cache(self, cache):
+ """Stampede _fetch_results should be a bounded TTLCache."""
+ assert isinstance(cache._fetch_results, TTLCache)
+ assert cache._fetch_results.maxsize == 1000
+ assert cache._fetch_results.ttl == 60