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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env python3
"""
Comprehensive API Integration Test Suite

Tests all API endpoints with full dependency verification.
Run this after installing all dependencies:
    pip install -r requirements.txt
    
Usage:
    python scripts/test_api_integration.py [--base-url http://localhost:5000]
"""

import os
import sys
import time
import json
import logging
from typing import Dict, Any, List, Optional
from dataclasses import dataclass, field

# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

try:
    import requests
except ImportError:
    print("❌ requests library not installed. Run: pip install requests")
    sys.exit(1)

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


@dataclass
class TestResult:
    """Result of a single test."""
    name: str
    passed: bool = False
    status_code: Optional[int] = None
    error: Optional[str] = None
    duration_ms: float = 0.0
    details: Dict[str, Any] = field(default_factory=dict)


@dataclass
class TestReport:
    """Overall test report."""
    total: int = 0
    passed: int = 0
    failed: int = 0
    skipped: int = 0
    results: List[TestResult] = field(default_factory=list)
    
    def add_result(self, result: TestResult):
        self.total += 1
        if result.passed:
            self.passed += 1
        elif result.error and "SKIPPED" in result.error:
            self.skipped += 1
        else:
            self.failed += 1
        self.results.append(result)
    
    def print_summary(self):
        print("\n" + "=" * 60)
        print("TEST SUMMARY")
        print("=" * 60)
        print(f"Total:   {self.total}")
        print(f"✅ Passed:  {self.passed}")
        print(f"❌ Failed:  {self.failed}")
        print(f"⏭️ Skipped: {self.skipped}")
        print("=" * 60)
        
        if self.failed > 0:
            print("\nFailed tests:")
            for result in self.results:
                if not result.passed and result.error and "SKIPPED" not in result.error:
                    print(f"  ❌ {result.name}: {result.error}")
        
        print(f"\nSuccess rate: {self.passed/self.total*100:.1f}%" if self.total > 0 else "No tests run")


class APIIntegrationTester:
    """Comprehensive API integration tester."""
    
    def __init__(self, base_url: str, api_key: Optional[str] = None):
        self.base_url = base_url.rstrip('/')
        self.api_key = api_key or os.getenv('API_KEY', '')
        self.session = requests.Session()
        self.session.headers.update({
            'Content-Type': 'application/json',
            'X-API-Key': self.api_key,
        })
        self.report = TestReport()
        
    def _make_request(
        self,
        method: str,
        endpoint: str,
        data: Optional[Dict] = None,
        timeout: int = 30
    ) -> requests.Response:
        """Make HTTP request with error handling."""
        url = f"{self.base_url}{endpoint}"
        start_time = time.time()
        
        try:
            if method.upper() == 'GET':
                response = self.session.get(url, params=data, timeout=timeout)
            elif method.upper() == 'POST':
                response = self.session.post(url, json=data, timeout=timeout)
            else:
                raise ValueError(f"Unsupported method: {method}")
            
            duration_ms = (time.time() - start_time) * 1000
            return response
            
        except requests.exceptions.Timeout:
            duration_ms = (time.time() - start_time) * 1000
            raise TimeoutError(f"Request timed out after {timeout}s")
        except requests.exceptions.ConnectionError as e:
            duration_ms = (time.time() - start_time) * 1000
            raise ConnectionError(f"Connection failed: {e}")
    
    def test_health_check(self) -> TestResult:
        """Test health check endpoint."""
        result = TestResult(name="Health Check")
        
        try:
            response = self._make_request('GET', '/api/health')
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = data.get('data', {}).get('status') in ['healthy', 'degraded']
                result.details = data.get('data', {})
                
                if not result.passed:
                    result.error = f"Unhealthy status: {data.get('data', {}).get('status')}"
            else:
                result.passed = False
                result.error = f"Unexpected status code: {response.status_code}"
                
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        return result
    
    def test_config_endpoints(self) -> List[TestResult]:
        """Test configuration endpoints."""
        results = []
        
        # Test objectives config
        result = TestResult(name="Config: Objectives")
        try:
            response = self._make_request('GET', '/api/config/objectives')
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = 'data' in data and len(data['data']) > 0
                if not result.passed:
                    result.error = "Empty or invalid response"
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        results.append(result)
        
        # Test presets config
        result = TestResult(name="Config: Presets")
        try:
            response = self._make_request('GET', '/api/config/presets')
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = 'data' in data and len(data['data']) > 0
                if not result.passed:
                    result.error = "Empty or invalid response"
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        results.append(result)

        # IBM Runtime workloads (requires API key; may be 400 if no IBM token)
        result = TestResult(name="Config: IBM Quantum workloads")
        try:
            if not self.api_key:
                result.passed = True
                result.error = "SKIPPED: no API_KEY for protected route"
            else:
                response = self._make_request(
                    "GET",
                    "/api/config/ibm-quantum/workloads",
                    data={"limit": 5},
                )
                result.status_code = response.status_code
                result.duration_ms = response.elapsed.total_seconds() * 1000
                if response.status_code == 200:
                    data = response.json()
                    result.passed = "data" in data and "workloads" in data.get("data", {})
                elif response.status_code in (400, 502, 503):
                    result.passed = True
                    result.details["note"] = "no IBM token or IBM SDK unavailable"
                else:
                    result.passed = False
                    result.error = f"Unexpected status: {response.status_code}"
        except Exception as e:
            result.passed = False
            result.error = str(e)
        results.append(result)
        
        return results
    
    def test_market_data(self) -> TestResult:
        """Test market data endpoint."""
        result = TestResult(name="Market Data Fetch")
        
        try:
            # Test with a small set of tickers
            response = self._make_request(
                'POST',
                '/api/market-data',
                data={'tickers': ['AAPL', 'MSFT', 'GOOGL']},
                timeout=60
            )
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = 'data' in data
                if result.passed:
                    result.details['tickers_count'] = len(data.get('data', {}).get('prices', {}))
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
                
        except TimeoutError:
            result.passed = False
            result.error = "TIMEOUT: Request took longer than 60s (SKIPPED - network issue)"
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        return result
    
    def test_portfolio_optimization(self) -> List[TestResult]:
        """Test portfolio optimization endpoints."""
        results = []
        
        # Test with mock data
        optimization_tests = [
            ('markowitz', 'Max Sharpe (Markowitz)'),
            ('hrp', 'Hierarchical Risk Parity'),
            ('qubo_sa', 'QUBO + Simulated Annealing'),
            ('hybrid', 'Hybrid Pipeline'),
        ]
        
        for objective, test_name in optimization_tests:
            result = TestResult(name=f"Optimization: {test_name}")
            
            try:
                response = self._make_request(
                    'POST',
                    '/api/portfolio/optimize',
                    data={
                        'tickers': ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'META'],
                        'objective': objective,
                        'weight_min': 0.05,
                        'weight_max': 0.50,
                    },
                    timeout=120
                )
                result.status_code = response.status_code
                result.duration_ms = response.elapsed.total_seconds() * 1000
                
                if response.status_code == 200:
                    data = response.json()
                    result.passed = 'data' in data
                    if result.passed:
                        result.details['sharpe_ratio'] = data.get('data', {}).get('sharpe_ratio')
                        result.details['execution_time_ms'] = data.get('meta', {}).get('duration_ms')
                else:
                    result.passed = False
                    result.error = f"Status code: {response.status_code}"
                    
            except TimeoutError:
                result.passed = False
                result.error = f"TIMEOUT: {objective} took longer than 120s (SKIPPED - slow computation)"
            except Exception as e:
                result.passed = False
                result.error = str(e)
            
            results.append(result)
        
        return results
    
    def test_backtest(self) -> TestResult:
        """Test backtesting endpoint."""
        result = TestResult(name="Backtest")
        
        try:
            response = self._make_request(
                'POST',
                '/api/portfolio/backtest',
                data={
                    'tickers': ['AAPL', 'MSFT', 'GOOGL'],
                    'start_date': '2023-01-01',
                    'end_date': '2023-12-31',
                    'objective': 'max_sharpe',
                    'rebalance_frequency': 'monthly',
                },
                timeout=120
            )
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = 'data' in data
                if result.passed:
                    result.details['results_count'] = len(data.get('data', {}).get('results', []))
                    result.details['summary_metrics'] = data.get('data', {}).get('summary_metrics', {})
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
                
        except TimeoutError:
            result.passed = False
            result.error = "TIMEOUT: Backtest took longer than 120s (SKIPPED - slow computation)"
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        return result
    
    def test_efficient_frontier(self) -> TestResult:
        """Test efficient frontier endpoint."""
        result = TestResult(name="Efficient Frontier")
        
        try:
            response = self._make_request(
                'POST',
                '/api/portfolio/efficient-frontier',
                data={
                    'tickers': ['AAPL', 'MSFT', 'GOOGL', 'AMZN'],
                },
                timeout=60
            )
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                data = response.json()
                result.passed = 'data' in data and 'frontier_points' in data.get('data', {})
                if result.passed:
                    result.details['frontier_points'] = len(data.get('data', {}).get('frontier_points', []))
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
                
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        return result
    
    def test_metrics_endpoint(self) -> TestResult:
        """Test Prometheus metrics endpoint."""
        result = TestResult(name="Prometheus Metrics")
        
        try:
            response = self._make_request('GET', '/metrics')
            result.status_code = response.status_code
            result.duration_ms = response.elapsed.total_seconds() * 1000
            
            if response.status_code == 200:
                # Check if response contains Prometheus format
                result.passed = 'http_requests_total' in response.text or 'python_info' in response.text
                if not result.passed:
                    result.error = "Response doesn't contain expected Prometheus metrics"
            else:
                result.passed = False
                result.error = f"Status code: {response.status_code}"
                
        except Exception as e:
            result.passed = False
            result.error = str(e)
        
        return result
    
    def run_all_tests(self) -> TestReport:
        """Run all integration tests."""
        logger.info("Starting API integration tests...")
        logger.info(f"Base URL: {self.base_url}")
        
        # Test 1: Health check
        logger.info("Testing health check...")
        self.report.add_result(self.test_health_check())
        
        # Test 2: Config endpoints
        logger.info("Testing config endpoints...")
        for result in self.test_config_endpoints():
            self.report.add_result(result)
        
        # Test 3: Market data
        logger.info("Testing market data...")
        self.report.add_result(self.test_market_data())
        
        # Test 4: Portfolio optimization
        logger.info("Testing portfolio optimization...")
        for result in self.test_portfolio_optimization():
            self.report.add_result(result)
        
        # Test 5: Backtest
        logger.info("Testing backtest...")
        self.report.add_result(self.test_backtest())
        
        # Test 6: Efficient frontier
        logger.info("Testing efficient frontier...")
        self.report.add_result(self.test_efficient_frontier())
        
        # Test 7: Metrics
        logger.info("Testing metrics endpoint...")
        self.report.add_result(self.test_metrics_endpoint())
        
        return self.report


def check_dependencies() -> Dict[str, bool]:
    """Check if all required dependencies are installed."""
    deps = {
        'requests': False,
        'numpy': False,
        'pandas': False,
        'flask': False,
        'scipy': False,
        'sklearn': False,
        'braket': False,
    }
    
    import importlib
    
    for dep in deps:
        try:
            if dep == 'sklearn':
                importlib.import_module('sklearn')
            else:
                importlib.import_module(dep)
            deps[dep] = True
        except ImportError:
            pass
    
    return deps


def main():
    """Main entry point."""
    import argparse
    
    parser = argparse.ArgumentParser(description='API Integration Test Suite')
    parser.add_argument(
        '--base-url',
        default='http://localhost:5000',
        help='Base URL of the API (default: http://localhost:5000)'
    )
    parser.add_argument(
        '--api-key',
        default='',
        help='API key for authentication'
    )
    parser.add_argument(
        '--check-deps-only',
        action='store_true',
        help='Only check dependencies, don\'t run tests'
    )
    
    args = parser.parse_args()
    
    print("=" * 60)
    print("QUANTUM HYBRID PORTFOLIO - API INTEGRATION TESTS")
    print("=" * 60)
    
    # Check dependencies
    print("\n📦 Checking dependencies...")
    deps = check_dependencies()
    
    all_deps_installed = True
    for dep, installed in deps.items():
        status = "✅" if installed else "❌"
        print(f"  {status} {dep}")
        if not installed and dep in ['requests', 'numpy', 'pandas', 'flask']:
            all_deps_installed = False
    
    if args.check_deps_only:
        return 0 if all_deps_installed else 1
    
    if not all_deps_installed:
        print("\n⚠️  Some critical dependencies are missing.")
        print("   Run: pip install -r requirements.txt")
        return 1
    
    print("\n" + "=" * 60)
    
    # Run tests
    tester = APIIntegrationTester(
        base_url=args.base_url,
        api_key=args.api_key
    )
    
    try:
        report = tester.run_all_tests()
        report.print_summary()
        
        # Save detailed report
        report_path = 'test_report.json'
        with open(report_path, 'w') as f:
            json.dump({
                'total': report.total,
                'passed': report.passed,
                'failed': report.failed,
                'skipped': report.skipped,
                'results': [
                    {
                        'name': r.name,
                        'passed': r.passed,
                        'status_code': r.status_code,
                        'error': r.error,
                        'duration_ms': r.duration_ms,
                        'details': r.details,
                    }
                    for r in report.results
                ],
            }, f, indent=2)
        
        print(f"\n📄 Detailed report saved to: {report_path}")
        
        return 0 if report.failed == 0 else 1
        
    except ConnectionError as e:
        print(f"\n❌ Failed to connect to API at {args.base_url}")
        print(f"   Error: {e}")
        print("\n   Make sure the API server is running:")
        print("   python api.py")
        return 1
    except Exception as e:
        print(f"\n❌ Unexpected error: {e}")
        import traceback
        traceback.print_exc()
        return 1


if __name__ == '__main__':
    sys.exit(main())