#!/usr/bin/env python3 """ 测试建议更新功能 这个脚本用于测试 config-man 的建议更新功能。 """ import sys import os import json import tempfile import subprocess # 添加项目根目录到Python路径 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from src.config_man.utils.mock_rclone import patch_rclone from src.config_man.utils.crypto import encrypt from src.config_man.core.suggest_update_command import SuggestUpdateCommand def create_test_configs(): """创建测试配置文件""" # 模拟目标版本配置 (0.29) target_config = { "Ver": "0.29.1abc123", "EventApiURL": "https://n3backend.azurewebsites.net/", "PlayFabTitle": "B066F", "Google_Play_URL": "https://play.google.com/store/apps/details?id=com.arkgame.ft", "APP_Store_URL": "https://apps.apple.com/us/app/id6505145935", "HeartBeat": "60", "RTMPid": "80000586", "RTMServerEndpoint": "rtm-intl-frontgate.ilivedata.com:13321", "RTMHmacSecret": "57047697437f4f2c97a835e8d9a53358", "FuncUrl": "https://leaderboardcreate.azurewebsites.net", "FuncKey": "R5kU45fNBRd52Eqp3tEqfpZqrbFw53uSSEo7wraUSqIfAzFuRmLm_w==" } # 模拟更新版本配置 (0.30) update_config = { "Ver": "0.30.1def456", "EventApiURL": "https://n3backend.azurewebsites.net/", "PlayFabTitle": "B066F", "Google_Play_URL": "https://play.google.com/store/apps/details?id=com.arkgame.ft", "APP_Store_URL": "https://apps.apple.com/us/app/id6505145935", "HeartBeat": "60", "RTMPid": "80000586", "RTMServerEndpoint": "rtm-intl-frontgate.ilivedata.com:13321", "RTMHmacSecret": "57047697437f4f2c97a835e8d9a53358", "FuncUrl": "https://leaderboardcreate.azurewebsites.net", "FuncKey": "R5kU45fNBRd52Eqp3tEqfpZqrbFw53uSSEo7wraUSqIfAzFuRmLm_w==" } return target_config, update_config def test_suggest_update(): """测试建议更新功能""" print("🧪 开始测试建议更新功能...") # 启用rclone模拟 patch_rclone() # 创建测试配置 target_config, update_config = create_test_configs() # 模拟存储测试配置 # 使用MockRclone类来管理测试数据 from src.config_man.utils.mock_rclone import MockRclone mock_rclone = MockRclone() # 创建测试配置文件 import os test_data_path = "test_data" storage_path = "ab" # 创建目标版本配置 (0.29) target_version_path = os.path.join(test_data_path, storage_path, "0_29") os.makedirs(target_version_path, exist_ok=True) # 保存Android配置 android_config_path = os.path.join(target_version_path, "androidconfig.json") with open(android_config_path, 'w', encoding='utf-8') as f: f.write(encrypt(json.dumps(target_config, indent=2, ensure_ascii=False))) # 保存iOS配置 ios_config_path = os.path.join(target_version_path, "iosconfig.json") with open(ios_config_path, 'w', encoding='utf-8') as f: f.write(encrypt(json.dumps(target_config, indent=2, ensure_ascii=False))) # 创建更新版本配置 (0.30) update_version_path = os.path.join(test_data_path, storage_path, "0_30") os.makedirs(update_version_path, exist_ok=True) # 保存Android配置 android_config_path = os.path.join(update_version_path, "androidconfig.json") with open(android_config_path, 'w', encoding='utf-8') as f: f.write(encrypt(json.dumps(update_config, indent=2, ensure_ascii=False))) # 保存iOS配置 ios_config_path = os.path.join(update_version_path, "iosconfig.json") with open(ios_config_path, 'w', encoding='utf-8') as f: f.write(encrypt(json.dumps(update_config, indent=2, ensure_ascii=False))) print("📁 已创建模拟配置文件") # 创建建议更新命令处理器 suggest_cmd = SuggestUpdateCommand("ab", "") # 测试Android平台 print("\n📱 测试Android平台...") success = suggest_cmd.execute("android", "0.29", "0.30") if success: print("✅ Android平台测试成功") # 验证结果 from src.config_man.utils.crypto import decrypt updated_config_path = os.path.join(test_data_path, storage_path, "0_29", "androidconfig.json") if os.path.exists(updated_config_path): with open(updated_config_path, 'r', encoding='utf-8') as f: encrypted_content = f.read().strip() decrypted_content = decrypt(encrypted_content) updated_config = json.loads(decrypted_content) if "Ver_New" in updated_config: print(f"✅ 已添加 Ver_New: {updated_config['Ver_New']}") print("✅ 配置对比功能正常显示") else: print("❌ 未找到 Ver_New 属性") else: print("❌ 更新后的配置文件不存在") else: print("❌ Android平台测试失败") # 测试iOS平台 print("\n📱 测试iOS平台...") success = suggest_cmd.execute("ios", "0.29", "0.30") if success: print("✅ iOS平台测试成功") # 验证结果 updated_config_path = os.path.join(test_data_path, storage_path, "0_29", "iosconfig.json") if os.path.exists(updated_config_path): with open(updated_config_path, 'r', encoding='utf-8') as f: encrypted_content = f.read().strip() decrypted_content = decrypt(encrypted_content) updated_config = json.loads(decrypted_content) if "Ver_New" in updated_config: print(f"✅ 已添加 Ver_New: {updated_config['Ver_New']}") print("✅ 配置对比功能正常显示") else: print("❌ 未找到 Ver_New 属性") else: print("❌ 更新后的配置文件不存在") else: print("❌ iOS平台测试失败") # 测试错误情况 print("\n🚫 测试错误情况...") # 测试无效平台 success = suggest_cmd.execute("invalid", "0.29", "0.30") if not success: print("✅ 无效平台测试通过") # 测试相同版本 success = suggest_cmd.execute("android", "0.29", "0.29") if not success: print("✅ 相同版本测试通过") # 测试无效版本格式 success = suggest_cmd.execute("android", "invalid", "0.30") if not success: print("✅ 无效版本格式测试通过") print("\n🎉 建议更新功能测试完成!") def test_cli_command(): """测试CLI命令""" print("\n🔧 测试CLI命令...") # 测试帮助信息 try: result = subprocess.run( ["python", "-m", "src.config_man.cli.main", "suggest-update", "--help"], capture_output=True, text=True, timeout=10 ) if result.returncode == 0: print("✅ CLI帮助信息正常") else: print("❌ CLI帮助信息异常") except Exception as e: print(f"❌ CLI命令测试失败: {e}") if __name__ == "__main__": test_suggest_update() test_cli_command()