Files
EvoScientist-Multi/tests/test_channel_command.py
T
Xi Zhang 92df3e1844 Refactor/cli command manager (#178)
* feat(cli): migrate command handling to CommandManager and enhance UI interactions

* feat(cli): add /clear and /help commands to enhance user experience

* feat(cli): implement /new and /resume commands with interactive session management

* Refactor MCP and Skills Command Handling

- Moved the interactive picker style to a centralized widget for consistency across MCP and Skills commands.
- Updated the MCP command to remove the old command dispatch logic, delegating to the new InstallMCPCommand.
- Enhanced the Skills command to utilize a new interactive picker for skill selection, improving user experience.
- Implemented cancellation handling in the picker to differentiate between user cancellations and empty selections.
- Added comprehensive tests for the new command structures and picker functionalities to ensure reliability.

* refactor(cli): streamline CommandManager dispatch and remove deprecated command set

* refactor(cli): enhance error handling and state management in ChannelCommand and RichCLICommandUI

* refactor(cli): update lifecycle callback terminology and improve async prompt handling in RichCLICommandUI

* refactor(cli): enhance SlashCommandCompleter to dynamically fetch workspace directory for autocompletion

* refactor(cli): unify quit handling in RichCLICommandUI with shared _stop helper

* refactor(cli): remove hardcoded slash commands and utilize command manager for dynamic completion

* refactor(cli): update MCP and skills command files for improved clarity and organization

* refactor(mcp_ui): remove unnecessary newline in _show_mcp_config function
2026-04-24 10:38:44 +01:00

220 lines
7.0 KiB
Python

"""Tests for the /channel command (ChannelCommand)."""
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from tests.conftest import run_async as _run
@pytest.fixture(autouse=True)
def _reset_channel_globals():
"""Reset module-level ``_cli_agent``/``_cli_thread_id`` around every test.
These globals are written by the start / add-to-running paths and would
otherwise leak between tests (and into unrelated suites).
"""
import EvoScientist.cli.channel as _ch
_ch._cli_agent = None
_ch._cli_thread_id = None
try:
yield
finally:
_ch._cli_agent = None
_ch._cli_thread_id = None
def _ctx():
from EvoScientist.commands.base import CommandContext
ui = MagicMock()
ui.supports_interactive = True
return CommandContext(
agent=object(), thread_id="tid-42", ui=ui, workspace_dir="/ws"
), ui
class TestNeedsAgent:
"""status + stop must not require the agent (recovery from broken load)."""
def test_status_does_not_need_agent(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
assert ChannelCommand().needs_agent(["status"]) is False
def test_stop_does_not_need_agent(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
assert ChannelCommand().needs_agent(["stop"]) is False
def test_stop_with_target_does_not_need_agent(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
assert ChannelCommand().needs_agent(["stop", "telegram"]) is False
def test_start_subcommand_needs_agent(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
assert ChannelCommand().needs_agent(["telegram"]) is True
def test_no_args_needs_agent(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
# no subcmd → falls through to start flow, needs agent
assert ChannelCommand().needs_agent([]) is True
class TestStartPath:
"""Start flow must propagate agent/thread_id globals."""
def test_start_sets_cli_agent_globals(self):
import EvoScientist.cli.channel as _ch_mod
from EvoScientist.commands.implementation.channel import ChannelCommand
ctx, _ui = _ctx()
config = SimpleNamespace(
channel_enabled="telegram",
channel_send_thinking=True,
)
with (
patch(
"EvoScientist.cli.channel._channels_is_running",
return_value=False,
),
patch(
"EvoScientist.cli.channel._start_channels_bus_mode",
),
patch(
"EvoScientist.config.load_config",
return_value=config,
),
):
_run(ChannelCommand().execute(ctx, ["telegram"]))
assert _ch_mod._cli_agent is ctx.agent
assert _ch_mod._cli_thread_id == "tid-42"
def test_start_propagates_send_thinking(self):
"""send_thinking flag must reach _start_channels_bus_mode."""
from EvoScientist.commands.implementation.channel import ChannelCommand
ctx, _ui = _ctx()
config = SimpleNamespace(
channel_enabled="telegram",
channel_send_thinking=False,
)
captured = {}
def _fake_start(cfg, agent, thread_id, *, send_thinking=None):
captured["agent"] = agent
captured["thread_id"] = thread_id
captured["send_thinking"] = send_thinking
with (
patch(
"EvoScientist.cli.channel._channels_is_running",
return_value=False,
),
patch(
"EvoScientist.cli.channel._start_channels_bus_mode",
_fake_start,
),
patch(
"EvoScientist.config.load_config",
return_value=config,
),
):
_run(ChannelCommand().execute(ctx, ["telegram"]))
assert captured["agent"] is ctx.agent
assert captured["thread_id"] == "tid-42"
assert captured["send_thinking"] is False
class TestAddToRunningPath:
def test_add_to_running_sets_cli_agent_globals(self):
import EvoScientist.cli.channel as _ch_mod
from EvoScientist.commands.implementation.channel import ChannelCommand
ctx, _ui = _ctx()
config = SimpleNamespace(
channel_enabled="telegram",
channel_send_thinking=False,
)
with (
patch(
"EvoScientist.cli.channel._channels_is_running",
return_value=True,
),
patch(
"EvoScientist.cli.channel._add_channel_to_running_bus",
),
patch(
"EvoScientist.config.load_config",
return_value=config,
),
):
_run(ChannelCommand().execute(ctx, ["discord"]))
assert _ch_mod._cli_agent is ctx.agent
assert _ch_mod._cli_thread_id == "tid-42"
def test_add_to_running_propagates_send_thinking(self):
"""Adding to a running bus must honor config.channel_send_thinking."""
from EvoScientist.commands.implementation.channel import ChannelCommand
ctx, _ui = _ctx()
config = SimpleNamespace(
channel_enabled="telegram",
channel_send_thinking=True,
)
captured = {}
def _fake_add(channel_type, cfg, *, send_thinking=None):
captured["channel_type"] = channel_type
captured["send_thinking"] = send_thinking
with (
patch(
"EvoScientist.cli.channel._channels_is_running",
return_value=True,
),
patch(
"EvoScientist.cli.channel._add_channel_to_running_bus",
_fake_add,
),
patch(
"EvoScientist.config.load_config",
return_value=config,
),
):
_run(ChannelCommand().execute(ctx, ["discord"]))
assert captured["channel_type"] == "discord"
assert captured["send_thinking"] is True
class TestStatusPath:
def test_status_without_running_channels(self):
from EvoScientist.commands.implementation.channel import ChannelCommand
ctx, ui = _ctx()
config = SimpleNamespace(channel_enabled="", channel_send_thinking=False)
with (
patch(
"EvoScientist.cli.channel._channels_is_running",
return_value=False,
),
patch(
"EvoScientist.cli.channel._channels_running_list",
return_value=[],
),
patch(
"EvoScientist.config.load_config",
return_value=config,
),
):
_run(ChannelCommand().execute(ctx, ["status"]))
msgs = [c.args[0] for c in ui.append_system.call_args_list]
assert any("No messaging channels" in m for m in msgs)