tmux-amq/tests/test_tmux_manager.py
tegwick 68475922bb
Some checks failed
tamq-ci / test (push) Failing after 6s
fix: restore interactive PTY behavior
Assistant: codex
Assistant-Model: gpt-5.6-sol
Assistant-Session: 01a03397-4d51-7fd1-8ff2-946eb22ea2bc
2026-08-24 19:28:49 +02:00

140 lines
5.4 KiB
Python

from tamq import tmux
import pytest
def test_tmux_manager_builds_tap_windows(tmp_path, monkeypatch):
calls = []
manager = tmux.TmuxManager("tamq-test")
repo_a = tmp_path / "a"
repo_b = tmp_path / "b"
repo_a.mkdir()
repo_b.mkdir()
monkeypatch.setattr(tmux, "validate_targets", lambda repos: None)
monkeypatch.setattr(tmux, "repository_paths", lambda: {"a": str(repo_a), "b": str(repo_b)})
monkeypatch.setattr(tmux.shutil, "which", lambda command: f"/bin/{command}")
def run(*args, check=True):
calls.append(args)
if args[:2] == ("list-windows", "-t"):
return "__tamq_boot"
if args[:2] == ("display-message", "-p"):
return "42"
return ""
monkeypatch.setattr(manager, "_run", run)
monkeypatch.setattr(tmux.subprocess, "run", lambda *args, **kwargs: type("R", (), {"returncode": 1})())
endpoint = manager.ensure(["a", "b"], "codex --quiet")
assert endpoint.endpoint_id == "tmux-amq-42"
assert endpoint.instance_id.startswith("tmux-amq-42-")
assert endpoint.repos == ["a", "b"]
new_session = next(call for call in calls if call and call[0] == "new-session")
assert new_session[-2:] == ("-c", str(repo_a))
assert "sleep" not in new_session
new_window = next(call for call in calls if call and call[0] == "new-window")
assert new_window[-2:] == ("-c", str(repo_b))
tap_calls = [call for call in calls if call and call[0] == "send-keys"]
assert len(tap_calls) == 2
assert all("tamq tap" in " ".join(call) for call in tap_calls)
assert all("codex --quiet" in " ".join(call) for call in tap_calls)
assert ("select-window", "-t", "tamq-test:a") in calls
assert any(call and call[0] == "set-option" for call in calls)
def test_tmux_manager_reuses_session_instance_id(tmp_path, monkeypatch):
repo = tmp_path / "a"
repo.mkdir()
manager = tmux.TmuxManager("tamq-test")
options = {"@tamq_managed": "1"}
monkeypatch.setattr(tmux, "validate_targets", lambda repos: None)
monkeypatch.setattr(tmux, "repository_paths", lambda: {"a": str(repo)})
monkeypatch.setattr(tmux.shutil, "which", lambda command: f"/bin/{command}")
def run(*args, check=True):
if args[:2] == ("display-message", "-p"):
return "42"
if args and args[0] == "show-options":
return options.get(args[-1], "")
if args and args[0] == "set-option":
options[args[-2]] = args[-1]
if args[:2] == ("list-windows", "-t"):
return "a"
return ""
monkeypatch.setattr(manager, "_run", run)
monkeypatch.setattr(
tmux.subprocess,
"run",
lambda *args, **kwargs: type("R", (), {"returncode": 0})(),
)
first = manager.ensure(["a"])
second = manager.ensure(["a"])
assert first.instance_id == second.instance_id
def test_preflight_rejects_foreign_session(tmp_path, monkeypatch):
repo = tmp_path / "a"
repo.mkdir()
manager = tmux.TmuxManager("tamq-test")
monkeypatch.setattr(tmux, "validate_targets", lambda repos: None)
monkeypatch.setattr(tmux, "repository_paths", lambda: {"a": str(repo)})
monkeypatch.setattr(tmux.shutil, "which", lambda command: f"/bin/{command}")
monkeypatch.setattr(
tmux.subprocess,
"run",
lambda *args, **kwargs: type("R", (), {"returncode": 0})(),
)
def run(*args, check=True):
if args[:2] == ("display-message", "-p"):
return "42"
return ""
monkeypatch.setattr(manager, "_run", run)
with pytest.raises(tmux.TmuxError, match="not managed by tamq"):
manager.preflight(["a"])
def test_tmux_only_plan_starts_agent_without_tap(tmp_path, monkeypatch):
manager = tmux.TmuxManager("tamq-test")
calls = []
plan = tmux.LaunchPlan(("a",), {"a": str(tmp_path)}, ("codex", "--quiet"))
def run(*args, check=True):
calls.append(args)
if args[:2] == ("display-message", "-p"):
return "42"
if args[:2] == ("list-windows", "-t"):
return "__tamq_boot"
return ""
monkeypatch.setattr(manager, "_run", run)
monkeypatch.setattr(
tmux.subprocess,
"run",
lambda *args, **kwargs: type("R", (), {"returncode": 1})(),
)
manager.ensure_plan(plan, tap=False)
send = next(call for call in calls if call and call[0] == "send-keys")
assert "codex --quiet" in send
assert "tamq tap" not in send
def test_preflight_rejects_missing_agent(tmp_path, monkeypatch):
repo = tmp_path / "a"
repo.mkdir()
manager = tmux.TmuxManager("tamq-test")
monkeypatch.setattr(tmux, "validate_targets", lambda repos: None)
monkeypatch.setattr(tmux, "repository_paths", lambda: {"a": str(repo)})
monkeypatch.setattr(tmux.shutil, "which", lambda command: None if command == "missing-agent" else f"/bin/{command}")
with pytest.raises(tmux.TmuxError, match="agent command not found"):
manager.preflight(["a"], "missing-agent")
def test_preflight_deduplicates_repositories(tmp_path, monkeypatch):
repo = tmp_path / "a"
repo.mkdir()
manager = tmux.TmuxManager("tamq-test")
monkeypatch.setattr(tmux, "validate_targets", lambda repos: None)
monkeypatch.setattr(tmux, "repository_paths", lambda: {"a": str(repo)})
monkeypatch.setattr(tmux.shutil, "which", lambda command: f"/bin/{command}")
plan = manager.preflight(["a", "a"], "codex")
assert plan.repos == ("a",)