Files
JWeBot/tests/test_example_extension.py
T

55 lines
1.5 KiB
Python

#!/usr/bin/env python3
import importlib.util
import sys
import unittest
from pathlib import Path
from unittest import mock
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SDK_SRC = PROJECT_ROOT / "packages" / "jwebot-sdk" / "src"
EXAMPLE_SRC = PROJECT_ROOT / "examples" / "hello-command" / "src"
sys.path[:0] = [str(SDK_SRC), str(EXAMPLE_SRC)]
from jwebot_sdk import Event, PlanKind # noqa: E402
SPEC = importlib.util.spec_from_file_location(
"hello_command",
EXAMPLE_SRC / "hello_command.py",
)
EXAMPLE = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(EXAMPLE)
class HelloCommandTests(unittest.TestCase):
def event(self):
return Event(
event_id="event-1",
observed_at="2026-10-14T10:00:00+08:00",
sender="Alice",
text="@JWeBot /hello",
group_fingerprint="group-a",
mention_bot=True,
)
def test_handler_returns_a_structured_reply(self):
result = EXAMPLE.handle_hello(self.event(), "world")
self.assertEqual(result.reply.kind, PlanKind.TEXT)
self.assertEqual(result.reply.message, "hello Alice world")
def test_plugin_registers_only_its_declared_command(self):
context = mock.Mock()
EXAMPLE.plugin(context)
context.commands.register.assert_called_once_with(
"/hello",
EXAMPLE.handle_hello,
)
if __name__ == "__main__":
unittest.main()