From 779cf9899fbd4a6bf9d5fe6b50bbab7bc33db7c5 Mon Sep 17 00:00:00 2001 From: huanghuoguoguo <1051233107@qq.com> Date: Sat, 16 May 2026 11:05:31 +0800 Subject: [PATCH] fix(plugin): use specific runtime not connected error --- src/langbot/pkg/plugin/connector.py | 6 +++- .../unit_tests/plugin/test_connector_ping.py | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/unit_tests/plugin/test_connector_ping.py diff --git a/src/langbot/pkg/plugin/connector.py b/src/langbot/pkg/plugin/connector.py index d47cb448..52a6c1a9 100644 --- a/src/langbot/pkg/plugin/connector.py +++ b/src/langbot/pkg/plugin/connector.py @@ -35,6 +35,10 @@ from ..core import taskmgr from ..entity.persistence import plugin as persistence_plugin +class PluginRuntimeNotConnectedError(RuntimeError): + """Raised when plugin runtime operations are requested before connection.""" + + class PluginRuntimeConnector: """Plugin runtime connector""" @@ -192,7 +196,7 @@ class PluginRuntimeConnector: async def ping_plugin_runtime(self): if not hasattr(self, 'handler'): - raise Exception('Plugin runtime is not connected') + raise PluginRuntimeNotConnectedError('Plugin runtime is not connected') return await self.handler.ping() diff --git a/tests/unit_tests/plugin/test_connector_ping.py b/tests/unit_tests/plugin/test_connector_ping.py new file mode 100644 index 00000000..766e51f8 --- /dev/null +++ b/tests/unit_tests/plugin/test_connector_ping.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from types import SimpleNamespace +from unittest.mock import AsyncMock + +import pytest + +from langbot.pkg.plugin.connector import PluginRuntimeConnector, PluginRuntimeNotConnectedError + + +def make_connector() -> PluginRuntimeConnector: + app = SimpleNamespace(instance_config=SimpleNamespace(data={'plugin': {'enable': True}})) + return PluginRuntimeConnector(app, AsyncMock()) + + +@pytest.mark.asyncio +async def test_ping_plugin_runtime_raises_specific_error_when_not_connected(): + connector = make_connector() + + with pytest.raises(PluginRuntimeNotConnectedError, match='Plugin runtime is not connected'): + await connector.ping_plugin_runtime() + + +@pytest.mark.asyncio +async def test_ping_plugin_runtime_delegates_to_connected_handler(): + connector = make_connector() + connector.handler = SimpleNamespace(ping=AsyncMock(return_value='pong')) + + result = await connector.ping_plugin_runtime() + + assert result == 'pong' + connector.handler.ping.assert_awaited_once()