diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index b1a15ebfd..53d17dfa8 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -1028,9 +1028,13 @@ def channels_status( ): """Show channel status.""" from nanobot.channels.registry import discover_all - from nanobot.config.loader import load_config + from nanobot.config.loader import load_config, set_config_path - config = load_config(Path(config_path) if config_path else None) + resolved_config_path = Path(config_path).expanduser().resolve() if config_path else None + if resolved_config_path is not None: + set_config_path(resolved_config_path) + + config = load_config(resolved_config_path) table = Table(title="Channel Status") table.add_column("Channel", style="cyan") @@ -1121,9 +1125,13 @@ def channels_login( ): """Authenticate with a channel via QR code or other interactive login.""" from nanobot.channels.registry import discover_all - from nanobot.config.loader import load_config + from nanobot.config.loader import load_config, set_config_path - config = load_config(Path(config_path) if config_path else None) + resolved_config_path = Path(config_path).expanduser().resolve() if config_path else None + if resolved_config_path is not None: + set_config_path(resolved_config_path) + + config = load_config(resolved_config_path) channel_cfg = getattr(config.channels, channel_name, None) or {} # Validate channel exists diff --git a/tests/channels/test_channel_plugins.py b/tests/channels/test_channel_plugins.py index 93bf7f1d0..4cf4fab21 100644 --- a/tests/channels/test_channel_plugins.py +++ b/tests/channels/test_channel_plugins.py @@ -220,6 +220,57 @@ def test_channels_login_uses_discovered_plugin_class(monkeypatch): assert seen["force"] is True +def test_channels_login_sets_custom_config_path(monkeypatch, tmp_path): + from nanobot.cli.commands import app + from nanobot.config.schema import Config + from typer.testing import CliRunner + + runner = CliRunner() + seen: dict[str, object] = {} + config_path = tmp_path / "custom-config.json" + + class _LoginPlugin(_FakePlugin): + async def login(self, force: bool = False) -> bool: + return True + + monkeypatch.setattr("nanobot.config.loader.load_config", lambda config_path=None: Config()) + monkeypatch.setattr( + "nanobot.config.loader.set_config_path", + lambda path: seen.__setitem__("config_path", path), + ) + monkeypatch.setattr( + "nanobot.channels.registry.discover_all", + lambda: {"fakeplugin": _LoginPlugin}, + ) + + result = runner.invoke(app, ["channels", "login", "fakeplugin", "--config", str(config_path)]) + + assert result.exit_code == 0 + assert seen["config_path"] == config_path.resolve() + + +def test_channels_status_sets_custom_config_path(monkeypatch, tmp_path): + from nanobot.cli.commands import app + from nanobot.config.schema import Config + from typer.testing import CliRunner + + runner = CliRunner() + seen: dict[str, object] = {} + config_path = tmp_path / "custom-config.json" + + monkeypatch.setattr("nanobot.config.loader.load_config", lambda config_path=None: Config()) + monkeypatch.setattr( + "nanobot.config.loader.set_config_path", + lambda path: seen.__setitem__("config_path", path), + ) + monkeypatch.setattr("nanobot.channels.registry.discover_all", lambda: {}) + + result = runner.invoke(app, ["channels", "status", "--config", str(config_path)]) + + assert result.exit_code == 0 + assert seen["config_path"] == config_path.resolve() + + @pytest.mark.asyncio async def test_manager_skips_disabled_plugin(): fake_config = SimpleNamespace(