~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test: add more web routes and services tests
LearningCircuitcce6a38e2cef
diff --git a/tests/web/routes/test_research_routes.py b/tests/web/routes/test_research_routes.pyindex 40539aa3f1..e963329fad 100644--- a/tests/web/routes/test_research_routes.py+++ b/tests/web/routes/test_research_routes.py@@ -310,3 +310,168 @@ class TestStartResearchApi:)# Should return error for non-JSON bodyassert response.status_code in [400, 415, 500]+++class TestTerminateResearchApi:+ """Tests for /api/terminate/<research_id> endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.post(f"{RESEARCH_PREFIX}/api/terminate/test-id")+ assert response.status_code in [401, 302, 404, 405]++ def test_returns_success_when_authenticated(self, authenticated_client):+ """Should handle terminate request when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.terminate_research.return_value = {"success": True}+ response = authenticated_client.post(+ f"{RESEARCH_PREFIX}/api/terminate/test-id"+ )+ assert response.status_code in [200, 404]+++class TestDeleteResearchApi:+ """Tests for /api/delete/<research_id> endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.delete(f"{RESEARCH_PREFIX}/api/delete/test-id")+ assert response.status_code in [401, 302, 404, 405]++ def test_returns_success_when_authenticated(self, authenticated_client):+ """Should handle delete request when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.delete_research.return_value = {"success": True}+ response = authenticated_client.delete(+ f"{RESEARCH_PREFIX}/api/delete/test-id"+ )+ assert response.status_code in [200, 404]+++class TestClearHistoryApi:+ """Tests for /api/clear_history endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.post(f"{RESEARCH_PREFIX}/api/clear_history")+ assert response.status_code in [401, 302, 404, 405]++ def test_returns_success_when_authenticated(self, authenticated_client):+ """Should handle clear history request when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.clear_history.return_value = {"success": True}+ response = authenticated_client.post(+ f"{RESEARCH_PREFIX}/api/clear_history"+ )+ assert response.status_code in [200, 500]+++class TestGetHistoryApi:+ """Tests for /api/history endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/history")+ assert response.status_code in [401, 302, 404]++ def test_returns_history_when_authenticated(self, authenticated_client):+ """Should return history when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.get_history.return_value = []+ response = authenticated_client.get(+ f"{RESEARCH_PREFIX}/api/history"+ )+ assert response.status_code in [200, 500]+++class TestGetResearchDetailsApi:+ """Tests for /api/research/<id> endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/research/test-id")+ assert response.status_code in [401, 302, 404]++ def test_returns_details_when_authenticated(self, authenticated_client):+ """Should return research details when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.get_research_details.return_value = {"id": "test-id"}+ response = authenticated_client.get(+ f"{RESEARCH_PREFIX}/api/research/test-id"+ )+ assert response.status_code in [200, 404, 500]+++class TestGetResearchLogsApi:+ """Tests for /api/research/<id>/logs endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/research/test-id/logs")+ assert response.status_code in [401, 302, 404]++ def test_returns_logs_when_authenticated(self, authenticated_client):+ """Should return research logs when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.get_research_logs.return_value = []+ response = authenticated_client.get(+ f"{RESEARCH_PREFIX}/api/research/test-id/logs"+ )+ assert response.status_code in [200, 404, 500]+++class TestGetResearchStatusApi:+ """Tests for /api/research/<id>/status endpoint."""++ def test_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/research/test-id/status")+ assert response.status_code in [401, 302, 404]++ def test_returns_status_when_authenticated(self, authenticated_client):+ """Should return research status when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.get_research_status.return_value = {"status": "running"}+ response = authenticated_client.get(+ f"{RESEARCH_PREFIX}/api/research/test-id/status"+ )+ assert response.status_code in [200, 404, 500]+++class TestQueueStatusApi:+ """Tests for queue status API endpoints."""++ def test_get_queue_status_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/queue/status")+ assert response.status_code in [401, 302, 404]++ def test_get_queue_status_when_authenticated(self, authenticated_client):+ """Should return queue status when authenticated."""+ with patch(+ "src.local_deep_research.web.routes.research_routes.research_service"+ ) as mock_service:+ mock_service.get_queue_status.return_value = {"queue": []}+ response = authenticated_client.get(+ f"{RESEARCH_PREFIX}/api/queue/status"+ )+ assert response.status_code in [200, 500]++ def test_get_queue_position_requires_authentication(self, client):+ """Should require authentication."""+ response = client.get(f"{RESEARCH_PREFIX}/api/queue/test-id/position")+ assert response.status_code in [401, 302, 404]diff --git a/tests/web/routes/test_settings_routes.py b/tests/web/routes/test_settings_routes.pynew file mode 100644index 0000000000..357225ced8--- /dev/null+++ b/tests/web/routes/test_settings_routes.py@@ -0,0 +1,300 @@+"""Tests for settings_routes module - Settings API endpoints."""++from unittest.mock import patch, MagicMock, Mock++SETTINGS_PREFIX = "/settings"+++class TestValidateSetting:+ """Tests for validate_setting function."""++ def test_validate_string_setting(self):+ """Test validating string setting."""+ from local_deep_research.web.routes.settings_routes import (+ validate_setting,+ )++ # Test valid string+ valid, msg = validate_setting(+ "test_string", "hello", expected_type="str"+ )+ assert valid is True++ def test_validate_integer_setting(self):+ """Test validating integer setting."""+ from local_deep_research.web.routes.settings_routes import (+ validate_setting,+ )++ # Test valid integer+ valid, msg = validate_setting("test_int", 42, expected_type="int")+ assert valid is True++ def test_validate_float_setting(self):+ """Test validating float setting."""+ from local_deep_research.web.routes.settings_routes import (+ validate_setting,+ )++ # Test valid float+ valid, msg = validate_setting("test_float", 3.14, expected_type="float")+ assert valid is True++ def test_validate_bool_setting(self):+ """Test validating boolean setting."""+ from local_deep_research.web.routes.settings_routes import (+ validate_setting,+ )++ # Test valid boolean+ valid, msg = validate_setting("test_bool", True, expected_type="bool")+ assert valid is True++ def test_validate_invalid_type(self):+ """Test validating setting with wrong type."""+ from local_deep_research.web.routes.settings_routes import (+ validate_setting,+ )++ # Test invalid type (string where int expected)+ valid, msg = validate_setting(+ "test_int", "not an int", expected_type="int"+ )+ assert valid is False+++class TestCalculateWarnings:+ """Tests for calculate_warnings function."""++ def test_calculate_warnings_returns_list(self):+ """Test that calculate_warnings returns a list."""+ from local_deep_research.web.routes.settings_routes import (+ calculate_warnings,+ )++ with patch(+ "local_deep_research.web.routes.settings_routes.get_user_db_session"+ ) as mock_session:+ mock_ctx = MagicMock()+ mock_session.return_value.__enter__ = Mock(return_value=mock_ctx)+ mock_session.return_value.__exit__ = Mock(return_value=False)++ with patch(+ "local_deep_research.web.routes.settings_routes.SettingsManager"+ ) as mock_sm:+ mock_instance = MagicMock()+ mock_instance.get_setting.return_value = "test"+ mock_sm.return_value = mock_instance++ with patch(+ "local_deep_research.web.routes.settings_routes.session",+ {"username": "testuser"},+ ):+ result = calculate_warnings()++ assert isinstance(result, list)+++class TestSettingsBlueprintImport:+ """Tests for settings blueprint import."""++ def test_blueprint_exists(self):+ """Test that settings blueprint exists."""+ from local_deep_research.web.routes.settings_routes import settings_bp++ assert settings_bp is not None+ assert settings_bp.name == "settings"+++class TestSettingsPageRoutes:+ """Tests for settings page routes."""++ def test_settings_page_route_exists(self, client):+ """Test settings page route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/")+ # Should exist but may require auth+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_main_config_page_route_exists(self, client):+ """Test main config page route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/main")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_collections_config_page_route_exists(self, client):+ """Test collections config page route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/collections")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_keys_config_page_route_exists(self, client):+ """Test API keys config page route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api_keys")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_search_engines_config_page_route_exists(self, client):+ """Test search engines config page route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/search_engines")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestSettingsApiRoutes:+ """Tests for settings API routes."""++ def test_api_get_all_settings_route_exists(self, client):+ """Test /api GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_get_categories_route_exists(self, client):+ """Test /api/categories GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/categories")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_get_types_route_exists(self, client):+ """Test /api/types GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/types")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_get_ui_elements_route_exists(self, client):+ """Test /api/ui_elements GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/ui_elements")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_get_warnings_route_exists(self, client):+ """Test /api/warnings GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/warnings")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestSaveAllSettings:+ """Tests for save_all_settings endpoint."""++ def test_save_all_settings_requires_post(self, client):+ """Test that save_all_settings requires POST method."""+ response = client.get(f"{SETTINGS_PREFIX}/save_all_settings")+ # GET should return 405 Method Not Allowed+ assert response.status_code in [302, 401, 403, 405]++ def test_save_all_settings_requires_json(self, client):+ """Test that save_all_settings requires JSON body."""+ response = client.post(f"{SETTINGS_PREFIX}/save_all_settings")+ assert response.status_code in [302, 400, 401, 403, 500]+++class TestResetToDefaults:+ """Tests for reset_to_defaults endpoint."""++ def test_reset_to_defaults_requires_post(self, client):+ """Test that reset_to_defaults requires POST method."""+ response = client.get(f"{SETTINGS_PREFIX}/reset_to_defaults")+ # GET should return 405 Method Not Allowed+ assert response.status_code in [302, 401, 403, 405]+++class TestApiImportSettings:+ """Tests for api_import_settings endpoint."""++ def test_import_settings_requires_post(self, client):+ """Test that import_settings requires POST method."""+ response = client.get(f"{SETTINGS_PREFIX}/api/import")+ # GET should return 405 Method Not Allowed+ assert response.status_code in [302, 401, 403, 405, 500]+++class TestAvailableModelsApi:+ """Tests for available models API endpoint."""++ def test_api_available_models_route_exists(self, client):+ """Test /api/available-models GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/available-models")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestAvailableSearchEnginesApi:+ """Tests for available search engines API endpoint."""++ def test_api_available_search_engines_route_exists(self, client):+ """Test /api/available-search-engines GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/available-search-engines")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestSearchFavoritesApi:+ """Tests for search favorites API endpoints."""++ def test_api_get_search_favorites_route_exists(self, client):+ """Test /api/search-favorites GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/search-favorites")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_toggle_search_favorite_requires_post(self, client):+ """Test /api/search-favorites/toggle requires POST."""+ response = client.get(f"{SETTINGS_PREFIX}/api/search-favorites/toggle")+ assert response.status_code in [302, 401, 403, 405]+++class TestOllamaStatusApi:+ """Tests for Ollama status API endpoint."""++ def test_api_ollama_status_route_exists(self, client):+ """Test /api/ollama-status GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/ollama-status")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestRateLimitingApi:+ """Tests for rate limiting API endpoints."""++ def test_api_rate_limiting_status_route_exists(self, client):+ """Test /api/rate-limiting/status GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/rate-limiting/status")+ assert response.status_code in [200, 302, 401, 403, 500]++ def test_api_rate_limiting_cleanup_requires_post(self, client):+ """Test /api/rate-limiting/cleanup requires POST."""+ response = client.get(f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup")+ assert response.status_code in [302, 401, 403, 405]+++class TestBulkSettingsApi:+ """Tests for bulk settings API endpoint."""++ def test_api_get_bulk_settings_route_exists(self, client):+ """Test /api/bulk GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/bulk")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestDataLocationApi:+ """Tests for data location API endpoint."""++ def test_api_data_location_route_exists(self, client):+ """Test /api/data-location GET route exists."""+ response = client.get(f"{SETTINGS_PREFIX}/api/data-location")+ assert response.status_code in [200, 302, 401, 403, 500]+++class TestNotificationTestApi:+ """Tests for notification test API endpoint."""++ def test_api_test_notification_requires_post(self, client):+ """Test /api/notifications/test-url requires POST."""+ response = client.get(f"{SETTINGS_PREFIX}/api/notifications/test-url")+ assert response.status_code in [302, 401, 403, 405]+++class TestOpenFileLocation:+ """Tests for open_file_location endpoint."""++ def test_open_file_location_requires_post(self, client):+ """Test open_file_location requires POST."""+ response = client.get(f"{SETTINGS_PREFIX}/open_file_location")+ assert response.status_code in [302, 401, 403, 405]+++class TestFixCorruptedSettings:+ """Tests for fix_corrupted_settings endpoint."""++ def test_fix_corrupted_settings_requires_post(self, client):+ """Test fix_corrupted_settings requires POST."""+ response = client.get(f"{SETTINGS_PREFIX}/fix_corrupted_settings")+ assert response.status_code in [302, 401, 403, 405]diff --git a/tests/web/services/test_research_service.py b/tests/web/services/test_research_service.pyindex 4a6b925acc..990eb7c78a 100644--- a/tests/web/services/test_research_service.py+++ b/tests/web/services/test_research_service.py@@ -577,3 +577,219 @@ class TestHandleTermination:mock_cleanup.assert_called_once_with(123, active_research, termination_flags, "testuser")+++class TestExportQuartoFormat:+ """Tests for quarto export format."""++ def test_export_quarto_creates_zip(self):+ """export_report_to_memory creates zip for quarto format."""+ from src.local_deep_research.web.services.research_service import (+ export_report_to_memory,+ )++ markdown_content = "# Test Report\n\nThis is test content."++ content, filename, mimetype = export_report_to_memory(+ markdown_content, "quarto", title="Test Report"+ )++ assert filename.endswith(".zip")+ assert mimetype == "application/zip"+ assert isinstance(content, bytes)+ # Verify it's a valid zip file by checking magic bytes+ assert content[:2] == b"PK"+++class TestExportMarkdownFormat:+ """Tests for markdown export format."""++ def test_export_markdown_format(self):+ """export_report_to_memory handles markdown format."""+ from src.local_deep_research.web.services.research_service import (+ export_report_to_memory,+ )++ markdown_content = "# Test Report\n\nThis is test content."++ content, filename, mimetype = export_report_to_memory(+ markdown_content, "markdown", title="Test Report"+ )++ assert filename.endswith(".md")+ assert mimetype == "text/markdown"+ assert isinstance(content, bytes)+++class TestGenerateReportPathUniqueHash:+ """Tests for _generate_report_path unique hash generation."""++ @patch("src.local_deep_research.web.services.research_service.OUTPUT_DIR")+ def test_different_queries_different_paths(self, mock_output_dir):+ """Different queries should generate different paths."""+ from src.local_deep_research.web.services.research_service import (+ _generate_report_path,+ )++ mock_output_dir.__truediv__ = lambda self, x: Path(f"/test/output/{x}")++ path1 = _generate_report_path("query one")+ path2 = _generate_report_path("query two")++ # Paths should be different+ assert str(path1) != str(path2)++ @patch("src.local_deep_research.web.services.research_service.OUTPUT_DIR")+ def test_same_query_same_path(self, mock_output_dir):+ """Same query should generate same path."""+ from src.local_deep_research.web.services.research_service import (+ _generate_report_path,+ )++ mock_output_dir.__truediv__ = lambda self, x: Path(f"/test/output/{x}")++ path1 = _generate_report_path("test query")+ path2 = _generate_report_path("test query")++ # Paths should be the same+ assert str(path1) == str(path2)+++class TestStartResearchProcessWithOptions:+ """Tests for start_research_process with various options."""++ @patch(+ "src.local_deep_research.web.services.research_service.thread_with_app_context"+ )+ @patch(+ "src.local_deep_research.web.services.research_service.thread_context"+ )+ def test_start_research_with_local_collections(+ self, mock_thread_context, mock_thread_with_context+ ):+ """start_research_process handles local_collections option."""+ from src.local_deep_research.web.services.research_service import (+ start_research_process,+ )++ mock_callback = Mock()+ mock_thread_with_context.return_value = mock_callback+ mock_thread_context.return_value = {}++ active_research = {}+ termination_flags = {}++ with patch(+ "src.local_deep_research.web.services.research_service.threading.Thread"+ ) as mock_thread_class:+ mock_thread = Mock()+ mock_thread_class.return_value = mock_thread++ start_research_process(+ research_id=123,+ query="test query",+ mode="detailed",+ active_research=active_research,+ termination_flags=termination_flags,+ run_research_callback=mock_callback,+ local_collections=["collection1", "collection2"],+ )++ assert 123 in active_research+ assert active_research[123]["settings"]["local_collections"] == [+ "collection1",+ "collection2",+ ]++ @patch(+ "src.local_deep_research.web.services.research_service.thread_with_app_context"+ )+ @patch(+ "src.local_deep_research.web.services.research_service.thread_context"+ )+ def test_start_research_stores_knowledge_graph_option(+ self, mock_thread_context, mock_thread_with_context+ ):+ """start_research_process stores knowledge_graph option."""+ from src.local_deep_research.web.services.research_service import (+ start_research_process,+ )++ mock_callback = Mock()+ mock_thread_with_context.return_value = mock_callback+ mock_thread_context.return_value = {}++ active_research = {}+ termination_flags = {}++ with patch(+ "src.local_deep_research.web.services.research_service.threading.Thread"+ ) as mock_thread_class:+ mock_thread = Mock()+ mock_thread_class.return_value = mock_thread++ start_research_process(+ research_id=456,+ query="test query",+ mode="quick",+ active_research=active_research,+ termination_flags=termination_flags,+ run_research_callback=mock_callback,+ enable_knowledge_graph=True,+ )++ assert 456 in active_research+ assert (+ active_research[456]["settings"]["enable_knowledge_graph"]+ is True+ )+++class TestDeleteResearchStrategy:+ """Tests for delete_research_strategy function."""++ @patch(+ "src.local_deep_research.web.services.research_service.get_user_db_session"+ )+ def test_delete_research_strategy_deletes_existing(self, mock_get_session):+ """delete_research_strategy deletes existing strategy."""+ from src.local_deep_research.web.services.research_service import (+ delete_research_strategy,+ )++ mock_strategy = Mock()++ mock_session = MagicMock()+ mock_query = Mock()+ mock_query.filter_by.return_value.first.return_value = mock_strategy+ mock_session.query.return_value = mock_query+ mock_session.__enter__ = Mock(return_value=mock_session)+ mock_session.__exit__ = Mock(return_value=False)+ mock_get_session.return_value = mock_session++ delete_research_strategy(123, username="testuser")++ mock_session.delete.assert_called_once_with(mock_strategy)+ mock_session.commit.assert_called_once()++ @patch(+ "src.local_deep_research.web.services.research_service.get_user_db_session"+ )+ def test_delete_research_strategy_handles_not_found(self, mock_get_session):+ """delete_research_strategy handles non-existent strategy."""+ from src.local_deep_research.web.services.research_service import (+ delete_research_strategy,+ )++ mock_session = MagicMock()+ mock_query = Mock()+ mock_query.filter_by.return_value.first.return_value = None+ mock_session.query.return_value = mock_query+ mock_session.__enter__ = Mock(return_value=mock_session)+ mock_session.__exit__ = Mock(return_value=False)+ mock_get_session.return_value = mock_session++ # Should not raise+ delete_research_strategy(999, username="testuser")++ mock_session.delete.assert_not_called()