Attach to existing tmux Claude session

This commit is contained in:
2026-02-17 04:04:28 +09:00
parent e616bebdb9
commit b83929fac2
7 changed files with 133 additions and 39 deletions

View File

@@ -7,7 +7,7 @@ from lazy_enter.config import Config
def test_config_defaults():
config = Config()
assert config.default_shell == "claude"
assert config.tmux_session_name == "claude"
assert config.pty_read_timeout == 5
assert config.output_buffer_interval == 2.0
assert config.max_message_length == 3000

View File

@@ -1,22 +1,84 @@
"""PtyManager 테스트."""
from __future__ import annotations
from collections.abc import Sequence
import pytest
from lazy_enter.pty_manager import PtyManager
class FakeSpawn:
def __init__(self, *_args: object, **_kwargs: object) -> None:
self.before = ""
self._alive = True
def isalive(self) -> bool:
return self._alive
def sendline(self, _text: str) -> None:
return None
def expect(self, *_args: object, **_kwargs: object) -> None:
return None
def close(self, force: bool = False) -> None:
assert force is True
self._alive = False
def test_initial_state():
pty = PtyManager("echo hello")
pty = PtyManager("claude")
assert not pty.is_alive
def test_start_and_stop():
pty = PtyManager("cat")
try:
pty.start()
except OSError as exc:
pytest.skip(f"PTY unavailable in this environment: {exc}")
def test_start_raises_when_tmux_session_missing(monkeypatch: pytest.MonkeyPatch):
def fake_run(
cmd: Sequence[str],
check: bool,
capture_output: bool,
text: bool,
):
assert list(cmd) == ["tmux", "has-session", "-t", "claude"]
assert check is False
assert capture_output is True
assert text is True
class Result:
returncode = 1
return Result()
monkeypatch.setattr("lazy_enter.pty_manager.subprocess.run", fake_run)
pty = PtyManager("claude")
with pytest.raises(RuntimeError):
pty.start()
def test_start_and_stop_attach(monkeypatch: pytest.MonkeyPatch):
def fake_run(
_cmd: Sequence[str],
check: bool,
capture_output: bool,
text: bool,
):
assert check is False
assert capture_output is True
assert text is True
class Result:
returncode = 0
return Result()
monkeypatch.setattr("lazy_enter.pty_manager.subprocess.run", fake_run)
monkeypatch.setattr("lazy_enter.pty_manager.pexpect.spawn", FakeSpawn)
pty = PtyManager("claude")
pty.start()
assert pty.is_alive
pty.stop()
assert not pty.is_alive