Files
EvoScientist-Multi/tests/test_cli_channel_bus_mode.py
T
dinos 5c829942d7 refactor(cli): replace channel module globals with ChannelRuntime (#197)
* refactor(cli): replace channel module globals with ChannelRuntime

Removes _cli_agent / _cli_thread_id from EvoScientist/cli/channel.py
and threads a ChannelRuntime via CommandContext.channel_runtime so
/model and /channel rebind without poking module-level state.

* fix(cli): address coderabbit review

- _auto_start_channel: bind ChannelRuntime only after
  _start_channels_bus_mode succeeds, so a startup failure no longer
  leaves a stale binding pointing at channels that never started.
- _sync_tui_command_completion (TUI) and the Rich CLI command-completion
  paths: rebind the runtime on thread rotation, not just agent swap, so
  /new and /resume keep ChannelRuntime in sync with the running thread
  (matches the serve-mode hook contract).
- test_hook_syncs_channel_runtime: pin ctx.thread_id explicitly so a
  bare MagicMock attribute can't silently mutate runtime.thread_id.
- New regression test covering the rebind-on-thread-rotation contract.
2026-04-30 18:06:22 +01:00

55 lines
1.6 KiB
Python

"""Tests for channel bus-mode thinking propagation."""
from __future__ import annotations
from types import SimpleNamespace
import pytest
from EvoScientist.cli import channel as channel_cli
@pytest.fixture(autouse=True)
def _restore_channel_globals():
"""Restore the bus-mode globals after each test."""
original = {
"_manager": channel_cli._manager,
"_bus_loop": channel_cli._bus_loop,
"_bus_thread": channel_cli._bus_thread,
}
yield
channel_cli._manager = original["_manager"]
channel_cli._bus_loop = original["_bus_loop"]
channel_cli._bus_thread = original["_bus_thread"]
def test_auto_start_channel_passes_send_thinking(monkeypatch):
from EvoScientist.commands.base import ChannelRuntime
captured = {}
def _fake_start(config, agent, thread_id, *, send_thinking=None):
captured["send_thinking"] = send_thinking
captured["thread_id"] = thread_id
captured["agent"] = agent
monkeypatch.setattr(channel_cli, "_start_channels_bus_mode", _fake_start)
monkeypatch.setattr(channel_cli, "_print_channel_panel", lambda _rows: None)
config = SimpleNamespace(channel_enabled="telegram")
agent = object()
runtime = ChannelRuntime()
channel_cli._auto_start_channel(
agent,
"thread-1",
config,
send_thinking=False,
runtime=runtime,
)
assert captured["send_thinking"] is False
assert captured["thread_id"] == "thread-1"
assert captured["agent"] is agent
assert runtime.agent is agent
assert runtime.thread_id == "thread-1"