aboutsummaryrefslogtreecommitdiff
path: root/tests/test-ai-term--live-count.el
blob: 1432599cce1333e5d1598d2394ebf0492dbc0625 (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
;;; test-ai-term--live-count.el --- Tests for cj/ai-term-live-count -*- lexical-binding: t; -*-

;;; Commentary:
;; The shutdown safety gate: the integer count of live AI-term (aiv-*) tmux
;; sessions, read by the rulesets wrap-it-up workflow via emacsclient -e.  No
;; server / no sessions is 0, not an error.

;;; 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-live-count--with-tmux (exit-code output &rest body)
  "Run BODY with `process-file' mocked to a tmux list-sessions response.
EXIT-CODE is returned (or the symbol `error' to 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)))
     (let ((cj/ai-term-tmux-session-prefix "aiv-"))
       ,@body)))

(ert-deftest test-ai-term-live-count-counts-matching-sessions ()
  "Normal: two aiv-* sessions among others count as 2."
  (test-ai-term-live-count--with-tmux 0 "aiv-foo\nrandom\naiv-bar\n"
    (should (= (cj/ai-term-live-count) 2))))

(ert-deftest test-ai-term-live-count-single-session ()
  "Boundary: a sole aiv-* session counts as 1."
  (test-ai-term-live-count--with-tmux 0 "aiv-only\nother\n"
    (should (= (cj/ai-term-live-count) 1))))

(ert-deftest test-ai-term-live-count-no-matching-sessions ()
  "Boundary: a running server with no aiv-* sessions is 0."
  (test-ai-term-live-count--with-tmux 0 "other-a\nother-b\n"
    (should (= (cj/ai-term-live-count) 0))))

(ert-deftest test-ai-term-live-count-no-server ()
  "Error: tmux exits non-zero (no server) -> 0, not a signal."
  (test-ai-term-live-count--with-tmux 1 "no server running\n"
    (should (= (cj/ai-term-live-count) 0))))

(ert-deftest test-ai-term-live-count-tmux-missing ()
  "Error: tmux not installed -> 0."
  (test-ai-term-live-count--with-tmux 'error ""
    (should (= (cj/ai-term-live-count) 0))))

(provide 'test-ai-term--live-count)
;;; test-ai-term--live-count.el ends here