From 0ca639bf2299554cfe4ca56f9dabbab6018b00f5 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Mon, 23 Mar 2026 16:39:24 +0000 Subject: [PATCH] fix(cli): use discovered class for channel login --- nanobot/cli/commands.py | 4 ++-- tests/test_channel_plugins.py | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index ff747b1..87b2bc5 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -1009,7 +1009,7 @@ def channels_login( force: bool = typer.Option(False, "--force", "-f", help="Force re-authentication even if already logged in"), ): """Authenticate with a channel via QR code or other interactive login.""" - from nanobot.channels.registry import discover_all, load_channel_class + from nanobot.channels.registry import discover_all from nanobot.config.loader import load_config config = load_config() @@ -1024,7 +1024,7 @@ def channels_login( console.print(f"{__logo__} {all_channels[channel_name].display_name} Login\n") - channel_cls = load_channel_class(channel_name) + channel_cls = all_channels[channel_name] channel = channel_cls(channel_cfg, bus=None) success = asyncio.run(channel.login(force=force)) diff --git a/tests/test_channel_plugins.py b/tests/test_channel_plugins.py index e8a6d49..3f34dc5 100644 --- a/tests/test_channel_plugins.py +++ b/tests/test_channel_plugins.py @@ -22,6 +22,10 @@ class _FakePlugin(BaseChannel): name = "fakeplugin" display_name = "Fake Plugin" + def __init__(self, config, bus): + super().__init__(config, bus) + self.login_calls: list[bool] = [] + async def start(self) -> None: pass @@ -31,6 +35,10 @@ class _FakePlugin(BaseChannel): async def send(self, msg: OutboundMessage) -> None: pass + async def login(self, force: bool = False) -> bool: + self.login_calls.append(force) + return True + class _FakeTelegram(BaseChannel): """Plugin that tries to shadow built-in telegram.""" @@ -183,6 +191,34 @@ async def test_manager_loads_plugin_from_dict_config(): assert isinstance(mgr.channels["fakeplugin"], _FakePlugin) +def test_channels_login_uses_discovered_plugin_class(monkeypatch): + from nanobot.cli.commands import app + from nanobot.config.schema import Config + from typer.testing import CliRunner + + runner = CliRunner() + seen: dict[str, object] = {} + + class _LoginPlugin(_FakePlugin): + display_name = "Login Plugin" + + async def login(self, force: bool = False) -> bool: + seen["force"] = force + seen["config"] = self.config + return True + + monkeypatch.setattr("nanobot.config.loader.load_config", lambda: Config()) + monkeypatch.setattr( + "nanobot.channels.registry.discover_all", + lambda: {"fakeplugin": _LoginPlugin}, + ) + + result = runner.invoke(app, ["channels", "login", "fakeplugin", "--force"]) + + assert result.exit_code == 0 + assert seen["force"] is True + + @pytest.mark.asyncio async def test_manager_skips_disabled_plugin(): fake_config = SimpleNamespace(