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

;;; Commentary:
;; The dispatch helper is a pure decision function used by F9.
;; Returns one of (toggle-off . WIN), (redisplay-recent . BUF),
;; or (pick-project) based on whether an agent buffer is currently
;; displayed and whether any alive agent 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))
(add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
(require 'ai-term)
(require 'testutil-ghostel-buffers)

(ert-deftest test-ai-term--dispatch-window-displayed-returns-toggle-off ()
  "Normal: displayed agent window -> (toggle-off . WIN)."
  (let ((sentinel-win 'fake-window))
    (cl-letf (((symbol-function 'cj/--ai-term-displayed-agent-window)
               (lambda (&optional _frame) sentinel-win)))
      (should (equal (cj/--ai-term-dispatch)
                     (cons 'toggle-off sentinel-win))))))

(ert-deftest test-ai-term--dispatch-no-window-single-buffer-returns-redisplay-recent ()
  "Normal: no displayed agent, one alive buffer -> redisplay-recent + buffer."
  (cj/test--kill-agent-buffers)
  (let ((b1 (get-buffer-create "agent [single]")))
    (unwind-protect
        (cl-letf (((symbol-function 'cj/--ai-term-displayed-agent-window)
                   (lambda (&optional _frame) nil))
                  ((symbol-function 'cj/--ai-term-agent-buffers)
                   (lambda () (list b1))))
          (should (equal (cj/--ai-term-dispatch)
                         (cons 'redisplay-recent b1))))
      (kill-buffer b1))))

(ert-deftest test-ai-term--dispatch-no-window-multiple-buffers-returns-redisplay-recent ()
  "Normal: no displayed agent, 2+ alive buffers -> redisplay-recent + MRU.
F9 redisplays the most-recently-selected agent (head of buffer-list
order) rather than opening the project picker, so the user toggles
THE agent they were last using.  Other agents are reachable via M-F9."
  (cj/test--kill-agent-buffers)
  (let ((b1 (get-buffer-create "agent [a]"))
        (b2 (get-buffer-create "agent [b]")))
    (unwind-protect
        (cl-letf (((symbol-function 'cj/--ai-term-displayed-agent-window)
                   (lambda (&optional _frame) nil))
                  ((symbol-function 'cj/--ai-term-agent-buffers)
                   (lambda () (list b1 b2))))
          (should (equal (cj/--ai-term-dispatch)
                         (cons 'redisplay-recent b1))))
      (kill-buffer b1)
      (kill-buffer b2))))

(ert-deftest test-ai-term--dispatch-no-window-zero-buffers-returns-pick-project ()
  "Boundary: no displayed agent, zero alive buffers -> pick-project."
  (cj/test--kill-agent-buffers)
  (cl-letf (((symbol-function 'cj/--ai-term-displayed-agent-window)
             (lambda (&optional _frame) nil))
            ((symbol-function 'cj/--ai-term-agent-buffers)
             (lambda () nil)))
    (should (equal (cj/--ai-term-dispatch) '(pick-project)))))

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