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: delete 26 shadow test files across 8 directories (683 tests, 10154 lines) (#4243)

LearningCircuit99eb62e97598
diff --git a/tests/advanced_search_system/filters/test_cross_engine_filter_extended.py b/tests/advanced_search_system/filters/test_cross_engine_filter_extended.py
deleted file mode 100644
index a8d65acfcc..0000000000
--- a/tests/advanced_search_system/filters/test_cross_engine_filter_extended.py
+++ /dev/null
@@ -1,412 +0,0 @@
-"""
-Extended tests for CrossEngineFilter - Cross-engine search result filtering.
-
-Tests cover:
-- Filter initialization with settings
-- Result filtering and ranking
-- Reordering and reindexing logic
-- LLM response parsing
-- Edge cases and error handling
-"""
-
-import json
-
-
-class TestCrossEngineFilterInitialization:
- """Tests for CrossEngineFilter initialization."""
-
- def test_default_max_results(self):
- """Default max_results should be 100."""
- default_max = 100
- assert default_max == 100
-
- def test_custom_max_results(self):
- """Custom max_results should be respected."""
- config = {"max_results": 50}
- assert config["max_results"] == 50
-
- def test_default_reorder_true(self):
- """Default reorder should be True."""
- default_reorder = True
- assert default_reorder is True
-
- def test_default_reindex_true(self):
- """Default reindex should be True."""
- default_reindex = True
- assert default_reindex is True
-
- def test_max_results_from_settings(self):
- """Should get max_results from settings if not provided."""
- settings_value = 75
-
- config = {"max_results": settings_value}
- assert config["max_results"] == 75
-
-
-class TestResultFiltering:
- """Tests for result filtering logic."""
-
- def test_few_results_not_filtered(self):
- """Should not filter if 10 or fewer results."""
- results = [{"title": f"Result {i}"} for i in range(8)]
- max_results = 100
-
- # Simulating the condition in filter_results
- if len(results) <= 10:
- filtered = results[: min(max_results, len(results))]
-
- assert len(filtered) == 8
-
- def test_results_limited_to_max(self):
- """Should limit results to max_results."""
- results = [{"title": f"Result {i}"} for i in range(150)]
- max_results = 100
-
- limited = results[:max_results]
-
- assert len(limited) == 100
-
- def test_empty_results_handled(self):
- """Should handle empty results gracefully."""
- results = []
- max_results = 100
-
- filtered = results[: min(max_results, len(results))]
-
- assert filtered == []
-
- def test_results_preserve_structure(self):
- """Filtered results should preserve original structure."""
- results = [
- {
- "title": "Test",
- "snippet": "Snippet",
- "engine": "google",
- "url": "http://test.com",
- }
- ]
- max_results = 100
-
- filtered = results[: min(max_results, len(results))]
-
- assert filtered[0]["title"] == "Test"
- assert filtered[0]["engine"] == "google"
-
-
-class TestReindexing:
- """Tests for result reindexing."""
-
- def test_reindex_updates_indices(self):
- """Reindexing should update result indices."""
- results = [{"title": f"Result {i}"} for i in range(5)]
- start_index = 0
-
- for i, result in enumerate(results):
- result["index"] = str(i + start_index + 1)
-
- assert results[0]["index"] == "1"
- assert results[4]["index"] == "5"
-
- def test_reindex_with_start_index(self):
- """Reindexing should respect start_index."""
- results = [{"title": f"Result {i}"} for i in range(3)]
- start_index = 10
-
- for i, result in enumerate(results):
- result["index"] = str(i + start_index + 1)
-
- assert results[0]["index"] == "11"
- assert results[2]["index"] == "13"
-
- def test_reindex_false_no_update(self):
- """When reindex=False, indices should not be updated."""
- results = [{"title": f"Result {i}", "index": "old"} for i in range(3)]
- reindex = False
-
- if reindex:
- for i, result in enumerate(results):
- result["index"] = str(i + 1)
-
- assert results[0]["index"] == "old"
-
-
-class TestReordering:
- """Tests for result reordering."""
-
- def test_reorder_by_indices(self):
- """Should reorder results by ranked indices."""
- results = [
- {"title": "A", "original_index": 0},
- {"title": "B", "original_index": 1},
- {"title": "C", "original_index": 2},
- ]
- ranked_indices = [2, 0, 1]
-
- reordered = [
- results[idx] for idx in ranked_indices if idx < len(results)
- ]
-
- assert reordered[0]["title"] == "C"
- assert reordered[1]["title"] == "A"
- assert reordered[2]["title"] == "B"
-
- def test_reorder_false_maintains_original_order(self):
- """When reorder=False, should maintain original order."""
- results = [
- {"title": "A"},
- {"title": "B"},
- {"title": "C"},
- ]
- ranked_indices = [2, 0, 1]
- reorder = False
-
- if reorder:
- filtered = [results[idx] for idx in ranked_indices]
- else:
- filtered = [results[idx] for idx in sorted(ranked_indices)]
-
- assert filtered[0]["title"] == "A"
- assert filtered[1]["title"] == "B"
- assert filtered[2]["title"] == "C"
-
- def test_invalid_indices_skipped(self):
- """Should skip indices that are out of range."""
- results = [{"title": "A"}, {"title": "B"}]
- ranked_indices = [0, 1, 5, 10] # 5 and 10 are invalid
-
- reordered = [
- results[idx] for idx in ranked_indices if idx < len(results)
- ]
-
- assert len(reordered) == 2
-
-
-class TestPreviewContextGeneration:
- """Tests for preview context generation."""
-
- def test_preview_context_format(self):
- """Preview context should have expected format."""
- results = [
- {
- "title": "Test Title",
- "snippet": "Test snippet",
- "engine": "google",
- }
- ]
-
- preview_context = []
- for i, result in enumerate(results):
- title = result.get("title", "Untitled").strip()
- snippet = result.get("snippet", "").strip()
- engine = result.get("engine", "Unknown engine")
-
- preview_context.append(
- f"[{i}] Engine: {engine} | Title: {title}\nSnippet: {snippet}"
- )
-
- assert "[0]" in preview_context[0]
- assert "Engine: google" in preview_context[0]
- assert "Test Title" in preview_context[0]
-
- def test_snippet_truncation(self):
- """Long snippets should be truncated to 200 chars."""
- long_snippet = "x" * 300
- max_length = 200
-
- if len(long_snippet) > max_length:
- truncated = long_snippet[:max_length] + "..."
-
- assert len(truncated) == 203
-
- def test_context_limited_to_30_items(self):
- """Context should be limited to 30 items."""
- results = [{"title": f"Result {i}"} for i in range(50)]
- max_context_items = min(30, len(results))
-
- limited = results[:max_context_items]
-
- assert len(limited) == 30
-
-
-class TestLLMResponseParsing:
- """Tests for LLM response parsing."""
-
- def test_parse_json_array_response(self):
- """Should parse JSON array from response."""
- response_text = "Here are the ranked results: [3, 0, 7, 1]"
-
- start_idx = response_text.find("[")
- end_idx = response_text.rfind("]")
-
- if start_idx >= 0 and end_idx > start_idx:
- array_text = response_text[start_idx : end_idx + 1]
- ranked_indices = json.loads(array_text)
-
- assert ranked_indices == [3, 0, 7, 1]
-
- def test_parse_empty_array_response(self):
- """Should parse empty array response."""
- response_text = "No relevant results found: []"
-
- start_idx = response_text.find("[")
- end_idx = response_text.rfind("]")
-
- if start_idx >= 0 and end_idx > start_idx:
- array_text = response_text[start_idx : end_idx + 1]
- ranked_indices = json.loads(array_text)
-
- assert ranked_indices == []
-
- def test_no_array_returns_original(self):
- """Should return original if no array found."""
- response_text = "I cannot determine relevance"
-
- start_idx = response_text.find("[")
- end_idx = response_text.rfind("]")
-
- if start_idx >= 0 and end_idx > start_idx:
- found_array = True
- else:
- found_array = False
-
- assert found_array is False
-
- def test_malformed_json_handled(self):
- """Should handle malformed JSON gracefully."""
- response_text = "[1, 2, 3" # Missing closing bracket
-
- start_idx = response_text.find("[")
- end_idx = response_text.rfind("]")
-
- if start_idx >= 0 and end_idx > start_idx:
- try:
- array_text = response_text[start_idx : end_idx + 1]
- ranked_indices = json.loads(array_text)
- except json.JSONDecodeError:
- ranked_indices = None
- else:
- ranked_indices = None
-
- assert ranked_indices is None
-
-
-class TestEmptyResultsHandling:
- """Tests for empty results handling."""
-
- def test_all_results_filtered_returns_top_10(self):
- """If all filtered, should return top 10 originals."""
- results = [{"title": f"Result {i}"} for i in range(20)]
- ranked_results = [] # All filtered
-
- if not ranked_results and results:
- top_results = results[: min(10, len(results))]
- else:
- top_results = ranked_results
-
- assert len(top_results) == 10
-
- def test_no_model_returns_limited_results(self):
- """Without model, should return limited results."""
- results = [{"title": f"Result {i}"} for i in range(150)]
- model = None
- max_results = 100
-
- if not model:
- filtered = results[: min(max_results, len(results))]
-
- assert len(filtered) == 100
-
-
-class TestPromptConstruction:
- """Tests for LLM prompt construction."""
-
- def test_prompt_includes_query(self):
- """Prompt should include the original query."""
- query = "machine learning algorithms"
- prompt = f'Query: "{query}"'
-
- assert "machine learning algorithms" in prompt
-
- def test_prompt_includes_result_context(self):
- """Prompt should include result context."""
- results = [{"title": "Test", "snippet": "Snippet", "engine": "google"}]
- context = "\n\n".join(
- [
- f"[{i}] {r['engine']} | {r['title']}"
- for i, r in enumerate(results)
- ]
- )
-
- prompt = f"Search Results:\n{context}"
-
- assert "Test" in prompt
- assert "google" in prompt
-
- def test_prompt_requests_json_array(self):
- """Prompt should request JSON array response."""
- prompt = "Return the search results as a JSON array of indices"
-
- assert "JSON array" in prompt
-
-
-class TestErrorHandling:
- """Tests for error handling."""
-
- def test_exception_returns_limited_results(self):
- """On exception, should return limited results."""
- results = [{"title": f"Result {i}"} for i in range(50)]
- max_results = 100
-
- try:
- raise Exception("LLM error")
- except Exception:
- top_results = results[: min(max_results, len(results))]
-
- assert len(top_results) == 50
-
- def test_reindex_on_error_path(self):
- """Should still reindex on error path if requested."""
- results = [{"title": f"Result {i}"} for i in range(5)]
- reindex = True
- start_index = 0
-
- try:
- raise Exception("Error")
- except Exception:
- if reindex:
- for i, result in enumerate(results):
- result["index"] = str(i + start_index + 1)
-
- assert results[0]["index"] == "1"
-
-
-class TestLogging:
- """Tests for logging functionality."""
-
- def test_log_message_format(self):
- """Should log filtering results."""
- final_count = 15
- original_count = 30
- reorder = True
- reindex = True
-
- log_message = (
- f"Cross-engine filtering kept {final_count} out of {original_count} "
- f"results with reordering={reorder}, reindex={reindex}"
- )
-
- assert "15" in log_message
- assert "30" in log_message
- assert "reordering=True" in log_message
-
- def test_log_no_reorder(self):
- """Should log when not reordering."""
- final_count = 10
- original_count = 20
-
- log_message = (
- f"Cross-engine filtering kept {final_count} out of {original_count} "
- f"results without reordering"
- )
-
- assert "without reordering" in log_message
diff --git a/tests/advanced_search_system/findings/test_findings_repository_extended.py b/tests/advanced_search_system/findings/test_findings_repository_extended.py
deleted file mode 100644
index 648cc084b6..0000000000
--- a/tests/advanced_search_system/findings/test_findings_repository_extended.py
+++ /dev/null
@@ -1,442 +0,0 @@
-"""
-Extended tests for FindingsRepository - Research findings management.
-
-Tests cover:
-- Repository initialization
-- Finding addition and retrieval
-- Document management
-- Synthesis functionality
-- Error handling and edge cases
-"""
-
-
-class TestFindingsRepositoryInitialization:
- """Tests for FindingsRepository initialization."""
-
- def test_empty_findings_on_init(self):
- """Findings should be empty on initialization."""
- findings = {}
- assert len(findings) == 0
-
- def test_empty_documents_on_init(self):
- """Documents list should be empty on initialization."""
- documents = []
- assert len(documents) == 0
-
- def test_empty_questions_by_iteration(self):
- """Questions by iteration should be empty on init."""
- questions_by_iteration = {}
- assert len(questions_by_iteration) == 0
-
-
-class TestFindingAddition:
- """Tests for adding findings."""
-
- def test_add_string_finding(self):
- """Should convert string finding to dict."""
- findings = {}
- query = "test query"
-
- findings.setdefault(query, [])
-
- finding = "This is a finding"
- finding_dict = {
- "phase": "Synthesis",
- "content": finding,
- "question": query,
- "search_results": [],
- "documents": [],
- }
- findings[query].append(finding_dict)
-
- assert len(findings[query]) == 1
- assert findings[query][0]["content"] == "This is a finding"
-
- def test_add_dict_finding(self):
- """Should add dict finding directly."""
- findings = {}
- query = "test query"
-
- findings.setdefault(query, [])
-
- finding = {
- "phase": "Analysis",
- "content": "Analysis content",
- "question": query,
- }
- findings[query].append(finding)
-
- assert findings[query][0]["phase"] == "Analysis"
-
- def test_final_synthesis_creates_extra_entry(self):
- """Final synthesis should create extra synthesis entry."""
- findings = {}
- query = "test query"
-
- findings.setdefault(query, [])
-
- finding = {
- "phase": "Final synthesis",
- "content": "Final content",
- }
- findings[query].append(finding)
-
- if finding.get("phase") == "Final synthesis":
- findings[query + "_synthesis"] = [
- {
- "phase": "Synthesis",
- "content": finding.get("content", ""),
- "question": query,
- "search_results": [],
- "documents": [],
- }
- ]
-
- assert query + "_synthesis" in findings
- assert findings[query + "_synthesis"][0]["content"] == "Final content"
-
- def test_multiple_findings_same_query(self):
- """Should support multiple findings for same query."""
- findings = {}
- query = "test query"
-
- findings.setdefault(query, [])
-
- for i in range(3):
- findings[query].append({"content": f"Finding {i}"})
-
- assert len(findings[query]) == 3
-
-
-class TestFindingRetrieval:
- """Tests for retrieving findings."""
-
- def test_get_findings_existing_query(self):
- """Should return findings for existing query."""
- findings = {"query1": [{"content": "content1"}]}
-
- result = findings.get("query1", [])
-
- assert len(result) == 1
- assert result[0]["content"] == "content1"
-
- def test_get_findings_nonexistent_query(self):
- """Should return empty list for nonexistent query."""
- findings = {"query1": [{"content": "content1"}]}
-
- result = findings.get("query2", [])
-
- assert result == []
-
-
-class TestFindingClearing:
- """Tests for clearing findings."""
-
- def test_clear_findings_removes_query(self):
- """Should remove findings for specific query."""
- findings = {
- "query1": [{"content": "content1"}],
- "query2": [{"content": "content2"}],
- }
-
- if "query1" in findings:
- del findings["query1"]
-
- assert "query1" not in findings
- assert "query2" in findings
-
- def test_clear_nonexistent_query(self):
- """Should handle clearing nonexistent query."""
- findings = {"query1": [{"content": "content1"}]}
-
- if "query2" in findings:
- del findings["query2"]
-
- # Should not raise error
- assert "query1" in findings
-
-
-class TestDocumentManagement:
- """Tests for document management."""
-
- def test_add_documents(self):
- """Should add documents to repository."""
- documents = []
- new_docs = ["doc1", "doc2", "doc3"]
-
- documents.extend(new_docs)
-
- assert len(documents) == 3
-
- def test_documents_accumulate(self):
- """Documents should accumulate across additions."""
- documents = ["existing"]
- new_docs = ["new1", "new2"]
-
- documents.extend(new_docs)
-
- assert len(documents) == 3
-
-
-class TestQuestionsManagement:
- """Tests for questions by iteration management."""
-
- def test_set_questions_by_iteration(self):
- """Should set questions by iteration."""
- questions_by_iteration = {}
- new_questions = {
- 1: ["Q1", "Q2"],
- 2: ["Q3", "Q4"],
- }
-
- questions_by_iteration = new_questions.copy()
-
- assert len(questions_by_iteration) == 2
- assert questions_by_iteration[1] == ["Q1", "Q2"]
-
- def test_questions_copy_not_reference(self):
- """Should copy questions, not reference."""
- new_questions = {1: ["Q1"]}
- questions_by_iteration = new_questions.copy()
-
- new_questions[1].append("Q2")
-
- # Original should be modified, copy should not
- assert len(new_questions[1]) == 2
- # Note: shallow copy - inner list is still referenced
- # The copy shares the same inner list reference
- assert len(questions_by_iteration[1]) == 2
-
-
-class TestSynthesisFormatting:
- """Tests for synthesis formatting."""
-
- def test_format_findings_to_text_structure(self):
- """Should format findings into text output."""
- findings_list = [
- {"phase": "Phase 1", "content": "Content 1"},
- {"phase": "Phase 2", "content": "Content 2"},
- ]
- synthesized_content = "Final synthesis content"
-
- # Simplified formatting
- output = f"Synthesis:\n{synthesized_content}\n\nFindings:\n"
- for f in findings_list:
- output += f"- {f['phase']}: {f['content']}\n"
-
- assert "Final synthesis content" in output
- assert "Phase 1" in output
-
- def test_format_empty_findings(self):
- """Should handle empty findings list."""
- findings_list = []
- synthesized_content = "Only synthesis"
-
- output = f"Synthesis:\n{synthesized_content}\n\n"
- if not findings_list:
- output += "No detailed findings."
-
- assert "No detailed findings" in output
-
-
-class TestSynthesisGeneration:
- """Tests for synthesis generation."""
-
- def test_synthesize_findings_prompt_structure(self):
- """Synthesis prompt should have correct structure."""
- query = "test query"
- sub_queries = ["sub1", "sub2"]
- current_knowledge = "Accumulated knowledge"
-
- prompt = f"""Original Query: {query}
-
-Accumulated Knowledge:
-{current_knowledge}
-
-Sub-questions asked (for context):
-{chr(10).join(f"- {sq}" for sq in sub_queries)}
-"""
-
- assert "test query" in prompt
- assert "Accumulated knowledge" in prompt
- assert "- sub1" in prompt
- assert "- sub2" in prompt
-
- def test_extract_finding_content(self):
- """Should extract content from findings."""
- findings = [
- {"content": "Content 1"},
- "String finding",
- {"content": "Content 2"},
- ]
-
- finding_texts = []
- for item in findings:
- if isinstance(item, dict) and "content" in item:
- finding_texts.append(item["content"])
- elif isinstance(item, str):
- finding_texts.append(item)
-
- assert len(finding_texts) == 3
- assert finding_texts[1] == "String finding"
-
- def test_knowledge_truncation_threshold(self):
- """Should truncate knowledge exceeding threshold."""
- current_knowledge = "x" * 30000
- max_chars = 24000
-
- if len(current_knowledge) > max_chars:
- first_part = current_knowledge[:12000]
- last_part = current_knowledge[-12000:]
- truncated = (
- f"{first_part}\n\n[...content truncated...]\n\n{last_part}"
- )
- else:
- truncated = current_knowledge
-
- assert "[...content truncated...]" in truncated
- assert len(truncated) < len(current_knowledge)
-
-
-class TestErrorTypeDetection:
- """Tests for error type detection in synthesis."""
-
- def test_detect_timeout_error(self):
- """Should detect timeout errors."""
- error_message = "Connection timed out after 30 seconds"
-
- error_type = "unknown"
- if (
- "timeout" in error_message.lower()
- or "timed out" in error_message.lower()
- ):
- error_type = "timeout"
-
- assert error_type == "timeout"
-
- def test_detect_token_limit_error(self):
- """Should detect token limit errors."""
- error_message = "Request exceeds context length limit"
-
- error_type = "unknown"
- if (
- "context length" in error_message.lower()
- or "token limit" in error_message.lower()
- ):
- error_type = "token_limit"
-
- assert error_type == "token_limit"
-
- def test_detect_rate_limit_error(self):
- """Should detect rate limit errors."""
- error_message = "API rate limit exceeded"
-
- error_type = "unknown"
- if (
- "rate limit" in error_message.lower()
- or "rate_limit" in error_message.lower()
- ):
- error_type = "rate_limit"
-
- assert error_type == "rate_limit"
-
- def test_detect_connection_error(self):
- """Should detect connection errors."""
- error_message = "Connection refused by server"
-
- error_type = "unknown"
- if (
- "connection" in error_message.lower()
- or "network" in error_message.lower()
- ):
- error_type = "connection"
-
- assert error_type == "connection"
-
- def test_detect_auth_error(self):
- """Should detect authentication errors."""
- error_message = "Invalid API key provided"
-
- error_type = "unknown"
- if (
- "api key" in error_message.lower()
- or "authentication" in error_message.lower()
- ):
- error_type = "authentication"
-
- assert error_type == "authentication"
-
-
-class TestLinkFormatting:
- """Tests for link formatting."""
-
- def test_format_links_basic(self):
- """Should format links correctly."""
- links = [
- {"title": "Link 1", "url": "http://example1.com"},
- {"title": "Link 2", "url": "http://example2.com"},
- ]
-
- formatted = "\n".join(
- f"{i + 1}. {link['title']}\n URL: {link['url']}"
- for i, link in enumerate(links)
- )
-
- assert "1. Link 1" in formatted
- assert "URL: http://example1.com" in formatted
-
- def test_format_empty_links(self):
- """Should handle empty links list."""
- links = []
-
- if not links:
- formatted = ""
- else:
- formatted = "Some links"
-
- assert formatted == ""
-
-
-class TestOldFormattingPath:
- """Tests for old formatting path."""
-
- def test_convert_string_findings_to_dicts(self):
- """Should convert string findings for old formatting."""
- findings = ["Finding 1", "Finding 2", {"phase": "P3", "content": "C3"}]
-
- findings_list = []
- for i, item in enumerate(findings):
- if isinstance(item, str):
- findings_list.append(
- {"phase": f"Finding {i + 1}", "content": item}
- )
- elif isinstance(item, dict):
- findings_list.append(item)
-
- assert len(findings_list) == 3
- assert findings_list[0]["phase"] == "Finding 1"
- assert findings_list[2]["phase"] == "P3"
-
-
-class TestTokenEstimation:
- """Tests for token estimation."""
-
- def test_estimate_tokens_from_chars(self):
- """Should estimate tokens from character count."""
- text = "x" * 4000
- chars_per_token = 4
-
- estimated_tokens = len(text) / chars_per_token
-
- assert estimated_tokens == 1000
-
- def test_check_token_limit_exceeded(self):
- """Should detect when token limit exceeded."""
- text = "x" * 60000 # ~15000 tokens
- max_safe_tokens = 12000
- chars_per_token = 4
-
- estimated_tokens = len(text) / chars_per_token
- exceeded = estimated_tokens > max_safe_tokens
-
- assert exceeded is True
diff --git a/tests/advanced_search_system/knowledge/test_base_knowledge.py b/tests/advanced_search_system/knowledge/test_base_knowledge.py
deleted file mode 100644
index 40571bf75f..0000000000
--- a/tests/advanced_search_system/knowledge/test_base_knowledge.py
+++ /dev/null
@@ -1,226 +0,0 @@
-"""
-Tests for the BaseKnowledgeGenerator class.
-
-Tests cover:
-- Initialization
-- Validation helpers
-- Key point extraction
-"""
-
-from unittest.mock import Mock
-
-
-class ConcreteKnowledgeGenerator:
- """Concrete implementation for testing the base pattern."""
-
- def __init__(self, model):
- self.model = model
-
- def generate(self, query, context):
- return "Generated knowledge"
-
- def generate_knowledge(
- self, query, context="", current_knowledge="", questions=None
- ):
- return "Generated knowledge"
-
- def generate_sub_knowledge(self, sub_query, context=""):
- return "Sub-knowledge"
-
- def compress_knowledge(
- self, current_knowledge, query, section_links, **kwargs
- ):
- return "Compressed knowledge"
-
- def format_citations(self, links):
- return "\n".join(f"[{i + 1}] {link}" for i, link in enumerate(links))
-
- def _validate_knowledge(self, knowledge):
- if not knowledge or not isinstance(knowledge, str):
- return False
- return True
-
- def _validate_links(self, links):
- if not isinstance(links, list):
- return False
- if not all(isinstance(link, str) for link in links):
- return False
- return True
-
- def _extract_key_points(self, knowledge):
- return knowledge.split("\n")
-
-
-class TestBaseKnowledgeGeneratorInit:
- """Tests for BaseKnowledgeGenerator initialization."""
-
- def test_init_stores_model(self):
- """Generator stores the model reference."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
- assert generator.model is mock_model
-
-
-class TestValidateKnowledge:
- """Tests for _validate_knowledge helper."""
-
- def test_validate_valid_knowledge(self):
- """Returns True for valid knowledge string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_knowledge("This is valid knowledge")
-
- assert result is True
-
- def test_validate_empty_knowledge(self):
- """Returns False for empty knowledge."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_knowledge("")
-
- assert result is False
-
- def test_validate_none_knowledge(self):
- """Returns False for None knowledge."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_knowledge(None)
-
- assert result is False
-
- def test_validate_non_string_knowledge(self):
- """Returns False for non-string knowledge."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_knowledge(123)
-
- assert result is False
-
-
-class TestValidateLinks:
- """Tests for _validate_links helper."""
-
- def test_validate_valid_links(self):
- """Returns True for valid list of link strings."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_links(["https://a.com", "https://b.com"])
-
- assert result is True
-
- def test_validate_empty_links(self):
- """Returns True for empty list."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_links([])
-
- assert result is True
-
- def test_validate_non_list_links(self):
- """Returns False for non-list input."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_links("not a list")
-
- assert result is False
-
- def test_validate_links_with_non_string_elements(self):
- """Returns False if any element is not a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._validate_links(["https://a.com", 123])
-
- assert result is False
-
-
-class TestExtractKeyPoints:
- """Tests for _extract_key_points helper."""
-
- def test_extract_single_line(self):
- """Extracts single line as one key point."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._extract_key_points("Single point")
-
- assert result == ["Single point"]
-
- def test_extract_multiple_lines(self):
- """Extracts multiple lines as multiple key points."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._extract_key_points("Point 1\nPoint 2\nPoint 3")
-
- assert len(result) == 3
- assert "Point 1" in result
- assert "Point 2" in result
- assert "Point 3" in result
-
- def test_extract_empty_knowledge(self):
- """Handles empty knowledge."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator._extract_key_points("")
-
- assert result == [""]
-
-
-class TestGenerateMethods:
- """Tests for generate methods."""
-
- def test_generate_returns_string(self):
- """generate returns a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator.generate("query", "context")
-
- assert isinstance(result, str)
-
- def test_generate_knowledge_returns_string(self):
- """generate_knowledge returns a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator.generate_knowledge("query")
-
- assert isinstance(result, str)
-
- def test_generate_sub_knowledge_returns_string(self):
- """generate_sub_knowledge returns a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator.generate_sub_knowledge("sub-query")
-
- assert isinstance(result, str)
-
- def test_compress_knowledge_returns_string(self):
- """compress_knowledge returns a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator.compress_knowledge("knowledge", "query", [])
-
- assert isinstance(result, str)
-
- def test_format_citations_returns_string(self):
- """format_citations returns a string."""
- mock_model = Mock()
- generator = ConcreteKnowledgeGenerator(mock_model)
-
- result = generator.format_citations(["https://a.com", "https://b.com"])
-
- assert isinstance(result, str)
- assert "[1]" in result
- assert "[2]" in result
diff --git a/tests/advanced_search_system/knowledge/test_standard_knowledge_extended.py b/tests/advanced_search_system/knowledge/test_standard_knowledge_extended.py
deleted file mode 100644
index 7dd8724921..0000000000
--- a/tests/advanced_search_system/knowledge/test_standard_knowledge_extended.py
+++ /dev/null
@@ -1,343 +0,0 @@
-"""
-Extended tests for StandardKnowledge - Standard knowledge generation.
-
-Tests cover:
-- Knowledge generation with various inputs
-- Sub-knowledge generation
-- Knowledge compression
-- Citation formatting
-- Error handling
-"""
-
-from datetime import datetime, UTC
-
-
-class TestKnowledgeGenerationBasics:
- """Tests for basic knowledge generation."""
-
- def test_prompt_includes_query(self):
- """Prompt should include the query."""
- query = "What is machine learning?"
- prompt = f"Query: {query}"
-
- assert "What is machine learning?" in prompt
-
- def test_prompt_includes_current_time(self):
- """Prompt should include current time."""
- now = datetime.now(UTC)
- current_time = now.strftime("%Y-%m-%d")
-
- prompt = f"Current Time: {current_time}"
-
- assert current_time in prompt
-
- def test_prompt_includes_context(self):
- """Prompt should include context if provided."""
- context = "Previous research showed X"
- prompt = f"Context: {context}"
-
- assert "Previous research showed X" in prompt
-
- def test_prompt_includes_current_knowledge(self):
- """Prompt should include current knowledge."""
- current_knowledge = "We already know Y"
- prompt = f"Current Knowledge: {current_knowledge}"
-
- assert "We already know Y" in prompt
-
-
-class TestKnowledgeGenerationWithQuestions:
- """Tests for knowledge generation with questions."""
-
- def test_prompt_with_questions(self):
- """Prompt should include questions when provided."""
- questions = ["Q1?", "Q2?", "Q3?"]
- prompt = f"Questions: {questions}"
-
- assert "Q1?" in prompt
- assert "Q2?" in prompt
-
- def test_prompt_without_questions(self):
- """Prompt should adapt when no questions provided."""
- questions = None
-
- if questions:
- prompt_part = f"Questions: {questions}"
- else:
- prompt_part = "No specific questions"
-
- assert "No specific questions" in prompt_part
- assert questions is None
-
- def test_prompt_addresses_each_question(self):
- """Generated knowledge should address each question."""
- questions = ["What is X?", "How does Y work?"]
-
- requirements = """Generate detailed knowledge that:
-1. Directly answers the query
-2. Addresses each question
-3. Includes relevant facts and details"""
-
- assert "Addresses each question" in requirements
- assert len(questions) == 2
-
-
-class TestSubKnowledgeGeneration:
- """Tests for sub-knowledge generation."""
-
- def test_sub_knowledge_prompt_structure(self):
- """Sub-knowledge prompt should have correct structure."""
- sub_query = "What is gradient descent?"
- context = "In the context of neural networks"
-
- prompt = f"""Generate comprehensive knowledge to answer this sub-question:
-
-Sub-question: {sub_query}
-
-{context}
-"""
-
- assert "What is gradient descent?" in prompt
- assert "neural networks" in prompt
-
- def test_sub_knowledge_empty_context(self):
- """Should handle empty context."""
- sub_query = "What is X?"
- context = ""
-
- prompt = f"""Sub-question: {sub_query}
-
-{context}
-"""
-
- assert "What is X?" in prompt
-
-
-class TestKnowledgeCompression:
- """Tests for knowledge compression."""
-
- def test_compression_prompt_includes_query(self):
- """Compression prompt should include query."""
- query = "machine learning"
- current_knowledge = "Lots of text..."
-
- prompt = f"""Compress the following accumulated knowledge relevant to the query '{query}'.
-Retain the key facts, findings, and citations. Remove redundancy.
-
-Accumulated Knowledge:
-{current_knowledge}
-
-Compressed Knowledge:"""
-
- assert "machine learning" in prompt
- assert "Lots of text..." in prompt
-
- def test_compression_failure_returns_original(self):
- """On compression failure, should return original."""
- original = "Original knowledge"
-
- try:
- raise Exception("Compression error")
- except Exception:
- result = original
-
- assert result == original
-
- def test_compression_log_format(self):
- """Should log compression details."""
- query = "test"
- original_length = 10000
- compressed_length = 5000
-
- log_message = f"Compressing knowledge for query: {query}. Original length: {original_length}, compressed to: {compressed_length}"
- assert "test" in log_message
- assert "10000" in log_message
- assert "5000" in log_message
-
-
-class TestCitationFormatting:
- """Tests for citation formatting."""
-
- def test_format_citations_ieee_style(self):
- """Should format citations in IEEE style."""
- links = [
- "http://example1.com",
- "http://example2.com",
- "http://example3.com",
- ]
-
- citations = []
- for i, link in enumerate(links, 1):
- citations.append(f"[{i}] {link}")
-
- formatted = "\n".join(citations)
-
- assert "[1] http://example1.com" in formatted
- assert "[2] http://example2.com" in formatted
-
- def test_format_empty_citations(self):
- """Should handle empty links list."""
- links = []
-
- if not links:
- formatted = ""
- else:
- formatted = "citations"
-
- assert formatted == ""
-
- def test_citation_numbering_starts_at_1(self):
- """Citation numbering should start at 1."""
- links = ["link1"]
-
- citations = []
- for i, link in enumerate(links, 1):
- citations.append(f"[{i}] {link}")
-
- assert "[1]" in citations[0]
- assert "[0]" not in citations[0]
-
-
-class TestGenerateMethod:
- """Tests for the simple generate method."""
-
- def test_generate_calls_generate_knowledge(self):
- """Generate should delegate to generate_knowledge."""
- query = "test query"
- context = "test context"
-
- # Simulate delegation
- result = {"query": query, "context": context}
-
- assert result["query"] == "test query"
- assert result["context"] == "test context"
-
-
-class TestKnowledgeRequirements:
- """Tests for knowledge generation requirements."""
-
- def test_requirements_directly_answers(self):
- """Knowledge should directly answer the query."""
- requirements = [
- "Directly answers the query",
- "Includes relevant facts and details",
- "Is up-to-date with current information",
- "Synthesizes information from multiple sources",
- ]
-
- assert "Directly answers the query" in requirements
-
- def test_requirements_formatting(self):
- """Knowledge should be well-structured."""
- format_requirement = (
- "Format your response as a well-structured paragraph."
- )
-
- assert "well-structured" in format_requirement
-
-
-class TestErrorHandling:
- """Tests for error handling."""
-
- def test_sub_knowledge_error_returns_empty(self):
- """Error in sub-knowledge should return empty string."""
- try:
- raise Exception("LLM error")
- except Exception:
- result = ""
-
- assert result == ""
-
- def test_compression_error_returns_original(self):
- """Error in compression should return original knowledge."""
- original_knowledge = "Original content"
-
- try:
- raise Exception("Compression error")
- except Exception:
- result = original_knowledge
-
- assert result == "Original content"
-
-
-class TestResponseHandling:
- """Tests for LLM response handling."""
-
- def test_extract_content_from_response(self):
- """Should extract content from response object."""
-
- class MockResponse:
- content = "Generated knowledge content"
-
- response = MockResponse()
- knowledge = response.content
-
- assert knowledge == "Generated knowledge content"
-
- def test_handle_string_response(self):
- """Should handle string response."""
- response = "Direct string response"
-
- if hasattr(response, "content"):
- knowledge = response.content
- else:
- knowledge = str(response)
-
- assert knowledge == "Direct string response"
-
-
-class TestDateTimeHandling:
- """Tests for datetime handling."""
-
- def test_utc_timestamp_format(self):
- """Should format UTC timestamp correctly."""
- now = datetime.now(UTC)
- formatted = now.strftime("%Y-%m-%d")
-
- # Should be YYYY-MM-DD format
- assert len(formatted) == 10
- assert formatted.count("-") == 2
-
- def test_current_time_in_prompt(self):
- """Current time should be included in prompt."""
- current_time = "2024-01-15"
-
- prompt = f"Current Time: {current_time}"
-
- assert "2024-01-15" in prompt
-
-
-class TestPromptVariations:
- """Tests for different prompt variations."""
-
- def test_prompt_with_all_params(self):
- """Prompt should include all parameters when provided."""
- query = "test query"
- context = "test context"
- current_knowledge = "existing knowledge"
- questions = ["Q1", "Q2"]
- current_time = "2024-01-15"
-
- prompt = f"""Query: {query}
-Current Time: {current_time}
-Context: {context}
-Current Knowledge: {current_knowledge}
-Questions: {questions}
-"""
-
- assert "test query" in prompt
- assert "test context" in prompt
- assert "existing knowledge" in prompt
- assert "Q1" in prompt
-
- def test_prompt_minimal_params(self):
- """Prompt should work with minimal parameters."""
- query = "simple query"
- current_time = "2024-01-15"
-
- prompt = f"""Query: {query}
-Current Time: {current_time}
-"""
-
- assert "simple query" in prompt
- assert "2024-01-15" in prompt
diff --git a/tests/advanced_search_system/questions/test_base_question.py b/tests/advanced_search_system/questions/test_base_question.py
deleted file mode 100644
index 67c05d840a..0000000000
--- a/tests/advanced_search_system/questions/test_base_question.py
+++ /dev/null
@@ -1,127 +0,0 @@
-"""
-Tests for the BaseQuestionGenerator class.
-
-Tests cover:
-- Initialization
-- Format previous questions helper
-"""
-
-from unittest.mock import Mock
-
-
-class ConcreteQuestionGenerator:
- """Concrete implementation for testing the base pattern."""
-
- def __init__(self, model):
- self.model = model
-
- def generate_questions(
- self,
- current_knowledge,
- query,
- questions_per_iteration,
- questions_by_iteration,
- ):
- return ["Question 1", "Question 2"]
-
- def _format_previous_questions(self, questions_by_iteration):
- formatted = []
- for iteration, questions in questions_by_iteration.items():
- formatted.append(f"Iteration {iteration}:")
- for q in questions:
- formatted.append(f"- {q}")
- return "\n".join(formatted)
-
-
-class TestBaseQuestionGeneratorInit:
- """Tests for BaseQuestionGenerator initialization."""
-
- def test_init_stores_model(self):
- """Generator stores the model reference."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
- assert generator.model is mock_model
-
-
-class TestFormatPreviousQuestions:
- """Tests for _format_previous_questions helper."""
-
- def test_format_empty_questions(self):
- """Formats empty questions dict."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- result = generator._format_previous_questions({})
-
- assert result == ""
-
- def test_format_single_iteration(self):
- """Formats single iteration questions."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- questions = {1: ["What is X?", "How does Y work?"]}
- result = generator._format_previous_questions(questions)
-
- assert "Iteration 1:" in result
- assert "- What is X?" in result
- assert "- How does Y work?" in result
-
- def test_format_multiple_iterations(self):
- """Formats multiple iteration questions."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- questions = {
- 1: ["Q1", "Q2"],
- 2: ["Q3", "Q4"],
- }
- result = generator._format_previous_questions(questions)
-
- assert "Iteration 1:" in result
- assert "Iteration 2:" in result
- assert "- Q1" in result
- assert "- Q4" in result
-
- def test_format_preserves_order(self):
- """Formatted output preserves iteration order."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- questions = {1: ["First"], 2: ["Second"]}
- result = generator._format_previous_questions(questions)
-
- assert result.index("Iteration 1:") < result.index("Iteration 2:")
-
-
-class TestGenerateQuestions:
- """Tests for generate_questions method."""
-
- def test_generate_questions_returns_list(self):
- """generate_questions returns a list."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- result = generator.generate_questions(
- current_knowledge="Some knowledge",
- query="Test query",
- questions_per_iteration=3,
- questions_by_iteration={},
- )
-
- assert isinstance(result, list)
-
- def test_generate_questions_returns_strings(self):
- """generate_questions returns list of strings."""
- mock_model = Mock()
- generator = ConcreteQuestionGenerator(mock_model)
-
- result = generator.generate_questions(
- current_knowledge="Knowledge",
- query="Query",
- questions_per_iteration=2,
- questions_by_iteration={},
- )
-
- for question in result:
- assert isinstance(question, str)
diff --git a/tests/advanced_search_system/questions/test_question_generator_extended.py b/tests/advanced_search_system/questions/test_question_generator_extended.py
deleted file mode 100644
index 92204e2982..0000000000
--- a/tests/advanced_search_system/questions/test_question_generator_extended.py
+++ /dev/null
@@ -1,316 +0,0 @@
-"""
-Extended tests for Question Generators - Follow-up and sub-question generation.
-
-Tests cover:
-- Standard question generation
-- Follow-up question generation
-- Sub-question decomposition
-- Context-aware query generation
-- Edge cases and error handling
-"""
-
-
-class TestStandardQuestionGeneration:
- """Tests for StandardQuestionGenerator."""
-
- def test_generate_questions_prompt_structure(self):
- """Prompt should have correct structure for question generation."""
- query = "What is quantum computing?"
- current_knowledge = "Quantum computing uses qubits..."
-
- prompt = f"""Based on the following query and current knowledge, generate follow-up questions:
-
-Query: {query}
-Current Knowledge: {current_knowledge}
-
-Generate 2 follow-up questions that would help deepen understanding."""
-
- assert "quantum computing" in prompt
- assert "qubits" in prompt
-
- def test_default_questions_per_iteration(self):
- """Default should be 2 questions per iteration."""
- default_count = 2
- assert default_count == 2
-
- def test_custom_questions_per_iteration(self):
- """Should support custom question count."""
- custom_count = 5
- assert custom_count == 5
-
-
-class TestSubQuestionGeneration:
- """Tests for sub-question generation."""
-
- def test_generate_sub_questions_from_complex_query(self):
- """Should break complex queries into sub-questions."""
- main_query = (
- "How does blockchain ensure security and what are its applications?"
- )
-
- # Simulated sub-question decomposition
- sub_questions = [
- "How does blockchain ensure security?",
- "What are the applications of blockchain?",
- ]
-
- assert len(sub_questions) == 2
- assert "security" in sub_questions[0]
- assert "applications" in sub_questions[1]
- # Sub-questions should relate to main query
- assert "blockchain" in main_query
-
- def test_simple_query_may_not_decompose(self):
- """Simple queries may not need decomposition."""
- simple_query = "What is Python?"
-
- # Simple query doesn't need decomposition
- sub_questions = [simple_query]
-
- assert len(sub_questions) == 1
-
-
-class TestFollowUpQuestionGeneration:
- """Tests for follow-up question generation."""
-
- def test_followup_analyzes_knowledge_gaps(self):
- """Follow-up questions should address knowledge gaps."""
- current_knowledge = "We know X but not Y"
-
- requirements = [
- "Critically reflects on knowledge timeliness",
- "Identifies gaps in current knowledge",
- "Generates targeted follow-up questions",
- ]
-
- assert "gaps" in requirements[1]
- # Knowledge statement indicates what we don't know
- assert "not Y" in current_knowledge
-
- def test_followup_question_count(self):
- """Should generate specified number of follow-up questions."""
- num_questions = 3
- questions = [f"Question {i + 1}?" for i in range(num_questions)]
-
- assert len(questions) == 3
-
-
-class TestContextualizedQueryGeneration:
- """Tests for contextualized query generation."""
-
- def test_simple_concatenation(self):
- """Simple generator concatenates context with query."""
- previous_context = "Previous findings show X"
- followup_query = "What about Y?"
-
- contextualized = f"{previous_context}\n\n{followup_query}"
-
- assert "Previous findings show X" in contextualized
- assert "What about Y?" in contextualized
-
- def test_preserves_exact_user_query(self):
- """Should preserve exact user query."""
- user_query = "provide data in a table"
-
- # Query should be preserved exactly
- assert user_query == "provide data in a table"
-
- def test_provides_full_context(self):
- """Should provide full context from previous research."""
- previous_research = {
- "findings": ["Finding 1", "Finding 2"],
- "sources": ["Source 1", "Source 2"],
- }
- followup_query = "More details?"
-
- context = f"Previous findings: {previous_research['findings']}\nQuery: {followup_query}"
-
- assert "Finding 1" in context
- assert "More details?" in context
-
-
-class TestLLMFollowUpGeneration:
- """Tests for LLM-based follow-up question generation."""
-
- def test_llm_reformulation_placeholder(self):
- """LLM reformulation is placeholder for future implementation."""
- # Current implementation falls back to simple concatenation
- is_placeholder = True
- assert is_placeholder is True
-
- def test_generates_multiple_targeted_questions(self):
- """Should generate multiple targeted questions."""
- num_questions = 5
- questions = [f"Targeted question {i + 1}" for i in range(num_questions)]
-
- assert len(questions) == 5
-
- def test_analyzes_followup_in_context(self):
- """Should analyze follow-up query in context of past findings."""
- past_findings = "We found A, B, C"
- followup = "What about D?"
-
- analysis_context = {
- "past_findings": past_findings,
- "followup_query": followup,
- }
-
- assert analysis_context["past_findings"] == "We found A, B, C"
-
-
-class TestQuestionQuality:
- """Tests for question quality requirements."""
-
- def test_questions_should_deepen_understanding(self):
- """Generated questions should deepen understanding."""
- requirements = [
- "Questions should deepen understanding",
- "Questions should address gaps",
- "Questions should be specific",
- ]
-
- assert "deepen understanding" in requirements[0]
-
- def test_questions_should_be_relevant(self):
- """Questions should be relevant to original query."""
- original_query = "machine learning"
- question = "How does supervised learning work?"
-
- # Question should relate to original query
- is_relevant = "learning" in question
- assert is_relevant is True
- # Both should contain the common topic
- assert "learning" in original_query
-
-
-class TestPromptConstruction:
- """Tests for prompt construction."""
-
- def test_prompt_includes_query(self):
- """Prompt should include the original query."""
- query = "test query"
- prompt = f"Query: {query}"
-
- assert "test query" in prompt
-
- def test_prompt_includes_current_knowledge(self):
- """Prompt should include current knowledge state."""
- knowledge = "Current state of knowledge"
- prompt = f"Current Knowledge: {knowledge}"
-
- assert "Current state of knowledge" in prompt
-
- def test_prompt_specifies_question_count(self):
- """Prompt should specify number of questions to generate."""
- num_questions = 3
- prompt = f"Generate {num_questions} follow-up questions"
-
- assert "3" in prompt
-
-
-class TestErrorHandling:
- """Tests for error handling in question generation."""
-
- def test_llm_error_handled_gracefully(self):
- """LLM errors should be handled gracefully."""
- try:
- raise Exception("LLM error")
- except Exception:
- questions = [] # Return empty on error
-
- assert questions == []
-
- def test_empty_knowledge_handled(self):
- """Should handle empty current knowledge."""
- current_knowledge = ""
-
- if not current_knowledge:
- knowledge_context = "No prior knowledge available"
- else:
- knowledge_context = current_knowledge
-
- assert knowledge_context == "No prior knowledge available"
-
-
-class TestResponseParsing:
- """Tests for parsing question generation responses."""
-
- def test_extract_questions_from_numbered_list(self):
- """Should extract questions from numbered list."""
- response = """1. What is X?
-2. How does Y work?
-3. Why is Z important?"""
-
- questions = []
- for line in response.strip().split("\n"):
- # Remove numbering
- if line.strip():
- cleaned = line.strip()
- if cleaned[0].isdigit() and "." in cleaned:
- cleaned = cleaned.split(".", 1)[1].strip()
- questions.append(cleaned)
-
- assert len(questions) == 3
- assert questions[0] == "What is X?"
-
- def test_extract_questions_from_bullet_list(self):
- """Should extract questions from bullet list."""
- response = """- What is X?
-- How does Y work?"""
-
- questions = []
- for line in response.strip().split("\n"):
- if line.startswith("-"):
- questions.append(line[1:].strip())
-
- assert len(questions) == 2
-
-
-class TestKnowledgeTimeliness:
- """Tests for knowledge timeliness reflection."""
-
- def test_reflects_on_knowledge_age(self):
- """Should reflect on timeliness of current knowledge."""
- requirements = """Critically reflects on knowledge timeliness.
-Considers whether information may be outdated.
-Generates questions about recent developments."""
-
- assert "timeliness" in requirements
- assert "outdated" in requirements
-
- def test_generates_update_questions(self):
- """Should generate questions about updates/changes."""
- sample_questions = [
- "Have there been recent updates to X?",
- "What are the latest developments in Y?",
- ]
-
- assert "recent" in sample_questions[0]
- assert "latest" in sample_questions[1]
-
-
-class TestContextPreservation:
- """Tests for context preservation in queries."""
-
- def test_table_reference_preserved(self):
- """References like 'provide data in a table' should be preserved."""
- followup = "provide data in a table"
- previous_findings = "Found data A, B, C"
-
- # User's exact query should be preserved
- query = followup
- context = previous_findings
-
- assert query == "provide data in a table"
- assert "Found data" in context
-
- def test_pronoun_references_understood(self):
- """Pronoun references should be understood from context."""
- previous = "We discussed Python programming"
- followup = "What are its main features?"
-
- # 'its' refers to Python from context
- full_context = f"{previous}\n{followup}"
-
- assert "Python" in full_context
- assert "its main features" in full_context
diff --git a/tests/api/test_research_functions_extended.py b/tests/api/test_research_functions_extended.py
deleted file mode 100644
index 3842dd6c22..0000000000
--- a/tests/api/test_research_functions_extended.py
+++ /dev/null
@@ -1,509 +0,0 @@
-"""
-Extended tests for research_functions API - Programmatic research access.
-
-Tests cover:
-- Search system initialization
-- Quick summary generation
-- Report generation
-- Detailed research
-- Document analysis
-- Settings handling
-- Error handling and edge cases
-"""
-
-from datetime import datetime, UTC
-
-
-class TestSearchSystemInitialization:
- """Tests for _init_search_system function."""
-
- def test_default_search_strategy(self):
- """Default search strategy should be source_based."""
- default_strategy = "source_based"
- assert default_strategy == "source_based"
-
- def test_default_iterations(self):
- """Default iterations should be 1."""
- default_iterations = 1
- assert default_iterations == 1
-
- def test_default_questions_per_iteration(self):
- """Default questions per iteration should be 1."""
- default_questions = 1
- assert default_questions == 1
-
- def test_default_temperature(self):
- """Default temperature should be 0.7."""
- default_temp = 0.7
- assert default_temp == 0.7
-
- def test_programmatic_mode_default_true(self):
- """Programmatic mode should default to True for API."""
- programmatic_mode = True
- assert programmatic_mode is True
-
- def test_search_original_query_default_true(self):
- """Search original query should default to True."""
- search_original_query = True
- assert search_original_query is True
-
- def test_retriever_registration_format(self):
- """Retrievers should be registered as dict."""
- retrievers = {"custom": "retriever_instance"}
- assert "custom" in retrievers
- assert isinstance(retrievers, dict)
-
- def test_llm_registration_format(self):
- """LLMs should be registered as dict."""
- llms = {"custom_llm": "llm_instance"}
- assert "custom_llm" in llms
- assert isinstance(llms, dict)
-
-
-class TestQuickSummary:
- """Tests for quick_summary function."""
-
- def test_required_query_parameter(self):
- """Query parameter is required."""
- query = "What is quantum computing?"
- assert query is not None
- assert len(query) > 0
-
- def test_return_structure_has_summary(self):
- """Return should have summary key."""
- result = {
- "summary": "Summary text",
- "findings": [],
- "iterations": 1,
- "questions": {},
- }
- assert "summary" in result
-
- def test_return_structure_has_findings(self):
- """Return should have findings key."""
- result = {
- "summary": "Summary text",
- "findings": [{"content": "finding1"}],
- }
- assert "findings" in result
-
- def test_return_structure_has_iterations(self):
- """Return should have iterations key."""
- result = {
- "summary": "Summary text",
- "iterations": 3,
- }
- assert "iterations" in result
- assert result["iterations"] == 3
-
- def test_return_structure_has_questions(self):
- """Return should have questions key."""
- result = {
- "summary": "Summary text",
- "questions": {"1": ["Q1", "Q2"]},
- }
- assert "questions" in result
-
- def test_return_structure_has_sources(self):
- """Return should have sources key."""
- result = {
- "summary": "Summary text",
- "sources": ["http://example.com"],
- }
- assert "sources" in result
-
- def test_research_id_auto_generation(self):
- """Research ID should be auto-generated if not provided."""
- import uuid
-
- research_id = None
- if research_id is None:
- research_id = str(uuid.uuid4())
-
- assert research_id is not None
- assert len(research_id) == 36 # UUID format
-
- def test_search_context_structure(self):
- """Search context should have required fields."""
- query = "test query"
- research_id = "test-id"
-
- search_context = {
- "research_id": research_id,
- "research_query": query,
- "research_mode": "quick",
- "research_phase": "init",
- "search_iteration": 0,
- }
-
- assert search_context["research_mode"] == "quick"
- assert search_context["research_phase"] == "init"
-
-
-class TestGenerateReport:
- """Tests for generate_report function."""
-
- def test_output_file_optional(self):
- """Output file parameter should be optional."""
- output_file = None
- assert output_file is None
-
- def test_default_searches_per_section(self):
- """Default searches per section should be 2."""
- default_searches = 2
- assert default_searches == 2
-
- def test_return_has_content(self):
- """Return should have content key."""
- result = {
- "content": "# Report\n\nContent here",
- "metadata": {},
- }
- assert "content" in result
-
- def test_return_has_metadata(self):
- """Return should have metadata key."""
- result = {
- "content": "Report",
- "metadata": {"timestamp": "2024-01-01"},
- }
- assert "metadata" in result
-
- def test_file_path_in_return_when_saved(self):
- """File path should be in return when saved."""
- result = {
- "content": "Report",
- "file_path": "/path/to/report.md",
- }
- assert "file_path" in result
-
- def test_progress_callback_optional(self):
- """Progress callback should be optional."""
- progress_callback = None
- assert progress_callback is None
-
-
-class TestDetailedResearch:
- """Tests for detailed_research function."""
-
- def test_return_has_query(self):
- """Return should have query key."""
- result = {
- "query": "test query",
- "research_id": "id",
- }
- assert "query" in result
-
- def test_return_has_research_id(self):
- """Return should have research_id key."""
- result = {
- "query": "test",
- "research_id": "test-id-123",
- }
- assert "research_id" in result
-
- def test_return_has_metadata(self):
- """Return should have metadata with details."""
- result = {
- "metadata": {
- "timestamp": datetime.now(UTC).isoformat(),
- "search_tool": "auto",
- "iterations_requested": 1,
- "strategy": "source_based",
- }
- }
- assert "timestamp" in result["metadata"]
- assert "strategy" in result["metadata"]
-
- def test_metadata_timestamp_format(self):
- """Metadata timestamp should be ISO format."""
- timestamp = datetime.now(UTC).isoformat()
-
- # Should contain T separator
- assert "T" in timestamp
-
- def test_default_search_tool_auto(self):
- """Default search tool should be 'auto'."""
- search_tool = "auto"
- assert search_tool == "auto"
-
-
-class TestAnalyzeDocuments:
- """Tests for analyze_documents function."""
-
- def test_collection_name_required(self):
- """Collection name parameter is required."""
- collection_name = "my_collection"
- assert collection_name is not None
-
- def test_default_max_results(self):
- """Default max results should be 10."""
- max_results = 10
- assert max_results == 10
-
- def test_default_temperature(self):
- """Default temperature should be 0.7."""
- temperature = 0.7
- assert temperature == 0.7
-
- def test_force_reindex_default_false(self):
- """Force reindex should default to False."""
- force_reindex = False
- assert force_reindex is False
-
- def test_return_has_summary(self):
- """Return should have summary key."""
- result = {
- "summary": "Analysis summary",
- "documents": [],
- }
- assert "summary" in result
-
- def test_return_has_documents(self):
- """Return should have documents key."""
- result = {
- "summary": "Summary",
- "documents": [{"title": "Doc1"}],
- }
- assert "documents" in result
-
- def test_return_has_collection_name(self):
- """Return should have collection name."""
- result = {
- "summary": "Summary",
- "documents": [],
- "collection": "my_collection",
- }
- assert result["collection"] == "my_collection"
-
- def test_return_has_document_count(self):
- """Return should have document count."""
- result = {
- "summary": "Summary",
- "documents": [{"title": "D1"}, {"title": "D2"}],
- "document_count": 2,
- }
- assert result["document_count"] == 2
-
- def test_collection_not_found_error(self):
- """Should return error when collection not found."""
- collection_name = "nonexistent"
- search = None
-
- if not search:
- result = {
- "summary": f"Error: Collection '{collection_name}' not found",
- "documents": [],
- }
- else:
- result = {"summary": "Found", "documents": []}
-
- assert "not found" in result["summary"]
-
- def test_no_documents_found_message(self):
- """Should return message when no documents found."""
- collection_name = "my_collection"
- query = "test query"
- results = []
-
- if not results:
- summary = f"No documents found in collection '{collection_name}' for query: '{query}'"
- else:
- summary = "Found documents"
-
- assert "No documents found" in summary
-
-
-class TestSettingsSnapshot:
- """Tests for settings snapshot handling."""
-
- def test_snapshot_from_explicit_params(self):
- """Should build snapshot from explicit parameters."""
- provider = "openai"
- api_key = "sk-test"
- temperature = 0.5
-
- snapshot_kwargs = {}
- if provider is not None:
- snapshot_kwargs["provider"] = provider
- if api_key is not None:
- snapshot_kwargs["api_key"] = api_key
- if temperature is not None:
- snapshot_kwargs["temperature"] = temperature
-
- assert snapshot_kwargs["provider"] == "openai"
- assert snapshot_kwargs["temperature"] == 0.5
-
- def test_snapshot_overrides(self):
- """Should apply settings overrides."""
- settings_override = {
- "llm.max_tokens": 4000,
- "search.engines.arxiv.enabled": True,
- }
-
- assert "llm.max_tokens" in settings_override
- assert settings_override["llm.max_tokens"] == 4000
-
- def test_base_settings_support(self):
- """Should support base settings dict."""
- base_settings = {
- "llm.provider": "anthropic",
- "search.tool": "wikipedia",
- }
-
- assert isinstance(base_settings, dict)
- assert "llm.provider" in base_settings
-
-
-class TestSearchContextSetup:
- """Tests for search context setup."""
-
- def test_context_has_research_id(self):
- """Context should have research_id."""
- context = {
- "research_id": "test-123",
- "research_query": "test",
- }
- assert "research_id" in context
-
- def test_context_has_research_query(self):
- """Context should have research_query."""
- context = {
- "research_id": "id",
- "research_query": "What is AI?",
- }
- assert context["research_query"] == "What is AI?"
-
- def test_context_has_research_mode(self):
- """Context should have research_mode."""
- context = {
- "research_mode": "quick",
- }
- assert context["research_mode"] == "quick"
-
- def test_context_has_research_phase(self):
- """Context should have research_phase."""
- context = {
- "research_phase": "init",
- }
- assert context["research_phase"] == "init"
-
- def test_context_has_search_iteration(self):
- """Context should have search_iteration."""
- context = {
- "search_iteration": 0,
- }
- assert context["search_iteration"] == 0
-
-
-class TestErrorHandling:
- """Tests for error handling."""
-
- def test_unable_to_generate_summary_fallback(self):
- """Should have fallback message for failed summary."""
- results = None
-
- if results and "current_knowledge" in results:
- summary = results["current_knowledge"]
- else:
- summary = "Unable to generate summary for the query."
-
- assert summary == "Unable to generate summary for the query."
-
- def test_search_engine_creation_warning(self):
- """Should warn when search engine creation fails."""
- search_tool = "invalid_engine"
- search_engine = None
-
- if search_engine is None:
- warning = f"Could not create search engine '{search_tool}', using default."
- else:
- warning = None
-
- assert warning is not None
- assert "invalid_engine" in warning
-
-
-class TestRetrieverRegistration:
- """Tests for retriever registration."""
-
- def test_register_multiple_retrievers(self):
- """Should register multiple retrievers."""
- retrievers = {
- "custom1": "retriever1",
- "custom2": "retriever2",
- }
-
- registered_count = len(retrievers)
- registered_names = list(retrievers.keys())
-
- assert registered_count == 2
- assert "custom1" in registered_names
-
-
-class TestLLMRegistration:
- """Tests for LLM registration."""
-
- def test_register_multiple_llms(self):
- """Should register multiple LLMs."""
- llms = {
- "llm1": "instance1",
- "llm2": "instance2",
- }
-
- registered_count = len(llms)
- assert registered_count == 2
-
- def test_llm_name_in_registration(self):
- """LLM name should be preserved in registration."""
- llms = {"my_custom_llm": "instance"}
-
- for name, _instance in llms.items():
- assert name == "my_custom_llm"
-
-
-class TestOutputFileSaving:
- """Tests for output file saving."""
-
- def test_report_content_format(self):
- """Report content should be markdown format."""
- content = "# Report Title\n\n## Section 1\n\nContent..."
-
- assert content.startswith("#")
- assert "##" in content
-
- def test_analysis_output_format(self):
- """Analysis output should include all sections."""
- query = "test query"
- summary = "Analysis summary"
- doc_count = 5
-
- content = f"# Document Analysis: {query}\n\n"
- content += f"## Summary\n\n{summary}\n\n"
- content += f"## Documents Found: {doc_count}\n\n"
-
- assert "Document Analysis" in content
- assert "Summary" in content
- assert "Documents Found: 5" in content
-
-
-class TestProgressCallback:
- """Tests for progress callback support."""
-
- def test_callback_function_optional(self):
- """Callback function should be optional."""
- callback = None
- assert callback is None
-
- def test_callback_receives_progress(self):
- """Callback should receive progress updates."""
- received_updates = []
-
- def callback(message, progress, data):
- received_updates.append((message, progress, data))
-
- # Simulate progress update
- callback("Processing", 50, {"phase": "analysis"})
-
- assert len(received_updates) == 1
- assert received_updates[0][1] == 50
diff --git a/tests/config/test_llm_config_context.py b/tests/config/test_llm_config_context.py
deleted file mode 100644
index 4ba46a0ab3..0000000000
--- a/tests/config/test_llm_config_context.py
+++ /dev/null
@@ -1,352 +0,0 @@
-"""
-Tests for LLM config context window and token counting.
-
-Tests cover:
-- Context window calculation
-- Token counting integration
-- Settings integration
-"""
-
-from unittest.mock import Mock
-
-
-class TestContextWindowCalculation:
- """Tests for context window size calculation."""
-
- def test_context_window_local_provider_detection(self):
- """Local providers are detected correctly."""
- local_providers = ["ollama", "llamacpp", "lmstudio"]
- cloud_providers = ["openai", "anthropic", "google"]
-
- for provider in local_providers:
- is_local = provider in ["ollama", "llamacpp", "lmstudio"]
- assert is_local, f"{provider} should be detected as local"
-
- for provider in cloud_providers:
- is_local = provider in ["ollama", "llamacpp", "lmstudio"]
- assert not is_local, f"{provider} should not be detected as local"
-
- def test_context_window_cloud_provider_detection(self):
- """Cloud providers are detected correctly."""
- cloud_providers = ["openai", "anthropic", "google", "openrouter"]
-
- for provider in cloud_providers:
- is_cloud = provider not in [
- "ollama",
- "llamacpp",
- "lmstudio",
- ]
- assert is_cloud, f"{provider} should be detected as cloud"
-
- def test_context_window_unrestricted_mode(self):
- """Unrestricted mode returns None for cloud providers."""
- use_unrestricted = True
- provider = "openai"
-
- if use_unrestricted and provider not in [
- "ollama",
- "llamacpp",
- "lmstudio",
- ]:
- context_window = None
- else:
- context_window = 128000
-
- assert context_window is None
-
- def test_context_window_restricted_mode(self):
- """Restricted mode uses configured window size."""
- use_unrestricted = False
- configured_size = 32000
-
- if not use_unrestricted:
- context_window = configured_size
- else:
- context_window = None
-
- assert context_window == 32000
-
- def test_context_window_max_tokens_80_percent(self):
- """Max tokens is 80% of context window."""
- context_window_size = 4096
- max_tokens_setting = 100000
-
- # 80% of context window
- calculated_max_tokens = int(context_window_size * 0.8)
-
- # Use minimum of setting and 80%
- max_tokens = min(max_tokens_setting, calculated_max_tokens)
-
- assert max_tokens == 3276 # 80% of 4096
-
- def test_context_window_context_limit_overflow_detection(self):
- """Context limit is set in research context for overflow detection."""
- research_context = {}
- context_window_size = 8192
-
- if research_context is not None and context_window_size:
- research_context["context_limit"] = context_window_size
-
- assert "context_limit" in research_context
- assert research_context["context_limit"] == 8192
-
- def test_context_window_ollama_specific_handling(self):
- """Ollama uses local context window size."""
- provider = "ollama"
- local_context_window_size = 4096
- cloud_context_window_size = 128000
-
- if provider in ["ollama", "llamacpp", "lmstudio"]:
- window_size = local_context_window_size
- else:
- window_size = cloud_context_window_size
-
- assert window_size == 4096
-
- def test_context_window_anthropic_specific_handling(self):
- """Anthropic uses cloud context handling."""
- provider = "anthropic"
- use_unrestricted = True
-
- if (
- provider not in ["ollama", "llamacpp", "lmstudio"]
- and use_unrestricted
- ):
- window_size = None # Let provider auto-handle
- else:
- window_size = 200000
-
- assert window_size is None
-
- def test_context_window_openai_specific_handling(self):
- """OpenAI uses cloud context handling."""
- provider = "openai"
- use_unrestricted = False
- configured_size = 128000
-
- if (
- provider not in ["ollama", "llamacpp", "lmstudio"]
- and not use_unrestricted
- ):
- window_size = configured_size
- else:
- window_size = None
-
- assert window_size == 128000
-
- def test_context_window_custom_endpoint_handling(self):
- """Custom OpenAI endpoint uses cloud handling."""
- provider = "openai_endpoint"
- is_local = provider in ["ollama", "llamacpp", "lmstudio"]
-
- assert not is_local
-
- def test_context_window_default_fallback(self):
- """Default context window is used when not configured."""
- default_local_window = 8192
- default_cloud_window = 128000
-
- # Local default
- assert default_local_window == 8192
-
- # Cloud default
- assert default_cloud_window == 128000
-
- def test_context_window_model_name_lookup(self):
- """Context window lookup by model name."""
- model_context_windows = {
- "gpt-4": 128000,
- "gpt-3.5-turbo": 16385,
- "claude-3-opus": 200000,
- "mistral": 4096,
- }
-
- assert model_context_windows.get("gpt-4") == 128000
- assert model_context_windows.get("claude-3-opus") == 200000
- assert model_context_windows.get("unknown", 4096) == 4096
-
-
-class TestTokenCountingIntegration:
- """Tests for token counting callback integration."""
-
- def test_token_counting_callback_attachment(self):
- """Token counting callback is attached to LLM."""
- research_id = 123
- callbacks = []
-
- if research_id is not None:
- # Create mock callback
- mock_callback = Mock()
- callbacks.append(mock_callback)
-
- assert len(callbacks) == 1
-
- def test_token_counting_provider_preset(self):
- """Provider is preset on token callback."""
- provider = "openai"
- token_callback = Mock()
-
- if provider:
- token_callback.preset_provider = provider
-
- assert token_callback.preset_provider == "openai"
-
- def test_token_counting_model_preset(self):
- """Model name is preset on token callback."""
- model_name = "gpt-4"
- token_callback = Mock()
-
- token_callback.preset_model = model_name
-
- assert token_callback.preset_model == "gpt-4"
-
- def test_token_counting_research_context_mutation(self):
- """Research context is updated with token counts."""
- research_context = {"context_limit": 4096}
- token_count = {"prompt_tokens": 100, "completion_tokens": 200}
-
- research_context.update(token_count)
-
- assert research_context["prompt_tokens"] == 100
- assert research_context["completion_tokens"] == 200
-
- def test_token_counting_prompt_tokens(self):
- """Prompt tokens are counted correctly."""
- prompt_tokens = 150
-
- assert prompt_tokens > 0
- assert isinstance(prompt_tokens, int)
-
- def test_token_counting_completion_tokens(self):
- """Completion tokens are counted correctly."""
- completion_tokens = 250
-
- assert completion_tokens > 0
- assert isinstance(completion_tokens, int)
-
- def test_token_counting_total_accumulation(self):
- """Total tokens accumulate correctly."""
- calls = [
- {"prompt": 100, "completion": 200},
- {"prompt": 150, "completion": 300},
- {"prompt": 50, "completion": 100},
- ]
-
- total_prompt = sum(c["prompt"] for c in calls)
- total_completion = sum(c["completion"] for c in calls)
-
- assert total_prompt == 300
- assert total_completion == 600
-
- def test_token_counting_error_handling(self):
- """Token counting handles errors gracefully."""
- error_occurred = False
-
- try:
- # Simulate token counting
- pass
- except Exception:
- error_occurred = True
-
- assert not error_occurred
-
-
-class TestSettingsIntegration:
- """Tests for settings snapshot integration."""
-
- def test_settings_snapshot_provider_selection(self):
- """Provider is selected from settings snapshot."""
- snapshot = {"llm.provider": "anthropic"}
-
- provider = snapshot.get("llm.provider", "ollama")
-
- assert provider == "anthropic"
-
- def test_settings_snapshot_model_override(self):
- """Model can be overridden via parameter."""
- snapshot = {"llm.model": "default-model"}
- override_model = "custom-model"
-
- model = override_model if override_model else snapshot.get("llm.model")
-
- assert model == "custom-model"
-
- def test_settings_snapshot_temperature_override(self):
- """Temperature can be overridden via parameter."""
- snapshot = {"llm.temperature": 0.7}
- override_temperature = 0.3
-
- temperature = (
- override_temperature
- if override_temperature is not None
- else snapshot.get("llm.temperature", 0.7)
- )
-
- assert temperature == 0.3
-
- def test_settings_snapshot_missing_key_defaults(self):
- """Missing keys use default values."""
- snapshot = {}
-
- provider = snapshot.get("llm.provider", "ollama")
- model = snapshot.get("llm.model", "gemma3:12b")
- temperature = snapshot.get("llm.temperature", 0.7)
-
- assert provider == "ollama"
- assert model == "gemma3:12b"
- assert temperature == 0.7
-
- def test_settings_snapshot_invalid_type_handling(self):
- """Invalid setting types are handled."""
- snapshot = {
- "llm.temperature": "not_a_number",
- "llm.max_tokens": "invalid",
- }
-
- # Temperature should be converted or default used
- try:
- temperature = float(snapshot.get("llm.temperature", 0.7))
- except (ValueError, TypeError):
- temperature = 0.7
-
- assert temperature == 0.7
-
-
-class TestContextWindowEdgeCases:
- """Tests for context window edge cases."""
-
- def test_context_window_zero_value(self):
- """Zero context window uses default."""
- configured_size = 0
- default_size = 8192
-
- window_size = configured_size if configured_size > 0 else default_size
-
- assert window_size == 8192
-
- def test_context_window_negative_value(self):
- """Negative context window uses default."""
- configured_size = -1000
- default_size = 8192
-
- window_size = configured_size if configured_size > 0 else default_size
-
- assert window_size == 8192
-
- def test_context_window_very_large_value(self):
- """Very large context window is capped."""
- configured_size = 10000000 # 10M tokens
- max_allowed = 1000000 # 1M tokens
-
- window_size = min(configured_size, max_allowed)
-
- assert window_size == max_allowed
-
- def test_context_window_float_conversion(self):
- """Float context window is converted to int."""
- configured_size = 4096.5
-
- window_size = int(configured_size) if configured_size else 4096
-
- assert window_size == 4096
- assert isinstance(window_size, int)
diff --git a/tests/config/test_llm_config_ollama.py b/tests/config/test_llm_config_ollama.py
deleted file mode 100644
index 3fc6c9d6b3..0000000000
--- a/tests/config/test_llm_config_ollama.py
+++ /dev/null
@@ -1,368 +0,0 @@
-"""
-Tests for LLM config Ollama provider specifics.
-
-Tests cover:
-- Ollama provider edge cases
-- Ollama availability checks
-"""
-
-
-class TestOllamaProviderEdgeCases:
- """Tests for Ollama provider edge cases."""
-
- def test_ollama_model_not_found_error(self):
- """Ollama model not found returns fallback."""
- model_name = "nonexistent-model"
- available_models = ["mistral", "llama2", "codellama"]
-
- model_found = model_name.lower() in [
- m.lower() for m in available_models
- ]
-
- assert not model_found
-
- def test_ollama_service_unavailable_503(self):
- """Ollama 503 triggers fallback."""
- status_code = 503
-
- if status_code == 503:
- use_fallback = True
- else:
- use_fallback = False
-
- assert use_fallback
-
- def test_ollama_connection_refused(self):
- """Connection refused triggers fallback."""
- error_message = "Connection refused: localhost:11434"
-
- if "connection refused" in error_message.lower():
- use_fallback = True
- else:
- use_fallback = False
-
- assert use_fallback
-
- def test_ollama_timeout_handling(self):
- """Timeout triggers fallback."""
- error_message = "Request timeout"
-
- if "timeout" in error_message.lower():
- use_fallback = True
- else:
- use_fallback = False
-
- assert use_fallback
-
- def test_ollama_thinking_mode_enabled(self):
- """Thinking mode enables reasoning parameter."""
- enable_thinking = True
-
- if enable_thinking:
- ollama_params = {"reasoning": True}
- else:
- ollama_params = {}
-
- assert ollama_params.get("reasoning") is True
-
- def test_ollama_thinking_mode_disabled(self):
- """Thinking mode disabled omits reasoning parameter."""
- enable_thinking = False
-
- if enable_thinking:
- ollama_params = {"reasoning": True}
- else:
- ollama_params = {}
-
- assert (
- "reasoning" not in ollama_params
- or ollama_params.get("reasoning") is False
- )
-
- def test_ollama_base_url_normalization_trailing_slash(self):
- """Base URL trailing slash is normalized."""
- raw_url = "http://localhost:11434/"
-
- # Normalize by removing trailing slash
- normalized_url = raw_url.rstrip("/")
-
- assert normalized_url == "http://localhost:11434"
-
- def test_ollama_base_url_normalization_no_slash(self):
- """Base URL without trailing slash is kept."""
- raw_url = "http://localhost:11434"
-
- normalized_url = raw_url.rstrip("/")
-
- assert normalized_url == "http://localhost:11434"
-
- def test_ollama_api_format_default(self):
- """Default API format is Ollama native."""
- api_format = "ollama"
-
- assert api_format == "ollama"
-
- def test_ollama_api_format_openai_compatible(self):
- """OpenAI compatible format is supported."""
- api_format = "openai_compatible"
-
- # Some Ollama setups use OpenAI format
- assert api_format in ["ollama", "openai_compatible"]
-
- def test_ollama_model_list_empty(self):
- """Empty model list triggers fallback."""
- models = []
-
- if not models:
- use_fallback = True
- else:
- use_fallback = False
-
- assert use_fallback
-
- def test_ollama_model_list_parsing(self):
- """Model list is parsed correctly."""
- response_data = {
- "models": [
- {"name": "mistral:latest", "size": 4000000000},
- {"name": "llama2:7b", "size": 3500000000},
- ]
- }
-
- models = [
- m.get("name", "").lower() for m in response_data.get("models", [])
- ]
-
- assert "mistral:latest" in models
- assert "llama2:7b" in models
-
- def test_ollama_keep_alive_parameter(self):
- """Keep alive parameter is configurable."""
- keep_alive = "5m" # 5 minutes
-
- assert keep_alive in ["5m", "10m", "30m", "1h", "-1"]
-
- def test_ollama_num_ctx_parameter(self):
- """Context size (num_ctx) parameter is set."""
- context_window_size = 8192
- ollama_params = {}
-
- if context_window_size is not None:
- ollama_params["num_ctx"] = context_window_size
-
- assert ollama_params["num_ctx"] == 8192
-
- def test_ollama_repeat_penalty_parameter(self):
- """Repeat penalty parameter is configurable."""
- repeat_penalty = 1.1
-
- # Default is usually 1.1
- assert 1.0 <= repeat_penalty <= 2.0
-
-
-class TestOllamaAvailability:
- """Tests for Ollama availability checks."""
-
- def test_ollama_is_available_responds_200(self):
- """Ollama available when API returns 200."""
- status_code = 200
-
- is_available = status_code == 200
-
- assert is_available
-
- def test_ollama_is_available_responds_non_200(self):
- """Ollama unavailable when API returns non-200."""
- status_codes = [400, 401, 403, 404, 500, 502, 503]
-
- for status_code in status_codes:
- is_available = status_code == 200
- assert not is_available, (
- f"Status {status_code} should be unavailable"
- )
-
- def test_ollama_is_available_connection_error(self):
- """Ollama unavailable on connection error."""
- connection_error = True
-
- if connection_error:
- is_available = False
- else:
- is_available = True
-
- assert not is_available
-
- def test_ollama_is_available_timeout(self):
- """Ollama unavailable on timeout."""
- timeout_error = True
-
- if timeout_error:
- is_available = False
- else:
- is_available = True
-
- assert not is_available
-
- def test_ollama_is_available_dns_resolution_failure(self):
- """Ollama unavailable on DNS failure."""
- error_message = "Name or service not known"
-
- if (
- "service not known" in error_message.lower()
- or "dns" in error_message.lower()
- ):
- is_available = False
- else:
- is_available = True
-
- assert not is_available
-
- def test_ollama_is_available_ssl_error(self):
- """Ollama unavailable on SSL error."""
- error_message = "SSL: CERTIFICATE_VERIFY_FAILED"
-
- if "ssl" in error_message.lower():
- is_available = False
- else:
- is_available = True
-
- assert not is_available
-
- def test_ollama_is_available_custom_port(self):
- """Ollama availability check uses custom port."""
- url = "http://localhost:8080"
-
- # Extract port
- port = url.split(":")[-1].split("/")[0]
-
- assert port == "8080"
-
- def test_ollama_is_available_ipv6_address(self):
- """Ollama supports IPv6 addresses."""
- url = "http://[::1]:11434"
-
- # IPv6 localhost
- assert "[::1]" in url
-
- def test_ollama_is_available_localhost_variants(self):
- """Various localhost variants are supported."""
- variants = [
- "http://localhost:11434",
- "http://127.0.0.1:11434",
- "http://[::1]:11434",
- "http://0.0.0.0:11434",
- ]
-
- for url in variants:
- # All should be valid localhost URLs
- assert (
- "localhost" in url
- or "127.0.0.1" in url
- or "::1" in url
- or "0.0.0.0" in url
- )
-
- def test_ollama_is_available_caching(self):
- """Availability check can be cached."""
- cache = {}
- cache_key = "ollama_available"
-
- # First check
- cache[cache_key] = True
-
- # Second check uses cache
- is_available = cache.get(cache_key)
-
- assert is_available
-
-
-class TestOllamaModelParsing:
- """Tests for Ollama model name parsing."""
-
- def test_model_name_with_tag(self):
- """Model name with tag is parsed correctly."""
- model_name = "mistral:7b-instruct"
-
- parts = model_name.split(":")
- base_name = parts[0]
- tag = parts[1] if len(parts) > 1 else "latest"
-
- assert base_name == "mistral"
- assert tag == "7b-instruct"
-
- def test_model_name_without_tag(self):
- """Model name without tag defaults to latest."""
- model_name = "mistral"
-
- parts = model_name.split(":")
- base_name = parts[0]
- tag = parts[1] if len(parts) > 1 else "latest"
-
- assert base_name == "mistral"
- assert tag == "latest"
-
- def test_model_name_case_insensitive(self):
- """Model name matching is case insensitive."""
- model_name = "MISTRAL"
- available_models = ["mistral", "llama2"]
-
- found = model_name.lower() in [m.lower() for m in available_models]
-
- assert found
-
- def test_model_name_with_version(self):
- """Model name with version number is handled."""
- model_name = "llama2:13b-chat-q4_0"
-
- parts = model_name.split(":")
- base_name = parts[0]
- variant = parts[1] if len(parts) > 1 else "latest"
-
- assert base_name == "llama2"
- assert "13b" in variant
-
-
-class TestOllamaErrorMessages:
- """Tests for Ollama error message handling."""
-
- def test_error_message_model_not_found(self):
- """Model not found error is user-friendly."""
- raw_error = "Error: model 'nonexistent' not found"
-
- if "not found" in raw_error.lower():
- user_message = (
- "The requested model is not available in Ollama. "
- "Please run 'ollama pull <model_name>' to download it."
- )
- else:
- user_message = raw_error
-
- assert "ollama pull" in user_message.lower()
-
- def test_error_message_service_unavailable(self):
- """Service unavailable error is user-friendly."""
- raw_error = "Error: status code: 503"
-
- if "503" in raw_error:
- user_message = (
- "Ollama service is temporarily unavailable. "
- "Please check that Ollama is running."
- )
- else:
- user_message = raw_error
-
- assert "unavailable" in user_message.lower()
-
- def test_error_message_connection_refused(self):
- """Connection refused error is user-friendly."""
- raw_error = "Connection refused: localhost:11434"
-
- if "connection refused" in raw_error.lower():
- user_message = (
- "Cannot connect to Ollama. "
- "Please ensure Ollama is running with 'ollama serve'."
- )
- else:
- user_message = raw_error
-
- assert "ollama serve" in user_message.lower()
diff --git a/tests/integration/test_concurrent_operations.py b/tests/integration/test_concurrent_operations.py
deleted file mode 100644
index a911ae01fe..0000000000
--- a/tests/integration/test_concurrent_operations.py
+++ /dev/null
@@ -1,673 +0,0 @@
-"""
-Concurrent operations integration tests.
-
-Tests cover:
-- Concurrent research requests
-- Thread safety of shared resources
-- Database connection pooling
-- Cache concurrency
-- Queue processing concurrency
-- Lock management
-- Resource contention handling
-"""
-
-import time
-import threading
-import queue
-from concurrent.futures import ThreadPoolExecutor, as_completed
-
-
-class TestConcurrentResearchRequests:
- """Tests for concurrent research request handling."""
-
- def test_multiple_simultaneous_research_requests(self):
- """Multiple research requests should be handled simultaneously."""
- results = {}
- lock = threading.Lock()
-
- def run_research(research_id, query):
- time.sleep(0.01) # Simulate work
- with lock:
- results[research_id] = {"query": query, "status": "completed"}
- return research_id
-
- threads = []
- for i in range(10):
- t = threading.Thread(
- target=run_research, args=(f"research_{i}", f"query_{i}")
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(results) == 10
- for i in range(10):
- assert f"research_{i}" in results
-
- def test_research_requests_isolated(self):
- """Concurrent requests should be isolated from each other."""
- research_states = {}
- lock = threading.Lock()
- errors = []
-
- def run_research(research_id, user_id):
- try:
- with lock:
- research_states[research_id] = {
- "user_id": user_id,
- "status": "started",
- }
-
- time.sleep(0.01)
-
- with lock:
- # Verify state wasn't modified by other threads
- if research_states[research_id]["user_id"] != user_id:
- errors.append(f"State corruption in {research_id}")
- research_states[research_id]["status"] = "completed"
- except Exception as e:
- errors.append(str(e))
-
- threads = []
- for i in range(20):
- t = threading.Thread(
- target=run_research, args=(f"research_{i}", f"user_{i % 5}")
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 0
-
- def test_max_concurrent_limit_enforced(self):
- """Maximum concurrent research limit should be enforced."""
- max_concurrent = 5
- active_count = {"current": 0, "max_reached": 0}
- lock = threading.Lock()
- semaphore = threading.Semaphore(max_concurrent)
-
- def run_research(research_id):
- with semaphore:
- with lock:
- active_count["current"] += 1
- active_count["max_reached"] = max(
- active_count["max_reached"], active_count["current"]
- )
-
- time.sleep(0.05) # Simulate work
-
- with lock:
- active_count["current"] -= 1
-
- threads = []
- for i in range(20):
- t = threading.Thread(target=run_research, args=(f"research_{i}",))
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert active_count["max_reached"] <= max_concurrent
-
-
-class TestDatabaseConnectionPooling:
- """Tests for database connection pooling."""
-
- def test_connection_pool_reuse(self):
- """Connections should be reused from pool."""
- pool = {"connections": [], "max_size": 5}
- lock = threading.Lock()
- connection_uses = []
-
- def get_connection():
- with lock:
- if pool["connections"]:
- conn = pool["connections"].pop()
- conn["reused"] = True
- return conn
- return {"id": len(connection_uses), "reused": False}
-
- def release_connection(conn):
- with lock:
- if len(pool["connections"]) < pool["max_size"]:
- pool["connections"].append(conn)
-
- def use_connection():
- conn = get_connection()
- with lock:
- connection_uses.append(conn)
- time.sleep(
- 0.02
- ) # Increased delay to ensure some connections are released
- release_connection(conn)
-
- # Run in two batches to ensure reuse - first batch releases before second starts
- first_batch = [
- threading.Thread(target=use_connection) for _ in range(5)
- ]
- for t in first_batch:
- t.start()
- for t in first_batch:
- t.join()
-
- # Second batch should reuse connections from first batch
- second_batch = [
- threading.Thread(target=use_connection) for _ in range(5)
- ]
- for t in second_batch:
- t.start()
- for t in second_batch:
- t.join()
-
- # Second batch should have reused connections
- reused = sum(1 for c in connection_uses if c.get("reused"))
- assert reused > 0
-
- def test_connection_pool_exhaustion(self):
- """Should handle connection pool exhaustion."""
- pool_size = 3
- semaphore = threading.Semaphore(pool_size)
- waiting_threads = {"count": 0}
- lock = threading.Lock()
-
- def use_connection():
- with lock:
- waiting_threads["count"] += 1
-
- acquired = semaphore.acquire(timeout=0.1)
-
- with lock:
- waiting_threads["count"] -= 1
-
- if acquired:
- time.sleep(0.1) # Hold connection
- semaphore.release()
- return True
- return False
-
- results = []
- threads = []
-
- for _ in range(10):
- t = threading.Thread(
- target=lambda: results.append(use_connection())
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # Some should have timed out waiting
- failed = sum(1 for r in results if not r)
- assert failed > 0
-
-
-class TestCacheThreadSafety:
- """Tests for cache thread safety."""
-
- def test_concurrent_cache_reads(self):
- """Concurrent cache reads should be safe."""
- cache = {"key1": "value1", "key2": "value2"}
- lock = threading.RLock()
- read_results = []
-
- def read_cache(key):
- with lock:
- value = cache.get(key)
- read_results.append(value)
-
- threads = []
- for _ in range(100):
- for key in ["key1", "key2"]:
- t = threading.Thread(target=read_cache, args=(key,))
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(read_results) == 200
-
- def test_concurrent_cache_writes(self):
- """Concurrent cache writes should be safe."""
- cache = {}
- lock = threading.Lock()
- errors = []
-
- def write_cache(key, value):
- try:
- with lock:
- cache[key] = value
- except Exception as e:
- errors.append(str(e))
-
- threads = []
- for i in range(100):
- t = threading.Thread(
- target=write_cache, args=(f"key_{i}", f"value_{i}")
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 0
- assert len(cache) == 100
-
- def test_cache_stampede_prevention(self):
- """Cache stampede should be prevented."""
- cache = {}
- lock = threading.Lock()
- fetch_counts = {"count": 0}
- fetch_events = {}
-
- def get_or_fetch(key, fetch_func):
- with lock:
- if key in cache:
- return cache[key]
-
- # Check if someone else is fetching
- if key in fetch_events:
- event = fetch_events[key]
- else:
- event = threading.Event()
- fetch_events[key] = event
-
- # If we set up the event, we do the fetch
- if not event.is_set():
- with lock:
- if key not in cache: # Double-check
- value = fetch_func(key)
- cache[key] = value
- fetch_counts["count"] += 1
- event.set()
- else:
- event.wait()
-
- return cache.get(key)
-
- def slow_fetch(key):
- time.sleep(0.05)
- return f"value_for_{key}"
-
- results = []
-
- def fetch_key():
- result = get_or_fetch("shared_key", slow_fetch)
- results.append(result)
-
- threads = [threading.Thread(target=fetch_key) for _ in range(10)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # Should only fetch once despite multiple concurrent requests
- assert fetch_counts["count"] == 1
- assert len(results) == 10
-
-
-class TestQueueProcessingConcurrency:
- """Tests for queue processing concurrency."""
-
- def test_queue_processes_concurrently(self):
- """Queue should process items concurrently."""
- work_queue = queue.Queue()
- results = []
- lock = threading.Lock()
-
- def worker():
- while True:
- try:
- item = work_queue.get(timeout=0.1)
- time.sleep(0.01) # Simulate work
- with lock:
- results.append(item)
- work_queue.task_done()
- except queue.Empty:
- break
-
- # Add work items
- for i in range(20):
- work_queue.put(f"item_{i}")
-
- # Start workers
- threads = [threading.Thread(target=worker) for _ in range(4)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(results) == 20
-
- def test_queue_order_preserved_per_user(self):
- """Queue order should be preserved per user."""
- user_queues = {}
- lock = threading.Lock()
-
- def add_to_queue(user_id, item):
- with lock:
- if user_id not in user_queues:
- user_queues[user_id] = []
- user_queues[user_id].append(item)
-
- def process_next(user_id):
- with lock:
- if user_id in user_queues and user_queues[user_id]:
- return user_queues[user_id].pop(0)
- return None
-
- # Add items
- for i in range(10):
- add_to_queue("user1", f"item_{i}")
-
- # Process in order
- processed = []
- while True:
- item = process_next("user1")
- if item is None:
- break
- processed.append(item)
-
- assert processed == [f"item_{i}" for i in range(10)]
-
-
-class TestLockManagement:
- """Tests for lock management."""
-
- def test_lock_prevents_race_condition(self):
- """Lock should prevent race conditions."""
- counter = {"value": 0}
- lock = threading.Lock()
-
- def increment():
- for _ in range(1000):
- with lock:
- counter["value"] += 1
-
- threads = [threading.Thread(target=increment) for _ in range(10)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert counter["value"] == 10000
-
- def test_deadlock_prevention_with_timeout(self):
- """Deadlocks should be prevented with lock timeout."""
- lock1 = threading.Lock()
- lock2 = threading.Lock()
- deadlocks = []
-
- def operation1():
- if lock1.acquire(timeout=0.1):
- time.sleep(0.05)
- if not lock2.acquire(timeout=0.1):
- deadlocks.append("op1_lock2_timeout")
- else:
- lock2.release()
- lock1.release()
- else:
- deadlocks.append("op1_lock1_timeout")
-
- def operation2():
- if lock2.acquire(timeout=0.1):
- time.sleep(0.05)
- if not lock1.acquire(timeout=0.1):
- deadlocks.append("op2_lock1_timeout")
- else:
- lock1.release()
- lock2.release()
- else:
- deadlocks.append("op2_lock2_timeout")
-
- t1 = threading.Thread(target=operation1)
- t2 = threading.Thread(target=operation2)
-
- t1.start()
- t2.start()
- t1.join(timeout=1)
- t2.join(timeout=1)
-
- # With timeouts, threads should complete (with possible timeout warnings)
- assert not t1.is_alive()
- assert not t2.is_alive()
-
- def test_reentrant_lock(self):
- """Reentrant lock should allow same thread to acquire multiple times."""
- lock = threading.RLock()
- acquisitions = []
-
- def nested_acquire():
- with lock:
- acquisitions.append(1)
- with lock: # Same thread reacquiring
- acquisitions.append(2)
- with lock: # Again
- acquisitions.append(3)
-
- nested_acquire()
-
- assert acquisitions == [1, 2, 3]
-
-
-class TestResourceContention:
- """Tests for resource contention handling."""
-
- def test_high_contention_handled(self):
- """High contention should be handled gracefully."""
- resource = {"value": 0}
- lock = threading.Lock()
- contention_waits = {"count": 0}
-
- def access_resource():
- start = time.time()
- with lock:
- elapsed = time.time() - start
- if elapsed > 0.001: # Waited for lock
- contention_waits["count"] += 1
- resource["value"] += 1
- time.sleep(0.001) # Hold lock briefly
-
- threads = [threading.Thread(target=access_resource) for _ in range(50)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # All should complete
- assert resource["value"] == 50
- # Some should have experienced contention
- assert contention_waits["count"] > 0
-
- def test_fair_resource_access(self):
- """Resource access should be reasonably fair."""
- access_counts = {}
- lock = threading.Lock()
-
- def access_resource(thread_id):
- for _ in range(10):
- with lock:
- if thread_id not in access_counts:
- access_counts[thread_id] = 0
- access_counts[thread_id] += 1
- time.sleep(0.001)
-
- threads = [
- threading.Thread(target=access_resource, args=(f"thread_{i}",))
- for i in range(10)
- ]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # Each thread should have similar access counts
- counts = list(access_counts.values())
- assert min(counts) == 10
- assert max(counts) == 10
-
-
-class TestThreadPoolExecutor:
- """Tests for ThreadPoolExecutor usage."""
-
- def test_executor_handles_concurrent_tasks(self):
- """ThreadPoolExecutor should handle concurrent tasks."""
- results = []
- lock = threading.Lock()
-
- def task(task_id):
- time.sleep(0.01)
- with lock:
- results.append(task_id)
- return task_id
-
- with ThreadPoolExecutor(max_workers=4) as executor:
- futures = [executor.submit(task, i) for i in range(20)]
- completed = [f.result() for f in as_completed(futures)]
-
- assert len(completed) == 20
- assert len(results) == 20
-
- def test_executor_exception_handling(self):
- """Executor should handle task exceptions."""
- errors = []
-
- def failing_task(task_id):
- if task_id % 2 == 0:
- raise ValueError(f"Task {task_id} failed")
- return task_id
-
- with ThreadPoolExecutor(max_workers=4) as executor:
- futures = [executor.submit(failing_task, i) for i in range(10)]
-
- for future in as_completed(futures):
- try:
- future.result()
- except ValueError as e:
- errors.append(str(e))
-
- assert len(errors) == 5 # Even numbered tasks failed
-
-
-class TestSocketConcurrency:
- """Tests for socket emission concurrency."""
-
- def test_concurrent_socket_emissions(self):
- """Concurrent socket emissions should be handled."""
- emissions = []
- lock = threading.Lock()
-
- def emit(event, data):
- with lock:
- emissions.append({"event": event, "data": data})
-
- threads = []
- for i in range(50):
- t = threading.Thread(
- target=emit, args=(f"event_{i % 5}", {"id": i})
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(emissions) == 50
-
- def test_emission_ordering_per_research(self):
- """Emissions for same research should maintain order."""
- emissions = {}
- lock = threading.Lock()
-
- def emit(research_id, sequence):
- with lock:
- if research_id not in emissions:
- emissions[research_id] = []
- emissions[research_id].append(sequence)
-
- threads = []
- for research_id in ["r1", "r2", "r3"]:
- for seq in range(10):
- t = threading.Thread(target=emit, args=(research_id, seq))
- threads.append(t)
-
- # Start in order but don't wait between
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # All emissions should be recorded
- for research_id in ["r1", "r2", "r3"]:
- assert len(emissions[research_id]) == 10
-
-
-class TestConcurrentSettingsAccess:
- """Tests for concurrent settings access."""
-
- def test_concurrent_settings_reads(self):
- """Concurrent settings reads should be safe."""
- settings = {"key1": "value1", "key2": "value2", "key3": "value3"}
- lock = threading.RLock()
- read_results = []
-
- def read_setting(key):
- with lock:
- value = settings.get(key)
- read_results.append((key, value))
-
- threads = []
- for _ in range(100):
- for key in settings.keys():
- t = threading.Thread(target=read_setting, args=(key,))
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(read_results) == 300
-
- def test_concurrent_settings_updates(self):
- """Concurrent settings updates should be safe."""
- settings = {}
- lock = threading.Lock()
- errors = []
-
- def update_setting(key, value):
- try:
- with lock:
- settings[key] = value
- except Exception as e:
- errors.append(str(e))
-
- threads = []
- for i in range(100):
- t = threading.Thread(
- target=update_setting, args=(f"key_{i % 10}", f"value_{i}")
- )
- threads.append(t)
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 0
- assert len(settings) == 10 # 10 unique keys
diff --git a/tests/integration/test_end_to_end_research.py b/tests/integration/test_end_to_end_research.py
deleted file mode 100644
index c419bddf6a..0000000000
--- a/tests/integration/test_end_to_end_research.py
+++ /dev/null
@@ -1,541 +0,0 @@
-"""
-End-to-end integration tests for research workflow.
-
-Tests cover:
-- Complete research flow from query to report
-- Research mode variations (quick, deep)
-- Settings propagation through phases
-- Database persistence and retrieval
-- Export functionality
-- Research lifecycle management
-"""
-
-import json
-import time
-from unittest.mock import Mock
-from datetime import datetime
-
-
-class TestResearchQueryValidation:
- """Tests for research query validation."""
-
- def test_valid_query_accepted(self):
- """Valid queries should be accepted."""
- valid_queries = [
- "What is machine learning?",
- "Explain quantum computing",
- "History of artificial intelligence",
- "比较不同的编程语言", # Chinese
- "¿Qué es la inteligencia artificial?", # Spanish
- ]
-
- def validate_query(query):
- if not query or not query.strip():
- return False, "Query cannot be empty"
- if len(query) > 10000:
- return False, "Query too long"
- return True, None
-
- for query in valid_queries:
- is_valid, error = validate_query(query)
- assert is_valid is True, f"Query '{query}' should be valid"
-
- def test_empty_query_rejected(self):
- """Empty queries should be rejected."""
- invalid_queries = ["", " ", "\t\n", None]
-
- def validate_query(query):
- if not query or not query.strip():
- return False, "Query cannot be empty"
- return True, None
-
- for query in invalid_queries:
- if query is None:
- is_valid = False
- else:
- is_valid, _ = validate_query(query)
- assert is_valid is False
-
- def test_query_length_limits(self):
- """Query length should be limited."""
- max_length = 10000
-
- def validate_query(query):
- if len(query) > max_length:
- return False, "Query too long"
- return True, None
-
- # Just under limit
- is_valid, _ = validate_query("x" * 10000)
- assert is_valid is True
-
- # Over limit
- is_valid, _ = validate_query("x" * 10001)
- assert is_valid is False
-
- def test_query_sanitization(self):
- """Queries should be sanitized."""
-
- def sanitize_query(query):
- # Remove control characters
- sanitized = "".join(
- c for c in query if c.isprintable() or c in "\n\t"
- )
- # Normalize whitespace
- sanitized = " ".join(sanitized.split())
- return sanitized
-
- query = "What is\t\nAI?\x00\x01"
- sanitized = sanitize_query(query)
- assert "\x00" not in sanitized
- assert "What is AI?" == sanitized
-
-
-class TestResearchModeSelection:
- """Tests for research mode selection."""
-
- def test_quick_mode_configuration(self):
- """Quick mode should have correct configuration."""
- quick_config = {
- "mode": "quick",
- "max_iterations": 1,
- "max_sources": 10,
- "synthesis_depth": "shallow",
- }
-
- assert quick_config["max_iterations"] == 1
-
- def test_deep_mode_configuration(self):
- """Deep mode should have correct configuration."""
- deep_config = {
- "mode": "deep",
- "max_iterations": 5,
- "max_sources": 50,
- "synthesis_depth": "comprehensive",
- }
-
- assert deep_config["max_iterations"] == 5
-
- def test_mode_selection_from_settings(self):
- """Mode should be selected from settings."""
- settings = {"research.mode": "deep"}
-
- def get_mode_config(settings):
- mode = settings.get("research.mode", "quick")
- configs = {
- "quick": {"iterations": 1, "depth": 1},
- "deep": {"iterations": 5, "depth": 3},
- }
- return configs.get(mode, configs["quick"])
-
- config = get_mode_config(settings)
- assert config["iterations"] == 5
-
-
-class TestResearchPhaseExecution:
- """Tests for research phase execution."""
-
- def test_analysis_phase_execution(self):
- """Analysis phase should execute correctly."""
- mock_llm = Mock()
- mock_llm.invoke.return_value = Mock(
- content="Analysis result: The topic involves..."
- )
-
- def run_analysis(query, llm):
- response = llm.invoke(f"Analyze: {query}")
- return {"phase": "analysis", "result": response.content}
-
- result = run_analysis("test query", mock_llm)
- assert result["phase"] == "analysis"
- assert "Analysis result" in result["result"]
-
- def test_search_phase_execution(self):
- """Search phase should execute correctly."""
- mock_search = Mock()
- mock_search.search.return_value = [
- {"title": "Result 1", "url": "http://example1.com"},
- {"title": "Result 2", "url": "http://example2.com"},
- ]
-
- def run_search(query, search_engine):
- results = search_engine.search(query)
- return {"phase": "search", "results": results}
-
- result = run_search("test query", mock_search)
- assert result["phase"] == "search"
- assert len(result["results"]) == 2
-
- def test_synthesis_phase_execution(self):
- """Synthesis phase should execute correctly."""
- mock_llm = Mock()
- mock_llm.invoke.return_value = Mock(
- content="# Research Report\n\n## Summary\n\nFindings..."
- )
-
- def run_synthesis(analysis, search_results, llm):
- prompt = (
- f"Synthesize: {analysis} with {len(search_results)} sources"
- )
- response = llm.invoke(prompt)
- return {"phase": "synthesis", "report": response.content}
-
- result = run_synthesis(
- "analysis data", ["source1", "source2"], mock_llm
- )
- assert result["phase"] == "synthesis"
- assert "# Research Report" in result["report"]
-
-
-class TestResearchProgressTracking:
- """Tests for research progress tracking."""
-
- def test_progress_updates_sequentially(self):
- """Progress should update sequentially."""
- progress_history = []
-
- def update_progress(phase, percentage, message):
- progress_history.append(
- {"phase": phase, "percentage": percentage, "message": message}
- )
-
- update_progress("initialization", 5, "Starting research...")
- update_progress("analysis", 20, "Analyzing query...")
- update_progress("search", 50, "Searching sources...")
- update_progress("synthesis", 80, "Generating report...")
- update_progress("complete", 100, "Research complete")
-
- assert len(progress_history) == 5
- # Percentages should increase
- percentages = [p["percentage"] for p in progress_history]
- assert percentages == sorted(percentages)
-
- def test_progress_callbacks_invoked(self):
- """Progress callbacks should be invoked."""
- callback_invocations = []
-
- def progress_callback(data):
- callback_invocations.append(data)
-
- # Simulate research with callbacks
- phases = ["init", "analyze", "search", "synthesize", "complete"]
- for i, phase in enumerate(phases):
- progress_callback(
- {"phase": phase, "progress": (i + 1) / len(phases) * 100}
- )
-
- assert len(callback_invocations) == 5
-
-
-class TestResearchDatabasePersistence:
- """Tests for research database persistence."""
-
- def test_research_saved_to_database(self):
- """Research should be saved to database."""
- mock_db = {}
-
- def save_research(research_id, data):
- mock_db[research_id] = {
- "id": research_id,
- "query": data["query"],
- "status": data["status"],
- "created_at": datetime.now().isoformat(),
- }
-
- save_research(
- "research_1", {"query": "test query", "status": "completed"}
- )
-
- assert "research_1" in mock_db
- assert mock_db["research_1"]["query"] == "test query"
-
- def test_research_retrievable_by_id(self):
- """Research should be retrievable by ID."""
- mock_db = {
- "research_1": {"id": "research_1", "query": "test query"},
- }
-
- def get_research(research_id):
- return mock_db.get(research_id)
-
- result = get_research("research_1")
- assert result is not None
- assert result["query"] == "test query"
-
- def test_research_status_updates_persisted(self):
- """Research status updates should be persisted."""
- mock_db = {
- "research_1": {"status": "in_progress"},
- }
-
- def update_status(research_id, new_status):
- if research_id in mock_db:
- mock_db[research_id]["status"] = new_status
- mock_db[research_id]["updated_at"] = datetime.now().isoformat()
-
- update_status("research_1", "completed")
-
- assert mock_db["research_1"]["status"] == "completed"
- assert "updated_at" in mock_db["research_1"]
-
-
-class TestResearchReportGeneration:
- """Tests for research report generation."""
-
- def test_markdown_report_generated(self):
- """Markdown report should be generated."""
- synthesis = "Research findings summary"
- sources = [{"title": "Source 1", "url": "http://example.com"}]
-
- def generate_markdown_report(synthesis, sources):
- report = f"# Research Report\n\n{synthesis}\n\n## Sources\n\n"
- for source in sources:
- report += f"- [{source['title']}]({source['url']})\n"
- return report
-
- report = generate_markdown_report(synthesis, sources)
- assert "# Research Report" in report
- assert "Research findings summary" in report
-
- def test_report_includes_metadata(self):
- """Report should include metadata."""
-
- def generate_report_with_metadata(content, metadata):
- return {
- "content": content,
- "metadata": {
- "generated_at": datetime.now().isoformat(),
- "query": metadata.get("query"),
- "mode": metadata.get("mode"),
- "source_count": metadata.get("source_count"),
- },
- }
-
- report = generate_report_with_metadata(
- "Report content",
- {"query": "test", "mode": "quick", "source_count": 5},
- )
-
- assert "metadata" in report
- assert report["metadata"]["query"] == "test"
-
-
-class TestResearchExport:
- """Tests for research export functionality."""
-
- def test_export_to_markdown(self):
- """Should export research to markdown."""
-
- def export_markdown(report):
- return f"# {report['title']}\n\n{report['content']}"
-
- report = {"title": "Test Report", "content": "Report content here"}
- exported = export_markdown(report)
-
- assert "# Test Report" in exported
-
- def test_export_to_json(self):
- """Should export research to JSON."""
-
- def export_json(report):
- return json.dumps(report, indent=2)
-
- report = {"title": "Test Report", "content": "Report content"}
- exported = export_json(report)
-
- assert "Test Report" in exported
- # Should be valid JSON
- parsed = json.loads(exported)
- assert parsed["title"] == "Test Report"
-
- def test_export_to_html(self):
- """Should export research to HTML."""
-
- def export_html(report):
- return f"""
- <!DOCTYPE html>
- <html>
- <head><title>{report["title"]}</title></head>
- <body>
- <h1>{report["title"]}</h1>
- <div>{report["content"]}</div>
- </body>
- </html>
- """
-
- report = {"title": "Test Report", "content": "Report content"}
- exported = export_html(report)
-
- assert "<html>" in exported
- assert "Test Report" in exported
-
-
-class TestResearchCancellation:
- """Tests for research cancellation."""
-
- def test_research_can_be_cancelled(self):
- """Research should be cancellable."""
- research = {
- "id": "research_1",
- "status": "in_progress",
- "cancelled": False,
- }
-
- def cancel_research(research):
- research["cancelled"] = True
- research["status"] = "cancelled"
- return research
-
- cancelled = cancel_research(research)
-
- assert cancelled["status"] == "cancelled"
- assert cancelled["cancelled"] is True
-
- def test_cancellation_stops_processing(self):
- """Cancellation should stop processing."""
- research = {"cancelled": False}
- processed_phases = []
-
- def process_phase(name, research):
- if research["cancelled"]:
- return False
- processed_phases.append(name)
- return True
-
- # Process some phases
- process_phase("analysis", research)
- process_phase("search", research)
-
- # Cancel
- research["cancelled"] = True
-
- # Should not process more
- result = process_phase("synthesis", research)
-
- assert result is False
- assert "synthesis" not in processed_phases
-
-
-class TestResearchTimeout:
- """Tests for research timeout handling."""
-
- def test_research_timeout_detected(self):
- """Research timeout should be detected."""
- research = {
- "started_at": time.time() - 400, # 400 seconds ago
- "timeout": 300, # 5 minute timeout
- }
-
- def is_timed_out(research):
- elapsed = time.time() - research["started_at"]
- return elapsed > research["timeout"]
-
- assert is_timed_out(research) is True
-
- def test_timeout_triggers_cleanup(self):
- """Timeout should trigger cleanup."""
- cleanup_called = []
-
- def handle_timeout(research_id):
- cleanup_called.append(research_id)
- return {"status": "timeout", "id": research_id}
-
- result = handle_timeout("research_1")
-
- assert "research_1" in cleanup_called
- assert result["status"] == "timeout"
-
-
-class TestResearchSettingsPropagation:
- """Tests for settings propagation through research."""
-
- def test_settings_available_in_all_phases(self):
- """Settings should be available in all phases."""
- settings = {
- "llm.model": "gpt-4",
- "llm.temperature": 0.7,
- "search.max_results": 10,
- }
-
- phases_settings = {}
-
- def run_phase(phase_name, settings):
- phases_settings[phase_name] = dict(settings)
- return settings.get("llm.model")
-
- # Run phases with settings
- run_phase("analysis", settings)
- run_phase("search", settings)
- run_phase("synthesis", settings)
-
- # All phases should have same settings
- for phase in ["analysis", "search", "synthesis"]:
- assert phases_settings[phase]["llm.model"] == "gpt-4"
-
- def test_settings_override_defaults(self):
- """User settings should override defaults."""
- defaults = {
- "llm.temperature": 0.5,
- "search.max_results": 5,
- }
- user_settings = {
- "llm.temperature": 0.8,
- }
-
- def merge_settings(defaults, user):
- merged = dict(defaults)
- merged.update(user)
- return merged
-
- merged = merge_settings(defaults, user_settings)
-
- assert merged["llm.temperature"] == 0.8 # User override
- assert merged["search.max_results"] == 5 # Default kept
-
-
-class TestResearchSourceDeduplication:
- """Tests for source deduplication."""
-
- def test_duplicate_urls_removed(self):
- """Duplicate URLs should be removed."""
- sources = [
- {"url": "http://example.com/1", "title": "Source 1"},
- {"url": "http://example.com/2", "title": "Source 2"},
- {"url": "http://example.com/1", "title": "Source 1 Duplicate"},
- ]
-
- def deduplicate_sources(sources):
- seen_urls = set()
- unique = []
- for source in sources:
- if source["url"] not in seen_urls:
- seen_urls.add(source["url"])
- unique.append(source)
- return unique
-
- unique = deduplicate_sources(sources)
-
- assert len(unique) == 2
-
- def test_similar_content_detected(self):
- """Similar content should be detected."""
- sources = [
- {"content": "Machine learning is a branch of AI"},
- {
- "content": "Machine learning is a branch of artificial intelligence"
- },
- ]
-
- def calculate_similarity(text1, text2):
- # Simple word overlap similarity
- words1 = set(text1.lower().split())
- words2 = set(text2.lower().split())
- overlap = len(words1 & words2)
- total = len(words1 | words2)
- return overlap / total if total > 0 else 0
-
- similarity = calculate_similarity(
- sources[0]["content"], sources[1]["content"]
- )
-
- assert similarity > 0.5 # High similarity
diff --git a/tests/integration/test_error_propagation.py b/tests/integration/test_error_propagation.py
deleted file mode 100644
index ef391c306d..0000000000
--- a/tests/integration/test_error_propagation.py
+++ /dev/null
@@ -1,574 +0,0 @@
-"""
-Tests for error propagation chains.
-
-Tests cover:
-- Error propagation between components
-- Concurrent request handling
-"""
-
-from unittest.mock import Mock
-import threading
-import time
-
-
-class TestErrorPropagationChains:
- """Tests for error propagation between components."""
-
- def test_error_propagation_llm_to_service(self):
- """LLM errors propagate to service layer."""
- llm_error = ConnectionError("LLM service unavailable")
-
- service_error = None
- try:
- raise llm_error
- except ConnectionError as e:
- service_error = {
- "source": "llm",
- "message": str(e),
- "recoverable": True,
- }
-
- assert service_error["source"] == "llm"
-
- def test_error_propagation_search_to_service(self):
- """Search errors propagate to service layer."""
- search_error = TimeoutError("Search timeout")
-
- service_error = None
- try:
- raise search_error
- except TimeoutError as e:
- service_error = {
- "source": "search",
- "message": str(e),
- "recoverable": True,
- }
-
- assert service_error["source"] == "search"
-
- def test_error_propagation_database_to_service(self):
- """Database errors propagate to service layer."""
- db_error = Exception("Database connection failed")
-
- service_error = None
- try:
- raise db_error
- except Exception as e:
- service_error = {
- "source": "database",
- "message": str(e),
- "recoverable": False,
- }
-
- assert service_error["source"] == "database"
- assert not service_error["recoverable"]
-
- def test_error_propagation_queue_to_service(self):
- """Queue errors propagate to service layer."""
- queue_error = Exception("Queue processing failed")
-
- service_error = None
- try:
- raise queue_error
- except Exception as e:
- service_error = {
- "source": "queue",
- "message": str(e),
- }
-
- assert service_error["source"] == "queue"
-
- def test_error_propagation_socket_to_service(self):
- """Socket errors propagate to service layer."""
- socket_error = ConnectionError("Socket disconnected")
-
- service_error = None
- try:
- raise socket_error
- except ConnectionError as e:
- service_error = {
- "source": "socket",
- "message": str(e),
- }
-
- assert service_error["source"] == "socket"
-
- def test_error_propagation_file_to_service(self):
- """File system errors propagate to service layer."""
- file_error = IOError("File not found")
-
- service_error = None
- try:
- raise file_error
- except IOError as e:
- service_error = {
- "source": "filesystem",
- "message": str(e),
- }
-
- assert service_error["source"] == "filesystem"
-
- def test_error_propagation_settings_to_service(self):
- """Settings errors propagate to service layer."""
- settings_error = ValueError("Invalid setting value")
-
- service_error = None
- try:
- raise settings_error
- except ValueError as e:
- service_error = {
- "source": "settings",
- "message": str(e),
- }
-
- assert service_error["source"] == "settings"
-
- def test_error_propagation_cache_to_service(self):
- """Cache errors propagate to service layer."""
- cache_error = Exception("Cache miss")
-
- service_error = None
- try:
- raise cache_error
- except Exception as e:
- service_error = {
- "source": "cache",
- "message": str(e),
- "recoverable": True,
- }
-
- assert service_error["source"] == "cache"
- assert service_error["recoverable"]
-
- def test_error_propagation_nested_errors(self):
- """Nested errors preserve chain."""
- original_error = ConnectionError("Original error")
-
- try:
- try:
- raise original_error
- except ConnectionError as e:
- raise RuntimeError(f"Wrapped: {e}") from e
- except RuntimeError as e:
- error_chain = {
- "outer": str(e),
- "inner": str(e.__cause__),
- }
-
- assert "Wrapped" in error_chain["outer"]
- assert "Original" in error_chain["inner"]
-
- def test_error_propagation_error_transformation(self):
- """Errors are transformed for API response."""
- internal_error = Exception("Internal error details")
-
- def transform_error(error):
- return {
- "status": "error",
- "message": "An error occurred",
- "code": 500,
- }
-
- api_response = transform_error(internal_error)
-
- assert api_response["status"] == "error"
- assert "Internal" not in api_response["message"]
-
- def test_error_propagation_logging(self):
- """Errors are logged during propagation."""
- logged_errors = []
-
- def log_error(error, context):
- logged_errors.append(
- {
- "error": str(error),
- "context": context,
- }
- )
-
- try:
- raise ValueError("Test error")
- except ValueError as e:
- log_error(e, {"phase": "analysis"})
-
- assert len(logged_errors) == 1
- assert logged_errors[0]["context"]["phase"] == "analysis"
-
- def test_error_propagation_notification(self):
- """Errors trigger notifications."""
- notifications = []
-
- def notify_error(error, severity):
- notifications.append(
- {
- "error": str(error),
- "severity": severity,
- }
- )
-
- try:
- raise Exception("Critical error")
- except Exception as e:
- notify_error(e, "critical")
-
- assert len(notifications) == 1
- assert notifications[0]["severity"] == "critical"
-
-
-class TestConcurrentRequestHandling:
- """Tests for concurrent request handling."""
-
- def test_concurrent_research_requests(self):
- """Concurrent research requests are handled."""
- results = {}
- lock = threading.Lock()
-
- def process_request(request_id):
- time.sleep(0.01)
- with lock:
- results[request_id] = {"status": "completed"}
-
- threads = [
- threading.Thread(target=process_request, args=(f"req_{i}",))
- for i in range(5)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(results) == 5
-
- def test_concurrent_settings_updates(self):
- """Concurrent settings updates are handled."""
- settings = {"value": 0}
- lock = threading.Lock()
-
- def update_setting(new_value):
- with lock:
- settings["value"] = new_value
-
- threads = [
- threading.Thread(target=update_setting, args=(i,))
- for i in range(10)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # One of the values should win
- assert settings["value"] in range(10)
-
- def test_concurrent_database_access(self):
- """Concurrent database access is safe."""
- db = {}
- lock = threading.Lock()
- errors = []
-
- def db_operation(key, value):
- try:
- with lock:
- db[key] = value
- _ = db[key]
- except Exception as e:
- errors.append(e)
-
- threads = [
- threading.Thread(target=db_operation, args=(f"key_{i}", i))
- for i in range(10)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 0
- assert len(db) == 10
-
- def test_concurrent_cache_access(self):
- """Concurrent cache access is safe."""
- cache = {}
- lock = threading.Lock()
-
- def cache_operation(key, value):
- with lock:
- if key not in cache:
- cache[key] = value
- return cache.get(key)
-
- threads = [
- threading.Thread(target=cache_operation, args=("shared_key", i))
- for i in range(10)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # First value should be preserved
- assert cache["shared_key"] in range(10)
-
- def test_concurrent_queue_operations(self):
- """Concurrent queue operations are safe."""
- import queue
-
- q = queue.Queue()
- results = []
- lock = threading.Lock()
-
- def producer(item):
- q.put(item)
-
- def consumer():
- while not q.empty():
- try:
- item = q.get_nowait()
- with lock:
- results.append(item)
- except queue.Empty:
- break
-
- # Add items
- for i in range(10):
- producer(i)
-
- # Consume concurrently
- threads = [threading.Thread(target=consumer) for _ in range(3)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(results) == 10
-
- def test_concurrent_socket_emissions(self):
- """Concurrent socket emissions are handled."""
- emissions = []
- lock = threading.Lock()
-
- def emit(event, data):
- with lock:
- emissions.append({"event": event, "data": data})
-
- threads = [
- threading.Thread(target=emit, args=(f"event_{i}", {"id": i}))
- for i in range(10)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(emissions) == 10
-
- def test_concurrent_resource_cleanup(self):
- """Concurrent resource cleanup is safe."""
- resources = {f"resource_{i}": Mock() for i in range(5)}
- cleaned = []
- lock = threading.Lock()
-
- def cleanup(resource_id):
- with lock:
- if resource_id in resources:
- del resources[resource_id]
- cleaned.append(resource_id)
-
- threads = [
- threading.Thread(target=cleanup, args=(f"resource_{i}",))
- for i in range(5)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(cleaned) == 5
- assert len(resources) == 0
-
- def test_concurrent_error_handling(self):
- """Concurrent error handling is safe."""
- errors = []
- lock = threading.Lock()
-
- def operation_with_error(op_id):
- try:
- if op_id % 2 == 0:
- raise ValueError(f"Error in op {op_id}")
- except ValueError as e:
- with lock:
- errors.append(str(e))
-
- threads = [
- threading.Thread(target=operation_with_error, args=(i,))
- for i in range(10)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 5 # Even numbers cause errors
-
-
-class TestErrorRecovery:
- """Tests for error recovery mechanisms."""
-
- def test_retry_with_backoff(self):
- """Retry with exponential backoff works."""
- attempts = 0
- max_attempts = 3
- success = False
-
- while attempts < max_attempts and not success:
- try:
- if attempts < 2:
- raise ConnectionError("Temporary failure")
- success = True
- except ConnectionError:
- attempts += 1
- # Would sleep with backoff in real code
-
- assert success
- assert attempts == 2
-
- def test_circuit_breaker_pattern(self):
- """Circuit breaker prevents repeated failures."""
- failures = 0
- failure_threshold = 3
- circuit_open = False
-
- def call_service():
- nonlocal failures, circuit_open
- if circuit_open:
- raise Exception("Circuit open")
- try:
- raise Exception("Service failed")
- except Exception:
- failures += 1
- if failures >= failure_threshold:
- circuit_open = True
- raise
-
- for _ in range(5):
- try:
- call_service()
- except Exception:
- pass
-
- assert circuit_open
- assert failures == 3
-
- def test_fallback_on_error(self):
- """Fallback is used on error."""
-
- def primary_operation():
- raise Exception("Primary failed")
-
- def fallback_operation():
- return "fallback_result"
-
- try:
- result = primary_operation()
- except Exception:
- result = fallback_operation()
-
- assert result == "fallback_result"
-
- def test_partial_result_preservation(self):
- """Partial results are preserved on error."""
- results = []
-
- for i in range(5):
- try:
- if i == 3:
- raise Exception("Failed at step 3")
- results.append(f"result_{i}")
- except Exception:
- break
-
- assert len(results) == 3
- assert results[-1] == "result_2"
-
- def test_graceful_degradation(self):
- """System degrades gracefully on partial failure."""
- services = {
- "primary": {"available": False},
- "secondary": {"available": True},
- "tertiary": {"available": True},
- }
-
- def get_available_service():
- for name, service in services.items():
- if service["available"]:
- return name
- return None
-
- service = get_available_service()
-
- assert service == "secondary"
-
-
-class TestErrorContextPreservation:
- """Tests for error context preservation."""
-
- def test_error_context_preserved(self):
- """Error context is preserved through layers."""
-
- def inner_operation():
- raise ValueError("Inner error")
-
- def outer_operation():
- try:
- inner_operation()
- except ValueError as e:
- raise RuntimeError("Outer error") from e
-
- context = None
- try:
- outer_operation()
- except RuntimeError as e:
- context = {
- "outer": str(e),
- "inner": str(e.__cause__),
- }
-
- assert "Outer" in context["outer"]
- assert "Inner" in context["inner"]
-
- def test_error_metadata_attached(self):
- """Error metadata is attached."""
-
- class ErrorWithMetadata(Exception):
- def __init__(self, message, metadata):
- super().__init__(message)
- self.metadata = metadata
-
- try:
- raise ErrorWithMetadata(
- "Error occurred", {"phase": "analysis", "query": "test"}
- )
- except ErrorWithMetadata as e:
- metadata = e.metadata
-
- assert metadata["phase"] == "analysis"
-
- def test_error_stack_trace_captured(self):
- """Stack trace is captured."""
- import traceback
-
- captured_trace = None
- try:
- raise Exception("Test error")
- except Exception:
- captured_trace = traceback.format_exc()
-
- assert "Test error" in captured_trace
- assert "Traceback" in captured_trace
diff --git a/tests/integration/test_error_recovery.py b/tests/integration/test_error_recovery.py
deleted file mode 100644
index d1e72d03f8..0000000000
--- a/tests/integration/test_error_recovery.py
+++ /dev/null
@@ -1,582 +0,0 @@
-"""
-Error recovery integration tests.
-
-Tests cover:
-- Error detection and classification
-- Retry mechanisms with backoff
-- Circuit breaker pattern
-- Graceful degradation
-- Partial result preservation
-- Transaction rollback
-- Resource cleanup on failure
-"""
-
-import time
-import threading
-from datetime import datetime
-
-
-class TestErrorDetection:
- """Tests for error detection."""
-
- def test_llm_error_detected(self):
- """LLM errors should be detected and classified."""
-
- def classify_error(error):
- error_str = str(error).lower()
- if "connection" in error_str:
- return "connection_error"
- if "rate limit" in error_str or "429" in error_str:
- return "rate_limit"
- if "timeout" in error_str:
- return "timeout"
- if "model not found" in error_str or "404" in error_str:
- return "model_not_found"
- return "unknown"
-
- assert classify_error("Connection refused") == "connection_error"
- assert classify_error("Rate limit exceeded 429") == "rate_limit"
- assert classify_error("Request timeout") == "timeout"
- assert classify_error("Model not found 404") == "model_not_found"
-
- def test_database_error_detected(self):
- """Database errors should be detected."""
-
- def is_database_error(error):
- db_indicators = [
- "database",
- "sqlite",
- "sqlalchemy",
- "connection pool",
- "integrity",
- "constraint",
- ]
- error_str = str(error).lower()
- return any(ind in error_str for ind in db_indicators)
-
- assert is_database_error("SQLAlchemy connection error") is True
- assert is_database_error("Database connection pool exhausted") is True
- assert is_database_error("Network timeout") is False
-
- def test_transient_vs_permanent_error(self):
- """Should distinguish transient from permanent errors."""
-
- def is_transient(error):
- transient_indicators = [
- "timeout",
- "rate limit",
- "connection",
- "unavailable",
- "503",
- "429",
- "temporary",
- ]
- error_str = str(error).lower()
- return any(ind in error_str for ind in transient_indicators)
-
- assert is_transient("Service temporarily unavailable 503") is True
- assert is_transient("Connection timeout") is True
- assert is_transient("Invalid API key") is False
- assert is_transient("Model not found") is False
-
-
-class TestRetryMechanisms:
- """Tests for retry mechanisms."""
-
- def test_simple_retry(self):
- """Simple retry should work for transient errors."""
- attempts = []
- max_retries = 3
-
- def operation_with_retry():
- for attempt in range(max_retries):
- attempts.append(attempt)
- try:
- if attempt < 2:
- raise ConnectionError("Transient failure")
- return "success"
- except ConnectionError:
- if attempt == max_retries - 1:
- raise
- continue
-
- result = operation_with_retry()
- assert result == "success"
- assert len(attempts) == 3
-
- def test_exponential_backoff(self):
- """Exponential backoff should increase wait times."""
- base_delay = 0.1
- max_delay = 2.0
-
- def calculate_delay(attempt, base=base_delay, max_d=max_delay):
- delay = base * (2**attempt)
- return min(delay, max_d)
-
- delays = [calculate_delay(i) for i in range(5)]
-
- assert delays[0] == 0.1
- assert delays[1] == 0.2
- assert delays[2] == 0.4
- assert delays[4] <= max_delay
-
- def test_retry_with_jitter(self):
- """Retry with jitter should add randomness."""
- import random
-
- def calculate_delay_with_jitter(attempt, base=0.1):
- delay = base * (2**attempt)
- jitter = random.uniform(0, delay * 0.1) # 10% jitter
- return delay + jitter
-
- # Multiple calculations should differ
- delays = [calculate_delay_with_jitter(2) for _ in range(10)]
- unique_delays = set(delays)
- assert len(unique_delays) > 1
-
- def test_retry_budget_enforced(self):
- """Retry budget should be enforced."""
- max_total_retries = 10
- retry_counts = {}
-
- def attempt_with_budget(operation_id):
- if operation_id not in retry_counts:
- retry_counts[operation_id] = 0
-
- if retry_counts[operation_id] >= 3: # Per-operation limit
- return False, "operation_limit"
-
- total = sum(retry_counts.values())
- if total >= max_total_retries:
- return False, "budget_exceeded"
-
- retry_counts[operation_id] += 1
- return True, None
-
- # Exhaust budget
- for op in ["op1", "op2", "op3", "op4"]:
- for _ in range(3):
- attempt_with_budget(op)
-
- # Budget should be exceeded
- can_retry, reason = attempt_with_budget("op5")
- assert can_retry is False
- assert reason == "budget_exceeded"
-
-
-class TestCircuitBreaker:
- """Tests for circuit breaker pattern."""
-
- def test_circuit_opens_after_failures(self):
- """Circuit should open after consecutive failures."""
- circuit = {
- "state": "closed",
- "failures": 0,
- "failure_threshold": 3,
- "last_failure": None,
- }
-
- def record_failure():
- circuit["failures"] += 1
- circuit["last_failure"] = time.time()
- if circuit["failures"] >= circuit["failure_threshold"]:
- circuit["state"] = "open"
-
- def record_success():
- circuit["failures"] = 0
- circuit["state"] = "closed"
-
- def can_execute():
- return circuit["state"] != "open"
-
- # Record failures
- for _ in range(3):
- record_failure()
-
- assert circuit["state"] == "open"
- assert can_execute() is False
-
- def test_circuit_half_open_after_timeout(self):
- """Circuit should become half-open after timeout."""
- circuit = {
- "state": "open",
- "opened_at": time.time() - 35, # 35 seconds ago
- "timeout": 30, # 30 second timeout
- }
-
- def check_state():
- if circuit["state"] == "open":
- if time.time() - circuit["opened_at"] > circuit["timeout"]:
- circuit["state"] = "half-open"
- return circuit["state"]
-
- assert check_state() == "half-open"
-
- def test_circuit_closes_on_success(self):
- """Circuit should close after successful call in half-open."""
- circuit = {"state": "half-open", "failures": 3}
-
- def record_success():
- if circuit["state"] == "half-open":
- circuit["state"] = "closed"
- circuit["failures"] = 0
-
- record_success()
-
- assert circuit["state"] == "closed"
- assert circuit["failures"] == 0
-
-
-class TestGracefulDegradation:
- """Tests for graceful degradation."""
-
- def test_fallback_to_cached_data(self):
- """Should fall back to cached data on error."""
- cache = {
- "query_hash": {"result": "cached_result", "timestamp": time.time()}
- }
-
- def get_result(query_hash, fetch_func):
- try:
- return fetch_func()
- except Exception:
- if query_hash in cache:
- return cache[query_hash]["result"]
- raise
-
- def failing_fetch():
- raise ConnectionError("Service unavailable")
-
- result = get_result("query_hash", failing_fetch)
- assert result == "cached_result"
-
- def test_reduced_functionality_mode(self):
- """Should operate in reduced functionality mode."""
- services = {
- "llm": {"available": False, "required": True},
- "search": {"available": True, "required": True},
- "cache": {"available": False, "required": False},
- }
-
- def get_available_mode():
- required_available = all(
- s["available"] for s in services.values() if s["required"]
- )
- if not required_available:
- return "degraded"
- return "full"
-
- assert get_available_mode() == "degraded"
-
- def test_alternative_provider_selection(self):
- """Should select alternative provider on failure."""
- providers = [
- {"name": "primary", "available": False},
- {"name": "secondary", "available": True},
- {"name": "tertiary", "available": True},
- ]
-
- def get_available_provider():
- for provider in providers:
- if provider["available"]:
- return provider["name"]
- return None
-
- assert get_available_provider() == "secondary"
-
-
-class TestPartialResultPreservation:
- """Tests for partial result preservation."""
-
- def test_partial_results_saved_on_error(self):
- """Partial results should be saved when error occurs."""
- partial_results = []
- error_occurred = None
-
- def process_with_checkpointing(items):
- nonlocal error_occurred
- for i, item in enumerate(items):
- try:
- if i == 3:
- raise Exception("Processing failed")
- partial_results.append(f"processed_{item}")
- except Exception as e:
- error_occurred = str(e)
- break
-
- process_with_checkpointing(["a", "b", "c", "d", "e"])
-
- assert len(partial_results) == 3
- assert error_occurred is not None
-
- def test_checkpoint_restoration(self):
- """Should be able to restore from checkpoint."""
- checkpoint = {
- "last_processed_index": 5,
- "partial_results": ["r0", "r1", "r2", "r3", "r4"],
- "state": {"analysis_complete": True},
- }
-
- def restore_from_checkpoint(checkpoint):
- return {
- "resume_index": checkpoint["last_processed_index"],
- "results": list(checkpoint["partial_results"]),
- "state": dict(checkpoint["state"]),
- }
-
- restored = restore_from_checkpoint(checkpoint)
-
- assert restored["resume_index"] == 5
- assert len(restored["results"]) == 5
-
- def test_incremental_save(self):
- """Results should be saved incrementally."""
- save_log = []
-
- def save_result(result_id, data):
- save_log.append(
- {"id": result_id, "data": data, "time": time.time()}
- )
-
- # Simulate incremental saves during processing
- for i in range(5):
- save_result(f"result_{i}", f"data_{i}")
-
- assert len(save_log) == 5
-
-
-class TestTransactionRollback:
- """Tests for transaction rollback."""
-
- def test_rollback_on_error(self):
- """Transaction should rollback on error."""
- database = {"committed": [], "pending": []}
-
- def transaction(operations):
- database["pending"] = []
- try:
- for op in operations:
- if op == "fail":
- raise Exception("Operation failed")
- database["pending"].append(op)
- # Commit
- database["committed"].extend(database["pending"])
- except Exception:
- # Rollback
- database["pending"] = []
- raise
-
- try:
- transaction(["op1", "op2", "fail", "op4"])
- except Exception:
- pass
-
- assert "op1" not in database["committed"]
- assert len(database["pending"]) == 0
-
- def test_partial_commit_prevention(self):
- """Should prevent partial commits."""
- state = {"phase1": False, "phase2": False, "phase3": False}
-
- def atomic_update(updates):
- backup = dict(state)
- try:
- for key, value in updates.items():
- if key == "phase2" and value:
- raise Exception("Phase 2 failed")
- state[key] = value
- except Exception:
- # Restore backup
- state.clear()
- state.update(backup)
- raise
-
- try:
- atomic_update({"phase1": True, "phase2": True, "phase3": True})
- except Exception:
- pass
-
- assert state == {"phase1": False, "phase2": False, "phase3": False}
-
-
-class TestResourceCleanup:
- """Tests for resource cleanup on failure."""
-
- def test_cleanup_on_exception(self):
- """Resources should be cleaned up on exception."""
- resources = {"allocated": [], "cleaned": []}
-
- def allocate(name):
- resources["allocated"].append(name)
- return name
-
- def cleanup(name):
- if name in resources["allocated"]:
- resources["allocated"].remove(name)
- resources["cleaned"].append(name)
-
- def operation_with_cleanup():
- r1 = allocate("resource1")
- r2 = allocate("resource2")
- try:
- raise Exception("Operation failed")
- finally:
- cleanup(r1)
- cleanup(r2)
-
- try:
- operation_with_cleanup()
- except Exception:
- pass
-
- assert len(resources["allocated"]) == 0
- assert len(resources["cleaned"]) == 2
-
- def test_context_manager_cleanup(self):
- """Context managers should ensure cleanup."""
-
- class Resource:
- instances = []
- cleaned = []
-
- def __init__(self, name):
- self.name = name
- Resource.instances.append(name)
-
- def __enter__(self):
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- Resource.instances.remove(self.name)
- Resource.cleaned.append(self.name)
- return False # Don't suppress exception
-
- try:
- with Resource("r1"):
- with Resource("r2"):
- raise Exception("Error")
- except Exception:
- pass
-
- assert len(Resource.instances) == 0
- assert len(Resource.cleaned) == 2
-
- def test_thread_cleanup_on_failure(self):
- """Threads should be cleaned up on failure."""
- threads_started = []
- threads_stopped = []
- stop_event = threading.Event()
-
- def worker(name):
- threads_started.append(name)
- while not stop_event.is_set():
- time.sleep(0.01)
- threads_stopped.append(name)
-
- threads = []
- for i in range(3):
- t = threading.Thread(target=worker, args=(f"worker_{i}",))
- t.start()
- threads.append(t)
-
- # Simulate failure - signal stop
- stop_event.set()
-
- for t in threads:
- t.join(timeout=1)
-
- assert len(threads_stopped) == 3
-
-
-class TestErrorReporting:
- """Tests for error reporting."""
-
- def test_error_context_captured(self):
- """Error context should be captured."""
-
- def capture_error_context(error, context):
- return {
- "error_type": type(error).__name__,
- "error_message": str(error),
- "timestamp": datetime.now().isoformat(),
- "context": context,
- }
-
- error = ValueError("Invalid input")
- report = capture_error_context(
- error, {"phase": "analysis", "query": "test query"}
- )
-
- assert report["error_type"] == "ValueError"
- assert "context" in report
- assert report["context"]["phase"] == "analysis"
-
- def test_error_chain_preserved(self):
- """Error chain should be preserved."""
-
- def inner_operation():
- raise ValueError("Inner error")
-
- def outer_operation():
- try:
- inner_operation()
- except ValueError as e:
- raise RuntimeError("Outer error") from e
-
- try:
- outer_operation()
- except RuntimeError as e:
- assert e.__cause__ is not None
- assert "Inner error" in str(e.__cause__)
-
-
-class TestRecoveryStrategies:
- """Tests for recovery strategies."""
-
- def test_automatic_recovery_attempt(self):
- """Should attempt automatic recovery."""
- recovery_attempts = []
-
- def attempt_recovery(error_type):
- recovery_attempts.append(error_type)
- recovery_actions = {
- "connection_error": "reconnect",
- "rate_limit": "wait_and_retry",
- "timeout": "increase_timeout",
- }
- return recovery_actions.get(error_type, "manual_intervention")
-
- action = attempt_recovery("connection_error")
- assert action == "reconnect"
- assert len(recovery_attempts) == 1
-
- def test_recovery_escalation(self):
- """Recovery should escalate if initial attempts fail."""
- escalation_levels = []
-
- def recover_with_escalation(error, max_levels=3):
- for level in range(max_levels):
- escalation_levels.append(level)
- if level == 2: # Succeed on level 2
- return True
- return False
-
- success = recover_with_escalation(Exception("test"))
- assert success is True
- assert escalation_levels == [0, 1, 2]
-
- def test_recovery_timeout(self):
- """Recovery should timeout if taking too long."""
- start_time = time.time()
- max_recovery_time = 0.1
-
- def recover_with_timeout():
- while time.time() - start_time < max_recovery_time:
- time.sleep(0.02)
- # Simulate recovery attempt
- if time.time() - start_time > max_recovery_time / 2:
- return True # Recovered in time
- return False # Timed out
-
- result = recover_with_timeout()
- assert result is True
diff --git a/tests/integration/test_research_flow.py b/tests/integration/test_research_flow.py
deleted file mode 100644
index 6a841454a1..0000000000
--- a/tests/integration/test_research_flow.py
+++ /dev/null
@@ -1,415 +0,0 @@
-"""
-Tests for end-to-end research flow.
-
-Tests cover:
-- Complete research flow
-- Multi-user isolation
-"""
-
-from unittest.mock import Mock
-import threading
-
-
-class TestEndToEndResearchFlow:
- """Tests for end-to-end research flow."""
-
- def test_e2e_quick_mode_with_mock_llm(self):
- """Quick mode research completes with mock LLM."""
- mock_llm = Mock()
- mock_llm.invoke.return_value = Mock(content="Research synthesis result")
-
- # Simulate quick mode flow
- result = {
- "status": "completed",
- "synthesis": mock_llm.invoke("test").content,
- }
-
- assert result["status"] == "completed"
- assert "synthesis" in result
-
- def test_e2e_deep_mode_with_mock_llm(self):
- """Deep mode research completes with mock LLM."""
- mock_llm = Mock()
- mock_llm.invoke.return_value = Mock(content="Deep research analysis")
-
- research_config = {
- "mode": "deep",
- "query": "test query",
- "iterations": 3,
- "llm": mock_llm,
- }
-
- # Simulate deep mode flow
- iterations_completed = 0
- for _ in range(research_config["iterations"]):
- mock_llm.invoke("analyze")
- iterations_completed += 1
-
- result = {"status": "completed", "iterations": iterations_completed}
-
- assert result["iterations"] == 3
-
- def test_e2e_research_with_queue(self):
- """Research with queue processing works."""
- queue = []
- active_researches = {}
-
- # Add to queue
- research_id = "research_1"
- queue.append({"id": research_id, "query": "test"})
-
- # Process from queue
- if queue:
- item = queue.pop(0)
- active_researches[item["id"]] = {"status": "in_progress"}
-
- assert research_id in active_researches
- assert len(queue) == 0
-
- def test_e2e_research_cancellation(self):
- """Research can be cancelled mid-flow."""
- research = {
- "id": "research_1",
- "status": "in_progress",
- "cancelled": False,
- }
-
- # Cancel research
- research["cancelled"] = True
- research["status"] = "cancelled"
-
- assert research["status"] == "cancelled"
-
- def test_e2e_research_error_recovery(self):
- """Research recovers from errors."""
- errors = []
- retries = 0
- max_retries = 3
- success = False
-
- while not success and retries < max_retries:
- try:
- if retries < 2:
- raise ConnectionError("Temporary failure")
- success = True
- except ConnectionError as e:
- errors.append(str(e))
- retries += 1
-
- assert success
- assert retries == 2
-
- def test_e2e_research_progress_tracking(self):
- """Progress is tracked throughout research."""
- progress_updates = []
-
- def update_progress(phase, percentage):
- progress_updates.append({"phase": phase, "percent": percentage})
-
- # Simulate research phases
- update_progress("analysis", 25)
- update_progress("synthesis", 50)
- update_progress("refinement", 75)
- update_progress("complete", 100)
-
- assert len(progress_updates) == 4
- assert progress_updates[-1]["percent"] == 100
-
- def test_e2e_research_report_generation(self):
- """Report is generated at end of research."""
- synthesis = "Research findings summary"
-
- report = {
- "title": "Research Report",
- "content": synthesis,
- "generated_at": "2024-01-15",
- }
-
- assert report["content"] == synthesis
-
- def test_e2e_research_export_formats(self):
- """Research exports to multiple formats."""
- content = "# Research Report\n\nFindings..."
-
- exports = {
- "markdown": content,
- "pdf": f"PDF({content})",
- "html": f"<html><body>{content}</body></html>",
- }
-
- assert len(exports) == 3
-
- def test_e2e_research_database_persistence(self):
- """Research is persisted to database."""
- mock_db = {}
-
- research = {
- "id": "research_1",
- "query": "test query",
- "result": "synthesis",
- }
-
- mock_db[research["id"]] = research
-
- retrieved = mock_db.get("research_1")
-
- assert retrieved is not None
- assert retrieved["query"] == "test query"
-
- def test_e2e_research_socket_notifications(self):
- """Socket notifications are sent during research."""
- notifications = []
-
- def emit(event, data):
- notifications.append({"event": event, "data": data})
-
- # Simulate research flow with notifications
- emit("research_started", {"id": "research_1"})
- emit("progress", {"percent": 50})
- emit("research_completed", {"id": "research_1"})
-
- assert len(notifications) == 3
- assert notifications[0]["event"] == "research_started"
-
- def test_e2e_research_settings_propagation(self):
- """Settings are propagated through research flow."""
- settings = {
- "llm.model": "gpt-4",
- "llm.temperature": 0.7,
- "search.max_results": 10,
- }
-
- # Settings should be accessible in each phase
- analysis_settings = settings.copy()
- synthesis_settings = settings.copy()
-
- assert analysis_settings["llm.model"] == "gpt-4"
- assert synthesis_settings["llm.temperature"] == 0.7
-
- def test_e2e_research_resource_cleanup(self):
- """Resources are cleaned up after research."""
- resources = {
- "llm_connection": Mock(),
- "cache_entries": ["entry1", "entry2"],
- "temp_files": ["/tmp/file1"],
- }
-
- # Cleanup
- resources["llm_connection"].close = Mock()
- resources["cache_entries"].clear()
- resources["temp_files"].clear()
-
- assert len(resources["cache_entries"]) == 0
- assert len(resources["temp_files"]) == 0
-
-
-class TestMultiUserIsolation:
- """Tests for multi-user isolation."""
-
- def test_multi_user_database_isolation(self):
- """Users have isolated databases."""
- user_dbs = {
- "user1": {"data": "user1_data"},
- "user2": {"data": "user2_data"},
- }
-
- # Each user's data is separate
- assert user_dbs["user1"]["data"] != user_dbs["user2"]["data"]
-
- def test_multi_user_settings_isolation(self):
- """Users have isolated settings."""
- user_settings = {
- "user1": {"llm.model": "gpt-4"},
- "user2": {"llm.model": "claude-3"},
- }
-
- assert user_settings["user1"]["llm.model"] == "gpt-4"
- assert user_settings["user2"]["llm.model"] == "claude-3"
-
- def test_multi_user_queue_isolation(self):
- """Users have isolated queues."""
- user_queues = {
- "user1": [{"id": "r1"}],
- "user2": [{"id": "r2"}, {"id": "r3"}],
- }
-
- assert len(user_queues["user1"]) == 1
- assert len(user_queues["user2"]) == 2
-
- def test_multi_user_cache_sharing(self):
- """Cache can be shared between users."""
- shared_cache = {
- "query_hash_1": {"result": "shared_result"},
- }
-
- # Both users can access shared cache
- user1_result = shared_cache.get("query_hash_1")
- user2_result = shared_cache.get("query_hash_1")
-
- assert user1_result == user2_result
-
- def test_multi_user_concurrent_research(self):
- """Concurrent research from multiple users works."""
- results = {}
- lock = threading.Lock()
-
- def run_research(user_id, query):
- # Simulate research
- result = f"result_{user_id}"
- with lock:
- results[user_id] = result
-
- threads = [
- threading.Thread(
- target=run_research, args=(f"user{i}", f"query{i}")
- )
- for i in range(3)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(results) == 3
-
- def test_multi_user_session_handling(self):
- """Sessions are handled per user."""
- sessions = {}
-
- def create_session(user_id):
- sessions[user_id] = {
- "user_id": user_id,
- "created": "now",
- "authenticated": True,
- }
-
- create_session("user1")
- create_session("user2")
-
- assert sessions["user1"]["user_id"] == "user1"
- assert sessions["user2"]["user_id"] == "user2"
-
- def test_multi_user_resource_limits(self):
- """Resource limits are enforced per user."""
- user_limits = {
- "user1": {"max_concurrent": 2, "current": 2},
- "user2": {"max_concurrent": 2, "current": 1},
- }
-
- def can_start_research(user_id):
- limits = user_limits[user_id]
- return limits["current"] < limits["max_concurrent"]
-
- assert not can_start_research("user1")
- assert can_start_research("user2")
-
- def test_multi_user_error_isolation(self):
- """Errors for one user don't affect others."""
- user_states = {
- "user1": {"status": "error", "error": "LLM failed"},
- "user2": {"status": "running", "error": None},
- }
-
- assert user_states["user1"]["status"] == "error"
- assert user_states["user2"]["status"] == "running"
-
-
-class TestResearchFlowEdgeCases:
- """Tests for research flow edge cases."""
-
- def test_empty_query_handling(self):
- """Empty queries are rejected."""
- query = ""
-
- if not query.strip():
- error = "Query cannot be empty"
- else:
- error = None
-
- assert error is not None
-
- def test_very_long_query_handling(self):
- """Very long queries are truncated."""
- max_length = 1000
- query = "x" * 2000
-
- if len(query) > max_length:
- truncated = query[:max_length]
- else:
- truncated = query
-
- assert len(truncated) == max_length
-
- def test_special_characters_in_query(self):
- """Special characters in query are handled."""
- query = "test <script>alert('xss')</script>"
-
- # Sanitize
- sanitized = query.replace("<", "&lt;").replace(">", "&gt;")
-
- assert "<script>" not in sanitized
-
- def test_unicode_query_handling(self):
- """Unicode queries are handled."""
- query = "recherche en fran\u00e7ais"
-
- # Should work without issues
- encoded = query.encode("utf-8")
- decoded = encoded.decode("utf-8")
-
- assert decoded == query
-
- def test_concurrent_modifications(self):
- """Concurrent modifications are handled."""
- research = {"status": "in_progress", "version": 1}
- lock = threading.Lock()
-
- def modify(new_status):
- with lock:
- research["status"] = new_status
- research["version"] += 1
-
- threads = [
- threading.Thread(target=modify, args=(f"status_{i}",))
- for i in range(5)
- ]
-
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert research["version"] == 6 # Initial 1 + 5 modifications
-
- def test_timeout_handling(self):
- """Timeouts are handled gracefully."""
- timeout_seconds = 30
- elapsed = 35
-
- if elapsed > timeout_seconds:
- status = "timeout"
- else:
- status = "completed"
-
- assert status == "timeout"
-
- def test_retry_exhaustion(self):
- """Retry exhaustion is handled."""
- max_retries = 3
- retries = 0
- success = False
-
- while not success and retries < max_retries:
- try:
- raise Exception("Always fails")
- except Exception:
- retries += 1
-
- if retries >= max_retries:
- final_status = "failed"
- else:
- final_status = "success"
-
- assert final_status == "failed"
- assert retries == max_retries
diff --git a/tests/security/test_input_validation.py b/tests/security/test_input_validation.py
deleted file mode 100644
index f481a28042..0000000000
--- a/tests/security/test_input_validation.py
+++ /dev/null
@@ -1,497 +0,0 @@
-"""
-Input Validation Security Tests
-
-Tests that verify user inputs are properly validated and sanitized
-across all application entry points.
-"""
-
-import pytest
-from tests.test_utils import add_src_to_path
-
-add_src_to_path()
-
-
-class TestResearchQueryValidation:
- """Test validation of research query inputs."""
-
- def test_query_length_validation(self):
- """Test that overly long queries are rejected or truncated."""
- # Very long query (potential DoS or buffer overflow)
- extremely_long_query = "test query " * 10000 # ~100KB
-
- # Should validate query length
- # Either reject with error or truncate to reasonable length
-
- # Maximum reasonable query length: 1000-10000 characters
- max_length = 10000
- assert len(extremely_long_query) > max_length # Test is valid
-
- # Implementation should check:
- # if len(query) > max_length:
- # raise ValueError("Query too long")
- # or:
- # query = query[:max_length]
-
- pass # Implementation-specific
-
- def test_query_special_characters_handling(self):
- """Test that special characters in queries are handled safely."""
- special_char_queries = [
- "test\0query", # Null byte
- "test\nquery", # Newline
- "test\r\nquery", # CRLF
- "test\tquery", # Tab
- "test\\query", # Backslash
- "test'query", # Single quote
- 'test"query', # Double quote
- "test`query", # Backtick
- "test;query", # Semicolon
- "test|query", # Pipe
- "test<query>", # Angle brackets
- ]
-
- for query in special_char_queries:
- # Should handle special characters safely
- # Either escape, sanitize, or reject
- assert isinstance(query, str)
-
- def test_unicode_handling(self):
- """Test that Unicode and international characters are handled correctly."""
- unicode_queries = [
- "研究查询", # Chinese
- "Исследование", # Russian
- "بحث", # Arabic (RTL text)
- "🔬📚🧪", # Emojis
- "café ñoño", # Accented characters
- "Σ∑∫", # Mathematical symbols
- ]
-
- for query in unicode_queries:
- # Should properly handle Unicode
- # UTF-8 encoding, no corruption
- assert isinstance(query, str)
- # Verify encoding/decoding works
- encoded = query.encode("utf-8")
- decoded = encoded.decode("utf-8")
- assert decoded == query
-
- def test_empty_and_whitespace_queries(self):
- """Test handling of empty and whitespace-only queries."""
- empty_queries = [
- "", # Empty string
- " ", # Single space
- " ", # Multiple spaces
- "\n", # Just newline
- "\t\n ", # Mixed whitespace
- None, # Null
- ]
-
- for query in empty_queries:
- # Should reject empty queries or handle gracefully
- # return meaningful error message
-
- if query is None:
- # None should be rejected or converted to empty string
- pass
- else:
- # Whitespace-only queries should be rejected
- stripped = query.strip() if query else ""
- if not stripped:
- # Should reject
- pass
-
- def test_path_traversal_in_query(self):
- """Test that path traversal attempts in queries are sanitized."""
- path_traversal_queries = [
- "../../../etc/passwd",
- "..\\..\\..\\windows\\system32",
- "....//....//....//etc/passwd",
- "%2e%2e%2f%2e%2e%2fetc/passwd", # URL encoded
- "test/../../../etc/passwd",
- ]
-
- for query in path_traversal_queries:
- # Query itself is fine (just a search string)
- # But should never be used directly in file system operations
- # If saving query to file, sanitize filename
-
- # Sanitization example:
- import re
-
- sanitized = re.sub(r"[./\\]", "", query)
- # Should not contain path separators
- assert "../" not in sanitized
-
- def test_command_injection_in_query(self):
- """Test that command injection attempts are prevented."""
- command_injection_queries = [
- "test; ls -la",
- "test | cat /etc/passwd",
- "test && rm -rf /",
- "test $(whoami)",
- "test `whoami`",
- "test; DROP TABLE users;--",
- ]
-
- for query in command_injection_queries:
- # Query string itself is safe (just text)
- # But must NEVER be passed to shell commands
-
- # NEVER do:
- # os.system(f"search {query}") # Dangerous!
-
- # Safe approaches:
- # - Use query as data only (database queries, API calls)
- # - Never concatenate into shell commands
- # - If shell needed, use subprocess with args list
-
- assert True # Query string is safe as data
-
-
-class TestURLInputValidation:
- """Test validation of URL inputs."""
-
- def test_url_scheme_validation(self):
- """Test that only allowed URL schemes are accepted."""
- allowed_schemes = ["http", "https"]
- dangerous_schemes = [
- "file:///etc/passwd",
- "javascript:alert('XSS')",
- "data:text/html,<script>alert('XSS')</script>",
- "ftp://example.com",
- "gopher://example.com",
- ]
-
- from urllib.parse import urlparse
-
- for url in dangerous_schemes:
- parsed = urlparse(url)
- scheme = parsed.scheme.lower()
-
- # Should reject non-HTTP(S) schemes
- assert scheme not in allowed_schemes
-
- def test_internal_url_blocking(self):
- """Test that internal/private URLs are blocked (SSRF prevention)."""
- # DevSkim: ignore DS137138 - Intentionally using insecure URLs for testing SSRF prevention
- internal_urls = [
- "http://localhost/admin",
- "http://127.0.0.1/",
- "http://[::1]/", # IPv6 localhost
- "http://169.254.169.254/latest/meta-data/", # AWS metadata
- "http://192.168.1.1/", # Private network
- "http://10.0.0.1/", # Private network
- "http://172.16.0.1/", # Private network
- ]
-
- for url in internal_urls:
- # Should block internal URLs when fetching external content
- # Use URL whitelist or IP address validation
- from urllib.parse import urlparse
-
- parsed = urlparse(url)
- hostname = parsed.hostname
-
- # Check if IP is internal
- if hostname:
- import ipaddress
-
- try:
- ip = ipaddress.ip_address(hostname)
- # Should block private/local IPs
- is_private = (
- ip.is_private
- or ip.is_loopback
- or ip.is_link_local
- or ip.is_reserved
- )
- if is_private:
- # Should reject
- pass
- except ValueError:
- # Not an IP address, might be hostname
- # Check for localhost, local domains
- if hostname.lower() in ["localhost", "127.0.0.1", "::1"]:
- # Should reject
- pass
-
- @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
- def test_url_redirect_handling(self):
- """Test that URL redirects are handled safely."""
- # URL redirects can be used for SSRF attacks:
- # 1. Attacker provides public URL
- # 2. Public URL redirects to internal URL
- # 3. Server follows redirect to internal resource
-
- # Mitigation:
- # - Validate final destination after redirects
- # - Limit number of redirects
- # - Check each redirect hop against blacklist
-
- pass # Implementation-specific
-
-
-class TestFileUploadValidation:
- """Test file upload security (if applicable)."""
-
- def test_file_type_validation(self):
- """Test that file types are validated."""
- # If LDR accepts file uploads:
- # - Validate file extension (whitelist)
- # - Validate MIME type
- # - Validate file content (magic bytes)
-
- allowed_extensions = [".txt", ".pdf", ".md"]
- dangerous_extensions = [
- ".exe",
- ".sh",
- ".bat",
- ".php",
- ".jsp",
- ".aspx",
- ]
-
- # Should reject dangerous file types
- for ext in dangerous_extensions:
- # Implementation should check:
- # if file_extension not in allowed_extensions:
- # raise ValueError("File type not allowed")
- assert ext not in allowed_extensions
-
- def test_file_size_limits(self):
- """Test that file size is limited."""
- # File size limits prevent:
- # - DoS via large uploads
- # - Disk space exhaustion
- # - Memory exhaustion
-
- max_file_size = 10 * 1024 * 1024 # 10 MB example
-
- # Implementation should check:
- # if file_size > max_file_size:
- # raise ValueError("File too large")
-
- assert max_file_size > 0
-
- def test_filename_sanitization(self):
- """Test that filenames are sanitized."""
- dangerous_filenames = [
- "../../../etc/passwd", # Path traversal
- "test.php.jpg", # Double extension
- "file\0.txt", # Null byte injection
- "CON.txt", # Windows reserved name
- "<script>.txt", # HTML injection
- ]
-
- import re
-
- for filename in dangerous_filenames:
- # Sanitize filename
- # Remove path separators, special characters
- sanitized = re.sub(r"[^a-zA-Z0-9._-]", "_", filename)
- sanitized = sanitized.replace("..", "")
-
- # Sanitized filename should be safe
- assert ".." not in sanitized
- assert "/" not in sanitized
- assert "\\" not in sanitized
-
-
-class TestDatabaseInputValidation:
- """Test validation for database-bound inputs."""
-
- def test_id_format_validation(self):
- """Test that ID parameters are validated."""
- # IDs should be validated to expected format
- # Prevents SQL injection and other attacks
-
- valid_id_patterns = [
- r"^[a-zA-Z0-9-_]{1,100}$", # Alphanumeric with hyphens
- r"^\d+$", # Numeric only
- r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", # UUID
- ]
-
- malicious_ids = [
- "1' OR '1'='1", # SQL injection
- "../admin", # Path traversal
- "<script>alert('xss')</script>", # XSS
- "$(rm -rf /)", # Command injection
- ]
-
- import re
-
- for pattern in valid_id_patterns:
- for mal_id in malicious_ids:
- # Malicious IDs should not match valid patterns
- assert not re.match(pattern, mal_id)
-
- def test_integer_bounds_validation(self):
- """Test that integer inputs are within valid bounds."""
- # Integer overflow/underflow prevention
- import sys
-
- test_values = [
- -1, # Negative (if not allowed)
- 0, # Zero (might be invalid)
- sys.maxsize, # Maximum int
- sys.maxsize + 1, # Overflow
- -sys.maxsize - 1, # Minimum int
- ]
-
- # Validate integers are within expected range
- min_value = 0
- max_value = 1000000
-
- for value in test_values:
- if value < min_value or value > max_value:
- # Should reject out-of-bounds values
- pass
-
-
-class TestSecurityInputValidationDocumentation:
- """Documentation tests for input validation strategy."""
-
- @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
- def test_input_validation_principles(self):
- """
- Document input validation best practices.
-
- Defense in Depth - Input Validation Layers:
- 1. Client-side validation (UX, not security)
- 2. API layer validation (format, type, length)
- 3. Business logic validation (rules, constraints)
- 4. Database layer validation (constraints, types)
-
- Input Validation Strategies:
- 1. Whitelist > Blacklist
- - Define what is allowed (whitelist)
- - Reject everything else
- - Don't try to block specific patterns (blacklist)
-
- 2. Validate Early
- - Validate at entry point
- - Fail fast with clear errors
- - Don't process invalid data
-
- 3. Sanitize vs Reject
- - Sanitize: Clean/escape dangerous content
- - Reject: Refuse invalid input entirely
- - Prefer reject for security-critical inputs
-
- 4. Context-Specific Validation
- - SQL context: Use parameterized queries
- - HTML context: Use escaping
- - Shell context: Use argument arrays
- - URL context: Use URL parsing/validation
-
- LDR Input Validation Areas:
- 1. Research queries (text)
- 2. URLs for fetching content
- 3. User authentication credentials
- 4. API parameters (research_id, settings)
- 5. File paths (if file operations exist)
-
- Validation Checklist:
- - [ ] Length limits enforced
- - [ ] Type checking (string, int, bool)
- - [ ] Format validation (regex patterns)
- - [ ] Range validation (min/max)
- - [ ] Encoding validation (UTF-8)
- - [ ] Injection prevention (SQL, XSS, command)
- - [ ] Path traversal prevention
- - [ ] SSRF prevention (URL validation)
- """
- assert True # Documentation test
-
- @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
- def test_common_validation_mistakes(self):
- """
- Document common input validation mistakes to avoid.
-
- Common Mistakes:
- 1. Validating after using the input
- - ✗ process(user_input); validate(user_input)
- - ✓ validate(user_input); process(user_input)
-
- 2. Client-side validation only
- - ✗ JavaScript validation without server check
- - ✓ Validate on both client and server
-
- 3. Blacklist instead of whitelist
- - ✗ if input contains "script": reject
- - ✓ if input matches "[a-z0-9]": accept
-
- 4. Inconsistent validation
- - ✗ Different rules in different endpoints
- - ✓ Centralized validation functions
-
- 5. Trusting "safe" sources
- - ✗ Database values are safe (stored XSS!)
- - ✓ Validate/escape all external data
-
- 6. Insufficient error handling
- - ✗ Silent failures, continue processing
- - ✓ Explicit errors, stop processing
-
- 7. Double encoding issues
- - URL encode, then HTML encode, etc.
- - Different contexts need different encoding
- """
- assert True # Documentation test
-
-
[email protected](reason="documentation/placeholder test - not implemented")
-def test_comprehensive_input_validation():
- """
- Comprehensive test documenting all input validation requirements.
-
- Input Types and Validation:
-
- 1. Text Inputs (research queries, usernames, etc.):
- - Length: 1-10000 characters
- - Encoding: UTF-8
- - Special chars: Allowed but escaped for output
- - Whitespace: Trimmed
- - Empty: Rejected
-
- 2. URLs:
- - Scheme: http/https only
- - No internal IPs (SSRF prevention)
- - Length limit: 2000 characters
- - Validation: url parse + IP check
-
- 3. IDs (research_id, user_id):
- - Format: UUID or alphanumeric
- - Length: Fixed or max 100 chars
- - Special chars: Not allowed
- - SQL injection: Prevented by ORM
-
- 4. Integers (page numbers, limits, counts):
- - Range: 0-1000000
- - Type: Python int
- - Overflow: Checked
-
- 5. Booleans (flags, settings):
- - Values: true/false, 1/0
- - Type: Python bool
- - Validation: Strict type checking
-
- 6. JSON Payloads:
- - Schema validation
- - Required fields check
- - Type checking
- - Size limit: 1MB
-
- 7. File Uploads (if applicable):
- - Type whitelist
- - Size limit: 10MB
- - Filename sanitization
- - Content validation
-
- All validation should:
- - Happen before processing
- - Provide clear error messages
- - Log validation failures
- - Never expose sensitive info in errors
- """
- assert True # Documentation test
diff --git a/tests/security/test_session_security_extended.py b/tests/security/test_session_security_extended.py
deleted file mode 100644
index bb46316bbb..0000000000
--- a/tests/security/test_session_security_extended.py
+++ /dev/null
@@ -1,579 +0,0 @@
-"""
-Extended tests for session security - Comprehensive session management coverage.
-
-Tests cover:
-- Session ID generation and uniqueness
-- Session expiry enforcement
-- Session tokens and refresh
-- Session binding (IP, user agent)
-- Hijack detection and replay prevention
-- Cookie security settings
-- Session storage and cleanup
-"""
-
-import secrets
-import time
-
-
-class TestSessionIdGeneration:
- """Tests for session ID generation."""
-
- def test_session_id_is_cryptographically_secure(self):
- """Session IDs should use cryptographically secure random."""
- session_id = secrets.token_hex(32)
- assert len(session_id) == 64
- assert isinstance(session_id, str)
-
- def test_session_id_format(self):
- """Session ID should be hex string."""
- session_id = secrets.token_hex(32)
- # All characters should be valid hex
- assert all(c in "0123456789abcdef" for c in session_id)
-
- def test_session_id_length_sufficient(self):
- """Session ID should be at least 128 bits (32 hex chars)."""
- session_id = secrets.token_hex(16) # 16 bytes = 128 bits
- assert len(session_id) >= 32
-
-
-class TestSessionIdUniqueness:
- """Tests for session ID uniqueness."""
-
- def test_generated_ids_are_unique(self):
- """Generated session IDs should be unique."""
- ids = set()
- for _ in range(10000):
- session_id = secrets.token_hex(32)
- assert session_id not in ids
- ids.add(session_id)
-
- def test_no_collision_in_large_set(self):
- """Should not have collisions in large sets."""
- ids = [secrets.token_hex(32) for _ in range(10000)]
- assert len(ids) == len(set(ids))
-
-
-class TestSessionExpiryEnforcement:
- """Tests for session expiry enforcement."""
-
- def test_session_expires_after_ttl(self):
- """Session should expire after TTL."""
- session = {
- "id": "test_session",
- "created_at": time.time() - 3700, # Created 1hr 1min ago
- "ttl": 3600, # 1 hour
- }
-
- def is_expired(session):
- return time.time() - session["created_at"] > session["ttl"]
-
- assert is_expired(session) is True
-
- def test_session_valid_before_expiry(self):
- """Session should be valid before TTL."""
- session = {
- "id": "test_session",
- "created_at": time.time() - 1800, # Created 30min ago
- "ttl": 3600, # 1 hour
- }
-
- def is_expired(session):
- return time.time() - session["created_at"] > session["ttl"]
-
- assert is_expired(session) is False
-
- def test_session_expiry_at_boundary(self):
- """Session should expire exactly at TTL boundary."""
- now = time.time()
- session = {
- "id": "test_session",
- "created_at": now - 3600, # Exactly at TTL
- "ttl": 3600,
- }
-
- def is_expired(session, current_time):
- return current_time - session["created_at"] >= session["ttl"]
-
- assert is_expired(session, now) is True
-
-
-class TestSessionRefreshToken:
- """Tests for session refresh token handling."""
-
- def test_refresh_token_generation(self):
- """Refresh token should be generated securely."""
- refresh_token = secrets.token_urlsafe(32)
- assert len(refresh_token) >= 32
-
- def test_refresh_token_rotation(self):
- """Refresh token should be rotated on use."""
- session = {
- "refresh_token": secrets.token_urlsafe(32),
- }
-
- old_token = session["refresh_token"]
-
- # Rotate token
- session["refresh_token"] = secrets.token_urlsafe(32)
-
- assert session["refresh_token"] != old_token
-
- def test_refresh_extends_session(self):
- """Refresh should extend session expiry."""
- session = {
- "expires_at": time.time() + 100, # 100 seconds left
- "ttl": 3600,
- }
-
- def refresh_session(session):
- session["expires_at"] = time.time() + session["ttl"]
-
- old_expiry = session["expires_at"]
- refresh_session(session)
-
- assert session["expires_at"] > old_expiry
-
-
-class TestSlidingExpiry:
- """Tests for sliding session expiry."""
-
- def test_sliding_expiry_extends_on_activity(self):
- """Session expiry should extend on activity."""
- session = {
- "last_activity": time.time() - 600, # 10 min ago
- "sliding_window": 1800, # 30 min
- }
-
- def update_activity(session):
- session["last_activity"] = time.time()
-
- update_activity(session)
-
- # Should not be expired now
- def is_expired(session):
- return (
- time.time() - session["last_activity"]
- > session["sliding_window"]
- )
-
- assert is_expired(session) is False
-
-
-class TestAbsoluteExpiry:
- """Tests for absolute session expiry."""
-
- def test_absolute_expiry_not_extendable(self):
- """Absolute expiry should not be extended."""
- session = {
- "absolute_expiry": time.time() - 1, # Already expired
- "sliding_expiry": time.time() + 3600, # Still valid sliding
- }
-
- def is_absolutely_expired(session):
- return time.time() > session["absolute_expiry"]
-
- assert is_absolutely_expired(session) is True
-
-
-class TestSessionInvalidationCascade:
- """Tests for session invalidation cascade."""
-
- def test_invalidation_clears_related_sessions(self):
- """Invalidating one session should clear related sessions."""
- user_sessions = {
- "user1": ["session1", "session2", "session3"],
- }
-
- def invalidate_all_sessions(user_id):
- user_sessions[user_id] = []
-
- invalidate_all_sessions("user1")
-
- assert len(user_sessions["user1"]) == 0
-
- def test_single_session_invalidation(self):
- """Should be able to invalidate single session."""
- user_sessions = {
- "user1": ["session1", "session2", "session3"],
- }
-
- def invalidate_session(user_id, session_id):
- if session_id in user_sessions[user_id]:
- user_sessions[user_id].remove(session_id)
-
- invalidate_session("user1", "session2")
-
- assert "session2" not in user_sessions["user1"]
- assert len(user_sessions["user1"]) == 2
-
-
-class TestConcurrentSessionLimit:
- """Tests for concurrent session limits."""
-
- def test_concurrent_session_limit_enforced(self):
- """Should enforce maximum concurrent sessions."""
- max_sessions = 3
- user_sessions = {
- "user1": ["s1", "s2", "s3"],
- }
-
- def can_create_session(user_id):
- return len(user_sessions.get(user_id, [])) < max_sessions
-
- assert can_create_session("user1") is False
- assert can_create_session("user2") is True
-
- def test_oldest_session_evicted(self):
- """Oldest session should be evicted when limit reached."""
- max_sessions = 3
- sessions = [
- {"id": "s1", "created": 1},
- {"id": "s2", "created": 2},
- {"id": "s3", "created": 3},
- ]
-
- def add_session_with_eviction(new_session):
- if len(sessions) >= max_sessions:
- # Remove oldest
- sessions.sort(key=lambda s: s["created"])
- sessions.pop(0)
- sessions.append(new_session)
-
- add_session_with_eviction({"id": "s4", "created": 4})
-
- assert len(sessions) == 3
- assert "s1" not in [s["id"] for s in sessions]
-
-
-class TestSessionDeviceTracking:
- """Tests for session device tracking."""
-
- def test_device_fingerprint_stored(self):
- """Device fingerprint should be stored with session."""
- session = {
- "id": "session1",
- "device": {
- "user_agent": "Mozilla/5.0...",
- "screen_resolution": "1920x1080",
- "timezone": "America/New_York",
- },
- }
-
- assert "device" in session
- assert "user_agent" in session["device"]
-
- def test_device_change_detection(self):
- """Should detect device changes."""
- original_device = {
- "user_agent": "Chrome/100",
- }
- new_device = {
- "user_agent": "Firefox/100",
- }
-
- def device_changed(original, new):
- return original.get("user_agent") != new.get("user_agent")
-
- assert device_changed(original_device, new_device) is True
-
-
-class TestSessionIPBinding:
- """Tests for session IP binding."""
-
- def test_ip_bound_to_session(self):
- """IP address should be bound to session."""
- session = {
- "id": "session1",
- "ip_address": "192.168.1.100",
- }
-
- def validate_ip(session, request_ip):
- return session["ip_address"] == request_ip
-
- assert validate_ip(session, "192.168.1.100") is True
- assert validate_ip(session, "10.0.0.1") is False
-
- def test_ip_binding_optional(self):
- """IP binding should be configurable."""
- config = {"strict_ip_binding": False}
- session = {"id": "session1", "ip_address": "192.168.1.100"}
-
- def validate_ip(session, request_ip, config):
- if not config.get("strict_ip_binding", True):
- return True
- return session["ip_address"] == request_ip
-
- assert validate_ip(session, "10.0.0.1", config) is True
-
-
-class TestUserAgentCheck:
- """Tests for user agent validation."""
-
- def test_user_agent_bound_to_session(self):
- """User agent should be bound to session."""
- session = {
- "id": "session1",
- "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
- }
-
- def validate_user_agent(session, request_ua):
- return session["user_agent"] == request_ua
-
- assert (
- validate_user_agent(
- session, "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
- )
- is True
- )
-
- def test_user_agent_mismatch_detection(self):
- """Should detect user agent mismatch."""
- session = {
- "user_agent": "Chrome/100",
- }
-
- def user_agent_changed(session, new_ua):
- return session["user_agent"] != new_ua
-
- assert user_agent_changed(session, "Firefox/100") is True
-
-
-class TestSessionHijackDetection:
- """Tests for session hijack detection."""
-
- def test_hijack_detected_on_ip_change(self):
- """Session hijack should be detected on IP change."""
- session = {"ip_address": "192.168.1.100", "user_agent": "Chrome/100"}
- request = {"ip_address": "10.0.0.1", "user_agent": "Chrome/100"}
-
- def detect_hijack(session, request):
- return session["ip_address"] != request["ip_address"]
-
- assert detect_hijack(session, request) is True
-
- def test_hijack_detected_on_multiple_factors(self):
- """Hijack detection should consider multiple factors."""
- session = {"ip_address": "192.168.1.100", "user_agent": "Chrome/100"}
- request = {"ip_address": "10.0.0.1", "user_agent": "Firefox/100"}
-
- def calculate_risk_score(session, request):
- score = 0
- if session["ip_address"] != request["ip_address"]:
- score += 50
- if session["user_agent"] != request["user_agent"]:
- score += 30
- return score
-
- assert calculate_risk_score(session, request) == 80
-
-
-class TestSessionReplayPrevention:
- """Tests for session replay attack prevention."""
-
- def test_nonce_prevents_replay(self):
- """Nonce should prevent session replay."""
- used_nonces = set()
-
- def validate_nonce(nonce):
- if nonce in used_nonces:
- return False # Replay detected
- used_nonces.add(nonce)
- return True
-
- assert validate_nonce("nonce1") is True
- assert validate_nonce("nonce1") is False # Replay
-
- def test_timestamp_validation(self):
- """Request timestamp should be within acceptable window."""
- window = 300 # 5 minutes
-
- def validate_timestamp(request_time, server_time):
- return abs(server_time - request_time) <= window
-
- now = time.time()
- assert validate_timestamp(now, now) is True
- assert validate_timestamp(now - 400, now) is False
-
-
-class TestCookieSecureFlag:
- """Tests for cookie Secure flag."""
-
- def test_secure_flag_set(self):
- """Secure flag should be set for session cookies."""
- cookie = {"name": "session", "value": "abc123", "secure": True}
-
- assert cookie["secure"] is True
-
- def test_secure_flag_required_in_production(self):
- """Secure flag should be required in production."""
- environment = "production"
- secure_required = environment == "production"
-
- assert secure_required is True
-
-
-class TestCookieHttpOnlyFlag:
- """Tests for cookie HttpOnly flag."""
-
- def test_httponly_flag_set(self):
- """HttpOnly flag should be set for session cookies."""
- cookie = {"name": "session", "value": "abc123", "httponly": True}
-
- assert cookie["httponly"] is True
-
- def test_httponly_prevents_js_access(self):
- """HttpOnly prevents JavaScript access to cookie."""
- # This is a behavioral test - HttpOnly is enforced by browser
- cookie = {"httponly": True}
- assert cookie["httponly"] is True
-
-
-class TestCookieSameSiteAttribute:
- """Tests for cookie SameSite attribute."""
-
- def test_samesite_strict(self):
- """SameSite should be Strict for sensitive cookies."""
- cookie = {"name": "session", "samesite": "Strict"}
-
- assert cookie["samesite"] in ["Strict", "Lax", "None"]
-
- def test_samesite_lax_default(self):
- """SameSite Lax is a reasonable default."""
- cookie = {"name": "session", "samesite": "Lax"}
-
- assert cookie["samesite"] == "Lax"
-
-
-class TestSessionDatabaseCleanup:
- """Tests for session database cleanup."""
-
- def test_expired_sessions_cleaned_up(self):
- """Expired sessions should be cleaned up."""
- sessions = {
- "s1": {"expires": time.time() - 100}, # Expired
- "s2": {"expires": time.time() + 100}, # Valid
- "s3": {"expires": time.time() - 50}, # Expired
- }
-
- def cleanup_expired():
- now = time.time()
- expired = [k for k, v in sessions.items() if v["expires"] < now]
- for k in expired:
- del sessions[k]
-
- cleanup_expired()
-
- assert "s1" not in sessions
- assert "s2" in sessions
- assert "s3" not in sessions
-
- def test_cleanup_runs_periodically(self):
- """Cleanup should run periodically."""
- cleanup_interval = 300 # 5 minutes
- last_cleanup = time.time() - 400 # 6+ minutes ago
-
- def should_run_cleanup(last_cleanup, interval):
- return time.time() - last_cleanup > interval
-
- assert should_run_cleanup(last_cleanup, cleanup_interval) is True
-
-
-class TestSessionRedisStorage:
- """Tests for Redis session storage."""
-
- def test_session_stored_in_redis(self):
- """Sessions should be storable in Redis."""
- redis_mock = {}
-
- def store_session(session_id, data, ttl):
- redis_mock[session_id] = {"data": data, "ttl": ttl}
-
- def get_session(session_id):
- return redis_mock.get(session_id, {}).get("data")
-
- store_session("session1", {"user_id": "user1"}, 3600)
-
- assert get_session("session1") == {"user_id": "user1"}
-
- def test_session_ttl_in_redis(self):
- """Session TTL should be set in Redis."""
- redis_mock = {}
-
- def store_with_ttl(key, data, ttl):
- redis_mock[key] = {"data": data, "ttl": ttl, "created": time.time()}
-
- store_with_ttl("session1", {"user": "test"}, 3600)
-
- assert redis_mock["session1"]["ttl"] == 3600
-
-
-class TestSessionEncryption:
- """Tests for session data encryption."""
-
- def test_session_data_encrypted(self):
- """Session data should be encrypted at rest."""
- import base64
-
- session_data = {"user_id": "user1", "role": "admin"}
-
- def encrypt_session(data, key="secret"):
- # Simple encoding for test (real impl uses proper encryption)
- return base64.b64encode(str(data).encode()).decode()
-
- encrypted = encrypt_session(session_data)
-
- # Should not contain plaintext
- assert "user1" not in encrypted
-
- def test_session_data_decryptable(self):
- """Encrypted session data should be decryptable."""
- import base64
-
- original = {"user_id": "user1"}
-
- def encrypt(data):
- return base64.b64encode(str(data).encode()).decode()
-
- def decrypt(encrypted):
- return base64.b64decode(encrypted.encode()).decode()
-
- encrypted = encrypt(original)
- decrypted = decrypt(encrypted)
-
- assert "user1" in decrypted
-
-
-class TestSessionRevocationList:
- """Tests for session revocation list."""
-
- def test_revoked_session_rejected(self):
- """Revoked sessions should be rejected."""
- revocation_list = {"session1", "session3"}
-
- def is_revoked(session_id):
- return session_id in revocation_list
-
- assert is_revoked("session1") is True
- assert is_revoked("session2") is False
-
- def test_revocation_list_cleanup(self):
- """Old entries should be cleaned from revocation list."""
- revocations = {
- "s1": {"revoked_at": time.time() - 86500}, # Old (>24h)
- "s2": {"revoked_at": time.time() - 100}, # Recent
- }
-
- def cleanup_old_revocations(max_age=86400):
- now = time.time()
- old = [
- k
- for k, v in revocations.items()
- if now - v["revoked_at"] > max_age
- ]
- for k in old:
- del revocations[k]
-
- cleanup_old_revocations()
-
- assert "s1" not in revocations
- assert "s2" in revocations
diff --git a/tests/utilities/test_search_cache_extended.py b/tests/utilities/test_search_cache_extended.py
deleted file mode 100644
index 72c2be3375..0000000000
--- a/tests/utilities/test_search_cache_extended.py
+++ /dev/null
@@ -1,418 +0,0 @@
-"""
-Tests for search cache extended functionality.
-
-Tests cover:
-- Stampede protection
-- Cache edge cases
-"""
-
-import threading
-import time
-
-
-class TestStampedeProtectionExtended:
- """Tests for cache stampede protection."""
-
- def test_stampede_double_check_locking(self):
- """Double-check locking prevents duplicate fetches."""
- cache = {}
- lock = threading.Lock()
- fetch_count = {"count": 0}
-
- def get_or_fetch(key):
- if key in cache:
- return cache[key]
-
- with lock:
- # Double check inside lock
- if key in cache:
- return cache[key]
-
- fetch_count["count"] += 1
- cache[key] = f"value_{key}"
- return cache[key]
-
- # Simulate concurrent access
- threads = [
- threading.Thread(target=get_or_fetch, args=("key1",))
- for _ in range(10)
- ]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- # Should only fetch once
- assert fetch_count["count"] == 1
-
- def test_stampede_event_signaling(self):
- """Event signaling coordinates waiting threads."""
- fetch_events = {}
- cache = {}
-
- def get_with_event(key):
- if key in cache:
- return cache[key]
-
- if key not in fetch_events:
- fetch_events[key] = threading.Event()
- # Simulate fetch
- time.sleep(0.01)
- cache[key] = "fetched_value"
- fetch_events[key].set()
- else:
- # Wait for fetch to complete
- fetch_events[key].wait(timeout=1.0)
-
- return cache.get(key)
-
- result = get_with_event("test_key")
-
- assert result == "fetched_value"
-
- def test_stampede_timeout_30s(self):
- """Timeout after 30 seconds of waiting."""
-
- event = threading.Event()
- start = time.time()
-
- # Simulate waiting with short timeout for test
- event.wait(timeout=0.01)
- time.time() - start
-
- # In real code, would check if elapsed > timeout_seconds
- timed_out = not event.is_set()
-
- assert timed_out
-
- def test_stampede_stale_event_detection(self):
- """Stale events are detected and cleaned up."""
- events = {
- "key1": {"event": threading.Event(), "created": time.time() - 60},
- "key2": {"event": threading.Event(), "created": time.time() - 10},
- }
- stale_threshold = 30
-
- stale_keys = [
- k
- for k, v in events.items()
- if time.time() - v["created"] > stale_threshold
- ]
-
- assert "key1" in stale_keys
- assert "key2" not in stale_keys
-
- def test_stampede_cleanup_thread_timing(self):
- """Cleanup thread runs periodically."""
- cleanup_interval = 60
- last_cleanup = time.time() - 70
-
- should_cleanup = time.time() - last_cleanup > cleanup_interval
-
- assert should_cleanup
-
- def test_stampede_cleanup_conflicts(self):
- """Cleanup doesn't conflict with active fetches."""
- active_fetches = {"key1", "key2"}
- stale_keys = {"key1", "key3"}
-
- # Only clean keys not actively being fetched
- safe_to_clean = stale_keys - active_fetches
-
- assert "key3" in safe_to_clean
- assert "key1" not in safe_to_clean
-
- def test_stampede_race_condition_window(self):
- """Race condition window is minimized."""
- cache = {}
- lock = threading.RLock() # Reentrant lock
- race_detected = {"value": False}
-
- def safe_update(key, value):
- with lock:
- if key in cache:
- race_detected["value"] = True
- cache[key] = value
-
- # Simulate concurrent updates
- t1 = threading.Thread(target=safe_update, args=("key", "value1"))
- t2 = threading.Thread(target=safe_update, args=("key", "value2"))
-
- t1.start()
- t2.start()
- t1.join()
- t2.join()
-
- # One value should win
- assert cache["key"] in ["value1", "value2"]
-
- def test_stampede_concurrent_fetches_same_query(self):
- """Concurrent fetches for same query are coalesced."""
- fetch_results = {}
- fetch_lock = threading.Lock()
- in_progress = {}
-
- def fetch_coalesced(query):
- with fetch_lock:
- if query in in_progress:
- # Wait for in-progress fetch
- return in_progress[query]["result"]
-
- in_progress[query] = {"result": None}
-
- # Simulate fetch
- result = f"result_{query}"
-
- with fetch_lock:
- in_progress[query]["result"] = result
- fetch_results[query] = result
-
- return result
-
- result = fetch_coalesced("test_query")
-
- assert result == "result_test_query"
-
- def test_stampede_fetch_result_propagation(self):
- """Fetch result is propagated to all waiting threads."""
- result_ready = threading.Event()
- shared_result = {"value": None}
- received_results = []
-
- def wait_for_result():
- result_ready.wait(timeout=1.0)
- received_results.append(shared_result["value"])
-
- # Start waiting threads
- threads = [threading.Thread(target=wait_for_result) for _ in range(5)]
- for t in threads:
- t.start()
-
- # Simulate fetch completion
- time.sleep(0.01)
- shared_result["value"] = "fetched_data"
- result_ready.set()
-
- for t in threads:
- t.join()
-
- assert all(r == "fetched_data" for r in received_results)
-
- def test_stampede_error_in_fetch_func(self):
- """Error in fetch function is handled."""
- error_occurred = {"value": False}
-
- def fetch_with_error():
- raise ConnectionError("Fetch failed")
-
- try:
- fetch_with_error()
- except ConnectionError:
- error_occurred["value"] = True
-
- assert error_occurred["value"]
-
-
-class TestCacheEdgeCases:
- """Tests for cache edge cases."""
-
- def test_cache_memory_pressure_eviction(self):
- """Items are evicted under memory pressure."""
- max_items = 100
- cache = {}
-
- # Fill cache beyond capacity
- for i in range(150):
- cache[f"key_{i}"] = f"value_{i}"
- if len(cache) > max_items:
- # Evict oldest
- oldest_key = next(iter(cache))
- del cache[oldest_key]
-
- assert len(cache) == max_items
-
- def test_cache_ttl_boundary_conditions(self):
- """TTL boundary conditions are handled."""
- ttl_seconds = 300
- current_time = time.time()
-
- entries = [
- {"key": "expired", "created": current_time - 301},
- {"key": "valid", "created": current_time - 299},
- {"key": "exact", "created": current_time - 300},
- ]
-
- valid_entries = [
- e for e in entries if current_time - e["created"] < ttl_seconds
- ]
-
- assert len(valid_entries) == 1
- assert valid_entries[0]["key"] == "valid"
-
- def test_cache_unicode_query_normalization(self):
- """Unicode queries are normalized."""
- queries = [
- "café",
- "cafe\u0301", # e + combining acute accent
- ]
-
- import unicodedata
-
- normalized = [unicodedata.normalize("NFC", q) for q in queries]
-
- # After normalization, they should be comparable
- assert len(normalized) == 2
-
- def test_cache_very_long_query(self):
- """Very long queries are handled."""
- max_query_length = 1000
- long_query = "x" * 2000
-
- if len(long_query) > max_query_length:
- truncated = long_query[:max_query_length]
- else:
- truncated = long_query
-
- assert len(truncated) == max_query_length
-
- def test_cache_concurrent_invalidation(self):
- """Concurrent cache invalidation is safe."""
- cache = {"key1": "value1", "key2": "value2", "key3": "value3"}
- lock = threading.Lock()
- invalidated = []
-
- def invalidate(key):
- with lock:
- if key in cache:
- del cache[key]
- invalidated.append(key)
-
- threads = [
- threading.Thread(target=invalidate, args=(f"key{i}",))
- for i in range(1, 4)
- ]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(cache) == 0
- assert len(invalidated) == 3
-
-
-class TestCacheMetrics:
- """Tests for cache metrics."""
-
- def test_cache_hit_rate_calculation(self):
- """Cache hit rate is calculated correctly."""
- hits = 80
- misses = 20
- total = hits + misses
-
- hit_rate = hits / total * 100
-
- assert hit_rate == 80.0
-
- def test_cache_miss_rate_calculation(self):
- """Cache miss rate is calculated correctly."""
- hits = 75
- misses = 25
- total = hits + misses
-
- miss_rate = misses / total * 100
-
- assert miss_rate == 25.0
-
- def test_cache_size_tracking(self):
- """Cache size is tracked."""
- cache = {}
-
- for i in range(10):
- cache[f"key_{i}"] = f"value_{i}"
-
- size = len(cache)
-
- assert size == 10
-
- def test_cache_eviction_count(self):
- """Eviction count is tracked."""
- eviction_count = 0
- max_size = 5
- cache = {}
-
- for i in range(10):
- if len(cache) >= max_size:
- oldest = next(iter(cache))
- del cache[oldest]
- eviction_count += 1
- cache[f"key_{i}"] = f"value_{i}"
-
- assert eviction_count == 5
-
- def test_cache_average_entry_age(self):
- """Average entry age is calculated."""
- current_time = time.time()
- entries = [
- {"created": current_time - 60},
- {"created": current_time - 120},
- {"created": current_time - 180},
- ]
-
- ages = [current_time - e["created"] for e in entries]
- avg_age = sum(ages) / len(ages)
-
- assert avg_age == 120.0
-
-
-class TestCacheKeyGeneration:
- """Tests for cache key generation."""
-
- def test_cache_key_from_query(self):
- """Cache key is generated from query."""
- query = "test search query"
-
- import hashlib
-
- key = hashlib.md5(query.encode()).hexdigest() # noqa: E501 # DevSkim: ignore DS126858
-
- assert len(key) == 32
-
- def test_cache_key_includes_engine(self):
- """Cache key includes search engine."""
- query = "test query"
- engine = "google"
-
- combined = f"{engine}:{query}"
- import hashlib
-
- key = hashlib.md5(combined.encode()).hexdigest() # noqa: E501 # DevSkim: ignore DS126858
-
- assert len(key) == 32
-
- def test_cache_key_case_sensitivity(self):
- """Cache keys are case-normalized."""
- query1 = "Test Query"
- query2 = "test query"
-
- normalized1 = query1.lower()
- normalized2 = query2.lower()
-
- assert normalized1 == normalized2
-
- def test_cache_key_whitespace_handling(self):
- """Cache keys normalize whitespace."""
- query = " test query "
-
- normalized = " ".join(query.split())
-
- assert normalized == "test query"
-
- def test_cache_key_special_characters(self):
- """Cache keys handle special characters."""
- query = "test@query#with$special%chars"
-
- import hashlib
-
- key = hashlib.md5(query.encode()).hexdigest() # noqa: E501 # DevSkim: ignore DS126858
-
- assert len(key) == 32
diff --git a/tests/web/queue/test_processor_v2_core.py b/tests/web/queue/test_processor_v2_core.py
deleted file mode 100644
index 715f784dc4..0000000000
--- a/tests/web/queue/test_processor_v2_core.py
+++ /dev/null
@@ -1,410 +0,0 @@
-"""
-Tests for queue processor v2 core functionality.
-
-Tests cover:
-- Process queue loop
-- Process user queue
-- Start queued researches
-"""
-
-import threading
-import time
-
-
-class TestProcessQueueLoop:
- """Tests for the main queue processing loop."""
-
- def test_process_queue_loop_execution(self):
- """Queue loop executes when running."""
- running = True
- iterations = 0
- max_iterations = 3
-
- while running and iterations < max_iterations:
- iterations += 1
- if iterations >= max_iterations:
- running = False
-
- assert iterations == max_iterations
-
- def test_process_queue_loop_user_check_list_processing(self):
- """Queue loop processes user check list."""
- users_to_check = {"user1:session1", "user2:session2", "user3:session3"}
- processed_users = []
-
- for user_session in users_to_check:
- username, session_id = user_session.split(":", 1)
- processed_users.append(username)
-
- assert len(processed_users) == 3
- assert "user1" in processed_users
-
- def test_process_queue_loop_queue_empty_detection(self):
- """Queue loop detects empty queue."""
- queue_status = {"queued_tasks": 0, "active_tasks": 0}
-
- queue_empty = queue_status["queued_tasks"] == 0
-
- assert queue_empty
-
- def test_process_queue_loop_error_handling_in_loop(self):
- """Queue loop handles errors gracefully."""
- errors_caught = []
-
- try:
- raise Exception("Processing error")
- except Exception as e:
- errors_caught.append(str(e))
- # Loop continues after error
-
- assert len(errors_caught) == 1
-
- def test_process_queue_loop_thread_safety_concurrent_ops(self):
- """Queue loop is thread-safe."""
- users_to_check = set()
- lock = threading.Lock()
-
- def add_user(username):
- with lock:
- users_to_check.add(username)
-
- threads = []
- for i in range(10):
- t = threading.Thread(target=add_user, args=(f"user{i}",))
- threads.append(t)
- t.start()
-
- for t in threads:
- t.join()
-
- assert len(users_to_check) == 10
-
- def test_process_queue_loop_check_interval_timing(self):
- """Queue loop respects check interval."""
- check_interval = 0.1 # 100ms
- start_time = time.time()
-
- # Simulate one interval
- time.sleep(check_interval)
-
- elapsed = time.time() - start_time
-
- assert elapsed >= check_interval
-
- def test_process_queue_loop_stop_flag_respected(self):
- """Queue loop stops when flag is set."""
- running = True
- iterations = 0
-
- while running:
- iterations += 1
- if iterations == 3:
- running = False # Stop flag
-
- assert iterations == 3
-
- def test_process_queue_loop_user_removal_during_processing(self):
- """Users are removed from check list when queue empty."""
- users_to_check = {"user1:session1", "user2:session2"}
- users_to_remove = []
-
- for user_session in users_to_check:
- # Simulate queue empty for user1
- if "user1" in user_session:
- users_to_remove.append(user_session)
-
- for user_session in users_to_remove:
- users_to_check.discard(user_session)
-
- assert len(users_to_check) == 1
- assert "user2:session2" in users_to_check
-
- def test_process_queue_loop_multiple_users_independence(self):
- """Multiple users are processed independently."""
- user_queues = {
- "user1": {"queued": 5, "active": 1},
- "user2": {"queued": 0, "active": 2},
- "user3": {"queued": 3, "active": 0},
- }
-
- users_with_work = [u for u, q in user_queues.items() if q["queued"] > 0]
-
- assert len(users_with_work) == 2
- assert "user2" not in users_with_work
-
- def test_process_queue_loop_database_error_recovery(self):
- """Queue loop recovers from database errors."""
- db_errors = 0
- max_retries = 3
-
- for _ in range(max_retries):
- try:
- # Simulate DB error
- raise Exception("Database error")
- except Exception:
- db_errors += 1
- # Continue processing
-
- assert db_errors == max_retries
-
-
-class TestProcessUserQueue:
- """Tests for processing individual user queues."""
-
- def test_process_user_queue_password_retrieval(self):
- """Password is retrieved from session store."""
- session_passwords = {"user1:session1": "password123"}
-
- username = "user1"
- session_id = "session1"
- key = f"{username}:{session_id}"
-
- password = session_passwords.get(key)
-
- assert password == "password123"
-
- def test_process_user_queue_database_opening_error(self):
- """Database opening error is handled."""
- db_open_success = False
-
- if not db_open_success:
- # Keep checking - could be temporary
- keep_checking = True
- else:
- keep_checking = False
-
- assert keep_checking
-
- def test_process_user_queue_queue_status_retrieval(self):
- """Queue status is retrieved correctly."""
- queue_status = {"active_tasks": 2, "queued_tasks": 5}
-
- assert queue_status["active_tasks"] == 2
- assert queue_status["queued_tasks"] == 5
-
- def test_process_user_queue_available_slots_calculation(self):
- """Available slots are calculated correctly."""
- max_concurrent = 3
- active_tasks = 1
-
- available_slots = max_concurrent - active_tasks
-
- assert available_slots == 2
-
- def test_process_user_queue_return_value_queue_empty(self):
- """Returns True when queue is empty."""
- queued_tasks = 0
-
- queue_empty = queued_tasks == 0
-
- assert queue_empty
-
- def test_process_user_queue_return_value_queue_not_empty(self):
- """Returns False when queue has items."""
- queued_tasks = 5
-
- queue_empty = queued_tasks == 0
-
- assert not queue_empty
-
- def test_process_user_queue_session_expired_handling(self):
- """Session expired removes user from checking."""
- password = None # Session expired
-
- if not password:
- remove_from_checking = True
- else:
- remove_from_checking = False
-
- assert remove_from_checking
-
- def test_process_user_queue_settings_manager_integration(self):
- """Settings manager is used for user settings."""
- settings = {
- "app.queue_mode": "direct",
- "app.max_concurrent_researches": 3,
- }
-
- queue_mode = settings.get("app.queue_mode", "direct")
- max_concurrent = settings.get("app.max_concurrent_researches", 3)
-
- assert queue_mode == "direct"
- assert max_concurrent == 3
-
- def test_process_user_queue_concurrent_access_safety(self):
- """User queue processing is thread-safe."""
- processing_users = set()
- lock = threading.Lock()
-
- def process_user(username):
- with lock:
- processing_users.add(username)
- time.sleep(0.01)
- with lock:
- processing_users.discard(username)
-
- threads = []
- for i in range(5):
- t = threading.Thread(target=process_user, args=(f"user{i}",))
- threads.append(t)
- t.start()
-
- for t in threads:
- t.join()
-
- assert len(processing_users) == 0
-
- def test_process_user_queue_transaction_rollback(self):
- """Transaction is rolled back on error."""
- transaction_committed = False
- transaction_rolled_back = False
-
- try:
- # Simulate error
- raise Exception("Transaction error")
- transaction_committed = True
- except Exception:
- transaction_rolled_back = True
-
- assert not transaction_committed
- assert transaction_rolled_back
-
-
-class TestStartQueuedResearches:
- """Tests for starting queued researches."""
-
- def test_start_queued_researches_fetch_items(self):
- """Queued items are fetched correctly."""
- queued_items = [
- {"research_id": 1, "position": 1},
- {"research_id": 2, "position": 2},
- {"research_id": 3, "position": 3},
- ]
-
- fetched = queued_items[:2] # Limit to available slots
-
- assert len(fetched) == 2
-
- def test_start_queued_researches_ordering_by_position(self):
- """Items are ordered by position."""
- queued_items = [
- {"research_id": 3, "position": 3},
- {"research_id": 1, "position": 1},
- {"research_id": 2, "position": 2},
- ]
-
- sorted_items = sorted(queued_items, key=lambda x: x["position"])
-
- assert sorted_items[0]["research_id"] == 1
- assert sorted_items[1]["research_id"] == 2
- assert sorted_items[2]["research_id"] == 3
-
- def test_start_queued_researches_processing_flag_set(self):
- """Processing flag is set before starting."""
- queued_research = {"is_processing": False}
-
- # Set flag
- queued_research["is_processing"] = True
-
- assert queued_research["is_processing"]
-
- def test_start_queued_researches_processing_flag_reset_on_error(self):
- """Processing flag is reset on error."""
- queued_research = {"is_processing": False}
-
- try:
- queued_research["is_processing"] = True
- raise Exception("Start error")
- except Exception:
- queued_research["is_processing"] = False
-
- assert not queued_research["is_processing"]
-
- def test_start_queued_researches_task_status_updates(self):
- """Task status is updated during processing."""
- statuses = []
-
- statuses.append("queued")
- statuses.append("processing")
- statuses.append("started")
-
- assert statuses == ["queued", "processing", "started"]
-
- def test_start_queued_researches_max_concurrent_limit(self):
- """Max concurrent limit is respected."""
- available_slots = 2
- queued_count = 5
-
- to_start = min(available_slots, queued_count)
-
- assert to_start == 2
-
- def test_start_queued_researches_empty_queue(self):
- """Empty queue returns without starting."""
- queued_items = []
-
- started_count = 0
- for item in queued_items:
- started_count += 1
-
- assert started_count == 0
-
- def test_start_queued_researches_all_slots_filled(self):
- """No starts when all slots filled."""
- available_slots = 0
-
- can_start = available_slots > 0
-
- assert not can_start
-
- def test_start_queued_researches_partial_start_on_error(self):
- """Partial starts are completed before error."""
- queued_items = [1, 2, 3, 4, 5]
- started = []
-
- for i, item in enumerate(queued_items):
- if i == 3:
- break # Error on 4th item
- started.append(item)
-
- assert len(started) == 3
-
- def test_start_queued_researches_database_commit(self):
- """Database is committed after starting."""
- commit_count = 0
-
- # Each successful start commits
- for _ in range(3):
- commit_count += 1
-
- assert commit_count == 3
-
-
-class TestQueueProcessorInitialization:
- """Tests for queue processor initialization."""
-
- def test_initialization_default_interval(self):
- """Default check interval is set."""
- default_interval = 10
-
- assert default_interval == 10
-
- def test_initialization_empty_sets(self):
- """User check set is empty on init."""
- users_to_check = set()
-
- assert len(users_to_check) == 0
-
- def test_initialization_pending_operations_dict(self):
- """Pending operations dict is empty on init."""
- pending_operations = {}
-
- assert len(pending_operations) == 0
-
- def test_initialization_locks_created(self):
- """Thread locks are created."""
- users_lock = threading.Lock()
- pending_lock = threading.Lock()
-
- assert users_lock is not None
- assert pending_lock is not None
diff --git a/tests/web/queue/test_processor_v2_operations.py b/tests/web/queue/test_processor_v2_operations.py
deleted file mode 100644
index e5d1158cb1..0000000000
--- a/tests/web/queue/test_processor_v2_operations.py
+++ /dev/null
@@ -1,261 +0,0 @@
-"""
-Tests for queue processor v2 pending operations.
-
-Tests cover:
-- Pending operations processing
-"""
-
-import threading
-import time
-import uuid
-
-
-class TestPendingOperations:
- """Tests for pending operations processing."""
-
- def test_pending_operations_progress_update_execution(self):
- """Progress update operation is executed."""
- operations = {
- "op1": {
- "username": "testuser",
- "operation_type": "progress_update",
- "research_id": 123,
- "progress": 50,
- }
- }
-
- for op_id, op_data in operations.items():
- if op_data["operation_type"] == "progress_update":
- progress = op_data["progress"]
- assert progress == 50
-
- def test_pending_operations_error_update_execution(self):
- """Error update operation is executed."""
- operations = {
- "op1": {
- "username": "testuser",
- "operation_type": "error_update",
- "research_id": 123,
- "status": "failed",
- "error_message": "Test error",
- }
- }
-
- for op_id, op_data in operations.items():
- if op_data["operation_type"] == "error_update":
- status = op_data["status"]
- error_msg = op_data["error_message"]
- assert status == "failed"
- assert error_msg == "Test error"
-
- def test_pending_operations_removal_from_dict(self):
- """Operations are removed after processing."""
- operations = {"op1": {"data": "test"}, "op2": {"data": "test2"}}
-
- # Process and remove
- for op_id in list(operations.keys()):
- del operations[op_id]
-
- assert len(operations) == 0
-
- def test_pending_operations_error_handling_with_rollback(self):
- """Errors trigger rollback."""
- rollback_called = False
-
- try:
- raise Exception("Operation error")
- except Exception:
- rollback_called = True
-
- assert rollback_called
-
- def test_pending_operations_concurrent_access_safety(self):
- """Concurrent access is thread-safe."""
- operations = {}
- lock = threading.Lock()
-
- def add_operation(op_id, data):
- with lock:
- operations[op_id] = data
-
- threads = []
- for i in range(10):
- t = threading.Thread(
- target=add_operation, args=(f"op{i}", {"idx": i})
- )
- threads.append(t)
- t.start()
-
- for t in threads:
- t.join()
-
- assert len(operations) == 10
-
- def test_pending_operations_multiple_operations_same_user(self):
- """Multiple operations for same user are processed."""
- operations = {
- "op1": {"username": "user1", "research_id": 1},
- "op2": {"username": "user1", "research_id": 2},
- "op3": {"username": "user1", "research_id": 3},
- }
-
- user1_ops = [
- op for op in operations.values() if op["username"] == "user1"
- ]
-
- assert len(user1_ops) == 3
-
- def test_pending_operations_ordering_preservation(self):
- """Operations are processed in order."""
- operations = []
-
- for i in range(5):
- operations.append({"order": i, "timestamp": time.time()})
-
- # Process in order
- for i, op in enumerate(operations):
- assert op["order"] == i
-
- def test_pending_operations_lock_acquisition(self):
- """Lock is acquired before processing."""
- lock = threading.Lock()
- acquired = []
-
- with lock:
- acquired.append(True)
-
- assert len(acquired) == 1
-
- def test_pending_operations_database_session_handling(self):
- """Database session is used for updates."""
- session_used = False
-
- # Simulate session use
- session_used = True
-
- assert session_used
-
- def test_pending_operations_partial_failure_recovery(self):
- """Partial failures are handled."""
- operations = [1, 2, 3, 4, 5]
- processed = []
- failed = []
-
- for op in operations:
- try:
- if op == 3:
- raise Exception("Op 3 failed")
- processed.append(op)
- except Exception:
- failed.append(op)
-
- assert len(processed) == 4
- assert len(failed) == 1
- assert failed[0] == 3
-
-
-class TestQueueProgressUpdate:
- """Tests for queueing progress updates."""
-
- def test_queue_progress_update_creates_operation(self):
- """Progress update creates operation entry."""
- pending_operations = {}
- operation_id = str(uuid.uuid4())
-
- pending_operations[operation_id] = {
- "username": "testuser",
- "operation_type": "progress_update",
- "research_id": 123,
- "progress": 75,
- "timestamp": time.time(),
- }
-
- assert operation_id in pending_operations
- assert pending_operations[operation_id]["progress"] == 75
-
- def test_queue_progress_update_thread_safe(self):
- """Progress update queuing is thread-safe."""
- pending_operations = {}
- lock = threading.Lock()
-
- def queue_update(progress):
- op_id = str(uuid.uuid4())
- with lock:
- pending_operations[op_id] = {"progress": progress}
-
- threads = []
- for i in range(10):
- t = threading.Thread(target=queue_update, args=(i * 10,))
- threads.append(t)
- t.start()
-
- for t in threads:
- t.join()
-
- assert len(pending_operations) == 10
-
-
-class TestQueueErrorUpdate:
- """Tests for queueing error updates."""
-
- def test_queue_error_update_creates_operation(self):
- """Error update creates operation entry."""
- pending_operations = {}
- operation_id = str(uuid.uuid4())
-
- pending_operations[operation_id] = {
- "username": "testuser",
- "operation_type": "error_update",
- "research_id": 123,
- "status": "failed",
- "error_message": "Test error",
- "metadata": {"phase": "search"},
- "completed_at": "2024-01-01T00:00:00Z",
- "report_path": None,
- "timestamp": time.time(),
- }
-
- assert operation_id in pending_operations
- assert pending_operations[operation_id]["status"] == "failed"
-
- def test_queue_error_update_includes_metadata(self):
- """Error update includes metadata."""
- metadata = {"phase": "synthesis", "iterations": 3}
-
- operation = {
- "metadata": metadata,
- }
-
- assert operation["metadata"]["phase"] == "synthesis"
- assert operation["metadata"]["iterations"] == 3
-
-
-class TestProcessUserRequest:
- """Tests for processing user request."""
-
- def test_process_user_request_adds_to_check_list(self):
- """User is added to check list."""
- users_to_check = set()
-
- users_to_check.add("user1:session1")
-
- assert "user1:session1" in users_to_check
-
- def test_process_user_request_returns_queued_count(self):
- """Returns number of queued tasks."""
- queued_tasks = 5
-
- result = queued_tasks if queued_tasks > 0 else 0
-
- assert result == 5
-
- def test_process_user_request_error_handling(self):
- """Errors are handled gracefully."""
- result = 0
-
- try:
- raise Exception("Request error")
- except Exception:
- result = 0
-
- assert result == 0
diff --git a/tests/web/queue/test_processor_v2_research.py b/tests/web/queue/test_processor_v2_research.py
deleted file mode 100644
index fefd955e1c..0000000000
--- a/tests/web/queue/test_processor_v2_research.py
+++ /dev/null
@@ -1,292 +0,0 @@
-"""
-Tests for queue processor v2 research handling.
-
-Tests cover:
-- Start research
-- Direct execution mode
-"""
-
-import threading
-
-
-class TestStartResearch:
- """Tests for starting individual researches."""
-
- def test_start_research_lookup_from_history(self):
- """Research is looked up from history."""
- research_history = {
- 123: {"query": "test", "mode": "quick"},
- 456: {"query": "another", "mode": "detailed"},
- }
-
- research_id = 123
- research = research_history.get(research_id)
-
- assert research is not None
- assert research["query"] == "test"
-
- def test_start_research_lookup_retry_with_backoff(self):
- """Research lookup retries with backoff."""
- attempts = []
- max_retries = 3
- initial_delay = 0.5
-
- for attempt in range(max_retries):
- delay = initial_delay * (2**attempt)
- attempts.append(delay)
-
- assert attempts == [0.5, 1.0, 2.0]
-
- def test_start_research_max_retries_exceeded(self):
- """Error raised when max retries exceeded."""
- max_retries = 3
- current_retry = 3
-
- if current_retry >= max_retries:
- should_raise = True
- else:
- should_raise = False
-
- assert should_raise
-
- def test_start_research_settings_snapshot_new_structure(self):
- """New settings snapshot structure is handled."""
- settings_snapshot = {
- "submission": {
- "model_provider": "ollama",
- "model": "mistral",
- },
- "settings_snapshot": {
- "llm.temperature": 0.7,
- },
- }
-
- if "submission" in settings_snapshot:
- submission_params = settings_snapshot["submission"]
- complete_settings = settings_snapshot.get("settings_snapshot", {})
- else:
- submission_params = settings_snapshot
- complete_settings = {}
-
- assert submission_params["model_provider"] == "ollama"
- assert complete_settings.get("llm.temperature") == 0.7
-
- def test_start_research_settings_snapshot_legacy_structure(self):
- """Legacy settings snapshot structure is handled."""
- settings_snapshot = {
- "model_provider": "openai",
- "model": "gpt-4",
- }
-
- if "submission" in settings_snapshot:
- submission_params = settings_snapshot["submission"]
- else:
- submission_params = settings_snapshot
-
- assert submission_params["model_provider"] == "openai"
-
- def test_start_research_user_active_research_creation(self):
- """Active research record is created."""
- active_record = {
- "username": "testuser",
- "research_id": 123,
- "status": "in_progress",
- "thread_id": "pending",
- }
-
- assert active_record["status"] == "in_progress"
- assert active_record["thread_id"] == "pending"
-
- def test_start_research_thread_creation(self):
- """Research thread is created."""
- thread = threading.Thread(target=lambda: None, daemon=True)
-
- assert thread is not None
- assert thread.daemon
-
- def test_start_research_thread_id_tracking(self):
- """Thread ID is tracked after start."""
- thread = threading.Thread(target=lambda: None)
- thread.start()
- thread_id = thread.ident
- thread.join()
-
- assert thread_id is not None
-
- def test_start_research_exception_handling_cleanup(self):
- """Exception during start triggers cleanup."""
- cleanup_called = False
-
- try:
- raise Exception("Start error")
- except Exception:
- cleanup_called = True
-
- assert cleanup_called
-
- def test_start_research_settings_snapshot_passing(self):
- """Settings snapshot is passed to research."""
- settings_snapshot = {"llm.model": "gpt-4"}
-
- # Passed to start_research_process
- passed_settings = settings_snapshot.copy()
-
- assert passed_settings["llm.model"] == "gpt-4"
-
- def test_start_research_research_options_propagation(self):
- """Research options are propagated correctly."""
- options = {
- "max_results": 10,
- "time_period": "7d",
- "iterations": 3,
- "questions_per_iteration": 5,
- "strategy": "source-based",
- }
-
- for key, value in options.items():
- assert value is not None
-
- def test_start_research_custom_search_engine_handling(self):
- """Custom search engine is handled."""
- search_engine = "google"
-
- # Custom engine passed to research
- assert search_engine in ["google", "duckduckgo", "bing", "auto"]
-
-
-class TestDirectExecutionMode:
- """Tests for direct execution mode."""
-
- def test_direct_execution_mode_settings_check(self):
- """Direct mode is checked from settings."""
- queue_mode = "direct"
-
- is_direct_mode = queue_mode == "direct"
-
- assert is_direct_mode
-
- def test_direct_execution_mode_max_concurrent_check(self):
- """Max concurrent is checked from settings."""
- max_concurrent = 3
-
- assert max_concurrent > 0
-
- def test_direct_execution_mode_active_research_counting(self):
- """Active researches are counted correctly."""
- active_researches = [
- {"status": "in_progress"},
- {"status": "in_progress"},
- {"status": "completed"},
- ]
-
- active_count = sum(
- 1 for r in active_researches if r["status"] == "in_progress"
- )
-
- assert active_count == 2
-
- def test_direct_execution_mode_slot_availability(self):
- """Slot availability is calculated correctly."""
- max_concurrent = 3
- active_count = 1
-
- slots_available = max_concurrent - active_count
-
- assert slots_available == 2
-
- def test_direct_execution_mode_fallback_to_queue(self):
- """Falls back to queue when no slots available."""
- max_concurrent = 3
- active_count = 3
-
- slots_available = max_concurrent - active_count
- use_queue = slots_available <= 0
-
- assert use_queue
-
- def test_direct_execution_mode_settings_snapshot_passing(self):
- """Settings snapshot is passed in direct mode."""
- settings_snapshot = {"llm.provider": "ollama"}
-
- # Passed directly
- assert "llm.provider" in settings_snapshot
-
- def test_direct_execution_mode_immediate_start(self):
- """Research starts immediately in direct mode."""
- queue_mode = "direct"
- slots_available = 2
-
- start_immediately = queue_mode == "direct" and slots_available > 0
-
- assert start_immediately
-
- def test_direct_execution_mode_error_recovery(self):
- """Direct mode recovers from errors."""
- error_occurred = False
- cleanup_done = False
-
- try:
- raise Exception("Direct start error")
- except Exception:
- error_occurred = True
- cleanup_done = True
-
- assert error_occurred
- assert cleanup_done
-
-
-class TestNotifyResearchCompleted:
- """Tests for research completion notification."""
-
- def test_notify_completed_updates_task_status(self):
- """Completion updates task status."""
- task_status = "processing"
-
- task_status = "completed"
-
- assert task_status == "completed"
-
- def test_notify_completed_sends_notification(self):
- """Completion sends notification."""
- notification_sent = False
-
- # Simulate notification
- notification_sent = True
-
- assert notification_sent
-
- def test_notify_completed_with_password(self):
- """Completion works with password."""
- password = "test_password"
-
- has_password = bool(password)
-
- assert has_password
-
-
-class TestNotifyResearchFailed:
- """Tests for research failure notification."""
-
- def test_notify_failed_updates_task_status(self):
- """Failure updates task status."""
- task_status = "processing"
-
- task_status = "failed"
-
- assert task_status == "failed"
-
- def test_notify_failed_includes_error_message(self):
- """Failure includes error message."""
- error_message = "LLM unavailable"
-
- has_error_message = bool(error_message)
-
- assert has_error_message
-
- def test_notify_failed_sends_notification(self):
- """Failure sends notification."""
- notification_sent = False
-
- notification_sent = True
-
- assert notification_sent
diff --git a/tests/web/routes/test_route_registry_extended.py b/tests/web/routes/test_route_registry_extended.py
deleted file mode 100644
index ab902472ff..0000000000
--- a/tests/web/routes/test_route_registry_extended.py
+++ /dev/null
@@ -1,538 +0,0 @@
-"""
-Extended tests for route_registry - Central documentation of all application routes.
-
-Tests cover:
-- Route registry structure
-- get_all_routes() function
-- get_routes_by_blueprint() function
-- find_route() function
-- Route data validation
-- Blueprint configuration
-"""
-
-
-class TestRouteRegistryStructure:
- """Tests for ROUTE_REGISTRY structure."""
-
- def test_registry_contains_research_blueprint(self):
- """Registry should contain research blueprint."""
- registry = {
- "research": {
- "blueprint": "research_bp",
- "url_prefix": None,
- "routes": [],
- }
- }
- assert "research" in registry
-
- def test_registry_contains_api_v1_blueprint(self):
- """Registry should contain api_v1 blueprint."""
- registry = {
- "api_v1": {
- "blueprint": "api_blueprint",
- "url_prefix": "/api/v1",
- "routes": [],
- }
- }
- assert "api_v1" in registry
-
- def test_registry_contains_history_blueprint(self):
- """Registry should contain history blueprint."""
- registry = {
- "history": {
- "blueprint": "history_bp",
- "url_prefix": "/history",
- "routes": [],
- }
- }
- assert "history" in registry
-
- def test_registry_contains_settings_blueprint(self):
- """Registry should contain settings blueprint."""
- registry = {
- "settings": {
- "blueprint": "settings_bp",
- "url_prefix": "/settings",
- "routes": [],
- }
- }
- assert "settings" in registry
-
- def test_registry_contains_metrics_blueprint(self):
- """Registry should contain metrics blueprint."""
- registry = {
- "metrics": {
- "blueprint": "metrics_bp",
- "url_prefix": "/metrics",
- "routes": [],
- }
- }
- assert "metrics" in registry
-
- def test_blueprint_info_has_required_keys(self):
- """Blueprint info should have required keys."""
- blueprint_info = {
- "blueprint": "test_bp",
- "url_prefix": "/test",
- "routes": [],
- }
-
- assert "blueprint" in blueprint_info
- assert "url_prefix" in blueprint_info
- assert "routes" in blueprint_info
-
- def test_route_tuple_structure(self):
- """Route tuple should have 4 elements."""
- route = ("GET", "/", "index", "Home page")
-
- assert len(route) == 4
- assert route[0] == "GET"
- assert route[1] == "/"
- assert route[2] == "index"
- assert route[3] == "Home page"
-
-
-class TestGetAllRoutes:
- """Tests for get_all_routes function."""
-
- def test_returns_list(self):
- """get_all_routes should return a list."""
- all_routes = []
- assert isinstance(all_routes, list)
-
- def test_route_dict_has_method(self):
- """Route dict should have method key."""
- route = {
- "method": "GET",
- "path": "/",
- "endpoint": "research.index",
- "description": "Home page",
- "blueprint": "research",
- }
- assert "method" in route
- assert route["method"] == "GET"
-
- def test_route_dict_has_path(self):
- """Route dict should have path key."""
- route = {
- "method": "GET",
- "path": "/api/history",
- "endpoint": "research.get_history",
- "description": "Get history",
- "blueprint": "research",
- }
- assert "path" in route
- assert route["path"] == "/api/history"
-
- def test_route_dict_has_endpoint(self):
- """Route dict should have endpoint key."""
- route = {
- "method": "POST",
- "path": "/api/start_research",
- "endpoint": "research.start_research",
- "description": "Start research",
- "blueprint": "research",
- }
- assert "endpoint" in route
- assert "." in route["endpoint"] # Blueprint.endpoint format
-
- def test_route_dict_has_description(self):
- """Route dict should have description key."""
- route = {
- "method": "GET",
- "path": "/settings",
- "endpoint": "settings.settings_page",
- "description": "Settings page",
- "blueprint": "settings",
- }
- assert "description" in route
- assert len(route["description"]) > 0
-
- def test_route_dict_has_blueprint(self):
- """Route dict should have blueprint key."""
- route = {
- "method": "GET",
- "path": "/metrics",
- "endpoint": "metrics.metrics_dashboard",
- "description": "Metrics dashboard",
- "blueprint": "metrics",
- }
- assert "blueprint" in route
-
- def test_prefix_concatenation_with_prefix(self):
- """Should concatenate prefix with path."""
- prefix = "/api/v1"
- path = "/health"
- full_path = f"{prefix}{path}"
-
- assert full_path == "/api/v1/health"
-
- def test_prefix_concatenation_without_prefix(self):
- """Should use path directly when no prefix."""
- prefix = None
- path = "/"
- full_path = f"{prefix}{path}" if prefix else path
-
- assert full_path == "/"
-
- def test_endpoint_format(self):
- """Endpoint should be blueprint.endpoint format."""
- blueprint_name = "research"
- endpoint = "index"
- full_endpoint = f"{blueprint_name}.{endpoint}"
-
- assert full_endpoint == "research.index"
-
-
-class TestGetRoutesByBlueprint:
- """Tests for get_routes_by_blueprint function."""
-
- def test_returns_list(self):
- """get_routes_by_blueprint should return a list."""
- routes = []
- assert isinstance(routes, list)
-
- def test_unknown_blueprint_returns_empty(self):
- """Unknown blueprint should return empty list."""
- registry = {
- "research": {
- "blueprint": "research_bp",
- "url_prefix": None,
- "routes": [],
- }
- }
- blueprint_name = "unknown"
-
- if blueprint_name not in registry:
- result = []
- else:
- result = registry[blueprint_name]["routes"]
-
- assert result == []
-
- def test_valid_blueprint_returns_routes(self):
- """Valid blueprint should return its routes."""
- registry = {
- "research": {
- "blueprint": "research_bp",
- "url_prefix": None,
- "routes": [("GET", "/", "index", "Home page")],
- }
- }
-
- result = registry["research"]["routes"]
- assert len(result) == 1
-
- def test_route_dict_structure(self):
- """Route dict should have expected structure."""
- route = {
- "method": "GET",
- "path": "/settings/api",
- "endpoint": "api_get_all_settings",
- "description": "Get all settings",
- }
-
- assert "method" in route
- assert "path" in route
- assert "endpoint" in route
- assert "description" in route
-
- def test_prefix_applied_to_routes(self):
- """Prefix should be applied to all routes."""
- prefix = "/settings"
- path = "/api"
- full_path = f"{prefix}{path}"
-
- assert full_path == "/settings/api"
-
-
-class TestFindRoute:
- """Tests for find_route function."""
-
- def test_returns_list(self):
- """find_route should return a list."""
- matching_routes = []
- assert isinstance(matching_routes, list)
-
- def test_case_insensitive_matching(self):
- """Should match routes case-insensitively."""
- pattern = "/API"
- route_path = "/api/history"
-
- matches = pattern.lower() in route_path.lower()
- assert matches is True
-
- def test_partial_matching(self):
- """Should match partial path patterns."""
- pattern = "research"
- route_path = "/api/research/123/status"
-
- matches = pattern.lower() in route_path.lower()
- assert matches is True
-
- def test_no_match_returns_empty(self):
- """No matching routes should return empty list."""
- pattern = "nonexistent"
- routes = [
- {"path": "/api/history"},
- {"path": "/settings"},
- ]
-
- matching = [r for r in routes if pattern.lower() in r["path"].lower()]
- assert matching == []
-
- def test_multiple_matches(self):
- """Should return all matching routes."""
- pattern = "api"
- routes = [
- {"path": "/api/history"},
- {"path": "/api/start_research"},
- {"path": "/settings"},
- ]
-
- matching = [r for r in routes if pattern.lower() in r["path"].lower()]
- assert len(matching) == 2
-
- def test_matching_preserves_route_info(self):
- """Matching should preserve full route info."""
- route = {
- "method": "GET",
- "path": "/api/history",
- "endpoint": "research.get_history",
- "description": "Get history",
- }
-
- pattern = "history"
- if pattern.lower() in route["path"].lower():
- matched = route
-
- assert matched["method"] == "GET"
- assert matched["endpoint"] == "research.get_history"
-
-
-class TestResearchBlueprintRoutes:
- """Tests for research blueprint routes."""
-
- def test_index_route_exists(self):
- """Index route should exist."""
- route = ("GET", "/", "index", "Home/Research page")
- assert route[0] == "GET"
- assert route[1] == "/"
-
- def test_start_research_route_exists(self):
- """Start research route should exist."""
- route = (
- "POST",
- "/api/start_research",
- "start_research",
- "Start new research",
- )
- assert route[0] == "POST"
-
- def test_get_research_details_route_exists(self):
- """Get research details route should exist."""
- route = (
- "GET",
- "/api/research/<string:research_id>",
- "get_research_details",
- "Get research details",
- )
- assert "<string:research_id>" in route[1]
-
- def test_terminate_research_route_exists(self):
- """Terminate research route should exist."""
- route = (
- "POST",
- "/api/terminate/<string:research_id>",
- "terminate_research",
- "Stop research",
- )
- assert route[0] == "POST"
-
- def test_delete_research_route_exists(self):
- """Delete research route should exist."""
- route = (
- "DELETE",
- "/api/delete/<string:research_id>",
- "delete_research",
- "Delete research",
- )
- assert route[0] == "DELETE"
-
-
-class TestApiV1BlueprintRoutes:
- """Tests for API v1 blueprint routes."""
-
- def test_url_prefix(self):
- """API v1 should have /api/v1 prefix."""
- prefix = "/api/v1"
- assert prefix == "/api/v1"
-
- def test_health_check_route_exists(self):
- """Health check route should exist."""
- route = ("GET", "/health", "health_check", "Health check")
- assert route[2] == "health_check"
-
- def test_quick_summary_route_exists(self):
- """Quick summary route should exist."""
- route = (
- "POST",
- "/quick_summary",
- "api_quick_summary",
- "Quick LLM summary",
- )
- assert route[0] == "POST"
-
- def test_generate_report_route_exists(self):
- """Generate report route should exist."""
- route = (
- "POST",
- "/generate_report",
- "api_generate_report",
- "Generate research report",
- )
- assert route[0] == "POST"
-
-
-class TestSettingsBlueprintRoutes:
- """Tests for settings blueprint routes."""
-
- def test_url_prefix(self):
- """Settings should have /settings prefix."""
- prefix = "/settings"
- assert prefix == "/settings"
-
- def test_save_all_settings_route_exists(self):
- """Save all settings route should exist."""
- route = (
- "POST",
- "/save_all_settings",
- "save_all_settings",
- "Save all settings",
- )
- assert route[0] == "POST"
-
- def test_reset_to_defaults_route_exists(self):
- """Reset to defaults route should exist."""
- route = (
- "POST",
- "/reset_to_defaults",
- "reset_to_defaults",
- "Reset to defaults",
- )
- assert route[0] == "POST"
-
- def test_api_crud_routes_exist(self):
- """API CRUD routes should exist."""
- routes = [
- ("GET", "/api", "api_get_all_settings", "Get all settings"),
- (
- "GET",
- "/api/<path:key>",
- "api_get_setting",
- "Get specific setting",
- ),
- ("POST", "/api/<path:key>", "api_update_setting", "Update setting"),
- (
- "DELETE",
- "/api/<path:key>",
- "api_delete_setting",
- "Delete setting",
- ),
- ]
-
- methods = [r[0] for r in routes]
- assert "GET" in methods
- assert "POST" in methods
- assert "DELETE" in methods
-
-
-class TestMetricsBlueprintRoutes:
- """Tests for metrics blueprint routes."""
-
- def test_url_prefix(self):
- """Metrics should have /metrics prefix."""
- prefix = "/metrics"
- assert prefix == "/metrics"
-
- def test_metrics_dashboard_route_exists(self):
- """Metrics dashboard route should exist."""
- route = ("GET", "/", "metrics_dashboard", "Metrics dashboard")
- assert route[2] == "metrics_dashboard"
-
- def test_costs_page_route_exists(self):
- """Costs page route should exist."""
- route = ("GET", "/costs", "costs_page", "Costs page")
- assert route[2] == "costs_page"
-
- def test_ratings_routes_exist(self):
- """Rating routes should exist."""
- routes = [
- (
- "GET",
- "/api/ratings/<string:research_id>",
- "api_get_research_rating",
- "Get research rating",
- ),
- (
- "POST",
- "/api/ratings/<string:research_id>",
- "api_save_research_rating",
- "Save research rating",
- ),
- ]
-
- assert routes[0][0] == "GET"
- assert routes[1][0] == "POST"
-
-
-class TestRoutePathPatterns:
- """Tests for route path patterns."""
-
- def test_string_parameter_pattern(self):
- """Should support string parameter pattern."""
- path = "/api/research/<string:research_id>"
- assert "<string:research_id>" in path
-
- def test_path_parameter_pattern(self):
- """Should support path parameter pattern."""
- path = "/api/<path:key>"
- assert "<path:key>" in path
-
- def test_root_path(self):
- """Should support root path."""
- path = "/"
- assert path == "/"
-
- def test_nested_path(self):
- """Should support nested paths."""
- path = "/api/metrics/research/<string:research_id>/timeline"
- assert path.count("/") == 5
-
-
-class TestHTTPMethods:
- """Tests for HTTP method support."""
-
- def test_get_method_supported(self):
- """GET method should be supported."""
- method = "GET"
- supported_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
- assert method in supported_methods
-
- def test_post_method_supported(self):
- """POST method should be supported."""
- method = "POST"
- supported_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
- assert method in supported_methods
-
- def test_delete_method_supported(self):
- """DELETE method should be supported."""
- method = "DELETE"
- supported_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
- assert method in supported_methods
-
- def test_method_case_sensitivity(self):
- """Methods should be uppercase."""
- methods = ["GET", "POST", "DELETE"]
- for method in methods:
- assert method == method.upper()
diff --git a/tests/web/routes/test_settings_routes_api.py b/tests/web/routes/test_settings_routes_api.py
deleted file mode 100644
index a4b0cf3961..0000000000
--- a/tests/web/routes/test_settings_routes_api.py
+++ /dev/null
@@ -1,177 +0,0 @@
-"""
-Tests for settings routes API endpoints.
-
-Tests cover:
-- Settings CRUD API operations
-"""
-
-
-class TestSettingsAPI:
- """Tests for settings API endpoints."""
-
- def test_api_get_single_setting_success(self):
- """Get single setting succeeds."""
- settings_db = {"llm.model": {"value": "gpt-4", "ui_element": "text"}}
- key = "llm.model"
-
- setting = settings_db.get(key)
-
- assert setting is not None
- assert setting["value"] == "gpt-4"
-
- def test_api_get_single_setting_not_found(self):
- """Get single setting returns 404 when not found."""
- settings_db = {}
- key = "nonexistent.key"
-
- setting = settings_db.get(key)
-
- assert setting is None
-
- def test_api_put_create_new_setting(self):
- """PUT creates new setting."""
- settings_db = {}
- key = "new.setting"
- value = "new_value"
-
- settings_db[key] = {"value": value}
-
- assert key in settings_db
- assert settings_db[key]["value"] == value
-
- def test_api_put_update_existing_setting(self):
- """PUT updates existing setting."""
- settings_db = {"existing.setting": {"value": "old_value"}}
- key = "existing.setting"
- new_value = "new_value"
-
- settings_db[key]["value"] = new_value
-
- assert settings_db[key]["value"] == new_value
-
- def test_api_put_validation_error(self):
- """PUT returns error on validation failure."""
- errors = []
-
- # Simulate validation
- value = "" # Invalid empty value
- if not value:
- errors.append("Value cannot be empty")
-
- assert len(errors) == 1
-
- def test_api_delete_setting_success(self):
- """DELETE removes setting."""
- settings_db = {"to.delete": {"value": "value"}}
- key = "to.delete"
-
- del settings_db[key]
-
- assert key not in settings_db
-
- def test_api_delete_setting_not_found(self):
- """DELETE returns 404 when not found."""
- settings_db = {}
- key = "nonexistent"
-
- exists = key in settings_db
-
- assert not exists
-
- def test_api_bulk_get_all_settings(self):
- """Bulk get returns all settings."""
- settings_db = {
- "setting1": {"value": "val1"},
- "setting2": {"value": "val2"},
- "setting3": {"value": "val3"},
- }
-
- all_settings = list(settings_db.items())
-
- assert len(all_settings) == 3
-
- def test_api_bulk_get_with_category_filter(self):
- """Bulk get with category filter."""
- settings_db = {
- "llm.model": {"value": "gpt-4", "category": "llm"},
- "llm.temperature": {"value": 0.7, "category": "llm"},
- "search.tool": {"value": "google", "category": "search"},
- }
-
- category = "llm"
- filtered = {
- k: v
- for k, v in settings_db.items()
- if v.get("category") == category
- }
-
- assert len(filtered) == 2
-
- def test_api_import_from_defaults(self):
- """Import from defaults creates settings."""
- defaults = {
- "llm.model": "gemma3:12b",
- "llm.provider": "ollama",
- }
-
- settings_db = {}
- for key, value in defaults.items():
- settings_db[key] = {"value": value}
-
- assert len(settings_db) == 2
-
- def test_api_reset_to_defaults(self):
- """Reset replaces with defaults."""
- defaults = {"setting1": "default1"}
- settings_db = {
- "setting1": {"value": "custom"},
- "setting2": {"value": "custom2"},
- }
-
- # Reset
- settings_db.clear()
- for key, value in defaults.items():
- settings_db[key] = {"value": value}
-
- assert settings_db["setting1"]["value"] == "default1"
- assert "setting2" not in settings_db
-
- def test_api_authentication_required(self):
- """API requires authentication."""
- is_authenticated = False
-
- if not is_authenticated:
- status_code = 401
- else:
- status_code = 200
-
- assert status_code == 401
-
- def test_api_session_handling(self):
- """API handles session correctly."""
- session = {"user": "testuser", "authenticated": True}
-
- has_session = "user" in session and session.get("authenticated")
-
- assert has_session
-
- def test_api_rate_limiting(self):
- """API respects rate limits."""
- requests_in_window = 100
- max_requests = 60
-
- rate_limited = requests_in_window > max_requests
-
- assert rate_limited
-
- def test_api_error_response_format(self):
- """API error responses have correct format."""
- error_response = {
- "status": "error",
- "message": "Setting not found",
- "code": 404,
- }
-
- assert error_response["status"] == "error"
- assert "message" in error_response
- assert "code" in error_response
diff --git a/tests/web/routes/test_settings_routes_batch.py b/tests/web/routes/test_settings_routes_batch.py
deleted file mode 100644
index e0ed15784b..0000000000
--- a/tests/web/routes/test_settings_routes_batch.py
+++ /dev/null
@@ -1,255 +0,0 @@
-"""
-Tests for settings routes batch update logic.
-
-Tests cover:
-- Batch update logic
-- Warning calculation
-"""
-
-
-class TestBatchUpdateLogic:
- """Tests for batch settings update logic."""
-
- def test_batch_update_prefetch_optimization(self):
- """Prefetch optimization loads all settings."""
- settings_to_update = ["setting1", "setting2", "setting3"]
- prefetched = {
- key: {"value": f"val_{key}"} for key in settings_to_update
- }
-
- assert len(prefetched) == 3
- assert "setting1" in prefetched
-
- def test_batch_update_validation_error_accumulation(self):
- """Validation errors are accumulated."""
- errors = []
-
- settings = [
- {"key": "setting1", "value": "valid"},
- {"key": "setting2", "value": "invalid!@#"},
- {"key": "setting3", "value": ""},
- ]
-
- for setting in settings:
- if not setting["value"] or "@" in setting["value"]:
- errors.append({"key": setting["key"], "error": "Invalid value"})
-
- assert len(errors) == 2
-
- def test_batch_update_transaction_rollback_on_failure(self):
- """Transaction is rolled back on failure."""
- committed = False
- rolled_back = False
-
- try:
- raise Exception("Update error")
- committed = True
- except Exception:
- rolled_back = True
-
- assert not committed
- assert rolled_back
-
- def test_batch_update_tracking_created_vs_updated(self):
- """Created and updated counts are tracked."""
- existing_settings = {"setting1", "setting3"}
- updates = ["setting1", "setting2", "setting3", "setting4"]
-
- created = 0
- updated = 0
-
- for key in updates:
- if key in existing_settings:
- updated += 1
- else:
- created += 1
-
- assert updated == 2
- assert created == 2
-
- def test_batch_update_partial_success_handling(self):
- """Partial success is reported."""
- results = {
- "success": [],
- "failed": [],
- }
-
- settings = [
- {"key": "setting1", "valid": True},
- {"key": "setting2", "valid": False},
- {"key": "setting3", "valid": True},
- ]
-
- for setting in settings:
- if setting["valid"]:
- results["success"].append(setting["key"])
- else:
- results["failed"].append(setting["key"])
-
- assert len(results["success"]) == 2
- assert len(results["failed"]) == 1
-
- def test_batch_update_empty_batch(self):
- """Empty batch returns early."""
- settings = []
-
- if not settings:
- result = {"updated": 0, "created": 0}
- else:
- result = None
-
- assert result["updated"] == 0
-
- def test_batch_update_single_item(self):
- """Single item batch works."""
- settings = [{"key": "setting1", "value": "value1"}]
-
- processed = 0
- for _ in settings:
- processed += 1
-
- assert processed == 1
-
- def test_batch_update_large_batch_performance(self):
- """Large batch is processed efficiently."""
- settings = [
- {"key": f"setting{i}", "value": f"value{i}"} for i in range(100)
- ]
-
- processed = len(settings)
-
- assert processed == 100
-
- def test_batch_update_concurrent_batches(self):
- """Concurrent batches don't interfere."""
- import threading
-
- results = {"batch1": 0, "batch2": 0}
- lock = threading.Lock()
-
- def process_batch(batch_name, count):
- with lock:
- results[batch_name] = count
-
- t1 = threading.Thread(target=process_batch, args=("batch1", 10))
- t2 = threading.Thread(target=process_batch, args=("batch2", 20))
-
- t1.start()
- t2.start()
- t1.join()
- t2.join()
-
- assert results["batch1"] == 10
- assert results["batch2"] == 20
-
- def test_batch_update_database_commit_timing(self):
- """Database is committed after all updates."""
- commit_count = 0
- updates = ["update1", "update2", "update3"]
-
- for _ in updates:
- pass # Process updates
-
- # Single commit at end
- commit_count = 1
-
- assert commit_count == 1
-
-
-class TestWarningCalculation:
- """Tests for settings warning calculation."""
-
- def test_warning_recalculation_on_key_change(self):
- """Warnings are recalculated when key settings change."""
- trigger_keys = ["llm.provider", "llm.model", "llm.context_window_size"]
-
- updated_key = "llm.model"
- should_recalculate = updated_key in trigger_keys
-
- assert should_recalculate
-
- def test_warning_high_context_local_provider(self):
- """High context warning for local provider."""
- provider = "ollama"
- context_size = 32000
- local_providers = ["ollama", "llamacpp", "lmstudio"]
-
- warnings = []
- if provider in local_providers and context_size > 8192:
- warnings.append(
- {
- "type": "high_context",
- "message": "Large context window may cause memory issues with local models",
- }
- )
-
- assert len(warnings) == 1
- assert "high_context" in warnings[0]["type"]
-
- def test_warning_model_mismatch_70b(self):
- """Warning for large models on limited hardware."""
- model = "llama2:70b"
- warnings = []
-
- if "70b" in model.lower() or "70B" in model:
- warnings.append(
- {
- "type": "model_size",
- "message": "70B models require significant GPU memory",
- }
- )
-
- assert len(warnings) == 1
-
- def test_warning_dismissal_persistence(self):
- """Dismissed warnings stay dismissed."""
- dismissed_warnings = {"high_context_ollama", "model_size_70b"}
-
- new_warning = "high_context_ollama"
- should_show = new_warning not in dismissed_warnings
-
- assert not should_show
-
- def test_warning_multiple_warnings_combination(self):
- """Multiple warnings are combined."""
- warnings = []
-
- # Check various conditions
- if True: # High context
- warnings.append({"type": "high_context"})
- if True: # Large model
- warnings.append({"type": "model_size"})
- if False: # Missing API key
- warnings.append({"type": "missing_key"})
-
- assert len(warnings) == 2
-
-
-class TestSettingsDynamicUpdate:
- """Tests for dynamic settings updates."""
-
- def test_dynamic_model_list_update(self):
- """Model list updates when provider changes."""
- provider = "openai"
- model_lists = {
- "openai": ["gpt-4", "gpt-3.5-turbo"],
- "anthropic": ["claude-3-opus", "claude-3-sonnet"],
- "ollama": ["mistral", "llama2"],
- }
-
- models = model_lists.get(provider, [])
-
- assert "gpt-4" in models
-
- def test_dynamic_search_engine_options(self):
- """Search engine options update based on config."""
- available_engines = ["google", "duckduckgo", "bing"]
-
- api_keys = {"google": True, "bing": False}
-
- enabled_engines = [
- e for e in available_engines if api_keys.get(e, True)
- ]
-
- assert "google" in enabled_engines
- assert "bing" not in enabled_engines
diff --git a/tests/web/routes/test_settings_routes_checkbox.py b/tests/web/routes/test_settings_routes_checkbox.py
deleted file mode 100644
index 9e977490b1..0000000000
--- a/tests/web/routes/test_settings_routes_checkbox.py
+++ /dev/null
@@ -1,281 +0,0 @@
-"""
-Tests for settings routes checkbox handling.
-
-Tests cover:
-- Checkbox dual mode handling
-- Corrupted value detection
-"""
-
-
-class TestCheckboxDualModeHandling:
- """Tests for checkbox dual mode (AJAX and POST) handling."""
-
- def test_checkbox_ajax_mode_boolean_true(self):
- """AJAX mode sends boolean True."""
- value = True
-
- assert value is True
- assert isinstance(value, bool)
-
- def test_checkbox_ajax_mode_boolean_false(self):
- """AJAX mode sends boolean False."""
- value = False
-
- assert value is False
- assert isinstance(value, bool)
-
- def test_checkbox_ajax_mode_string_true(self):
- """AJAX mode string 'true' is converted."""
- value = "true"
-
- # Convert string to boolean
- if isinstance(value, str):
- bool_value = value.lower() == "true"
- else:
- bool_value = bool(value)
-
- assert bool_value is True
-
- def test_checkbox_ajax_mode_string_false(self):
- """AJAX mode string 'false' is converted."""
- value = "false"
-
- if isinstance(value, str):
- bool_value = value.lower() == "true"
- else:
- bool_value = bool(value)
-
- assert bool_value is False
-
- def test_checkbox_post_mode_hidden_input_fallback(self):
- """POST mode uses hidden input fallback."""
- # Hidden input provides default value when checkbox unchecked
- form_data = {"setting_hidden": "false"}
-
- value = form_data.get("setting_hidden", "false")
-
- assert value == "false"
-
- def test_checkbox_post_mode_disabled_state(self):
- """POST mode disabled checkbox uses hidden value."""
- form_data = {"setting_hidden": "false"}
- # Disabled checkbox not in form data
-
- value = form_data.get(
- "setting", form_data.get("setting_hidden", "false")
- )
-
- assert value == "false"
-
- def test_checkbox_post_mode_checked_value(self):
- """POST mode checked checkbox has value."""
- form_data = {"setting": "on", "setting_hidden": "false"}
-
- # Checkbox is present when checked
- checkbox_present = "setting" in form_data
- value = checkbox_present # Convert presence to True
-
- assert value is True
-
- def test_checkbox_post_mode_unchecked_value(self):
- """POST mode unchecked checkbox not in form."""
- form_data = {"setting_hidden": "false"}
-
- # Checkbox absent when unchecked
- checkbox_present = "setting" in form_data
- value = checkbox_present
-
- assert value is False
-
- def test_checkbox_javascript_disabled_fallback(self):
- """Works when JavaScript is disabled."""
- # POST mode should work without JS
- form_data = {"setting_hidden": "false"}
-
- value = form_data.get("setting_hidden", "false")
-
- assert value == "false"
-
- def test_checkbox_conversion_string_to_bool(self):
- """String values are converted to boolean."""
- test_cases = [
- ("true", True),
- ("false", False),
- ("True", True),
- ("False", False),
- ("1", True),
- ("0", False),
- ("on", True),
- ("off", False),
- ]
-
- for string_val, expected in test_cases:
- if string_val.lower() in ["true", "1", "on"]:
- result = True
- else:
- result = False
- assert result == expected, f"Failed for {string_val}"
-
- def test_checkbox_mixed_mode_consistency(self):
- """AJAX and POST produce same result."""
- ajax_value = True
- post_value = "on"
-
- # Both should result in True
- ajax_bool = ajax_value
- post_bool = post_value.lower() in ["true", "1", "on"]
-
- assert ajax_bool == post_bool
-
- def test_checkbox_array_value_handling(self):
- """Array values are handled for multiple checkboxes."""
- values = ["option1", "option3"]
-
- # Multiple selections
- assert len(values) == 2
- assert "option1" in values
-
-
-class TestCorruptedValueDetection:
- """Tests for corrupted value detection."""
-
- def test_corrupted_value_object_object_detection(self):
- """'[object Object]' is detected as corrupted."""
- value = "[object Object]"
-
- is_corrupted = value == "[object Object]"
-
- assert is_corrupted
-
- def test_corrupted_value_empty_json_object_detection(self):
- """Empty JSON object '{}' is detected as corrupted."""
- value = "{}"
-
- is_corrupted = value == "{}"
-
- assert is_corrupted
-
- def test_corrupted_value_empty_json_array_detection(self):
- """Empty JSON array '[]' is detected as corrupted."""
- value = "[]"
-
- is_corrupted = value == "[]"
-
- assert is_corrupted
-
- def test_corrupted_value_partial_json_detection(self):
- """Partial JSON is detected as corrupted."""
- value = '{"incomplete'
-
- try:
- import json
-
- json.loads(value)
- is_corrupted = False
- except json.JSONDecodeError:
- is_corrupted = True
-
- assert is_corrupted
-
- def test_corrupted_value_default_assignment(self):
- """Corrupted value is replaced with default."""
- value = "[object Object]"
- default = "default_value"
-
- corrupted_markers = ["[object Object]", "{}", "[]"]
- if value in corrupted_markers:
- value = default
-
- assert value == "default_value"
-
- def test_corrupted_value_logging(self):
- """Corrupted values are logged."""
- logged = []
-
- def log_corrupted(key, value):
- logged.append((key, value))
-
- # Simulate detection
- log_corrupted("setting.key", "[object Object]")
-
- assert len(logged) == 1
-
- def test_corrupted_value_partial_corruption_handling(self):
- """Batch with partial corruption is handled."""
- settings = {
- "good_setting": "valid",
- "bad_setting": "[object Object]",
- "another_good": 123,
- }
-
- defaults = {
- "good_setting": "default1",
- "bad_setting": "default2",
- "another_good": 0,
- }
-
- corrupted_markers = ["[object Object]", "{}", "[]"]
- for key, value in settings.items():
- if value in corrupted_markers:
- settings[key] = defaults[key]
-
- assert settings["good_setting"] == "valid"
- assert settings["bad_setting"] == "default2"
- assert settings["another_good"] == 123
-
- def test_corrupted_value_unicode_corruption(self):
- """Unicode corruption is detected."""
- value = "\x00\x00\x00" # Null bytes
-
- # Check for invalid characters
- is_corrupted = "\x00" in value
-
- assert is_corrupted
-
-
-class TestSettingsValidation:
- """Tests for settings validation."""
-
- def test_validate_boolean_setting(self):
- """Boolean settings are validated."""
- valid_booleans = [True, False, "true", "false", "1", "0"]
-
- for value in valid_booleans:
- if isinstance(value, bool):
- is_valid = True
- elif isinstance(value, str):
- is_valid = value.lower() in ["true", "false", "1", "0"]
- else:
- is_valid = False
-
- assert is_valid, f"Failed for {value}"
-
- def test_validate_number_setting(self):
- """Number settings are validated."""
- valid_numbers = [0, 1, 100, 3.14, "42", "3.14"]
-
- for value in valid_numbers:
- try:
- float(value)
- is_valid = True
- except (ValueError, TypeError):
- is_valid = False
-
- assert is_valid, f"Failed for {value}"
-
- def test_validate_select_setting(self):
- """Select settings are validated against options."""
- options = ["option1", "option2", "option3"]
- value = "option2"
-
- is_valid = value in options
-
- assert is_valid
-
- def test_validate_text_setting(self):
- """Text settings accept strings."""
- value = "any text value"
-
- is_valid = isinstance(value, str)
-
- assert is_valid
diff --git a/tests/web/routes/test_urlparse_exception_handling.py b/tests/web/routes/test_urlparse_exception_handling.py
deleted file mode 100644
index ee038e01e8..0000000000
--- a/tests/web/routes/test_urlparse_exception_handling.py
+++ /dev/null
@@ -1,124 +0,0 @@
-"""Tests for urlparse ValueError handling in web routes (PR #2013).
-
-PR #2013 changed bare `except:` to `except ValueError:` in several routes
-where `urlparse` is used. These tests verify that:
-1. Valid URLs are parsed correctly
-2. Malformed URLs that trigger ValueError are handled gracefully
-3. The specific ValueError exception is caught (not a generic catch-all)
-"""
-
-from urllib.parse import urlparse
-
-
-class TestUrlparseValueErrorHandling:
- """Test that urlparse ValueError is properly handled.
-
- Python's urlparse can raise ValueError for certain malformed URLs.
- The code in metrics_routes and library_routes now catches ValueError
- specifically instead of bare except.
- """
-
- def test_urlparse_handles_normal_url(self):
- """urlparse should parse normal URLs without error."""
- parsed = urlparse("https://example.com/path")
- assert parsed.netloc == "example.com"
- assert parsed.scheme == "https"
-
- def test_urlparse_handles_empty_string(self):
- """urlparse should handle empty string without ValueError."""
- parsed = urlparse("")
- assert parsed.netloc == ""
-
- def test_urlparse_handles_url_without_scheme(self):
- """urlparse should handle URL without scheme."""
- parsed = urlparse("example.com/path")
- # Without scheme, the whole thing goes to path
- assert parsed.netloc == ""
-
- def test_classification_progress_domain_extraction(self):
- """Test domain extraction logic from api_classification_progress.
-
- This mirrors the logic in metrics_routes.py line ~2168-2177
- that was changed from bare except to except ValueError.
- """
- test_urls = [
- ("https://example.com/path", "example.com"),
- ("https://www.arxiv.org/abs/1234", "arxiv.org"),
- ("https://sub.domain.co.uk/path", "sub.domain.co.uk"),
- ("", ""),
- ("not-a-url", ""),
- ]
-
- for url, expected_domain in test_urls:
- try:
- parsed = urlparse(url)
- domain = parsed.netloc.lower()
- if domain.startswith("www."):
- domain = domain[4:]
- except ValueError:
- # This is the specific exception caught in the PR
- domain = ""
-
- if expected_domain:
- assert domain == expected_domain, (
- f"URL {url!r}: expected {expected_domain!r}, got {domain!r}"
- )
-
- def test_library_routes_hostname_extraction(self):
- """Test hostname extraction logic from library_routes.py.
-
- This mirrors the logic in library_routes.py line ~1247-1253
- that was changed from bare except to except ValueError.
- """
- test_urls = [
- ("https://example.com/path", "example.com"),
- ("https://arxiv.org/abs/2024.1234", "arxiv.org"),
- ("", None),
- ]
-
- for url, expected_hostname in test_urls:
- domain = ""
- if url:
- try:
- domain = urlparse(url).hostname or ""
- except ValueError:
- # urlparse can raise ValueError for malformed URLs
- pass
-
- if expected_hostname is not None:
- assert domain == expected_hostname, (
- f"URL {url!r}: expected {expected_hostname!r}, got {domain!r}"
- )
-
- def test_metrics_routes_domain_classification_error_handling(self):
- """Test that domain classification errors are caught as Exception.
-
- In metrics_routes.py line ~1108, the bare except was changed to
- `except Exception as e:` with debug logging for classification errors.
- """
- # This verifies the pattern: catch Exception from domain classification
- # and log it at debug level rather than silently swallowing
-
- class MockClassifier:
- def get_classification(self, domain):
- raise RuntimeError("Classification service unavailable")
-
- classifier = MockClassifier()
- category_counts = {}
-
- domain = "example.com"
- try:
- classification = classifier.get_classification(domain)
- if classification:
- category = classification.category
- category_counts[category] = category_counts.get(category, 0) + 1
- else:
- category_counts["Unclassified"] = (
- category_counts.get("Unclassified", 0) + 1
- )
- except Exception:
- # This is the pattern from PR #2013 - catch Exception instead of bare except
- pass
-
- # Should not crash, category_counts should be unchanged
- assert category_counts == {}