unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 5354b300b4768a85e75d057c03817ace76e1d415 32645 bytes (raw)
name: test/lisp/erc/resources/erc-scenarios-common.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
 
;;; erc-scenarios-common.el --- Common helpers for ERC scenarios -*- lexical-binding: t -*-

;; Copyright (C) 2022-2023 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/>.

;;; Commentary:

;; These are e2e-ish test cases primarily intended to assert core,
;; fundamental behavior expected of any modern IRC client.  Tests may
;; also simulate specific scenarios drawn from bug reports.  Incoming
;; messages are provided by playback scripts resembling I/O logs.  In
;; place of time stamps, they have time deltas, which are used to
;; govern the test server in a fashion reminiscent of music rolls (or
;; the script(1) UNIX program).  These scripts can be found in the
;; other directories under test/lisp/erc/resources.
;;
;; Isolation:
;;
;; The set of enabled modules is shared among all tests.  The function
;; `erc-update-modules' activates them (as minor modes), but it never
;; deactivates them.  So there's no going back, and let-binding
;; `erc-modules' is useless.  The safest route is therefore to (1)
;; assume the set of default modules is already activated or will be
;; over the course of the test session and (2) let-bind relevant user
;; options as needed.  For example, to limit the damage of
;; `erc-autojoin-channels-alist' to a given test, assume the
;; `erc-join' library has already been loaded or will be on the next
;; call to `erc-open'.  And then just let-bind
;; `erc-autojoin-channels-alist' for the duration of the test.
;;
;; Playing nice:
;;
;; Right now, these tests all rely on an ugly fixture macro named
;; `erc-scenarios-common-with-cleanup', which is defined just below.
;; It helps restore (but not really prepare) the environment by
;; destroying any stray processes or buffers named in the first
;; argument, a `let*'-style VAR-LIST.  Relying on such a macro is
;; unfortunate because in many ways it actually hampers readability by
;; favoring magic over verbosity.  But without it (or something
;; similar), any failing test would cause all subsequent tests in a
;; file to fail like dominoes (making all but the first backtrace
;; useless).
;;
;; Misc:
;;
;; Note that in the following examples, nicknames Alice and Bob are
;; always associated with the fake network FooNet, while nicks Joe and
;; Mike are always on BarNet.  (Networks are sometimes downcased.)
;;
;; XXX This file should *not* contain any test cases.

;;; Code:

(require 'ert-x) ; cl-lib
(eval-and-compile
  (let* ((d (expand-file-name ".." (ert-resource-directory)))
         (load-path (cons (concat d "/erc-d") load-path)))
    (require 'erc-d-t)
    (require 'erc-d)))

(require 'erc)

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

(declare-function erc-network "erc-networks")
(defvar erc-network)

(defvar erc-scenarios-common--resources-dir
  (expand-file-name "../" (ert-resource-directory)))

;; Teardown is already inhibited when running interactively, which
;; prevents subsequent tests from succeeding, so we might as well
;; treat inspection as the goal.
(unless noninteractive
  (setq erc-server-auto-reconnect nil))

(defvar erc-scenarios-common-dialog nil)
(defvar erc-scenarios-common-extra-teardown nil)

(defun erc-scenarios-common--add-silence ()
  (advice-add #'erc-login :around #'erc-d-t-silence-around)
  (advice-add #'erc-handle-login :around #'erc-d-t-silence-around)
  (advice-add #'erc-server-connect :around #'erc-d-t-silence-around))

(defun erc-scenarios-common--remove-silence ()
  (advice-remove #'erc-login #'erc-d-t-silence-around)
  (advice-remove #'erc-handle-login #'erc-d-t-silence-around)
  (advice-remove #'erc-server-connect #'erc-d-t-silence-around))

(defun erc-scenarios-common--print-trace ()
  (when (and (boundp 'trace-buffer) (get-buffer trace-buffer))
    (with-current-buffer trace-buffer
      (message "%S" (buffer-string))
      (kill-buffer))))

(eval-and-compile
  (defun erc-scenarios-common--make-bindings (bindings)
    `((erc-d-u-canned-dialog-dir (expand-file-name
                                  (or erc-scenarios-common-dialog
                                      (cadr (assq 'erc-scenarios-common-dialog
                                                  ',bindings)))
                                  erc-scenarios-common--resources-dir))
      (erc-d-tmpl-vars `(,@erc-d-tmpl-vars
                         (quit . ,(erc-quit/part-reason-default))
                         (erc-version . ,erc-version)))
      (erc-modules (copy-sequence erc-modules))
      (inhibit-interaction t)
      (auth-source-do-cache nil)
      (timer-list (copy-sequence timer-list))
      (timer-idle-list (copy-sequence timer-idle-list))
      (erc-auth-source-parameters-join-function nil)
      (erc-autojoin-channels-alist nil)
      (erc-server-auto-reconnect nil)
      (erc-after-connect nil)
      (erc-d-linger-secs 10)
      ,@bindings)))

(defmacro erc-scenarios-common-with-cleanup (bindings &rest body)
  "Provide boilerplate cleanup tasks after calling BODY with BINDINGS.

If an `erc-d' process exists, wait for it to start before running BODY.
If `erc-autojoin-mode' mode is bound, restore it during cleanup if
disabled by BODY.  Other defaults common to these test cases are added
below and can be overridden, except when wanting the \"real\" default
value, which must be looked up or captured outside of the calling form.

Dialog resource directories are located by expanding the variable
`erc-scenarios-common-dialog' or its value in BINDINGS."
  (declare (indent 1))

  (let* ((orig-autojoin-mode (make-symbol "orig-autojoin-mode"))
         (combined `((,orig-autojoin-mode (bound-and-true-p erc-autojoin-mode))
                    ,@(erc-scenarios-common--make-bindings bindings))))

    `(erc-d-t-with-cleanup (,@combined)

         (ert-info ("Restore autojoin, etc., kill ERC buffers")
           (dolist (buf (buffer-list))
             (when-let ((erc-d-u--process-buffer)
                        (proc (get-buffer-process buf)))
               (delete-process proc)))

           (erc-scenarios-common--remove-silence)

           (when erc-scenarios-common-extra-teardown
             (ert-info ("Running extra teardown")
               (funcall erc-scenarios-common-extra-teardown)))

           (when (and (boundp 'erc-autojoin-mode)
                      (not (eq erc-autojoin-mode ,orig-autojoin-mode)))
             (erc-autojoin-mode (if ,orig-autojoin-mode +1 -1)))

           (when noninteractive
             (erc-scenarios-common--print-trace)
             (erc-d-t-kill-related-buffers)
             (delete-other-windows)))

       (erc-scenarios-common--add-silence)

       (ert-info ("Wait for dumb server")
         (dolist (buf (buffer-list))
           (with-current-buffer buf
             (when erc-d-u--process-buffer
               (erc-d-t-search-for 3 "Starting")))))

       (ert-info ("Activate erc-debug-irc-protocol")
         (unless (and noninteractive (not erc-debug-irc-protocol))
           (erc-toggle-debug-irc-protocol)))

       ,@body)))

(defvar erc-scenarios-common--term-size '(34 . 80))
(declare-function term-char-mode "term" nil)
(declare-function term-line-mode "term" nil)

;; Much of this concerns accommodating test environments outside of
;; the emacs.git tree, such as CI jobs running ERC's ELPA-package on
;; older Emacsen.  See also `erc-tests--assert-printed-in-subprocess'.
(defun erc-scenarios-common--run-in-term (&optional debug)
  (require 'term)
  (let* ((default-directory (or (getenv "EMACS_TEST_DIRECTORY")
                                (expand-file-name
                                 ".." erc-scenarios-common--resources-dir)))
         ;; In the emacs.git tree, "HOME" will be "/nonexistent", which
         ;; is fine because we don't need any ELPA packages.
         (process-environment (cons "ERC_TESTS_SUBPROCESS=1"
                                    process-environment))
         (name (ert-test-name (ert-running-test)))
         (temp-file (make-temp-file "erc-term-test-"))
         (cmd `(let ((stats 1))
                 (setq enable-dir-local-variables nil)
                 (unwind-protect
                     (setq stats (ert-run-tests-batch ',name))
                   (unless ',debug
                     (let ((buf (with-current-buffer (messages-buffer)
                                  (buffer-string))))
                       (with-temp-file ,temp-file
                         (insert buf)))
                     (kill-emacs (ert-stats-completed-unexpected stats))))))
         ;; The `ert-test' object in Emacs 29 has a `file-name' field.
         (file-name (symbol-file name 'ert--test))
         (default-directory (expand-file-name (file-name-directory file-name)))
         (package (if-let* ((found (getenv "ERC_PACKAGE_NAME"))
                            ((string-prefix-p "erc-" found)))
                      (intern found)
                    'erc))
         (init (and-let* ((found (getenv "ERC_TESTS_INIT"))
                          (files (split-string found ",")))
                 (mapcan (lambda (f) (list "-l" f)) files)))
         (setup `(progn
                   ,@(and (not init) (featurep 'compat)
                          `((require 'package)
                            (let ((package-load-list
                                   '((compat t) (,package t))))
                              (package-initialize))))
                   (require 'erc)
                   (cl-assert (equal erc-version ,erc-version) t)))
         ;; Make subprocess terminal bigger than controlling.
         (buf (cl-letf (((symbol-function 'window-screen-lines)
                         (lambda () (car erc-scenarios-common--term-size)))
                        ((symbol-function 'window-max-chars-per-line)
                         (lambda () (cdr erc-scenarios-common--term-size))))
                (apply #'make-term (symbol-name name)
                       (expand-file-name invocation-name invocation-directory)
                       nil `(,@(or init '("-Q")) "-nw"
                             "-eval" ,(format "%S" setup)
                             "-l" ,file-name
                             "-eval" ,(format "%S" cmd)))))
         (proc (get-buffer-process buf))
         (err (lambda ()
                (with-temp-buffer
                  (insert-file-contents temp-file)
                  (message "Subprocess: %s" (buffer-string))
                  (delete-file temp-file)))))
    (unless noninteractive
      (set-window-buffer (selected-window) buf)
      (delete-other-windows))
    (with-current-buffer buf
      (set-process-query-on-exit-flag proc nil)
      (unless noninteractive (term-char-mode))
      (erc-d-t-wait-for 30 (process-live-p proc))
      (while (accept-process-output proc))
      (term-line-mode)
      (goto-char (point-min))
      ;; Otherwise gives process exited abnormally with exit-code >0
      (unless (search-forward (format "Process %s finished" name) nil t)
        (funcall err)
        (ert-fail (when (search-forward "exited" nil t)
                    (buffer-substring-no-properties (line-beginning-position)
                                                    (line-end-position)))))
      (delete-file temp-file)
      (when noninteractive
        (kill-buffer)))))

(defvar erc-scenarios-common-interactive-debug-term-p nil
  "Non-nil means run test in an inferior Emacs, even if interactive.")

(defmacro erc-scenarios-common-with-noninteractive-in-term (&rest body)
  "Run BODY via `erc-scenarios-common-with-cleanup' in a `term' subprocess.
Also do so when `erc-scenarios-common-interactive-debug-term-p'
is non-nil.  When debugging, leave the `term-mode' buffer around
for inspection and name it after the test, bounded by asterisks.
When debugging, ensure the test always fails, as a reminder to
disable `erc-scenarios-common-interactive-debug-term-p'.

See Info node `(emacs) Term Mode' for the various commands."
  (declare (indent 1))
  `(if (and (or erc-scenarios-common-interactive-debug-term-p
                noninteractive)
            (not (getenv "ERC_TESTS_SUBPROCESS")))
       (progn
         (when (memq system-type '(windows-nt ms-dos cygwin haiku))
           (ert-skip "System must be UNIX-like"))
         (erc-scenarios-common--run-in-term
          erc-scenarios-common-interactive-debug-term-p))
     (erc-scenarios-common-with-cleanup ,@body)))

(defun erc-scenarios-common-assert-initial-buf-name (id port)
  ;; Assert no limbo period when explicit ID given
  (should (string= (if id
                       (format "%s" id)
                     (format "127.0.0.1:%d" port))
                   (buffer-name))))

(defun erc-scenarios-common-buflist (prefix)
  "Return list of buffers with names sharing PREFIX."
  (let (case-fold-search)
    (erc-networks--id-sort-buffers
     (delq nil
           (mapcar (lambda (b)
                     (when (string-prefix-p prefix (buffer-name b)) b))
                   (buffer-list))))))

;; This is more realistic than `erc-send-message' because it runs
;; `erc-pre-send-functions', etc.  Keyboard macros may be preferable,
;; but they sometimes experience complications when an earlier test
;; has failed.
(defun erc-scenarios-common-say (str)
  (let (erc-accidental-paste-threshold-seconds)
    (goto-char erc-input-marker)
    (insert str)
    (erc-send-current-line)))

(defun erc-scenarios-common--at-win-end-p (&optional window)
  (= (window-body-height window)
     (count-screen-lines (window-start window) (point-max) nil window)))

(defun erc-scenarios-common--above-win-end-p (&optional window)
  (> (window-body-height window)
     (count-screen-lines (window-start window) (point-max))))

(defun erc-scenarios-common--prompt-past-win-end-p (&optional window)
  (< (window-body-height window)
     (count-screen-lines (window-start window) (point-max))))

(defun erc-scenarios-common--recenter-top-bottom-around (orig &rest args)
  (let (this-command last-command) (apply orig args)))

(defun erc-scenarios-common--recenter-top-bottom ()
  (advice-add 'recenter-top-bottom
              :around #'erc-scenarios-common--recenter-top-bottom-around)
  (execute-kbd-macro "\C-l")
  (advice-remove 'recenter-top-bottom
                 #'erc-scenarios-common--recenter-top-bottom-around))


;;;; Fixtures

(defun erc-scenarios-common-scrolltobottom--normal (test)
  (erc-scenarios-common-with-noninteractive-in-term
      ((erc-scenarios-common-dialog "scrolltobottom")
       (dumb-server (erc-d-run "localhost" t 'help))
       (port (process-contact dumb-server :service))
       (erc-modules `(scrolltobottom fill-wrap ,@erc-modules))
       (erc-server-flood-penalty 0.1)
       (expect (erc-d-t-make-expecter)))

    (ert-info ("Connect")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :full-name "tester"
                                :nick "tester")
        (funcall expect 10 "debug mode")))

    (with-current-buffer "foonet"
      (should (looking-at " and"))
      (set-window-buffer nil (current-buffer))
      (delete-other-windows)
      (split-window-below 15)
      (recenter 0)

      (ert-info ("Moving into prompt in other window triggers scroll")
        (with-selected-window (next-window)
          (should-not (erc-scenarios-common--at-win-end-p))
          (goto-char (1- erc-insert-marker))
          (execute-kbd-macro "\C-n")
          ;; Ensure point is at prompt and aligned to bottom.
          (should (erc-scenarios-common--at-win-end-p))))

      (ert-info ("Module `move-to-prompt' still works")
        ;; Prompt is somewhere in the middle of the window.
        (should (erc-scenarios-common--above-win-end-p))
        ;; Hitting a self-insert key triggers `move-to-prompt' as well
        ;; as a scroll (to bottom).
        (execute-kbd-macro "hi")
        ;; Prompt and input appear on last line of window.
        (should (erc-scenarios-common--at-win-end-p)))

      (ert-info ("Command `recenter-top-bottom' disallowed at prompt")
        ;; Hitting C-l does not recenter the window.
        (erc-scenarios-common--recenter-top-bottom)
        (should (erc-scenarios-common--at-win-end-p))
        (erc-scenarios-common--recenter-top-bottom)
        (should (erc-scenarios-common--at-win-end-p)))

      (ert-info ("Command `beginning-of-buffer' allowed at prompt")
        ;; Hitting C-< goes to beginning of buffer.
        (call-interactively #'beginning-of-buffer)
        (should (= 1 (point)))
        (redisplay)
        (should (zerop (count-screen-lines (window-start) (point))))
        (should (erc-scenarios-common--prompt-past-win-end-p)))

      (ert-info ("New message doesn't trigger scroll when away from prompt")
        ;; Arriving insertions don't trigger a scroll when away from the
        ;; prompt.  New output not seen.
        (erc-cmd-MSG "NickServ help register")
        (save-excursion (erc-d-t-search-for 10 "End of NickServ"))
        (should (= 1 (point)))
        (redisplay)
        (should (zerop (count-screen-lines (window-start) (window-point))))
        (should (erc-scenarios-common--prompt-past-win-end-p)))

      (funcall test)

      (ert-info ("New message does trigger a scroll when at prompt")
        ;; Recenter so prompt is above rather than at window's end.
        (funcall expect 10 "If you are currently logged in")
        (recenter 0)
        ;; Prompt is somewhere in the middle of the window.
        (erc-d-t-wait-for 10 (erc-scenarios-common--above-win-end-p))
        (erc-scenarios-common-say "/msg NickServ help identify")
        ;; New arriving messages trigger a snap when inserted.
        (erc-d-t-wait-for 10 (erc-scenarios-common--at-win-end-p))
        (funcall expect 10 "IDENTIFY lets you login")))))

(cl-defun erc-scenarios-common--base-network-id-bouncer
    ((&key autop foo-id bar-id after
           &aux
           (foo-id (and foo-id 'oofnet))
           (bar-id (and bar-id 'rabnet))
           (serv-buf-foo (if foo-id "oofnet" "foonet"))
           (serv-buf-bar (if bar-id "rabnet" "barnet"))
           (chan-buf-foo (if foo-id "#chan@oofnet" "#chan@foonet"))
           (chan-buf-bar (if bar-id "#chan@rabnet" "#chan@barnet")))
     &rest dialogs)
  "Ensure retired option `erc-rename-buffers' is now the default behavior.
The option `erc-rename-buffers' is now deprecated and on by default, so
this now just asserts baseline behavior.  Originally from scenario
clash-of-chans/rename-buffers as explained in Bug#48598: 28.0.50;
buffer-naming collisions involving bouncers in ERC."
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/netid/bouncer")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-server-flood-penalty 0.1)
       (dumb-server (apply #'erc-d-run "localhost" t dialogs))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       (erc-server-auto-reconnect autop)
       erc-server-buffer-foo erc-server-process-foo
       erc-server-buffer-bar erc-server-process-bar)

    (ert-info ("Connect to foonet")
      (with-current-buffer
          (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                           :port port
                                           :nick "tester"
                                           :password "foonet:changeme"
                                           :full-name "tester"
                                           :id foo-id))
        (setq erc-server-process-foo erc-server-process)
        (erc-scenarios-common-assert-initial-buf-name foo-id port)
        (erc-d-t-wait-for 3 (eq (erc-network) 'foonet))
        (erc-d-t-wait-for 3 (string= (buffer-name) serv-buf-foo))
        (funcall expect 5 "foonet")))

    (ert-info ("Join #chan@foonet")
      (with-current-buffer erc-server-buffer-foo (erc-cmd-JOIN "#chan"))
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan"))
        (funcall expect 5 "<alice>")))

    (ert-info ("Connect to barnet")
      (with-current-buffer
          (setq erc-server-buffer-bar (erc :server "127.0.0.1"
                                           :port port
                                           :nick "tester"
                                           :password "barnet:changeme"
                                           :full-name "tester"
                                           :id bar-id))
        (setq erc-server-process-bar erc-server-process)
        (erc-scenarios-common-assert-initial-buf-name bar-id port)
        (erc-d-t-wait-for 6 (eq (erc-network) 'barnet))
        (erc-d-t-wait-for 3 (string= (buffer-name) serv-buf-bar))
        (funcall expect 5 "barnet")))

    (ert-info ("Server buffers are unique, no names based on IPs")
      (should-not (eq erc-server-buffer-foo erc-server-buffer-bar))
      (should-not (erc-scenarios-common-buflist "127.0.0.1")))

    (ert-info ("Join #chan@barnet")
      (with-current-buffer erc-server-buffer-bar (erc-cmd-JOIN "#chan")))

    (erc-d-t-wait-for 5 "Exactly 2 #chan-prefixed buffers exist"
      (equal (list (get-buffer chan-buf-bar)
                   (get-buffer chan-buf-foo))
             (erc-scenarios-common-buflist "#chan")))

    (ert-info ("#chan@<esid> is exclusive to foonet")
      (with-current-buffer chan-buf-foo
        (erc-d-t-search-for 1 "<bob>")
        (erc-d-t-absent-for 0.1 "<joe>")
        (should (eq erc-server-process erc-server-process-foo))
        (erc-d-t-search-for 15 "ape is dead")
        (erc-d-t-wait-for 5 (not (erc-server-process-alive)))))

    (ert-info ("#chan@<esid> is exclusive to barnet")
      (with-current-buffer chan-buf-bar
        (erc-d-t-search-for 1 "<joe>")
        (erc-d-t-absent-for 0.1 "<bob>")
        (erc-d-t-wait-for 5 (eq erc-server-process erc-server-process-bar))
        (erc-d-t-search-for 15 "joe: It is a rupture")
        (erc-d-t-wait-for 5 (not (erc-server-process-alive)))))

    (when after (funcall after))))

(defun erc-scenarios-common--clash-rename-pass-handler (dialog exchange)
  (when (eq (erc-d-dialog-name dialog) 'stub-again)
    (let* ((match (erc-d-exchange-match exchange 1))
           (sym (if (string= match "foonet") 'foonet-again 'barnet-again)))
      (should (member match (list "foonet" "barnet")))
      (erc-d-load-replacement-dialog dialog sym 1))))

(defun erc-scenarios-common--base-network-id-bouncer--reconnect (foo-id bar-id)
  (let ((erc-d-tmpl-vars '((token . (group (| "barnet" "foonet")))))
        (erc-d-match-handlers
         ;; Auto reconnect is nondeterministic, so let computer decide
         (list :pass #'erc-scenarios-common--clash-rename-pass-handler))
        (after
         (lambda ()
           ;; Simulate disconnection and `erc-server-auto-reconnect'
           (ert-info ("Reconnect to foonet and barnet back-to-back")
             (with-current-buffer (if foo-id "oofnet" "foonet")
               (erc-d-t-wait-for 10 (erc-server-process-alive)))
             (with-current-buffer (if bar-id "rabnet" "barnet")
               (erc-d-t-wait-for 10 (erc-server-process-alive))))

           (ert-info ("#chan@foonet is exclusive to foonet")
             (with-current-buffer (if foo-id "#chan@oofnet" "#chan@foonet")
               (erc-d-t-search-for 1 "<alice>")
               (erc-d-t-absent-for 0.1 "<joe>")
               (erc-d-t-search-for 20 "please your lordship")))

           (ert-info ("#chan@barnet is exclusive to barnet")
             (with-current-buffer (if bar-id "#chan@rabnet" "#chan@barnet")
               (erc-d-t-search-for 1 "<joe>")
               (erc-d-t-absent-for 0.1 "<bob>")
               (erc-d-t-search-for 20 "much in private")))

           ;; XXX this is important (reconnects overlapped, so we'd get
           ;; chan@127.0.0.1:6667)
           (should-not (erc-scenarios-common-buflist "127.0.0.1"))
           ;; Reconnection order doesn't matter here because session objects
           ;; are persisted, meaning original timestamps preserved.
           (should (equal (list (get-buffer (if bar-id "#chan@rabnet"
                                              "#chan@barnet"))
                                (get-buffer (if foo-id "#chan@oofnet"
                                              "#chan@foonet")))
                          (erc-scenarios-common-buflist "#chan"))))))
    (erc-scenarios-common--base-network-id-bouncer
     (list :autop t :foo-id foo-id :bar-id bar-id :after after)
     'foonet-drop 'barnet-drop
     'stub-again 'stub-again
     'foonet-again 'barnet-again)))

(defun erc-scenarios-common--upstream-reconnect (test &rest dialogs)
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/upstream-reconnect")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-server-flood-penalty 0.1)
       (dumb-server (apply #'erc-d-run "localhost" t dialogs))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter)))

    (ert-info ("Connect to foonet")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :user "tester@vanilla/foonet"
                                :password "changeme"
                                :full-name "tester")
        (erc-scenarios-common-assert-initial-buf-name nil port)
        (erc-d-t-wait-for 3 (eq (erc-network) 'foonet))
        (erc-d-t-wait-for 3 (string= (buffer-name) "foonet"))
        (funcall expect 5 "foonet")))

    (ert-info ("Join #chan@foonet")
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan"))
        (funcall expect 5 "<alice>")))

    (ert-info ("Connect to barnet")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :user "tester@vanilla/barnet"
                                :password "changeme"
                                :full-name "tester")
        (erc-scenarios-common-assert-initial-buf-name nil port)
        (erc-d-t-wait-for 10 (eq (erc-network) 'barnet))
        (erc-d-t-wait-for 3 (string= (buffer-name) "barnet"))
        (funcall expect 5 "barnet")))

    (ert-info ("Server buffers are unique, no names based on IPs")
      (should-not (erc-scenarios-common-buflist "127.0.0.1")))

    (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan@foonet"))
      (funcall expect 5 "#chan was created on ")
      (ert-info ("Joined again #chan@foonet")
        (funcall expect 10 "#chan was created on "))
      (funcall expect 10 "My lord, in heart"))

    (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan@barnet"))
      (funcall expect 5 "#chan was created on ")
      (ert-info ("Joined again #chan@barnet")
        (funcall expect 10 "#chan was created on "))
      (funcall expect 10 "Go to; farewell"))

    (funcall test)))

;; XXX this is okay, but we also need to check that target buffers are
;; already associated with a new process *before* a JOIN is sent by a
;; server's playback burst.  This doesn't do that.
;;
;; This *does* check that superfluous JOINs sent by the autojoin
;; module are harmless when they're not acked (superfluous because the
;; bouncer/server intitates the JOIN).

(defun erc-scenarios-common--join-network-id (foo-reconnector foo-id bar-id)
  "Ensure channels rejoined by erc-join.el DTRT.
Originally from scenario clash-of-chans/autojoin as described in
Bug#48598: 28.0.50; buffer-naming collisions involving bouncers in ERC."
  (erc-scenarios-common-with-cleanup
      ((chan-buf-foo (format "#chan@%s" (or foo-id "foonet")))
       (chan-buf-bar (format "#chan@%s" (or bar-id "barnet")))
       (erc-scenarios-common-dialog "join/network-id")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-server-flood-penalty 0.5)
       (dumb-server (erc-d-run "localhost" t 'foonet 'barnet 'foonet-again))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       erc-server-buffer-foo erc-server-process-foo
       erc-server-buffer-bar erc-server-process-bar)

    (should (memq 'autojoin erc-modules))

    (ert-info ("Connect to foonet")
      (with-current-buffer
          (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                           :port port
                                           :nick "tester"
                                           :password "foonet:changeme"
                                           :full-name "tester"
                                           :id foo-id))
        (setq erc-server-process-foo erc-server-process)
        (erc-scenarios-common-assert-initial-buf-name foo-id port)
        (erc-d-t-wait-for 5 (eq (erc-network) 'foonet))
        (funcall expect 5 "foonet")))

    (ert-info ("Join #chan, find sentinel, quit")
      (with-current-buffer erc-server-buffer-foo (erc-cmd-JOIN "#chan"))
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan"))
        (funcall expect 5 "vile thing")
        (erc-cmd-QUIT "")))

    (erc-d-t-wait-for 2 "Foonet connection deceased"
      (not (erc-server-process-alive erc-server-buffer-foo)))

    (should (equal erc-autojoin-channels-alist
                   (if foo-id '((oofnet "#chan")) '((foonet "#chan")))))

    (ert-info ("Connect to barnet")
      (with-current-buffer
          (setq erc-server-buffer-bar (erc :server "127.0.0.1"
                                           :port port
                                           :nick "tester"
                                           :password "barnet:changeme"
                                           :full-name "tester"
                                           :id bar-id))
        (setq erc-server-process-bar erc-server-process)
        (erc-d-t-wait-for 5 (eq erc-network 'barnet))
        (should (string= (buffer-name) (if bar-id "rabnet" "barnet")))))

    (ert-info ("Server buffers are unique, no stray IP-based names")
      (should-not (eq erc-server-buffer-foo erc-server-buffer-bar))
      (should-not (erc-scenarios-common-buflist "127.0.0.1")))

    (ert-info ("Only one #chan buffer exists")
      (should (equal (list (get-buffer "#chan"))
                     (erc-scenarios-common-buflist "#chan"))))

    (ert-info ("#chan is not auto-joined")
      (with-current-buffer "#chan"
        (erc-d-t-absent-for 0.1 "<joe>")
        (should-not (process-live-p erc-server-process))
        (erc-d-t-ensure-for 0.1 "server buffer remains foonet"
          (eq erc-server-process erc-server-process-foo))))

    (with-current-buffer erc-server-buffer-bar
      (erc-cmd-JOIN "#chan")
      (erc-d-t-wait-for 3 (get-buffer chan-buf-foo))
      (erc-d-t-wait-for 3 (get-buffer chan-buf-bar))
      (with-current-buffer chan-buf-bar
        (erc-d-t-wait-for 3 (eq erc-server-process erc-server-process-bar))
        (funcall expect 5 "marry her instantly")))

    (ert-info ("Reconnect to foonet")
      (with-current-buffer (setq erc-server-buffer-foo
                                 (funcall foo-reconnector))
        (should (member (if foo-id '(oofnet "#chan") '(foonet "#chan"))
                        erc-autojoin-channels-alist))
        (erc-d-t-wait-for 3 (erc-server-process-alive))
        (setq erc-server-process-foo erc-server-process)
        (erc-d-t-wait-for 2 (eq erc-network 'foonet))
        (should (string= (buffer-name) (if foo-id "oofnet" "foonet")))
        (funcall expect 5 "foonet")))

    (ert-info ("#chan@foonet is clean, no cross-contamination")
      (with-current-buffer chan-buf-foo
        (erc-d-t-wait-for 3 (eq erc-server-process erc-server-process-foo))
        (funcall expect 3 "<bob>")
        (erc-d-t-absent-for 0.1 "<joe>")
        (funcall expect 10 "not given me")))

    (ert-info ("All #chan@barnet output received")
      (with-current-buffer chan-buf-bar
        (funcall expect 10 "hath an uncle here")))))

(provide 'erc-scenarios-common)

;;; erc-scenarios-common.el ends here

debug log:

solving 5354b300b47 ...
found 5354b300b47 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).