From 1d93e1a6569e4193c2b078a3d5df0bf47eeba9df Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Thu, 7 May 2026 22:09:15 -0500 Subject: fix(ai-vterm): direction-based display + per-project tmux session names Two post-ship issues blocked practical use of the new launcher. The display rule used `display-buffer-in-side-window` with `(dedicated . t)`. Side-window dedication caused `set-window-buffer` to error during `buffer-move` (C-M-arrows), which left a half-finished swap with both sides showing the claude buffer. Then `switch-to-buffer` on a non-claude buffer in that dedicated window split instead of replacing. I rewrote the rule as `display-buffer-reuse-window -> display-buffer-use-some-window -> display-buffer-in-direction (right)`. The resulting window is ordinary, not dedicated, so swap and replace work normally. I also narrowed `vterm-toggle`'s broad lambda (which matches any vterm-mode buffer) to exclude `claude [` buffers. Otherwise vterm-toggle's `:defer` made it install last and capture our buffers first with its own bottom-split + dedicated treatment. The tmux side: vterm's auto-launch hook ran a bare `tmux\n`, so each session got an auto-named one. After an Emacs crash the tmux session would survive but I couldn't find it. A second F9 just spawned another. The launcher now sends `tmux new-session -A -s -c '; exec bash'`. The `-A` reattaches to a same-named session if it already exists. The `exec bash` keeps the tmux window alive if claude itself exits. A `cj/--ai-vterm-suppress-tmux` flag tells the existing vterm hook to skip its bare tmux step so the named launch runs instead. 11 new tests across 2 files cover the session-name and launch-command helpers. I updated tests for show-or-create and the display rule. All 34 ai-vterm tests are green. --- tests/test-ai-vterm--launch-command.el | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/test-ai-vterm--launch-command.el (limited to 'tests/test-ai-vterm--launch-command.el') diff --git a/tests/test-ai-vterm--launch-command.el b/tests/test-ai-vterm--launch-command.el new file mode 100644 index 00000000..c6b7ac2b --- /dev/null +++ b/tests/test-ai-vterm--launch-command.el @@ -0,0 +1,61 @@ +;;; test-ai-vterm--launch-command.el --- Tests for cj/--ai-vterm-launch-command -*- lexical-binding: t; -*- + +;;; Commentary: +;; The launch command is what gets typed into a fresh vterm shell to bring +;; up Claude inside a per-project tmux session. The session is named after +;; the project basename so a second F9 on the same project reattaches to +;; the running Claude rather than spawning a new one. The trailing +;; `exec bash' keeps the tmux window alive if Claude exits, leaving the +;; session intact for recovery. + +;;; Code: + +(require 'ert) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'ai-vterm) + +(ert-deftest test-ai-vterm--launch-command-uses-new-session-attach () + "Normal: starts with `tmux new-session -A' so existing sessions reattach." + (let ((cj/ai-vterm-claude-command "claude")) + (should (string-prefix-p + "tmux new-session -A " + (cj/--ai-vterm-launch-command "/code/foo"))))) + +(ert-deftest test-ai-vterm--launch-command-includes-session-name () + "Normal: the session name comes from the basename helper." + (let ((cj/ai-vterm-claude-command "claude")) + (should (string-match-p + " -s foo " + (cj/--ai-vterm-launch-command "/code/foo"))))) + +(ert-deftest test-ai-vterm--launch-command-includes-start-directory () + "Normal: `-c ' so the new session's first window starts in DIR." + (let ((cj/ai-vterm-claude-command "claude")) + (should (string-match-p + " -c /code/foo " + (cj/--ai-vterm-launch-command "/code/foo"))))) + +(ert-deftest test-ai-vterm--launch-command-includes-claude-command () + "Normal: the configured claude command is in the launched shell command." + (let ((cj/ai-vterm-claude-command "claude --some-flag")) + (should (string-match-p + "claude --some-flag" + (cj/--ai-vterm-launch-command "/code/foo"))))) + +(ert-deftest test-ai-vterm--launch-command-tails-with-exec-bash () + "Boundary: `exec bash' tails so the tmux window survives Claude exiting." + (let ((cj/ai-vterm-claude-command "claude")) + (should (string-match-p + "exec bash" + (cj/--ai-vterm-launch-command "/code/foo"))))) + +(ert-deftest test-ai-vterm--launch-command-handles-spaces-in-basename () + "Boundary: a basename with whitespace becomes hyphenated before quoting." + (let ((cj/ai-vterm-claude-command "claude")) + (should (string-match-p + " -s my-work " + (cj/--ai-vterm-launch-command "/code/my work"))))) + +(provide 'test-ai-vterm--launch-command) +;;; test-ai-vterm--launch-command.el ends here -- cgit v1.2.3