mirror of
https://github.com/snowy-yang/qqbotsdk.git
synced 2026-10-21 11:54:48 +00:00
以「语义可读、逻辑清晰、减少心智负担」为目标,不追求极致速度:
- **移除 pyee 依赖**:EventEmitter 改为内置的 `事件名 → handler 列表` 注册表
(on() 追加、emit() 顺序遍历)。SDK 用不到 pyee 的 once/new_listener/错误
事件机制,去掉后少一个依赖、分发语义一眼可读。注册语义不变(同函数重复
注册只生效一次),并补测试锁定「重复注册只生效一次」「按注册顺序执行」。
破坏性:once/remove_listener 等装饰性 API 不再提供。
- **去掉热路径上的重复计算**(纯缓存,不改语义):
· payloads 按类缓存解析计划(字段名 + 是否嵌套 Author),不再每次事件重跑
dataclasses.fields 与联合类型解包;未收录字段照旧忽略(新增回归测试,
线上 payload 会带 author.union_openid 这类字段)。实测 7.5µs → 2.5µs。
· crypto 按 AppSecret 缓存派生私钥(每个 webhook 请求都要验签,派生一次
约 40µs);公钥派生只差 3µs,不再多缓存一层。
- **命名与接口一致性**:
· WebsocketConnecter.reply_helper → send_helper(它从出站缓冲取帧发送,
原名会让人误以为还有 reply 队列;model.py 的对应注释同步)
· Connecter 由 typing.Protocol 改为 abc.ABC,与 BaseProtocol 风格统一
- **webhook 去重改摊还清理**:过期判定按当前 id 做(O(1),且 TTL 外重发的
同一 id 仍按新事件处理——原先的写法在阈值清理下会误判为重复,已由测试
抓出并修正),整表清过期项只在表长到阈值时执行,不再每请求 O(n) 扫描。
测试 85 全绿(新增未知字段/解析计划缓存/注册语义/去重摊还与 TTL 过期),
ruff check/format + pyright 干净。文档 ARCHITECTURE/API/CHANGELOG 同步。
195 lines
5.9 KiB
Python
195 lines
5.9 KiB
Python
from qqbotsdk.emitter import EventEmitter
|
|
from qqbotsdk.model import Opcode
|
|
from qqbotsdk.payloads import (
|
|
_PARSE_PLANS,
|
|
C2CMessage,
|
|
GroupAtMessage,
|
|
Interaction,
|
|
Ready,
|
|
parse_event,
|
|
)
|
|
from qqbotsdk.queue import EventQueue
|
|
|
|
|
|
def test_parse_ignores_unknown_fields():
|
|
"""回归:线上 payload 会带未收录字段(如 author.union_openid),
|
|
解析必须忽略而非透传给 dataclass 构造(那会直接报错)。"""
|
|
msg = parse_event(
|
|
"GROUP_AT_MESSAGE_CREATE",
|
|
{
|
|
"id": "msgid-9",
|
|
"content": "hi",
|
|
"group_openid": "G1",
|
|
"unknown_top_level": {"whatever": 1},
|
|
"author": {"member_openid": "M9", "union_openid": "U-should-be-dropped"},
|
|
},
|
|
)
|
|
assert isinstance(msg, GroupAtMessage)
|
|
assert msg.content == "hi"
|
|
assert msg.author is not None and msg.author.member_openid == "M9"
|
|
|
|
|
|
def test_parse_plan_is_cached_per_class():
|
|
"""解析计划按类缓存,且二次解析结果与首次一致。"""
|
|
d = {"content": "x", "group_openid": "G"}
|
|
first = parse_event("GROUP_AT_MESSAGE_CREATE", d)
|
|
second = parse_event("GROUP_AT_MESSAGE_CREATE", d)
|
|
assert isinstance(first, GroupAtMessage) and isinstance(second, GroupAtMessage)
|
|
assert first.content == second.content == "x"
|
|
assert GroupAtMessage in _PARSE_PLANS
|
|
|
|
msg = parse_event(
|
|
"GROUP_AT_MESSAGE_CREATE",
|
|
{
|
|
"author": {"id": None, "member_openid": "M1"},
|
|
"content": "hi",
|
|
"group_openid": "G1",
|
|
"id": "msgid-1",
|
|
"timestamp": "2026-01-01",
|
|
},
|
|
)
|
|
assert isinstance(msg, GroupAtMessage)
|
|
assert msg.group_openid == "G1"
|
|
assert msg.user_openid == "M1"
|
|
assert msg.id == "msgid-1"
|
|
assert msg.author is not None and msg.author.member_openid == "M1"
|
|
|
|
|
|
def test_parse_group_full_message():
|
|
msg = parse_event(
|
|
"GROUP_MESSAGE_CREATE",
|
|
{
|
|
"author": {"member_openid": "M2"},
|
|
"content": "大家好",
|
|
"group_openid": "G1",
|
|
"id": "msgid-2",
|
|
"timestamp": "2026-01-01",
|
|
"message_type": 0,
|
|
"message_scene": {"unknown": False},
|
|
"attachments": [{"content_type": 1, "url": "https://x/img.png"}],
|
|
"mentions": [{"id": "U1"}],
|
|
"msg_elements": [{"type": 1, "text_element": {"content": "大家好"}}],
|
|
},
|
|
)
|
|
assert isinstance(msg, GroupAtMessage)
|
|
assert msg.group_openid == "G1"
|
|
assert msg.user_openid == "M2"
|
|
assert msg.message_type == 0
|
|
assert msg.message_scene == {"unknown": False}
|
|
assert msg.attachments == [{"content_type": 1, "url": "https://x/img.png"}]
|
|
assert msg.mentions == [{"id": "U1"}]
|
|
assert msg.msg_elements == [{"type": 1, "text_element": {"content": "大家好"}}]
|
|
|
|
|
|
async def test_group_full_message_reaches_typed_handler():
|
|
queue = EventQueue()
|
|
emitter = EventEmitter(queue)
|
|
received: list[GroupAtMessage] = []
|
|
|
|
@emitter.on("GROUP_MESSAGE_CREATE")
|
|
async def handler(msg: GroupAtMessage):
|
|
received.append(msg)
|
|
|
|
await emitter.emit(
|
|
{
|
|
"op": Opcode.DISPATCH,
|
|
"t": "GROUP_MESSAGE_CREATE",
|
|
"d": {"content": "hi", "group_openid": "G1"},
|
|
}
|
|
)
|
|
assert len(received) == 1
|
|
assert received[0].content == "hi"
|
|
assert received[0].group_openid == "G1"
|
|
|
|
|
|
def test_parse_c2c_message():
|
|
msg = parse_event(
|
|
"C2C_MESSAGE_CREATE",
|
|
{"author": {"user_openid": "U1"}, "content": "hello", "id": "m2"},
|
|
)
|
|
assert isinstance(msg, C2CMessage)
|
|
assert msg.user_openid == "U1"
|
|
|
|
|
|
def test_parse_ready():
|
|
ready = parse_event(
|
|
"READY", {"session_id": "s1", "version": 1, "user": {"id": "b"}}
|
|
)
|
|
assert isinstance(ready, Ready)
|
|
assert ready.session_id == "s1"
|
|
|
|
|
|
def test_parse_unknown_type_returns_dict():
|
|
d = {"whatever": 1}
|
|
assert parse_event("SOME_FUTURE_EVENT", d) is d
|
|
assert parse_event(None, d) is d
|
|
|
|
|
|
def test_parse_ignores_extra_and_missing_fields():
|
|
msg = parse_event("GROUP_AT_MESSAGE_CREATE", {"content": "x", "future_field": 9})
|
|
assert isinstance(msg, GroupAtMessage)
|
|
assert msg.content == "x"
|
|
assert msg.group_openid is None
|
|
|
|
|
|
def test_parse_non_dict_d_returns_dict():
|
|
d = 42 # 心跳等 opcode 的 d 是数字
|
|
assert parse_event("GROUP_AT_MESSAGE_CREATE", d) == 42
|
|
|
|
|
|
def test_interaction_button_data():
|
|
inter = parse_event(
|
|
"INTERACTION_CREATE",
|
|
{
|
|
"id": "i1",
|
|
"chat_type": 1,
|
|
"group_openid": "G1",
|
|
"data": {"resolved": {"button_data": "btn|like"}},
|
|
},
|
|
)
|
|
assert isinstance(inter, Interaction)
|
|
assert inter.button_data == "btn|like"
|
|
|
|
|
|
def test_interaction_button_data_defaults_to_empty():
|
|
inter = Interaction(id="i2", data=None)
|
|
assert inter.button_data == ""
|
|
inter = Interaction(id="i3", data={"resolved": None})
|
|
assert inter.button_data == ""
|
|
|
|
|
|
async def test_typed_annotation_receives_dataclass():
|
|
queue = EventQueue()
|
|
emitter = EventEmitter(queue)
|
|
received: list[GroupAtMessage] = []
|
|
|
|
@emitter.on("GROUP_AT_MESSAGE_CREATE")
|
|
async def handler(msg: GroupAtMessage):
|
|
received.append(msg)
|
|
|
|
await emitter.emit(
|
|
{
|
|
"op": Opcode.DISPATCH,
|
|
"t": "GROUP_AT_MESSAGE_CREATE",
|
|
"d": {"content": "hi", "group_openid": "G1"},
|
|
}
|
|
)
|
|
assert len(received) == 1
|
|
assert received[0].content == "hi"
|
|
assert received[0].group_openid == "G1"
|
|
|
|
|
|
async def test_untyped_annotation_still_receives_raw_dict():
|
|
queue = EventQueue()
|
|
emitter = EventEmitter(queue)
|
|
received: list[dict] = []
|
|
|
|
@emitter.on("GROUP_AT_MESSAGE_CREATE")
|
|
async def handler(d):
|
|
received.append(d)
|
|
|
|
await emitter.emit(
|
|
{"op": Opcode.DISPATCH, "t": "GROUP_AT_MESSAGE_CREATE", "d": {"content": "x"}}
|
|
)
|
|
assert received == [{"content": "x"}]
|