@@ -16,6 +16,7 @@ from jwebot_sdk import ( # noqa: E402
|
||||
Fact,
|
||||
PlanKind,
|
||||
ReplyPlan,
|
||||
ServiceCommandAdapter,
|
||||
)
|
||||
|
||||
|
||||
@@ -108,5 +109,70 @@ class PlanContractTests(unittest.TestCase):
|
||||
ExtensionResult.from_mapping(value)
|
||||
|
||||
|
||||
class ServiceCommandAdapterTests(unittest.TestCase):
|
||||
def event(self):
|
||||
return Event(
|
||||
event_id="event-1",
|
||||
observed_at="2026-10-14T10:00:00+08:00",
|
||||
sender="Alice",
|
||||
text="@JWeBot /coin",
|
||||
group_fingerprint="group-a",
|
||||
mention_bot=True,
|
||||
)
|
||||
|
||||
def context(self, response):
|
||||
class Commands:
|
||||
def __init__(self):
|
||||
self.handlers = {}
|
||||
|
||||
def register(self, command, handler):
|
||||
self.handlers[command] = handler
|
||||
|
||||
class Services:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
def call(self, profile, *, operation, payload):
|
||||
self.calls.append((profile, operation, payload))
|
||||
return response
|
||||
|
||||
class Context:
|
||||
pass
|
||||
|
||||
context = Context()
|
||||
context.commands = Commands()
|
||||
context.services = Services()
|
||||
return context
|
||||
|
||||
def test_adapter_delegates_to_declared_service_profile(self):
|
||||
context = self.context({"texts": ["第一条", "第二条"]})
|
||||
adapter = ServiceCommandAdapter(
|
||||
"coin",
|
||||
metadata={"local_extension": "coin"},
|
||||
)
|
||||
|
||||
adapter.register(context, ["/翻硬币"])
|
||||
result = context.commands.handlers["/翻硬币"](self.event(), "")
|
||||
|
||||
self.assertEqual(result.reply.kind, PlanKind.MULTI_TEXT)
|
||||
self.assertEqual(result.reply.messages, ("第一条", "第二条"))
|
||||
self.assertEqual(dict(result.metadata), {"local_extension": "coin"})
|
||||
self.assertEqual(context.services.calls[0][0], "coin")
|
||||
self.assertEqual(context.services.calls[0][1], "execute")
|
||||
self.assertEqual(context.services.calls[0][2]["command"], "/翻硬币")
|
||||
self.assertEqual(
|
||||
context.services.calls[0][2]["event"]["event_id"],
|
||||
"event-1",
|
||||
)
|
||||
|
||||
def test_adapter_rejects_invalid_service_text(self):
|
||||
context = self.context({"texts": ["ok", ""]})
|
||||
adapter = ServiceCommandAdapter("coin")
|
||||
|
||||
adapter.register(context, ["/翻硬币"])
|
||||
with self.assertRaisesRegex(ValueError, "invalid text parts"):
|
||||
context.commands.handlers["/翻硬币"](self.event(), "")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user