import sys import time from pathlib import Path # Add project root to path project_root = Path(__file__).parent sys.path.append(str(project_root)) def test_critical_fixes(): """Test the critical fixes for HF endpoint spamming and response issues""" print("=== Critical Fixes Test ===") print() # Test 1: Check HF endpoint monitor rate limiting print("1. Testing HF Endpoint Monitor Rate Limiting:") try: from services.hf_endpoint_monitor import hf_monitor # Check initial configuration print(f" Check interval: {hf_monitor.check_interval} seconds") if hf_monitor.check_interval >= 300: print(" ✅ Rate limiting properly configured (5+ minutes between checks)") else: print(" ❌ Rate limiting not properly configured") # Test rate limiting behavior first_check = hf_monitor.check_endpoint_status() time.sleep(1) # Very short delay second_check = hf_monitor.check_endpoint_status() # Second check should return cached results quickly if first_check['timestamp'] == second_check['timestamp']: print(" ✅ Rate limiting working - cached results returned") else: print(" ⚠️ Rate limiting may not be working properly") except Exception as e: print(f" ❌ Error testing HF monitor: {e}") print() # Test 2: Check app.py structure print("2. Testing App.py Structure:") try: with open('app.py', 'r') as f: content = f.read() # Check for key fixes required_fixes = [ 'st.chat_input', 'is_processing', 'response_placeholder', 'status_placeholder', 'user_input and not st.session_state.is_processing' ] missing_fixes = [] for fix in required_fixes: if fix not in content: missing_fixes.append(fix) if missing_fixes: print(f" ❌ Missing fixes: {missing_fixes}") else: print(" ✅ All critical fixes present in app.py") except Exception as e: print(f" ❌ Error reading app.py: {e}") print() # Test 3: Check for proper error handling print("3. Testing Error Handling:") try: with open('app.py', 'r') as f: content = f.read() error_handling_features = [ 'except Exception as ollama_error', 'except Exception as hf_error', 'System error', 'Sorry, I couldn\'t process your request' ] missing_features = [] for feature in error_handling_features: if feature not in content: missing_features.append(feature) if missing_features: print(f" ❌ Missing error handling: {missing_features}") else: print(" ✅ Proper error handling implemented") except Exception as e: print(f" ❌ Error checking error handling: {e}") print() print("🎉 Critical Fixes Test Completed!") print() print("🔧 SUMMARY OF FIXES APPLIED:") print("1. ✅ HF Endpoint Spamming Fixed - Rate limiting to 5+ minutes") print("2. ✅ Response Delivery Fixed - Using st.chat_input() properly") print("3. ✅ Error Handling Improved - Better user feedback") print("4. ✅ Processing State Management - Proper is_processing flags") print("5. ✅ UI Responsiveness - Immediate message display") if __name__ == "__main__": test_critical_fixes()