#!/usr/bin/env python3
"""
测试连接显示修复是否成功
"""

import sys
from pathlib import Path

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


def test_simple_client_display():
    """测试SimpleChatClient的连接显示"""
    print("🔍 测试SimpleChatClient连接显示")
    print("=" * 40)
    
    try:
        from client.main import SimpleChatClient
        
        # 创建客户端（不传入参数，使用配置文件默认值）
        client = SimpleChatClient()
        
        host = client.chat_client.network_client.host
        port = client.chat_client.network_client.port
        
        print(f"✅ 客户端主机: {host}")
        print(f"✅ 客户端端口: {port}")
        
        if host == "47.116.210.212" and port == 8888:
            print("✅ SimpleChatClient正确使用配置文件中的服务器地址")
            return True
        else:
            print(f"❌ SimpleChatClient未使用配置文件中的服务器地址")
            print(f"   期望: 47.116.210.212:8888")
            print(f"   实际: {host}:{port}")
            return False
            
    except Exception as e:
        print(f"❌ SimpleChatClient测试失败: {e}")
        return False


def test_tui_app_display():
    """测试TUI应用的连接显示"""
    print("\n🔍 测试TUI应用连接显示")
    print("=" * 40)
    
    try:
        from client.ui.app import ChatRoomApp
        
        # 创建TUI应用（不传入参数，使用配置文件默认值）
        app = ChatRoomApp()
        
        host = app.host
        port = app.port
        
        print(f"✅ TUI应用主机: {host}")
        print(f"✅ TUI应用端口: {port}")
        
        if host == "47.116.210.212" and port == 8888:
            print("✅ TUI应用正确使用配置文件中的服务器地址")
            return True
        else:
            print(f"❌ TUI应用未使用配置文件中的服务器地址")
            print(f"   期望: 47.116.210.212:8888")
            print(f"   实际: {host}:{port}")
            return False
            
    except Exception as e:
        print(f"❌ TUI应用测试失败: {e}")
        return False


def test_command_line_args():
    """测试命令行参数传递"""
    print("\n🔍 测试命令行参数传递")
    print("=" * 40)
    
    try:
        from client.main import SimpleChatClient
        
        # 测试传入自定义参数
        custom_host = "192.168.1.100"
        custom_port = 9999
        
        client = SimpleChatClient(custom_host, custom_port)
        
        host = client.chat_client.network_client.host
        port = client.chat_client.network_client.port
        
        print(f"✅ 自定义主机: {host}")
        print(f"✅ 自定义端口: {port}")
        
        if host == custom_host and port == custom_port:
            print("✅ 命令行参数正确传递")
            return True
        else:
            print(f"❌ 命令行参数传递失败")
            print(f"   期望: {custom_host}:{custom_port}")
            print(f"   实际: {host}:{port}")
            return False
            
    except Exception as e:
        print(f"❌ 命令行参数测试失败: {e}")
        return False


def test_config_file_reading():
    """测试配置文件读取"""
    print("\n🔍 测试配置文件读取")
    print("=" * 40)
    
    try:
        from client.config.client_config import get_client_config
        
        config = get_client_config()
        host = config.get_default_host()
        port = config.get_default_port()
        
        print(f"✅ 配置文件主机: {host}")
        print(f"✅ 配置文件端口: {port}")
        
        if host == "47.116.210.212" and port == 8888:
            print("✅ 配置文件读取正确")
            return True
        else:
            print(f"❌ 配置文件读取错误")
            print(f"   期望: 47.116.210.212:8888")
            print(f"   实际: {host}:{port}")
            return False
            
    except Exception as e:
        print(f"❌ 配置文件读取测试失败: {e}")
        return False


def main():
    """主函数"""
    print("🚀 连接显示修复测试")
    print("=" * 50)
    
    # 运行所有测试
    tests = [
        ("配置文件读取", test_config_file_reading),
        ("SimpleChatClient显示", test_simple_client_display),
        ("TUI应用显示", test_tui_app_display),
        ("命令行参数传递", test_command_line_args)
    ]
    
    results = []
    for test_name, test_func in tests:
        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📊 测试结果汇总")
    print("=" * 50)
    
    all_passed = True
    for test_name, result in results:
        status = "✅ 通过" if result else "❌ 失败"
        print(f"{test_name}: {status}")
        if not result:
            all_passed = False
    
    print("\n" + "=" * 50)
    if all_passed:
        print("🎉 所有测试通过！连接显示修复成功。")
        print("💡 现在客户端启动时会显示正确的服务器地址。")
    else:
        print("❌ 部分测试失败，请检查修复。")
    
    return all_passed


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)
