blob: 1952caed8e1182d251981481ec90ba5f708ce209 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
;;; test-ai-term--live-tmux-sessions.el --- Tests for cj/--ai-term-live-tmux-sessions -*- lexical-binding: t; -*-
;;; Commentary:
;; Lists the live tmux sessions that carry the AI-term prefix so the
;; project picker can surface projects whose agent session survived an
;; Emacs crash. tmux being absent or no server running is a normal
;; "nothing to match" outcome, not an error -- the lister returns nil.
;;; Code:
(require 'ert)
(require 'cl-lib)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'ai-term)
(defmacro test-ai-term--with-tmux-list (exit-code output &rest body)
"Run BODY with `process-file' mocked to a tmux list-sessions response.
EXIT-CODE is what `process-file' returns (or the symbol `error' to
make it signal). OUTPUT is written to the stdout destination buffer."
(declare (indent 2))
`(cl-letf (((symbol-function 'process-file)
(lambda (_program _infile destination _display &rest _args)
(when (eq ,exit-code 'error)
(error "tmux: command not found"))
(let ((buffer (cond
((eq destination t) (current-buffer))
((bufferp destination) destination)
((consp destination)
(and (eq (car destination) t)
(current-buffer))))))
(when (bufferp buffer)
(with-current-buffer buffer (insert ,output))))
,exit-code)))
,@body))
(ert-deftest test-ai-term--live-tmux-sessions-filters-to-prefix ()
"Normal: only sessions starting with the AI-term prefix come back."
(let ((cj/ai-term-tmux-session-prefix "aiv-"))
(test-ai-term--with-tmux-list 0 "aiv-foo\nrandom-session\naiv-bar\n"
(should (equal (cj/--ai-term-live-tmux-sessions)
'("aiv-foo" "aiv-bar"))))))
(ert-deftest test-ai-term--live-tmux-sessions-honors-custom-prefix ()
"Normal: a non-default prefix is what gets matched."
(let ((cj/ai-term-tmux-session-prefix "em-"))
(test-ai-term--with-tmux-list 0 "em-foo\naiv-bar\nem-baz\n"
(should (equal (cj/--ai-term-live-tmux-sessions)
'("em-foo" "em-baz"))))))
(ert-deftest test-ai-term--live-tmux-sessions-empty-output-yields-nil ()
"Boundary: a running server with no matching sessions yields nil."
(let ((cj/ai-term-tmux-session-prefix "aiv-"))
(test-ai-term--with-tmux-list 0 "other-a\nother-b\n"
(should (null (cj/--ai-term-live-tmux-sessions))))))
(ert-deftest test-ai-term--live-tmux-sessions-no-server-yields-nil ()
"Error: tmux exits non-zero (no server running) -> nil, not a signal."
(let ((cj/ai-term-tmux-session-prefix "aiv-"))
(test-ai-term--with-tmux-list 1 "no server running on /tmp/tmux-1000/default\n"
(should (null (cj/--ai-term-live-tmux-sessions))))))
(ert-deftest test-ai-term--live-tmux-sessions-tmux-missing-yields-nil ()
"Error: tmux not installed -> `process-file' signals; lister returns nil."
(let ((cj/ai-term-tmux-session-prefix "aiv-"))
(test-ai-term--with-tmux-list 'error ""
(should (null (cj/--ai-term-live-tmux-sessions))))))
(provide 'test-ai-term--live-tmux-sessions)
;;; test-ai-term--live-tmux-sessions.el ends here
|