""" 测试 API 配置文件持久化 """ import sys import json from pathlib import Path # 添加 backend 到路径 sys.path.insert(0, 'backend') from backend.core.config import settings def test_config_persistence(): """测试配置文件持久化""" config_dir = Path(settings.DATA_PATH) / "apiconfig" print("=" * 60) print("API 配置文件持久化测试") print("=" * 60) print(f"\n配置文件目录: {config_dir}") print(f"目录存在: {config_dir.exists()}") if not config_dir.exists(): print("\n❌ 配置文件目录不存在!") return False # 列出所有配置文件 config_files = list(config_dir.glob("*.json")) print(f"\n找到 {len(config_files)} 个配置文件:") if len(config_files) == 0: print("\n⚠️ 没有保存的配置文件") print("提示:请在前端创建并保存至少一个配置文件") return False for config_file in config_files: print(f"\n{'-' * 60}") print(f"文件: {config_file.name}") try: with open(config_file, 'r', encoding='utf-8') as f: config = json.load(f) print(f" ID: {config.get('id', 'N/A')}") print(f" 名称: {config.get('name', 'N/A')}") print(f" 创建时间: {config.get('createdAt', 'N/A')}") apis = config.get('apis', {}) print(f" API 配置数量: {len(apis)}") for category, api_config in apis.items(): print(f"\n [{category}]") print(f" URL: {api_config.get('apiUrl', 'N/A')}") # 检查 API Key 是否已加密 api_key = api_config.get('apiKey', '') if api_key: if api_key == '****': print(f" API Key: ⚠️ 明文存储(不安全!)") elif len(api_key) > 50: # 加密后的 key 通常很长 print(f" API Key: ✓ 已加密") else: print(f" API Key: ? 未知格式") else: print(f" API Key: (空)") print(f" 模型: {api_config.get('model', 'N/A')}") print(f"\n✓ 配置文件格式正确") except Exception as e: print(f"\n❌ 读取配置文件失败: {e}") return False print(f"\n{'=' * 60}") print("✓ 测试通过:配置文件持久化正常工作") print("=" * 60) return True if __name__ == "__main__": success = test_config_persistence() sys.exit(0 if success else 1)