#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
快速运行测试脚本
提供便捷的测试运行命令
"""

import sys
import os
import subprocess
from pathlib import Path


def run_command(cmd, description):
    """运行命令并显示结果"""
    print(f"\n{'='*60}")
    print(f"🚀 {description}")
    print(f"{'='*60}\n")
    
    result = subprocess.run(cmd, shell=True)
    
    if result.returncode == 0:
        print(f"\n✅ {description} - 成功")
    else:
        print(f"\n❌ {description} - 失败")
    
    return result.returncode


def main():
    """主函数"""
    # 切换到项目根目录
    project_root = Path(__file__).parent.parent
    os.chdir(project_root)
    
    print(f"📁 工作目录: {os.getcwd()}")
    
    # 检查是否安装了测试依赖
    print("\n📦 检查测试依赖...")
    try:
        import pytest
        import pytest_asyncio
        print("✅ 测试依赖已安装")
    except ImportError:
        print("❌ 缺少测试依赖，正在安装...")
        subprocess.run([
            sys.executable, "-m", "pip", "install", 
            "-r", "Semantic_Logic_Test/requirements_test.txt"
        ])
    
    # 显示菜单
    print("\n" + "="*60)
    print("🧪 语义逻辑审查模块测试套件")
    print("="*60)
    print("\n请选择要运行的测试:")
    print("  1. 运行所有测试")
    print("  2. 运行所有测试（详细输出）")
    print("  3. 运行基础功能测试")
    print("  4. 运行边界情况测试")
    print("  5. 运行测试并生成覆盖率报告")
    print("  6. 运行测试并生成 HTML 报告")
    print("  7. 只运行失败的测试")
    print("  8. 运行特定测试（手动输入）")
    print("  0. 退出")
    
    choice = input("\n请输入选项 (0-8): ").strip()
    
    test_file = "Semantic_Logic_Test/test_semantic_logic.py"
    
    if choice == "1":
        return run_command(
            f"pytest {test_file} -v",
            "运行所有测试"
        )
    
    elif choice == "2":
        return run_command(
            f"pytest {test_file} -v -s",
            "运行所有测试（详细输出）"
        )
    
    elif choice == "3":
        return run_command(
            f"pytest {test_file}::TestSemanticLogicReviewer -v",
            "运行基础功能测试"
        )
    
    elif choice == "4":
        return run_command(
            f"pytest {test_file}::TestEdgeCases -v",
            "运行边界情况测试"
        )
    
    elif choice == "5":
        return run_command(
            f"pytest {test_file} --cov=core.construction_review.component.reviewers.semantic_logic --cov-report=html --cov-report=term",
            "运行测试并生成覆盖率报告"
        )
    
    elif choice == "6":
        return run_command(
            f"pytest {test_file} --html=Semantic_Logic_Test/report.html --self-contained-html",
            "运行测试并生成 HTML 报告"
        )
    
    elif choice == "7":
        return run_command(
            f"pytest {test_file} --lf -v",
            "只运行失败的测试"
        )
    
    elif choice == "8":
        test_name = input("请输入测试方法名称（例如: test_reviewer_initialization）: ").strip()
        return run_command(
            f"pytest {test_file}::TestSemanticLogicReviewer::{test_name} -v -s",
            f"运行测试: {test_name}"
        )
    
    elif choice == "0":
        print("\n👋 再见！")
        return 0
    
    else:
        print("\n❌ 无效的选项")
        return 1


if __name__ == "__main__":
    sys.exit(main())

