File size: 4,568 Bytes
33d3592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# final_test.py - Final comprehensive test

import os
import sys
from dotenv import load_dotenv

def test_environment():
    """Test environment configuration"""
    print("πŸ” Testing Environment Configuration...")
    
    load_dotenv(override=True)
    
    required_vars = {
        'OPENROUTER_API_KEY': os.getenv('OPENROUTER_API_KEY'),
        'OPENROUTER_MODEL': os.getenv('OPENROUTER_MODEL'),
        'GROQ_API_KEY': os.getenv('GROQ_API_KEY'),
        'GEMINI_API_KEY': os.getenv('GEMINI_API_KEY')
    }
    
    all_good = True
    for var, value in required_vars.items():
        if value:
            if 'KEY' in var:
                print(f"βœ… {var}: {value[:15]}...{value[-8:]}")
            else:
                print(f"βœ… {var}: {value}")
        else:
            print(f"❌ {var}: Missing")
            all_good = False
    
    return all_good

def test_translator_fresh():
    """Test translator with fresh import"""
    print("\nπŸ” Testing Translator (Fresh Import)...")
    
    try:
        # Clear module cache
        if 'translator' in sys.modules:
            del sys.modules['translator']
        
        # Fresh import
        from translator import get_translator
        
        translator = get_translator()
        print(f"πŸ“‹ OpenRouter Model: {translator.openrouter_model}")
        
        # Test OpenRouter
        result, error = translator._openrouter_complete("Say 'Hello' in Arabic")
        
        if result:
            print(f"βœ… OpenRouter working: {result[:50]}...")
            return True
        else:
            print(f"❌ OpenRouter failed: {error}")
            return False
            
    except Exception as e:
        print(f"❌ Translator test failed: {str(e)}")
        return False

def test_ai_questions_fresh():
    """Test AI questions with fresh import"""
    print("\nπŸ” Testing AI Questions (Fresh Import)...")
    
    try:
        # Clear module cache
        if 'ai_questions' in sys.modules:
            del sys.modules['ai_questions']
        
        # Fresh import
        from ai_questions import get_ai_question_engine
        
        engine = get_ai_question_engine()
        
        # Test question processing
        response, error, session_id, model_used = engine.process_question(
            selected_text="This is a test text about artificial intelligence.",
            question="What is this text about?",
            segment_info={"id": "test"},
            ui_language='en',
            preferred_model='auto'
        )
        
        if response:
            print(f"βœ… AI Questions working: {response[:50]}...")
            print(f"πŸ”§ Model used: {model_used}")
            return True
        else:
            print(f"❌ AI Questions failed: {error}")
            return False
            
    except Exception as e:
        print(f"❌ AI Questions test failed: {str(e)}")
        return False

def test_app_imports():
    """Test app.py imports"""
    print("\nπŸ” Testing App Imports...")
    
    try:
        # Test critical imports
        from translator import get_translator
        from ai_questions import get_ai_question_engine
        from exporter import BroadcastExporter
        from style_fixes import apply_custom_styling
        
        print("βœ… All critical imports successful")
        return True
        
    except Exception as e:
        print(f"❌ Import test failed: {str(e)}")
        return False

def main():
    """Run all final tests"""
    
    print("πŸš€ Final Comprehensive Test")
    print("=" * 60)
    
    # Test environment
    env_ok = test_environment()
    
    # Test translator
    translator_ok = test_translator_fresh()
    
    # Test AI questions
    ai_questions_ok = test_ai_questions_fresh()
    
    # Test app imports
    imports_ok = test_app_imports()
    
    print("\n" + "=" * 60)
    print("πŸ“Š Final Test Results:")
    print(f"  Environment: {'βœ… PASS' if env_ok else '❌ FAIL'}")
    print(f"  Translator: {'βœ… PASS' if translator_ok else '❌ FAIL'}")
    print(f"  AI Questions: {'βœ… PASS' if ai_questions_ok else '❌ FAIL'}")
    print(f"  App Imports: {'βœ… PASS' if imports_ok else '❌ FAIL'}")
    
    if all([env_ok, translator_ok, ai_questions_ok, imports_ok]):
        print("\nπŸŽ‰ ALL TESTS PASSED!")
        print("πŸ’‘ Your app should work perfectly now!")
        print("πŸš€ Run: streamlit run app.py")
        return True
    else:
        print("\n⚠️ Some tests failed. Check the logs above.")
        return False

if __name__ == "__main__":
    main()