沙箱 v2 - 多层隔离: - AST 静态分析: 30+ 危险模式检测 - 子进程隔离: 独立进程执行 + 资源限制 - 网络阻断: socket 模块替换 - 模块白名单: 30 个安全模块 - 文件系统限制: 临时目录隔离 - SandboxResult 数据类 + 执行历史 测试套件 (8 个模块, 192 个用例): - test_sandbox.py: AST检查(47) + 沙箱执行(15) + 安全拒绝(20) + 超时(2) + 模式对比(3) - test_conversation.py: 响应解析(13) + 思考标记(5) + 工具提取(10) - test_config.py: 配置加载/保存/缓存/损坏处理(8) - test_history.py: 历史CRUD/持久化/边界(12) - test_engines.py: 引擎管理/加载/切换(14) - test_plugins.py: code_editor(10) + code_search(7) + git_tools(4) + tool_manager(7) - test_integration.py: 沙箱+对话(2) + 历史+对话(2) + 安全链(3) + 提示词(1)
138 lines
3.8 KiB
Python
138 lines
3.8 KiB
Python
"""配置管理测试"""
|
|
import os
|
|
import sys
|
|
import json
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from xiaoli import config as cfg
|
|
|
|
|
|
class TestConfig:
|
|
"""配置加载/保存测试"""
|
|
|
|
def setup_method(self):
|
|
"""重置缓存"""
|
|
cfg._cache = None
|
|
|
|
def test_default_config(self):
|
|
"""无配置文件时使用默认值"""
|
|
# 临时指向不存在的文件
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = '/tmp/nonexistent_config.json'
|
|
cfg._cache = None
|
|
|
|
result = cfg.load()
|
|
assert 'system' in result
|
|
assert result['system']['default_engine'] == 'ollama'
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_load_existing_config(self, config_file):
|
|
"""加载现有配置文件"""
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_file
|
|
cfg._cache = None
|
|
|
|
result = cfg.load()
|
|
assert result['system']['default_engine'] == 'ollama'
|
|
assert result['system']['max_history'] == 100
|
|
assert 'mimo' in result['api']['engines']
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_get_specific_key(self, config_file):
|
|
"""获取特定配置项"""
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_file
|
|
cfg._cache = None
|
|
|
|
assert cfg.get('default_engine') == 'ollama'
|
|
assert cfg.get('max_history') == 100
|
|
assert cfg.get('nonexistent', 'fallback') == 'fallback'
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_set_and_save(self, tmp_dir):
|
|
"""设置并保存配置"""
|
|
config_path = os.path.join(tmp_dir, "config.json")
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_path
|
|
cfg._cache = None
|
|
|
|
cfg.set('default_engine', 'mimo')
|
|
assert cfg.get('default_engine') == 'mimo'
|
|
|
|
# 验证文件持久化
|
|
with open(config_path, 'r') as f:
|
|
saved = json.load(f)
|
|
assert saved['system']['default_engine'] == 'mimo'
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_reload(self, config_file):
|
|
"""强制重新加载"""
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_file
|
|
cfg._cache = None
|
|
|
|
cfg.load()
|
|
assert cfg._cache is not None
|
|
|
|
reloaded = cfg.reload()
|
|
assert cfg._cache is not None
|
|
assert 'system' in reloaded
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_caching(self, config_file):
|
|
"""配置缓存机制"""
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_file
|
|
cfg._cache = None
|
|
|
|
r1 = cfg.load()
|
|
r2 = cfg.load()
|
|
assert r1 is r2 # 同一对象(缓存)
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_corrupted_json(self, tmp_dir):
|
|
"""损坏的 JSON 文件处理"""
|
|
config_path = os.path.join(tmp_dir, "config.json")
|
|
with open(config_path, 'w') as f:
|
|
f.write("{invalid json")
|
|
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_path
|
|
cfg._cache = None
|
|
|
|
result = cfg.load()
|
|
assert 'system' in result # 应回退到默认值
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|
|
|
|
def test_missing_system_section(self, tmp_dir):
|
|
"""缺少 system 节时的行为"""
|
|
config_path = os.path.join(tmp_dir, "config.json")
|
|
with open(config_path, 'w') as f:
|
|
json.dump({"api": {}}, f)
|
|
|
|
original = cfg.CONFIG_FILE
|
|
cfg.CONFIG_FILE = config_path
|
|
cfg._cache = None
|
|
|
|
assert cfg.get('default_engine') is None
|
|
assert cfg.get('default_engine', 'ollama') == 'ollama'
|
|
|
|
cfg.CONFIG_FILE = original
|
|
cfg._cache = None
|