From cdda0a01decb7dc61c71d830823a2ba36207306d Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 9 May 2026 15:22:20 -0500 Subject: refactor(tests): extract shared buffer-cleanup and fake-vterm helpers Eight test files across the ai-vterm and vterm-toggle suites each shipped a small variant of the same cleanup loop: walk `buffer-list`, kill any buffer whose name starts with a given prefix. Each file also re-implemented the `(string-prefix-p ...)` check inline. One file additionally had its own fake-vterm-mode-buffer constructor for tests that needed `cj/--vterm-toggle-buffer-p` to fire. I pulled the shared logic into `tests/testutil-vterm-buffers.el`: - `cj/test--kill-buffers-matching-prefix` is the primitive. - `cj/test--kill-claude-buffers` and `cj/test--kill-test-vterm-buffers` are thin wrappers for the two prefixes that actually appear. - `cj/test--make-fake-vterm-buffer` constructs a buffer with `major-mode` set to `vterm-mode` without launching a real vterm process. Each affected test file now `(require 'testutil-vterm-buffers)` and calls the shared helpers directly. `test-vterm-toggle--buffer-filter.el` keeps a 3-line wrapper that calls both kill helpers in sequence (the only place that needs both prefixes). Net diff: -116 / +72 across 8 test files, plus ~30 lines in the new testutil. Roughly -45 lines after the abstraction is paid for. No behavior change. 80 ai-vterm tests, 15 vterm-toggle tests, 15 cj-window-geometry tests all pass. Full make test green. --- tests/testutil-vterm-buffers.el | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/testutil-vterm-buffers.el (limited to 'tests/testutil-vterm-buffers.el') diff --git a/tests/testutil-vterm-buffers.el b/tests/testutil-vterm-buffers.el new file mode 100644 index 00000000..864dd8f3 --- /dev/null +++ b/tests/testutil-vterm-buffers.el @@ -0,0 +1,38 @@ +;;; testutil-vterm-buffers.el --- Shared helpers for vterm/claude buffer tests -*- lexical-binding: t; -*- + +;;; Commentary: +;; Cleanup helpers and a fake-vterm constructor used across the +;; ai-vterm and vterm-toggle test files. Before this module, each +;; test file re-implemented the same `(dolist (b (buffer-list)) +;; (when (string-prefix-p ...) (kill-buffer b)))' loop with a +;; different prefix. + +;;; Code: + +(defun cj/test--kill-buffers-matching-prefix (prefix) + "Kill all live buffers whose name starts with PREFIX." + (dolist (b (buffer-list)) + (when (string-prefix-p prefix (buffer-name b)) + (kill-buffer b)))) + +(defun cj/test--kill-claude-buffers () + "Kill all live buffers whose name matches the AI-vterm prefix \"claude [\"." + (cj/test--kill-buffers-matching-prefix "claude [")) + +(defun cj/test--kill-test-vterm-buffers () + "Kill all live buffers whose name starts with \"*test-vterm\"." + (cj/test--kill-buffers-matching-prefix "*test-vterm")) + +(defun cj/test--make-fake-vterm-buffer (name) + "Return a buffer named NAME with `major-mode' set to `vterm-mode'. + +Avoids actually launching a vterm process by setting the mode +buffer-locally. Used by tests that need a buffer satisfying the +vterm-mode predicate without the side-effects of `(vterm)'." + (let ((buf (get-buffer-create name))) + (with-current-buffer buf + (setq-local major-mode 'vterm-mode)) + buf)) + +(provide 'testutil-vterm-buffers) +;;; testutil-vterm-buffers.el ends here -- cgit v1.2.3