;;; erc-networks-tests.el --- Tests for erc-networks. -*- lexical-binding:t -*- ;; Copyright (C) 2020-2021 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 . ;;; Code: (require 'ert-x) ; cl-lib (require 'erc-networks) ;; FIXME maybe create common helpers file; these three are copied from ;; test/lisp/erc/erc-tests.el (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--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--clean-bufs () (let (erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) (dolist (buf (erc-buffer-list)) (kill-buffer buf)))) (ert-deftest erc-networks--set-name () (with-current-buffer (get-buffer-create "localhost:6667") (let (erc-server-announced-name erc-isupport-parameters erc-network calls) (erc-mode) (cl-letf (((symbol-function 'erc-display-line-1) (lambda (&rest r) (push r calls)))) (ert-info ("Errors when `erc-server-announced-name' unset") (should-error (erc-networks--set-name nil (make-erc-response)))) (should-not calls) (setq erc-server-announced-name "irc.fake.gnu.org") (ert-info ("Errors out when table empty and NETWORK param unset") (let ((err (should-error (erc-networks--set-name nil (make-erc-response))))) (should (string-match-p "failed" (cadr err))) (should (eq (car err) 'error))) (should (string-match-p "*** Failed" (car (pop calls))))))) (erc-tests--clean-bufs))) (ert-deftest erc-networks--rename-server-buffer--no-existing--orphan () (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc--buffer-target (erc--target-from-string "#chan") erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet"))) (ert-info ("Channel buffer reassociated") (erc-server-process-alive "#chan") (with-current-buffer "#chan" (should erc-server-connected) (erc-with-server-buffer (should (string= (buffer-name) "FooNet"))))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--existing--reuse () (let* ((old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-dead-proc old-buf))) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc--session (erc--sid-create nil) erc--buffer-target (erc--target-from-string "#chan"))) (ert-info ("New buffer steals name, content") (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet")) (goto-char (point-min)) (should (search-forward "Old buf")))) (ert-info ("Channel buffer reassociated") (erc-server-process-alive "#chan") (with-current-buffer "#chan" (should erc-server-connected) (should-not (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "FooNet"))))) (ert-info ("Original buffer killed off") (should-not (buffer-live-p old-buf)))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--existing--noreuse () (should erc-reuse-buffers) ; default (let* ((old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-dead-proc old-buf)) erc-reuse-buffers) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc--session (erc--sid-create nil) erc--buffer-target (erc--target-from-string "#chan"))) (ert-info ("Server buffer uniquely renamed") (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet<2>")) (goto-char (point-min)) (should-not (search-forward "Old buf" nil t)))) (ert-info ("Channel buffer reassociated") (erc-server-process-alive "#chan") (with-current-buffer "#chan" (should erc-server-connected) (should-not (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "FooNet<2>"))))) (ert-info ("Old buffer still around") (should (buffer-live-p old-buf)))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--reconnecting () (let* ((old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-dead-proc old-buf))) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc--buffer-target (erc--target-from-string "#chan") erc--session (erc--sid-create nil))) (ert-info ("No new buffer") (with-current-buffer old-buf (setq erc-server-process (erc-tests--create-live-proc)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet")) (goto-char (point-min)) (should (search-forward "Old buf")))) (ert-info ("Channel buffer updated with live proc") (erc-server-process-alive "#chan") (with-current-buffer "#chan" (should erc-server-connected) (should-not (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "FooNet")))))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--session-id () (let* ((old-buf (get-buffer-create "MySession")) (old-proc (erc-tests--create-dead-proc old-buf))) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc--session (erc--sid-create 'MySession) erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc)) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc--session (erc--sid-create 'MySession) erc-server-process old-proc erc--buffer-target (erc--target-from-string "#chan"))) (ert-info ("No new buffer") (with-current-buffer old-buf (setq erc-server-process (erc-tests--create-live-proc)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "MySession")) (goto-char (point-min)) (should (search-forward "Old buf")))) (ert-info ("Channel buffer updated with live proc") (erc-server-process-alive "#chan") (with-current-buffer "#chan" (should erc-server-connected) (should-not (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "MySession")))))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--existing--live () (let* (erc-kill-server-hook erc-insert-modify-hook (old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-live-proc old-buf))) ; live (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil)) (should (erc-server-process-alive))) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc--session (erc--sid-create nil) erc-server-connected t erc--buffer-target (erc--target-from-string "#chan"))) (ert-info ("New buffer rejected, abandoned, not killed") (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (eq erc-active-buffer old-buf)) (should-not (erc-server-process-alive)) (should (string= (buffer-name) "irc.foonet.org")) (goto-char (point-min)) (search-forward "still connected"))) (ert-info ("Channel buffer updated with live proc") (should (erc-server-process-alive "#chan")) (with-current-buffer "#chan" (should erc-server-connected) (should (erc-server-buffer-live-p)) (should (eq erc-server-process old-proc)) (should (buffer-live-p (process-buffer erc-server-process))) (with-current-buffer (process-buffer erc-server-process) (should (eq (current-buffer) (get-buffer "FooNet"))) (should (eq (current-buffer) old-buf)))))) (should (get-buffer "FooNet")) (should (get-buffer "irc.foonet.org")) (erc-tests--clean-bufs)) (ert-deftest erc-networks--rename-server-buffer--local-match () (let* ((old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-dead-proc old-buf)) (erc-isupport-parameters '((CHANTYPES "&#")))) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-announced-name "us-east.foonet.org" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "&chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc-server-announced-name "us-east.foonet.org" erc--buffer-target (erc--target-from-string "&chan") erc--session (erc--sid-create nil))) (ert-info ("New server buffer steals name, content") (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-announced-name "us-east.foonet.org" erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet")) (goto-char (point-min)) (should (search-forward "Old buf")))) (ert-info ("Channel buffer reassociated when &local server matches") (should (erc-server-process-alive "&chan")) (with-current-buffer "&chan" (should erc-server-connected) (should-not (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "FooNet"))))) (ert-info ("Original buffer killed off") (should-not (buffer-live-p old-buf))) (erc-tests--clean-bufs))) (ert-deftest erc-networks--rename-server-buffer--local-nomatch () (let* ((old-buf (get-buffer-create "FooNet")) (old-proc (erc-tests--create-dead-proc old-buf)) (erc-isupport-parameters '((CHANTYPES "&#")))) (with-current-buffer old-buf (erc-mode) (insert "*** Old buf") (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-announced-name "us-west.foonet.org" erc-insert-marker (set-marker (make-marker) (point-max)) erc-server-process old-proc erc--session (erc--sid-create nil))) (with-current-buffer (get-buffer-create "&chan") (erc-mode) (setq erc-network 'FooNet erc-server-process old-proc erc-server-announced-name "us-west.foonet.org" ; west erc--buffer-target (erc--target-from-string "&chan") erc--session (erc--sid-create nil))) (ert-info ("New server buffer steals name, content") (with-current-buffer (get-buffer-create "irc.foonet.org") (erc-mode) (setq erc-network 'FooNet erc-server-current-nick "tester" erc-server-announced-name "us-east.foonet.org" ; east erc-server-process (erc-tests--create-live-proc) erc--session (erc--sid-create nil)) (should-not (erc-networks--rename-server-buffer erc-server-process)) (should (string= (buffer-name) "FooNet")) (goto-char (point-min)) (should (search-forward "Old buf")))) (ert-info ("Channel buffer now orphaned even though network matches") (should-not (erc-server-process-alive "&chan")) (with-current-buffer "&chan" (should-not erc-server-connected) (should (eq erc-server-process old-proc)) (erc-with-server-buffer (should (string= (buffer-name) "FooNet"))))) (ert-info ("Original buffer killed off") (should-not (buffer-live-p old-buf))) (erc-tests--clean-bufs))) (ert-deftest erc-networks--update-server-session--double-existing () (with-temp-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (with-current-buffer (get-buffer-create "#chan@foonet/bob") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 2))) (with-current-buffer (get-buffer-create "foonet/alice") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice"] :len 2))) (ert-info ("Adopt equivalent session") (should (eq (erc-networks--update-server-session) (with-current-buffer "#chan@foonet/bob" erc--session)))) (ert-info ("Ignore non-matches") (should-not (erc-networks--update-server-session)) (should (eq erc--session (with-current-buffer "#chan@foonet/bob" erc--session))))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--update-server-session--double-new () (with-temp-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (with-current-buffer (get-buffer-create "foonet/alice") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice"] :len 2))) (with-current-buffer (get-buffer-create "#chan@foonet/alice") (erc-mode) (setq erc--session (with-current-buffer "foonet/alice" erc--session))) (ert-info ("Evolve session to prevent ambiguity") (should-not (erc-networks--update-server-session)) (should (= (erc--sid-dynamic-len erc--session) 2)) (should (eq (erc--sid-symbol erc--session) 'foonet/bob)))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--update-server-session--double-bounded () (with-temp-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (with-current-buffer (get-buffer-create "foonet/alice/home") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice" home] :len 3))) (with-current-buffer (get-buffer-create "#chan@foonet/alice/home") (erc-mode) (setq erc--session (with-current-buffer "foonet/alice/home" erc--session))) (ert-info ("Evolve session to prevent ambiguity") (should-not (erc-networks--update-server-session)) (should (= (erc--sid-dynamic-len erc--session) 2)) (should (eq (erc--sid-symbol erc--session) 'foonet/bob)))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--update-server-session--double-even () (with-temp-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1)) (with-current-buffer (get-buffer-create "foonet") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice"] :len 1))) (with-current-buffer (get-buffer-create "#chan") (erc-mode) (setq erc--buffer-target (erc--target-from-string "#chan")) (setq erc--session (with-current-buffer "foonet" erc--session))) (ert-info ("Evolve session to prevent ambiguity") (should-not (erc-networks--update-server-session)) (should (= (erc--sid-dynamic-len erc--session) 2)) (should (eq (erc--sid-symbol erc--session) 'foonet/bob))) (ert-info ("Collision renamed") (with-current-buffer "foonet/alice" (should (eq (erc--sid-symbol erc--session) 'foonet/alice))) (with-current-buffer "#chan@foonet/alice" (should (eq (erc--sid-symbol erc--session) 'foonet/alice))))) (erc-tests--clean-bufs)) (ert-deftest erc-networks--update-server-session--triple-new () (with-temp-buffer (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob" home] :len 1)) (with-current-buffer (get-buffer-create "foonet/bob/office") (erc-mode) (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob" office] :len 3))) (with-current-buffer (get-buffer-create "#chan@foonet/bob/office") (erc-mode) (setq erc--session (with-current-buffer "foonet/bob/office" erc--session))) (ert-info ("Extend our session name so that it's unique") (should-not (erc-networks--update-server-session)) (should (= (erc--sid-dynamic-len erc--session) 3)))) (erc-tests--clean-bufs)) ;;; erc-networks-tests.el ends here