#!/bin/bash

# Ansible-rs nextest 测试脚本
# 使用 cargo-nextest 运行各种测试配置

set -e

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

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# 检查 cargo-nextest 是否安装
check_nextest() {
    if ! command -v cargo-nextest &> /dev/null; then
        print_warning "cargo-nextest 未安装，正在安装..."
        cargo install cargo-nextest --locked
        print_success "cargo-nextest 安装完成"
    else
        print_info "cargo-nextest 已安装"
    fi
}

# 运行快速测试
run_quick_tests() {
    print_info "运行快速测试..."
    cargo nextest run --profile quick
    print_success "快速测试完成"
}

# 运行完整测试套件
run_full_tests() {
    print_info "运行完整测试套件..."
    cargo nextest run --profile default --all-features
    print_success "完整测试套件完成"
}

# 运行 CI 测试
run_ci_tests() {
    print_info "运行 CI 测试配置..."
    cargo nextest run --profile ci --all-features
    print_success "CI 测试完成"
}

# 运行覆盖率测试
run_coverage_tests() {
    print_info "运行覆盖率测试..."
    cargo nextest run --profile coverage --all-features
    print_success "覆盖率测试完成"
}

# 运行特定测试类型
run_specific_tests() {
    print_info "运行单元测试..."
    cargo nextest run test_errors test_command_config test_ansible test_playbook
    
    print_info "运行集成测试..."
    cargo nextest run test_integration
    
    print_info "运行 Mock 测试..."
    cargo nextest run test_mock
    
    print_info "运行属性测试..."
    cargo nextest run test_property
    
    print_success "特定测试类型完成"
}

# 运行文档测试
run_doc_tests() {
    print_info "运行文档测试..."
    cargo test --doc
    print_success "文档测试完成"
}

# 生成测试报告
generate_reports() {
    print_info "生成测试报告..."
    
    # 创建报告目录
    mkdir -p target/nextest-reports
    
    # 运行测试并生成 JUnit 报告
    cargo nextest run --profile ci --all-features || true
    
    # 复制报告
    if [ -f "target/nextest/ci-junit.xml" ]; then
        cp target/nextest/ci-junit.xml target/nextest-reports/
        print_success "JUnit 报告已生成: target/nextest-reports/ci-junit.xml"
    fi
    
    print_success "测试报告生成完成"
}

# 显示帮助信息
show_help() {
    echo "Ansible-rs nextest 测试脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  quick      运行快速测试"
    echo "  full       运行完整测试套件"
    echo "  ci         运行 CI 测试配置"
    echo "  coverage   运行覆盖率测试"
    echo "  specific   运行特定测试类型"
    echo "  doc        运行文档测试"
    echo "  report     生成测试报告"
    echo "  all        运行所有测试"
    echo "  help       显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 quick          # 快速测试"
    echo "  $0 full           # 完整测试"
    echo "  $0 all            # 所有测试"
}

# 运行所有测试
run_all_tests() {
    print_info "开始运行所有测试..."
    
    check_nextest
    run_quick_tests
    run_full_tests
    run_specific_tests
    run_doc_tests
    generate_reports
    
    print_success "所有测试完成！"
}

# 主函数
main() {
    case "${1:-all}" in
        "quick")
            check_nextest
            run_quick_tests
            ;;
        "full")
            check_nextest
            run_full_tests
            ;;
        "ci")
            check_nextest
            run_ci_tests
            ;;
        "coverage")
            check_nextest
            run_coverage_tests
            ;;
        "specific")
            check_nextest
            run_specific_tests
            ;;
        "doc")
            run_doc_tests
            ;;
        "report")
            check_nextest
            generate_reports
            ;;
        "all")
            run_all_tests
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            print_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
