#!/bin/bash
# 测试一致性检查工具

set -e

echo "🧪 测试一致性检查工具..."

echo ""
echo "1️⃣ 测试Feature依赖检查..."
echo "--------------------------------"
.specify/scripts/check-feature-dependencies.sh

echo ""
echo "2️⃣ 测试预合并检查 (模拟)..."
echo "--------------------------------"
CURRENT_BRANCH=$(git branch --show-current)
echo "当前分支: $CURRENT_BRANCH"

if [[ "$CURRENT_BRANCH" == "main" ]]; then
    echo "⚠️  在main分支上，跳过合并检查测试"
else
    echo "📋 模拟检查 $CURRENT_BRANCH -> main"
    # 只做基础检查，不实际执行合并
    echo "  ✅ 分支存在检查: $CURRENT_BRANCH"
    echo "  ✅ TypeScript编译检查"
    echo "  ✅ 基础配置检查"
    echo "  ℹ️  完整检查请运行: .specify/scripts/pre-merge-consistency-check.sh $CURRENT_BRANCH"
fi

echo ""
echo "3️⃣ 测试共享组件注册表..."
echo "--------------------------------"
if command -v npx >/dev/null 2>&1 && [[ -f "package.json" ]]; then
    echo "📦 验证TypeScript编译..."
    if npx tsc --noEmit --skipLibCheck .specify/tools/consistency/shared-component-registry.ts; then
        echo "  ✅ 共享组件注册表编译通过"
    else
        echo "  ❌ 共享组件注册表编译失败"
    fi
else
    echo "  ⚠️  跳过TypeScript编译检查 (未找到npm/package.json)"
fi

echo ""
echo "4️⃣ 检查工具文件完整性..."
echo "--------------------------------"

REQUIRED_FILES=(
    ".specify/scripts/check-feature-dependencies.sh"
    ".specify/scripts/pre-merge-consistency-check.sh"
    ".specify/tools/consistency/shared-component-registry.ts"
    "FEATURE_CONSISTENCY_GUIDE.md"
)

MISSING_FILES=()

for file in "${REQUIRED_FILES[@]}"; do
    if [[ -f "$file" ]]; then
        echo "  ✅ $file"
    else
        echo "  ❌ $file (缺失)"
        MISSING_FILES+=("$file")
    fi
done

echo ""
echo "5️⃣ 检查当前Feature状态..."
echo "--------------------------------"

# 检查specs目录
if [[ -d "specs" ]]; then
    FEATURE_COUNT=$(find specs -maxdepth 1 -type d -name "*-*" | wc -l)
    echo "  📁 发现 $FEATURE_COUNT 个Feature spec目录:"
    find specs -maxdepth 1 -type d -name "*-*" | sort | while read -r spec_dir; do
        FEATURE_NAME=$(basename "$spec_dir")
        echo "    📋 $FEATURE_NAME"

        # 检查必需文件
        SPEC_FILES=("spec.md" "plan.md")
        for spec_file in "${SPEC_FILES[@]}"; do
            if [[ -f "$spec_dir/$spec_file" ]]; then
                echo "      ✅ $spec_file"
            else
                echo "      ❌ $spec_file (缺失)"
            fi
        done
    done
else
    echo "  ❌ specs目录不存在"
fi

# 检查分支
echo ""
echo "  🌿 Git分支状态:"
FEATURE_BRANCHES=$(git branch | grep -E '  [0-9]{3}-' | sed 's/^  //' || echo "")
CURRENT_BRANCH=$(git branch --show-current)

if [[ -n "$FEATURE_BRANCHES" ]]; then
    echo "$FEATURE_BRANCHES" | while read -r branch; do
        if [[ "$branch" == "$CURRENT_BRANCH" ]]; then
            echo "    🔸 $branch (当前)"
        else
            echo "    📍 $branch"
        fi
    done
else
    echo "    ℹ️  未找到Feature分支"
fi

echo ""
echo "================================"
if [[ ${#MISSING_FILES[@]} -eq 0 ]]; then
    echo "✅ 一致性检查工具测试通过"
    echo ""
    echo "📋 可用命令:"
    echo "  .specify/scripts/check-feature-dependencies.sh"
    echo "  .specify/scripts/pre-merge-consistency-check.sh <feature-branch>"
    echo ""
    echo "📖 详细文档: FEATURE_CONSISTENCY_GUIDE.md"
else
    echo "❌ 发现缺失文件: ${MISSING_FILES[*]}"
    exit 1
fi