hermes-agent/tests/cli/test_cli_background_tui_refresh.py
kshitijk4poor 66827f8947 chore: prune unused imports and duplicate import redefinitions
Remove unused imports (F401) and duplicate/shadowed import
redefinitions (F811) across the codebase using ruff's safe
autofixes. No behavioral changes -- imports only.

- ~1400 safe autofixes applied across 644 files (net -1072 lines)
- __init__.py re-exports preserved (excluded from F401 removal so
  public re-export surfaces stay intact)
- Re-exports that are imported or monkeypatched by tests but look
  unused in their defining module are kept with explicit # noqa:
  F401 (gateway/run.py load_dotenv; run_agent re-exports from
  agent.message_sanitization, agent.context_compressor,
  agent.retry_utils, agent.prompt_builder, agent.process_bootstrap,
  agent.codex_responses_adapter)
- Unsafe F841 (unused-variable) fixes deliberately skipped -- those
  can change behavior when the RHS has side effects
- ruff lints remain disabled in pyproject.toml (only PLW1514 is
  selected); this is a one-time cleanup, not a config change

Verification:
- python -m compileall: clean
- pytest --collect-only: all 27161 tests collect (zero import errors)
- core entry points import clean (run_agent, model_tools, cli,
  toolsets, hermes_state, batch_runner, gateway)
- static scan: every name any test imports directly from an edited
  module still resolves
2026-05-28 22:26:25 -07:00

102 lines
3.2 KiB
Python

"""Tests for CLI background command TUI refresh behavior.
Ensures the TUI is properly refreshed before printing background task output
to prevent spinner/status bar overlap (#2718).
"""
from unittest.mock import MagicMock, patch
from cli import HermesCLI
def _make_cli():
"""Create a minimal HermesCLI instance for testing."""
cli_obj = HermesCLI.__new__(HermesCLI)
cli_obj.model = "test-model"
cli_obj._background_tasks = {}
cli_obj._background_task_counter = 0
cli_obj.conversation_history = []
cli_obj.agent = None
cli_obj._app = None
return cli_obj
class TestBackgroundCommandTuiRefresh:
"""Tests for TUI refresh in background command output."""
def test_invalidate_called_before_success_output(self):
"""App.invalidate() is called before printing background success output."""
cli_obj = _make_cli()
mock_app = MagicMock()
cli_obj._app = mock_app
# Track call order
call_order = []
original_invalidate = mock_app.invalidate
def track_invalidate():
call_order.append("invalidate")
return original_invalidate()
mock_app.invalidate = track_invalidate
# Patch print to track when it's called
with patch("builtins.print") as mock_print:
mock_print.side_effect = lambda *args, **kwargs: call_order.append("print")
# Simulate the background task output code path
if cli_obj._app:
cli_obj._app.invalidate()
import time
time.sleep(0.01) # reduced for test
print()
# Verify invalidate was called before print
assert call_order[0] == "invalidate"
assert "print" in call_order
def test_invalidate_called_before_error_output(self):
"""App.invalidate() is called before printing background error output."""
cli_obj = _make_cli()
mock_app = MagicMock()
cli_obj._app = mock_app
call_order = []
mock_app.invalidate.side_effect = lambda: call_order.append("invalidate")
with patch("builtins.print") as mock_print:
mock_print.side_effect = lambda *args, **kwargs: call_order.append("print")
# Simulate error path
if cli_obj._app:
cli_obj._app.invalidate()
import time
time.sleep(0.01)
print()
assert call_order[0] == "invalidate"
assert "print" in call_order
def test_no_crash_when_app_is_none(self):
"""No crash when _app is None (non-TUI mode)."""
cli_obj = _make_cli()
cli_obj._app = None
# This should not raise
if cli_obj._app:
cli_obj._app.invalidate()
# If we get here without exception, test passes
def test_background_task_thread_safety(self):
"""Background task tracking is thread-safe."""
cli_obj = _make_cli()
# Simulate adding and removing background tasks
task_id = "test_task_1"
cli_obj._background_tasks[task_id] = MagicMock()
assert task_id in cli_obj._background_tasks
# Clean up
cli_obj._background_tasks.pop(task_id, None)
assert task_id not in cli_obj._background_tasks