#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单的API测试脚本
测试.env.example中的配置是否正确
"""

import requests
import json
import logging

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

def test_simple_api():
    """简单的API测试"""
    
    # 从.env.example读取的配置
    BASE_URL = "http://master.tailnet-68f9.ts.net:40012"
    API_KEY = "sk-0jdcGHZJpX2oUJmyEs7zVA"
    MODEL = "gemini/gemini-2.5-pro"
    
    logger.info(f"🧪 测试配置:")
    logger.info(f"📡 BASE_URL: {BASE_URL}")
    logger.info(f"🔑 API_KEY: {API_KEY[:10]}...")
    logger.info(f"🤖 MODEL: {MODEL}")
    
    # 最简单的请求
    payload = {
        "model": MODEL,
        "messages": [
            {"role": "user", "content": "Hello"}
        ],
        "max_tokens": 50
    }
    
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}"
    }
    
    try:
        logger.info("🚀 发送请求...")
        response = requests.post(
            f"{BASE_URL}/chat/completions",
            json=payload,
            headers=headers,
            timeout=120  # 增加超时时间
        )
        
        logger.info(f"📊 状态码: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            logger.info(f"✅ 请求成功!")
            logger.info(f"📋 响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
            return True
        else:
            logger.error(f"❌ 请求失败: {response.status_code}")
            logger.error(f"📋 错误响应: {response.text}")
            return False
            
    except requests.exceptions.Timeout:
        logger.error(f"⏰ 请求超时 (120秒)")
        return False
    except requests.exceptions.ConnectionError as e:
        logger.error(f"🔌 连接错误: {e}")
        return False
    except Exception as e:
        logger.error(f"💥 未知错误: {e}")
        return False

if __name__ == "__main__":
    logger.info("🎯 开始简单API测试")
    success = test_simple_api()
    
    if success:
        logger.info("🎉 测试成功!")
    else:
        logger.error("💀 测试失败!")
    
    logger.info("🏁 测试完成")