blob: 64b8a5d48271fdc18fc5f5511323bfbb0cf6b0ea (
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
72
73
74
75
76
77
78
79
80
81
|
;;; test-ai-term--quit.el --- Tests for cj/ai-term-quit -*- lexical-binding: t; -*-
;;; Commentary:
;; Headless teardown of a project's AI-term: kill the aiv-<name> tmux session,
;; then the agent buffer. Driven by the rulesets Stop hook via emacsclient -e,
;; keyed by project basename. Must be idempotent (a no-op when already gone).
;;; 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-quit--with-tmux (calls-var &rest body)
"Run BODY with `process-file' mocked to record arg lists into CALLS-VAR (0 exit)."
(declare (indent 1))
`(cl-letf (((symbol-function 'process-file)
(lambda (_program _infile _destination _display &rest args)
(push args ,calls-var) 0)))
,@body))
(ert-deftest test-ai-term-quit-kills-session-and-buffer ()
"Normal: quit kills the project's aiv- session and its agent buffer."
(let ((buf (get-buffer-create "agent [myproj]"))
(calls nil))
(unwind-protect
(test-ai-term-quit--with-tmux calls
(cj/ai-term-quit "myproj")
(should (member '("kill-session" "-t" "aiv-myproj") calls))
(should-not (buffer-live-p buf)))
(when (buffer-live-p buf) (kill-buffer buf)))))
(ert-deftest test-ai-term-quit-sanitizes-dotted-basename ()
"Boundary: a dotted basename maps to the sanitized session tmux really uses."
(let ((buf (get-buffer-create "agent [.emacs.d]"))
(calls nil))
(unwind-protect
(test-ai-term-quit--with-tmux calls
(cj/ai-term-quit ".emacs.d")
(should (member '("kill-session" "-t" "aiv-_emacs_d") calls))
(should-not (buffer-live-p buf)))
(when (buffer-live-p buf) (kill-buffer buf)))))
(ert-deftest test-ai-term-quit-nil-project-from-drifted-agent-buffer ()
"Regression: nil PROJECT inside an agent buffer keys off the buffer name.
After a cd in the agent shell, ghostel's OSC 7 tracking moves the buffer's
`default-directory' away from the project, so keying off it would kill the
wrong session and miss the buffer."
(let ((buf (get-buffer-create "agent [realproj]"))
(calls nil))
(unwind-protect
(test-ai-term-quit--with-tmux calls
(with-current-buffer buf
(setq-local default-directory "/tmp/elsewhere/")
(cj/ai-term-quit))
(should (member '("kill-session" "-t" "aiv-realproj") calls))
(should-not (buffer-live-p buf)))
(when (buffer-live-p buf) (kill-buffer buf)))))
(ert-deftest test-ai-term-quit-idempotent-when-gone ()
"Error/Boundary: a second quit (session + buffer already gone) does not error."
(let ((calls nil))
(test-ai-term-quit--with-tmux calls
;; No buffer named "agent [ghost]" exists; session kill is a no-op in tmux.
(should (stringp (cj/ai-term-quit "ghost")))
(should (member '("kill-session" "-t" "aiv-ghost") calls)))))
(ert-deftest test-ai-term-quit-leaves-non-agent-buffers ()
"Error: a same-named-but-non-agent buffer is never killed (prefix guard)."
(let ((buf (get-buffer-create "notes-myproj"))
(calls nil))
(unwind-protect
(test-ai-term-quit--with-tmux calls
(cj/ai-term-quit "myproj")
(should (buffer-live-p buf)))
(when (buffer-live-p buf) (kill-buffer buf)))))
(provide 'test-ai-term--quit)
;;; test-ai-term--quit.el ends here
|