#!/bin/bash
# DCU Performance Analyzer 测试脚本
# 用于验证工具的功能正确性和稳定性

set -e

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

# 工具路径
ANALYZER="./dist/dcu_analyzer"
PYTHON_ANALYZER="./dcu_performance_analyzer.py"

# 测试计数
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 日志文件
TEST_LOG="test_results.log"

# 函数定义
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1" | tee -a "$TEST_LOG"
}

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

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

run_test() {
    local test_name="$1"
    local test_cmd="$2"
    local expected_result="$3"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    log_info "运行测试: $test_name"
    echo "命令: $test_cmd" | tee -a "$TEST_LOG"
    
    if eval "$test_cmd"; then
        if [ "$expected_result" = "pass" ]; then
            log_info "✓ $test_name 通过"
            PASSED_TESTS=$((PASSED_TESTS + 1))
            return 0
        else
            log_error "✗ $test_name 失败 (期望失败但通过了)"
            FAILED_TESTS=$((FAILED_TESTS + 1))
            return 1
        fi
    else
        if [ "$expected_result" = "fail" ]; then
            log_info "✓ $test_name 通过 (期望失败)"
            PASSED_TESTS=$((PASSED_TESTS + 1))
            return 0
        else
            log_error "✗ $test_name 失败"
            FAILED_TESTS=$((FAILED_TESTS + 1))
            return 1
        fi
    fi
}

check_file_exists() {
    local file="$1"
    local description="$2"
    
    if [ -f "$file" ]; then
        log_info "✓ $description 文件存在: $file"
        return 0
    else
        log_error "✗ $description 文件不存在: $file"
        return 1
    fi
}

check_directory_exists() {
    local dir="$1"
    local description="$2"
    
    if [ -d "$dir" ]; then
        log_info "✓ $description 目录存在: $dir"
        return 0
    else
        log_error "✗ $description 目录不存在: $dir"
        return 1
    fi
}

# 清理函数
cleanup() {
    log_info "清理测试文件..."
    rm -rf dcu_analysis_* test_output 2>/dev/null || true
}

# 主测试函数
main() {
    echo "=========================================" | tee "$TEST_LOG"
    echo "DCU Performance Analyzer 测试套件" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    echo "测试开始时间: $(date)" | tee -a "$TEST_LOG"
    echo "" | tee -a "$TEST_LOG"
    
    # 清理之前的测试文件
    cleanup
    
    # 测试1: Python版本帮助信息
    run_test "Python版本帮助信息" \
        "python3 $PYTHON_ANALYZER --help > /dev/null 2>&1" \
        "pass"
    
    # 测试2: Python版本版本信息
    run_test "Python版本版本信息" \
        "python3 $PYTHON_ANALYZER --version > /dev/null 2>&1" \
        "pass"
    
    # 测试3: 可执行文件帮助信息
    if [ -f "$ANALYZER" ]; then
        run_test "可执行文件帮助信息" \
            "$ANALYZER --help > /dev/null 2>&1" \
            "pass"
        
        # 测试4: 可执行文件版本信息
        run_test "可执行文件版本信息" \
            "$ANALYZER --version > /dev/null 2>&1" \
            "pass"
    else
        log_warning "可执行文件不存在，跳过相关测试"
    fi
    
    # 测试5: 基本功能测试 (Python版本)
    run_test "基本功能测试 (Python)" \
        "python3 $PYTHON_ANALYZER -c system performance -o test_output > /dev/null 2>&1" \
        "pass"
    
    # 验证输出文件
    if check_directory_exists "test_output" "输出目录"; then
        check_file_exists "test_output/reports/analysis_report.json" "JSON报告"
        check_file_exists "test_output/reports/analysis_summary.txt" "文本摘要"
        check_file_exists "test_output/data/system_info.json" "系统信息"
        check_file_exists "test_output/data/performance_metrics.json" "性能指标"
        check_file_exists "test_output/logs/analysis.log" "分析日志"
    fi
    
    # 测试6: 数据打包功能
    if [ -d "test_output" ]; then
        run_test "数据打包功能" \
            "python3 $PYTHON_ANALYZER -c system -o test_package > /dev/null 2>&1 && ls test_package.tar.gz > /dev/null 2>&1" \
            "pass"
    fi
    
    # 测试7: 静默模式
    run_test "静默模式" \
        "python3 $PYTHON_ANALYZER -c system -q -o test_quiet > /dev/null 2>&1" \
        "pass"
    
    # 测试8: 调试模式
    run_test "调试模式" \
        "python3 $PYTHON_ANALYZER -c system -d -o test_debug > /dev/null 2>&1" \
        "pass"
    
    # 测试9: 错误处理 (无效模块)
    run_test "错误处理 (无效模块)" \
        "python3 $PYTHON_ANALYZER -c invalid_module -o test_error 2>&1 | grep -q 'error'" \
        "fail"
    
    # 测试10: 单元测试
    if [ -f "test_analyzer.py" ]; then
        run_test "单元测试" \
            "python3 -m pytest test_analyzer.py -v > /dev/null 2>&1 || python3 test_analyzer.py" \
            "pass"
    else
        log_warning "单元测试文件不存在，跳过单元测试"
    fi
    
    # 测试11: 性能测试 (检查执行时间)
    log_info "运行性能测试..."
    start_time=$(date +%s)
    python3 $PYTHON_ANALYZER -c system performance -o perf_test > /dev/null 2>&1
    end_time=$(date +%s)
    execution_time=$((end_time - start_time))
    
    if [ $execution_time -lt 30 ]; then
        log_info "✓ 性能测试通过 (执行时间: ${execution_time}秒)"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        log_error "✗ 性能测试失败 (执行时间过长: ${execution_time}秒)"
        FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
    
    # 测试结果统计
    echo "" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    echo "测试完成时间: $(date)" | tee -a "$TEST_LOG"
    echo "" | tee -a "$TEST_LOG"
    echo "测试结果统计:" | tee -a "$TEST_LOG"
    echo "  总测试数: $TOTAL_TESTS" | tee -a "$TEST_LOG"
    echo "  通过: $PASSED_TESTS" | tee -a "$TEST_LOG"
    echo "  失败: $FAILED_TESTS" | tee -a "$TEST_LOG"
    echo "  通过率: $(echo "scale=1; $PASSED_TESTS * 100 / $TOTAL_TESTS" | bc -l 2>/dev/null || echo "N/A")%" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    
    # 清理
    cleanup
    
    # 返回结果
    if [ $FAILED_TESTS -eq 0 ]; then
        log_info "所有测试通过!"
        exit 0
    else
        log_error "部分测试失败!"
        exit 1
    fi
}

# 检查依赖
log_info "检查测试依赖..."
if ! command -v python3 &> /dev/null; then
    log_error "Python3 未安装"
    exit 1
fi

if ! command -v bc &> /dev/null; then
    log_warning "bc 计算器未安装，无法显示精确通过率"
fi

# 运行主函数
main