test_startup.py 4.42 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AIfeng/2025-07-02 14:01:37
启动测试脚本
用于验证同步版本的启动流程
"""

import os
import sys
import time

# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def test_directory_creation():
    """
    测试目录创建
    """
    print("测试目录创建...")
    
    required_dirs = ['logs', 'cache', 'temp', 'audio']
    
    for directory in required_dirs:
        if os.path.exists(directory):
            print(f"✓ 目录存在: {directory}")
        else:
            print(f"✗ 目录不存在: {directory}")
            try:
                os.makedirs(directory, exist_ok=True)
                print(f"✓ 已创建目录: {directory}")
            except Exception as e:
                print(f"✗ 创建目录失败 {directory}: {e}")
                return False
    
    return True

def test_utils_import():
    """
    测试utils模块导入
    """
    print("\n测试utils模块导入...")
    
    try:
        from utils import util
        print("✓ utils.util 导入成功")
        
        from utils.config_util import get_server_config, get_config
        print("✓ utils.config_util 导入成功")
        
        return True
    except Exception as e:
        print(f"✗ utils模块导入失败: {e}")
        return False

def test_core_modules_import():
    """
    测试核心模块导入
    """
    print("\n测试核心模块导入...")
    
    try:
        from recorder_sync import RecorderSync
        print("✓ RecorderSync 导入成功")
        
        from funasr_asr_sync import FunASRSync
        print("✓ FunASRSync 导入成功")
        
        from server_recording_api_sync import app
        print("✓ Flask应用 导入成功")
        
        return True
    except Exception as e:
        print(f"✗ 核心模块导入失败: {e}")
        return False

def test_config_loading():
    """
    测试配置加载
    """
    print("\n测试配置加载...")
    
    try:
        from utils.config_util import load_config, get_server_config
        
        config = load_config()
        print("✓ 配置文件加载成功")
        
        server_config = get_server_config()
        print(f"✓ 服务器配置: {server_config}")
        
        return True
    except Exception as e:
        print(f"✗ 配置加载失败: {e}")
        return False

def test_log_file_creation():
    """
    测试日志文件创建
    """
    print("\n测试日志文件创建...")
    
    try:
        from utils import util
        
        # 测试日志记录
        util.log(1, "测试日志消息", "TestUser")
        
        # 检查日志文件是否存在
        log_file = 'logs/system.log'
        if os.path.exists(log_file):
            print(f"✓ 日志文件创建成功: {log_file}")
            
            # 检查文件大小
            file_size = os.path.getsize(log_file)
            print(f"✓ 日志文件大小: {file_size} 字节")
            
            return True
        else:
            print(f"✗ 日志文件不存在: {log_file}")
            return False
            
    except Exception as e:
        print(f"✗ 日志文件创建失败: {e}")
        return False

def main():
    """
    主测试函数
    """
    print("eman_one 同步版本启动测试")
    print("=" * 40)
    
    tests = [
        ("目录创建", test_directory_creation),
        ("utils模块导入", test_utils_import),
        ("核心模块导入", test_core_modules_import),
        ("配置加载", test_config_loading),
        ("日志文件创建", test_log_file_creation)
    ]
    
    passed = 0
    total = len(tests)
    
    for test_name, test_func in tests:
        print(f"\n[{passed + 1}/{total}] {test_name}")
        print("-" * 30)
        
        try:
            if test_func():
                passed += 1
                print(f"✓ {test_name} 通过")
            else:
                print(f"✗ {test_name} 失败")
        except Exception as e:
            print(f"✗ {test_name} 异常: {e}")
    
    print("\n" + "=" * 40)
    print(f"测试结果: {passed}/{total} 通过")
    
    if passed == total:
        print("✓ 所有测试通过,可以启动main_sync.py")
        return True
    else:
        print("✗ 部分测试失败,请检查问题")
        return False

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