simplify docstrings, fix some bugs

This commit is contained in:
balyan.sid@gmail.com 2026-03-15 01:12:16 +05:30
parent 861202b56c
commit 9001b34146
6 changed files with 37 additions and 196 deletions

View file

@ -1,10 +1,4 @@
"""Tests for the local persistent shell backend.
Unit tests cover config plumbing (no real shell needed).
Integration tests run real commands no external dependencies required.
pytest tests/tools/test_local_persistent.py -v
"""
"""Tests for the local persistent shell backend."""
import glob as glob_mod
@ -14,10 +8,6 @@ from tools.environments.local import LocalEnvironment
from tools.environments.persistent_shell import PersistentShellMixin
# ---------------------------------------------------------------------------
# Unit tests — config plumbing
# ---------------------------------------------------------------------------
class TestLocalConfig:
def test_local_persistent_default_false(self, monkeypatch):
monkeypatch.delenv("TERMINAL_LOCAL_PERSISTENT", raising=False)
@ -36,8 +26,6 @@ class TestLocalConfig:
class TestMergeOutput:
"""Test the shared _merge_output static method."""
def test_stdout_only(self):
assert PersistentShellMixin._merge_output("out", "") == "out"
@ -54,13 +42,7 @@ class TestMergeOutput:
assert PersistentShellMixin._merge_output("out\n\n", "err\n") == "out\nerr"
# ---------------------------------------------------------------------------
# One-shot regression tests — ensure refactor didn't break anything
# ---------------------------------------------------------------------------
class TestLocalOneShotRegression:
"""Verify one-shot mode still works after adding the mixin."""
def test_echo(self):
env = LocalEnvironment(persistent=False)
r = env.execute("echo hello")
@ -75,22 +57,14 @@ class TestLocalOneShotRegression:
env.cleanup()
def test_state_does_not_persist(self):
"""Env vars set in one command should NOT survive in one-shot mode."""
env = LocalEnvironment(persistent=False)
env.execute("export HERMES_ONESHOT_LOCAL=yes")
r = env.execute("echo $HERMES_ONESHOT_LOCAL")
# In one-shot mode, env var should not persist
assert r["output"].strip() == ""
env.cleanup()
# ---------------------------------------------------------------------------
# Persistent shell integration tests
# ---------------------------------------------------------------------------
class TestLocalPersistent:
"""Persistent mode: state persists across execute() calls."""
@pytest.fixture
def env(self):
e = LocalEnvironment(persistent=True)
@ -128,8 +102,7 @@ class TestLocalPersistent:
def test_timeout_then_recovery(self, env):
r = env.execute("sleep 999", timeout=2)
assert r["returncode"] in (124, 130) # timeout or interrupted
# Shell should survive — next command works
assert r["returncode"] in (124, 130)
r = env.execute("echo alive")
assert r["returncode"] == 0
assert "alive" in r["output"]
@ -143,7 +116,6 @@ class TestLocalPersistent:
assert lines[-1] == "1000"
def test_shell_variable_persists(self, env):
"""Shell variables (not exported) should also persist."""
env.execute("MY_LOCAL_VAR=hello123")
r = env.execute("echo $MY_LOCAL_VAR")
assert r["output"].strip() == "hello123"
@ -151,14 +123,12 @@ class TestLocalPersistent:
def test_cleanup_removes_temp_files(self, env):
env.execute("echo warmup")
prefix = env._temp_prefix
# Temp files should exist
assert len(glob_mod.glob(f"{prefix}-*")) > 0
env.cleanup()
remaining = glob_mod.glob(f"{prefix}-*")
assert remaining == []
def test_state_does_not_leak_between_instances(self):
"""Two separate persistent instances don't share state."""
env1 = LocalEnvironment(persistent=True)
env2 = LocalEnvironment(persistent=True)
try:
@ -170,7 +140,6 @@ class TestLocalPersistent:
env2.cleanup()
def test_special_characters_in_command(self, env):
"""Commands with quotes and special chars should work."""
r = env.execute("echo 'hello world'")
assert r["output"].strip() == "hello world"

View file

@ -1,15 +1,4 @@
"""Tests for the SSH remote execution environment backend.
Unit tests (no SSH required) cover pure logic: command building, output merging,
config plumbing.
Integration tests require a real SSH target. Set TERMINAL_SSH_HOST and
TERMINAL_SSH_USER to enable them. In CI, start an sshd container or enable
the localhost SSH service.
TERMINAL_SSH_HOST=localhost TERMINAL_SSH_USER=$(whoami) \
pytest tests/tools/test_ssh_environment.py -v
"""
"""Tests for the SSH remote execution environment backend."""
import json
import os
@ -20,11 +9,6 @@ import pytest
from tools.environments.ssh import SSHEnvironment
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
_SSH_HOST = os.getenv("TERMINAL_SSH_HOST", "")
_SSH_USER = os.getenv("TERMINAL_SSH_USER", "")
_SSH_PORT = int(os.getenv("TERMINAL_SSH_PORT", "22"))
@ -39,7 +23,6 @@ requires_ssh = pytest.mark.skipif(
def _run(command, task_id="ssh_test", **kwargs):
"""Call terminal_tool like an LLM would, return parsed JSON."""
from tools.terminal_tool import terminal_tool
return json.loads(terminal_tool(command, task_id=task_id, **kwargs))
@ -49,12 +32,7 @@ def _cleanup(task_id="ssh_test"):
cleanup_vm(task_id)
# ---------------------------------------------------------------------------
# Unit tests — no SSH connection needed
# ---------------------------------------------------------------------------
class TestBuildSSHCommand:
"""Pure logic: verify the ssh command list is assembled correctly."""
@pytest.fixture(autouse=True)
def _mock_connection(self, monkeypatch):
@ -100,12 +78,7 @@ class TestTerminalToolConfig:
assert _get_env_config()["ssh_persistent"] is True
# ---------------------------------------------------------------------------
# Integration tests — real SSH, through terminal_tool() interface
# ---------------------------------------------------------------------------
def _setup_ssh_env(monkeypatch, persistent: bool):
"""Configure env vars for SSH integration tests."""
monkeypatch.setenv("TERMINAL_ENV", "ssh")
monkeypatch.setenv("TERMINAL_SSH_HOST", _SSH_HOST)
monkeypatch.setenv("TERMINAL_SSH_USER", _SSH_USER)
@ -118,7 +91,6 @@ def _setup_ssh_env(monkeypatch, persistent: bool):
@requires_ssh
class TestOneShotSSH:
"""One-shot mode: each command is a fresh ssh invocation."""
@pytest.fixture(autouse=True)
def _setup(self, monkeypatch):
@ -136,7 +108,6 @@ class TestOneShotSSH:
assert r["exit_code"] == 42
def test_state_does_not_persist(self):
"""Env vars set in one command should NOT survive to the next."""
_run("export HERMES_ONESHOT_TEST=yes")
r = _run("echo $HERMES_ONESHOT_TEST")
assert r["output"].strip() == ""
@ -144,7 +115,6 @@ class TestOneShotSSH:
@requires_ssh
class TestPersistentSSH:
"""Persistent mode: single long-lived shell, state persists."""
@pytest.fixture(autouse=True)
def _setup(self, monkeypatch):
@ -184,7 +154,6 @@ class TestPersistentSSH:
def test_timeout_then_recovery(self):
r = _run("sleep 999", timeout=2)
assert r["exit_code"] == 124
# Shell should survive — next command works
r = _run("echo alive")
assert r["exit_code"] == 0
assert "alive" in r["output"]