summaryrefslogtreecommitdiff
path: root/tests/test-ai-vterm--display-saved.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--display-saved.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--display-saved.el')
-rw-r--r--tests/test-ai-vterm--display-saved.el95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/test-ai-vterm--display-saved.el b/tests/test-ai-vterm--display-saved.el
new file mode 100644
index 00000000..9cb3521c
--- /dev/null
+++ b/tests/test-ai-vterm--display-saved.el
@@ -0,0 +1,95 @@
+;;; test-ai-vterm--display-saved.el --- Tests for the display-saved action -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; The action reads `cj/--ai-vterm-last-direction' +
+;; `cj/--ai-vterm-last-size' (with default fallbacks), builds an
+;; alist with direction + the matching size key, strips any
+;; conflicting entries that came in via the rule, and delegates to
+;; `display-buffer-in-direction'.
+;;
+;; Tests stub `display-buffer-in-direction' to capture the alist
+;; that would have reached it.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'ai-vterm)
+
+(ert-deftest test-ai-vterm--display-saved-uses-defaults-when-state-nil ()
+ "Normal: nil state -> direction=right, size=cj/ai-vterm-window-width."
+ (let (received-buf received-alist
+ (cj/--ai-vterm-last-direction nil)
+ (cj/--ai-vterm-last-size nil)
+ (cj/ai-vterm-window-width 0.5))
+ (cl-letf (((symbol-function 'display-buffer-in-direction)
+ (lambda (b a)
+ (setq received-buf b received-alist a)
+ 'fake-window)))
+ (cj/--ai-vterm-display-saved 'fake-buf '((inhibit-same-window . t))))
+ (should (eq received-buf 'fake-buf))
+ (should (eq (cdr (assq 'direction received-alist)) 'right))
+ (should (= (cdr (assq 'window-width received-alist)) 0.5))
+ (should (eq (cdr (assq 'inhibit-same-window received-alist)) t))))
+
+(ert-deftest test-ai-vterm--display-saved-uses-saved-direction-and-size-below ()
+ "Normal: saved direction=below, size=0.4 -> below + window-height 0.4."
+ (let (received-alist
+ (cj/--ai-vterm-last-direction 'below)
+ (cj/--ai-vterm-last-size 0.4))
+ (cl-letf (((symbol-function 'display-buffer-in-direction)
+ (lambda (_b a) (setq received-alist a) 'fake-window)))
+ (cj/--ai-vterm-display-saved 'fake-buf nil))
+ (should (eq (cdr (assq 'direction received-alist)) 'below))
+ (should (= (cdr (assq 'window-height received-alist)) 0.4))
+ (should-not (assq 'window-width received-alist))))
+
+(ert-deftest test-ai-vterm--display-saved-uses-saved-direction-and-size-right ()
+ "Normal: saved direction=right, size=0.7 -> right + window-width 0.7."
+ (let (received-alist
+ (cj/--ai-vterm-last-direction 'right)
+ (cj/--ai-vterm-last-size 0.7))
+ (cl-letf (((symbol-function 'display-buffer-in-direction)
+ (lambda (_b a) (setq received-alist a) 'fake-window)))
+ (cj/--ai-vterm-display-saved 'fake-buf nil))
+ (should (eq (cdr (assq 'direction received-alist)) 'right))
+ (should (= (cdr (assq 'window-width received-alist)) 0.7))
+ (should-not (assq 'window-height received-alist))))
+
+(ert-deftest test-ai-vterm--display-saved-strips-conflicting-alist-entries ()
+ "Boundary: caller-supplied direction/size are stripped, saved values win."
+ (let (received-alist
+ (cj/--ai-vterm-last-direction 'right)
+ (cj/--ai-vterm-last-size 0.7))
+ (cl-letf (((symbol-function 'display-buffer-in-direction)
+ (lambda (_b a) (setq received-alist a) 'fake-window)))
+ (cj/--ai-vterm-display-saved
+ 'fake-buf
+ '((direction . below)
+ (window-width . 0.2)
+ (window-height . 0.3)
+ (inhibit-same-window . t))))
+ (should (eq (cdr (assq 'direction received-alist)) 'right))
+ (should (= (cdr (assq 'window-width received-alist)) 0.7))
+ (should (eq (cdr (assq 'inhibit-same-window received-alist)) t))
+ ;; window-height should not be in the alist when direction is right
+ ;; -- the action picks the matching size key based on direction.
+ (let ((wh-cells (cl-remove-if-not
+ (lambda (cell) (eq (car-safe cell) 'window-height))
+ received-alist)))
+ (should (null wh-cells)))))
+
+(ert-deftest test-ai-vterm--display-saved-passes-buffer-through ()
+ "Normal: BUFFER argument reaches display-buffer-in-direction unchanged."
+ (let (received-buf
+ (cj/--ai-vterm-last-direction 'right)
+ (cj/--ai-vterm-last-size 0.5))
+ (cl-letf (((symbol-function 'display-buffer-in-direction)
+ (lambda (b _a) (setq received-buf b) 'fake-window)))
+ (cj/--ai-vterm-display-saved 'sentinel-buffer nil))
+ (should (eq received-buf 'sentinel-buffer))))
+
+(provide 'test-ai-vterm--display-saved)
+;;; test-ai-vterm--display-saved.el ends here