#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
测试集成启动器
"""

import sys
import os
from pathlib import Path

# 添加项目路径
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))

def test_integrated_launcher():
    """测试集成启动器"""
    print("测试集成启动器...")
    
    try:
        # 添加packaging目录到路径
        packaging_dir = project_root / "packaging"
        if str(packaging_dir) not in sys.path:
            sys.path.insert(0, str(packaging_dir))
        
        # 切换到packaging目录
        original_cwd = os.getcwd()
        os.chdir(str(packaging_dir))
        
        # 导入启动器
        import integrated_launcher
        
        launcher = integrated_launcher.IntegratedLauncher()
        
        # 测试配置检查
        print("测试配置文件检查...")
        has_complete_config = launcher.check_config_complete()
        print(f"配置完整性检查结果: {has_complete_config}")
        
        # 恢复工作目录
        os.chdir(original_cwd)
        
        print("✓ 集成启动器导入成功")
        return True
        
    except ImportError as e:
        print(f"✗ 导入失败: {e}")
        return False
    except Exception as e:
        print(f"✗ 测试异常: {e}")
        return False

def test_config_gui():
    """测试配置GUI"""
    print("测试配置GUI...")
    
    try:
        # 添加packaging目录到路径
        packaging_dir = project_root / "packaging"
        if str(packaging_dir) not in sys.path:
            sys.path.insert(0, str(packaging_dir))
        
        # 切换到packaging目录
        original_cwd = os.getcwd()
        os.chdir(str(packaging_dir))
        
        # 导入配置GUI
        from config_tool.integrated_config_gui import IntegratedConfigGUI
        
        def dummy_callback(config_data):
            print(f"测试回调: {config_data}")
        
        # 创建GUI实例但不启动
        gui = IntegratedConfigGUI(dummy_callback)
        
        # 恢复工作目录
        os.chdir(original_cwd)
        
        print("✓ 配置GUI创建成功")
        return True
        
    except ImportError as e:
        print(f"✗ GUI导入失败: {e}")
        return False
    except Exception as e:
        print(f"✗ GUI测试异常: {e}")
        return False

def main():
    """主测试函数"""
    print("VWED集成启动器测试")
    print("=" * 40)
    
    tests = [
        ("集成启动器", test_integrated_launcher),
        ("配置GUI", test_config_gui)
    ]
    
    results = []
    for test_name, test_func in tests:
        print(f"\n=== {test_name} ===")
        try:
            result = test_func()
            results.append((test_name, result))
        except Exception as e:
            print(f"测试 {test_name} 发生异常: {e}")
            results.append((test_name, False))
    
    # 总结
    print("\n" + "=" * 40)
    print("测试结果:")
    
    all_passed = True
    for test_name, result in results:
        status = "✓ 通过" if result else "✗ 失败"
        print(f"{test_name}: {status}")
        if not result:
            all_passed = False
    
    if all_passed:
        print("\n🎉 所有测试通过！可以进行集成打包。")
        print("运行命令: packaging/scripts/build_integrated.bat")
        return 0
    else:
        print("\n❌ 部分测试失败，请检查并解决问题。")
        return 1

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