unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 1d2f9883381c5dfe82c4f6bf708ff4a1455bb83b 82402 bytes (raw)
name: test/lisp/erc/erc-scenarios.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
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
 
;;; erc-scenarios.el --- user test cases for ERC -*- lexical-binding: t -*-

;; Copyright (C) 2021 Free Software Foundation, Inc.
;;
;; This file is part of GNU Emacs.
;;
;; This program 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.
;;
;; This program 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 this program.  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
;; accompanying erc-scenarios-resources directory.
;;
;; 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 simply 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 in the
;; companion file erc-scenarios-common.el.  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 this file to fail in a cascading manner (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.
;;

;;; Code:
(require 'ert-x) ; cl-lib

(eval-and-compile
  (let ((dir (getenv "EMACS_TEST_DIRECTORY")))
    (when dir (load (concat dir "/lisp/erc/erc-scenarios-common") nil t))))

(require 'erc-d)
(require 'erc-scenarios-common)
(require 'erc-backend)

(declare-function erc-network-name "erc-networks")
(declare-function erc-network "erc-networks")
(defvar erc-autojoin-channels-alist)
(defvar erc-network)

;; Two networks, same channel name, no confusion (no bouncer).  Some
;; of this draws from bug#47522 "foil-in-server-buf".  It shows that
;; disambiguation-related changes added for bug#48598 are not specific
;; to bouncers.

(defun erc-scenarios-common--base-association-multi-net (second-join)
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/multi-net")
       (erc-server-flood-penalty 0.1)
       (erc-d-linger-secs 1)
       (dumb-server-foonet-buffer (get-buffer-create "*server-foonet*"))
       (dumb-server-barnet-buffer (get-buffer-create "*server-barnet*"))
       (dumb-server-foonet (erc-d-run "localhost" t "server-foonet" 'foonet))
       (dumb-server-barnet (erc-d-run "localhost" t "server-barnet" 'barnet))
       (expect (erc-d-t-make-expecter)))

    (ert-info ("Connect to foonet, join #chan")
      (with-current-buffer
          (erc :server "127.0.0.1"
               :port (process-contact dumb-server-foonet :service)
               :nick "tester"
               :password "changeme"
               :full-name "tester")
        (funcall expect 3 "debug mode")
        (erc-cmd-JOIN "#chan")))

    (erc-d-t-wait-for 2 "Buffer #chan created" (get-buffer "#chan"))

    (ert-info ("Connect to barnet, join #chan")
      (with-current-buffer
          (erc :server "127.0.0.1"
               :port (process-contact dumb-server-barnet :service)
               :nick "tester"
               :password "changeme"
               :full-name "tester")
        (funcall expect 1 "debug mode")))

    (funcall second-join)

    (erc-d-t-wait-for 3 "Buf #chan@barnet created" (get-buffer "#chan@barnet"))

    (erc-d-t-wait-for 2 "Buf #chan now #chan@foonet"
      (and (get-buffer "#chan@foonet") (not (get-buffer "#chan"))))

    (ert-info ("All #chan@foonet output consumed")
      (with-current-buffer "#chan@foonet"
        (funcall expect 3 "bob")
        (funcall expect 3 "was created on")
        (funcall expect 3 "prosperous")))

    (ert-info ("All #chan@barnet output consumed")
      (with-current-buffer "#chan@barnet"
        (funcall expect 3 "mike")
        (funcall expect 3 "was created on")
        (funcall expect 3 "ingenuous")))))

(ert-deftest erc-scenarios-base-association-multi-net--baseline ()
  (erc-scenarios-common--base-association-multi-net
   (lambda () (with-current-buffer "barnet" (erc-cmd-JOIN "#chan")))))

;; The /join command only targets the current buffer's process.  This
;; recasts scenario bug#48598 "ambiguous-join" (which was based on
;; bug#47522) to show that issuing superfluous /join commands
;; (apparently fairly common) is benign.

(ert-deftest erc-scenarios-base-association-multi-net--ambiguous-join ()
  (erc-scenarios-common--base-association-multi-net
   (lambda ()
     (ert-info ("Nonsensical JOIN attempts silently dropped.")
       (with-current-buffer "foonet" (erc-cmd-JOIN "#chan"))
       (sit-for 0.1)
       (with-current-buffer "#chan" (erc-cmd-JOIN "#chan"))
       (sit-for 0.1)
       (erc-d-t-wait-for 2 "Buffer #chan endures" (get-buffer "#chan"))
       (erc-d-t-wait-for 1 "Only one #chan buffer exists"
         (should (equal (erc-scenarios-common-buflist "#chan")
                        (list (get-buffer "#chan")))))
       (with-current-buffer "*server-barnet*"
         (erc-d-t-search-for -0.1 "JOIN"))
       (with-current-buffer "barnet" (erc-cmd-JOIN "#chan"))))))

;; One network, two simultaneous connections, no IDs.
;; Reassociates on reconnect with and without server buffer.

(defun erc-scenarios-common--base-association-same-network (after)
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/same-network")
       (dumb-server (erc-d-run "localhost" t 'tester 'chester 'tester-again))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.5)
       (erc-server-flood-margin 30))

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

    (ert-info ("Connect to foonet with nick chester")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "chester"
                                :password "changeme"
                                :full-name "chester")
        (erc-scenarios-common-assert-initial-buf-name nil port)))

    (erc-d-t-wait-for 3 "Dialed Buflist is Empty"
      (not (erc-scenarios-common-buflist "127.0.0.1")))

    (with-current-buffer "foonet/tester"
      (funcall expect 3 "debug mode")
      (erc-cmd-JOIN "#chan"))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with tester"
      (get-buffer "#chan@foonet/tester"))

    (with-current-buffer "foonet/chester" (funcall expect 3 "debug mode"))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with chester"
      (get-buffer "#chan@foonet/chester"))

    (ert-info ("Nick tester sees other nick chester in channel")
      (with-current-buffer "#chan@foonet/tester"
        (funcall expect 5 "chester")
        (funcall expect 5 "find the forester")
        (erc-cmd-QUIT "")))

    (ert-info ("Nick chester sees other nick tester in same channel")
      (with-current-buffer  "#chan@foonet/chester"
        (funcall expect 5 "tester")
        (funcall expect 5 "find the forester")))

    (funcall after expect)))

(ert-deftest erc-scenarios-base-association-same-network--reconnect-one ()
  (erc-scenarios-common--base-association-same-network
   (lambda (expect)

     (ert-info ("Connection tester reconnects")
       (with-current-buffer "foonet/tester"
         (erc-d-t-wait-for 10 "Foonet connection deceased"
           (not (erc-server-process-alive)))
         (funcall expect 10 "*** ERC finished")
         (erc-cmd-RECONNECT)
         (funcall expect 5 "debug mode")))

     (ert-info ("Reassociated to same channel")
       (with-current-buffer "#chan@foonet/tester"
         (funcall expect 5 "chester")
         (funcall expect 5 "welcome again")
         (erc-cmd-QUIT "")))

     (with-current-buffer "#chan@foonet/chester"
       (funcall expect 5 "tester")
       (funcall expect 5 "welcome again")
       (funcall expect 5 "welcome again")
       (erc-cmd-QUIT "")))))

(ert-deftest erc-scenarios-base-association-same-network--new-buffer ()
  (erc-scenarios-common--base-association-same-network
   (lambda (expect)

     (ert-info ("Tester kills buffer and connects from scratch")

       (let (port)
         (with-current-buffer "foonet/tester"
           (erc-d-t-wait-for 10 "Foonet connection deceased"
             (not (erc-server-process-alive)))
           (funcall expect 10 "*** ERC finished")
           (setq port erc-session-port)
           (kill-buffer))

         (with-current-buffer (erc :server "127.0.0.1"
                                   :port port
                                   :nick "tester"
                                   :password "changeme"
                                   :full-name "tester")

           (erc-d-t-wait-for 5 "network determined"
             (eq erc-network 'foonet)))))

     (with-current-buffer "foonet/tester" (funcall expect 3 "debug mode"))

     (ert-info ("Reassociated to same channel")
       (with-current-buffer "#chan@foonet/tester"
         (funcall expect 5 "chester")
         (funcall expect 5 "welcome again")
         (erc-cmd-QUIT "")))

     (with-current-buffer "#chan@foonet/chester"
       (funcall expect 5 "tester")
       (funcall expect 5 "welcome again")
       (funcall expect 5 "welcome again")
       (erc-cmd-QUIT "")))))

;; 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.

(defun erc-scenarios-common--base-session-id-autojoin (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 "base/session-id/autojoin")
       (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))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (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 1 "Network set to foonet" (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"))
      (erc-d-t-wait-for 5 "Buffer #chan created" (get-buffer "#chan"))
      (with-current-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 "Network set to barnet" (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-search-for -0.1 "<joe>")
        (should-not (process-live-p erc-server-process))
        (erc-d-t-wait-for -0.1 "server buffer remains foonet"
          (not (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 "#chan buffers renamed" (get-buffer chan-buf-foo))
      (erc-d-t-wait-for 3 "#chan buffers renamed" (get-buffer chan-buf-bar))
      (with-current-buffer chan-buf-bar
        (erc-d-t-wait-for 3 "server-buffer is barnet"
          (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 "process is alive" (erc-server-process-alive))
        (setq erc-server-process-foo erc-server-process)
        (erc-d-t-wait-for 2 "Network set to foonet" (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 "server buffer is foonet"
          (eq erc-server-process erc-server-process-foo))
        (funcall expect 3 "<bob>")
        (erc-d-t-search-for -0.1 "<joe>")
        (while (accept-process-output erc-server-process-foo))
        (funcall expect 3 "not given me")))

    (ert-info ("All #chan@barnet output received")
      (with-current-buffer chan-buf-bar
        (while (accept-process-output erc-server-process-bar))
        (funcall expect 3 "hath an uncle here")))))

(ert-deftest erc-scenarios-base-session-id-autojoin--cmd-reconnect ()
  (let ((connect (lambda ()
                   (with-current-buffer "foonet"
                     (erc-cmd-RECONNECT)
                     (should (eq (current-buffer)
                                 (process-buffer erc-server-process)))
                     (current-buffer)))))
    (erc-scenarios-common--base-session-id-autojoin connect nil nil)))

(ert-deftest erc-scenarios-base-session-id-autojoin--cmd-reconnect-id ()
  (let ((connect (lambda ()
                   (with-current-buffer "oofnet"
                     (erc-cmd-RECONNECT)
                     (should (eq (current-buffer)
                                 (process-buffer erc-server-process)))
                     (current-buffer)))))
    (erc-scenarios-common--base-session-id-autojoin connect 'oofnet nil)))

(ert-deftest erc-scenarios-base-session-id-autojoin--cmd-reconnect-ids ()
  (let ((connect (lambda ()
                   (with-current-buffer "oofnet"
                     (erc-cmd-RECONNECT)
                     (should (eq (current-buffer)
                                 (process-buffer erc-server-process)))
                     (current-buffer)))))
    (erc-scenarios-common--base-session-id-autojoin connect 'oofnet 'rabnet)))

(ert-deftest erc-scenarios-base-session-id-autojoin--new-invocation ()
  (let ((connect (lambda ()
                   (erc :server "127.0.0.1"
                        :port (with-current-buffer "foonet"
                                (process-contact erc-server-process :service))
                        :nick "tester"
                        :password "foonet:changeme"
                        :full-name "tester"))))
    (erc-scenarios-common--base-session-id-autojoin connect nil nil)))

(ert-deftest erc-scenarios-base-session-id-autojoin--new-invocation-id ()
  (let ((connect (lambda ()
                   (erc :server "127.0.0.1"
                        :port (with-current-buffer "oofnet"
                                (process-contact erc-server-process :service))
                        :nick "tester"
                        :password "foonet:changeme"
                        :full-name "tester"
                        :id 'oofnet))))
    (erc-scenarios-common--base-session-id-autojoin connect 'oofnet nil)))

(ert-deftest erc-scenarios-base-session-id-autojoin--new-invocation-ids ()
  (let ((connect (lambda ()
                   (erc :server "127.0.0.1"
                        :port (with-current-buffer "oofnet"
                                (process-contact erc-server-process :service))
                        :nick "tester"
                        :password "foonet:changeme"
                        :full-name "tester"
                        :id 'oofnet))))
    (erc-scenarios-common--base-session-id-autojoin connect 'oofnet 'rabnet)))

;; Playback for same channel on two networks routed correctly.
;; Originally from Bug#48598: 28.0.50; buffer-naming collisions
;; involving bouncers in ERC.

(ert-deftest erc-scenarios-base-association-bouncer-history ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/bouncer-history")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-d-linger-secs 1)
       (dumb-server (erc-d-run "localhost" t 'foonet 'barnet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (erc-server-flood-penalty 0.5)
       (expect (erc-d-t-make-expecter))
       erc-autojoin-channels-alist
       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"))
        (setq erc-server-process-foo erc-server-process)
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (funcall expect 5 "foonet")))

    (erc-d-t-wait-for 5 "Playback JOINs tester to #chan" (get-buffer "#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"))
        (setq erc-server-process-bar erc-server-process)
        (erc-d-t-wait-for 5 "Temporary name assigned"
          (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (funcall expect 5 "barnet")))

    (ert-info ("Server buffers are unique")
      (should-not (eq erc-server-buffer-foo erc-server-buffer-bar)))

    (ert-info ("Networks correctly determined and adopted as buffer names")
      (with-current-buffer erc-server-buffer-foo
        (erc-d-t-wait-for 3 "network name foonet becomes buffer name"
          (and (eq (erc-network) 'foonet) (string= (buffer-name) "foonet"))))
      (with-current-buffer erc-server-buffer-bar
        (erc-d-t-wait-for 3 "network name barnet becomes buffer name"
          (and (eq (erc-network) 'barnet) (string= (buffer-name) "barnet")))))

    (erc-d-t-wait-for 5 "Barnet playback JOINs tester to it's #chan"
      (get-buffer "#chan@barnet"))

    (ert-info ("Two channel buffers created, original #chan renamed")
      (should (= 4 (length (erc-buffer-list))))
      (should (equal (list (get-buffer "#chan@barnet")
                           (get-buffer "#chan@foonet"))
                     (erc-scenarios-common-buflist "#chan"))))

    (ert-info ("#chan@foonet is exclusive, no cross-contamination")
      (with-current-buffer "#chan@foonet"
        (erc-d-t-search-for 1 "<bob>")
        (erc-d-t-search-for -0.1 "<joe>")
        (should (eq erc-server-process erc-server-process-foo))))

    (ert-info ("#chan@barnet is exclusive, no cross-contamination")
      (with-current-buffer "#chan@barnet"
        (erc-d-t-search-for 1 "<joe>")
        (erc-d-t-search-for -0.1 "<bob>")
        (should (eq erc-server-process erc-server-process-bar))))

    (ert-info ("All output sent")
      (with-current-buffer "#chan@foonet"
        (while (accept-process-output erc-server-process-foo))
        (erc-d-t-search-for 3 "please your lordship"))
      (with-current-buffer "#chan@barnet"
        (while (accept-process-output erc-server-process-bar))
        (erc-d-t-search-for 3 "I'll bid adieu")))))

(cl-defun erc-scenarios-common--base-session-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/session-id/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))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (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 "Network determined" (eq (erc-network) 'foonet))
        (erc-d-t-wait-for 3 "Final buffer name determined"
          (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"))
      (erc-d-t-wait-for 5 "Buffer #chan created" (get-buffer "#chan"))
      (with-current-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 3 "Network determined" (eq (erc-network) 'barnet))
        (erc-d-t-wait-for 3 "Final buffer name determined"
          (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-search-for -0.1 "<joe>")
        (should (eq erc-server-process erc-server-process-foo))
        (while (accept-process-output erc-server-process-foo))
        (erc-d-t-search-for 1 "ape is dead")
        (should-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-search-for -0.1 "<bob>")
        (should (eq erc-server-process erc-server-process-bar))
        (while (accept-process-output erc-server-process-bar))
        (erc-d-t-search-for 1 "keeps you from dishonour")
        (should-not (erc-server-process-alive))))

    (when after (funcall after))))

(ert-deftest erc-scenarios-base-session-id-bouncer--base ()
  (erc-scenarios-common--base-session-id-bouncer () 'foonet 'barnet))

(ert-deftest erc-scenarios-base-session-id-bouncer--id-foo ()
  (erc-scenarios-common--base-session-id-bouncer '(:foo-id t) 'foonet 'barnet))

(ert-deftest erc-scenarios-base-session-id-bouncer--id-bar ()
  (erc-scenarios-common--base-session-id-bouncer '(:bar-id t) 'foonet 'barnet))

(ert-deftest erc-scenarios-base-session-id-bouncer--both ()
  (erc-scenarios-common--base-session-id-bouncer '(:foo-id t :bar-id t)
                                                 'foonet 'barnet))

(defun erc-scenarios--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-session-id-bouncer--reconnect (foo-id bar-id)
  (let ((erc-d-spec-vars '((token . (group (| "barnet" "foonet")))))
        (erc-d-match-handlers
         ;; Auto reconnect is nondeterministic, so let computer decide
         (list :pass #'erc-scenarios--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 5 "ERC reconnected to foonet"
                 (erc-server-process-alive)))
             (with-current-buffer (if bar-id "rabnet" "barnet")
               (erc-d-t-wait-for 5 "ERC reconnected to barnet"
                 (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-search-for -0.1 "<joe>")
               (while (accept-process-output erc-server-process))
               (erc-d-t-search-for 3 "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-search-for -0.1 "<bob>")
               (while (accept-process-output erc-server-process))
               (erc-d-t-search-for 1 "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-session-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)))

(ert-deftest erc-scenarios-base-session-id-bouncer--reconnect-base ()
  (erc-scenarios-common--base-session-id-bouncer--reconnect nil nil))

(ert-deftest erc-scenarios-base-session-id-bouncer--reconnect-id-foo ()
  (erc-scenarios-common--base-session-id-bouncer--reconnect 'foo-id nil))

(ert-deftest erc-scenarios-base-session-id-bouncer--reconnect-id-bar ()
  (erc-scenarios-common--base-session-id-bouncer--reconnect nil 'bar-id))

(ert-deftest erc-scenarios-base-session-id-bouncer--reconnect-both ()
  (erc-scenarios-common--base-session-id-bouncer--reconnect 'foo-id 'bar-id))

;; Ensure deprecated option still respected when old default value
;; explicitly set ("respected" in the sense of having names reflect
;; dialed TCP endpoints with possible uniquifiers but without any of
;; the old issues, pre-bug#48598).

(defun erc-scenarios-common--base-compat-no-rename-bouncer (dialogs auto more)
  (erc-scenarios-common-with-cleanup
      ;; These actually *are* (assigned-)session-id related because
      ;; our kludge assigns one after the fact.
      ((erc-scenarios-common-dialog "base/session-id/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))
       (chan-buf-foo (format "#chan@127.0.0.1:%d" port))
       (chan-buf-bar (format "#chan@127.0.0.1:%d<2>" port))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       (erc-server-auto-reconnect auto)
       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 nil))
        (setq erc-server-process-foo erc-server-process)
        (erc-d-t-wait-for 3 "Network determined" (eq (erc-network) 'foonet))
        (erc-d-t-wait-for 3 "Final buffer name determined"
          (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (funcall expect 5 "foonet")))

    (ert-info ("Join #chan@foonet")
      (with-current-buffer erc-server-buffer-foo (erc-cmd-JOIN "#chan"))
      (erc-d-t-wait-for 5 "Buffer #chan created" (get-buffer "#chan"))
      (with-current-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 nil))
        (setq erc-server-process-bar erc-server-process)
        (erc-d-t-wait-for 3 "Network determined" (eq (erc-network) 'barnet))
        (erc-d-t-wait-for 3 "Final buffer name determined"
          (string= (buffer-name) (format "127.0.0.1:%d<2>" port)))
        (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 (equal (erc-scenarios-common-buflist "127.0.0.1")
                     (list (get-buffer (format "127.0.0.1:%d<2>" port))
                           (get-buffer (format "127.0.0.1:%d" port))))))

    (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@127.0.0.1:$port is exclusive to foonet")
      (with-current-buffer chan-buf-foo
        (erc-d-t-search-for 1 "<bob>")
        (erc-d-t-search-for -0.1 "<joe>")
        (should (eq erc-server-process erc-server-process-foo))
        (while (accept-process-output erc-server-process-foo))
        (erc-d-t-search-for 1 "ape is dead")
        (should-not (erc-server-process-alive))))

    (ert-info ("#chan@127.0.0.1:$port<2> is exclusive to barnet")
      (with-current-buffer chan-buf-bar
        (erc-d-t-search-for 1 "<joe>")
        (erc-d-t-search-for -0.1 "<bob>")
        (should (eq erc-server-process erc-server-process-bar))
        (while (accept-process-output erc-server-process-bar))
        (erc-d-t-search-for 1 "keeps you from dishonour")
        (should-not (erc-server-process-alive))))

    (when more (funcall more))))

(ert-deftest erc-scenarios-base-compat-no-rename-bouncer--basic ()
  (with-suppressed-warnings ((obsolete erc-rename-buffers))
    (let (erc-rename-buffers)
      (erc-scenarios-common--base-compat-no-rename-bouncer
       '(foonet barnet) nil nil))))

(ert-deftest erc-scenarios-base-compat-no-rename-bouncer--reconnect ()
  (let ((erc-d-spec-vars '((token . (group (| "barnet" "foonet")))))
        (erc-d-match-handlers
         (list :pass #'erc-scenarios--clash-rename-pass-handler))
        (dialogs '(foonet-drop barnet-drop stub-again stub-again
                               foonet-again barnet-again))
        (after
         (lambda ()
           (pcase-let* ((`(,barnet ,foonet)
                         (erc-scenarios-common-buflist "127.0.0.1"))
                        (port (process-contact (with-current-buffer foonet
                                                 erc-server-process)
                                               :service)))

             (ert-info ("Sanity check: barnet retains uniquifying suffix")
               (should (string-suffix-p "<2>" (buffer-name barnet))))

             ;; Simulate disconnection and `erc-server-auto-reconnect'
             (ert-info ("Reconnect to foonet and barnet back-to-back")
               (with-current-buffer foonet
                 (erc-d-t-wait-for 5 "ERC reconnected to foonet"
                   (erc-server-process-alive)))
               (with-current-buffer barnet
                 (erc-d-t-wait-for 5 "ERC reconnected to barnet"
                   (erc-server-process-alive))))

             (ert-info ("#chan@127.0.0.1:<port> is exclusive to foonet")
               (with-current-buffer  (format "#chan@127.0.0.1:%d" port)
                 (erc-d-t-search-for 1 "<alice>")
                 (erc-d-t-search-for -0.1 "<joe>")
                 (while (accept-process-output erc-server-process))
                 (erc-d-t-search-for 3 "please your lordship")))

             (ert-info ("#chan@barnet is exclusive to barnet")
               (with-current-buffer  (format "#chan@127.0.0.1:%d<2>" port)
                 (erc-d-t-search-for 1 "<joe>")
                 (erc-d-t-search-for -0.1 "<bob>")
                 (while (accept-process-output erc-server-process))
                 (erc-d-t-search-for 1 "much in private")))

             ;; Ordering deterministic here even though not so for reconnect
             (should (equal (list barnet foonet)
                            (erc-scenarios-common-buflist "127.0.0.1")))
             (should (equal (list
                             (get-buffer (format "#chan@127.0.0.1:%d<2>" port))
                             (get-buffer (format "#chan@127.0.0.1:%d" port)))
                            (erc-scenarios-common-buflist "#chan")))))))

    (with-suppressed-warnings ((obsolete erc-rename-buffers))
      (let (erc-rename-buffers)
        (erc-scenarios-common--base-compat-no-rename-bouncer dialogs
                                                             'auto after)))))

;; The added complexity of a request handler definitely stinks. But on
;; some machines, the ordering from the selector is nondeterministic,
;; whereas normally, the filter for the last process created (in the
;; code) gets all the initial attention. FIXME delete obsolete comment

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

(ert-deftest erc-scenarios-base-gapless-connect ()
  "Back-to-back entry-point invocations happen successfully.
Originally from scenario rebuffed/gapless 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/gapless-connect")
       (erc-server-flood-penalty 0.1)
       (erc-d-linger-secs 4)
       (erc-server-flood-penalty erc-server-flood-penalty)
       (erc-d-spec-vars '((token . (group (| "barnet" "foonet")))))
       (erc-d-match-handlers
        (list :pass #'erc-scenarios--rebuffed-gapless-pass-handler))
       (dumb-server (erc-d-run "localhost" t
                               'pass-stub 'pass-stub 'barnet 'foonet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       erc-autojoin-channels-alist
       erc-server-buffer-foo
       erc-server-buffer-bar)

    (ert-info ("Connect twice to same endpoint without pausing")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester")
            erc-server-buffer-bar (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "barnet:changeme"
                                       :full-name "tester")))

    (ert-info ("Returned server buffers are unique")
      (should-not (eq erc-server-buffer-foo erc-server-buffer-bar)))

    (ert-info ("Both connections still alive")
      (should (get-process (format "erc-127.0.0.1-%d" port)))
      (should (get-process (format "erc-127.0.0.1-%d<1>" port))))

    (with-current-buffer erc-server-buffer-bar
      (funcall expect 1 "marked as being away"))

    (with-current-buffer
        (erc-d-t-wait-for 20 "Buffer #bar exists" (get-buffer "#bar"))
      (while (accept-process-output erc-server-process))
      (funcall expect 2 "was created on")
      (funcall expect 2 "his second fit"))

    (with-current-buffer
        (erc-d-t-wait-for 20 "Buffer #foo exists" (get-buffer "#foo"))
      (while (accept-process-output erc-server-process))
      (funcall expect 2 "was created on")
      (funcall expect 2 "no use of him"))))

(defun erc-scenarios-common--base-reuse-buffers-server-buffers (&optional more)
  "Show that `erc-reuse-buffers' doesn't affect server buffers.
Overlaps some with `clash-of-chans/uniquify'.  Adapted from
rebuffed/reuseless, described in Bug#48598: 28.0.50; buffer-naming
collisions involving bouncers in ERC.  Run EXTRA."
  (erc-scenarios-common-with-cleanup
      ((erc-d-linger-secs 1)
       (dumb-server (erc-d-run "localhost" t 'foonet 'barnet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist)

    (ert-info ("Connect to foonet")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :password "foonet:changeme"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (erc-d-t-search-for 2 "marked as being away")))

    (ert-info ("Connect to barnet")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :password "barnet:changeme"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (erc-d-t-search-for 2 "marked as being away")))

    (erc-d-t-wait-for 2 "Buffer named foonet exists" (get-buffer "foonet"))
    (erc-d-t-wait-for 2 "Buffer named barnet exists" (get-buffer "barnet"))

    (ert-info ("Server buffers are unique, no IP-based names")
      (should-not (eq (get-buffer "foonet") (get-buffer "barnet")))
      (should-not (erc-scenarios-common-buflist "127.0.0.1")))

    (when more (funcall more))))

(ert-deftest erc-scenarios-base-reuse-buffers-server-buffers--enabled ()
  (should erc-reuse-buffers)
  (let ((erc-scenarios-common-dialog "base/reuse-buffers/server-buffers"))
    (erc-scenarios-common--base-reuse-buffers-server-buffers)))

(ert-deftest erc-scenarios-base-reuse-buffers-server-buffers--disabled ()
  (should erc-reuse-buffers)
  (let ((erc-scenarios-common-dialog "base/reuse-buffers/server-buffers")
        erc-reuse-buffers)
    (erc-scenarios-common--base-reuse-buffers-server-buffers)))

;; The server changes your nick just after registration.

(ert-deftest erc-scenarios-base-renick-self-auto ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/self")
       (erc-d-linger-secs 0.1)
       (erc-server-flood-penalty 0.1)
       (dumb-server (erc-d-run "localhost" t 'auto))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 3 "server for foonet renamed" (get-buffer "foonet"))

    (with-current-buffer "foonet"
      (erc-d-t-search-for 10 "Your new nickname is dummy"))

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (erc-d-t-wait-for 1 "#foo created" (get-buffer "#foo"))
      (with-current-buffer "#foo"
        (erc-d-t-search-for 10 "dummy")
        (erc-d-t-search-for 10 "On Thursday")))))

;; You change your nickname manually in a server buffer; a message is
;; printed in channel buffers.

(ert-deftest erc-scenarios-base-renick-self-manual ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/self")
       (erc-d-linger-secs 0.1)
       (erc-server-flood-penalty 0.1)
       (dumb-server (erc-d-run "localhost" t 'manual))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 3 "server for foonet renamed" (get-buffer "foonet"))

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (erc-d-t-wait-for 1 "#foo created" (get-buffer "#foo"))
      (with-current-buffer "#foo"
        (funcall expect 5 "tester")
        (funcall expect 5 "On Thursday")
        (erc-with-server-buffer (erc-cmd-NICK "dummy"))
        (funcall expect 5 "Your new nickname is dummy")
        (funcall expect 5 "<bob> dummy: Hi")))))

;; You connect to the same network with two different nicks.  You
;; manually change the first nick at some point, and buffer names are
;; updated correctly.

(ert-deftest erc-scenarios-base-renick-self-qualified ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/self")
       (dumb-server (erc-d-run "localhost" t 'qual-tester 'qual-chester))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.1)
       (erc-server-flood-margin 30)
       erc-serv-buf-a erc-serv-buf-b)

    (ert-info ("Connect to foonet with nick tester")
      (with-current-buffer
          (setq erc-serv-buf-a (erc :server "127.0.0.1"
                                    :port port
                                    :nick "tester"
                                    :password "changeme"
                                    :full-name "tester"))
        (erc-d-t-wait-for 5 "network determined" (eq erc-network 'foonet))))

    (ert-info ("Connect to foonet with nick chester")
      (with-current-buffer
          (setq erc-serv-buf-b (erc :server "127.0.0.1"
                                    :port port
                                    :nick "chester"
                                    :password "changeme"
                                    :full-name "chester"))))

    (erc-d-t-wait-for 3 "Dialed Buflist is Empty"
      (not (erc-scenarios-common-buflist "127.0.0.1")))

    (with-current-buffer  "foonet/tester"
      (funcall expect 3 "debug mode")
      (erc-cmd-JOIN "#chan"))

    (with-current-buffer  "foonet/chester"
      (funcall expect 3 "debug mode")
      (erc-cmd-JOIN "#chan"))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with tester"
      (get-buffer "#chan@foonet/tester"))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with chester"
      (get-buffer "#chan@foonet/chester"))

    (ert-info ("Greets other nick in same channel")
      (with-current-buffer "#chan@foonet/tester"
        (funcall expect 5 "<bob> chester, welcome!")
        (erc-cmd-NICK "dummy")
        (funcall expect 5 "Your new nickname is dummy")
        (funcall expect 5 "find the forester")
        (should (string= (buffer-name) "#chan@foonet/dummy"))))

    (ert-info ("Renick propagated throughout all buffers of process")
      (should-not (get-buffer "#chan@foonet/tester"))
      (should-not (get-buffer "foonet/tester"))
      (should (get-buffer "foonet/dummy")))))

;; When a channel user changes their nick, any query buffers for them
;; are updated.

(ert-deftest erc-scenarios-base-renick-queries-solo ()

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/queries")
       (erc-d-linger-secs 0.1)
       (erc-server-flood-penalty 0.1)
       (erc-server-flood-margin 20)
       (dumb-server (erc-d-run "localhost" t 'solo))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 1 "server for foonet renamed" (get-buffer "foonet"))

    (ert-info ("Joined by bouncer to #foo, pal persent")
      (with-current-buffer
          (erc-d-t-wait-for 1 "chan #foo exists" (get-buffer "#foo"))
        (erc-d-t-search-for 1 "On Thursday")
        (goto-char erc-input-marker)
        (insert "hi")
        (erc-send-current-line)))

    (erc-d-t-wait-for 10 "Query buffer appears with message from pal"
      (get-buffer "Lal"))

    (ert-info ("Chat with pal, who changes name")
      (with-current-buffer "Lal"
        (erc-d-t-search-for 3 "hello")
        (goto-char erc-input-marker)
        (insert "hi")
        (erc-send-current-line)
        (erc-d-t-search-for 10 "is now known as Linguo")
        (should-not (search-forward "is now known as Linguo" nil t))))

    (erc-d-t-wait-for 1 "Query buffer's name changes"
      (get-buffer "Linguo"))
    (should-not (get-buffer "Lal"))

    (with-current-buffer "Linguo"
      (goto-char erc-input-marker)
      (insert "howdy Linguo")
      (erc-send-current-line))

    (with-current-buffer "#foo"
      (erc-d-t-search-for 10 "is now known as Linguo")
      (should-not (search-forward "is now known as Linguo" nil t))
      (erc-cmd-PART ""))

    (with-current-buffer "Linguo"
      (erc-d-t-search-for 10 "get along"))))

;; You share a channel and a query buffer with a user on two different
;; networks (through a proxy).  The user changes their nick on both
;; networks at the same time.  Query buffers are updated accordingly.

(ert-deftest erc-scenarios-base-renick-queries-bouncer ()

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/queries")
       (erc-d-linger-secs 0.5)
       (erc-server-flood-penalty 0.1)
       (erc-server-flood-margin 30)
       (dumb-server (erc-d-run "localhost" t 'bouncer-foonet 'bouncer-barnet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       erc-accidental-paste-threshold-seconds
       erc-autojoin-channels-alist
       erc-server-buffer-foo
       erc-server-buffer-bar)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 1 "server for foonet renamed" (get-buffer "foonet"))

    (ert-info ("Connect to barnet")
      (setq erc-server-buffer-bar (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "barnet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-bar
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 1 "server for barnet renamed" (get-buffer "barnet"))
    (should-not (erc-scenarios-common-buflist "127.0.0.1"))

    (ert-info ("Joined by bouncer to #chan@foonet, pal persent")
      (erc-d-t-wait-for 10 "#chan@foonet exists" (get-buffer "#chan@foonet"))
      (with-current-buffer "#chan@foonet"
        (funcall expect 1 "rando")
        (funcall expect 1 "simply misused")))

    (ert-info ("Joined by bouncer to #chan@barnet, pal persent")
      (erc-d-t-wait-for 10 "#chan@barnet exists" (get-buffer "#chan@barnet"))
      (with-current-buffer "#chan@barnet"
        (funcall expect 1 "rando")
        (funcall expect 1 "come, sir, I am")))

    (ert-info ("Query buffer exists for rando@foonet")
      (erc-d-t-wait-for 10 "rando@foonet exists" (get-buffer "rando@foonet"))
      (with-current-buffer "rando@foonet"
        (funcall expect 1 "guess not")
        (goto-char erc-input-marker)
        (insert "I here")
        (erc-send-current-line)))

    (ert-info ("Query buffer exists for rando@barnet")
      (erc-d-t-wait-for 10 "rando@barnet exists" (get-buffer "rando@barnet"))
      (with-current-buffer "rando@barnet"
        (funcall expect 2 "rentacop")
        (goto-char erc-input-marker)
        (insert "Linda said you were gonna kill me.")
        (erc-send-current-line)))

    (ert-info ("Sync convo for rando@foonet")
      (with-current-buffer "rando@foonet"
        (funcall expect 1 "u are dumb")
        (goto-char erc-input-marker)
        (insert "not so")
        (erc-send-current-line)))

    (ert-info ("Sync convo for rando@barnet")
      (with-current-buffer "rando@barnet"
        (funcall expect 3 "I never saw her before")
        (goto-char erc-input-marker)
        (insert "You aren't with Wage?")
        (erc-send-current-line)))

    (erc-d-t-wait-for 1 "buffer name change for rando@foonet"
      (get-buffer "frenemy@foonet"))
    (erc-d-t-wait-for 1 "buffer name change for rando@barnet"
      (get-buffer "frenemy@barnet"))

    (should-not (get-buffer "rando@foonet"))
    (should-not (get-buffer "rando@barnet"))

    (with-current-buffer "frenemy@foonet"
      (funcall expect 1 "now known as")
      (funcall expect 1 "doubly so"))

    (with-current-buffer "frenemy@barnet"
      (funcall expect 1 "now known as")
      (funcall expect 1 "reality picture"))

    (when noninteractive
      (with-current-buffer "frenemy@barnet" (kill-buffer))
      (erc-d-t-wait-for 2 "frenemy@foonet changed back to frenemy"
        (get-buffer "frenemy"))
      (should-not (get-buffer "frenemy@foonet")))

    (with-current-buffer "#chan@foonet"
      (funcall expect 10 "is now known as frenemy")
      (should-not (search-forward "now known as frenemy" nil t)) ; regression
      (funcall expect 10 "words are razors"))

    (with-current-buffer "#chan@barnet"
      (funcall expect 10 "is now known as frenemy")
      (should-not (search-forward "now known as frenemy" nil t))
      (while (accept-process-output erc-server-process))
      (funcall expect 10 "I have lost"))))

(ert-deftest erc-scenarios-aux-unix-socket ()
  (skip-unless (featurep 'make-network-process '(:family local)))
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/renick/self")
       (erc-d-linger-secs 0.1)
       (erc-server-flood-penalty 0.1)
       (sock (expand-file-name "erc-d.sock" temporary-file-directory))
       (erc-scenarios-common-extra-teardown (lambda ()
                                              (delete-file sock)))
       (erc-server-connect-function
        (lambda (n b _ p &rest r)
          (apply #'make-network-process
                 `(:name ,n :buffer ,b :service ,p :family local ,@r))))
       (dumb-server (erc-d-run nil sock 'auto))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "fake"
                                       :port sock
                                       :nick "tester"
                                       :password "foonet:changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "fake:%s" sock)))))

    (erc-d-t-wait-for 3 "server for foonet renamed" (get-buffer "foonet"))

    (with-current-buffer "foonet"
      (erc-d-t-search-for 10 "Your new nickname is dummy"))

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (erc-d-t-wait-for 1 "#foo created" (get-buffer "#foo"))
      (with-current-buffer "#foo"
        (erc-d-t-search-for 10 "dummy")
        (erc-d-t-search-for 10 "On Thursday")))))

;; See `erc-update-server-buffer-name'.  A perceived loss in
;; network connectivity turns out to be a false alarm, but the
;; bouncer has already accepted the second connection

(defun erc-scenarios--base-aborted-reconnect ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/aborted-reconnect")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-d-linger-secs 0.5)
       (dumb-server (erc-d-run "localhost" t 'foonet 'foonet-dupe))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (ert-info ("Server buffer is unique and temp name is absent")
      (erc-d-t-wait-for 1 "server for foonet renamed" (get-buffer "FooNet"))
      (should-not (erc-scenarios-common-buflist "127.0.0.1"))
      (with-current-buffer erc-server-buffer-foo
        (erc-cmd-JOIN "#chan")))

    (ert-info ("Channel buffer #chan alive and well")
      (with-current-buffer
          (erc-d-t-wait-for 4 "#chan created" (get-buffer "#chan"))
        (erc-d-t-search-for 10 "welcome")))

    (ert-info ("Connect to foonet again")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "changeme"
                                       :full-name "tester"))
      (let ((inhibit-message noninteractive))
        (with-current-buffer erc-server-buffer-foo
          (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
          (erc-d-t-wait-for 5 "Second process killed"
            (not (erc-server-process-alive)))
          (erc-d-t-search-for 10 "FooNet still connected"))))

    (ert-info ("Server buffer is unique and temp name is absent")
      (should (equal (list (get-buffer "FooNet"))
                     (erc-scenarios-common-buflist "FooNet")))
      (should (equal (list (get-buffer (format "127.0.0.1:%d" port)))
                     (erc-scenarios-common-buflist "127.0.0.1"))))

    (ert-info ("Channel buffer #chan still going")
      (with-current-buffer "#chan"
        (erc-d-t-search-for 10 "and be prosperous")))))

(ert-deftest erc-scenarios-base-aborted-reconnect ()
  :tags '(:unstable)
  (let ((tries 3)
        (timeout 1)
        failed)
    (while (condition-case _err
               (progn
                 (erc-scenarios--base-aborted-reconnect)
                 nil)
             (ert-test-failed
              (message "Test %S failed; %s attempt(s) remaining."
                       (ert-test-name (ert-running-test))
                       tries)
              (sleep-for (cl-incf timeout))
              (not (setq failed (zerop (cl-decf tries)))))))
    (should-not failed)))

;; This defends against a regression in `erc-server-PRIVMSG' caused by
;; the removal of `erc-auto-query'.  When an active channel buffer is
;; killed off and PRIVMSGs arrive targeting it, the buffer should be
;; recreated.  See elsewhere for NOTICE logic, which is more complex.

(ert-deftest erc-scenarios-base-channel-buffer-revival ()

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/channel-buffer-revival")
       (erc-d-linger-secs 0.5)
       (dumb-server (erc-d-run "localhost" t 'foonet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (ert-info ("Server buffer is unique and temp name is absent")
      (erc-d-t-wait-for 1 "server for foonet renamed" (get-buffer "FooNet"))
      (should-not (erc-scenarios-common-buflist "127.0.0.1"))
      (with-current-buffer erc-server-buffer-foo
        (erc-cmd-JOIN "#chan")))

    (ert-info ("Channel buffer #chan alive and well")
      (with-current-buffer
          (erc-d-t-wait-for 8 "#chan exists" (get-buffer "#chan"))
        (erc-d-t-search-for 10 "Our queen and all her elves")
        (kill-buffer)))

    (should-not (get-buffer "#chan"))

    (ert-info ("Channel buffer #chan revived")
      (erc-d-t-wait-for 5 "#chan exists again" (get-buffer "#chan"))
      (with-current-buffer "#chan"
        (erc-d-t-search-for 10 "and be prosperous")))))

;; This ensures we only reconnect `erc-server-reconnect-attempts'
;; (rather than infinitely many) times, which can easily happen when
;; tweaking code related to process sentinels in erc-backend.el.

(ert-deftest erc-scenarios-base-reconnect-timer ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/reconnect-timer")
       (dumb-server (erc-d-run "localhost" t 'foonet 'foonet 'foonet-last))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       (erc-server-auto-reconnect t)
       erc-autojoin-channels-alist
       erc-server-buffer)

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

    (ert-info ("Server tries to connect thrice (including initial attempt)")
      (with-current-buffer erc-server-buffer
        (dotimes (n 3)
          (ert-info ((format "Attempt %d" n))
            (funcall expect 3 "Opening connection")
            (funcall expect 2 "Password incorrect")
            (funcall expect 2 "Connection failed!")
            (funcall expect 2 "Re-establishing connection")))
        (ert-info ("Prev attempt was final")
          (erc-d-t-search-for -1 "Opening connection" (point)))))

    (ert-info ("Server buffer is unique and temp name is absent")
      (should (equal (list (get-buffer (format "127.0.0.1:%d" port)))
                     (erc-scenarios-common-buflist "127.0.0.1"))))))

(cl-defun erc-scenarios-common--base-session-id-same-network
    ((&key nick id server chan
           &aux (nick-a nick) (id-a id) (serv-buf-a server) (chan-buf-a chan))
     (&key nick id server chan
           &aux (nick-b nick) (id-b id) (serv-buf-b server) (chan-buf-b chan)))
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/session-id/same-network")
       (dumb-server (erc-d-run "localhost" t 'tester 'chester))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.1)
       (erc-server-flood-margin 30)
       erc-serv-buf-a erc-serv-buf-b)

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

    (ert-info ("Connect to foonet with nick chester")
      (with-current-buffer
          (setq erc-serv-buf-b (erc :server "127.0.0.1"
                                    :port port
                                    :nick nick-b
                                    :password "changeme"
                                    :full-name nick-b
                                    :id id-b))
        (erc-scenarios-common-assert-initial-buf-name id-b port)))

    (erc-d-t-wait-for 3 "Dialed Buflist is Empty"
      (not (erc-scenarios-common-buflist "127.0.0.1")))

    (with-current-buffer erc-serv-buf-a
      (should (string= (buffer-name) serv-buf-a))
      (funcall expect 3 "debug mode")
      (erc-cmd-JOIN "#chan"))

    (with-current-buffer erc-serv-buf-b
      (should (string= (buffer-name) serv-buf-b))
      (funcall expect 3 "debug mode")
      (erc-cmd-JOIN "#chan"))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with tester"
      (get-buffer chan-buf-a))

    (erc-d-t-wait-for 10 "Channel buffer appears suffixed with chester"
      (get-buffer chan-buf-b))

    (ert-info ("Greets other nick in same channel")
      (with-current-buffer chan-buf-a
        (funcall expect 5 "chester")
        (funcall expect 5 "find the forester")
        (erc-cmd-MSG "#chan chester: hi")))

    (ert-info ("Sees other nick in same channel")
      (with-current-buffer chan-buf-b
        (funcall expect 5 "tester")
        (funcall expect 10 "<tester> chester: hi")
        (funcall expect 5 "This was lofty")
        (erc-cmd-MSG "#chan hi tester")))

    (with-current-buffer chan-buf-a
      (funcall expect 5 "To employ you towards")
      (erc-cmd-QUIT ""))

    (with-current-buffer chan-buf-b
      (funcall expect 5 "To employ you towards")
      (erc-cmd-QUIT ""))))

(ert-deftest erc-scenarios-base-session-id-same-network--two-ids ()
  (erc-scenarios-common--base-session-id-same-network
   (list :nick "tester"
         :id 'tester/foonet
         :server "tester/foonet"
         :chan "#chan@tester/foonet")
   (list :nick "chester"
         :id 'chester/foonet
         :server "chester/foonet"
         :chan "#chan@chester/foonet")))

(ert-deftest erc-scenarios-base-session-id-same-network--one-id-tester ()
  (erc-scenarios-common--base-session-id-same-network
   (list :nick "tester"
         :id 'tester/foonet
         :server "tester/foonet"
         :chan "#chan@tester/foonet")
   (list :nick "chester"
         :id nil
         :server "foonet"
         :chan "#chan@foonet")))

(ert-deftest erc-scenarios-base-session-id-same-network--one-id-chester ()
  (erc-scenarios-common--base-session-id-same-network
   (list :nick "tester"
         :id nil
         :server "foonet"
         :chan "#chan@foonet")
   (list :nick "chester"
         :id 'chester/foonet
         :server "chester/foonet"
         :chan "#chan@chester/foonet")))

(ert-deftest erc-scenarios-base-session-id-same-network--no-ids ()
  (erc-scenarios-common--base-session-id-same-network
   (list :nick "tester"
         :id nil
         :server "foonet/tester"
         :chan "#chan@foonet/tester") ; <- note net before nick
   (list :nick "chester"
         :id nil
         :server "foonet/chester"
         :chan "#chan@foonet/chester")))

;; Upon reconnecting, playback for channel and target buffers is
;; routed correctly.

(ert-deftest erc-scenarios-base-association-reconnect-playback ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/reconnect-playback")
       (erc-d-linger-secs 0.5)
       (erc-server-flood-penalty 0.1)
       (erc-server-flood-margin 30)
       (dumb-server (erc-d-run "localhost" t 'foonet 'foonet-again))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter))
       erc-autojoin-channels-alist
       erc-server-buffer-foo)

    (ert-info ("Connect to foonet")
      (setq erc-server-buffer-foo (erc :server "127.0.0.1"
                                       :port port
                                       :nick "tester"
                                       :password "changeme"
                                       :full-name "tester"))
      (with-current-buffer erc-server-buffer-foo
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (ert-info ("Setup")

      (ert-info ("Server buffer is unique and temp name is absent")
        (erc-d-t-wait-for 1 "server for foonet renamed" (get-buffer "foonet"))
        (should-not (erc-scenarios-common-buflist "127.0.0.1")))

      (ert-info ("Channel buffer #chan playback received")
        (erc-d-t-wait-for 3 "#chan exists" (get-buffer "#chan"))
        (with-current-buffer "#chan"
          (funcall expect 10 "But purgatory")))

      (ert-info ("Ask for help from services or bouncer bot")
        (with-current-buffer erc-server-buffer-foo
          (erc-cmd-MSG "*status help")))

      (ert-info ("Help received")
        (erc-d-t-wait-for 5 "*status buffer exists" (get-buffer "*status"))
        (with-current-buffer "*status"
          (funcall expect 10 "Rehash")))

      (ert-info ("#chan convo done")
        (with-current-buffer "#chan"
          (funcall expect 10 "most egregious indignity"))))

    (with-current-buffer erc-server-buffer-foo
      (erc-cmd-QUIT "")
      (erc-d-t-wait-for 4 "proc dies" (not (erc-server-process-alive)))
      (erc-cmd-RECONNECT))

    (ert-info ("Channel buffer found and associated")
      (with-current-buffer "#chan"
        (funcall expect 10 "Wilt thou rest damned")))

    (ert-info ("Help buffer found and associated")
      (with-current-buffer "*status"
        (goto-char erc-input-marker)
        (insert "help")
        (erc-send-current-line)
        (funcall expect 10 "Restart ZNC")))

    (ert-info ("#chan convo done")
      (with-current-buffer "#chan"
        (funcall expect 10 "here comes the lady")))))

;; You register a new nick, disconnect, and log back in, but your nick
;; is not granted, so ERC obtains a backtick'd version.  You open a
;; query buffer for NickServ, and ERC names it using the session ID
;; (which includes the backtick'd nick) as a suffix.  The original
;; (disconnected) NickServ buffer gets renamed with *its* session ID
;; as well.  You then identify to NickServ, and the dead session is no
;; longer considered distinct.

(ert-deftest erc-scenarios-base-association-nick-bumped ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/nick-bump")
       (dumb-server (erc-d-run "localhost" t 'renicked 'renicked-again))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.5)
       (erc-server-flood-margin 30))

    (ert-info ("Connect to foonet with nick tester")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :full-name "tester")
        (erc-scenarios-common-assert-initial-buf-name nil port)
        (erc-d-t-wait-for 5 "network determined" (eq erc-network 'foonet))))

    (ert-info ("Create an account for tester and quit")
      (with-current-buffer "foonet"
        (funcall expect 3 "debug mode")

        (erc-cmd-QUERY "NickServ")
        (with-current-buffer "NickServ"
          (erc-send-input-line "NickServ" "REGISTER changeme")
          (funcall expect 5 "Account created")
          (funcall expect 1 "You're now logged in as tester"))

        (with-current-buffer "foonet"
          (erc-cmd-QUIT "")
          (erc-d-t-wait-for 4 "proc dies" (not (erc-server-process-alive)))
          (funcall expect 5 "ERC finished"))))

    (with-current-buffer "foonet"
      (erc-cmd-RECONNECT))

    (erc-d-t-wait-for 10 "Nick request rejection prevents reassociation (good)"
      (get-buffer "foonet/tester`"))

    (ert-info ("Ask NickServ to change nick")
      (with-current-buffer "foonet/tester`"
        (funcall expect 3 "already in use")
        (funcall expect 3 "debug mode")
        (erc-cmd-QUERY "NickServ"))

      (erc-d-t-wait-for 1 "Dead NickServ query buffer renamed now qualified"
        (get-buffer "NickServ@foonet/tester"))

      (with-current-buffer "NickServ@foonet/tester`" ; new one
        (erc-send-input-line "NickServ" "IDENTIFY tester changeme")
        (funcall expect 5 "You're now logged in as tester")
        (should (equal (buffer-name) "NickServ@foonet/tester"))))

    (ert-info ("Ours is the only NickServ buffer that remains")
      (should-not (cdr (erc-scenarios-common-buflist "NickServ"))))

    (should (not (get-buffer "foonet/tester`")))))

;; Ensure the old way of specifying a partial domain name still works.

(ert-deftest erc-scenarios-base-legacy-autojoin ()

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/legacy-autojoin")
       (erc-d-linger-secs 1)
       (erc-server-flood-penalty 0.1)
       (dumb-server (erc-d-run "localhost" t 'foonet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (erc-autojoin-channels-alist '(("libera\\.chat" "#erc")
                                      ("foonet\\.org" "#chan"))))

    (ert-info ("Connect")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :password "changeme"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))))

    (erc-d-t-wait-for 1 "server buffer renamed" (get-buffer "FooNet"))

    (ert-info ("Channel buffer #chan autojoined")
      (with-current-buffer
          (erc-d-t-wait-for 6 "#chan exists" (get-buffer "#chan"))
        (erc-d-t-search-for 10 "Live, and be prosperous")))))

(ert-deftest erc-scenarios-services-password ()

  (defvar erc-nickserv-passwords)

  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "services/password")
       (erc-server-flood-penalty 0.1)
       (erc-modules (cons 'services erc-modules))
       (erc-nickserv-passwords '((Libera.Chat (("joe" . "bar")
                                               ("tester" . "changeme")))))
       (expect (erc-d-t-make-expecter))
       (dumb-server (erc-d-run "localhost" t 'libera))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*")))

    (ert-info ("Connect without password")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (erc-d-t-wait-for 2 "Network determined"
          (eq erc-network 'Libera.Chat))
        (funcall expect 1 "This nickname is registered.")
        (funcall expect 1 "You are now identified")
        (funcall expect 1 "Last login from")
        (erc-cmd-QUIT "")))

    (erc-services-mode -1)

    (should-not (memq 'services erc-modules))))

(ert-deftest erc-scenarios-services-prompt ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "services/password")
       (erc-server-flood-penalty 0.1)
       (erc-modules (cons 'services erc-modules))
       (expect (erc-d-t-make-expecter))
       (dumb-server (erc-d-run "localhost" t 'libera))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*")))

    (ert-info ("Connect without password")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (ert-simulate-keys "changeme\r"
          (erc-d-t-wait-for 2 "Network determined"
            (eq erc-network 'Libera.Chat))
          (funcall expect 3 "This nickname is registered.")
          (funcall expect 3 "You are now identified")
          (funcall expect 3 "Last login from"))
        (erc-cmd-QUIT "")))

    (erc-services-mode -1)

    (should-not (memq 'services erc-modules))))

;; Corner case demoing fallback behavior for an absent 004 RPL but a
;; present 422 or 375.  If this is unlikely enough, remove or guard
;; with `ert-skip' plus some condition so it only runs when explicitly
;; named via ERT specifier

(ert-deftest erc-scenarios-networks-announced-missing ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "networks/announced-missing")
       (erc-d-linger-secs 0.5)
       (expect (erc-d-t-make-expecter))
       (dumb-server (erc-d-run "localhost" t 'foonet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*")))

    (ert-info ("Connect without password")
      (with-current-buffer (erc :server "127.0.0.1"
                                :port port
                                :nick "tester"
                                :full-name "tester")
        (should (string= (buffer-name) (format "127.0.0.1:%d" port)))
        (let ((err (should-error (sleep-for 1))))
          (should (string-match-p "Failed to determine" (cadr err))))
        (funcall expect 1 "Failed to determine")
        (funcall expect 1 "Failed to determine")
        (should-not erc-network)
        (should (string= erc-server-announced-name "irc.foonet.org"))))))

;; Targets that are host/server masks like $*, $$*, and #* are routed
;; to the server buffer: https://github.com/ircdocs/wooooms/issues/5

(ert-deftest erc-scenarios-base-mask-target-routing ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/mask-target-routing")
       (erc-d-linger-secs 0.5)
       (dumb-server (erc-d-run "localhost" t 'foonet))
       (port (process-contact dumb-server :service))
       (dumb-server-buffer (get-buffer "*erc-d-server*"))
       (expect (erc-d-t-make-expecter)))

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

    (erc-d-t-wait-for 1 "server buffer ready" (get-buffer "foonet"))

    (ert-info ("Channel buffer #foo playback received")
      (erc-d-t-wait-for 3 "#foo exists" (get-buffer "#foo"))
      (with-current-buffer "#foo"
        (funcall expect 10 "Excellent workman")))

    (ert-info ("Global notices routed to server buffer")
      (with-current-buffer "foonet"
        (funcall expect 10 "going down soon")
        (funcall expect 10 "this is a warning")
        (funcall expect 10 "second warning")
        (funcall expect 10 "final warning")))

    (should-not (get-buffer "$*"))))

;;; erc-scenarios.el ends here

debug log:

solving 1d2f988338 ...
found 1d2f988338 in https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
found 0e2928b9a1 in https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
found f7931b35b9 in https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
found 19d4a33fdd in https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/

applying [1/4] https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
new file mode 100644
index 0000000000..19d4a33fdd


applying [2/4] https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
index 19d4a33fdd..f7931b35b9 100644


applying [3/4] https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
index f7931b35b9..0e2928b9a1 100644


applying [4/4] https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
index 0e2928b9a1..1d2f988338 100644

Checking patch test/lisp/erc/erc-scenarios.el...
Applied patch test/lisp/erc/erc-scenarios.el cleanly.
Checking patch test/lisp/erc/erc-scenarios.el...
Applied patch test/lisp/erc/erc-scenarios.el cleanly.
Checking patch test/lisp/erc/erc-scenarios.el...
Applied patch test/lisp/erc/erc-scenarios.el cleanly.
Checking patch test/lisp/erc/erc-scenarios.el...
Applied patch test/lisp/erc/erc-scenarios.el cleanly.

index at:
100644 1d2f9883381c5dfe82c4f6bf708ff4a1455bb83b	test/lisp/erc/erc-scenarios.el

(*) 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).