refactor!: 移除 dishka,改为内置裸标注参数注入

- handler 形参直接标注组件类型(api: BotApi)即注入,解析为 emitter 内同步查表(payload dataclass / Event / services 登记类型 / 回退原始 dict)
This commit is contained in:
snowy-yang committed 2026-09-16 19:18:06 +08:00
1 parent 953d44f62e
commit cff0c142c2
16 files changed
+161 -240

No files matched your search

+47
View File
@@ -2,6 +2,7 @@ import pytest
from qqbotsdk.emitter import EventEmitter
from qqbotsdk.model import Opcode, payload_of
from qqbotsdk.payloads import GroupAtMessage
from qqbotsdk.queue import EventQueue
from qqbotsdk.session import Session
@@ -53,3 +54,49 @@ async def test_reply_payload_is_enqueued():
reply = await queue.get_reply()
assert reply["op"] == Opcode.HEARTBEAT
assert reply["d"] == 42
@pytest.mark.asyncio
async def test_service_annotation_resolves_from_services():
queue = EventQueue()
emitter = EventEmitter(queue)
class Store:
pass
store = Store()
emitter.services[Store] = store
received: list[tuple[GroupAtMessage, Store, dict]] = []
@emitter.on("GROUP_AT_MESSAGE_CREATE")
async def handler(msg: GroupAtMessage, st: Store, d):
received.append((msg, st, d))
await emitter.emit(
{
"op": Opcode.DISPATCH,
"t": "GROUP_AT_MESSAGE_CREATE",
"d": {"content": "hi", "group_openid": "G1"},
}
)
assert len(received) == 1
msg, got, d = received[0]
assert got is store
assert isinstance(msg, GroupAtMessage) and msg.content == "hi"
assert d == {"content": "hi", "group_openid": "G1"}
@pytest.mark.asyncio
async def test_unregistered_annotation_falls_back_to_raw_dict():
queue = EventQueue()
emitter = EventEmitter(queue)
received: list[dict] = []
@emitter.on("READY")
async def handler(dep: Session): # Session 未登记进 services
received.append(dep)
await emitter.emit(
{"op": Opcode.DISPATCH, "t": "READY", "d": {"session_id": "s1"}}
)
assert received == [{"session_id": "s1"}]