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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
573
574
575
576
577
578
579
#!/usr/bin/env python3
"""

Client SDK testing script for FastAPI Video Generation Backend.



This script tests the generated client SDKs to ensure they work correctly

with the API endpoints. It performs basic functionality tests and validates

the client-server communication.

"""

import os
import sys
import json
import time
import asyncio
import requests
import subprocess
from pathlib import Path
from typing import Dict, List, Optional, Any
import tempfile
import logging

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


class ClientTester:
    """Test suite for generated client SDKs."""
    
    def __init__(self, api_url: str = "http://localhost:8000", clients_dir: str = "generated_clients"):
        self.api_url = api_url.rstrip("/")
        self.clients_dir = Path(clients_dir)
        self.test_results: Dict[str, Dict[str, Any]] = {}
        
    def check_api_availability(self) -> bool:
        """Check if the API server is running and accessible."""
        try:
            logger.info(f"Checking API availability at {self.api_url}")
            response = requests.get(f"{self.api_url}/health", timeout=10)
            response.raise_for_status()
            logger.info("API server is available")
            return True
        except requests.RequestException as e:
            logger.error(f"API server is not available: {e}")
            return False
    
    def test_openapi_spec(self) -> bool:
        """Test if OpenAPI specification is accessible."""
        try:
            logger.info("Testing OpenAPI specification accessibility")
            response = requests.get(f"{self.api_url}/openapi.json", timeout=10)
            response.raise_for_status()
            
            spec = response.json()
            
            # Validate basic OpenAPI structure
            required_fields = ["openapi", "info", "paths"]
            for field in required_fields:
                if field not in spec:
                    logger.error(f"OpenAPI spec missing required field: {field}")
                    return False
            
            logger.info("OpenAPI specification is valid")
            return True
            
        except requests.RequestException as e:
            logger.error(f"Failed to fetch OpenAPI specification: {e}")
            return False
        except json.JSONDecodeError as e:
            logger.error(f"Invalid JSON in OpenAPI specification: {e}")
            return False
    
    def test_typescript_client(self) -> Dict[str, Any]:
        """Test TypeScript client."""
        client_dir = self.clients_dir / "typescript"
        result = {
            "language": "typescript",
            "exists": False,
            "builds": False,
            "has_examples": False,
            "has_docs": False,
            "errors": []
        }
        
        try:
            if not client_dir.exists():
                result["errors"].append("Client directory does not exist")
                return result
            
            result["exists"] = True
            
            # Check for essential files
            essential_files = ["package.json", "README.md"]
            for file in essential_files:
                if not (client_dir / file).exists():
                    result["errors"].append(f"Missing {file}")
            
            # Check for example file
            if (client_dir / "example.ts").exists():
                result["has_examples"] = True
            
            # Check for documentation
            if (client_dir / "README.md").exists():
                result["has_docs"] = True
            
            # Try to build the client
            if (client_dir / "package.json").exists():
                logger.info("Testing TypeScript client build")
                
                # Install dependencies
                install_result = subprocess.run(
                    ["npm", "install"],
                    cwd=client_dir,
                    capture_output=True,
                    text=True,
                    timeout=120
                )
                
                if install_result.returncode != 0:
                    result["errors"].append(f"npm install failed: {install_result.stderr}")
                else:
                    # Try to build
                    build_result = subprocess.run(
                        ["npm", "run", "build"],
                        cwd=client_dir,
                        capture_output=True,
                        text=True,
                        timeout=60
                    )
                    
                    if build_result.returncode == 0:
                        result["builds"] = True
                    else:
                        result["errors"].append(f"Build failed: {build_result.stderr}")
            
        except subprocess.TimeoutExpired:
            result["errors"].append("Build timeout")
        except Exception as e:
            result["errors"].append(f"Unexpected error: {str(e)}")
        
        return result
    
    def test_python_client(self) -> Dict[str, Any]:
        """Test Python client."""
        client_dir = self.clients_dir / "python"
        result = {
            "language": "python",
            "exists": False,
            "installs": False,
            "imports": False,
            "has_examples": False,
            "has_docs": False,
            "errors": []
        }
        
        try:
            if not client_dir.exists():
                result["errors"].append("Client directory does not exist")
                return result
            
            result["exists"] = True
            
            # Check for essential files
            essential_files = ["setup.py", "README.md"]
            for file in essential_files:
                if not (client_dir / file).exists():
                    result["errors"].append(f"Missing {file}")
            
            # Check for example file
            if (client_dir / "example.py").exists():
                result["has_examples"] = True
            
            # Check for documentation
            if (client_dir / "README.md").exists():
                result["has_docs"] = True
            
            # Try to install the client in development mode
            if (client_dir / "setup.py").exists():
                logger.info("Testing Python client installation")
                
                install_result = subprocess.run(
                    [sys.executable, "setup.py", "develop", "--user"],
                    cwd=client_dir,
                    capture_output=True,
                    text=True,
                    timeout=120
                )
                
                if install_result.returncode == 0:
                    result["installs"] = True
                    
                    # Try to import the client
                    try:
                        import importlib.util
                        
                        # Find the main module
                        main_module_path = None
                        for py_file in client_dir.rglob("*.py"):
                            if "__init__.py" in py_file.name:
                                main_module_path = py_file
                                break
                        
                        if main_module_path:
                            spec = importlib.util.spec_from_file_location("test_client", main_module_path)
                            if spec and spec.loader:
                                module = importlib.util.module_from_spec(spec)
                                spec.loader.exec_module(module)
                                result["imports"] = True
                        
                    except Exception as e:
                        result["errors"].append(f"Import failed: {str(e)}")
                else:
                    result["errors"].append(f"Installation failed: {install_result.stderr}")
            
        except subprocess.TimeoutExpired:
            result["errors"].append("Installation timeout")
        except Exception as e:
            result["errors"].append(f"Unexpected error: {str(e)}")
        
        return result
    
    def test_java_client(self) -> Dict[str, Any]:
        """Test Java client."""
        client_dir = self.clients_dir / "java"
        result = {
            "language": "java",
            "exists": False,
            "compiles": False,
            "has_examples": False,
            "has_docs": False,
            "errors": []
        }
        
        try:
            if not client_dir.exists():
                result["errors"].append("Client directory does not exist")
                return result
            
            result["exists"] = True
            
            # Check for essential files
            essential_files = ["pom.xml", "README.md"]
            for file in essential_files:
                if not (client_dir / file).exists():
                    result["errors"].append(f"Missing {file}")
            
            # Check for example file
            if (client_dir / "example.java").exists():
                result["has_examples"] = True
            
            # Check for documentation
            if (client_dir / "README.md").exists():
                result["has_docs"] = True
            
            # Try to compile the client (if Maven is available)
            if (client_dir / "pom.xml").exists():
                logger.info("Testing Java client compilation")
                
                # Check if Maven is available
                maven_check = subprocess.run(
                    ["mvn", "--version"],
                    capture_output=True,
                    text=True,
                    timeout=10
                )
                
                if maven_check.returncode == 0:
                    compile_result = subprocess.run(
                        ["mvn", "compile"],
                        cwd=client_dir,
                        capture_output=True,
                        text=True,
                        timeout=180
                    )
                    
                    if compile_result.returncode == 0:
                        result["compiles"] = True
                    else:
                        result["errors"].append(f"Compilation failed: {compile_result.stderr}")
                else:
                    result["errors"].append("Maven not available for compilation test")
            
        except subprocess.TimeoutExpired:
            result["errors"].append("Compilation timeout")
        except Exception as e:
            result["errors"].append(f"Unexpected error: {str(e)}")
        
        return result
    
    def test_client_structure(self, language: str) -> Dict[str, Any]:
        """Test the structure of a generated client."""
        client_dir = self.clients_dir / language
        result = {
            "language": language,
            "exists": False,
            "structure_valid": False,
            "has_api_files": False,
            "has_model_files": False,
            "has_docs": False,
            "file_count": 0,
            "errors": []
        }
        
        try:
            if not client_dir.exists():
                result["errors"].append("Client directory does not exist")
                return result
            
            result["exists"] = True
            
            # Count files
            all_files = list(client_dir.rglob("*"))
            result["file_count"] = len([f for f in all_files if f.is_file()])
            
            # Check for API files
            api_files = list(client_dir.rglob("*api*"))
            if api_files:
                result["has_api_files"] = True
            
            # Check for model files
            model_files = list(client_dir.rglob("*model*"))
            if model_files:
                result["has_model_files"] = True
            
            # Check for documentation
            doc_files = list(client_dir.rglob("README*")) + list(client_dir.rglob("*.md"))
            if doc_files:
                result["has_docs"] = True
            
            # Basic structure validation
            if result["has_api_files"] and result["has_model_files"]:
                result["structure_valid"] = True
            
        except Exception as e:
            result["errors"].append(f"Unexpected error: {str(e)}")
        
        return result
    
    def run_functional_tests(self) -> Dict[str, Any]:
        """Run functional tests against the API using generated clients."""
        result = {
            "api_reachable": False,
            "openapi_valid": False,
            "endpoints_tested": 0,
            "endpoints_passed": 0,
            "errors": []
        }
        
        try:
            # Test API availability
            result["api_reachable"] = self.check_api_availability()
            
            # Test OpenAPI spec
            result["openapi_valid"] = self.test_openapi_spec()
            
            if not result["api_reachable"]:
                result["errors"].append("API not reachable - skipping functional tests")
                return result
            
            # Test basic endpoints
            endpoints_to_test = [
                {"method": "GET", "path": "/health", "expected_status": 200},
                {"method": "GET", "path": "/", "expected_status": 200},
                {"method": "GET", "path": "/openapi.json", "expected_status": 200},
            ]
            
            for endpoint in endpoints_to_test:
                try:
                    result["endpoints_tested"] += 1
                    
                    response = requests.request(
                        endpoint["method"],
                        f"{self.api_url}{endpoint['path']}",
                        timeout=10
                    )
                    
                    if response.status_code == endpoint["expected_status"]:
                        result["endpoints_passed"] += 1
                    else:
                        result["errors"].append(
                            f"{endpoint['method']} {endpoint['path']} returned {response.status_code}, "
                            f"expected {endpoint['expected_status']}"
                        )
                        
                except requests.RequestException as e:
                    result["errors"].append(f"Failed to test {endpoint['path']}: {str(e)}")
            
        except Exception as e:
            result["errors"].append(f"Functional test error: {str(e)}")
        
        return result
    
    def run_all_tests(self) -> Dict[str, Any]:
        """Run all client tests."""
        logger.info("Starting comprehensive client testing")
        
        # Test API functionality
        functional_results = self.run_functional_tests()
        
        # Test individual clients
        client_results = {}
        
        # Test TypeScript client
        if (self.clients_dir / "typescript").exists():
            logger.info("Testing TypeScript client")
            client_results["typescript"] = self.test_typescript_client()
        
        # Test Python client
        if (self.clients_dir / "python").exists():
            logger.info("Testing Python client")
            client_results["python"] = self.test_python_client()
        
        # Test Java client
        if (self.clients_dir / "java").exists():
            logger.info("Testing Java client")
            client_results["java"] = self.test_java_client()
        
        # Test structure for all clients
        structure_results = {}
        for client_dir in self.clients_dir.iterdir():
            if client_dir.is_dir():
                language = client_dir.name
                logger.info(f"Testing {language} client structure")
                structure_results[language] = self.test_client_structure(language)
        
        return {
            "functional_tests": functional_results,
            "client_tests": client_results,
            "structure_tests": structure_results,
            "summary": self._generate_summary(functional_results, client_results, structure_results)
        }
    
    def _generate_summary(self, functional: Dict, clients: Dict, structures: Dict) -> Dict[str, Any]:
        """Generate test summary."""
        total_clients = len(structures)
        working_clients = 0
        
        for language, result in clients.items():
            if language == "typescript" and result.get("builds", False):
                working_clients += 1
            elif language == "python" and result.get("imports", False):
                working_clients += 1
            elif language == "java" and result.get("compiles", False):
                working_clients += 1
        
        return {
            "total_clients_found": total_clients,
            "clients_tested": len(clients),
            "working_clients": working_clients,
            "api_functional": functional.get("api_reachable", False),
            "openapi_valid": functional.get("openapi_valid", False),
            "endpoints_success_rate": (
                functional.get("endpoints_passed", 0) / max(functional.get("endpoints_tested", 1), 1) * 100
            )
        }
    
    def print_results(self, results: Dict[str, Any]) -> None:
        """Print test results in a formatted way."""
        print("\n" + "="*60)
        print("CLIENT SDK TEST RESULTS")
        print("="*60)
        
        # Print summary
        summary = results["summary"]
        print(f"\nSUMMARY:")
        print(f"  Total clients found: {summary['total_clients_found']}")
        print(f"  Clients tested: {summary['clients_tested']}")
        print(f"  Working clients: {summary['working_clients']}")
        print(f"  API functional: {'βœ…' if summary['api_functional'] else '❌'}")
        print(f"  OpenAPI valid: {'βœ…' if summary['openapi_valid'] else '❌'}")
        print(f"  Endpoints success rate: {summary['endpoints_success_rate']:.1f}%")
        
        # Print functional test results
        print(f"\nFUNCTIONAL TESTS:")
        functional = results["functional_tests"]
        print(f"  API reachable: {'βœ…' if functional['api_reachable'] else '❌'}")
        print(f"  OpenAPI valid: {'βœ…' if functional['openapi_valid'] else '❌'}")
        print(f"  Endpoints tested: {functional['endpoints_tested']}")
        print(f"  Endpoints passed: {functional['endpoints_passed']}")
        
        if functional["errors"]:
            print("  Errors:")
            for error in functional["errors"]:
                print(f"    - {error}")
        
        # Print client test results
        print(f"\nCLIENT TESTS:")
        for language, result in results["client_tests"].items():
            print(f"\n  {language.upper()}:")
            print(f"    Exists: {'βœ…' if result['exists'] else '❌'}")
            
            if language == "typescript":
                print(f"    Builds: {'βœ…' if result['builds'] else '❌'}")
            elif language == "python":
                print(f"    Installs: {'βœ…' if result['installs'] else '❌'}")
                print(f"    Imports: {'βœ…' if result['imports'] else '❌'}")
            elif language == "java":
                print(f"    Compiles: {'βœ…' if result['compiles'] else '❌'}")
            
            print(f"    Has examples: {'βœ…' if result['has_examples'] else '❌'}")
            print(f"    Has docs: {'βœ…' if result['has_docs'] else '❌'}")
            
            if result["errors"]:
                print("    Errors:")
                for error in result["errors"]:
                    print(f"      - {error}")
        
        # Print structure test results
        print(f"\nSTRUCTURE TESTS:")
        for language, result in results["structure_tests"].items():
            print(f"\n  {language.upper()}:")
            print(f"    Exists: {'βœ…' if result['exists'] else '❌'}")
            print(f"    Structure valid: {'βœ…' if result['structure_valid'] else '❌'}")
            print(f"    Has API files: {'βœ…' if result['has_api_files'] else '❌'}")
            print(f"    Has model files: {'βœ…' if result['has_model_files'] else '❌'}")
            print(f"    Has docs: {'βœ…' if result['has_docs'] else '❌'}")
            print(f"    File count: {result['file_count']}")
            
            if result["errors"]:
                print("    Errors:")
                for error in result["errors"]:
                    print(f"      - {error}")
        
        print("\n" + "="*60)


def main():
    """Main function for command-line usage."""
    import argparse
    
    parser = argparse.ArgumentParser(description="Test generated client SDKs")
    parser.add_argument(
        "--api-url",
        default="http://localhost:8000",
        help="Base URL of the API (default: http://localhost:8000)"
    )
    parser.add_argument(
        "--clients-dir",
        default="generated_clients",
        help="Directory containing generated clients (default: generated_clients)"
    )
    parser.add_argument(
        "--output-file",
        help="Save test results to JSON file"
    )
    parser.add_argument(
        "--verbose",
        action="store_true",
        help="Enable verbose logging"
    )
    
    args = parser.parse_args()
    
    if args.verbose:
        logging.getLogger().setLevel(logging.DEBUG)
    
    # Create tester instance
    tester = ClientTester(args.api_url, args.clients_dir)
    
    # Run tests
    results = tester.run_all_tests()
    
    # Print results
    tester.print_results(results)
    
    # Save results to file if requested
    if args.output_file:
        with open(args.output_file, 'w') as f:
            json.dump(results, f, indent=2, default=str)
        logger.info(f"Test results saved to {args.output_file}")
    
    # Exit with error code if tests failed
    summary = results["summary"]
    if not summary["api_functional"] or summary["working_clients"] == 0:
        sys.exit(1)


if __name__ == "__main__":
    main()