#!/bin/bash

# Novalon后台管理系统 - 综合测试套件运行脚本
# 用途: 执行所有类型的测试

set -e

echo "========================================="
echo "  Novalon后台管理系统 - 综合测试套件"
echo "========================================="
echo ""

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

# 项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}错误: 未找到Python3环境${NC}"
    exit 1
fi

# 检查依赖
echo -e "${YELLOW}检查测试依赖...${NC}"
if ! python3 -c "import pytest" &> /dev/null; then
    echo -e "${YELLOW}正在安装测试依赖...${NC}"
    pip3 install -r requirements.txt
fi

# 解析命令行参数
TEST_TYPE="${1:-all}"
VERBOSE="${2:--v}"

# 测试类型映射
declare -A TEST_PATHS
TEST_PATHS["all"]="tests/"
TEST_PATHS["unit"]="tests/unit/"
TEST_PATHS["integration"]="tests/integration/"
TEST_PATHS["e2e"]="tests/e2e/"
TEST_PATHS["uat"]="tests/uat/"
TEST_PATHS["performance"]="tests/performance/"
TEST_PATHS["security"]="tests/security/"

# 显示帮助信息
show_help() {
    echo "用法: $0 [测试类型] [详细级别]"
    echo ""
    echo "测试类型:"
    echo "  all          - 运行所有测试 (默认)"
    echo "  unit         - 运行单元测试"
    echo "  integration  - 运行集成测试"
    echo "  e2e          - 运行端到端测试"
    echo "  uat          - 运行用户验收测试"
    echo "  performance  - 运行性能测试"
    echo "  security     - 运行安全测试"
    echo ""
    echo "详细级别:"
    echo "  -v           - 详细输出 (默认)"
    echo "  -vv          - 更详细输出"
    echo "  -s           - 显示打印输出"
    echo ""
    echo "示例:"
    echo "  $0 all -v            # 运行所有测试，详细输出"
    echo "  $0 integration -vv   # 运行集成测试，更详细输出"
    echo "  $0 uat -s            # 运行UAT测试，显示打印输出"
    echo ""
    echo "快速测试:"
    echo "  pytest -m smoke      # 运行冒烟测试"
    echo "  pytest -m critical   # 运行关键业务测试"
    echo "  pytest -m regression # 运行回归测试"
}

# 检查参数
if [[ "$TEST_TYPE" == "-h" ]] || [[ "$TEST_TYPE" == "--help" ]]; then
    show_help
    exit 0
fi

# 验证测试类型
if [[ ! -v TEST_PATHS[$TEST_TYPE] ]]; then
    echo -e "${RED}错误: 未知的测试类型 '$TEST_TYPE'${NC}"
    echo ""
    show_help
    exit 1
fi

TEST_PATH="${TEST_PATHS[$TEST_TYPE]}"

echo -e "${BLUE}测试类型: $TEST_TYPE${NC}"
echo -e "${BLUE}测试路径: $TEST_PATH${NC}"
echo -e "${BLUE}详细级别: $VERBOSE${NC}"
echo ""

# 设置环境变量
export PYTHONPATH="$PROJECT_ROOT:$PYTHONPATH"

# 创建测试报告目录
mkdir -p htmlcov/allure-results

# 运行测试
echo -e "${YELLOW}开始执行测试...${NC}"
echo ""

if [[ "$TEST_TYPE" == "all" ]]; then
    # 运行所有测试
    pytest "$TEST_PATH" \
        "$VERBOSE" \
        --strict-markers \
        --tb=short \
        --cov=. \
        --cov-report=html:htmlcov/all \
        --cov-report=term-missing \
        --alluredir=allure-results/all \
        --maxfail=10
else
    # 运行特定类型测试
    pytest "$TEST_PATH" \
        "$VERBOSE" \
        --strict-markers \
        --tb=short \
        --cov=. \
        --cov-report=html:htmlcov/$TEST_TYPE \
        --cov-report=term-missing \
        --alluredir=allure-results/$TEST_TYPE \
        --maxfail=5
fi

# 检查测试结果
TEST_EXIT_CODE=$?

echo ""
echo "========================================="
if [ $TEST_EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}✓ 测试全部通过!${NC}"
    echo ""
    echo "测试报告:"
    if [[ "$TEST_TYPE" == "all" ]]; then
        echo "  - HTML覆盖率报告: htmlcov/all/index.html"
        echo "  - Allure测试报告: allure-results/all/"
    else
        echo "  - HTML覆盖率报告: htmlcov/$TEST_TYPE/index.html"
        echo "  - Allure测试报告: allure-results/$TEST_TYPE/"
    fi
    echo ""
    echo "查看Allure报告:"
    if [[ "$TEST_TYPE" == "all" ]]; then
        echo "  allure serve allure-results/all"
    else
        echo "  allure serve allure-results/$TEST_TYPE"
    fi
else
    echo -e "${RED}✗ 测试失败!${NC}"
    echo ""
    echo "请检查测试日志并修复问题后重新运行。"
fi
echo "========================================="

exit $TEST_EXIT_CODE
