| |
| """ |
| Debug test script for Smart Auto-Complete |
| Tests context integration and other functionality |
| """ |
|
|
| import sys |
| import os |
|
|
| |
| 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 |
| |
| |
| 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 |
| |
| |
| 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" |
| |
| |
| class MockCacheManager: |
| def __init__(self, settings=None): |
| pass |
| def get(self, key): |
| return None |
| def set(self, key, value): |
| pass |
| |
| |
| settings = MockSettings() |
| autocomplete = SmartAutoComplete(settings) |
| autocomplete.api_client = MockAPIClient(settings) |
| autocomplete.cache_manager = MockCacheManager(settings) |
| |
| |
| 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']}") |
| |
| |
| 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']}") |
| |
| |
| 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: |
| |
| suggestions = ["This is a test suggestion that should be copyable."] |
| |
| |
| html_suggestions = "<div style='space-y: 10px;'>" |
| |
| for i, suggestion in enumerate(suggestions, 1): |
| suggestion_id = f"suggestion-{i}" |
| |
| html_suggestions += f""" |
| <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| border-radius: 10px; padding: 15px; margin: 10px 0; color: white;'> |
| <div style='margin-bottom: 10px;'> |
| <strong>π‘ Suggestion {i}:</strong> |
| </div> |
| <div id='{suggestion_id}' style='background: rgba(255,255,255,0.1); padding: 10px; border-radius: 5px; |
| margin: 10px 0; font-style: italic; line-height: 1.4; user-select: text;'> |
| {suggestion} |
| </div> |
| <div style='margin-top: 10px;'> |
| <button onclick=' |
| const text = document.getElementById("{suggestion_id}").innerText; |
| navigator.clipboard.writeText(text).then(() => {{ |
| this.innerHTML = "β
Copied!"; |
| this.style.backgroundColor = "#10b981"; |
| setTimeout(() => {{ |
| this.innerHTML = "π Copy to Clipboard"; |
| this.style.backgroundColor = "rgba(255,255,255,0.2)"; |
| }}, 2000); |
| }}).catch(() => {{ |
| alert("Failed to copy to clipboard"); |
| }}); |
| ' |
| style='background: rgba(255,255,255,0.2); border: none; color: white; |
| padding: 8px 16px; border-radius: 5px; cursor: pointer; |
| font-size: 14px; transition: all 0.2s;'> |
| π Copy to Clipboard |
| </button> |
| </div> |
| </div> |
| """ |
| html_suggestions += "</div>" |
| |
| print("β
HTML generation successful") |
| print(f"π Generated HTML length: {len(html_suggestions)} characters") |
| |
| |
| 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()) |
|
|