Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Test script for preview functionality | |
""" | |
import os | |
import sys | |
import tempfile | |
# Add current directory to path for imports | |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
def test_preview_chat_response(): | |
"""Test the preview chat response function""" | |
try: | |
from app import preview_chat_response | |
# Mock config data | |
config_data = { | |
'name': 'Test Assistant', | |
'model': 'google/gemini-2.0-flash-001', | |
'system_prompt': 'You are a helpful assistant.', | |
'temperature': 0.7, | |
'max_tokens': 500, | |
'enable_dynamic_urls': False, | |
'enable_vector_rag': False | |
} | |
# Test with no API key (should give preview message) | |
if 'OPENROUTER_API_KEY' in os.environ: | |
del os.environ['OPENROUTER_API_KEY'] | |
message = "Hello, how are you?" | |
history = [] | |
result_msg, result_history = preview_chat_response( | |
message, history, config_data, "", "", "", "" | |
) | |
print("β preview_chat_response function works") | |
print(f"Result message: {result_msg}") | |
print(f"History length: {len(result_history)}") | |
print(f"Last response: {result_history[-1] if result_history else 'None'}") | |
return True | |
except Exception as e: | |
print(f"β preview_chat_response failed: {e}") | |
return False | |
def test_url_extraction(): | |
"""Test URL extraction function""" | |
try: | |
from app import extract_urls_from_text | |
test_text = "Check out https://example.com and also https://test.org/page" | |
urls = extract_urls_from_text(test_text) | |
print("β extract_urls_from_text works") | |
print(f"Extracted URLs: {urls}") | |
return True | |
except Exception as e: | |
print(f"β extract_urls_from_text failed: {e}") | |
return False | |
def test_url_fetching(): | |
"""Test URL content fetching""" | |
try: | |
from app import fetch_url_content | |
# Test with a simple URL | |
content = fetch_url_content("https://httpbin.org/get") | |
print("β fetch_url_content works") | |
print(f"Content length: {len(content)}") | |
print(f"Content preview: {content[:100]}...") | |
return True | |
except Exception as e: | |
print(f"β fetch_url_content failed: {e}") | |
return False | |
if __name__ == "__main__": | |
print("Testing preview functionality components...") | |
tests = [ | |
test_url_extraction, | |
test_url_fetching, | |
test_preview_chat_response | |
] | |
passed = 0 | |
total = len(tests) | |
for test in tests: | |
if test(): | |
passed += 1 | |
print() | |
print(f"Test Results: {passed}/{total} passed") | |
if passed == total: | |
print("β All preview functionality tests passed!") | |
sys.exit(0) | |
else: | |
print("β Some tests failed") | |
sys.exit(1) |