#!/bin/bash

# 声音克隆MCP服务测试运行脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Python环境
check_python() {
    if ! command -v python3 &> /dev/null; then
        log_error "Python3未安装"
        exit 1
    fi
    
    log_success "Python3环境检查通过"
}

# 检查pytest
check_pytest() {
    if ! python3 -c "import pytest" &> /dev/null; then
        log_warning "pytest未安装，正在安装..."
        pip3 install pytest pytest-asyncio
    fi
    
    log_success "pytest环境检查通过"
}

# 运行单元测试
run_unit_tests() {
    log_info "运行单元测试..."
    
    python3 -m pytest tests/ -v --tb=short -m "not integration and not slow"
    
    if [ $? -eq 0 ]; then
        log_success "单元测试通过"
    else
        log_error "单元测试失败"
        return 1
    fi
}

# 运行集成测试
run_integration_tests() {
    log_info "运行集成测试..."
    
    python3 -m pytest tests/ -v --tb=short -m "integration"
    
    if [ $? -eq 0 ]; then
        log_success "集成测试通过"
    else
        log_warning "集成测试失败（可能需要实际的TTS模型）"
    fi
}

# 运行安全性测试
run_security_tests() {
    log_info "运行安全性测试..."
    
    python3 -m pytest tests/test_security.py -v --tb=short
    
    if [ $? -eq 0 ]; then
        log_success "安全性测试通过"
    else
        log_error "安全性测试失败"
        return 1
    fi
}

# 运行覆盖率测试
run_coverage_tests() {
    log_info "运行覆盖率测试..."
    
    if ! python3 -c "import coverage" &> /dev/null; then
        log_warning "coverage未安装，正在安装..."
        pip3 install coverage
    fi
    
    python3 -m coverage run -m pytest tests/
    python3 -m coverage report
    python3 -m coverage html
    
    log_success "覆盖率报告已生成到htmlcov/目录"
}

# 运行性能测试
run_performance_tests() {
    log_info "运行性能测试..."
    
    python3 -m pytest tests/ -v --tb=short -m "performance" --durations=0
    
    if [ $? -eq 0 ]; then
        log_success "性能测试通过"
    else
        log_warning "性能测试失败（可能需要实际的服务运行）"
    fi
}

# 清理测试文件
cleanup_test_files() {
    log_info "清理测试文件..."
    
    # 清理临时文件
    find . -name "*.pyc" -delete
    find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true
    find . -name ".pytest_cache" -type d -exec rm -rf {} + 2>/dev/null || true
    
    log_success "测试文件清理完成"
}

# 显示帮助信息
show_help() {
    echo "声音克隆MCP服务测试脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  unit        运行单元测试"
    echo "  integration 运行集成测试"
    echo "  security    运行安全性测试"
    echo "  coverage    运行覆盖率测试"
    echo "  performance 运行性能测试"
    echo "  all         运行所有测试"
    echo "  clean       清理测试文件"
    echo "  help        显示此帮助信息"
    echo ""
}

# 主函数
main() {
    case "${1:-all}" in
        unit)
            check_python
            check_pytest
            run_unit_tests
            ;;
        integration)
            check_python
            check_pytest
            run_integration_tests
            ;;
        security)
            check_python
            check_pytest
            run_security_tests
            ;;
        coverage)
            check_python
            check_pytest
            run_coverage_tests
            ;;
        performance)
            check_python
            check_pytest
            run_performance_tests
            ;;
        all)
            echo -e "${BLUE}🧪 运行声音克隆MCP服务完整测试套件${NC}"
            echo ""
            
            check_python
            check_pytest
            
            run_unit_tests
            echo ""
            
            run_security_tests
            echo ""
            
            run_integration_tests
            echo ""
            
            run_performance_tests
            echo ""
            
            run_coverage_tests
            echo ""
            
            log_success "所有测试完成！"
            ;;
        clean)
            cleanup_test_files
            ;;
        help)
            show_help
            ;;
        *)
            log_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@"