From 63a21a9390afe8a579d2fcd2cf8535e16a58df88 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 9 May 2026 14:48:01 -0500 Subject: refactor: extract window-geometry helpers shared by F9 and F12 `ai-vterm.el` (F9) and `eshell-vterm-config.el` (F12) both grew the same geometry-preservation pattern: classify a window's position, capture its body size, map cardinal direction to its frame-edge variant. The shared helpers were sitting as near-duplicates in both modules. With two real consumers established, the abstraction has the right shape. I pulled them into `cj-window-geometry.el`. The new module exposes three pure helpers: - `cj/window-direction` returns right/below/left/above based on edges relative to `frame-root-window`. Takes an optional DEFAULT for the single-window-frame fallback so each consumer picks its own (ai-vterm wants 'right, vterm-toggle wants 'below). - `cj/window-body-size` returns body-cols (right/left) or body-lines (below/above). Same body-vs-total reasoning as before: divider-independent, matches what the user sees. - `cj/cardinal-to-edge-direction` maps right/left/below/above to rightmost/leftmost/bottom/top, used by each consumer's `display-saved` action. `ai-vterm.el` and `eshell-vterm-config.el` now `(require 'cj-window-geometry)` and call the shared helpers directly. The consumer-specific `capture-state` and `display-saved` bodies stay in each module because they bind to consumer-specific state vars. Extracting those would either need parameter-passing-via-symbol or a macro, both heavier than the duplication they would remove. Tests: 15 in `test-cj-window-geometry.el` covering all four directions, body-size on both axes, cardinal-to-edge mapping, default-arg fallback, and the unknown-direction nil case. Deleted `test-ai-vterm--window-geometry.el` (now redundant) and trimmed four duplicate window-direction/size tests from `test-vterm-toggle--display.el`. Net LOC: each consumer ~40-50 lines lighter, with the new module + tests paying roughly half that back. Full make test green. make validate-modules green. --- tests/test-ai-vterm--window-geometry.el | 88 --------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 tests/test-ai-vterm--window-geometry.el (limited to 'tests/test-ai-vterm--window-geometry.el') diff --git a/tests/test-ai-vterm--window-geometry.el b/tests/test-ai-vterm--window-geometry.el deleted file mode 100644 index c2200273..00000000 --- a/tests/test-ai-vterm--window-geometry.el +++ /dev/null @@ -1,88 +0,0 @@ -;;; test-ai-vterm--window-geometry.el --- Tests for direction + fraction helpers -*- lexical-binding: t; -*- - -;;; Commentary: -;; Two pure helpers used by F9's geometry-preservation feature: -;; -;; - `cj/--ai-vterm-window-direction' classifies a window's position -;; relative to its frame as right / below / left / above (with a -;; right fallback when the window fills the frame). -;; -;; - `cj/--ai-vterm-window-fraction' returns the window's size on -;; the matching axis as a fraction of the frame. -;; -;; Tests use real window splits in `save-window-excursion' rather -;; than mocking, since the helpers consume `window-edges' and -;; `frame-width' / `frame-height' directly. - -;;; Code: - -(require 'ert) - -(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) -(require 'ai-vterm) - -(ert-deftest test-ai-vterm--window-direction-right-split () - "Normal: 2-window vertical split, right-side window -> right." - (save-window-excursion - (delete-other-windows) - (let ((right (split-window (selected-window) nil 'right))) - (should (eq (cj/--ai-vterm-window-direction right) 'right))))) - -(ert-deftest test-ai-vterm--window-direction-left-split () - "Normal: 2-window vertical split, left-side window -> left." - (save-window-excursion - (delete-other-windows) - (split-window (selected-window) nil 'right) - (should (eq (cj/--ai-vterm-window-direction (selected-window)) 'left)))) - -(ert-deftest test-ai-vterm--window-direction-below-split () - "Normal: 2-window horizontal split, bottom window -> below." - (save-window-excursion - (delete-other-windows) - (let ((below (split-window (selected-window) nil 'below))) - (should (eq (cj/--ai-vterm-window-direction below) 'below))))) - -(ert-deftest test-ai-vterm--window-direction-above-split () - "Normal: 2-window horizontal split, top window -> above." - (save-window-excursion - (delete-other-windows) - (split-window (selected-window) nil 'below) - (should (eq (cj/--ai-vterm-window-direction (selected-window)) 'above)))) - -(ert-deftest test-ai-vterm--window-direction-single-window-fallback () - "Boundary: single-window frame -> default right." - (save-window-excursion - (delete-other-windows) - (should (eq (cj/--ai-vterm-window-direction (selected-window)) 'right)))) - -(ert-deftest test-ai-vterm--window-size-right-split-returns-body-cols () - "Normal: right window -> integer body-cols matching window-body-width." - (save-window-excursion - (delete-other-windows) - (let* ((right (split-window (selected-window) nil 'right)) - (size (cj/--ai-vterm-window-size right 'right))) - (should (integerp size)) - (should (= size (window-body-width right)))))) - -(ert-deftest test-ai-vterm--window-size-below-split-returns-body-lines () - "Normal: bottom window -> integer body-lines matching window-body-height." - (save-window-excursion - (delete-other-windows) - (let* ((below (split-window (selected-window) nil 'below)) - (size (cj/--ai-vterm-window-size below 'below))) - (should (integerp size)) - (should (= size (window-body-height below)))))) - -(ert-deftest test-ai-vterm--window-size-narrow-right-split () - "Normal: deliberately narrow right window -> matching body-col count." - (save-window-excursion - (delete-other-windows) - (let* ((frame-w (frame-width)) - (target-cols (/ frame-w 4)) - (right (split-window (selected-window) (- target-cols) 'right)) - (size (cj/--ai-vterm-window-size right 'right))) - (should (integerp size)) - (should (= size (window-body-width right)))))) - -(provide 'test-ai-vterm--window-geometry) -;;; test-ai-vterm--window-geometry.el ends here -- cgit v1.2.3