;;; erc-tests.el --- Tests for erc. -*- lexical-binding:t -*- ;; Copyright (C) 2020-2021 Free Software Foundation, Inc. ;; Author: Lars Ingebrigtsen ;; 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 . ;;; Code: (require 'ert) (require 'erc) (require 'erc-ring) (require 'erc-networks) (ert-deftest erc--read-time-period () (cl-letf (((symbol-function 'read-string) (lambda (&rest _) ""))) (should (equal (erc--read-time-period "foo: ") nil))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " "))) (should (equal (erc--read-time-period "foo: ") nil))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " 432 "))) (should (equal (erc--read-time-period "foo: ") 432))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "432"))) (should (equal (erc--read-time-period "foo: ") 432))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1h"))) (should (equal (erc--read-time-period "foo: ") 3600))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1h10s"))) (should (equal (erc--read-time-period "foo: ") 3610))) (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1d"))) (should (equal (erc--read-time-period "foo: ") 86400)))) (ert-deftest erc-with-all-buffers-of-server () (let (proc-exnet proc-onet erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) (with-current-buffer (get-buffer-create "OtherNet") (erc-mode) (setq proc-onet (start-process "sleep" (current-buffer) "sleep" "1") erc-server-process proc-onet erc-network 'OtherNet) (set-process-query-on-exit-flag erc-server-process nil)) (with-current-buffer (get-buffer-create "ExampleNet") (erc-mode) (setq proc-exnet (start-process "sleep" (current-buffer) "sleep" "1") erc-server-process proc-exnet erc-network 'ExampleNet) (set-process-query-on-exit-flag erc-server-process nil)) (with-current-buffer (get-buffer-create "#foo") (erc-mode) (setq erc-server-process proc-exnet) (setq erc-default-recipients '("#foo"))) (with-current-buffer (get-buffer-create "#spam") (erc-mode) (setq erc-server-process proc-onet) (setq erc-default-recipients '("#spam"))) (with-current-buffer (get-buffer-create "#bar") (erc-mode) (setq erc-server-process proc-onet) (setq erc-default-recipients '("#bar"))) (with-current-buffer (get-buffer-create "#baz") (erc-mode) (setq erc-server-process proc-exnet) (setq erc-default-recipients '("#baz"))) (should (eq (get-buffer-process "ExampleNet") proc-exnet)) (erc-with-all-buffers-of-server (get-buffer-process "ExampleNet") nil (kill-buffer)) (should-not (get-buffer "ExampleNet")) (should-not (get-buffer "#foo")) (should-not (get-buffer "#baz")) (should (get-buffer "OtherNet")) (should (get-buffer "#bar")) (should (get-buffer "#spam")) (let* ((test (lambda () (not (string= (buffer-name) "#spam")))) (calls 0) (get-test (lambda () (cl-incf calls) test))) (erc-with-all-buffers-of-server proc-onet (funcall get-test) (kill-buffer)) (should (= calls 1))) (should-not (get-buffer "OtherNet")) (should-not (get-buffer "#bar")) (should (get-buffer "#spam")) (kill-buffer "#spam"))) (ert-deftest erc-lurker-maybe-trim () (let (erc-lurker-trim-nicks (erc-lurker-ignore-chars "_`")) (should (string= "nick`" (erc-lurker-maybe-trim "nick`"))) (setq erc-lurker-trim-nicks t) (should (string= "nick" (erc-lurker-maybe-trim "nick`"))) (should (string= "ni`_ck" (erc-lurker-maybe-trim "ni`_ck__``"))) (setq erc-lurker-ignore-chars "_-`") ; set of chars, not character alts (should (string= "nick" (erc-lurker-maybe-trim "nick-_`"))))) (ert-deftest erc-parse-isupport-value () (should (equal (erc-parse-isupport-value "a,b") '("a" "b"))) (should (equal (erc-parse-isupport-value "a,b,c") '("a" "b" "c"))) (should (equal (erc-parse-isupport-value "abc") '("abc"))) (should (equal (erc-parse-isupport-value "\\x20foo") '(" foo"))) (should (equal (erc-parse-isupport-value "foo\\x20") '("foo "))) (should (equal (erc-parse-isupport-value "a\\x20b\\x20c") '("a b c"))) (should (equal (erc-parse-isupport-value "a\\x20b\\x20c\\x20") '("a b c "))) (should (equal (erc-parse-isupport-value "\\x20a\\x20b\\x20c") '(" a b c"))) (should (equal (erc-parse-isupport-value "a\\x20\\x20c") '("a c"))) (should (equal (erc-parse-isupport-value "\\x20\\x20\\x20") '(" "))) (should (equal (erc-parse-isupport-value "\\x5Co/") '("\\o/"))) (should (equal (erc-parse-isupport-value "\\x7F,\\x19") '("\\x7F" "\\x19"))) (should (equal (erc-parse-isupport-value "a\\x2Cb,c") '("a,b" "c")))) (ert-deftest erc-server-005 () (with-suppressed-warnings ((obsolete erc-server-parameters)) (let* ((erc-server-005-functions (copy-sequence erc-server-005-functions)) (hooked 0) (verify #'ignore) (hook (lambda (_ _) (funcall verify) (cl-incf hooked))) erc-server-parameters erc-isupport-parameters erc-timer-hook calls args parsed) (add-hook 'erc-server-005-functions hook 90) (should (eq (cadr erc-server-005-functions) hook)) (cl-letf (((symbol-function 'erc-display-message) (lambda (_ _ _ line) (push line calls)))) (ert-info ("Baseline") (setq args '("tester" "BOT=B" "EXCEPTS" "PREFIX=(ov)@+" "are supp...") parsed (make-erc-response :command-args args :command "005")) (setq verify (lambda () (should (equal erc-server-parameters '(("PREFIX" . "(ov)@+") ("EXCEPTS") ("BOT" . "B")))) (should (equal erc-isupport-parameters '((PREFIX "(ov)@+") (EXCEPTS) (BOT "B")))) (should (string= (pop calls) "BOT=B EXCEPTS PREFIX=(ov)@+ are supp...")) (should (equal args (erc-response.command-args parsed))))) (erc-call-hooks nil parsed)) (ert-info ("Negated, updated") (setq args '("tester" "-EXCEPTS" "PREFIX=(ohv)@%+" "are supported") parsed (make-erc-response :command-args args :command "005")) (setq verify (lambda () (should (equal erc-server-parameters '(("PREFIX" . "(ohv)@%+") ("BOT" . "B")))) (should (equal erc-isupport-parameters '((PREFIX "(ohv)@%+") (BOT "B")))) (should (string= (pop calls) "-EXCEPTS PREFIX=(ohv)@%+ are supported")) (should (equal args (erc-response.command-args parsed))))) (erc-call-hooks nil parsed)) (should (= hooked 2))))) (should-not (cadr erc-server-005-functions))) (ert-deftest erc-downcase () (let ((erc-isupport-parameters '((PREFIX "(ov)@+") (BOT "B")))) (ert-info ("ascii") (setf (alist-get 'CASEMAPPING erc-isupport-parameters) '("ascii")) (should (equal (erc-downcase "Bob[m]`") "bob[m]`")) (should (equal (erc-downcase "Tilde~") "tilde~" )) (should (equal (erc-downcase "\\O/") "\\o/" ))) (ert-info ("rfc1459") (setf (car (alist-get 'CASEMAPPING erc-isupport-parameters)) "rfc1459") (should (equal (erc-downcase "Bob[m]`") "bob{m}`" )) (should (equal (erc-downcase "Tilde~") "tilde^" )) (should (equal (erc-downcase "\\O/") "|o/" ))) (ert-info ("rfc1459-strict") (setf (car (alist-get 'CASEMAPPING erc-isupport-parameters)) "rfc1459-strict") (should (equal (erc-downcase "Bob[m]`") "bob{m}`")) (should (equal (erc-downcase "Tilde~") "tilde~" )) (should (equal (erc-downcase "\\O/") "|o/" ))))) (ert-deftest erc-local-channel-p () (ert-info ("Local channels not supported") (let ((erc-isupport-parameters '((CHANTYPES "#")))) (should-not (erc-valid-local-channel-p "#chan")) (should-not (erc-valid-local-channel-p "&local")))) (ert-info ("Local channels supported") (let ((erc-isupport-parameters '((CHANTYPES "&#")))) (should-not (erc-valid-local-channel-p "#chan")) (should (erc-valid-local-channel-p "&local"))))) (ert-deftest erc--target-from-string () (should (equal (erc--target-from-string "#chan") #s(erc--target "#chan" \#chan t nil))) (should (equal (erc--target-from-string "Bob") #s(erc--target "Bob" bob nil nil))) (let ((erc-isupport-parameters '((CHANTYPES "#&")))) (should (equal (erc--target-from-string "&Bitlbee") #s(erc--target "&Bitlbee" &bitlbee t t))))) ;; This stuff is related to bug#23438. (defvar erc-join-tests--auth-source-entries '("machine irc.gnu.org port irc user \"#chan\" password bar" "machine west.gnu.org port irc user \"#chan\" password baz" "machine GNU.chat port irc user \"#chan\" password foo")) (ert-deftest erc--auth-source-search--standard () (let* ((entries (sort (copy-sequence erc-join-tests--auth-source-entries) (lambda (&rest _) (zerop (random 2))))) (netrc-file (make-temp-file "auth-source-test" nil nil (mapconcat 'identity entries "\n"))) (auth-sources (list netrc-file)) (auth-source-do-cache nil)) (unwind-protect (ert-info ("Normal ordering") (ert-info ("Session wins") (let ((erc-session-server "irc.gnu.org") (erc-server-announced-name "west.gnu.org") (erc-session-port 6697) (erc-network 'fake) (erc-server-current-nick "tester") (erc--session (erc--sid-create 'GNU.chat))) (should (string= (erc--auth-source-search :user "#chan") "foo")))) (ert-info ("Network wins") (let* ((erc-session-server "irc.gnu.org") (erc-server-announced-name "west.gnu.org") (erc-session-port 6697) (erc-network 'GNU.chat) (erc-server-current-nick "tester") (erc--session (erc--sid-create nil))) (should (string= (erc--auth-source-search :user "#chan") "foo")))) (ert-info ("Announced wins") (let ((erc-session-server "irc.gnu.org") (erc-server-announced-name "west.gnu.org") (erc-session-port 6697) erc-network (erc--session (erc--sid-create nil))) (should (string= (erc--auth-source-search :user "#chan") "baz"))))) (delete-file netrc-file)))) (ert-deftest erc--auth-source-search--announced () (let* ((entries (sort (copy-sequence erc-join-tests--auth-source-entries) (lambda (&rest _) (zerop (random 2))))) (netrc-file (make-temp-file "auth-source-test" nil nil (mapconcat 'identity entries "\n"))) (auth-sources (list netrc-file)) (auth-source-do-cache nil) (erc-isupport-parameters '((CHANTYPES "&#"))) (erc--buffer-target (erc--target-from-string "&chan"))) (unwind-protect (ert-info ("Announced prioritized") (ert-info ("Announced wins") (let* ((erc-session-server "irc.gnu.org") (erc-server-announced-name "west.gnu.org") (erc-session-port 6697) (erc-network 'GNU.chat) (erc-server-current-nick "tester") (erc--session (erc--sid-create nil))) (should (string= (erc--auth-source-search :user "#chan") "baz")))) (ert-info ("Peer next") (let* ((erc-server-announced-name "irc.gnu.org") (erc-session-port 6697) (erc-network 'GNU.chat) (erc-server-current-nick "tester") (erc--session (erc--sid-create nil))) (should (string= (erc--auth-source-search :user "#chan") "bar")))) (ert-info ("Network used as fallback") (let* ((erc-session-port 6697) (erc-network 'GNU.chat) (erc-server-current-nick "tester") (erc--session (erc--sid-create nil))) (should (string= (erc--auth-source-search :user "#chan") "foo"))))) (delete-file netrc-file)))) (ert-deftest erc--auth-source-search--overrides () (let* ((extra (list "machine GNU.chat port 6697 user \"#chan\" password spam" "machine west.gnu.org port irc user \"#fsf\" password 42" "machine irc.gnu.org port 6667 password sesame" "machine MyHost port irc password 456" "machine MyHost port 6667 password 123")) (entries (sort (append erc-join-tests--auth-source-entries extra) (lambda (&rest _) (zerop (random 2))))) (netrc-file (make-temp-file "auth-source-test" nil nil (mapconcat 'identity entries "\n"))) (auth-sources (list netrc-file)) (auth-source-do-cache nil) (erc-session-server "irc.gnu.org") (erc-server-announced-name "west.gnu.org") (erc-network 'GNU.chat) (erc-server-current-nick "tester") (erc--session (erc--sid-create nil)) (erc-session-port 6667)) (unwind-protect (ert-info ("Specificity and overrides") (ert-info ("More specific port") (let ((erc-session-port 6697)) (should (string= (erc--auth-source-search :user "#chan") "spam")))) (ert-info ("Network wins") (should (string= (erc--auth-source-search :user '("#fsf")) "42"))) (ert-info ("Actual override") (should (string= (erc--auth-source-search :port "6667") "sesame"))) (ert-info ("Overrides don't interfere with post-processing") (should (string= (erc--auth-source-search :host "MyHost") "123")))) (delete-file netrc-file)))) (ert-deftest erc-ring-previous-command-base-case () (ert-info ("Create ring when nonexistent and do nothing") (let (erc-input-ring erc-input-ring-index) (erc-previous-command) (should (ring-p erc-input-ring)) (should (zerop (ring-length erc-input-ring))) (should-not erc-input-ring-index))) (should-not erc-input-ring)) (ert-deftest erc-ring-previous-command () (with-current-buffer (get-buffer-create "*#fake*") (erc-mode) (insert "\n\n") (should-not (local-variable-if-set-p 'erc-send-completed-hook)) (set (make-local-variable 'erc-send-completed-hook) nil) ; skip t (globals) (setq erc-input-marker (make-marker) erc-insert-marker (make-marker)) (set-marker erc-insert-marker (point-max)) (erc-display-prompt) (should (= (point) erc-input-marker)) ;; Just in case erc-ring-mode is already on (setq-local erc-pre-send-functions nil) (add-hook 'erc-pre-send-functions #'erc-add-to-input-ring) ;; (cl-letf (((symbol-function 'erc-process-input-line) (lambda (&rest _) (insert-before-markers (erc-display-message-highlight 'notice "echo: one\n")))) ((symbol-function 'erc-command-no-process-p) (lambda (&rest _) t))) (ert-info ("Create ring, populate, recall") (insert "/one") (erc-send-current-line) (should (ring-p erc-input-ring)) (should (zerop (ring-member erc-input-ring "/one"))) ; equal (should (save-excursion (forward-line -1) (goto-char (point-at-bol)) (looking-at-p "[*]+ echo: one"))) (should-not erc-input-ring-index) (erc-bol) (should (looking-at "$")) (erc-previous-command) (erc-bol) (should (looking-at "/one")) (should (zerop erc-input-ring-index))) (ert-info ("Back to one") (should (= (ring-length erc-input-ring) (1+ erc-input-ring-index))) (erc-previous-command) (should-not erc-input-ring-index) (erc-bol) (should (looking-at "$")) (should (equal (ring-ref erc-input-ring 0) "/one"))) (ert-info ("Swap input after prompt with previous (#bug46339)") (insert "abc") (erc-previous-command) (should (= 1 erc-input-ring-index)) (erc-bol) (should (looking-at "/one")) (should (equal (ring-ref erc-input-ring 0) "abc")) (should (equal (ring-ref erc-input-ring 1) "/one")) (erc-next-command) (erc-bol) (should (looking-at "abc"))))) (when noninteractive (kill-buffer "*#fake*"))) (ert-deftest erc-log-irc-protocol () (should-not erc-debug-irc-protocol) (with-temp-buffer (setq erc-server-process (start-process "fake" (current-buffer) "true") erc-server-current-nick "tester" erc-session-server "myproxy.localhost" erc-session-port 6667) (let ((inhibit-message noninteractive)) (erc-toggle-debug-irc-protocol) (erc-log-irc-protocol "PASS changeme\r\n" 'outgoing) (setq erc-server-announced-name "irc.gnu.org") (erc-log-irc-protocol ":irc.gnu.org 001 tester :Welcome") (erc-log-irc-protocol ":irc.gnu.org 002 tester :Your host is irc.gnu.org") (setq erc-network 'FooNet) (setq erc--session (erc--sid-create nil)) (erc-log-irc-protocol ":irc.gnu.org 422 tester :MOTD missing") (setq erc--session (erc--sid-create 'BarNet)) (erc-log-irc-protocol ":irc.gnu.org 221 tester +i") (set-process-query-on-exit-flag erc-server-process nil))) (with-current-buffer "*erc-protocol*" (goto-char (point-min)) (search-forward "Version") (search-forward "\r\n\r\n") (search-forward "myproxy.localhost:6667 >> PASS" (line-end-position)) (forward-line) (search-forward "irc.gnu.org << :irc.gnu.org 001" (line-end-position)) (forward-line) (search-forward "irc.gnu.org << :irc.gnu.org 002" (line-end-position)) (forward-line) (search-forward "FooNet << :irc.gnu.org 422" (line-end-position)) (forward-line) (search-forward "BarNet << :irc.gnu.org 221" (line-end-position))) (when noninteractive (kill-buffer "*erc-protocol*") (should-not erc-debug-irc-protocol))) ;; The point of this test is to ensure output is handled identically ;; regardless of whether a command handler is summoned. (ert-deftest erc-process-input-line () (let (erc-server-last-sent-time erc-server-flood-queue (orig-erc-cmd-MSG (symbol-function 'erc-cmd-MSG)) (erc-default-recipients '("#chan")) calls) (with-temp-buffer (cl-letf (((symbol-function 'erc-cmd-MSG) (lambda (line) (push line calls) (funcall orig-erc-cmd-MSG line))) ((symbol-function 'erc-server-buffer) (lambda () (current-buffer))) ((symbol-function 'erc-server-process-alive) (lambda () t)) ((symbol-function 'erc-server-send-queue) #'ignore)) (ert-info ("Dispatch to user command handler") (ert-info ("Baseline") (erc-process-input-line "/msg #chan hi\n") (should (equal (pop calls) " #chan hi")) (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :hi\r\n" . utf-8)))) (ert-info ("Quote preserves line intact") (erc-process-input-line "/QUOTE FAKE foo bar\n") (should (equal (pop erc-server-flood-queue) '("FAKE foo bar\r\n" . utf-8)))) (ert-info ("Unknown command respected") (erc-process-input-line "/FAKE foo bar\n") (should (equal (pop erc-server-flood-queue) '("FAKE foo bar\r\n" . utf-8)))) (ert-info ("Spaces preserved") (erc-process-input-line "/msg #chan hi you\n") (should (equal (pop calls) " #chan hi you")) (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :hi you\r\n" . utf-8)))) (ert-info ("Empty line honored") (erc-process-input-line "/msg #chan\n") (should (equal (pop calls) " #chan")) (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :\r\n" . utf-8))))) (ert-info ("Implicit cmd via `erc-send-input-line-function'") (ert-info ("Baseline") (erc-process-input-line "hi") (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :hi\r\n" . utf-8)))) (ert-info ("Spaces preserved") (erc-process-input-line "hi you") (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :hi you\r\n" . utf-8)))) (ert-info ("Empty line transmitted without injected-space kludge") (erc-process-input-line "") (should (equal (pop erc-server-flood-queue) '("PRIVMSG #chan :\r\n" . utf-8)))) (should-not calls)))))) (defun erc-tests--create-dead-proc (&optional buf) (let ((p (start-process "true" (or buf (current-buffer)) "true"))) (while (process-live-p p) (sit-for 0.1)) p)) (defun erc-tests--create-live-proc (&optional buf) (let ((proc (start-process "sleep" (or buf (current-buffer)) "sleep" "1"))) (set-process-query-on-exit-flag proc nil) proc)) (defun erc-tests--clean-bufs () (let (erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) (dolist (buf (erc-buffer-list)) (kill-buffer buf)))) (defun erc-tests--bufnames (prefix) (let* ((case-fold-search) (pred (lambda (b) (string-prefix-p prefix (buffer-name b)))) (prefixed (seq-filter pred (buffer-list)))) (sort (mapcar #'buffer-name prefixed) #'string<))) (ert-deftest erc--sid () (cl-letf (((symbol-function 'float-time) (lambda () 0.0))) ;; Fixed (should (equal (erc--sid-fixed-create 'foo) (make-erc--sid-fixed :ts (float-time) :symbol 'foo))) ;; Dynamic (let* ((erc-network 'FooNet) (erc-server-current-nick "Joe") (session (erc--sid-create nil))) (should (equal session #s(erc--sid-dynamic 0.0 FooNet [FooNet "joe"] 1))) (should (equal (erc--sid-dynamic-grow-id session) 'FooNet/joe)) (should (equal session #s(erc--sid-dynamic 0.0 FooNet/joe [FooNet "joe"] 2))) (should-not (erc--sid-dynamic-grow-id session)) (should (equal session #s(erc--sid-dynamic 0.0 FooNet/joe [FooNet "joe"] 2)))))) (ert-deftest erc--sid-dynamic-prefix-length () (should-not (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic) (make-erc--sid-dynamic))) (should-not (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic :parts [1 2]) (make-erc--sid-dynamic :parts [2 3]))) (should (= 1 (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic :parts [1]) (make-erc--sid-dynamic :parts [1 2])))) (should (= 1 (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic :parts [1 2]) (make-erc--sid-dynamic :parts [1 3])))) (should (= 2 (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic :parts [1 2]) (make-erc--sid-dynamic :parts [1 2])))) (should (= 1 (erc--sid-dynamic-prefix-length (make-erc--sid-dynamic :parts ["1"]) (make-erc--sid-dynamic :parts ["1"]))))) (ert-deftest erc--sid-sort-buffers () (let (oldest middle newest) (with-temp-buffer (setq erc--session (erc--sid-fixed-create 'oldest) oldest (current-buffer)) (with-temp-buffer (setq erc--session (erc--sid-fixed-create 'middle) middle (current-buffer)) (with-temp-buffer (setq erc--session (erc--sid-fixed-create 'newest) newest (current-buffer)) (should (equal (erc--sid-sort-buffers (list oldest newest middle)) (list newest middle oldest)))))))) (ert-deftest erc--maybe-rename-surviving-target-buffer--channel () (should (memq #'erc--maybe-rename-surviving-target-buffer erc-kill-channel-hook)) (let ((chan-foonet-buffer (get-buffer-create "#chan@foonet"))) (with-current-buffer chan-foonet-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (setq erc--buffer-target (erc--target-from-string "#chan"))) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [barnet "bob"] :len 1)) (setq erc--buffer-target (erc--target-from-string "#chan"))) (kill-buffer "#chan@barnet") (should (equal (erc-tests--bufnames "#chan") '("#chan"))) (should (eq chan-foonet-buffer (get-buffer "#chan")))) (erc-tests--clean-bufs)) (ert-deftest erc--maybe-rename-surviving-target-buffer--query () (should (memq #'erc--maybe-rename-surviving-target-buffer erc-kill-buffer-hook)) (let ((bob-foonet (get-buffer-create "bob@foonet"))) (with-current-buffer bob-foonet (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (setq erc--buffer-target (erc--target-from-string "bob"))) (with-current-buffer (get-buffer-create "bob@barnet") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [barnet "bob"] :len 1)) (setq erc--buffer-target (erc--target-from-string "bob"))) (kill-buffer "bob@barnet") (should (equal (erc-tests--bufnames "bob") '("bob"))) (should (eq bob-foonet (get-buffer "bob")))) (erc-tests--clean-bufs)) (ert-deftest erc--maybe-rename-surviving-target-buffer--multi () (ert-info ("Multiple leftover channels untouched") (with-current-buffer (get-buffer-create "#chan@foonet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan"))) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan"))) (with-current-buffer (get-buffer-create "#chan@baznet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan"))) (kill-buffer "#chan@baznet") (should (equal (erc-tests--bufnames "#chan") '("#chan@barnet" "#chan@foonet"))) (erc-tests--clean-bufs)) (ert-info ("Multiple leftover queries untouched") (with-current-buffer (get-buffer-create "bob@foonet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "bob"))) (with-current-buffer (get-buffer-create "bob@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "bob"))) (with-current-buffer (get-buffer-create "bob@baznet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "bob"))) (kill-buffer "bob@baznet") (should (equal (erc-tests--bufnames "bob") '("bob@barnet" "bob@foonet"))) (erc-tests--clean-bufs))) (ert-deftest erc--shrink-ids-and-buffer-names-perform--outstanding () ;; Not collapsed because we have one collision outstanding. ;; ;; Overlaps with quite a bit with the ;; `erc--shrink-ids-and-buffer-names--hook-outstanding-*' stuff ;; below. If this ever fails, just delete this and fix those. ;; Presumably, some buffer foonet/chester was just killed (with-current-buffer (get-buffer-create "foonet/tester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'foonet/tester :parts [foonet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create (elt ["#a" "#a@foonet" "#a@foonet/tester"] (random 3))) (erc-mode) (setq erc-server-process (with-current-buffer "foonet/tester" erc-server-process) erc-network 'foonet erc-server-current-nick "tester" erc--session (with-current-buffer "foonet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a"))) (with-current-buffer (get-buffer-create "barnet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'barnet/tester :parts [barnet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "barnet/chester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'barnet/chester :parts [barnet "chester"] :len 2) erc-server-process (erc-tests--create-live-proc))) ;; Presumably, some buffer #a@barnet/chester was just killed (with-current-buffer (get-buffer-create (elt ["#a@barnet" "#a@barnet/tester"] (random 2))) (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc-server-process (with-current-buffer "barnet/tester" erc-server-process) erc--session (with-current-buffer "barnet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a"))) (with-temp-buffer (setq erc--session (make-erc--sid-dynamic)) (erc--shrink-ids-and-buffer-names)) (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "#a@foonet" "barnet/tester" "barnet/chester" "#a@barnet/tester"))) (erc-tests--clean-bufs)) (ert-deftest erc--shrink-ids-and-buffer-names-perform--collapse () ;; Overlaps with `erc--shrink-ids-and-buffer-names--collapse-hook-*' ;; quite a bit. If this ever fails, just delete it and fix ^. ;; Presumably, some buffer foonet/chester was just killed (with-current-buffer (get-buffer-create "foonet/tester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'foonet/tester :parts [foonet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create (elt ["#a" "#a@foonet/tester"] (random 2))) (erc-mode) (setq erc-server-process (with-current-buffer "foonet/tester" erc-server-process) erc-network 'foonet erc-server-current-nick "tester" erc--session (with-current-buffer "foonet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a"))) (with-current-buffer (get-buffer-create "barnet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'barnet/tester :parts [barnet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create (elt ["#b" "#b@foonet/tester"] (random 2))) (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc-server-process (with-current-buffer "barnet/tester" erc-server-process) erc--session (with-current-buffer "barnet/tester" erc--session) erc--buffer-target (erc--target-from-string "#b"))) (with-temp-buffer (setq erc--session (make-erc--sid-dynamic)) (erc--shrink-ids-and-buffer-names)) (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "#a" "barnet" "#b"))) (erc-tests--clean-bufs)) (defun erc--shrink-ids-and-buffer-names--hook-outstanding-common () (with-current-buffer (get-buffer-create "foonet/tester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'foonet/tester :parts [foonet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "#a@foonet/tester") (erc-mode) (setq erc-server-process (with-current-buffer "foonet/tester" erc-server-process) erc-network 'foonet erc-server-current-nick "tester" erc--session (with-current-buffer "foonet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a"))) (with-current-buffer (get-buffer-create "barnet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'barnet/tester :parts [barnet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "barnet/chester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'barnet/chester :parts [barnet "chester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "#a@barnet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc-server-process (with-current-buffer "barnet/tester" erc-server-process) erc--session (with-current-buffer "barnet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a")))) (ert-deftest erc--shrink-ids-and-buffer-names--hook-outstanding-server () (erc--shrink-ids-and-buffer-names--hook-outstanding-common) (with-current-buffer (get-buffer-create "foonet/chester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'foonet/chester :parts [foonet "chester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer "foonet/chester" (kill-buffer)) (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "#a@foonet" "barnet/tester" "barnet/chester" "#a@barnet/tester"))) (erc-tests--clean-bufs)) (ert-deftest erc--shrink-ids-and-buffer-names--hook-outstanding-target () (erc--shrink-ids-and-buffer-names--hook-outstanding-common) (with-current-buffer (get-buffer-create "#a@foonet/chester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'foonet/chester :parts [foonet "chester"] :len 2) erc--buffer-target (erc--target-from-string "#a") erc-server-process (with-temp-buffer (erc-tests--create-dead-proc)))) (with-current-buffer "#a@foonet/chester" (kill-buffer)) ;; Identical to *-server variant above (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "#a@foonet" "barnet/tester" "barnet/chester" "#a@barnet/tester"))) (erc-tests--clean-bufs)) (ert-deftest erc--maybe-rename-surviving-target-buffer--shrink () (erc--shrink-ids-and-buffer-names--hook-outstanding-common) ;; This buffer isn't "#a@foonet" (yet) because the shrink-ids hook ;; hasn't run. However, when it's the rename hook runs, its session ;; id *is* "foonet", not "foonet/tester". (with-current-buffer "#a@foonet/tester" (kill-buffer)) (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "barnet/tester" "barnet/chester" "#a"))) (erc-tests--clean-bufs)) (defun erc--shrink-ids-and-buffer-names--hook-collapse (check) (with-current-buffer (get-buffer-create "foonet/tester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'foonet/tester :parts [foonet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "#a@foonet/tester") (erc-mode) (setq erc-server-process (with-current-buffer "foonet/tester" erc-server-process) erc-network 'foonet erc-server-current-nick "tester" erc--session (with-current-buffer "foonet/tester" erc--session) erc--buffer-target (erc--target-from-string "#a"))) (with-current-buffer (get-buffer-create "barnet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (make-erc--sid-dynamic :symbol 'barnet/tester :parts [barnet "tester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "#b@foonet/tester") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc-server-process (with-current-buffer "barnet/tester" erc-server-process) erc--session (with-current-buffer "barnet/tester" erc--session) erc--buffer-target (erc--target-from-string "#b"))) (funcall check) (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet" "#a" "barnet" "#b"))) (erc-tests--clean-bufs)) (ert-deftest erc--shrink-ids-and-buffer-names--hook-collapse-server () (erc--shrink-ids-and-buffer-names--hook-collapse (lambda () (with-current-buffer (get-buffer-create "foonet/chester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'foonet/chester :parts [foonet "chester"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer "foonet/chester" (kill-buffer))))) (ert-deftest erc--shrink-ids-and-buffer-names--hook-collapse-target () (erc--shrink-ids-and-buffer-names--hook-collapse (lambda () (with-current-buffer (get-buffer-create "#a@foonet/chester") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "chester" erc--session (make-erc--sid-dynamic :symbol 'foonet/chester :parts [foonet "chester"] :len 2) ;; `erc-kill-buffer-function' uses legacy target detection ;; but falls back on buffer name, so no need for: ;; ;; erc-default-recipients '("#a") ;; erc--buffer-target (erc--target-from-string "#a") erc-server-process (with-temp-buffer (erc-tests--create-dead-proc)))) (with-current-buffer "#a@foonet/chester" (kill-buffer))))) ;; FIXME this test is old and may describe impossible states: ;; leftover sessions being qual-equal but not eq (implies ;; `erc-networks--maybe-reclaim-target-buffers' is somehow broken). ;; ;; Otherwise, the point of this test is to show that server process ;; identity does not impact the hunt for duplicates. (defun erc-tests--prep-erc-reconcile-buffer-names--duplicates (start) (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (erc--sid-create nil) erc-server-process (funcall start))) (with-current-buffer (get-buffer-create "#chan") ; prior session (erc-mode) (setq erc-server-process (with-current-buffer "foonet" erc-server-process) erc--buffer-target (erc--target-from-string "#chan") erc--session (erc--sid-create nil))) (ert-info ("Conflicts not recognized as ERC buffers and not renamed") (get-buffer-create "#chan@foonet") (should (equal (erc-tests--bufnames "#chan") '("#chan" "#chan@foonet")))) ;; These are dupes (not "collisions") (with-current-buffer "#chan@foonet" ; same proc (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan") erc-network 'foonet erc-server-current-nick "tester" erc-server-process (with-current-buffer "foonet" erc-server-process) erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "#chan@foonet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan") erc-server-process (erc-tests--create-dead-proc) erc-network 'foonet erc-server-current-nick "tester" erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "#chan@foonet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan") erc-server-process (erc-tests--create-live-proc) erc-network 'foonet erc-server-current-nick "tester" erc--session (erc--sid-create nil))) (let ((created (list (get-buffer "#chan@foonet") (get-buffer "#chan@foonet") (get-buffer "#chan@foonet")))) (with-current-buffer "foonet" (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan"))) (ert-info ("All buffers considered dupes renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan" "#chan<2>" "#chan<3>" "#chan<4>")))) (ert-info ("All buffers renamed from newest to oldest") (should (equal created (list (get-buffer "#chan<2>") (get-buffer "#chan<3>") (get-buffer "#chan<4>")))))) (erc-tests--clean-bufs)) (defun erc-tests--prep-erc-reconcile-buffer-names--duplicates-given (start) (with-current-buffer (get-buffer-create "oofnet") (erc-mode) (setq erc--session (erc--sid-create 'oofnet)) ;; The network is determined before target bufs are created. At ;; least this shows that the network doesn't matter when only ;; assigned (given) IDs are present. (setq erc-network 'foonet) (setq erc-server-current-nick "tester") (setq erc-server-process (funcall start))) (with-current-buffer (get-buffer-create "#chan") ; prior session (erc-mode) (setq erc--session (erc--sid-create 'oofnet) erc-server-process (with-current-buffer "oofnet" erc-server-process) erc--buffer-target (erc--target-from-string "#chan"))) (with-current-buffer (get-buffer-create "#chan@oofnet") ;dupe/not collision (erc-mode) (setq erc--session (erc--sid-create 'oofnet) erc-server-process (with-current-buffer "oofnet" erc-server-process) erc--buffer-target (erc--target-from-string "#chan"))) (with-current-buffer "oofnet" (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan"))) (ert-info ("All buffers matching target and network renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan" "#chan<2>")))) (erc-tests--clean-bufs)) (ert-deftest erc--reconcile-buffer-names--duplicates () (ert-info ("Process live, no error") (erc-tests--prep-erc-reconcile-buffer-names--duplicates #'erc-tests--create-live-proc)) (ert-info ("Process live, no error, given ID") (erc-tests--prep-erc-reconcile-buffer-names--duplicates-given #'erc-tests--create-live-proc)) (ert-info ("Process dead") (erc-tests--prep-erc-reconcile-buffer-names--duplicates #'erc-tests--create-dead-proc)) (ert-info ("Process dead, given ID") (erc-tests--prep-erc-reconcile-buffer-names--duplicates-given #'erc-tests--create-dead-proc))) (defun erc-tests--prep-erc-reconcile-buffer-names--no-server-buf (check) (let ((foonet-proc (with-temp-buffer (erc-tests--create-dead-proc)))) (with-current-buffer (get-buffer-create "barnet") (erc-mode) (setq erc-network 'barnet) (setq erc-server-current-nick "tester") (setq erc--session (erc--sid-create nil)) (setq erc-server-process (erc-tests--create-dead-proc))) ;; Different proc and not "qual-equal" (different elts) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'foonet) (setq erc-server-current-nick "tester") (setq erc--session (erc--sid-create nil)) (setq erc--buffer-target (erc--target-from-string "#chan")) (setq erc-server-process foonet-proc)) (funcall check) (erc-tests--clean-bufs))) (ert-deftest erc--reconcile-buffer-names--no-server-buf () (ert-info ("Existing #chan buffer respected") (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf (lambda () (with-current-buffer "barnet" (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan@barnet"))) (ert-info ("Existing #chan buffer found and renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan@foonet"))))))) (ert-info ("Existing #chan buffer") (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf (lambda () (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc-network 'foonet) (setq erc-server-current-nick "tester") (setq erc--session (erc--sid-create nil)) (setq erc-server-process (erc-tests--create-dead-proc)) (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan"))) (ert-info ("Nothing renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan"))))))) (ert-info ("Existing #chan@foonet and #chan@barnet buffers") (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf (lambda () (with-current-buffer "#chan" (rename-buffer "#chan@foonet")) (should-not (get-buffer "#chan@barnet")) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (setq erc-server-process (with-current-buffer "barnet" erc-server-process)) (setq erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc-network 'foonet) (setq erc-server-current-nick "tester") (setq erc-server-process (erc-tests--create-live-proc)) (setq erc--session (erc--sid-create nil)) (set-process-query-on-exit-flag erc-server-process nil) (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan@foonet"))) (ert-info ("Nothing renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan@barnet" "#chan@foonet")))))))) (defun erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given (check) (let ((oofnet-proc (with-temp-buffer (erc-tests--create-dead-proc)))) (with-current-buffer (get-buffer-create "rabnet") (erc-mode) ;; Again, given name preempts network lookup (unrealistic but ;; highlights priorities) (setq erc--session (erc--sid-create 'rabnet)) (setq erc-network 'barnet) (setq erc-server-current-nick "tester") (setq erc-server-process (erc-tests--create-dead-proc))) ;; Session is not "qual-equal" to above (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc--session (erc--sid-create 'oofnet)) (setq erc-network 'foonet) (setq erc--buffer-target (erc--target-from-string "#chan")) (setq erc-server-process oofnet-proc)) (funcall check) (erc-tests--clean-bufs))) (ert-deftest erc--reconcile-buffer-names--no-server-buf-given () (ert-info ("Existing #chan buffer respected") (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given (lambda () (with-current-buffer "rabnet" (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan@rabnet"))) (ert-info ("Existing #chan buffer found and renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet"))))))) (ert-info ("Existing #chan@oofnet and #chan@rabnet buffers") (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given (lambda () ;; #chan has already been uniquified (but not grown) (with-current-buffer "#chan" (rename-buffer "#chan@oofnet")) (should-not (get-buffer "#chan@rabnet")) (with-current-buffer (get-buffer-create "#chan@rabnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (setq erc-server-process (with-current-buffer "rabnet" erc-server-process)) (setq erc--session (with-current-buffer "rabnet" erc--session))) (with-current-buffer (get-buffer-create "oofnet") (erc-mode) (setq erc-network 'oofnet) (setq erc-server-current-nick "tester") (setq erc-server-process (erc-tests--create-live-proc)) (setq erc--session (erc--sid-create 'oofnet)) ; given (set-process-query-on-exit-flag erc-server-process nil) (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan@oofnet"))) (ert-info ("Nothing renamed") (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet" "#chan@rabnet")))))))) ;; This is a corner case in which the user previously explicitly ;; assigned an ID via `erc-tls' but has since connected again, this ;; time without one. It would actually probably be better if the ;; given session were to win and the derived got an -suffix. ;; ;; If we just compared session-IDs, the two would match, but they ;; don't here because one has a given name and the other a ;; discovered/assembled one; so they are *not* qual-equal. (ert-deftest erc--reconcile-buffer-names--no-server-buf-given-mismatch () ;; Existing #chan buffer *not* respected (erc-tests--prep-erc-reconcile-buffer-names--no-server-buf-given (lambda () (with-current-buffer (get-buffer-create "oofnet") (erc-mode) (setq erc-network 'oofnet) (setq erc-server-current-nick "tester") (setq erc-server-process (erc-tests--create-dead-proc)) (setq erc--session (erc--sid-create nil)) ; derived (should (string= (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session) "#chan@oofnet"))) (ert-info ("Collision renamed but not grown (because it's a given)") ;; Original chan uniquified and moved out of the way (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet<2>"))))))) (defun erc-tests--prep-erc-reconcile-buffer-names--multi-net (check) (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc-server-process (erc-tests--create-dead-proc) erc--session (erc--sid-create nil))) ; derived (with-current-buffer (get-buffer-create "barnet") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc-server-process (erc-tests--create-dead-proc) erc--session (erc--sid-create nil))) ; derived (with-current-buffer (get-buffer-create (elt ["#chan" "#chan@foonet"] (random 2))) (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "foonet" (list erc-server-process erc--session)))) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "barnet" (list erc-server-process erc--session)))) (funcall check) (erc-tests--clean-bufs)) (ert-deftest erc--reconcile-buffer-names--multi-net () (ert-info ("Same network rename") (erc-tests--prep-erc-reconcile-buffer-names--multi-net (lambda () (with-current-buffer "foonet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@foonet")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@barnet" "#chan@foonet")))))) (ert-info ("Same network keep name") (erc-tests--prep-erc-reconcile-buffer-names--multi-net (lambda () (with-current-buffer "barnet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@barnet")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@barnet" "#chan@foonet"))))))) (defun erc-tests--prep-erc-reconcile-buffer-names--multi-net-given (check) (with-current-buffer (get-buffer-create "oofnet") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (erc--sid-create 'oofnet) ; one given erc-server-process (erc-tests--create-dead-proc))) (with-current-buffer (get-buffer-create "rabnet") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (erc--sid-create 'rabnet) ; another given erc-server-process (erc-tests--create-dead-proc))) (with-current-buffer (get-buffer-create (elt ["chan" "#chan@oofnet"] (random 2))) (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "oofnet" (list erc-server-process erc--session)))) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "rabnet" (list erc-server-process erc--session)))) (funcall check) (erc-tests--clean-bufs)) (ert-deftest erc--reconcile-buffer-names--multi-net-given () (ert-info ("Same network rename") (erc-tests--prep-erc-reconcile-buffer-names--multi-net-given (lambda () (with-current-buffer "oofnet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@oofnet")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet" "#chan@rabnet")))))) (ert-info ("Same network keep name") (erc-tests--prep-erc-reconcile-buffer-names--multi-net-given (lambda () (with-current-buffer "rabnet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@rabnet")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@oofnet" "#chan@rabnet"))))))) (defun erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed (check) (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "tester" erc--session (erc--sid-create nil) ; one derived erc-server-process (erc-tests--create-dead-proc))) (with-current-buffer (get-buffer-create "my-conn") (erc-mode) (setq erc-network 'barnet erc-server-current-nick "tester" erc--session (erc--sid-create 'my-conn) ; one given erc-server-process (erc-tests--create-dead-proc))) (with-current-buffer (get-buffer-create (elt ["#chan" "#chan@foonet"] (random 2))) (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "foonet" (list erc-server-process erc--session)))) (with-current-buffer (get-buffer-create "#chan@my-conn") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (cl-multiple-value-setq (erc-server-process erc--session) (with-current-buffer "my-conn" (list erc-server-process erc--session)))) (funcall check) (erc-tests--clean-bufs)) (ert-deftest erc--reconcile-buffer-names--multi-net-existing () (ert-info ("Buf name derived from network") (erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed (lambda () (with-current-buffer "foonet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@foonet")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@foonet" "#chan@my-conn")))))) (ert-info ("Buf name given") (erc-tests--prep-erc-reconcile-buffer-names--multi-net-mixed (lambda () (with-current-buffer "my-conn" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@my-conn")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@foonet" "#chan@my-conn"))))))) (ert-deftest erc--reconcile-buffer-names--multi-net-suffixed () ;; Two networks, same channel. One network has two connections. ;; When same channel joined on latter network with different nick, ;; all buffer names invovling that net are suffixed with session-id. (with-current-buffer (get-buffer-create "foonet/bob") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "bob" erc--session (make-erc--sid-dynamic :symbol 'foonet/bob :parts [foonet "bob"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create (elt ["#chan@foonet" "#chan@foonet/bob"] (random 2))) (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan") erc-server-process (with-current-buffer "foonet/bob" erc-server-process) erc--session (with-current-buffer "foonet/bob" erc--session))) (with-current-buffer (get-buffer-create "barnet") (erc-mode) (setq erc-network 'barnet erc-server-current-nick (elt ["alice" "bob"] (random 2)) erc--session (erc--sid-create 'barnet) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer (get-buffer-create "#chan@barnet") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan") erc-server-process (with-current-buffer "barnet" erc-server-process) erc--session (with-current-buffer "barnet" erc--session))) (with-current-buffer (get-buffer-create "foonet/alice") (erc-mode) (setq erc-network 'foonet erc-server-current-nick "alice" erc--session (make-erc--sid-dynamic :symbol 'foonet/alice :parts [foonet "alice"] :len 2) erc-server-process (erc-tests--create-live-proc))) (with-current-buffer "foonet/alice" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "#chan") erc--session))) (should (string= result "#chan@foonet/alice")))) (should (equal (erc-tests--bufnames "#chan") '("#chan@barnet" "#chan@foonet/bob"))) (erc-tests--clean-bufs)) (ert-deftest erc-reconcile-buffer-names--local () (let ((erc-isupport-parameters '((CHANTYPES "&#")))) (with-current-buffer (get-buffer-create "DALnet") (erc-mode) (setq erc-network 'DALnet erc-server-announced-name "elysium.ga.us.dal.net" erc-server-process (erc-tests--create-dead-proc) erc--session (erc--sid-create nil))) (ert-info ("Local chan buffer from older, disconnected session") (with-current-buffer (get-buffer-create "&chan") (erc-mode) (setq erc--buffer-target (erc--target-from-string "&chan") erc-network 'DALnet erc-server-announced-name "twisted.ma.us.dal.net" erc-server-process (erc-tests--create-dead-proc) erc--session (erc--sid-create nil)))) (ert-info ("Local channels renamed using network server names") (with-current-buffer "DALnet" (let ((result (erc--reconcile-buffer-names (erc--target-from-string "&chan") erc--session))) (should (string= result "&chan@elysium.ga.us.dal.net"))))) (should (get-buffer "&chan@twisted.ma.us.dal.net")) (should-not (get-buffer "&chan")) (erc-tests--clean-bufs))) ;;; erc-tests.el ends here