#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化的Agent功能测试脚本
逐个测试每个Agent避免超时问题
"""

import asyncio
import sys
import os
from datetime import datetime

# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

def print_separator(title: str):
    """打印分隔符"""
    print("\n" + "="*60)
    print(f"🧪 {title}")
    print("="*60)

def print_test_result(test_name: str, result: str, status="✅"):
    """打印测试结果"""
    print(f"\n{status} 测试: {test_name}")
    print("-" * 40)
    print(result[:500] + "..." if len(result) > 500 else result)
    print("-" * 40)

async def test_enhanced_rag_agent():
    """测试Enhanced RAG Agent"""
    print_separator("Enhanced RAG Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试1: 电信专业问题
        result1 = await enhanced_system_query("什么是电信客户流失？")
        print_test_result("电信专业问题", result1)
        
        return True
    except Exception as e:
        print_test_result("Enhanced RAG Agent", f"❌ 错误: {str(e)}", "❌")
        return False

async def test_enhanced_python_agent():
    """测试Enhanced Python Agent"""
    print_separator("Enhanced Python Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试简单Python代码执行
        result = await enhanced_system_query("计算1+1等于多少，用Python代码执行")
        print_test_result("Python代码执行", result)
        
        return True
    except Exception as e:
        print_test_result("Enhanced Python Agent", f"❌ 错误: {str(e)}", "❌")
        return False

async def test_enhanced_amap_agent():
    """测试Enhanced AMAP Agent"""
    print_separator("Enhanced AMAP Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试天气查询
        result = await enhanced_system_query("查询北京今天的天气")
        print_test_result("天气查询", result)
        
        return True
    except Exception as e:
        print_test_result("Enhanced AMAP Agent", f"❌ 错误: {str(e)}", "❌")
        return False

async def test_safe_file_agent():
    """测试Safe File Agent"""
    print_separator("Safe File Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试文件操作
        result = await enhanced_system_query("在workspace目录下创建一个test.txt文件，内容为'Hello World'")
        print_test_result("文件创建", result)
        
        return True
    except Exception as e:
        print_test_result("Safe File Agent", f"❌ 错误: {str(e)}", "❌")
        return False

async def test_sql_agent():
    """测试SQL Agent"""
    print_separator("SQL Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试数据库连接状态
        result = await enhanced_system_query("检查数据库连接状态")
        print_test_result("数据库连接检查", result)
        
        return True
    except Exception as e:
        print_test_result("SQL Agent", f"❌ 错误: {str(e)}", "❌")  
        return False

async def test_terminal_command_agent():
    """测试Terminal Command Agent"""
    print_separator("Terminal Command Agent 测试")
    
    try:
        from enhanced_data_agent import enhanced_system_query
        
        # 测试系统命令
        result = await enhanced_system_query("查看当前目录的文件列表")
        print_test_result("系统命令执行", result)
        
        return True
    except Exception as e:
        print_test_result("Terminal Command Agent", f"❌ 错误: {str(e)}", "❌")
        return False

async def main():
    """主测试函数"""
    print("🚀 开始Agent功能测试")
    print(f"📅 测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    test_results = {}
    
    # 逐个测试各Agent
    agents_to_test = [
        ("Enhanced RAG Agent", test_enhanced_rag_agent),
        ("Enhanced Python Agent", test_enhanced_python_agent), 
        ("Enhanced AMAP Agent", test_enhanced_amap_agent),
        ("Safe File Agent", test_safe_file_agent),
        ("SQL Agent", test_sql_agent),
        ("Terminal Command Agent", test_terminal_command_agent)
    ]
    
    for agent_name, test_func in agents_to_test:
        try:
            print(f"\n🔧 正在测试 {agent_name}...")
            result = await test_func()
            test_results[agent_name] = result
        except Exception as e:
            print(f"❌ {agent_name} 测试失败: {str(e)}")
            test_results[agent_name] = False
    
    # 输出测试摘要
    print_separator("测试结果摘要")
    passed = sum(test_results.values())
    total = len(test_results)
    
    for agent_name, result in test_results.items():
        status = "✅ 通过" if result else "❌ 失败"
        print(f"{status} {agent_name}")
    
    print(f"\n📊 总体结果: {passed}/{total} 个Agent测试通过")
    
    if passed == total:
        print("🎉 所有Agent测试通过！")
    else:
        print(f"⚠️  有 {total - passed} 个Agent需要修复")

if __name__ == "__main__":
    asyncio.run(main())