File size: 3,075 Bytes
0a378f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)