""" Test script for multi-provider configuration. Tests both OpenAI and Gemini providers with simple completion requests to verify API keys are valid and providers are functional. Task Reference: T008 Spec Reference: specs/001-multi-model-chat/spec.md (US1) Usage: uv run python -m scripts.test_providers """ import asyncio import os import sys from pathlib import Path # Add backend/src to path for imports backend_dir = Path(__file__).parent.parent sys.path.insert(0, str(backend_dir / "src")) from agents import Agent, Runner from dotenv import load_dotenv from core.providers import get_provider # Load environment variables load_dotenv() async def test_provider(provider_name: str) -> bool: """ Test a single provider with a simple completion request. Args: provider_name: "openai" or "gemini" Returns: True if provider test succeeds, False otherwise """ print(f"\n{'=' * 60}") print(f"Testing {provider_name.upper()}") print('=' * 60) try: # Get provider config provider = get_provider(provider_name) # type: ignore # Check API key if not provider.api_key: print(f"❌ FAIL: {provider_name.upper()}_API_KEY not set") return False print(f"✓ API key configured: {provider.api_key[:10]}...") print(f"✓ Model: {provider.model}") print(f"✓ Base URL: {provider.base_url or 'default (OpenAI)'}") # Create agent with this provider config = provider.create_run_config() agent = Agent( name="test_agent", instructions="You respond in haikus only.", model=provider.create_model(), ) # Run test completion print("\nSending test request: 'Tell me about recursion in programming'") result = await Runner.run( agent, input="Tell me about recursion in programming", run_config=config ) # Display response print(f"\n✅ SUCCESS - Response from {provider_name}:") print("-" * 60) print(result.final_output) print("-" * 60) return True except Exception as e: print(f"\n❌ FAIL: {provider_name} test failed") print(f"Error: {e}") return False async def main(): """Run tests for all configured providers.""" print("=" * 60) print("MULTI-PROVIDER TEST SCRIPT") print("=" * 60) print("\nThis script tests both OpenAI and Gemini providers.") print("Ensure both API keys are set in backend/.env:") print(" - OPENAI_API_KEY") print(" - GEMINI_API_KEY") # Test both providers openai_success = await test_provider("openai") gemini_success = await test_provider("gemini") # Summary print("\n" + "=" * 60) print("TEST SUMMARY") print("=" * 60) print(f"OpenAI: {'✅ PASS' if openai_success else '❌ FAIL'}") print(f"Gemini: {'✅ PASS' if gemini_success else '❌ FAIL'}") if openai_success and gemini_success: print("\n✅ All providers working!") return 0 else: print("\n⚠️ Some providers failed. Check API keys and try again.") return 1 if __name__ == "__main__": exit_code = asyncio.run(main()) sys.exit(exit_code)