summaryrefslogtreecommitdiff
path: root/tests/test-vterm-copy-mode-cursor.el
blob: c549a44fd416a8bfb0d2312fc5010d23ce340143 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; test-vterm-copy-mode-cursor.el --- Tests for cursor visibility in vterm-copy-mode -*- lexical-binding: t; -*-

;;; Commentary:
;; vterm's C module sets `cursor-type' to nil when the underlying TUI
;; sends DECTCEM (`\e[?25l').  Most full-screen TUIs (Claude Code, htop,
;; etc.) hide the cursor on startup.  In `vterm-copy-mode' the user is
;; navigating the buffer, not watching the TUI, so the cursor must be
;; forced visible -- the hook in `vterm-config.el' handles that.  On
;; exit, the buffer-local override is killed so the live terminal goes
;; back to the TUI's chosen cursor state.

;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'package)

(setq package-user-dir (expand-file-name "elpa" user-emacs-directory))
(package-initialize)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(defvar vterm-copy-mode nil)
(require 'vterm-config)
(require 'vterm)

(defmacro test-vterm-copy-mode-cursor--in-fake-vterm-buffer (&rest body)
  "Run BODY in a temp buffer pretending to be a live vterm.
Stubs `vterm--enter-copy-mode' and `vterm--exit-copy-mode' so toggling
`vterm-copy-mode' doesn't try to talk to a real vterm process."
  (declare (indent 0))
  `(cl-letf (((symbol-function 'vterm--enter-copy-mode) #'ignore)
             ((symbol-function 'vterm--exit-copy-mode) #'ignore))
     (with-temp-buffer
       (setq-local major-mode 'vterm-mode)
       ,@body)))

(ert-deftest test-vterm-copy-mode-cursor-restored-on-enter ()
  "Normal: entering copy-mode with cursor-type nil sets a visible cursor."
  (with-temp-buffer
    (setq-local cursor-type nil)
    (let ((vterm-copy-mode t))
      (cj/--vterm-copy-mode-restore-cursor))
    (should (equal cursor-type 'box))))

(ert-deftest test-vterm-copy-mode-cursor-restored-when-prior-was-hbar ()
  "Boundary: entering copy-mode overrides any prior cursor-type with the block."
  (with-temp-buffer
    (setq-local cursor-type 'hbar)
    (let ((vterm-copy-mode t))
      (cj/--vterm-copy-mode-restore-cursor))
    (should (equal cursor-type 'box))))

(ert-deftest test-vterm-copy-mode-cursor-override-killed-on-exit ()
  "Normal: exiting copy-mode kills the buffer-local cursor-type override."
  (with-temp-buffer
    (setq-local cursor-type 'box)
    (should (local-variable-p 'cursor-type))
    (let ((vterm-copy-mode nil))
      (cj/--vterm-copy-mode-restore-cursor))
    (should-not (local-variable-p 'cursor-type))))

(ert-deftest test-vterm-copy-mode-cursor-hook-installed ()
  "Normal: the cursor-restoration hook is registered on vterm-copy-mode-hook."
  (should (memq #'cj/--vterm-copy-mode-restore-cursor
                vterm-copy-mode-hook)))

(ert-deftest test-vterm-copy-mode-cursor-end-to-end-via-mode-toggle ()
  "Normal: toggling `vterm-copy-mode' on then off via the real minor mode
command produces the visible cursor on entry and removes the override on
exit.  This exercises the full path -- mode body, hook registration, our
restore function -- not just the helper in isolation."
  (test-vterm-copy-mode-cursor--in-fake-vterm-buffer
    (setq-local cursor-type nil)
    ;; Enter copy-mode through the actual minor-mode command, not by
    ;; let-binding the variable.  This fires `vterm-copy-mode-hook'.
    (vterm-copy-mode 1)
    (should (eq vterm-copy-mode t))
    (should (equal cursor-type 'box))
    ;; Exit through the same path.
    (vterm-copy-mode -1)
    (should (eq vterm-copy-mode nil))
    (should-not (local-variable-p 'cursor-type))))

(ert-deftest test-vterm-copy-mode-cursor-end-to-end-via-copy-done ()
  "Normal: `vterm-copy-mode-done' toggles copy-mode off and triggers cursor
restoration.  No key is bound to it in this config (M-w copies and stays;
RET is unbound), but it stays reachable via \\[execute-extended-command]
and its exit path must still restore the cursor."
  (test-vterm-copy-mode-cursor--in-fake-vterm-buffer
    (setq-local cursor-type nil)
    (vterm-copy-mode 1)
    (should (eq vterm-copy-mode t))
    (should (equal cursor-type 'box))
    (insert "selectable text on this line")
    (set-mark (point-min))
    (goto-char (point-max))
    (vterm-copy-mode-done nil)
    (should (eq vterm-copy-mode nil))
    (should-not (local-variable-p 'cursor-type))))

(ert-deftest test-vterm-copy-mode-cursor-end-to-end-via-cancel ()
  "Normal: `cj/vterm-copy-mode-cancel' (C-g / <escape> binding) toggles
copy-mode off and triggers cursor restoration even when no region was
selected -- the cancel path skips the kill-ring step entirely."
  (test-vterm-copy-mode-cursor--in-fake-vterm-buffer
    (setq-local cursor-type nil)
    (vterm-copy-mode 1)
    (should (equal cursor-type 'box))
    (cj/vterm-copy-mode-cancel)
    (should (eq vterm-copy-mode nil))
    (should-not (local-variable-p 'cursor-type))))

(ert-deftest test-vterm-copy-mode-cursor-end-to-end-via-copy-done-no-region ()
  "Boundary: `vterm-copy-mode-done' called with no active region falls
into its line-selection branch.  The branch calls vterm-internal
helpers that aren't safe in a fake buffer, so stub them to point-min /
point-max.  The exit-and-fire-hook chain at the function's tail must
still run; cursor restoration must still happen."
  (cl-letf (((symbol-function 'vterm--get-beginning-of-line)
             (lambda (&rest _) (point-min)))
            ((symbol-function 'vterm--get-end-of-line)
             (lambda (&rest _) (point-max))))
    (test-vterm-copy-mode-cursor--in-fake-vterm-buffer
      (insert "line content")
      (setq-local cursor-type nil)
      (vterm-copy-mode 1)
      (should (equal cursor-type 'box))
      (deactivate-mark)
      (should-not (use-region-p))
      (vterm-copy-mode-done nil)
      (should (eq vterm-copy-mode nil))
      (should-not (local-variable-p 'cursor-type)))))

(ert-deftest test-vterm-copy-mode-cursor-survives-multiple-cycles ()
  "Boundary: enter/exit/enter/exit cycles don't accumulate buffer-local
state.  The cursor goes back and forth cleanly."
  (test-vterm-copy-mode-cursor--in-fake-vterm-buffer
    (setq-local cursor-type nil)
    (dotimes (_ 3)
      (vterm-copy-mode 1)
      (should (equal cursor-type 'box))
      (vterm-copy-mode -1)
      (should-not (local-variable-p 'cursor-type)))))

(provide 'test-vterm-copy-mode-cursor)
;;; test-vterm-copy-mode-cursor.el ends here