Files
LocalAgent/tests/test_config_refresh.py
Mimikko-zeus 8a538bb950 feat: refactor API key configuration and enhance application initialization
- Renamed `check_environment` to `check_api_key_configured` for clarity, simplifying the API key validation logic.
- Removed the blocking behavior of the API key check during application startup, allowing the app to run while providing a prompt for configuration.
- Updated `LocalAgentApp` to accept an `api_configured` parameter, enabling conditional messaging for API key setup.
- Enhanced the `SandboxRunner` to support backup management and improved execution result handling with detailed metrics.
- Integrated data governance strategies into the `HistoryManager`, ensuring compliance and improved data management.
- Added privacy settings and metrics tracking across various components to enhance user experience and application safety.
2026-02-27 14:32:30 +08:00

101 lines
3.2 KiB
Python

"""
测试配置刷新功能
验证配置变更后客户端单例是否正确重置
"""
import os
import sys
from pathlib import Path
# 添加项目根目录到路径
PROJECT_ROOT = Path(__file__).parent
sys.path.insert(0, str(PROJECT_ROOT))
from dotenv import load_dotenv, set_key
from llm.client import get_client, reset_client, test_connection, LLMClientError
def test_config_refresh():
"""测试配置刷新流程"""
env_path = PROJECT_ROOT / ".env"
print("=" * 60)
print("测试配置刷新功能")
print("=" * 60)
# 1. 加载初始配置
print("\n[步骤 1] 加载初始配置...")
load_dotenv(env_path)
initial_api_key = os.getenv("LLM_API_KEY", "")
print(f"初始 API Key: {initial_api_key[:10]}..." if initial_api_key else "未配置")
# 2. 获取客户端实例
print("\n[步骤 2] 获取客户端实例...")
try:
client1 = get_client()
print(f"✓ 客户端实例创建成功")
print(f" API URL: {client1.api_url}")
print(f" API Key: {client1.api_key[:10]}..." if client1.api_key else "未配置")
except LLMClientError as e:
print(f"✗ 客户端创建失败: {e}")
return
# 3. 模拟配置变更(这里只是演示,不实际修改)
print("\n[步骤 3] 模拟配置变更...")
print(" (实际场景中,用户在设置页修改并保存配置)")
# 4. 重置客户端单例
print("\n[步骤 4] 重置客户端单例...")
reset_client()
print("✓ 客户端单例已重置")
# 5. 重新获取客户端实例
print("\n[步骤 5] 重新获取客户端实例...")
try:
client2 = get_client()
print(f"✓ 新客户端实例创建成功")
print(f" API URL: {client2.api_url}")
print(f" API Key: {client2.api_key[:10]}..." if client2.api_key else "未配置")
# 验证是否是新实例
if client1 is client2:
print("✗ 警告: 客户端实例未更新(仍是旧实例)")
else:
print("✓ 确认: 客户端实例已更新(新实例)")
except LLMClientError as e:
print(f"✗ 新客户端创建失败: {e}")
return
# 6. 测试连接
print("\n[步骤 6] 测试 API 连接...")
success, message = test_connection(timeout=10)
if success:
print(f"{message}")
else:
print(f"{message}")
print("\n" + "=" * 60)
print("测试完成")
print("=" * 60)
# 7. 显示度量统计
print("\n[度量统计]")
try:
from llm.config_metrics import get_config_metrics
workspace = PROJECT_ROOT / "workspace"
metrics = get_config_metrics(workspace)
stats = metrics.get_statistics()
print(f"配置变更总次数: {stats['total_config_changes']}")
print(f"首次调用成功率: {stats['first_call_success_rate']:.1%}")
print(f"平均重试次数: {stats['avg_retry_count']:.2f}")
print(f"连接测试成功率: {stats['connection_test_success_rate']:.1%}")
except Exception as e:
print(f"无法获取度量统计: {e}")
if __name__ == "__main__":
test_config_refresh()