92 lines
5.4 KiB
Python
92 lines
5.4 KiB
Python
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from failure import Failure
|
|
from loop_effects.doubles import FakeEffects
|
|
from agent_cycle import tmux
|
|
from agent_cycle.doubles import WINDOW
|
|
|
|
|
|
class TmuxCommands(unittest.TestCase):
|
|
def test_refuses_a_tmux_older_than_needed_naming_both_versions(self):
|
|
for found, reason in (("tmux 3.2", None), ("tmux 3.8", None), ("tmux next-3.4", None), ("tmux 10.0", None),
|
|
("tmux 3.1c", "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c"),
|
|
("tmux 2.9a", "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 2.9a"),
|
|
("", "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found no tmux")):
|
|
with self.subTest(found=found):
|
|
fake = FakeEffects(tmux_version=found)
|
|
self.assertEqual(tmux.unusable(fake.tmux, "loop-claude"), reason)
|
|
self.assertEqual(fake.tmux_calls, [["-V"]])
|
|
|
|
def test_names_the_session_for_the_project(self):
|
|
self.assertEqual(tmux.session_name(tmux.LOOP, "claude-config"), "loop-claude-config")
|
|
# tmux reads `.` and `:` in a target as separators.
|
|
self.assertEqual(tmux.session_name(tmux.LOOP, "my.site:2 x"), "loop-my-site-2-x")
|
|
self.assertEqual(tmux.session_name(tmux.LOOP, "Acme/Billing"), "loop-Acme-Billing")
|
|
|
|
def test_lists_the_windows_without_the_waiting_mark(self):
|
|
fake = FakeEffects(windows="item-372 ?\t0\t@1\nitem-380\t1\t@2\nno-item\t0\t@5\n")
|
|
self.assertEqual(tmux.listed("loop-proj", fake.tmux), [tmux.Listed("item-372", "@1", False, asks=True), tmux.Listed("item-380", "@2", True), tmux.Listed("no-item", "@5", False)])
|
|
self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-proj", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]])
|
|
self.assertEqual(tmux.listed("loop-proj", FakeEffects().tmux), [])
|
|
failed = lambda args: subprocess.CompletedProcess(args, 1, "item-1\t0\t@1\n", "no server")
|
|
self.assertEqual(tmux.listed("loop-proj", failed), [])
|
|
|
|
def test_the_first_window_makes_the_session_and_each_later_one_joins_it(self):
|
|
fake = FakeEffects()
|
|
window = tmux.Window("loop-proj", "item-7", "")
|
|
self.assertIsNone(tmux.start_window(window, "/r", {"LOOP_CLAUDE_X": "1"}, ["/b/loop-claude", "--chunk", "item-7", "ship it;"], True, True, fake.tmux))
|
|
self.assertIsNone(tmux.start_window(window, "/r", {}, ["/b/loop-claude"], False, True, fake.tmux))
|
|
self.assertEqual(fake.tmux_calls, [
|
|
["new-session", "-d", "-s", "loop-proj", "-n", "item-7", "-c", "/r", "-e", "LOOP_CLAUDE_X=1", "-P", "-F", "#{window_id}", "--", "/b/loop-claude", "--chunk", "item-7", "ship it\;"],
|
|
["set-option", "-t", "=loop-proj:", "@loop-root", "/r"],
|
|
["set-option", "-w", "-t", "@9", "@chunk-root", "/r"],
|
|
["set-option", "-w", "-t", "@9", "remain-on-exit", "failed"],
|
|
["new-window", "-d", "-t", "=loop-proj:", "-n", "item-7", "-c", "/r", "-P", "-F", "#{window_id}", "--", "/b/loop-claude"],
|
|
["set-option", "-w", "-t", "@9", "@chunk-root", "/r"],
|
|
["set-option", "-w", "-t", "@9", "remain-on-exit", "failed"],
|
|
])
|
|
|
|
def test_says_why_tmux_started_no_window(self):
|
|
failed = lambda args: subprocess.CompletedProcess(args, 1, "", "duplicate session: loop-proj\n")
|
|
silent = lambda args: subprocess.CompletedProcess(args, 2, "", "")
|
|
window = tmux.Window("loop-proj", "item-7", "")
|
|
self.assertEqual(tmux.start_window(window, "/r", {}, ["x"], True, True, failed), Failure("tmux could not start the window loop-proj:item-7: duplicate session: loop-proj"))
|
|
self.assertEqual(tmux.start_window(window, "/r", {}, ["x"], True, True, silent), Failure("tmux could not start the window loop-proj:item-7: it exited 2"))
|
|
|
|
def test_fails_where_it_cannot_mark_a_session_it_made_as_the_checkouts_but_not_where_remain_on_exit_fails(self):
|
|
def answering(failing: str):
|
|
def run(args):
|
|
fails = args[:1] == ["set-option"] and failing in args
|
|
return subprocess.CompletedProcess(args, 1 if fails else 0, "" if fails else "@9\n", "no such session\n" if fails else "")
|
|
return run
|
|
window = tmux.Window("loop-proj", "item-7", "")
|
|
self.assertEqual(tmux.start_window(window, "/r", {}, ["x"], True, True, answering(tmux.OWNER)), Failure("tmux started the session loop-proj but could not mark it as this checkout's: no such session"))
|
|
self.assertIsNone(tmux.start_window(window, "/r", {}, ["x"], True, True, answering("remain-on-exit")))
|
|
self.assertIsNone(tmux.start_window(window, "/r", {}, ["x"], False, True, answering(tmux.OWNER)))
|
|
|
|
def test_reads_the_checkout_the_session_names(self):
|
|
fake = FakeEffects(owner="/c/proj")
|
|
self.assertEqual(tmux.owner("loop-proj", fake.tmux), "/c/proj")
|
|
self.assertEqual(fake.tmux_calls, [["show-options", "-qv", "-t", "=loop-proj:", "@loop-root"]])
|
|
|
|
def test_marks_a_waiting_window_by_its_pane_and_clears_the_mark(self):
|
|
fake = FakeEffects()
|
|
tmux.mark(WINDOW, True, fake.tmux)
|
|
tmux.mark(WINDOW, False, fake.tmux)
|
|
tmux.close(tmux.Listed("item-7", "@3", True), fake.tmux)
|
|
self.assertEqual(fake.tmux_calls, [["rename-window", "-t", "%4", "item-7 ?"], ["rename-window", "-t", "%4", "item-7"], ["kill-window", "-t", "@3"]])
|
|
self.assertEqual(WINDOW.target, "loop-proj:item-7")
|
|
|
|
def test_shows_the_session_by_attaching_or_inside_tmux_by_switching(self):
|
|
self.assertEqual(tmux.show("loop-proj", False), ["attach-session", "-t", "=loop-proj"])
|
|
self.assertEqual(tmux.show("loop-proj", True), ["switch-client", "-t", "=loop-proj"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|