#!/bin/bash
# Copyright 2025 Huawei Technologies Co., Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
# ===========================================================================

# 系统测试执行脚本
# 用于执行 ascend-deployer 的系统测试

set -e

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"

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

# 打印帮助信息
print_help() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -h, --help              显示帮助信息"
    echo "  -c, --coverage          生成覆盖率报告"
    echo ""
    echo "Examples:"
    echo "  $0                      # 运行所有测试"
    echo "  $0 -c                   # 运行测试并生成覆盖率报告"
}

# 检查并安装必要的依赖
install_dependencies() {
    echo -e "${YELLOW}=== 检查必要依赖 ===${NC}"
    
    # 从 test/requirements.txt 安装依赖
    echo -e "${GREEN}正在从 test/requirements.txt 安装依赖...${NC}"
    pip3 install -r "${PROJECT_ROOT}/test/requirements.txt" -i https://mirrors.aliyun.com/pypi/simple/
    
    # 如果需要覆盖率报告,确保 pytest-cov 已安装
    if [ "$COVERAGE" != "" ]; then
        if ! python3 -m pytest --cov --version &> /dev/null; then
            echo -e "${GREEN}正在安装 pytest-cov...${NC}"
            pip3 install pytest-cov
        fi
    fi
    
    echo -e "${GREEN}依赖检查完成${NC}"
    echo ""
}

# 默认参数
COVERAGE=""

# 解析参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            print_help
            exit 0
            ;;
        -c|--coverage)
            COVERAGE="--cov=ascend_deployer --cov-report=html --cov-report=term"
            shift
            ;;
        *)
            echo -e "${RED}Error: Unknown option: $1${NC}"
            print_help
            exit 1
            ;;
    esac
done

# 切换到项目根目录
cd "${PROJECT_ROOT}"

echo -e "${GREEN}=== 开始执行系统测试 ===${NC}"
echo "项目根目录: ${PROJECT_ROOT}"
echo "测试目录: ${SCRIPT_DIR}"
echo ""

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

# 获取Python版本
PYTHON_VERSION=$(python3 --version 2>&1 | awk '{print $2}')
echo -e "${YELLOW}Python 版本:${PYTHON_VERSION}${NC}"

# 检查Python版本是否至少为 3.11
# 解析版本号的主版本和次版本
MAJOR_VERSION=$(echo "$PYTHON_VERSION" | cut -d '.' -f 1)
MINOR_VERSION=$(echo "$PYTHON_VERSION" | cut -d '.' -f 2)

if [ "$MAJOR_VERSION" -lt 3 ] || ([ "$MAJOR_VERSION" -eq 3 ] && [ "$MINOR_VERSION" -lt 11 ]); then
    echo -e "${RED}Error: Python 版本过低${NC}"
    echo -e "${RED}当前版本: ${PYTHON_VERSION}, 最低要求: 3.11${NC}"
    exit 1
fi

echo ""

# 安装必要的依赖
install_dependencies

# 构建pytest命令
# 运行所有系统测试
TEST_FILES=""
for test_file in ${SCRIPT_DIR}/test_*.py; do
    if [ -f "$test_file" ]; then
        TEST_FILES="${TEST_FILES} $test_file"
    fi
done

if [ -z "$TEST_FILES" ]; then
    echo -e "${RED}Error: 未找到测试文件${NC}"
    exit 1
fi

PYTEST_CMD="python3 -m pytest ${TEST_FILES}"

if [ "$COVERAGE" != "" ]; then
    PYTEST_CMD="${PYTEST_CMD} ${COVERAGE}"
fi

# 执行测试
echo -e "${GREEN}执行命令: ${PYTEST_CMD}${NC}"
echo ""

if $PYTEST_CMD; then
    echo ""
    echo -e "${GREEN}=== 测试执行成功 ===${NC}"
    
    # 如果生成了覆盖率报告
    if [ "$COVERAGE" != "" ]; then
        echo -e "${GREEN}覆盖率报告已生成: ${PROJECT_ROOT}/htmlcov/index.html${NC}"
    fi
    
    exit 0
else
    echo ""
    echo -e "${RED}=== 测试执行失败 ===${NC}"
    exit 1
fi