aboutsummaryrefslogtreecommitdiff
path: root/tests/test-ai-vterm--sort-candidates.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-11 05:17:44 -0500
committerCraig Jennings <c@cjennings.net>2026-05-11 05:17:44 -0500
commitca7015486d230192e94c51c0e5d014fc83a7a35f (patch)
tree2e06df454a3915ea6074e697fc64f6981fe0177e /tests/test-ai-vterm--sort-candidates.el
parent364d69dc6f9be5d310c0ac1f0c69c31b08d82821 (diff)
downloaddotemacs-ca7015486d230192e94c51c0e5d014fc83a7a35f.tar.gz
dotemacs-ca7015486d230192e94c51c0e5d014fc83a7a35f.zip
feat(ai-vterm): surface surviving tmux sessions in the project picker
Each project's tmux session is now named `<cj/ai-vterm-tmux-session-prefix><basename>` (default `aiv-`), so `tmux ls` can be filtered to AI-vterm's own sessions. After an Emacs crash the C-F9 project picker reads `tmux list-sessions`, matches surviving sessions back to their directories, and sorts those to the top: `[detached]` when only the tmux session is alive, `[running]` when a vterm buffer exists. The rest follow alphabetically. With tmux missing or no server running, it falls back to a plain alphabetical list. The picker's collection is a completion table that pins display order so Vertico doesn't re-sort and undo the active-first grouping. The prefix is a new `defcustom` rather than `claude-`, which collides with hand-rolled tmux sessions. Sessions named before this change use the bare basename and won't be matched afterward. One `tmux kill-server` clears any orphans.
Diffstat (limited to 'tests/test-ai-vterm--sort-candidates.el')
-rw-r--r--tests/test-ai-vterm--sort-candidates.el51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test-ai-vterm--sort-candidates.el b/tests/test-ai-vterm--sort-candidates.el
new file mode 100644
index 00000000..0b602083
--- /dev/null
+++ b/tests/test-ai-vterm--sort-candidates.el
@@ -0,0 +1,51 @@
+;;; test-ai-vterm--sort-candidates.el --- Tests for cj/--ai-vterm-sort-candidates -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; The project picker lists candidates with a live tmux session first
+;; (so a Claude that survived an Emacs crash is easy to get back to),
+;; then everything else. Within each group the order is alphabetical
+;; by abbreviated path.
+
+;;; Code:
+
+(require 'ert)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'ai-vterm)
+
+(ert-deftest test-ai-vterm--sort-candidates-active-first-then-alpha ()
+ "Normal: the one project with a live session leads; the rest go alpha."
+ (let ((cj/ai-vterm-tmux-session-prefix "aiv-"))
+ (should (equal (cj/--ai-vterm-sort-candidates
+ '("/c/foo" "/c/bar" "/c/baz")
+ '("aiv-bar"))
+ '("/c/bar" "/c/baz" "/c/foo")))))
+
+(ert-deftest test-ai-vterm--sort-candidates-multiple-active-each-group-alpha ()
+ "Normal: both groups sort alphabetically internally."
+ (let ((cj/ai-vterm-tmux-session-prefix "aiv-"))
+ (should (equal (cj/--ai-vterm-sort-candidates
+ '("/c/foo" "/c/bar" "/c/baz")
+ '("aiv-foo" "aiv-bar"))
+ '("/c/bar" "/c/foo" "/c/baz")))))
+
+(ert-deftest test-ai-vterm--sort-candidates-no-sessions-is-plain-alpha ()
+ "Boundary: nil session set -> a plain alphabetical list."
+ (let ((cj/ai-vterm-tmux-session-prefix "aiv-"))
+ (should (equal (cj/--ai-vterm-sort-candidates
+ '("/c/foo" "/c/bar") nil)
+ '("/c/bar" "/c/foo")))))
+
+(ert-deftest test-ai-vterm--sort-candidates-empty-dirs-yields-nil ()
+ "Boundary: no candidates -> nil."
+ (should (null (cj/--ai-vterm-sort-candidates nil '("aiv-foo")))))
+
+(ert-deftest test-ai-vterm--session-active-p-matches-by-derived-name ()
+ "Normal: a dir is active when its derived session name is in the set."
+ (let ((cj/ai-vterm-tmux-session-prefix "aiv-"))
+ (should (cj/--ai-vterm-session-active-p "/c/foo" '("aiv-bar" "aiv-foo")))
+ (should-not (cj/--ai-vterm-session-active-p "/c/qux" '("aiv-bar" "aiv-foo")))
+ (should-not (cj/--ai-vterm-session-active-p "/c/foo" nil))))
+
+(provide 'test-ai-vterm--sort-candidates)
+;;; test-ai-vterm--sort-candidates.el ends here