import os import sys import importlib.util import subprocess import pytest from colorama import init, Fore, Style # Initialize colorama for colored output init() # Ensure project root is on sys.path PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) if PROJECT_ROOT not in sys.path: sys.path.insert(0, PROJECT_ROOT) def check_dependencies(): """Check if all required dependencies are installed""" required_packages = ['httpx', 'pytest', 'colorama', 'fastapi'] missing_packages = [] for package in required_packages: if importlib.util.find_spec(package) is None: missing_packages.append(package) if missing_packages: print(f"{Fore.RED}Missing required dependencies: {', '.join(missing_packages)}{Style.RESET_ALL}") install = input(f"Would you like to install them now? (y/n): ").lower().strip() == 'y' if install: print(f"{Fore.YELLOW}Installing missing packages...{Style.RESET_ALL}") subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing_packages) print(f"{Fore.GREEN}Dependencies installed successfully!{Style.RESET_ALL}") return True else: print(f"{Fore.RED}Cannot run tests without required dependencies.{Style.RESET_ALL}") return False return True def main(): # Check dependencies first if not check_dependencies(): return 1 print(f"{Fore.CYAN}===== Running Unified API Tests ====={Style.RESET_ALL}") # Define test patterns to run test_patterns = [ "tests/test_api_endpoints.py::test_unified_chat_text", "tests/test_live_ai.py::test_unified_text_live" ] # Run each test and collect results results = {} for pattern in test_patterns: print(f"\n{Fore.YELLOW}Running: {pattern}{Style.RESET_ALL}") exit_code = pytest.main([pattern, "-v"]) results[pattern] = exit_code == 0 # Print summary print(f"\n{Fore.CYAN}===== Test Results Summary ====={Style.RESET_ALL}") all_passed = True for pattern, passed in results.items(): status = f"{Fore.GREEN}PASSED{Style.RESET_ALL}" if passed else f"{Fore.RED}FAILED{Style.RESET_ALL}" print(f"{pattern}: {status}") if not passed: all_passed = False # Final status if all_passed: print(f"\n{Fore.GREEN}All unified tests passed!{Style.RESET_ALL}") else: print(f"\n{Fore.RED}Some unified tests failed. Check the output above for details.{Style.RESET_ALL}") return 0 if all_passed else 1 if __name__ == "__main__": sys.exit(main())