Files
snowy-yang 755013b9db 初始提交:QQ 群入群验证机器人
基于 qqbotsdk-py 的入群验证:进群发按钮验证码、完成前禁言、
超时踢出(11253 时兜底长期禁言)、乱点他人按钮处罚、状态持久化
与重启恢复。38 个单元/集成测试。
2026-10-04 18:44:08 +08:00

68 lines
2.0 KiB
Python

"""处理器装配与配置装配测试(不发起任何网络请求)。"""
from qqbotsdk import Bot
from authbot.config import AuthConfig
from authbot.handlers import KICK_NOT_ALLOWED, setup
def _bot() -> Bot:
import os
os.environ.setdefault("APPID", "test-appid")
os.environ.setdefault("APPSECRET", "test-secret")
return Bot(intents=0) # 掩码直传,避免读 intents.toml
def test_config_defaults(monkeypatch, tmp_path):
monkeypatch.delenv("VERIFY_TIMEOUT_MINUTES", raising=False)
monkeypatch.delenv("TIMEOUT_KICK", raising=False)
monkeypatch.delenv("DATA_DIR", raising=False)
cfg = AuthConfig.load()
assert cfg.verify_timeout_minutes == 10
assert cfg.timeout_kick is True
assert cfg.timeout_fallback_mute_hours == 72
assert cfg.captcha_options == 4
assert cfg.verified_trust_days == 0
def test_config_env_override(monkeypatch, tmp_path):
monkeypatch.setenv("VERIFY_TIMEOUT_MINUTES", "5")
monkeypatch.setenv("TIMEOUT_KICK", "false")
monkeypatch.setenv("DATA_DIR", str(tmp_path))
cfg = AuthConfig.load()
assert cfg.verify_timeout_minutes == 5
assert cfg.timeout_kick is False
assert cfg.data_dir == tmp_path
def test_config_rejects_out_of_range(monkeypatch):
monkeypatch.setenv("VERIFY_TIMEOUT_MINUTES", "0")
try:
AuthConfig.load()
except ValueError as e:
assert "VERIFY_TIMEOUT_MINUTES" in str(e)
else:
raise AssertionError("应当拒绝越界值")
def test_setup_registers_all_handlers(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path))
bot = _bot()
cfg = AuthConfig.load()
state = setup(bot, cfg)
registered = {
event
for event, handlers in bot._emitter._handlers.items() # noqa: SLF001
if handlers
}
assert {
"READY",
"GROUP_MEMBER_ADD",
"INTERACTION_CREATE",
"GROUP_MEMBER_REMOVE",
"GROUP_AT_MESSAGE_CREATE",
} <= registered
assert state.all_pending() == []
assert KICK_NOT_ALLOWED == 11253