Files
EvoScientist/tests/test_tui_command_sync.py
T
dinos 690b903f85 test: standardize async tests on pytest-asyncio auto mode (#338)
* chore: add pytest-asyncio in auto mode

* test: migrate channel and stream tests to native async

Convert run_async() wrapper tests to plain 'async def test_*' under
pytest-asyncio auto mode. collect_events() in stream_v3_fakes becomes a
coroutine awaited at every call site.

* test: migrate command and model/middleware tests to native async

Convert run_async() wrappers (import, alias, and fixture forms) to plain
'async def test_*'. Multi-call tests merge onto one loop as sequential
awaits; none asserted on loop identity.

* test: migrate TUI, notifier, gateway, and session tests to native async

TUI/notifier/gateway files convert run_async wrappers to plain async
tests. test_sessions.py's unittest.TestCase classes move to
unittest.IsolatedAsyncioTestCase (pytest-asyncio does not await async
methods on plain TestCase; converting blindly would have made ~70 tests
silently vacuous). Its setUpClass keeps a one-shot asyncio.run() since
IsolatedAsyncioTestCase has no async class-level hook. TestLoadingWidget
in test_tui_widgets.py drops its TestCase base for the same reason.

* test: replace direct asyncio.run() calls with native async tests

Convert tests that called asyncio.run() (directly or via a local _run
helper) to plain 'async def test_*'; delete the local helpers.

* test: drop undeclared anyio markers and delete run_async helper

The @pytest.mark.anyio tests relied on anyio being a transitive dep of
httpx; auto-mode pytest-asyncio collects them natively. run_async() and
its fixture are unreferenced after the migration, so remove them —
pytest-asyncio's per-test loop teardown covers the pending-task
cancellation the helper existed for (verified: full suite runs with no
'Event loop is closed' errors or destroyed-task warnings).
2026-07-08 18:37:48 +00:00

119 lines
3.6 KiB
Python

"""Tests for TUI command-completion state sync."""
from types import SimpleNamespace
import pytest
from EvoScientist.commands.base import ChannelRuntime, CommandContext
pytest.importorskip("textual")
class _Loader:
def __init__(self) -> None:
self.adopt_calls: list[object] = []
self.agent: object | None = None
def adopt(self, agent: object) -> None:
self.adopt_calls.append(agent)
self.agent = agent
class _StubApp:
def __init__(self) -> None:
self._agent_loader = _Loader()
self._conversation_tid = "thread-1"
self._channel_runtime = ChannelRuntime()
self.model_updates: list[tuple[str, str | None]] = []
self.refresh_calls: list[bool] = []
def update_status_after_model_change(
self,
new_model: str,
new_provider: str | None = None,
) -> None:
self.model_updates.append((new_model, new_provider))
async def _refresh_status_snapshot(
self,
*,
reset_streaming_text: bool = True,
) -> None:
self.refresh_calls.append(reset_streaming_text)
async def test_sync_tui_command_completion_adopts_agent_swap(monkeypatch):
import EvoScientist.cli.tui_interactive as tui_mod
from EvoScientist import EvoScientist as evosci_mod
app = _StubApp()
ctx = CommandContext(
agent="new-agent",
thread_id="thread-1",
ui=SimpleNamespace(),
)
cmd = SimpleNamespace(name="/model")
monkeypatch.setattr(
evosci_mod,
"_ensure_config",
lambda: SimpleNamespace(model="gpt-5.5", provider="openai"),
)
monkeypatch.setattr(tui_mod, "_channels_is_running", lambda: True)
app._channel_runtime.bind("old-agent", "old-thread")
await tui_mod._sync_tui_command_completion(app, ctx, "old-agent", cmd)
assert app._agent_loader.adopt_calls == ["new-agent"]
assert app.model_updates == [("gpt-5.5", "openai")]
assert app.refresh_calls == [True]
assert app._channel_runtime.agent == "new-agent"
assert app._channel_runtime.thread_id == "thread-1"
async def test_sync_tui_command_completion_refreshes_without_agent_swap(monkeypatch):
import EvoScientist.cli.tui_interactive as tui_mod
app = _StubApp()
ctx = CommandContext(
agent="same-agent",
thread_id="thread-1",
ui=SimpleNamespace(),
)
cmd = SimpleNamespace(name="/compact")
monkeypatch.setattr(tui_mod, "_channels_is_running", lambda: False)
await tui_mod._sync_tui_command_completion(app, ctx, "same-agent", cmd)
assert app._agent_loader.adopt_calls == []
assert app.model_updates == []
assert app.refresh_calls == [True]
async def test_sync_tui_rebinds_runtime_on_thread_rotation_without_agent_swap(
monkeypatch,
):
"""Regression: ``/new`` and ``/resume`` rotate ``app._conversation_tid``
without swapping the agent. The runtime must still pick up the new
thread id so the bus contract stays consistent with serve mode."""
import EvoScientist.cli.tui_interactive as tui_mod
app = _StubApp()
app._conversation_tid = "rotated-thread"
app._agent_loader.agent = "same-agent"
app._channel_runtime.bind("same-agent", "old-thread")
ctx = CommandContext(
agent="same-agent",
thread_id="rotated-thread",
ui=SimpleNamespace(),
)
cmd = SimpleNamespace(name="/new")
monkeypatch.setattr(tui_mod, "_channels_is_running", lambda: True)
await tui_mod._sync_tui_command_completion(app, ctx, "same-agent", cmd)
assert app._channel_runtime.agent == "same-agent"
assert app._channel_runtime.thread_id == "rotated-thread"