Files
AstrBot/examples/hello_plugin/tests/test_plugin.py
whatevertogo 84184c4c0a feat(errors): Enhance AstrBotError with detailed documentation and examples
feat(events): Expand MessageEvent with reply capabilities and detailed docstrings

fix(loader): Ensure plugin path is correctly managed in sys.path

feat(star): Improve Star class documentation and lifecycle method descriptions

feat(testing): Add plugin metadata handling in MockContext and enhance PluginHarness

feat(hello): Refactor HelloPlugin to utilize new reply methods and structured capabilities

test(decorators): Add tests for input/output model support in provide_capability

test(events): Implement tests for reply_image and reply_chain methods in MessageEvent

test(http): Validate API registration with capability handler references and error handling

test(tests): Enhance tests for plugin harness and directory handling in dev commands
2026-03-14 23:37:09 +08:00

53 lines
1.3 KiB
Python

import importlib.util
from pathlib import Path
import pytest
from astrbot_sdk.testing import MockContext, MockMessageEvent
PLUGIN_DIR = Path(__file__).resolve().parents[1]
def _load_plugin_class():
module_path = PLUGIN_DIR / "main.py"
spec = importlib.util.spec_from_file_location(
"examples_hello_plugin_main",
module_path,
)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.HelloPlugin
HelloPlugin = _load_plugin_class()
@pytest.mark.asyncio
async def test_hello_handler() -> None:
plugin = HelloPlugin()
ctx = MockContext(
plugin_id="hello_plugin",
plugin_metadata={"display_name": "Hello Plugin"},
)
event = MockMessageEvent(text="/hello", context=ctx)
await plugin.hello(event, ctx)
assert event.replies == ["Hello, World!"]
ctx.platform.assert_sent("Hello, World!")
@pytest.mark.asyncio
async def test_about_handler() -> None:
plugin = HelloPlugin()
ctx = MockContext(
plugin_id="hello_plugin",
plugin_metadata={"display_name": "Hello Plugin"},
)
event = MockMessageEvent(text="/about", context=ctx)
await plugin.about(event, ctx)
assert any("Hello Plugin" in reply for reply in event.replies)