all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 37b58227e7d82a629c542786d61bccd9b12acab3 88884 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
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
 
;;; 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)
(eval-when-compile (require 'erc-services))

(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 (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 (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 (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-absent-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 (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 (get-buffer "#chan@foonet/tester"))
    (with-current-buffer "foonet/chester" (funcall expect 3 "debug mode"))
    (erc-d-t-wait-for 10 (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 (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 (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 (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.
;;
;; This *does* check that superfluous JOINs sent by the autojoin
;; module are harmless when they're not acked (superfluous because the
;; bouncer/server intitates the JOIN).

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

    (should (memq 'autojoin erc-modules))

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

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

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

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

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

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

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

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

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

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

    (ert-info ("#chan@foonet is clean, no cross-contamination")
      (with-current-buffer chan-buf-foo
        (erc-d-t-wait-for 3 (eq erc-server-process erc-server-process-foo))
        (funcall expect 3 "<bob>")
        (erc-d-t-absent-for 0.1 "<joe>")
        (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-join-network-id--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--join-network-id connect nil nil)))

(ert-deftest erc-scenarios-join-network-id--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--join-network-id connect 'oofnet nil)))

(ert-deftest erc-scenarios-join-network-id--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--join-network-id connect 'oofnet 'rabnet)))

(ert-deftest erc-scenarios-join-network-id--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--join-network-id connect nil nil)))

(ert-deftest erc-scenarios-join-network-id--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--join-network-id connect 'oofnet nil)))

(ert-deftest erc-scenarios-join-network-id--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--join-network-id connect 'oofnet 'rabnet)))

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

(ert-deftest erc-scenarios-base-legacy-autojoin--announced ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "join/legacy")
       (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))
       (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 (get-buffer "FooNet"))

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

(ert-deftest erc-scenarios-join-reconnect ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "join/reconnect")
       (dumb-server (erc-d-run "localhost" t 'foonet 'foonet-again))
       (port (process-contact dumb-server :service))
       (expect (erc-d-t-make-expecter))
       (erc-server-flood-penalty 0.1)
       (erc-server-auto-reconnect t)
       erc-autojoin-channels-alist
       erc-server-buffer)

    (should (memq 'autojoin erc-modules))

    (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)))
        (funcall expect 1 "debug mode")))

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

    (should (equal erc-autojoin-channels-alist '((FooNet "#spam" "#chan"))))

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

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

;; 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))
       (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 (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 (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-absent-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-absent-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-network-id-bouncer
    ((&key autop foo-id bar-id after
           &aux
           (foo-id (and foo-id 'oofnet))
           (bar-id (and bar-id 'rabnet))
           (serv-buf-foo (if foo-id "oofnet" "foonet"))
           (serv-buf-bar (if bar-id "rabnet" "barnet"))
           (chan-buf-foo (if foo-id "#chan@oofnet" "#chan@foonet"))
           (chan-buf-bar (if bar-id "#chan@rabnet" "#chan@barnet")))
     &rest dialogs)
  "Ensure retired option `erc-rename-buffers' is now the default behavior.
The option `erc-rename-buffers' is now deprecated and on by default, so
this now just asserts baseline behavior.  Originally from scenario
clash-of-chans/rename-buffers as explained in Bug#48598: 28.0.50;
buffer-naming collisions involving bouncers in ERC."
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/network-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))
       (expect (erc-d-t-make-expecter))
       (erc-server-auto-reconnect autop)
       erc-server-buffer-foo erc-server-process-foo
       erc-server-buffer-bar erc-server-process-bar)

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

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

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

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

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

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

    (ert-info ("#chan@<esid> is exclusive to foonet")
      (with-current-buffer chan-buf-foo
        (erc-d-t-search-for 1 "<bob>")
        (erc-d-t-absent-for 0.1 "<joe>")
        (should (eq erc-server-process erc-server-process-foo))
        (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-absent-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-network-id-bouncer--base ()
  (erc-scenarios-common--base-network-id-bouncer () 'foonet 'barnet))

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

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

(ert-deftest erc-scenarios-base-network-id-bouncer--both ()
  (erc-scenarios-common--base-network-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-network-id-bouncer--reconnect (foo-id bar-id)
  (let ((erc-d-tmpl-vars '((token . (group (| "barnet" "foonet")))))
        (erc-d-match-handlers
         ;; Auto reconnect is nondeterministic, so let computer decide
         (list :pass #'erc-scenarios--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-server-process-alive)))
             (with-current-buffer (if bar-id "rabnet" "barnet")
               (erc-d-t-wait-for 5 (erc-server-process-alive))))

           (ert-info ("#chan@foonet is exclusive to foonet")
             (with-current-buffer (if foo-id "#chan@oofnet" "#chan@foonet")
               (erc-d-t-search-for 1 "<alice>")
               (erc-d-t-absent-for 0.1 "<joe>")
               (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-absent-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-network-id-bouncer
     (list :autop t :foo-id foo-id :bar-id bar-id :after after)
     'foonet-drop 'barnet-drop
     'stub-again 'stub-again
     'foonet-again 'barnet-again)))

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

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

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

(ert-deftest erc-scenarios-base-network-id-bouncer--reconnect-both ()
  (erc-scenarios-common--base-network-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-)network-id related because
      ;; our kludge assigns one after the fact.
      ((erc-scenarios-common-dialog "base/network-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))
       (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 (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"))
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan"))
        (funcall expect 5 "<alice>")))

    (ert-info ("Connect to barnet")
      (with-current-buffer
          (setq erc-server-buffer-bar (erc :server "127.0.0.1"
                                           :port port
                                           :nick "tester"
                                           :password "barnet:changeme"
                                           :full-name "tester"
                                           :id nil))
        (setq erc-server-process-bar erc-server-process)
        (erc-d-t-wait-for 3 (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-absent-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-absent-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-tmpl-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-server-process-alive)))
               (with-current-buffer barnet
                 (erc-d-t-wait-for 5 (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-absent-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-absent-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-tmpl-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))
       (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 2 "marked as being away"))

    (with-current-buffer (erc-d-t-wait-for 20 (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 (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))
       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 (get-buffer "foonet"))
    (erc-d-t-wait-for 2 (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))
       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)))))

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

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (with-current-buffer (erc-d-t-wait-for 1 (get-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))
       (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 (get-buffer "foonet"))

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (with-current-buffer (erc-d-t-wait-for 1 (get-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")
        ;; Regression in which changing a nick would trigger #foo@foonet
        (erc-d-t-ensure-for 0.4 (equal (buffer-name) "#foo"))))))

;; 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))
       (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 (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 (get-buffer "#chan@foonet/tester"))
    (erc-d-t-wait-for 10 (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")
        (erc-d-t-wait-for 5 (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))
       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 (get-buffer "foonet"))

    (ert-info ("Joined by bouncer to #foo, pal persent")
      (with-current-buffer (erc-d-t-wait-for 1 (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 (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))
       (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 (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 (get-buffer "barnet"))
    (should-not (erc-scenarios-common-buflist "127.0.0.1"))

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

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

    (ert-info ("Query buffer exists for rando@foonet")
      (with-current-buffer (erc-d-t-wait-for 10 (get-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")
      (with-current-buffer (erc-d-t-wait-for 10 (get-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 (get-buffer "frenemy@foonet"))
    (erc-d-t-wait-for 1 (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 (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))
       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)))))

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

    (ert-info ("Joined by bouncer to #foo, own nick present")
      (with-current-buffer (erc-d-t-wait-for 1 (get-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/reconnect")
       (erc-d-t-cleanup-sleep-secs 1)
       (erc-d-linger-secs 0.5)
       (dumb-server (erc-d-run "localhost" t 'aborted 'aborted-dupe))
       (port (process-contact dumb-server :service))
       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 (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 (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 (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))
       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 (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 (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")
      (with-current-buffer (erc-d-t-wait-for 5 (get-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")
       (dumb-server (erc-d-run "localhost" t 'timer 'timer 'timer-last))
       (port (process-contact dumb-server :service))
       (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-absent-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"))))))

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

    (should (memq 'autojoin erc-modules))

    (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)))
        (funcall expect 1 "debug mode")))

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

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

    (should (equal erc-autojoin-channels-alist '((FooNet "#chan"))))

    (funcall test)

    (with-current-buffer "FooNet" (erc-cmd-JOIN "#spam"))

    (erc-d-t-wait-for 5 "Channel #spam shown when autojoined"
      (eq (window-buffer) (get-buffer "#spam")))

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

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

(ert-deftest erc-scenarios-base-reconnect-options--default ()
  (should (eq erc-join-buffer 'buffer))
  (should-not erc-reconnect-display)

  ;; FooNet (the server buffer) is not switched to because it's
  ;; already current (but not shown) when `erc-open' is called.  See
  ;; related conditional guard towards the end of that function.

  (erc-scenarios-common--base-reconnect-options
   (lambda ()
     (pop-to-buffer-same-window "*Messages*")

     (erc-d-t-ensure-for 1 "Server buffer not shown"
       (not (eq (window-buffer) (get-buffer "FooNet"))))

     (erc-d-t-wait-for 5 "Channel #chan shown when autojoined"
       (eq (window-buffer) (get-buffer "#chan"))))))

(ert-deftest erc-scenarios-base-reconnect-options--bury ()
  (should (eq erc-join-buffer 'buffer))
  (should-not erc-reconnect-display)

  (let ((erc-reconnect-display 'bury))
    (erc-scenarios-common--base-reconnect-options

     (lambda ()
       (pop-to-buffer-same-window "*Messages*")

       (erc-d-t-ensure-for 1 "Server buffer not shown"
         (not (eq (window-buffer) (get-buffer "FooNet"))))

       (erc-d-t-ensure-for 3 "Channel #chan not shown"
         (not (eq (window-buffer) (get-buffer "#chan"))))

       (eq (window-buffer) (messages-buffer))))))

(cl-defun erc-scenarios-common--base-network-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/network-id/same-network")
       (dumb-server (erc-d-run "localhost" t 'tester 'chester))
       (port (process-contact dumb-server :service))
       (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 (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 (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 (get-buffer chan-buf-a))
    (erc-d-t-wait-for 10 (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-network-id-same-network--two-ids ()
  (erc-scenarios-common--base-network-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-network-id-same-network--one-id-tester ()
  (erc-scenarios-common--base-network-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-network-id-same-network--one-id-chester ()
  (erc-scenarios-common--base-network-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-network-id-same-network--no-ids ()
  (erc-scenarios-common--base-network-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.  Autojoin is irrelevant here, but for the
;; skeptical, see `erc-scenarios-common--join-network-id', which
;; overlaps with this and includes spurious JOINs ignored by the
;; server.

(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))
       (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 (get-buffer "foonet"))
        (should-not (erc-scenarios-common-buflist "127.0.0.1")))

      (ert-info ("Channel buffer #chan playback received")
        (with-current-buffer (erc-d-t-wait-for 3 (get-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")
        (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "*status"))
          (funcall expect 10 "Rehash")))

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

    ;; KLUDGE (see note above test)
    (should erc-autojoin-channels-alist)
    (setq erc-autojoin-channels-alist nil)

    (with-current-buffer erc-server-buffer-foo
      (erc-cmd-QUIT "")
      (erc-d-t-wait-for 4 (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 (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 (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")
        (ert-info ("Original buffer found, reused")
          (erc-d-t-wait-for 2 (equal (buffer-name) "NickServ")))))

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

    (ert-info ("Visible network ID truncated to one component")
      (should (not (get-buffer "foonet/tester`")))
      (should (not (get-buffer "foonet/tester")))
      (should (get-buffer "foonet")))))

;; A less common variant is when your bouncer switches to an alternate
;; nick while you're disconnected, and upon reconnecting, you get
;; a new nick.

(ert-deftest erc-scenarios-base-association-nick-bumped-mandated-renick ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/association/nick-bump")
       (dumb-server (erc-d-run "localhost" t
                               'renicked-foisted 'renicked-foisted-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 (eq erc-network 'foonet))))

    (ert-info ("Greet bob and quit")
      (with-current-buffer "foonet"
        (funcall expect 3 "debug mode")

        (erc-cmd-QUERY "bob")
        (with-current-buffer "bob"
          (erc-send-input-line "bob" "hi")
          (funcall expect 5 "hola")
          (funcall expect 1 "how r u?"))

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

    ;; Since we use reconnect, a new buffer won't be created
    ;; TODO add variant with clean `erc' invocation
    (with-current-buffer "foonet"
      (erc-cmd-RECONNECT))

    (ert-info ("Server-initiated renick")
      (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "foonet/dummy"))
        (should-not (get-buffer "foonet/tester"))
        (funcall expect 5 "debug mode"))

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

      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "bob@foonet/dummy"))
        (erc-cmd-NICK "tester")
        (ert-info ("Buffers combined")
          (erc-d-t-wait-for 2 (equal (buffer-name) "bob")))))

    (with-current-buffer "foonet"
      (funcall expect 5 "You're now logged in as tester"))

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

    (ert-info ("Visible network ID truncated to one component")
      (should (not (get-buffer "foonet/dummy")))
      (should (get-buffer "foonet")))))

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

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

    (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 (eq erc-network 'Libera.Chat))
        (funcall expect 1 "This nickname is registered.")
        (funcall expect 2 "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)
       (inhibit-interaction nil)
       (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)))

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

(ert-deftest erc-scenarios-base-flood ()
  (erc-scenarios-common-with-cleanup
      ((erc-scenarios-common-dialog "base/flood")
       (erc-d-linger-secs 0.5)
       (dumb-server (erc-d-run "localhost" t 'soju))
       (port (process-contact dumb-server :service))
       (erc-server-flood-penalty 0.5) ; this ratio MUST match
       (erc-server-flood-margin 1.5) ;  the default of 3:10
       (expect (erc-d-t-make-expecter))
       erc-autojoin-channels-alist)

    (ert-info ("Connect to bouncer")
      (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)))
        (funcall expect 5 "Soju")))

    (ert-info ("#chan@foonet exists")
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan/foonet"))
        (erc-d-t-search-for 2 "<bob/foonet>")
        (erc-d-t-absent-for 0.1 "<joe")
        (funcall expect 3 "was created on")))

    (ert-info ("#chan@barnet exists")
      (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan/barnet"))
        (erc-d-t-search-for 2 "<joe/barnet>")
        (erc-d-t-absent-for 0.1 "<bob")
        (funcall expect 3 "was created on")
        (funcall expect 5 "To get good guard")))

    (ert-info ("Message not held in queue limbo")
      (with-current-buffer "#chan/foonet"
        ;; Without 'no-penalty param in `erc-server-send', should fail
        ;; after ~10 secs with:
        ;;
        ;;   (erc-d-timeout "Timed out awaiting request: (:name ~privmsg
        ;;    :pattern \\`PRIVMSG #chan/foonet :alice: hi :timeout 2
        ;;    :dialog soju)")
        ;;
        ;; Try reversing commit and spying on queue interactively
        (erc-cmd-MSG "#chan/foonet alice: hi")
        (funcall expect 5 "tester: Good, very good")))

    (ert-info ("All output sent")
      (with-current-buffer "#chan/foonet"
        (funcall expect 5 "Some man or other"))
      (with-current-buffer "#chan/barnet"
        (while (accept-process-output erc-server-process))
        (funcall expect 5 "That's he that was Othello")))))

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

    (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))
       (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 (get-buffer "foonet"))

    (ert-info ("Channel buffer #foo playback received")
      (with-current-buffer (erc-d-t-wait-for 3 (get-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 37b58227e7 ...
found 37b58227e7 in https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/
found ba2abe732d in https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/
found 3d0d41b482 in https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/

applying [1/3] https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
new file mode 100644
index 0000000000..3d0d41b482


applying [2/3] https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
index 3d0d41b482..ba2abe732d 100644


applying [3/3] https://yhetil.org/emacs/87leweez89.fsf__368.691612833061$1649538949$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-scenarios.el b/test/lisp/erc/erc-scenarios.el
index ba2abe732d..37b58227e7 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.

index at:
100644 37b58227e7d82a629c542786d61bccd9b12acab3	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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.