1. 移除Telegram格式化测试中未使用的导入项 2. 修复Teams测试用例,添加monkeypatch参数并配置通配符开关 3. 更新钉钉适配器测试,替换弃用的流属性检查 4. 修正Twitch规范化测试,更新ROOMSTATE测试逻辑 5. 重构会话映射测试,完善数据库执行结果模拟 6. 格式化Slack块构建测试的长参数调用 7. 修复LINE适配器测试,更新能力断言和异步锁使用 8. 修正Slack会话解析测试,修复聊天类型判断错误 9. 更新能力测试,补充缺失的字段检查 10. 修复Matrix适配器测试,修正位置参数和配置校验逻辑 11. 为飞书分析模块测试添加跳过标记 12. 新增微信能力、限流、链接格式、会话路由等模块的单元测试 13. 修复Twitch适配器导入路径和测试断言 14. 新增Discord Webhook、Nextcloud Talk、Signal多账户等模块的单元测试 15. 修复Manager阶段测试的导入路径 16. 新增iMessage异常和命令处理的单元测试 17. 新增Nostr健康检查和相关模块的单元测试 18. 新增Signal守护进程和SSE重连相关测试
251 lines
8.6 KiB
Python
251 lines
8.6 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from yuxi.channels.adapters.nextcloudtalk.config import (
|
|
resolve_history_limit,
|
|
resolve_room_allow_from,
|
|
resolve_room_config,
|
|
resolve_room_enabled,
|
|
resolve_room_require_mention,
|
|
resolve_room_skills,
|
|
resolve_room_system_prompt,
|
|
)
|
|
from yuxi.channels.adapters.nextcloudtalk.config_schema import (
|
|
NextcloudTalkWebhookConfig,
|
|
validate_config,
|
|
)
|
|
|
|
|
|
class TestResolveRoomRequireMention:
|
|
def test_default_true_when_no_config(self):
|
|
assert resolve_room_require_mention("room-1", {}) is True
|
|
|
|
def test_camel_case_key(self):
|
|
config = {"rooms": {"room-1": {"requireMention": False}}}
|
|
assert resolve_room_require_mention("room-1", config) is False
|
|
|
|
def test_snake_case_key(self):
|
|
config = {"rooms": {"room-1": {"require_mention": False}}}
|
|
assert resolve_room_require_mention("room-1", config) is False
|
|
|
|
def test_wildcard_fallback(self):
|
|
config = {"rooms": {"*": {"requireMention": False}}}
|
|
assert resolve_room_require_mention("unknown-room", config) is False
|
|
|
|
|
|
class TestResolveRoomAllowFrom:
|
|
def test_empty_default(self):
|
|
assert resolve_room_allow_from("room-1", {}) == []
|
|
|
|
def test_camel_case(self):
|
|
config = {"rooms": {"room-1": {"allowFrom": ["user-1", "user-2"]}}}
|
|
assert resolve_room_allow_from("room-1", config) == ["user-1", "user-2"]
|
|
|
|
def test_snake_case(self):
|
|
config = {"rooms": {"room-1": {"allow_from": ["user-3"]}}}
|
|
assert resolve_room_allow_from("room-1", config) == ["user-3"]
|
|
|
|
def test_wildcard(self):
|
|
config = {"rooms": {"*": {"allowFrom": ["global-user"]}}}
|
|
assert resolve_room_allow_from("unknown-room", config) == ["global-user"]
|
|
|
|
def test_direct_overrides_wildcard(self):
|
|
config = {
|
|
"rooms": {
|
|
"room-1": {"allowFrom": ["specific-user"]},
|
|
"*": {"allowFrom": ["global-user"]},
|
|
}
|
|
}
|
|
assert resolve_room_allow_from("room-1", config) == ["specific-user"]
|
|
|
|
|
|
class TestResolveRoomSkills:
|
|
def test_empty_default(self):
|
|
assert resolve_room_skills("room-1", {}) == []
|
|
|
|
def test_exact_match(self):
|
|
config = {"rooms": {"room-1": {"skills": ["skill-a"]}}}
|
|
assert resolve_room_skills("room-1", config) == ["skill-a"]
|
|
|
|
def test_wildcard(self):
|
|
config = {"rooms": {"*": {"skills": ["base-skill"]}}}
|
|
assert resolve_room_skills("any-room", config) == ["base-skill"]
|
|
|
|
def test_direct_overrides(self):
|
|
config = {
|
|
"rooms": {
|
|
"room-1": {"skills": ["premium-skill"]},
|
|
"*": {"skills": ["base-skill"]},
|
|
}
|
|
}
|
|
assert resolve_room_skills("room-1", config) == ["premium-skill"]
|
|
|
|
|
|
class TestResolveRoomSystemPromptNoneEmpty:
|
|
def test_none_when_no_config(self):
|
|
assert resolve_room_system_prompt("room-1", {}) is None
|
|
|
|
def test_empty_string_when_empty_value(self):
|
|
config = {"rooms": {"room-1": {"systemPrompt": ""}}}
|
|
assert resolve_room_system_prompt("room-1", config) == ""
|
|
|
|
|
|
class TestResolveRoomEnabledEdgeCases:
|
|
def test_enabled_key(self):
|
|
config = {"rooms": {"room-1": {"enabled": True}}}
|
|
assert resolve_room_enabled("room-1", config) is True
|
|
|
|
def test_enabled_underscore_key(self):
|
|
config = {"rooms": {"room-1": {"enabled_": False}}}
|
|
assert resolve_room_enabled("room-1", config) is False
|
|
|
|
def test_wildcard_fallback(self):
|
|
config = {"rooms": {"*": {"enabled": False}}}
|
|
assert resolve_room_enabled("unknown-room", config) is False
|
|
|
|
def test_default_true_when_no_rooms(self):
|
|
assert resolve_room_enabled("room-1", {}) is True
|
|
|
|
|
|
class TestResolveHistoryLimitEdgeCases:
|
|
def test_group_wildcard_room(self):
|
|
config = {"rooms": {"*": {"historyLimit": 10}}}
|
|
assert resolve_history_limit("any-room", config, "group") == 10
|
|
|
|
def test_group_global_and_room_both_set(self):
|
|
config = {
|
|
"historyLimit": 50,
|
|
"rooms": {"room-1": {"historyLimit": 25}},
|
|
}
|
|
assert resolve_history_limit("room-1", config, "group") == 25
|
|
|
|
def test_group_global_only(self):
|
|
assert resolve_history_limit("room-1", {"historyLimit": 50}, "group") == 50
|
|
|
|
def test_dm_global_snake_case(self):
|
|
assert resolve_history_limit("user-1", {"dm_history_limit": 30}, "direct") == 30
|
|
|
|
def test_dm_per_user_overrides_global(self):
|
|
config = {
|
|
"dmHistoryLimit": 30,
|
|
"dms": {"user-1": {"historyLimit": 10}},
|
|
}
|
|
assert resolve_history_limit("user-1", config, "direct") == 10
|
|
|
|
def test_dm_default_zero(self):
|
|
assert resolve_history_limit("user-1", {}, "direct") == 0
|
|
|
|
|
|
class TestResolveRoomConfigEdgeCases:
|
|
def test_no_rooms_key(self):
|
|
result = resolve_room_config("token", {})
|
|
assert result["_match_source"] == "none"
|
|
|
|
def test_empty_rooms_returns_none_source(self):
|
|
result = resolve_room_config("token", {"rooms": {}})
|
|
assert result["_match_source"] == "none"
|
|
|
|
def test_no_wildcard_no_match(self):
|
|
result = resolve_room_config("token", {"rooms": {"other": {"enabled": True}}})
|
|
assert result["_match_source"] == "wildcard"
|
|
|
|
|
|
class TestWebhookConfigSchema:
|
|
def test_default_values(self):
|
|
cfg = NextcloudTalkWebhookConfig()
|
|
assert cfg.enabled is False
|
|
assert cfg.port == 8788
|
|
assert cfg.host == "0.0.0.0"
|
|
assert cfg.path == "/nextcloud-talk-webhook"
|
|
assert cfg.rate_limit_max_requests == 10
|
|
assert cfg.rate_limit_window_s == 60
|
|
assert cfg.rate_limit_lockout_s == 300
|
|
assert cfg.max_body_size == 1 * 1024 * 1024
|
|
assert cfg.pre_auth_max_body_size == 64 * 1024
|
|
|
|
def test_custom_values(self):
|
|
cfg = NextcloudTalkWebhookConfig(
|
|
port=9999,
|
|
host="127.0.0.1",
|
|
path="/custom-webhook",
|
|
rateLimitMaxRequests=5,
|
|
rateLimitWindowS=30,
|
|
rateLimitLockoutS=60,
|
|
maxBodySize=2 * 1024 * 1024,
|
|
preAuthMaxBodySize=128 * 1024,
|
|
)
|
|
assert cfg.port == 9999
|
|
assert cfg.host == "127.0.0.1"
|
|
assert cfg.path == "/custom-webhook"
|
|
assert cfg.rate_limit_max_requests == 5
|
|
assert cfg.rate_limit_window_s == 30
|
|
assert cfg.rate_limit_lockout_s == 60
|
|
assert cfg.max_body_size == 2 * 1024 * 1024
|
|
assert cfg.pre_auth_max_body_size == 128 * 1024
|
|
|
|
|
|
class TestValidateConfigEdgeCases:
|
|
def test_invalid_group_policy(self):
|
|
with pytest.raises(ValueError, match="group_policy"):
|
|
validate_config({
|
|
"serverUrl": "https://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
"groupPolicy": "unknown",
|
|
})
|
|
|
|
def test_invalid_chunk_mode(self):
|
|
with pytest.raises(ValueError, match="chunk_mode"):
|
|
validate_config({
|
|
"serverUrl": "https://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
"chunkMode": "invalid_mode",
|
|
})
|
|
|
|
def test_empty_server_url_passes_validation(self):
|
|
config = validate_config({
|
|
"serverUrl": "",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
})
|
|
assert config.server_url == ""
|
|
|
|
def test_http_url_allowed(self):
|
|
config = validate_config({
|
|
"serverUrl": "http://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
})
|
|
assert config.server_url == "http://nc.example.com"
|
|
|
|
def test_all_policies_valid(self):
|
|
for policy in ("open", "pairing", "allowlist", "disabled"):
|
|
config = validate_config({
|
|
"serverUrl": "https://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
"dmPolicy": policy,
|
|
})
|
|
assert config.dm_policy == policy
|
|
|
|
def test_all_group_policies_valid(self):
|
|
for policy in ("open", "allowlist", "disabled"):
|
|
config = validate_config({
|
|
"serverUrl": "https://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
"groupPolicy": policy,
|
|
})
|
|
assert config.group_policy == policy
|
|
|
|
def test_all_chunk_modes_valid(self):
|
|
for mode in ("split", "stop", "truncate"):
|
|
config = validate_config({
|
|
"serverUrl": "https://nc.example.com",
|
|
"botUser": "bot",
|
|
"appPassword": "secret",
|
|
"chunkMode": mode,
|
|
})
|
|
assert config.chunk_mode == mode |