#!/usr/bin/env python3 """ Debug test script for Smart Auto-Complete Tests context integration and other functionality """ import sys import os # Add current directory to Python path script_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, script_dir) def test_context_integration(): """Test that user context is properly integrated""" print("๐Ÿงช Testing Context Integration...") try: from src.autocomplete import SmartAutoComplete from config.settings import AppSettings # Create mock settings class MockSettings: def __init__(self): self.OPENAI_API_KEY = "test-key" self.ANTHROPIC_API_KEY = "" self.DEFAULT_PROVIDER = "openai" self.CACHE_TTL = 3600 self.CACHE_MAX_SIZE = 100 # Create mock API client that returns the prompt for inspection class MockAPIClient: def __init__(self, settings=None): self.last_messages = None def get_completion(self, messages, temperature=0.7, max_tokens=150, provider=None): self.last_messages = messages return "Mock completion response" # Create mock cache class MockCacheManager: def __init__(self, settings=None): pass def get(self, key): return None def set(self, key, value): pass # Test setup settings = MockSettings() autocomplete = SmartAutoComplete(settings) autocomplete.api_client = MockAPIClient(settings) autocomplete.cache_manager = MockCacheManager(settings) # Test without context print("๐Ÿ“ Testing without user context...") suggestions = autocomplete.get_suggestions( text="Dear Mr. Johnson,", context="email", max_tokens=150, user_context="" ) messages_without_context = autocomplete.api_client.last_messages print(f"โœ… System prompt (no context): {messages_without_context[0]['content'][:100]}...") print(f"โœ… User message (no context): {messages_without_context[1]['content']}") # Test with context print("\n๐Ÿ“ Testing with user context...") user_context = "Meeting scheduled for next Tuesday to discuss quarterly budget review" suggestions = autocomplete.get_suggestions( text="Dear Mr. Johnson,", context="email", max_tokens=150, user_context=user_context ) messages_with_context = autocomplete.api_client.last_messages print(f"โœ… System prompt (with context): {messages_with_context[0]['content'][:150]}...") print(f"โœ… User message (with context): {messages_with_context[1]['content']}") # Verify context is included system_prompt = messages_with_context[0]['content'] user_message = messages_with_context[1]['content'] context_in_system = user_context in system_prompt context_in_user = user_context in user_message print(f"\n๐Ÿ” Context Analysis:") print(f" Context in system prompt: {context_in_system}") print(f" Context in user message: {context_in_user}") print(f" Context properly integrated: {context_in_system or context_in_user}") if context_in_system or context_in_user: print("โœ… Context integration working correctly!") return True else: print("โŒ Context integration failed!") return False except Exception as e: print(f"โŒ Context integration test failed: {str(e)}") import traceback traceback.print_exc() return False def test_copy_html_generation(): """Test HTML generation for copy functionality""" print("\n๐Ÿงช Testing Copy HTML Generation...") try: # Mock suggestion suggestions = ["This is a test suggestion that should be copyable."] # Generate HTML (simplified version of the app logic) html_suggestions = "
" for i, suggestion in enumerate(suggestions, 1): suggestion_id = f"suggestion-{i}" html_suggestions += f"""
๐Ÿ’ก Suggestion {i}:
{suggestion}
""" html_suggestions += "
" print("โœ… HTML generation successful") print(f"๐Ÿ“„ Generated HTML length: {len(html_suggestions)} characters") # Check for key elements has_suggestion_id = "suggestion-1" in html_suggestions has_onclick = "onclick=" in html_suggestions has_clipboard_api = "navigator.clipboard" in html_suggestions print(f"๐Ÿ” HTML Analysis:") print(f" Has suggestion ID: {has_suggestion_id}") print(f" Has onclick handler: {has_onclick}") print(f" Uses clipboard API: {has_clipboard_api}") if has_suggestion_id and has_onclick and has_clipboard_api: print("โœ… Copy HTML generation working correctly!") return True else: print("โŒ Copy HTML generation has issues!") return False except Exception as e: print(f"โŒ Copy HTML test failed: {str(e)}") return False def main(): """Main test function""" print("๐Ÿš€ Smart Auto-Complete Debug Tests") print("=" * 50) tests = [ ("Context Integration", test_context_integration), ("Copy HTML Generation", test_copy_html_generation), ] passed = 0 total = len(tests) for test_name, test_func in tests: print(f"\n๐Ÿ“‹ Running: {test_name}") if test_func(): passed += 1 print("-" * 30) print(f"\n{'='*50}") print(f"Debug Test Results: {passed}/{total} tests passed") if passed == total: print("๐ŸŽ‰ All debug tests passed!") print("\n๐Ÿ’ก If issues persist:") print("1. Check browser console for JavaScript errors") print("2. Ensure you're using HTTPS or localhost") print("3. Test the copy functionality with test_copy.html") print("4. Check that API keys are properly configured") else: print("โŒ Some debug tests failed.") print("Please check the error messages above.") return 0 if passed == total else 1 if __name__ == "__main__": sys.exit(main())