Files
qqbotsdk/tests/test_events.py
T
snowy-yang 7e7dcbaa94 refactor!: Event 收敛为推送信封封装,删除字段名兼容属性
删除 Event 的 user_id/group_id/content/message_id 四个属性:它们是
"兼容群(openid)与频道两套字段名"的映射层,其中 user_id 还带着
旧版群消息 payload 顶层 from_user_id 的回退(现行格式发言人标识在
author 里,该回退永远轮不到)。Event 只保留信封语义:
.raw/.data/.typed/.type/.event_id,事件体字段一律标注 payload
dataclass(如 msg: GroupAtMessage)获取解析结果。

破坏性变更:event.group_id → msg.group_openid、event.message_id →
msg.id,以此类推;被动回复凭据分工不变——消息事件用事件体 d.id
(msg_id),其余事件用信封顶层 event_id。

迁移:examples/group_bot 入群欢迎改标注 GroupAddRobot(已确认
MusicBot 未使用被删属性);GUIDE/API 同步删除对应属性行;测试
重写为信封语义断言 + typed 解析断言。

107 测试全绿,ruff/pyright 干净。
2026-09-24 20:18:29 +08:00

46 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Event 封装:信封顶层 event_id 与事件体 d 的访问(.data/.typed)。"""
from qqbotsdk.events import Event
from qqbotsdk.payloads import GroupAddRobot
def make_event() -> Event:
return Event(
{
"op": 0,
"id": "evt-1", # 信封顶层,与 op/t/d 平级
"t": "GROUP_ADD_ROBOT",
"d": {"group_openid": "G1", "op_member_openid": "M1"},
}
)
def test_event_id_from_envelope():
event = make_event()
assert event.event_id == "evt-1"
def test_message_id_from_event_body():
event = Event(
{
"op": 0,
"id": "evt-2",
"t": "GROUP_AT_MESSAGE_CREATE",
"d": {"id": "msgid-1", "group_openid": "G1", "content": "hi"},
}
)
assert event.data["id"] == "msgid-1" # 消息事件回复用 msg_id(事件体 d.id)
assert event.event_id == "evt-2" # 信封顶层 id 另有其值
def test_typed_parses_event_body():
event = make_event()
typed = event.typed
assert isinstance(typed, GroupAddRobot)
assert typed.group_openid == "G1"
def test_event_id_defaults_to_none():
event = Event({"op": 0, "t": "READY", "d": {}})
assert event.event_id is None