unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 5c3c526f86d481d16828e58c1af470b6c6961f64 10949 bytes (raw)
name: test/lisp/erc/erc-scenarios-base-buffer-display.el 	 # note: path name is non-authoritative(*)

  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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
 
;;; erc-scenarios-base-buffer-display.el --- Buffer display scenarios -*- lexical-binding: t -*-

;; Copyright (C) 2023-2024 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert-x)
(eval-and-compile
  (let ((load-path (cons (ert-resource-directory) load-path)))
    (require 'erc-scenarios-common)))

(eval-when-compile (require 'erc-join))

;; These first couple `erc-auto-reconnect-display' tests used to live
;; in erc-scenarios-base-reconnect but have since been renamed.  Note
;; that these are somewhat difficult to reason about because the user
;; joins a second channel after reconnecting, and the first is
;; controlled by `autojoin'.

(defun erc-scenarios-base-buffer-display--reconnect-common
    (assert-server assert-chan assert-rest)
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/reconnect")
       (dumb-server (erc-d-run "localhost" t 'options 'options-again))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.1)
       (erc-server-auto-reconnect t)
       erc-autojoin-channels-alist)

    (should (memq 'autojoin erc-modules))

    (ert-info ("Connect to foonet")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :password "changeme"
                                :full-name "tester")
        (funcall assert-server expect)
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (funcall expect 10 "debug mode")))

    (ert-info ("Wait for some output in channels")
      (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
        (funcall assert-chan expect)
        (funcall expect 10 "welcome")
        (funcall expect 10 "welcome")))

    (ert-info ("Server buffer shows connection failed")
      (with-current-buffer "FooNet"
        (funcall expect 10 "Connection failed!  Re-establishing")))

    (should (equal erc-autojoin-channels-alist '((FooNet "#chan"))))
    (delete-other-windows)
    (pop-to-buffer-same-window "*Messages*")

    (ert-info ("Wait for auto reconnect")
      (with-current-buffer "FooNet" (funcall expect 10 "still in debug mode")))

    (ert-info ("Lone window still shows messages buffer")
      (should (eq (window-buffer) (messages-buffer)))
      (should (frame-root-window-p (selected-window))))

    (funcall assert-rest expect)

    (ert-info ("Wait for activity to recommence in both channels")
      (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
        (funcall expect 10 "forest of Arden"))
      (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#spam"))
        (funcall expect 10 "her elves come here anon")))))

;; Interactively issuing a slash command resets the auto-reconnect
;; count, making ERC ignore the option `erc-auto-reconnect-display'
;; when next displaying a newly set up buffer.  In the case of a
;; /JOIN, the option `erc-interactive-display' takes precedence.
(ert-deftest erc-scenarios-base-buffer-display--defwin-recbury-intbuf ()
  :tags '(:expensive-test)
  (should (eq erc-buffer-display 'bury))
  (should (eq erc-interactive-display 'window))
  (should-not erc-auto-reconnect-display)

  (let ((erc-buffer-display 'window) ; defwin
        (erc-interactive-display 'buffer) ; intbuf
        (erc-auto-reconnect-display 'bury)) ; recbury

    (erc-scenarios-base-buffer-display--reconnect-common

     (lambda (_)
       (ert-info ("New server buffer appears in a selected split")
         (should (eq (window-buffer) (current-buffer)))
         (should-not (frame-root-window-p (selected-window)))))

     (lambda (_)
       (ert-info ("New channel buffer appears in other window")
         (should (eq (window-buffer) (current-buffer))) ; selected
         (should (equal (get-buffer "FooNet") (window-buffer (next-window))))))

     (lambda (expect)
       ;; If we /JOIN #spam now, we'll cancel the auto-reconnect
       ;; timer, and "#chan" may well pop up in a split before we can
       ;; verify that the lone window displays #spam (a race, IOW).
       (ert-info ("Autojoined channel #chan buried on JOIN")
         (with-current-buffer "#chan"
           (funcall expect 10 "You have joined channel #chan"))
         (should (frame-root-window-p (selected-window)))
         (should (eq (window-buffer) (messages-buffer))))

       (with-current-buffer "FooNet" (erc-scenarios-common-say "/JOIN #spam"))

       (ert-info ("A /JOIN ignores `erc-auto-reconnect-display'")
         (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#spam"))
           (should (eq (window-buffer) (get-buffer "#spam")))
           ;; Option `erc-interactive-display' being `buffer' means
           ;; Emacs reuses the selected window (no split).
           (should (frame-root-window-p (selected-window)))))))))

(ert-deftest erc-scenarios-base-buffer-display--defwino-recbury-intbuf ()
  :tags '(:expensive-test)
  (should (eq erc-buffer-display 'bury))
  (should (eq erc-interactive-display 'window))
  (should-not erc-auto-reconnect-display)

  (let ((erc-buffer-display 'window-noselect) ; defwino
        (erc-auto-reconnect-display 'bury)
        (erc-interactive-display 'buffer))
    (erc-scenarios-base-buffer-display--reconnect-common

     (lambda (_)
       ;; Selected window shows some non-ERC buffer.  New server
       ;; buffer appears in another window (other side of split).
       (should-not (frame-root-window-p (selected-window)))
       (should-not (eq (window-buffer) (current-buffer)))
       (with-current-buffer (window-buffer)
         (should-not (derived-mode-p 'erc-mode)))
       (should (eq (current-buffer) (window-buffer (next-window)))))

     (lambda (_)
       (should-not (frame-root-window-p (selected-window)))
       ;; Current split likely shows scratch.
       (with-current-buffer (window-buffer)
         (should-not (derived-mode-p 'erc-mode)))
       (should (eq (current-buffer) (window-buffer (next-window)))))

     (lambda (_)
       ;; A JOIN command sent from lisp code is "non-interactive" and
       ;; doesn't reset the auto-reconnect count, so ERC treats the
       ;; response as possibly server-initiated or otherwise the
       ;; result of an autojoin and continues to favor
       ;; `erc-auto-reconnect-display'.
       (ert-info ("Join chan non-interactively and open a /QUERY")
         (with-current-buffer "FooNet"
           (erc-cmd-JOIN "#spam") ; "non-interactive" according to ERC
           (erc-scenarios-common-say "/QUERY bob") ; resets count
           (should (eq (window-buffer) (get-buffer "bob")))
           (should (frame-root-window-p (selected-window)))))

       ;; The /QUERY above resets the count, and `erc-buffer-display'
       ;; again decides how #spam is displayed.
       (ert-info ("Newly joined chan ignores `erc-auto-reconnect-display'")
         (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#spam"))
           (should (eq (window-buffer) (get-buffer "bob")))
           (should-not (frame-root-window-p (selected-window))) ; noselect
           (should (eq (current-buffer) (window-buffer (next-window))))))))))

(ert-deftest erc-scenarios-base-buffer-display--count-reset-timeout ()
  :tags '(:expensive-test)
  (should (eq erc-buffer-display 'bury))
  (should (eq erc-interactive-display 'window))
  (should (eq erc-auto-reconnect-display-timeout 10))
  (should-not erc-auto-reconnect-display)

  (let ((erc-buffer-display 'window-noselect)
        (erc-auto-reconnect-display 'bury)
        (erc-interactive-display 'buffer)
        (erc-auto-reconnect-display-timeout 0.5))
    (erc-scenarios-base-buffer-display--reconnect-common
     #'ignore #'ignore ; These two are identical to the previous test.

     (lambda (_)
       (with-current-buffer "FooNet"
         (erc-d-t-wait-for 1 erc--server-reconnect-display-timer))

       ;; A non-interactive JOIN command doesn't signal that we're
       ;; done auto-reconnecting.
       (ert-info ("Join channel #spam non-interactively")
         (with-current-buffer "FooNet"
           (erc-d-t-wait-for 1 (null erc--server-reconnect-display-timer))
           (erc-cmd-JOIN "#spam"))) ; not processed as a /JOIN

       (ert-info ("Option `erc-auto-reconnect-display' ignored w/o timer")
         (should (eq (window-buffer) (messages-buffer)))
         (erc-d-t-wait-for 10 (get-buffer "#spam"))
         ;; If `erc-auto-reconnect-display-timeout' were left alone,
         ;; this would be (frame-root-window-p #<window 1 on scratch*>).
         (should-not (frame-root-window-p (selected-window)))
         (should (eq (get-buffer "#spam") (window-buffer (next-window)))))))))

;; This shows that the option `erc-interactive-display' overrides
;; `erc-join-buffer' during cold opens and interactive /JOINs.

(ert-deftest erc-scenarios-base-buffer-display--interactive-default ()
  :tags '(:expensive-test)
  (should (eq erc-join-buffer 'bury))
  (should (eq erc-interactive-display 'window))

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "join/legacy")
       (dumb-server (erc-d-run "localhost" t 'foonet))
       (port (process-contact dumb-server :service))
       (url (format "tester:changeme@127.0.0.1:%d\r\r" port))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.1)
       (erc-server-auto-reconnect t)
       (erc-user-full-name "tester"))

    (ert-info ("Connect to foonet")
      (with-current-buffer (let (inhibit-interaction)
                             (ert-simulate-keys url
                               (call-interactively #'erc)))
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))

        (erc-d-t-wait-for 10 "Server buffer shown"
          (eq (window-buffer) (current-buffer)))
        (funcall expect 10 "debug mode")
        (erc-scenarios-common-say "/JOIN #chan")))

    (ert-info ("Wait for output in #chan")
      (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
        (funcall expect 10 "welcome")
        (erc-d-t-ensure-for 3 "Channel #chan shown"
          (eq (window-buffer) (current-buffer)))
        (funcall expect 10 "be prosperous")))))

;;; erc-scenarios-base-buffer-display.el ends here

debug log:

solving 5c3c526f86d ...
found 5c3c526f86d in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).