summaryrefslogtreecommitdiff
path: root/tests/test-ai-vterm--reuse-existing-claude.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-08 19:21:26 -0500
committerCraig Jennings <c@cjennings.net>2026-05-08 19:21:26 -0500
commitfb5663954ca8c692f1bebbfdb2dd139e8a63667f (patch)
tree798fdbd18a7469c48ef1c7da307892b77542f412 /tests/test-ai-vterm--reuse-existing-claude.el
parent374999971a409af58e71fc4e5c378e5ec244497e (diff)
downloaddotemacs-fb5663954ca8c692f1bebbfdb2dd139e8a63667f.tar.gz
dotemacs-fb5663954ca8c692f1bebbfdb2dd139e8a63667f.zip
feat(ai-vterm): F9 toggle/redisplay/pick + persistent split geometry
F9 was a single command that always opened the project picker. Three small frustrations stacked up. With one claude buffer open and not visible, F9 was a redundant prompt to pick a project that already had a session. With claude visible, there was no way to bury it without M-x quit-window. With two projects' buffers alive, swapping between them was a buffer-switch chore. F9 is now a dispatch: - Claude visible in this frame: quit the window (toggle off) and capture the geometry first. - Exactly one claude buffer alive but hidden: re-display it (DWIM single-buffer case). - Zero or two-plus alive: fall through to the project picker. C-F9 is the always-pick-project entry point for explicit project switches. M-F9 is a buffer picker over the alive claude buffers. If a claude window is currently shown, the picked buffer replaces it in that window so the split orientation and size carry over. The shown buffer sorts last in the picker with a [shown] marker so RET picks "the other one." Split geometry persists across toggles. Two module-level vars (cj/--ai-vterm-last-direction, cj/--ai-vterm-last-size) capture at toggle-off and feed a custom display action. After M-S-t flips claude from right to bottom, F9 toggle-off-then-on returns it at the bottom. After a mouse resize, the next toggle restores that fraction. State is per-session. Restarts reset to default right/0.5. Two display-buffer fixes came out of testing: - save-window-excursion around (vterm name) keeps the dashboard from being buried on a fresh F9 at startup. vterm calls pop-to-buffer-same-window internally, which would otherwise replace the selected window's buffer before the alist could route the new one. - The action chain swaps display-buffer-use-some-window for a more specific cj/--ai-vterm-reuse-existing-claude. The generic version stole non-claude windows on C-F9 when the user was focused inside claude (claude on bottom, code on top -> new project landed in the code window). The specific version only reuses windows that already show a claude buffer. I reclaimed C-F9 from the gptel toggle in ai-config.el. C-; a t still binds gptel. I added eight new test files (claude-buffers, displayed-claude-window, dispatch, pick-buffer-candidates, window-geometry, capture-state, display-saved, reuse-existing-claude) plus a regression test on cj/--ai-vterm-show-or-create for the dashboard-preservation fix. All 73 ai-vterm tests pass and the full make test suite is green.
Diffstat (limited to 'tests/test-ai-vterm--reuse-existing-claude.el')
-rw-r--r--tests/test-ai-vterm--reuse-existing-claude.el103
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/test-ai-vterm--reuse-existing-claude.el b/tests/test-ai-vterm--reuse-existing-claude.el
new file mode 100644
index 00000000..4668188d
--- /dev/null
+++ b/tests/test-ai-vterm--reuse-existing-claude.el
@@ -0,0 +1,103 @@
+;;; test-ai-vterm--reuse-existing-claude.el --- Tests for reuse-existing-claude action -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; The action looks for any window in the selected frame whose buffer
+;; satisfies `cj/--ai-vterm-buffer-p'. When found, swaps that
+;; window's buffer for the one being displayed and returns the
+;; window. When not found, returns nil so the next action in the
+;; chain runs.
+;;
+;; This is the action that keeps C-F9 (project-switch) from stealing
+;; a non-claude window when the user is focused inside claude.
+
+;;; Code:
+
+(require 'ert)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'ai-vterm)
+
+(defun test-ai-vterm--reuse-cleanup ()
+ "Kill any leftover claude-prefixed buffers."
+ (dolist (b (buffer-list))
+ (when (string-prefix-p "claude [" (buffer-name b))
+ (kill-buffer b))))
+
+(ert-deftest test-ai-vterm--reuse-existing-claude-swaps-buffer-when-window-exists ()
+ "Normal: a claude window exists -> swap its buffer, return the window."
+ (test-ai-vterm--reuse-cleanup)
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((existing (get-buffer-create "claude [existing]"))
+ (new-buf (get-buffer-create "claude [new]"))
+ (split (split-window (selected-window) nil 'right)))
+ (unwind-protect
+ (progn
+ (set-window-buffer split existing)
+ (let ((result (cj/--ai-vterm-reuse-existing-claude new-buf nil)))
+ (should (eq result split))
+ (should (eq (window-buffer split) new-buf))))
+ (kill-buffer existing)
+ (kill-buffer new-buf)))))
+
+(ert-deftest test-ai-vterm--reuse-existing-claude-returns-nil-when-no-claude-window ()
+ "Boundary: no claude window in frame -> nil (chain continues to next action)."
+ (test-ai-vterm--reuse-cleanup)
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((new-buf (get-buffer-create "claude [no-existing]")))
+ (unwind-protect
+ (should (null (cj/--ai-vterm-reuse-existing-claude new-buf nil)))
+ (kill-buffer new-buf)))))
+
+(ert-deftest test-ai-vterm--reuse-existing-claude-leaves-non-claude-windows-alone ()
+ "Boundary: only non-claude windows in frame -> nil; other windows untouched."
+ (test-ai-vterm--reuse-cleanup)
+ (save-window-excursion
+ (delete-other-windows)
+ (let ((code-buf (get-buffer-create "*test-code-buffer*"))
+ (new-claude (get-buffer-create "claude [new-here]"))
+ (other-win (split-window (selected-window) nil 'right)))
+ (unwind-protect
+ (progn
+ (set-window-buffer (selected-window) code-buf)
+ (set-window-buffer other-win code-buf)
+ (let ((result (cj/--ai-vterm-reuse-existing-claude
+ new-claude nil)))
+ (should (null result))
+ (should (eq (window-buffer (selected-window)) code-buf))
+ (should (eq (window-buffer other-win) code-buf))))
+ (kill-buffer code-buf)
+ (kill-buffer new-claude)))))
+
+(ert-deftest test-ai-vterm--reuse-existing-claude-preserves-non-claude-window-when-swapping ()
+ "Normal: swap claude window only; the other window keeps its buffer.
+
+This is the C-F9-from-claude regression: with claude at the bottom
+and code on top, switching projects must replace the bottom window's
+buffer, not the top window's."
+ (test-ai-vterm--reuse-cleanup)
+ (save-window-excursion
+ (delete-other-windows)
+ (let* ((code-buf (get-buffer-create "*test-code-top*"))
+ (claude-a (get-buffer-create "claude [a]"))
+ (claude-b (get-buffer-create "claude [b]"))
+ (top-win (selected-window))
+ (bottom-win (split-window top-win nil 'below)))
+ (unwind-protect
+ (progn
+ (set-window-buffer top-win code-buf)
+ (set-window-buffer bottom-win claude-a)
+ ;; Focus the claude window -- this is the regression scenario.
+ (select-window bottom-win)
+ (let ((result (cj/--ai-vterm-reuse-existing-claude
+ claude-b nil)))
+ (should (eq result bottom-win))
+ (should (eq (window-buffer bottom-win) claude-b))
+ (should (eq (window-buffer top-win) code-buf))))
+ (kill-buffer code-buf)
+ (kill-buffer claude-a)
+ (kill-buffer claude-b)))))
+
+(provide 'test-ai-vterm--reuse-existing-claude)
+;;; test-ai-vterm--reuse-existing-claude.el ends here