File size: 8,377 Bytes
b309c22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
#!/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 = "<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")
# 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())
|