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 resource cleanup tests for FD leak fixes

LearningCircuit906f8a4b1ff7
diff --git a/tests/database/test_auth_db.py b/tests/database/test_auth_db.py
index 44088bd9d2..4c3f61d266 100644
--- a/tests/database/test_auth_db.py
+++ b/tests/database/test_auth_db.py
@@ -233,3 +233,155 @@ class TestGetAuthDbSession:
call_args = mock_engine.call_args[0][0]
assert call_args.startswith("sqlite:///")
assert "ldr_auth.db" in call_args
+
+
+class TestAuthDbEngineCache:
+ """Tests for cached engine behavior."""
+
+ def test_get_auth_engine_returns_engine(self):
+ """Test that _get_auth_engine() returns an engine."""
+ from local_deep_research.database.auth_db import (
+ _get_auth_engine,
+ dispose_auth_engine,
+ )
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ db_path = Path(temp_dir) / "ldr_auth.db"
+
+ with patch(
+ "local_deep_research.database.auth_db.get_auth_db_path"
+ ) as mock_path:
+ mock_path.return_value = db_path
+
+ # Ensure we start with a clean state
+ dispose_auth_engine()
+
+ engine = _get_auth_engine()
+
+ assert engine is not None
+
+ # Clean up
+ dispose_auth_engine()
+
+ def test_engine_cached_on_subsequent_calls(self):
+ """Test that _get_auth_engine() returns cached engine."""
+ from local_deep_research.database.auth_db import (
+ _get_auth_engine,
+ dispose_auth_engine,
+ )
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ db_path = Path(temp_dir) / "ldr_auth.db"
+
+ with patch(
+ "local_deep_research.database.auth_db.get_auth_db_path"
+ ) as mock_path:
+ mock_path.return_value = db_path
+
+ # Ensure we start with a clean state
+ dispose_auth_engine()
+
+ engine1 = _get_auth_engine()
+ engine2 = _get_auth_engine()
+
+ # Should return the same cached engine
+ assert engine1 is engine2
+
+ # Clean up
+ dispose_auth_engine()
+
+ def test_engine_recreated_on_path_change(self):
+ """Test that engine is recreated when data directory changes."""
+ from local_deep_research.database.auth_db import (
+ _get_auth_engine,
+ dispose_auth_engine,
+ )
+
+ with tempfile.TemporaryDirectory() as temp_dir1:
+ with tempfile.TemporaryDirectory() as temp_dir2:
+ db_path1 = Path(temp_dir1) / "ldr_auth.db"
+ db_path2 = Path(temp_dir2) / "ldr_auth.db"
+
+ # Ensure we start with a clean state
+ dispose_auth_engine()
+
+ with patch(
+ "local_deep_research.database.auth_db.get_auth_db_path"
+ ) as mock_path:
+ # First call with path1
+ mock_path.return_value = db_path1
+ engine1 = _get_auth_engine()
+
+ # Change to path2
+ mock_path.return_value = db_path2
+ engine2 = _get_auth_engine()
+
+ # Should be different engines
+ assert engine1 is not engine2
+
+ # Clean up
+ dispose_auth_engine()
+
+ def test_dispose_clears_cache(self):
+ """Test that dispose_auth_engine() clears the cached engine."""
+ from local_deep_research.database import auth_db
+ from local_deep_research.database.auth_db import (
+ _get_auth_engine,
+ dispose_auth_engine,
+ )
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ db_path = Path(temp_dir) / "ldr_auth.db"
+
+ with patch(
+ "local_deep_research.database.auth_db.get_auth_db_path"
+ ) as mock_path:
+ mock_path.return_value = db_path
+
+ # Ensure we start with a clean state
+ dispose_auth_engine()
+
+ # Create an engine
+ _get_auth_engine()
+
+ # Dispose it
+ dispose_auth_engine()
+
+ # Module-level _auth_engine should be None
+ assert auth_db._auth_engine is None
+
+ def test_dispose_clears_path(self):
+ """Test that dispose_auth_engine() clears the cached path."""
+ from local_deep_research.database import auth_db
+ from local_deep_research.database.auth_db import (
+ _get_auth_engine,
+ dispose_auth_engine,
+ )
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ db_path = Path(temp_dir) / "ldr_auth.db"
+
+ with patch(
+ "local_deep_research.database.auth_db.get_auth_db_path"
+ ) as mock_path:
+ mock_path.return_value = db_path
+
+ # Ensure we start with a clean state
+ dispose_auth_engine()
+
+ # Create an engine
+ _get_auth_engine()
+
+ # Dispose it
+ dispose_auth_engine()
+
+ # Module-level _auth_engine_path should be None
+ assert auth_db._auth_engine_path is None
+
+ def test_dispose_handles_no_engine(self):
+ """Test that dispose_auth_engine() handles no engine gracefully."""
+ from local_deep_research.database.auth_db import dispose_auth_engine
+
+ # Dispose when there's no engine should not raise
+ dispose_auth_engine()
+ dispose_auth_engine() # Calling twice should be fine
diff --git a/tests/research_library/downloaders/test_base_downloader.py b/tests/research_library/downloaders/test_base_downloader.py
index 8fd9f1e6ed..aa6775f872 100644
--- a/tests/research_library/downloaders/test_base_downloader.py
+++ b/tests/research_library/downloaders/test_base_downloader.py
@@ -393,3 +393,92 @@ class TestGetMetadata:
downloader = ConcreteDownloader()
metadata = downloader.get_metadata("https://example.com/paper.pdf")
assert metadata == {}
+
+
+class TestBaseDownloaderResourceCleanup:
+ """Tests for session cleanup and resource management."""
+
+ def test_close_closes_session(self):
+ """Test that close() properly closes the session."""
+ downloader = ConcreteDownloader()
+
+ # Verify session exists
+ assert downloader.session is not None
+
+ # Close the downloader
+ downloader.close()
+
+ # Session should be None after close
+ assert downloader.session is None
+
+ def test_close_handles_none_session(self):
+ """Test that close() handles already-closed session gracefully."""
+ downloader = ConcreteDownloader()
+
+ # Close twice - should not raise
+ downloader.close()
+ downloader.close() # Should not raise
+
+ assert downloader.session is None
+
+ def test_close_handles_exception(self, mocker):
+ """Test that close() handles session.close() exceptions."""
+ downloader = ConcreteDownloader()
+
+ # Mock session to raise on close
+ mock_session = mocker.Mock()
+ mock_session.close.side_effect = Exception("Close failed")
+ downloader.session = mock_session
+
+ # Should not raise, just log the exception
+ downloader.close()
+
+ # Session should be set to None even after exception
+ assert downloader.session is None
+
+ def test_del_calls_close(self, mocker):
+ """Test that __del__ calls close()."""
+ downloader = ConcreteDownloader()
+
+ mock_close = mocker.patch.object(downloader, "close")
+
+ downloader.__del__()
+
+ mock_close.assert_called_once()
+
+ def test_context_manager_calls_close(self, mocker):
+ """Test that exiting context manager calls close()."""
+ from local_deep_research.research_library.downloaders.base import (
+ BaseDownloader,
+ )
+
+ mock_close = mocker.patch.object(BaseDownloader, "close")
+
+ with ConcreteDownloader() as downloader:
+ assert downloader is not None
+
+ mock_close.assert_called_once()
+
+ def test_context_manager_returns_self(self):
+ """Test that __enter__ returns self."""
+ downloader = ConcreteDownloader()
+
+ result = downloader.__enter__()
+
+ assert result is downloader
+
+ # Clean up
+ downloader.close()
+
+ def test_context_manager_closes_on_exception(self):
+ """Test that context manager closes session even when exception occurs."""
+ downloader = None
+ try:
+ with ConcreteDownloader() as dl:
+ downloader = dl
+ raise ValueError("Test exception")
+ except ValueError:
+ pass
+
+ # Session should be closed even after exception
+ assert downloader.session is None
diff --git a/tests/search_engines/test_search_engine_semantic_scholar.py b/tests/search_engines/test_search_engine_semantic_scholar.py
index 84eb289181..88ad9c636a 100644
--- a/tests/search_engines/test_search_engine_semantic_scholar.py
+++ b/tests/search_engines/test_search_engine_semantic_scholar.py
@@ -749,3 +749,113 @@ class TestSemanticScholarGetPreviews:
# Authors should be extracted
assert len(result) == 1
+
+
+class TestSemanticScholarResourceCleanup:
+ """Tests for session cleanup and resource management."""
+
+ def test_close_closes_session(self):
+ """Test that close() properly closes the session."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = SemanticScholarSearchEngine()
+
+ # Verify session exists
+ assert engine.session is not None
+
+ # Close the engine
+ engine.close()
+
+ # Session should be None after close
+ assert engine.session is None
+
+ def test_close_handles_none_session(self):
+ """Test that close() handles already-closed session gracefully."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = SemanticScholarSearchEngine()
+
+ # Close twice - should not raise
+ engine.close()
+ engine.close() # Should not raise
+
+ assert engine.session is None
+
+ def test_close_handles_exception(self):
+ """Test that close() handles session.close() exceptions."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = SemanticScholarSearchEngine()
+
+ # Mock session to raise on close
+ mock_session = Mock()
+ mock_session.close.side_effect = Exception("Close failed")
+ engine.session = mock_session
+
+ # Should not raise, just log the exception
+ engine.close()
+
+ # Session should be set to None even after exception
+ assert engine.session is None
+
+ def test_del_calls_close(self):
+ """Test that __del__ calls close()."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = SemanticScholarSearchEngine()
+
+ with patch.object(engine, "close") as mock_close:
+ engine.__del__()
+ mock_close.assert_called_once()
+
+ def test_context_manager_calls_close(self):
+ """Test that exiting context manager calls close()."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ with patch.object(SemanticScholarSearchEngine, "close") as mock_close:
+ with SemanticScholarSearchEngine() as engine:
+ assert engine is not None
+
+ mock_close.assert_called_once()
+
+ def test_context_manager_returns_self(self):
+ """Test that __enter__ returns self."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = SemanticScholarSearchEngine()
+
+ result = engine.__enter__()
+
+ assert result is engine
+
+ # Clean up
+ engine.close()
+
+ def test_context_manager_closes_on_exception(self):
+ """Test that context manager closes session even when exception occurs."""
+ from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
+ SemanticScholarSearchEngine,
+ )
+
+ engine = None
+ try:
+ with SemanticScholarSearchEngine() as eng:
+ engine = eng
+ raise ValueError("Test exception")
+ except ValueError:
+ pass
+
+ # Session should be closed even after exception
+ assert engine.session is None
diff --git a/tests/utilities/test_search_cache.py b/tests/utilities/test_search_cache.py
index 548e6a2f1c..1c2045955a 100644
--- a/tests/utilities/test_search_cache.py
+++ b/tests/utilities/test_search_cache.py
@@ -584,6 +584,91 @@ class TestNormalizeEntityQuery:
assert cache_info.hits == 1
+class TestSearchCacheResourceCleanup:
+ """Tests for engine disposal and resource management."""
+
+ def test_dispose_disposes_engine(self):
+ """Test that dispose() calls engine.dispose()."""
+ from local_deep_research.utilities.search_cache import SearchCache
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ cache = SearchCache(cache_dir=tmpdir)
+
+ # Mock the engine
+ mock_engine = cache.engine
+ original_dispose = mock_engine.dispose
+
+ dispose_called = []
+
+ def track_dispose():
+ dispose_called.append(True)
+ original_dispose()
+
+ mock_engine.dispose = track_dispose
+
+ cache.dispose()
+
+ assert len(dispose_called) == 1
+
+ def test_dispose_sets_engine_to_none(self):
+ """Test that dispose() sets engine to None."""
+ from local_deep_research.utilities.search_cache import SearchCache
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ cache = SearchCache(cache_dir=tmpdir)
+
+ # Verify engine exists
+ assert cache.engine is not None
+
+ cache.dispose()
+
+ # Engine should be None after dispose
+ assert cache.engine is None
+
+ def test_dispose_handles_no_engine(self):
+ """Test that dispose() handles missing engine gracefully."""
+ from local_deep_research.utilities.search_cache import SearchCache
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ cache = SearchCache(cache_dir=tmpdir)
+
+ # Dispose twice - should not raise
+ cache.dispose()
+ cache.dispose()
+
+ assert cache.engine is None
+
+ def test_dispose_handles_exception(self):
+ """Test that dispose() handles engine.dispose() exceptions."""
+ from local_deep_research.utilities.search_cache import SearchCache
+ from unittest.mock import Mock
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ cache = SearchCache(cache_dir=tmpdir)
+
+ # Mock engine to raise on dispose
+ mock_engine = Mock()
+ mock_engine.dispose.side_effect = Exception("Dispose failed")
+ cache.engine = mock_engine
+
+ # Should not raise, just log the exception
+ cache.dispose()
+
+ # Engine should be set to None even after exception
+ assert cache.engine is None
+
+ def test_del_calls_dispose(self):
+ """Test that __del__ calls dispose()."""
+ from local_deep_research.utilities.search_cache import SearchCache
+
+ with tempfile.TemporaryDirectory() as tmpdir:
+ cache = SearchCache(cache_dir=tmpdir)
+
+ with patch.object(cache, "dispose") as mock_dispose:
+ cache.__del__()
+ mock_dispose.assert_called_once()
+
+
class TestSearchCacheEdgeCases:
"""Tests for edge cases and error handling."""