From 4e1401d1499be3d24df78edd97310d200e719e10 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 24 Jun 2026 06:43:45 -0400 Subject: feat(ai-term): wrap-teardown + shutdown entry points for rulesets Add the three headless functions the rulesets wrap-it-up workflow calls via emacsclient -e, since this module owns the aiv- session naming, the agent buffer, and the geometry restore. cj/ai-term-quit kills a project's tmux session and agent buffer and restores the layout, idempotent and safe when already gone. cj/ai-term-live-count returns the integer count of live aiv- sessions for the shutdown safety gate. cj/ai-term-shutdown-countdown re-checks that gate, then runs an abort-able run-at-time countdown in the echo area and, uncancelled, runs the shutdown command (a defcustom so tests stub it). Reuses the existing kill/close helpers. 13 ERT tests cover the live-count parsing, the quit kill-and-idempotency, and the gate-abort/cancel/tick logic; the tmux and shutdown side effects are manual. Claude-Session: https://claude.ai/code/session_01BqrdWUo9GcznYX2pZr76gZ --- tests/test-ai-term--quit.el | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/test-ai-term--quit.el (limited to 'tests/test-ai-term--quit.el') diff --git a/tests/test-ai-term--quit.el b/tests/test-ai-term--quit.el new file mode 100644 index 000000000..55ace81db --- /dev/null +++ b/tests/test-ai-term--quit.el @@ -0,0 +1,65 @@ +;;; 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- 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-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 -- cgit v1.2.3