Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Regression testing for search optimizer refactoring. | |
| This script validates that the refactored functions behave exactly the same | |
| as they did before the refactoring, ensuring no behavioral changes. | |
| """ | |
| import sys | |
| from typing import List, Dict, Any, Optional | |
| def test_search_decision_consistency(): | |
| """Test that search decisions are consistent and logical""" | |
| print("π Regression Testing: Search Decision Consistency") | |
| print("=" * 60) | |
| try: | |
| from search_optimizer import should_perform_search | |
| # Test cases with expected behaviors that should remain consistent | |
| test_cases = [ | |
| { | |
| "name": "Greeting detection", | |
| "prompt": "Hello there!", | |
| "history": None, | |
| "expected_decision": False, | |
| "expected_reason_contains": "greeting" | |
| }, | |
| { | |
| "name": "New information request", | |
| "prompt": "What is the latest news about artificial intelligence?", | |
| "history": None, | |
| "expected_decision": True, | |
| "expected_reason_contains": ["information", "history", "No conversation"] | |
| }, | |
| { | |
| "name": "Follow-up elaboration", | |
| "prompt": "Tell me more about that", | |
| "history": [{"user": "What is AI?", "assistant": "AI is artificial intelligence used to create smart systems..."}], | |
| "expected_decision": False, | |
| "expected_reason_contains": "Follow-up" | |
| }, | |
| { | |
| "name": "Referential question", | |
| "prompt": "Can you explain that concept better?", | |
| "history": [{"user": "What is ML?", "assistant": "Machine learning is a subset of AI that enables systems to learn..."}], | |
| "expected_decision": False, | |
| "expected_reason_contains": "question" | |
| }, | |
| { | |
| "name": "Continuation request", | |
| "prompt": "What else should I know?", | |
| "history": [{"user": "Basics of AI?", "assistant": "AI involves creating intelligent systems..."}], | |
| "expected_decision": True, | |
| "expected_reason_contains": ["topic", "patterns", "insufficient"] | |
| }, | |
| { | |
| "name": "Fresh topic change", | |
| "prompt": "How does quantum computing work?", | |
| "history": [{"user": "What is AI?", "assistant": "AI is artificial intelligence..."}], | |
| "expected_decision": True, | |
| "expected_reason_contains": ["information", "topic"] | |
| } | |
| ] | |
| passed_tests = 0 | |
| total_tests = len(test_cases) | |
| for i, case in enumerate(test_cases, 1): | |
| print(f"\nπ Test {i}: {case['name']}") | |
| result = should_perform_search( | |
| case["prompt"], | |
| case["history"] | |
| ) | |
| # Check decision consistency | |
| decision_correct = result["should_search"] == case["expected_decision"] | |
| # Check reason consistency | |
| reason_correct = False | |
| expected_reasons = case["expected_reason_contains"] | |
| if isinstance(expected_reasons, str): | |
| expected_reasons = [expected_reasons] | |
| for expected_reason in expected_reasons: | |
| if expected_reason.lower() in result["reason"].lower(): | |
| reason_correct = True | |
| break | |
| print(f" π Decision: {result['should_search']} (expected: {case['expected_decision']})") | |
| print(f" π Reason: {result['reason']}") | |
| print(f" π Confidence: {result['confidence']:.2f}") | |
| if decision_correct and reason_correct: | |
| print(" β PASS - Behavior consistent") | |
| passed_tests += 1 | |
| else: | |
| print(" β FAIL - Behavior inconsistent") | |
| if not decision_correct: | |
| print(" πΈ Decision mismatch") | |
| if not reason_correct: | |
| print(" πΈ Reason doesn't match expected pattern") | |
| print(f"\nπ Search Decision Tests: {passed_tests}/{total_tests} passed") | |
| return passed_tests == total_tests | |
| except Exception as e: | |
| print(f"β Search decision regression test error: {e}") | |
| return False | |
| def test_conversation_history_consistency(): | |
| """Test conversation history analysis consistency""" | |
| print("\nπ Regression Testing: Conversation History Analysis") | |
| print("=" * 60) | |
| try: | |
| from search_optimizer import has_meaningful_conversation_history | |
| # Test cases with expected behaviors | |
| test_cases = [ | |
| { | |
| "name": "None history", | |
| "history": None, | |
| "expected": False | |
| }, | |
| { | |
| "name": "Empty history", | |
| "history": [], | |
| "expected": False | |
| }, | |
| { | |
| "name": "Too short entries (role format)", | |
| "history": [{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hi"}], | |
| "expected": False | |
| }, | |
| { | |
| "name": "Too short entries (user/assistant format)", | |
| "history": [{"user": "Hi", "assistant": "Hi"}], | |
| "expected": False | |
| }, | |
| { | |
| "name": "Meaningful conversation (role format)", | |
| "history": [{"role": "user", "content": "What is machine learning?"}, {"role": "assistant", "content": "Machine learning is a subset of artificial intelligence..."}], | |
| "expected": True | |
| }, | |
| { | |
| "name": "Meaningful conversation (user/assistant format)", | |
| "history": [{"user": "Explain neural networks", "assistant": "Neural networks are computing systems inspired by biological neural networks..."}], | |
| "expected": True | |
| }, | |
| { | |
| "name": "Mixed meaningful and short entries", | |
| "history": [ | |
| {"user": "Hi", "assistant": "Hello"}, | |
| {"user": "What is deep learning?", "assistant": "Deep learning is a subset of machine learning that uses neural networks with multiple layers..."} | |
| ], | |
| "expected": True | |
| }, | |
| ] | |
| passed_tests = 0 | |
| total_tests = len(test_cases) | |
| for i, case in enumerate(test_cases, 1): | |
| print(f"\nπ Test {i}: {case['name']}") | |
| result = has_meaningful_conversation_history(case["history"]) | |
| print(f" π Result: {result} (expected: {case['expected']})") | |
| if result == case["expected"]: | |
| print(" β PASS - Behavior consistent") | |
| passed_tests += 1 | |
| else: | |
| print(" β FAIL - Behavior inconsistent") | |
| print(f"\nπ History Analysis Tests: {passed_tests}/{total_tests} passed") | |
| return passed_tests == total_tests | |
| except Exception as e: | |
| print(f"β History analysis regression test error: {e}") | |
| return False | |
| def test_utility_functions_consistency(): | |
| """Test utility functions consistency""" | |
| print("\nπ οΈ Regression Testing: Utility Functions") | |
| print("=" * 60) | |
| try: | |
| from search_optimizer import format_search_context | |
| # Test format_search_context with various inputs | |
| test_cases = [ | |
| { | |
| "name": "Empty results list", | |
| "results": [], | |
| "expected_empty": True | |
| }, | |
| { | |
| "name": "Single result", | |
| "results": [{"source": "Brave", "title": "Test Title", "body": "Test body content"}], | |
| "expected_contains": ["[Brave]", "Test Title", "Test body content"] | |
| }, | |
| { | |
| "name": "Multiple results", | |
| "results": [ | |
| {"source": "Brave", "title": "Title 1", "body": "Body 1"}, | |
| {"source": "DuckDuckGo", "title": "Title 2", "body": "Body 2"} | |
| ], | |
| "expected_contains": ["[Brave]", "[DuckDuckGo]", "Title 1", "Title 2"] | |
| }, | |
| { | |
| "name": "Results with missing fields", | |
| "results": [{"title": "Only Title"}, {"source": "Only Source"}], | |
| "expected_contains": ["Only Title", "Only Source"] | |
| }, | |
| { | |
| "name": "Very long body content (truncation test)", | |
| "results": [{"source": "Test", "title": "Long Content", "body": "x" * 2000}], | |
| "expected_contains": ["[Test]", "Long Content"], | |
| "expected_truncated": True | |
| } | |
| ] | |
| passed_tests = 0 | |
| total_tests = len(test_cases) | |
| for i, case in enumerate(test_cases, 1): | |
| print(f"\nπ Test {i}: {case['name']}") | |
| result = format_search_context(case["results"]) | |
| # Check if result is empty as expected | |
| if case.get("expected_empty", False): | |
| if not result: | |
| print(" β PASS - Empty result as expected") | |
| passed_tests += 1 | |
| else: | |
| print(" β FAIL - Expected empty result") | |
| continue | |
| # Check expected content | |
| contains_all = True | |
| for expected_content in case.get("expected_contains", []): | |
| if expected_content not in result: | |
| contains_all = False | |
| print(f" πΈ Missing expected content: {expected_content}") | |
| # Check truncation if expected | |
| if case.get("expected_truncated", False): | |
| if len(result) < 2000: # Should be truncated from original 2000+ chars | |
| print(" π Content appropriately truncated") | |
| else: | |
| print(" β οΈ Content may not be truncated as expected") | |
| if contains_all: | |
| print(" β PASS - Content formatting consistent") | |
| passed_tests += 1 | |
| else: | |
| print(" β FAIL - Content formatting issue") | |
| print(f"\nπ Utility Function Tests: {passed_tests}/{total_tests} passed") | |
| return passed_tests == total_tests | |
| except Exception as e: | |
| print(f"β Utility function regression test error: {e}") | |
| return False | |
| def test_error_handling_consistency(): | |
| """Test that error handling behaves consistently""" | |
| print("\nπ‘οΈ Regression Testing: Error Handling") | |
| print("=" * 60) | |
| try: | |
| from search_optimizer import should_perform_search, has_meaningful_conversation_history, format_search_context | |
| print("π Testing graceful error handling...") | |
| # Test functions with malformed inputs | |
| error_tests_passed = 0 | |
| total_error_tests = 0 | |
| # Test should_perform_search with malformed history | |
| print("\n π Testing should_perform_search with malformed history") | |
| total_error_tests += 1 | |
| try: | |
| result = should_perform_search("test prompt", [{"malformed": "entry"}]) | |
| if isinstance(result, dict) and "should_search" in result: | |
| print(" β Handled malformed history gracefully") | |
| error_tests_passed += 1 | |
| else: | |
| print(" β Unexpected result format") | |
| except Exception as e: | |
| print(f" β Unexpected exception: {e}") | |
| # Test has_meaningful_conversation_history with malformed data | |
| print("\n π Testing has_meaningful_conversation_history with malformed data") | |
| total_error_tests += 1 | |
| try: | |
| result = has_meaningful_conversation_history([{"invalid": "format"}, "not_a_dict"]) | |
| if isinstance(result, bool): | |
| print(" β Handled malformed data gracefully") | |
| error_tests_passed += 1 | |
| else: | |
| print(" β Unexpected result type") | |
| except Exception as e: | |
| print(f" β Unexpected exception: {e}") | |
| # Test format_search_context with malformed results | |
| print("\n π Testing format_search_context with malformed results") | |
| total_error_tests += 1 | |
| try: | |
| result = format_search_context([{"missing_keys": True}, None, "not_a_dict"]) | |
| if isinstance(result, str): | |
| print(" β Handled malformed results gracefully") | |
| error_tests_passed += 1 | |
| else: | |
| print(" β Unexpected result type") | |
| except Exception as e: | |
| print(f" β Unexpected exception: {e}") | |
| print(f"\nπ Error Handling Tests: {error_tests_passed}/{total_error_tests} passed") | |
| return error_tests_passed == total_error_tests | |
| except Exception as e: | |
| print(f"β Error handling regression test error: {e}") | |
| return False | |
| def main(): | |
| """Run regression validation suite""" | |
| print("π Search Optimizer Regression Validation Suite") | |
| print("=" * 70) | |
| print("Validating behavioral consistency after refactoring...") | |
| print() | |
| tests_passed = 0 | |
| total_tests = 4 | |
| # Run regression tests | |
| if test_search_decision_consistency(): | |
| tests_passed += 1 | |
| if test_conversation_history_consistency(): | |
| tests_passed += 1 | |
| if test_utility_functions_consistency(): | |
| tests_passed += 1 | |
| if test_error_handling_consistency(): | |
| tests_passed += 1 | |
| # Summary | |
| print("\n" + "=" * 70) | |
| print("π REGRESSION VALIDATION SUMMARY") | |
| print("=" * 70) | |
| if tests_passed == total_tests: | |
| print(f"β ALL REGRESSION TESTS PASSED ({tests_passed}/{total_tests})") | |
| print("π Behavioral consistency maintained after refactoring!") | |
| print("π The refactored code behaves exactly as expected!") | |
| return True | |
| else: | |
| print(f"β SOME REGRESSION TESTS FAILED ({tests_passed}/{total_tests})") | |
| print("β οΈ Behavioral changes detected - review required") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) |