File size: 8,290 Bytes
ea46ec8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
"""
Comprehensive test suite for Web3 Research Co-Pilot
"""

import sys
import asyncio
import time
from datetime import datetime

def test_imports():
    """Test all critical imports"""
    print("πŸ§ͺ Testing imports...")
    
    try:
        # Core imports
        from src.visualizations import CryptoVisualizations, create_price_chart
        from src.agent.research_agent import Web3ResearchAgent
        from src.utils.config import config
        from src.tools.coingecko_tool import CoinGeckoTool
        from src.tools.defillama_tool import DeFiLlamaTool
        from src.tools.etherscan_tool import EtherscanTool
        from src.api.airaa_integration import AIRAAIntegration
        
        # FastAPI app
        from app import app, service, Web3CoPilotService
        
        print("βœ… All imports successful")
        return True
        
    except Exception as e:
        print(f"❌ Import failed: {e}")
        return False

def test_configuration():
    """Test configuration setup"""
    print("πŸ§ͺ Testing configuration...")
    
    try:
        from src.utils.config import config
        
        print(f"   β€’ GEMINI_API_KEY: {'βœ… Set' if config.GEMINI_API_KEY else '❌ Not set'}")
        print(f"   β€’ COINGECKO_API_KEY: {'βœ… Set' if config.COINGECKO_API_KEY else '⚠️ Not set'}")
        print(f"   β€’ ETHERSCAN_API_KEY: {'βœ… Set' if config.ETHERSCAN_API_KEY else '⚠️ Not set'}")
        
        return True
        
    except Exception as e:
        print(f"❌ Configuration test failed: {e}")
        return False

def test_visualizations():
    """Test visualization creation"""
    print("πŸ§ͺ Testing visualizations...")
    
    try:
        from src.visualizations import CryptoVisualizations
        
        # Test empty chart
        fig1 = CryptoVisualizations._create_empty_chart("Test message")
        print("   βœ… Empty chart creation")
        
        # Test price chart with sample data
        sample_data = {
            'prices': [
                [1672531200000, 16500.50], 
                [1672617600000, 16750.25],
                [1672704000000, 17100.00]
            ],
            'total_volumes': [
                [1672531200000, 1000000], 
                [1672617600000, 1200000],
                [1672704000000, 1100000]
            ]
        }
        
        fig2 = CryptoVisualizations.create_price_chart(sample_data, 'BTC')
        print("   βœ… Price chart with data")
        
        # Test market overview
        market_data = [
            {'name': 'Bitcoin', 'market_cap': 500000000000, 'price_change_percentage_24h': 2.5},
            {'name': 'Ethereum', 'market_cap': 200000000000, 'price_change_percentage_24h': -1.2}
        ]
        
        fig3 = CryptoVisualizations.create_market_overview(market_data)
        print("   βœ… Market overview chart")
        
        return True
        
    except Exception as e:
        print(f"❌ Visualization test failed: {e}")
        return False

def test_tools():
    """Test individual tools"""
    print("πŸ§ͺ Testing tools...")
    
    try:
        from src.tools.coingecko_tool import CoinGeckoTool
        from src.tools.defillama_tool import DeFiLlamaTool
        from src.tools.etherscan_tool import EtherscanTool
        
        # Test tool initialization
        coingecko = CoinGeckoTool()
        print("   βœ… CoinGecko tool initialization")
        
        defillama = DeFiLlamaTool()
        print("   βœ… DeFiLlama tool initialization")
        
        etherscan = EtherscanTool()
        print("   βœ… Etherscan tool initialization")
        
        return True
        
    except Exception as e:
        print(f"❌ Tools test failed: {e}")
        return False

async def test_service():
    """Test service functionality"""
    print("πŸ§ͺ Testing service...")
    
    try:
        from app import service
        
        print(f"   β€’ Service enabled: {'βœ…' if service.enabled else '❌'}")
        print(f"   β€’ Agent available: {'βœ…' if service.agent else '❌'}")
        print(f"   β€’ AIRAA enabled: {'βœ…' if service.airaa and service.airaa.enabled else '❌'}")
        
        # Test a simple query
        if service.enabled:
            print("   πŸ”„ Testing query processing...")
            response = await service.process_query("What is Bitcoin?")
            
            if response.success:
                print("   βœ… Query processing successful")
                print(f"      Response length: {len(response.response)} characters")
            else:
                print(f"   ⚠️ Query failed: {response.error}")
        else:
            print("   ⚠️ Service disabled - limited testing")
            
        return True
        
    except Exception as e:
        print(f"❌ Service test failed: {e}")
        return False

def test_app_health():
    """Test FastAPI app health"""
    print("πŸ§ͺ Testing FastAPI app...")
    
    try:
        from fastapi.testclient import TestClient
        from app import app
        
        with TestClient(app) as client:
            # Test health endpoint
            response = client.get("/health")
            if response.status_code == 200:
                print("   βœ… Health endpoint")
            else:
                print(f"   ❌ Health endpoint failed: {response.status_code}")
                
            # Test status endpoint  
            response = client.get("/status")
            if response.status_code == 200:
                print("   βœ… Status endpoint")
                status_data = response.json()
                print(f"      Version: {status_data.get('version', 'Unknown')}")
            else:
                print(f"   ❌ Status endpoint failed: {response.status_code}")
                
            # Test homepage
            response = client.get("/")
            if response.status_code == 200:
                print("   βœ… Homepage endpoint")
            else:
                print(f"   ❌ Homepage failed: {response.status_code}")
            
        return True
        
    except Exception as e:
        print(f"❌ FastAPI test failed: {e}")
        return False

def run_performance_test():
    """Simple performance test"""
    print("πŸ§ͺ Performance test...")
    
    try:
        from src.visualizations import CryptoVisualizations
        
        # Time visualization creation
        start_time = time.time()
        
        for i in range(10):
            sample_data = {
                'prices': [[1672531200000 + i*3600000, 16500 + i*10] for i in range(100)],
                'total_volumes': [[1672531200000 + i*3600000, 1000000 + i*1000] for i in range(100)]
            }
            fig = CryptoVisualizations.create_price_chart(sample_data, 'TEST')
        
        end_time = time.time()
        avg_time = (end_time - start_time) / 10
        
        print(f"   ⏱️ Average chart creation: {avg_time:.3f}s")
        
        if avg_time < 1.0:
            print("   βœ… Performance acceptable")
            return True
        else:
            print("   ⚠️ Performance slow")
            return True
            
    except Exception as e:
        print(f"❌ Performance test failed: {e}")
        return False

async def main():
    """Run all tests"""
    print("=" * 50)
    print("πŸš€ Web3 Research Co-Pilot - Test Suite")
    print("=" * 50)
    print()
    
    test_results = []
    
    # Run all tests
    test_results.append(test_imports())
    test_results.append(test_configuration())
    test_results.append(test_visualizations())
    test_results.append(test_tools())
    test_results.append(await test_service())
    test_results.append(test_app_health())
    test_results.append(run_performance_test())
    
    print()
    print("=" * 50)
    print("πŸ“Š Test Results Summary")
    print("=" * 50)
    
    passed = sum(test_results)
    total = len(test_results)
    
    print(f"Tests passed: {passed}/{total}")
    print(f"Success rate: {(passed/total)*100:.1f}%")
    
    if passed == total:
        print("πŸŽ‰ All tests passed!")
        return 0
    else:
        print("⚠️ Some tests failed")
        return 1

if __name__ == "__main__":
    exit_code = asyncio.run(main())
    sys.exit(exit_code)