Files
agent-safe-middleware/tests/test_config.py
T
starrrLight 1b48d0146c test(宿主): 使用内置 Lua 解耦业务插件文件
将暂存事件与 HTTP 回调测试改为内置 Lua 源码,在临时目录生成插件;覆盖 HTTP 回调放行、拒绝和非成功状态。配置测试自行生成配置和插件,不再读取项目业务配置与模型目录。

在不包含业务 plugins、config.yaml 和模型目录的隔离目录中执行全量测试:132 项通过,1 项因缺少 LibreOffice 跳过。Ruff 静态检查、改动测试格式检查和差异检查通过。
2026-10-15 18:13:19 +08:00

117 lines
4.1 KiB
Python

import pytest
from pydantic import ValidationError
from middleware.config import NetworkRule, PluginConfig, PluginMatch, Settings, load_settings
def test_configuration_and_paths(tmp_path, monkeypatch):
config_dir = tmp_path / "configuration"
plugin = config_dir / "plugins" / "inline.lua"
plugin.parent.mkdir(parents=True)
plugin.write_text("return {}", encoding="utf-8")
config = config_dir / "config.yaml"
config.write_text(
"upstream: http://agent:8000\nplugins:\n - id: inline\n file: plugins/inline.lua\n",
encoding="utf-8",
)
monkeypatch.chdir(tmp_path)
settings = load_settings(config)
assert settings.plugins[0].file == plugin.resolve()
assert settings.limits.max_sessions == 4
@pytest.mark.parametrize("absolute", [False, True])
def test_model_path_uses_working_directory(tmp_path, monkeypatch, absolute):
workdir = tmp_path / "service"
model_dir = workdir / "models" / "guard_model"
model_dir.mkdir(parents=True)
config_dir = tmp_path / "config"
config_dir.mkdir()
plugin = config_dir / "guard.lua"
plugin.write_text("return {}", encoding="utf-8")
model_path = model_dir.as_posix() if absolute else "models/guard_model"
config = config_dir / "config.yaml"
config.write_text(
f"upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n"
f" file: {model_path}\nplugins:\n - id: guard\n file: guard.lua\n"
" llm: {allow: [guard]}\n",
encoding="utf-8",
)
monkeypatch.chdir(workdir)
settings = load_settings(config)
assert settings.llm[0].file == model_dir.resolve()
assert settings.plugins[0].file == plugin.resolve()
def test_missing_model_does_not_fall_back_to_config_directory(tmp_path, monkeypatch):
workdir = tmp_path / "service"
workdir.mkdir()
config_dir = tmp_path / "config"
(config_dir / "models" / "guard_model").mkdir(parents=True)
config = config_dir / "config.yaml"
config.write_text(
"upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n"
" file: models/guard_model\n",
encoding="utf-8",
)
monkeypatch.chdir(workdir)
with pytest.raises(ValueError, match="llm model path not found: guard"):
load_settings(config)
def test_duplicate_ids_and_missing_file(tmp_path):
path = tmp_path / "config.yaml"
path.write_text(
"upstream: http://agent:8000\nplugins:\n - id: x\n file: absent.lua\n", encoding="utf-8"
)
with pytest.raises(ValueError):
load_settings(path)
with pytest.raises(ValidationError):
Settings(
upstream="http://agent",
plugins=[
{"id": "x", "file": "a.lua"},
{"id": "x", "file": "b.lua"},
],
)
def test_plugin_match_paths_are_exact_and_legacy_paths_are_rejected(tmp_path):
plugin = tmp_path / "plugin.lua"
plugin.write_text("return {}", encoding="utf-8")
match = PluginMatch(paths=["/chat/stream"])
assert match.matches("/chat/stream")
assert not match.matches("/chat/stream/")
assert not match.matches("/chat/stream?token=x")
with pytest.raises(ValidationError):
PluginMatch(paths=[])
for invalid in ["chat/stream", "/chat/*", "/chat/stream?x=1", "/chat/stream#part"]:
with pytest.raises(ValidationError):
PluginMatch(paths=[invalid])
with pytest.raises(ValidationError, match="config.paths.*match.paths"):
PluginConfig(id="legacy", file=plugin, config={"paths": ["/chat/stream"]})
@pytest.mark.parametrize("domain", ["*", "bad/example.com", "foo..com", "127.0.0.1", "*.com*"])
def test_bad_domains(domain):
with pytest.raises(ValueError):
NetworkRule(domain=domain)
def test_wildcard_and_idna():
rule = NetworkRule(domain="*.EXAMPLE.com.")
assert rule.matches("a.example.com", "https", 443)
assert rule.matches("a.b.example.com", "https", 443)
assert not rule.matches("example.com", "https", 443)
assert not rule.matches("badexample.com", "https", 443)
assert not rule.matches("a.example.com", "http", 443)
assert NetworkRule(domain="例子.测试").domain == "xn--fsqu00a.xn--0zwm56d"