125 lines
3.3 KiB
Python
125 lines
3.3 KiB
Python
"""核心模块测试"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from minenasai.core import Settings, get_settings, load_config, reset_settings
|
|
from minenasai.core.config import expand_path
|
|
|
|
|
|
class TestConfig:
|
|
"""配置模块测试"""
|
|
|
|
def test_default_settings(self):
|
|
"""测试默认配置"""
|
|
settings = Settings()
|
|
|
|
assert settings.app.name == "MineNASAI"
|
|
assert settings.app.version == "0.1.0"
|
|
assert settings.gateway.port == 8000
|
|
assert settings.webtui.port == 8080
|
|
|
|
def test_expand_path(self):
|
|
"""测试路径展开"""
|
|
import os
|
|
|
|
home = os.path.expanduser("~")
|
|
path = expand_path("~/test")
|
|
|
|
assert str(path).startswith(home)
|
|
|
|
def test_load_config_no_file(self, tmp_path):
|
|
"""测试配置文件不存在时使用默认值"""
|
|
config_path = tmp_path / "nonexistent.json5"
|
|
settings = load_config(config_path)
|
|
|
|
assert settings.app.name == "MineNASAI"
|
|
|
|
def test_load_config_with_file(self, tmp_path):
|
|
"""测试从文件加载配置"""
|
|
import json5
|
|
|
|
config_path = tmp_path / "config.json5"
|
|
config_data = {
|
|
"app": {"name": "TestApp", "debug": True},
|
|
"gateway": {"port": 9000},
|
|
}
|
|
|
|
with open(config_path, "w", encoding="utf-8") as f:
|
|
json5.dump(config_data, f)
|
|
|
|
settings = load_config(config_path)
|
|
|
|
assert settings.app.name == "TestApp"
|
|
assert settings.app.debug is True
|
|
assert settings.gateway.port == 9000
|
|
|
|
def test_get_settings_singleton(self):
|
|
"""测试全局配置单例"""
|
|
reset_settings()
|
|
|
|
settings1 = get_settings()
|
|
settings2 = get_settings()
|
|
|
|
assert settings1 is settings2
|
|
|
|
def test_env_override(self, monkeypatch):
|
|
"""测试环境变量覆盖"""
|
|
# Settings 使用 MINENASAI_ 前缀
|
|
monkeypatch.setenv("MINENASAI_ANTHROPIC_API_KEY", "test-key")
|
|
|
|
reset_settings()
|
|
settings = Settings()
|
|
|
|
assert settings.anthropic_api_key == "test-key"
|
|
|
|
|
|
class TestLogging:
|
|
"""日志模块测试"""
|
|
|
|
def test_setup_logging(self):
|
|
"""测试日志初始化"""
|
|
from minenasai.core import setup_logging
|
|
from minenasai.core.config import LoggingConfig
|
|
|
|
config = LoggingConfig(level="DEBUG", format="console", file=False)
|
|
setup_logging(config)
|
|
|
|
# 应该不抛出异常
|
|
|
|
def test_get_logger(self):
|
|
"""测试获取日志记录器"""
|
|
from minenasai.core import get_logger
|
|
|
|
logger = get_logger("test")
|
|
|
|
assert logger is not None
|
|
|
|
def test_audit_logger(self, tmp_path):
|
|
"""测试审计日志"""
|
|
from minenasai.core.logging import AuditLogger
|
|
|
|
audit_path = tmp_path / "audit.jsonl"
|
|
audit = AuditLogger(audit_path)
|
|
|
|
audit.log_tool_call(
|
|
agent_id="test-agent",
|
|
tool_name="read",
|
|
params={"path": "/test"},
|
|
danger_level="safe",
|
|
result="success",
|
|
duration_ms=100,
|
|
)
|
|
|
|
assert audit_path.exists()
|
|
|
|
with open(audit_path, encoding="utf-8") as f:
|
|
record = json.loads(f.readline())
|
|
|
|
assert record["agent_id"] == "test-agent"
|
|
assert record["tool_name"] == "read"
|
|
assert record["danger_level"] == "safe"
|