File size: 3,273 Bytes
507973e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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)