#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试重构后的应用
"""

import sys
import os

# 添加项目路径
sys.path.append('src')

def test_blueprints():
    """测试蓝图注册"""
    try:
        from src.web.app import app

        print("✓ Flask应用导入成功")

        # 测试蓝图注册
        blueprints = list(app.blueprints.keys())
        print(f"✓ 已注册蓝图: {blueprints}")

        expected_blueprints = [
            'alerts', 'workorders', 'conversations', 'knowledge',
            'monitoring', 'system', 'feishu_sync', 'core', 'auth',
            'agent', 'vehicle', 'analytics', 'test'
        ]

        for bp in expected_blueprints:
            if bp in blueprints:
                print(f"✓ 蓝图 {bp} 已注册")
            else:
                print(f"✗ 蓝图 {bp} 未注册")

        # 测试路由
        routes = [str(rule) for rule in app.url_map.iter_rules()]
        agent_routes = [r for r in routes if 'agent' in r]
        vehicle_routes = [r for r in routes if 'vehicle' in r]
        analytics_routes = [r for r in routes if 'analytics' in r]

        print(f"✓ Agent相关路由数量: {len(agent_routes)}")
        print(f"✓ Vehicle相关路由数量: {len(vehicle_routes)}")
        print(f"✓ Analytics相关路由数量: {len(analytics_routes)}")

        return True

    except Exception as e:
        print(f"✗ 应用测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_blueprint_imports():
    """测试蓝图模块导入"""
    blueprints_to_test = [
        ('src.web.blueprints.agent', 'agent_bp'),
        ('src.web.blueprints.vehicle', 'vehicle_bp'),
        ('src.web.blueprints.analytics', 'analytics_bp'),
        ('src.web.blueprints.test', 'test_bp'),
    ]

    for module_name, bp_name in blueprints_to_test:
        try:
            module = __import__(module_name, fromlist=[bp_name])
            bp = getattr(module, bp_name)
            print(f"✓ {module_name}.{bp_name} 导入成功")
        except Exception as e:
            print(f"✗ {module_name}.{bp_name} 导入失败: {e}")
            return False

    return True

if __name__ == '__main__':
    print("开始测试重构后的应用...")
    print("=" * 50)

    success = True

    print("\n1. 测试蓝图模块导入...")
    success &= test_blueprint_imports()

    print("\n2. 测试Flask应用和蓝图注册...")
    success &= test_blueprints()

    print("\n" + "=" * 50)
    if success:
        print("✓ 所有测试通过！重构成功！")
    else:
        print("✗ 部分测试失败，请检查代码")
        sys.exit(1)
