From eab070e5b542f525340ee7f07ea0560944639721 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Fri, 8 May 2026 19:21:26 -0500 Subject: 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. --- tests/test-ai-vterm--dispatch.el | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/test-ai-vterm--dispatch.el (limited to 'tests/test-ai-vterm--dispatch.el') diff --git a/tests/test-ai-vterm--dispatch.el b/tests/test-ai-vterm--dispatch.el new file mode 100644 index 000000000..3c0ae7667 --- /dev/null +++ b/tests/test-ai-vterm--dispatch.el @@ -0,0 +1,70 @@ +;;; test-ai-vterm--dispatch.el --- Tests for cj/--ai-vterm-dispatch -*- lexical-binding: t; -*- + +;;; Commentary: +;; The dispatch helper is a pure decision function used by F9. +;; Returns one of (toggle-off . WIN), (redisplay-single . BUF), +;; or (pick-project) based on whether a claude buffer is currently +;; displayed and how many alive claude buffers exist. Tests mock the +;; two underlying helpers so the dispatch logic can be exercised +;; without touching real windows. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'ai-vterm) + +(defun test-ai-vterm--dispatch-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--dispatch-window-displayed-returns-toggle-off () + "Normal: displayed claude window -> (toggle-off . WIN)." + (let ((sentinel-win 'fake-window)) + (cl-letf (((symbol-function 'cj/--ai-vterm-displayed-claude-window) + (lambda (&optional _frame) sentinel-win))) + (should (equal (cj/--ai-vterm-dispatch) + (cons 'toggle-off sentinel-win)))))) + +(ert-deftest test-ai-vterm--dispatch-no-window-single-buffer-returns-redisplay () + "Normal: no displayed claude, exactly one alive buffer -> redisplay-single." + (test-ai-vterm--dispatch-cleanup) + (let ((b1 (get-buffer-create "claude [single]"))) + (unwind-protect + (cl-letf (((symbol-function 'cj/--ai-vterm-displayed-claude-window) + (lambda (&optional _frame) nil)) + ((symbol-function 'cj/--ai-vterm-claude-buffers) + (lambda () (list b1)))) + (should (equal (cj/--ai-vterm-dispatch) + (cons 'redisplay-single b1)))) + (kill-buffer b1)))) + +(ert-deftest test-ai-vterm--dispatch-no-window-multiple-buffers-returns-pick-project () + "Normal: no displayed claude, 2+ alive buffers -> pick-project." + (test-ai-vterm--dispatch-cleanup) + (let ((b1 (get-buffer-create "claude [a]")) + (b2 (get-buffer-create "claude [b]"))) + (unwind-protect + (cl-letf (((symbol-function 'cj/--ai-vterm-displayed-claude-window) + (lambda (&optional _frame) nil)) + ((symbol-function 'cj/--ai-vterm-claude-buffers) + (lambda () (list b1 b2)))) + (should (equal (cj/--ai-vterm-dispatch) '(pick-project)))) + (kill-buffer b1) + (kill-buffer b2)))) + +(ert-deftest test-ai-vterm--dispatch-no-window-zero-buffers-returns-pick-project () + "Boundary: no displayed claude, zero alive buffers -> pick-project." + (test-ai-vterm--dispatch-cleanup) + (cl-letf (((symbol-function 'cj/--ai-vterm-displayed-claude-window) + (lambda (&optional _frame) nil)) + ((symbol-function 'cj/--ai-vterm-claude-buffers) + (lambda () nil))) + (should (equal (cj/--ai-vterm-dispatch) '(pick-project))))) + +(provide 'test-ai-vterm--dispatch) +;;; test-ai-vterm--dispatch.el ends here -- cgit v1.2.3