Files
AstrBot/tests_v4/test_testing_module.py
whatevertogo 94514fb19b feat: Enhance handler and capability dispatchers with improved error handling
- Updated HandlerDispatcher to raise TypeError for uninjectable required parameters, logging errors appropriately.
- Refactored CapabilityDispatcher to raise TypeError for missing required parameters during capability execution.
- Renamed _load_plugin_config to load_plugin_config for clarity and consistency.
- Introduced _sync_plugin_registry method in SupervisorRuntime to manage plugin capabilities more effectively.
- Enhanced capability registration logic to handle naming conflicts with better logging and automatic renaming.
- Added tests for handler and capability dispatchers to ensure proper error handling and functionality.
- Implemented new HTTP and metadata capabilities with corresponding tests for registration and retrieval.
- Improved MemoryClient methods with additional tests for save_with_ttl, get_many, delete_many, and stats.
- Added tests for the testing module to ensure proper import and functionality of PluginHarness.
2026-03-14 22:07:10 +08:00

71 lines
2.0 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.
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
import pytest
def _repo_root() -> Path:
return Path(__file__).resolve().parents[1]
def _source_env() -> dict[str, str]:
env = os.environ.copy()
src_new = str(_repo_root() / "src-new")
current = env.get("PYTHONPATH")
env["PYTHONPATH"] = f"{src_new}{os.pathsep}{current}" if current else src_new
return env
def test_testing_module_importable() -> None:
from astrbot_sdk import testing
assert testing.PluginHarness is not None
assert testing.MockContext is not None
def test_cli_help_works_from_source_tree() -> None:
process = subprocess.run(
[sys.executable, "-m", "astrbot_sdk", "--help"],
capture_output=True,
text=True,
check=False,
env=_source_env(),
)
assert process.returncode == 0, process.stderr
assert "Usage" in process.stdout
@pytest.mark.asyncio
async def test_plugin_harness_dispatches_sample_plugin() -> None:
from astrbot_sdk.testing import LocalRuntimeConfig, PluginHarness
plugin_dir = _repo_root() / "test_plugin" / "new"
async with PluginHarness(LocalRuntimeConfig(plugin_dir=plugin_dir)) as harness:
records = await harness.dispatch_text("hello")
assert any(record.text == "Echo: hello" for record in records)
@pytest.mark.asyncio
async def test_plugin_harness_supports_metadata_and_http_commands() -> None:
from astrbot_sdk.testing import LocalRuntimeConfig, PluginHarness
plugin_dir = _repo_root() / "test_plugin" / "new"
async with PluginHarness(LocalRuntimeConfig(plugin_dir=plugin_dir)) as harness:
plugin_records = await harness.dispatch_text("plugins")
api_records = await harness.dispatch_text("register_api")
assert any(
"astrbot_plugin_v4demo" in (record.text or "") for record in plugin_records
)
assert any(
"已注册 API当前共 1 个" in (record.text or "") for record in api_records
)