all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob fc12bf7ce370861dd3bc109fcb7a7bd71b54bdd2 73537 bytes (raw)
name: test/lisp/erc/erc-networks-tests.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
 
;;; erc-networks-tests.el --- Tests for erc-networks.  -*- lexical-binding:t -*-

;; Copyright (C) 2020-2022 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert-x) ; cl-lib
(require 'erc)

(defun erc-networks-tests--create-dead-proc (&optional buf)
  (let ((p (start-process "true" (or buf (current-buffer)) "true")))
    (while (process-live-p p) (sit-for 0.1))
    p))

(defun erc-networks-tests--create-live-proc (&optional buf)
  (let ((proc (start-process "sleep" (or buf (current-buffer)) "sleep" "1")))
    (set-process-query-on-exit-flag proc nil)
    proc))

;; When we drop 27, call `get-buffer-create with INHIBIT-BUFFER-HOOKS.
(defun erc-networks-tests--clean-bufs ()
  (let (erc-kill-channel-hook
        erc-kill-server-hook
        erc-kill-buffer-hook)
    (dolist (buf (erc-buffer-list))
      (kill-buffer buf))))

(defun erc-networks-tests--bufnames (prefix)
  (let* ((case-fold-search)
         (pred (lambda (b) (string-prefix-p prefix (buffer-name b))))
         (prefixed (seq-filter pred (buffer-list))))
    (sort (mapcar #'buffer-name prefixed) #'string<)))

(ert-deftest erc-networks--id ()
  (cl-letf (((symbol-function 'float-time)
             (lambda (&optional _) 0.0)))

    ;; Fixed
    (should (equal (erc-networks--id-fixed-create 'foo)
                   (make-erc-networks--id-fixed :ts (float-time)
                                                :symbol 'foo)))

    ;; Eliding
    (let* ((erc-network 'FooNet)
           (erc-server-current-nick "Joe")
           (identity (erc-networks--id-create nil)))

      (should (equal identity #s(erc-networks--id-qualifying
                                 0.0 FooNet [FooNet "joe"] 1)))
      (should (equal (erc-networks--id-qualifying-grow-id identity)
                     'FooNet/joe))
      (should (equal identity #s(erc-networks--id-qualifying
                                 0.0 FooNet/joe [FooNet "joe"] 2)))
      (should-not (erc-networks--id-qualifying-grow-id identity))
      (should (equal identity #s(erc-networks--id-qualifying
                                 0.0 FooNet/joe [FooNet "joe"] 2))))

    ;; Compat
    (with-current-buffer (get-buffer-create "fake.chat")
      (with-suppressed-warnings ((obsolete erc-rename-buffers))
        (let (erc-rename-buffers)
          (should (equal (erc-networks--id-create nil)
                         (make-erc-networks--id-fixed :ts (float-time)
                                                      :symbol 'fake.chat)))))
      (kill-buffer))))

(ert-deftest erc-networks--id-create ()
  (cl-letf (((symbol-function 'float-time)
             (lambda (&optional _) 0.0)))

    (should (equal (erc-networks--id-create 'foo)
                   (make-erc-networks--id-fixed :ts (float-time)
                                                :symbol 'foo)))
    (should (equal (erc-networks--id-create "foo")
                   (make-erc-networks--id-fixed :ts (float-time)
                                                :symbol 'foo)))
    (should (equal (erc-networks--id-create [h i])
                   (make-erc-networks--id-fixed :ts (float-time)
                                                :symbol (quote \[h\ \i\]))))

    (with-current-buffer (get-buffer-create "foo")
      (let ((expected (make-erc-networks--id-fixed :ts (float-time)
                                                   :symbol 'foo)))
        (with-suppressed-warnings ((obsolete erc-rename-buffers))
          (let (erc-rename-buffers)
            (should (equal (erc-networks--id-create nil) expected))))
        (with-suppressed-warnings ((obsolete erc-reuse-buffers))
          (let (erc-reuse-buffers)
            (should (equal (erc-networks--id-create nil) expected))
            (should (equal (erc-networks--id-create 'bar) expected)))))
      (kill-buffer))))

(ert-deftest erc-networks--id-qualifying-prefix-length ()
  (should-not (erc-networks--id-qualifying-prefix-length
               (make-erc-networks--id-qualifying)
               (make-erc-networks--id-qualifying)))

  (should-not (erc-networks--id-qualifying-prefix-length
               (make-erc-networks--id-qualifying :parts [1 2])
               (make-erc-networks--id-qualifying :parts [2 3])))

  (should (= 1 (erc-networks--id-qualifying-prefix-length
                (make-erc-networks--id-qualifying :parts [1])
                (make-erc-networks--id-qualifying :parts [1 2]))))

  (should (= 1 (erc-networks--id-qualifying-prefix-length
                (make-erc-networks--id-qualifying :parts [1 2])
                (make-erc-networks--id-qualifying :parts [1 3]))))

  (should (= 2 (erc-networks--id-qualifying-prefix-length
                (make-erc-networks--id-qualifying :parts [1 2])
                (make-erc-networks--id-qualifying :parts [1 2]))))

  (should (= 1 (erc-networks--id-qualifying-prefix-length
                (make-erc-networks--id-qualifying :parts ["1"])
                (make-erc-networks--id-qualifying :parts ["1"])))))

(ert-deftest erc-networks--id-sort-buffers ()
  (let (oldest middle newest)

    (with-temp-buffer
      (setq erc-networks--id (erc-networks--id-fixed-create 'oldest)
            oldest (current-buffer))

      (with-temp-buffer
        (setq erc-networks--id (erc-networks--id-fixed-create 'middle)
              middle (current-buffer))

        (with-temp-buffer
          (setq erc-networks--id (erc-networks--id-fixed-create 'newest)
                newest (current-buffer))

          (should (equal (erc-networks--id-sort-buffers
                          (list oldest newest middle))
                         (list newest middle oldest))))))))

(ert-deftest erc-networks-rename-surviving-target-buffer--channel ()
  (should (memq #'erc-networks-rename-surviving-target-buffer
                erc-kill-channel-hook))

  (let ((chan-foonet-buffer (get-buffer-create "#chan@foonet")))

    (with-current-buffer chan-foonet-buffer
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [foonet "bob"] :len 1)
            erc--target (erc--target-from-string "#chan")))

    (with-current-buffer (get-buffer-create "#chan@barnet")
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [barnet "bob"] :len 1)
            erc--target (erc--target-from-string "#chan")))

    (kill-buffer "#chan@barnet")
    (should (equal (erc-networks-tests--bufnames "#chan") '("#chan")))
    (should (eq chan-foonet-buffer (get-buffer "#chan"))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks-rename-surviving-target-buffer--query ()
  (should (memq #'erc-networks-rename-surviving-target-buffer
                erc-kill-buffer-hook))

  (let ((bob-foonet (get-buffer-create "bob@foonet")))

    (with-current-buffer bob-foonet
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [foonet "bob"] :len 1)
            erc--target (erc--target-from-string "bob")))

    (with-current-buffer (get-buffer-create "bob@barnet")
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [barnet "bob"] :len 1)
            erc--target (erc--target-from-string "bob")))

    (kill-buffer "bob@barnet")
    (should (equal (erc-networks-tests--bufnames "bob") '("bob")))
    (should (eq bob-foonet (get-buffer "bob"))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks-rename-surviving-target-buffer--multi ()

  (ert-info ("Multiple leftover channels untouched")
    (with-current-buffer (get-buffer-create "#chan@foonet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "#chan")))
    (with-current-buffer (get-buffer-create "#chan@barnet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "#chan")))
    (with-current-buffer (get-buffer-create "#chan@baznet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "#chan")))
    (kill-buffer "#chan@baznet")
    (should (equal (erc-networks-tests--bufnames "#chan")
                   '("#chan@barnet" "#chan@foonet")))
    (erc-networks-tests--clean-bufs))

  (ert-info ("Multiple leftover queries untouched")
    (with-current-buffer (get-buffer-create "bob@foonet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "bob")))
    (with-current-buffer (get-buffer-create "bob@barnet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "bob")))
    (with-current-buffer (get-buffer-create "bob@baznet")
      (erc-mode)
      (setq erc--target (erc--target-from-string "bob")))
    (kill-buffer "bob@baznet")
    (should (equal (erc-networks-tests--bufnames "bob")
                   '("bob@barnet" "bob@foonet")))
    (erc-networks-tests--clean-bufs)))

;; As of May 2022, this "shrink" stuff runs whenever an ERC buffer is
;; killed because `erc-networks-shrink-ids-and-buffer-names' is a
;; default member of all three erc-kill-* functions.

;; Note: this overlaps a fair bit with the "hook" variants, i.e.,
;; `erc-networks--shrink-ids-and-buffer-names--hook-outstanding-*' If
;; this ever fails, just delete this and fix those.  But please copy
;; over and adapt the comments first.

(ert-deftest erc-networks--shrink-ids-and-buffer-names--perform-outstanding ()
  ;; While some buffer #a@barnet/dummy is being killed, its display ID
  ;; is not collapsed because collisions still exist.
  ;;
  ;; Note that we don't have to set `erc-server-connected' because
  ;; this function is intentionally connectivity agnostic.
  (with-current-buffer (get-buffer-create "foonet/tester")
    (erc-mode)
    (setq erc-server-current-nick "tester" ; Always set (`erc-open')
          ;; Set when transport connected
          erc-server-process (erc-networks-tests--create-live-proc)
          ;; Both set just before IRC (logically) connected (post MOTD)
          erc-network 'foonet
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/tester
                            :parts [foonet "tester"]
                            :len 2))) ; is/was a plain foonet collision

  ;; Presumably, some server buffer named foonet/dummy was just
  ;; killed, hence the length 2 display ID.

  ;; A target buffer for chan #a exists for foonet/tester.  The
  ;; precise form of its name should not affect shrinking.
  (with-current-buffer (get-buffer-create
                        (elt ["#a" "#a@foonet" "#a@foonet/tester"] (random 3)))
    (erc-mode)
    (setq erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet/tester"))
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "foonet/tester"))
          erc--target (erc--target-from-string "#a")))

  ;; Another network context exists (so we have buffers to iterate
  ;; over), and it's also part of a collision group.
  (with-current-buffer (get-buffer-create "barnet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/tester
                            :parts [barnet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "barnet/dummy")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "dummy"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/dummy
                            :parts [barnet "dummy"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  ;; The buffer being killed is not part of the foonet collision
  ;; group, which contains one display ID eligible for shrinkage.
  (with-current-buffer (get-buffer-create
                        (elt ["#a@barnet" "#a@barnet/tester"] (random 2)))
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "barnet/tester"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "barnet/tester"))
          erc--target (erc--target-from-string "#a")))

  (with-temp-buffer ; doesn't matter what the current buffer is
    (setq erc-networks--id (make-erc-networks--id-qualifying)) ; mock
    (erc-networks--shrink-ids-and-buffer-names))

  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet" ; shrunk
                   "#a@foonet" ; shrunk
                   "barnet/tester"
                   "barnet/dummy"
                   "#a@barnet/tester")))

  (erc-networks-tests--clean-bufs))

;; This likewise overlaps with the "hook" variants below.  If this
;; should ever fail, just delete it and optionally fix those.

(ert-deftest erc-networks--shrink-ids-and-buffer-names--perform-collapse ()
  ;; This is similar to the "outstanding" variant above, but both
  ;; groups are eligible for renaming, which is abnormal but possible
  ;; when recovering from some mishap.
  (with-current-buffer (get-buffer-create "foonet/tester")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/tester
                            :parts [foonet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer
      (get-buffer-create (elt ["#a" "#a@foonet/tester"] (random 2)))
    (erc-mode)
    (setq erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet/tester"))
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "foonet/tester"))
          erc--target (erc--target-from-string "#a")))

  (with-current-buffer (get-buffer-create "barnet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/tester
                            :parts [barnet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer
      (get-buffer-create (elt ["#b" "#b@foonet/tester"] (random 2)))
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "barnet/tester"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "barnet/tester"))
          erc--target (erc--target-from-string "#b")))

  (with-temp-buffer
    (setq erc-networks--id (make-erc-networks--id-qualifying))
    (erc-networks--shrink-ids-and-buffer-names))

  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet" "#a" "barnet" "#b")))

  (erc-networks-tests--clean-bufs))

(defun erc-networks--shrink-ids-and-buffer-names--hook-outstanding-common ()

  (with-current-buffer (get-buffer-create "foonet/tester")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/tester
                            :parts [foonet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "#a@foonet/tester")
    (erc-mode)
    (setq erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet/tester"))
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "foonet/tester"))
          erc--target (erc--target-from-string "#a")))

  (with-current-buffer (get-buffer-create "barnet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/tester
                            :parts [barnet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "barnet/dummy")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "dummy"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/dummy
                            :parts [barnet "dummy"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "#a@barnet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "barnet/tester"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "barnet/tester"))
          erc--target (erc--target-from-string "#a"))))

(ert-deftest erc-networks--shrink-ids-and-buffer-names--hook-outstanding-srv ()
  (erc-networks--shrink-ids-and-buffer-names--hook-outstanding-common)
  (with-current-buffer (get-buffer-create "foonet/dummy")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "dummy"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/dummy
                            :parts [foonet "dummy"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc))
    (kill-buffer))

  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet"
                   "#a@foonet"
                   "barnet/tester"
                   "barnet/dummy"
                   "#a@barnet/tester")))
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--shrink-ids-and-buffer-names--hook-outstanding-tgt ()
  (erc-networks--shrink-ids-and-buffer-names--hook-outstanding-common)
  (with-current-buffer (get-buffer-create "#a@foonet/dummy")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "dummy"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/dummy
                            :parts [foonet "dummy"]
                            :len 2)
          erc--target (erc--target-from-string "#a")
          erc-server-process (with-temp-buffer
                               (erc-networks-tests--create-dead-proc))))

  (with-current-buffer "#a@foonet/dummy" (kill-buffer))

  ;; Identical to *-server variant above
  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet"
                   "#a@foonet"
                   "barnet/tester"
                   "barnet/dummy"
                   "#a@barnet/tester")))
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks-rename-surviving-target-buffer--shrink ()
  (erc-networks--shrink-ids-and-buffer-names--hook-outstanding-common)

  ;; This buffer isn't "#a@foonet" (yet) because the shrink-ids hook
  ;; hasn't run.  However, when it's the rename hook runs, its network
  ;; id *is* "foonet", not "foonet/tester".
  (with-current-buffer "#a@foonet/tester" (kill-buffer))

  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet"
                   "barnet/tester"
                   "barnet/dummy"
                   "#a")))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--shrink-ids-and-buffer-names--server ()

  (with-current-buffer (get-buffer-create "foonet/tester")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/tester
                            :parts [foonet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "foonet/dummy")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "dummy"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/dummy
                            :parts [foonet "dummy"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-dead-proc))
    (kill-buffer))

  (should (equal (mapcar #'buffer-name (erc-buffer-list)) '("foonet")))

  (erc-networks-tests--clean-bufs))

(defun erc-networks--shrink-ids-and-buffer-names--hook-collapse (check)

  (with-current-buffer (get-buffer-create "foonet/tester")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/tester
                            :parts [foonet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "#a@foonet/tester")
    (erc-mode)
    (setq erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet/tester"))
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "foonet/tester"))
          erc--target (erc--target-from-string "#a")))

  (with-current-buffer (get-buffer-create "barnet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'barnet/tester
                            :parts [barnet "tester"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create  "#b@foonet/tester")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "barnet/tester"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "barnet/tester"))
          erc--target (erc--target-from-string "#b")))

  (funcall check)

  (should (equal (mapcar #'buffer-name (erc-buffer-list))
                 '("foonet" "#a" "barnet" "#b")))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--shrink-ids-and-buffer-names--hook-collapse-server ()
  (erc-networks--shrink-ids-and-buffer-names--hook-collapse
   (lambda ()
     (with-current-buffer (get-buffer-create "foonet/dummy")
       (erc-mode)
       (setq erc-network 'foonet
             erc-server-current-nick "dummy"
             erc-networks--id (make-erc-networks--id-qualifying
                               :symbol 'foonet/dummy
                               :parts [foonet "dummy"]
                               :len 2)
             erc-server-process (erc-networks-tests--create-live-proc))
       (kill-buffer)))))

(ert-deftest erc-networks--shrink-ids-and-buffer-names--hook-collapse-target ()
  (erc-networks--shrink-ids-and-buffer-names--hook-collapse
   (lambda ()
     (with-current-buffer (get-buffer-create "#a@foonet/dummy")
       (erc-mode)
       (setq erc-network 'foonet
             erc-server-current-nick "dummy"
             erc-networks--id (make-erc-networks--id-qualifying
                               :symbol 'foonet/dummy
                               :parts [foonet "dummy"]
                               :len 2)
             ;; `erc-kill-buffer-function' uses legacy target detection
             ;; but falls back on buffer name, so no need for:
             ;;
             ;;   erc-default-recipients '("#a")
             ;;
             erc--target (erc--target-from-string "#a")
             erc-server-process (with-temp-buffer
                                  (erc-networks-tests--create-dead-proc)))
       (kill-buffer)))))

;; FIXME this test is old and may describe impossible states:
;; leftover identities being qual-equal but not eq (implies
;; `erc-networks--reclaim-orphaned-target-buffers' is somehow broken).
;;
;; Otherwise, the point of this test is to show that server process
;; identity does not impact the hunt for duplicates.

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--duplicates (start)

  (with-current-buffer (get-buffer-create "foonet")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create nil)
          erc-server-process (funcall start)))

  (with-current-buffer (get-buffer-create "#chan") ; prior session
    (erc-mode)
    (setq erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet"))
          erc--target (erc--target-from-string "#chan")
          erc-networks--id (erc-networks--id-create nil)))

  (ert-info ("Conflicts not recognized as ERC buffers and not renamed")
    (get-buffer-create "#chan@foonet")
    (should (equal (erc-networks-tests--bufnames "#chan")
                   '("#chan" "#chan@foonet"))))

  ;; These are dupes (not "collisions")

  (with-current-buffer "#chan@foonet" ; same proc
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan")
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet"))
          erc-networks--id (erc-networks--id-create nil)))

  (with-current-buffer (get-buffer-create "#chan@foonet<dead>")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan")
          erc-server-process (erc-networks-tests--create-dead-proc)
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create nil)))

  (with-current-buffer (get-buffer-create "#chan@foonet<live>")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan")
          erc-server-process (erc-networks-tests--create-live-proc)
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create nil)))

  (let ((created (list (get-buffer "#chan@foonet<live>")
                       (get-buffer "#chan@foonet<dead>")
                       (get-buffer "#chan@foonet"))))

    (with-current-buffer "foonet"
      (should (string= (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)
                       "#chan")))

    (ert-info ("All buffers considered dupes renamed")
      (should (equal (erc-networks-tests--bufnames "#chan")
                     '("#chan" "#chan<2>" "#chan<3>" "#chan<4>"))))

    (ert-info ("All buffers renamed from newest to oldest")
      (should (equal created (list (get-buffer "#chan<2>")
                                   (get-buffer "#chan<3>")
                                   (get-buffer "#chan<4>"))))))

  (erc-networks-tests--clean-bufs))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--dupes-given (go)

  ;; The connection's network is discovered before target buffers are
  ;; created.  This shows that the network doesn't matter when only
  ;; "given" IDs are present.
  (with-current-buffer (get-buffer-create "oofnet")
    (erc-mode)
    (setq erc-networks--id (erc-networks--id-create 'oofnet)
          erc-network 'foonet
          erc-server-current-nick "tester"
          erc-server-process (funcall go)))

  (with-current-buffer (get-buffer-create "#chan") ; prior session
    (erc-mode)
    (setq erc-networks--id (erc-networks--id-create 'oofnet)
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "oofnet"))
          erc--target (erc--target-from-string "#chan")))

  (with-current-buffer (get-buffer-create "#chan@oofnet") ;dupe/not collision
    (erc-mode)
    (setq erc-networks--id (erc-networks--id-create 'oofnet)
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "oofnet"))
          erc--target (erc--target-from-string "#chan")))

  (with-current-buffer "oofnet"
    (should (string= (erc-networks--reconcile-buffer-names
                      (erc--target-from-string "#chan") erc-networks--id)
                     "#chan")))

  (ert-info ("All buffers matching target and network renamed")
    (should (equal (erc-networks-tests--bufnames "#chan")
                   '("#chan" "#chan<2>"))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--reconcile-buffer-names--duplicates ()
  (ert-info ("Process live, no error")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--duplicates
     #'erc-networks-tests--create-live-proc))

  (ert-info ("Process live, no error, given ID")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--dupes-given
     #'erc-networks-tests--create-live-proc))

  (ert-info ("Process dead")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--duplicates
     #'erc-networks-tests--create-dead-proc))

  (ert-info ("Process dead, given ID")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--dupes-given
     #'erc-networks-tests--create-dead-proc)))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf (check)
  (let ((foonet-proc (with-temp-buffer
                       (erc-networks-tests--create-dead-proc))))
    (with-current-buffer (get-buffer-create "barnet")
      (erc-mode)
      (setq erc-network 'barnet
            erc-server-current-nick "tester"
            erc-networks--id (erc-networks--id-create nil)
            erc-server-process (erc-networks-tests--create-dead-proc)))

    ;; Different proc and not "qual-equal" (different elts)
    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'foonet
            erc-server-current-nick "tester"
            erc-networks--id (erc-networks--id-create nil)
            erc--target (erc--target-from-string "#chan")
            erc-server-process foonet-proc))
    (funcall check)
    (erc-networks-tests--clean-bufs)))

(ert-deftest erc-networks--reconcile-buffer-names--no-server-buf ()
  (ert-info ("Existing #chan buffer respected")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf
     (lambda ()
       (with-current-buffer "barnet"
         (should (string= (erc-networks--reconcile-buffer-names
                           (erc--target-from-string "#chan") erc-networks--id)
                          "#chan@barnet")))
       (ert-info ("Existing #chan buffer found and renamed")
         (should (equal (erc-networks-tests--bufnames "#chan")
                        '("#chan@foonet")))))))

  (ert-info ("Existing #chan buffer")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf
     (lambda ()
       (with-current-buffer (get-buffer-create "foonet")
         (erc-mode)
         (setq erc-network 'foonet
               erc-server-current-nick "tester"
               erc-networks--id (erc-networks--id-create nil)
               erc-server-process (erc-networks-tests--create-dead-proc))
         (should (string= (erc-networks--reconcile-buffer-names
                           (erc--target-from-string "#chan") erc-networks--id)
                          "#chan")))
       (ert-info ("Nothing renamed")
         (should (equal (erc-networks-tests--bufnames "#chan") '("#chan")))))))

  (ert-info ("Existing #chan@foonet and #chan@barnet buffers")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf
     (lambda ()
       (with-current-buffer "#chan"
         (rename-buffer "#chan@foonet"))
       (should-not (get-buffer "#chan@barnet"))
       (with-current-buffer (get-buffer-create "#chan@barnet")
         (erc-mode)
         (setq erc--target (erc--target-from-string "#chan")
               erc-server-process (buffer-local-value 'erc-server-process
                                                      (get-buffer "barnet"))
               erc-networks--id (erc-networks--id-create nil)))
       (with-current-buffer (get-buffer-create "foonet")
         (erc-mode)
         (setq erc-network 'foonet
               erc-server-current-nick "tester"
               erc-server-process (erc-networks-tests--create-live-proc)
               erc-networks--id (erc-networks--id-create nil))
         (set-process-query-on-exit-flag erc-server-process nil)
         (should (string= (erc-networks--reconcile-buffer-names
                           (erc--target-from-string "#chan") erc-networks--id)
                          "#chan@foonet")))
       (ert-info ("Nothing renamed")
         (should (equal (erc-networks-tests--bufnames "#chan")
                        '("#chan@barnet" "#chan@foonet"))))))))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf-given
    (check)
  (let ((oofnet-proc (with-temp-buffer
                       (erc-networks-tests--create-dead-proc))))

    (with-current-buffer (get-buffer-create "rabnet")
      (erc-mode)
      ;; Again, given name preempts network lookup (unrealistic but
      ;; highlights priorities)
      (setq erc-networks--id (erc-networks--id-create 'rabnet)
            erc-network 'barnet
            erc-server-current-nick "tester"
            erc-server-process (erc-networks-tests--create-dead-proc)))

    ;; Identity is not "qual-equal" to above
    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-networks--id (erc-networks--id-create 'oofnet)
            erc-network 'foonet
            erc--target (erc--target-from-string "#chan")
            erc-server-process oofnet-proc))
    (funcall check)
    (erc-networks-tests--clean-bufs)))

(ert-deftest erc-networks--reconcile-buffer-names--no-server-buf-given ()

  (ert-info ("Existing #chan buffer respected")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf-given
     (lambda ()
       (with-current-buffer "rabnet"
         (should (string= (erc-networks--reconcile-buffer-names
                           (erc--target-from-string "#chan") erc-networks--id)
                          "#chan@rabnet")))

       (ert-info ("Existing #chan buffer found and renamed")
         (should (equal (erc-networks-tests--bufnames "#chan")
                        '("#chan@oofnet")))))))

  (ert-info ("Existing #chan@oofnet and #chan@rabnet buffers")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf-given
     (lambda ()
       ;; #chan has already been uniquified (but not grown)
       (with-current-buffer "#chan" (rename-buffer "#chan@oofnet"))
       (should-not (get-buffer "#chan@rabnet"))

       (with-current-buffer (get-buffer-create "#chan@rabnet")
         (erc-mode)
         (setq erc--target (erc--target-from-string "#chan")
               erc-server-process (buffer-local-value 'erc-server-process
                                                      (get-buffer "rabnet"))
               erc-networks--id (buffer-local-value 'erc-networks--id
                                                    (get-buffer "rabnet"))))

       (with-current-buffer (get-buffer-create "oofnet")
         (erc-mode)
         (setq erc-network 'oofnet
               erc-server-current-nick "tester"
               erc-server-process (erc-networks-tests--create-live-proc)
               erc-networks--id (erc-networks--id-create 'oofnet)) ; given
         (set-process-query-on-exit-flag erc-server-process nil)
         (should (string= (erc-networks--reconcile-buffer-names
                           (erc--target-from-string "#chan") erc-networks--id)
                          "#chan@oofnet")))

       (ert-info ("Nothing renamed")
         (should (equal (erc-networks-tests--bufnames "#chan")
                        '("#chan@oofnet" "#chan@rabnet"))))))))

;; This shows a corner case where a user explicitly assigns a "given"
;; ID via `erc-tls' but later connects again without one.  It would
;; actually probably be better if the given identity were to win and
;; the derived one got an <n>-suffix.
;;
;; If we just compared net identities, the two would match, but they
;; don't here because one has a given name and the other a
;; discovered/assembled one; so they are *not* qual-equal.
(ert-deftest erc-networks--reconcile-buffer-names--no-srv-buf-given-mismatch ()
  ;; Existing #chan buffer *not* respected
  (erc-tests--prep-erc-networks--reconcile-buffer-names--no-srv-buf-given
   (lambda ()
     (with-current-buffer (get-buffer-create "oofnet")
       (erc-mode)
       (setq erc-network 'oofnet
             erc-server-current-nick "tester"
             erc-server-process (erc-networks-tests--create-dead-proc)
             erc-networks--id (erc-networks--id-create nil)) ; derived
       (should (string= (erc-networks--reconcile-buffer-names
                         (erc--target-from-string "#chan") erc-networks--id)
                        "#chan@oofnet")))

     (ert-info ("Collision renamed but not grown (because it's a given)")
       ;; Original chan uniquified and moved out of the way
       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@oofnet<2>")))))))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net (check)

  (with-current-buffer (get-buffer-create "foonet")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-server-process (erc-networks-tests--create-dead-proc)
          erc-networks--id (erc-networks--id-create nil))) ; derived

  (with-current-buffer (get-buffer-create "barnet")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-server-process (erc-networks-tests--create-dead-proc)
          erc-networks--id (erc-networks--id-create nil))) ; derived

  (with-current-buffer
      (get-buffer-create (elt ["#chan" "#chan@foonet"] (random 2)))
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "foonet"
        (list erc-server-process erc-networks--id))))

  (with-current-buffer (get-buffer-create "#chan@barnet")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "barnet"
        (list erc-server-process erc-networks--id))))

  (funcall check)
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--reconcile-buffer-names--multi-net ()
  (ert-info ("Same network rename")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net
     (lambda ()
       (with-current-buffer "foonet"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@foonet"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@barnet" "#chan@foonet"))))))

  (ert-info ("Same network keep name")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net
     (lambda ()
       (with-current-buffer "barnet"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@barnet"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@barnet" "#chan@foonet")))))))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-given
    (check)

  (with-current-buffer (get-buffer-create "oofnet")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create 'oofnet) ; one given
          erc-server-process (erc-networks-tests--create-dead-proc)))

  (with-current-buffer (get-buffer-create "rabnet")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create 'rabnet) ; another given
          erc-server-process (erc-networks-tests--create-dead-proc)))

  (with-current-buffer (get-buffer-create (elt ["chan" "#chan@oofnet"]
                                               (random 2)))
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "oofnet"
        (list erc-server-process erc-networks--id))))

  (with-current-buffer (get-buffer-create "#chan@barnet")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "rabnet"
        (list erc-server-process erc-networks--id))))

  (funcall check)
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--reconcile-buffer-names--multi-net-given ()
  (ert-info ("Same network rename")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-given
     (lambda ()
       (with-current-buffer "oofnet"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@oofnet"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@oofnet" "#chan@rabnet"))))))

  (ert-info ("Same network keep name")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-given
     (lambda ()
       (with-current-buffer "rabnet"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@rabnet"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@oofnet" "#chan@rabnet")))))))

(defun erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-mixed
    (check)

  (with-current-buffer (get-buffer-create "foonet")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create nil) ; one derived
          erc-server-process (erc-networks-tests--create-dead-proc)))

  (with-current-buffer (get-buffer-create "my-conn")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick "tester"
          erc-networks--id (erc-networks--id-create 'my-conn) ; one given
          erc-server-process (erc-networks-tests--create-dead-proc)))

  (with-current-buffer (get-buffer-create (elt ["#chan" "#chan@foonet"]
                                               (random 2)))
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "foonet"
        (list erc-server-process erc-networks--id))))

  (with-current-buffer (get-buffer-create "#chan@my-conn")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan"))
    (cl-multiple-value-setq (erc-server-process erc-networks--id)
      (with-current-buffer "my-conn"
        (list erc-server-process erc-networks--id))))

  (funcall check)
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--reconcile-buffer-names--multi-net-existing ()

  (ert-info ("Buf name derived from network")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-mixed
     (lambda ()
       (with-current-buffer "foonet"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@foonet"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@foonet" "#chan@my-conn"))))))

  (ert-info ("Buf name given")
    (erc-tests--prep-erc-networks--reconcile-buffer-names--multi-net-mixed
     (lambda ()
       (with-current-buffer "my-conn"
         (let ((result (erc-networks--reconcile-buffer-names
                        (erc--target-from-string "#chan") erc-networks--id)))
           (should (string= result "#chan@my-conn"))))

       (should (equal (erc-networks-tests--bufnames "#chan")
                      '("#chan@foonet" "#chan@my-conn")))))))

(ert-deftest erc-networks--reconcile-buffer-names--multi-net-suffixed ()
  ;; Two networks, same channel.  One network has two connections.
  ;; When the same channel is joined on the latter under a different
  ;; nick, all buffer names involving that network are suffixed with
  ;; the network identity.

  (with-current-buffer (get-buffer-create "foonet/bob")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "bob"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/bob
                            :parts [foonet "bob"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create
                        (elt ["#chan@foonet" "#chan@foonet/bob"] (random 2)))
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan")
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "foonet/bob"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "foonet/bob"))))

  (with-current-buffer (get-buffer-create "barnet")
    (erc-mode)
    (setq erc-network 'barnet
          erc-server-current-nick (elt ["alice" "bob"] (random 2))
          erc-networks--id (erc-networks--id-create 'barnet)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer (get-buffer-create "#chan@barnet")
    (erc-mode)
    (setq erc--target (erc--target-from-string "#chan")
          erc-server-process (buffer-local-value 'erc-server-process
                                                 (get-buffer "barnet"))
          erc-networks--id (buffer-local-value 'erc-networks--id
                                               (get-buffer "barnet"))))

  (with-current-buffer (get-buffer-create "foonet/alice")
    (erc-mode)
    (setq erc-network 'foonet
          erc-server-current-nick "alice"
          erc-networks--id (make-erc-networks--id-qualifying
                            :symbol 'foonet/alice
                            :parts [foonet "alice"]
                            :len 2)
          erc-server-process (erc-networks-tests--create-live-proc)))

  (with-current-buffer "foonet/alice"
    (let ((result (erc-networks--reconcile-buffer-names
                   (erc--target-from-string "#chan") erc-networks--id)))
      (should (string= result "#chan@foonet/alice"))))

  (should (equal (erc-networks-tests--bufnames "#chan")
                 '("#chan@barnet" "#chan@foonet/bob")))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--reconcile-buffer-names--local ()
  (with-current-buffer (get-buffer-create "DALnet")
    (erc-mode)
    (setq erc-network 'DALnet
          erc-server-announced-name "elysium.ga.us.dal.net"
          erc-server-process (erc-networks-tests--create-dead-proc)
          erc--isupport-params (make-hash-table)
          erc-networks--id (erc-networks--id-create nil))
    (puthash 'CHANTYPES '("&#") erc--isupport-params))

  (ert-info ("Local chan buffer from older, disconnected identity")
    (with-current-buffer (get-buffer-create "&chan")
      (erc-mode)
      ;; Cheat here because localp is determined on identity init
      (setq erc--target (with-current-buffer "DALnet"
                          (erc--target-from-string "&chan"))
            erc-network 'DALnet
            erc-server-announced-name "twisted.ma.us.dal.net"
            erc-server-process (erc-networks-tests--create-dead-proc)
            erc-networks--id (erc-networks--id-create nil))))

  (ert-info ("Local channels renamed using network server names")
    (with-current-buffer "DALnet"
      (let ((result (erc-networks--reconcile-buffer-names
                     (erc--target-from-string "&chan") erc-networks--id)))
        (should (string= result "&chan@elysium.ga.us.dal.net")))))

  (should (get-buffer "&chan@twisted.ma.us.dal.net"))
  (should-not (get-buffer "&chan"))
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--set-name ()
  (with-current-buffer (get-buffer-create "localhost:6667")
    (let (erc-server-announced-name
          (erc--isupport-params (make-hash-table))
          erc-network
          calls)
      (erc-mode)

      (cl-letf (((symbol-function 'erc-display-line)
                 (lambda (&rest r) (push r calls))))

        (ert-info ("Signals when `erc-server-announced-name' unset")
          (should-error (erc-networks--set-name nil (make-erc-response)))
          (should-not calls))

        (ert-info ("Signals when table empty and NETWORK param unset")
          (setq erc-server-announced-name "irc.fake.gnu.org")
          (let ((err (should-error (erc-networks--set-name
                                    nil (make-erc-response)))))
            (should (string-match-p "failed" (cadr err)))
            (should (eq (car err) 'error)))
          (should (string-match-p (rx "*** Failed") (car (pop calls)))))))

    (erc-networks-tests--clean-bufs)))

(ert-deftest erc-networks--ensure-announced ()
  (with-current-buffer (get-buffer-create "localhost:6667")
    (should (local-variable-if-set-p 'erc-server-announced-name))
    (let (erc-insert-modify-hook
          (erc-server-process (erc-networks-tests--create-live-proc))
          (parsed (make-erc-response
                   :unparsed ":irc.barnet.org 422 tester :MOTD File is missing"
                   :sender "irc.barnet.org"
                   :command "422"
                   :command-args '("tester" "MOTD File is missing")
                   :contents "MOTD File is missing")))

      (erc-mode) ; boilerplate displayable start (needs `erc-server-process')
      (insert "\n\n")
      (setq erc-input-marker (make-marker) erc-insert-marker (make-marker))
      (set-marker erc-insert-marker (point-max))
      (erc-display-prompt) ; boilerplate displayable end

      (erc-networks--ensure-announced erc-server-process parsed)
      (goto-char (point-min))
      (search-forward "Failed")
      (should (string= erc-server-announced-name "irc.barnet.org")))
    (when noninteractive (kill-buffer))))

(ert-deftest erc-networks--rename-server-buffer--no-existing--orphan ()
  (with-current-buffer (get-buffer-create "#chan")
    (erc-mode)
    (setq erc-network 'FooNet
          erc-server-current-nick "tester"
          erc--target (erc--target-from-string "#chan")
          erc-networks--id (erc-networks--id-create nil)))

  (with-current-buffer (get-buffer-create "irc.foonet.org")
    (erc-mode)
    (setq erc-network 'FooNet
          erc-server-current-nick "tester"
          erc-server-process (erc-networks-tests--create-live-proc)
          erc-networks--id (erc-networks--id-create nil))
    (should-not (erc-networks--rename-server-buffer erc-server-process))
    (should (string= (buffer-name) "FooNet")))

  (ert-info ("Channel buffer reassociated")
    (erc-server-process-alive "#chan")
    (with-current-buffer "#chan"
      (should erc-server-connected)
      (erc-with-server-buffer
        (should (string= (buffer-name) "FooNet")))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--existing--reuse ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-networks-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc-networks--id (erc-networks--id-create nil)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-networks--id (erc-networks--id-create nil)
            erc--target (erc--target-from-string "#chan")))

    (ert-info ("New buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-process (erc-networks-tests--create-live-proc)
              erc-networks--id (erc-networks--id-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer reassociated")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf))))

  (erc-networks-tests--clean-bufs))

;; This is for compatibility with pre-28.1 behavior.  Basically, we're
;; trying to match the behavior bug for bug.  All buffers were always
;; suffixed and never reassociated.  28.1 introduced a regression that
;; reversed the latter, but we've reverted that.

(ert-deftest erc-networks--rename-server-buffer--existing--noreuse ()
  (with-suppressed-warnings ((obsolete erc-reuse-buffers))
    (should erc-reuse-buffers) ; default
    (let* ((old-buf (get-buffer-create "irc.foonet.org:6697/irc.foonet.org"))
           (old-proc (erc-networks-tests--create-dead-proc old-buf))
           erc-reuse-buffers)
      (with-current-buffer old-buf
        (erc-mode)
        (insert "*** Old buf")
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-insert-marker (set-marker (make-marker) (point-max))
              erc-server-process old-proc
              erc-networks--id (erc-networks--id-create nil)))
      (with-current-buffer (get-buffer-create "#chan")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-process old-proc
              erc-networks--id (buffer-local-value 'erc-networks--id old-buf)
              erc--target (erc--target-from-string "#chan"))
        (rename-buffer (erc-networks--construct-target-buffer-name erc--target)))

      (ert-info ("Server buffer uniquely renamed")
        (with-current-buffer
            (get-buffer-create "irc.foonet.org:6697/irc.foonet.org<2>")
          (erc-mode)
          (setq erc-network 'FooNet
                erc-server-current-nick "tester"
                erc-server-process (erc-networks-tests--create-live-proc)
                erc-networks--id (erc-networks--id-create nil))
          (should-not (erc-networks--rename-server-buffer erc-server-process))
          (should (string= (buffer-name)
                           "irc.foonet.org:6697/irc.foonet.org<2>"))
          (goto-char (point-min))
          (should-not (search-forward "Old buf" nil t))))

      (ert-info ("Channel buffer not reassociated")
        (should-not
         (erc-server-process-alive
          (should (get-buffer "#chan/irc.foonet.org"))))
        (with-current-buffer (get-buffer "#chan/irc.foonet.org")
          (should-not erc-server-connected)
          (should (eq erc-server-process old-proc))
          (erc-with-server-buffer
            (should (string= (buffer-name)
                             "irc.foonet.org:6697/irc.foonet.org")))))

      (ert-info ("Old buffer still around")
        (should (buffer-live-p old-buf)))))
  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--reconnecting ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-networks-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc-networks--id (erc-networks--id-create nil)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc--target (erc--target-from-string "#chan")
            erc-networks--id (erc-networks--id-create nil)))

    (ert-info ("No new buffer")
      (with-current-buffer old-buf
        (setq erc-server-process (erc-networks-tests--create-live-proc))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer updated with live proc")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet"))))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--id ()
  (let* ((old-buf (get-buffer-create "MySession"))
         (old-proc (erc-networks-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-networks--id (erc-networks--id-create 'MySession)
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-networks--id (erc-networks--id-create 'MySession)
            erc-server-process old-proc
            erc--target (erc--target-from-string "#chan")))

    (ert-info ("No new buffer")
      (with-current-buffer old-buf
        (setq erc-server-process (erc-networks-tests--create-live-proc))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "MySession"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer updated with live proc")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "MySession"))))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--existing--live ()
  (let* (erc-kill-server-hook
         erc-insert-modify-hook
         (old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-networks-tests--create-live-proc old-buf))) ; live

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc-networks--id (erc-networks--id-create nil))
      (should (erc-server-process-alive)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-networks--id (erc-networks--id-create nil)
            erc-server-connected t
            erc--target (erc--target-from-string "#chan")))

    (ert-info ("New buffer rejected, abandoned, not killed")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-insert-marker (set-marker (make-marker) (point-max))
              erc-server-process (erc-networks-tests--create-live-proc)
              erc-networks--id (erc-networks--id-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (eq erc-active-buffer old-buf))
        (should-not (erc-server-process-alive))
        (should (string= (buffer-name) "irc.foonet.org"))
        (goto-char (point-min))
        (search-forward "still connected")))

    (ert-info ("Channel buffer updated with live proc")
      (should (erc-server-process-alive "#chan"))
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should (erc-server-buffer-live-p))
        (should (eq erc-server-process old-proc))
        (should (buffer-live-p (process-buffer erc-server-process)))
        (with-current-buffer (process-buffer erc-server-process)
          (should (eq (current-buffer) (get-buffer "FooNet")))
          (should (eq (current-buffer) old-buf))))))

  (should (get-buffer "FooNet"))
  (should (get-buffer "irc.foonet.org"))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--local-match ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-networks-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-server-announced-name "us-east.foonet.org"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--isupport-params (make-hash-table)
            erc-networks--id (erc-networks--id-create nil))
      (puthash 'CHANTYPES '("&#") erc--isupport-params))

    (with-current-buffer (get-buffer-create "&chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-server-announced-name "us-east.foonet.org"
            erc--target (erc--target-from-string "&chan")
            erc-networks--id (erc-networks--id-create nil)))

    (ert-info ("New server buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-announced-name "us-east.foonet.org"
              erc-server-process (erc-networks-tests--create-live-proc)
              erc--isupport-params (make-hash-table)
              erc-networks--id (erc-networks--id-create nil))
        (puthash 'CHANTYPES '("&#") erc--isupport-params)
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer reassociated when &local server matches")
      (should (erc-server-process-alive "&chan"))
      (with-current-buffer "&chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf)))

    (erc-networks-tests--clean-bufs)))

(ert-deftest erc-networks--rename-server-buffer--local-nomatch ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-networks-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-server-announced-name "us-west.foonet.org"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--isupport-params (make-hash-table)
            erc-networks--id (erc-networks--id-create nil))
      (puthash 'CHANTYPES '("&#") erc--isupport-params))

    (with-current-buffer (get-buffer-create "&chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-server-announced-name "us-west.foonet.org" ; west
            erc--target (erc--target-from-string "&chan")
            erc-networks--id (erc-networks--id-create nil)))

    (ert-info ("New server buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-announced-name "us-east.foonet.org" ; east
              erc-server-process (erc-networks-tests--create-live-proc)
              erc--isupport-params (make-hash-table)
              erc-networks--id (erc-networks--id-create nil))

        (puthash 'CHANTYPES '("&#") erc--isupport-params)
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer now orphaned even though network matches")
      (should-not (erc-server-process-alive "&chan"))
      (with-current-buffer "&chan"
        (should-not erc-server-connected)
        (should (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf)))

    (erc-networks-tests--clean-bufs)))

(ert-deftest erc-networks--update-server-identity--double-existing ()
  (with-temp-buffer
    (erc-mode)
    (setq erc-networks--id (make-erc-networks--id-qualifying
                            :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "#chan@foonet/bob")
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [foonet "bob"] :len 2)))
    (with-current-buffer (get-buffer-create "foonet/alice")
      (erc-mode)
      (setq erc-networks--id
            (make-erc-networks--id-qualifying :parts [foonet "alice"] :len 2)))

    (ert-info ("Adopt equivalent identity")
      (should (eq (erc-networks--update-server-identity)
                  (buffer-local-value 'erc-networks--id
                                      (get-buffer "#chan@foonet/bob")))))

    (ert-info ("Ignore non-matches")
      (should-not (erc-networks--update-server-identity))
      (should (eq erc-networks--id
                  (buffer-local-value 'erc-networks--id
                                      (get-buffer "#chan@foonet/bob"))))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--update-server-identity--double-new ()
  (with-temp-buffer
    (erc-mode)
    (setq erc-networks--id (make-erc-networks--id-qualifying
                            :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet/alice")
      (erc-mode)
      (setq erc-networks--id
            (make-erc-networks--id-qualifying :parts [foonet "alice"] :len 2)))
    (with-current-buffer (get-buffer-create "#chan@foonet/alice")
      (erc-mode)
      (setq erc-networks--id (buffer-local-value 'erc-networks--id
                                                 (get-buffer "foonet/alice"))))

    (ert-info ("Evolve identity to prevent ambiguity")
      (should-not (erc-networks--update-server-identity))
      (should (= (erc-networks--id-qualifying-len erc-networks--id) 2))
      (should (eq (erc-networks--id-symbol erc-networks--id) 'foonet/bob))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--update-server-identity--double-bounded ()
  (with-temp-buffer
    (erc-mode)
    (setq erc-networks--id (make-erc-networks--id-qualifying
                            :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet/alice/home")
      (erc-mode)
      (setq erc-networks--id (make-erc-networks--id-qualifying
                              :parts [foonet "alice" home] :len 3)))
    (with-current-buffer (get-buffer-create "#chan@foonet/alice/home")
      (erc-mode)
      (setq erc-networks--id
            (buffer-local-value 'erc-networks--id
                                (get-buffer "foonet/alice/home"))))

    (ert-info ("Evolve identity to prevent ambiguity")
      (should-not (erc-networks--update-server-identity))
      (should (= (erc-networks--id-qualifying-len erc-networks--id) 2))
      (should (eq (erc-networks--id-symbol erc-networks--id) 'foonet/bob))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--update-server-identity--double-even ()
  (with-temp-buffer
    (erc-mode)
    (setq erc-networks--id
          (make-erc-networks--id-qualifying :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet")
      (erc-mode)
      (setq erc-networks--id
            (make-erc-networks--id-qualifying :parts [foonet "alice"] :len 1)))
    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc--target (erc--target-from-string "#chan")
            erc-networks--id (buffer-local-value 'erc-networks--id
                                                 (get-buffer "foonet"))))

    (ert-info ("Evolve identity to prevent ambiguity")
      (should-not (erc-networks--update-server-identity))
      (should (= (erc-networks--id-qualifying-len erc-networks--id) 2))
      (should (eq (erc-networks--id-symbol erc-networks--id) 'foonet/bob)))

    (ert-info ("Collision renamed")
      (with-current-buffer "foonet/alice"
        (should (eq (erc-networks--id-symbol erc-networks--id) 'foonet/alice)))

      (with-current-buffer "#chan@foonet/alice"
        (should (eq (erc-networks--id-symbol erc-networks--id)
                    'foonet/alice)))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--update-server-identity--triple-new ()
  (with-temp-buffer
    (erc-mode)
    (setq erc-networks--id
          (make-erc-networks--id-qualifying :parts [foonet "bob" home] :len 1))

    (with-current-buffer (get-buffer-create "foonet/bob/office")
      (erc-mode)
      (setq erc-networks--id
            (make-erc-networks--id-qualifying :parts [foonet "bob" office]
                                              :len 3)))
    (with-current-buffer (get-buffer-create "#chan@foonet/bob/office")
      (erc-mode)
      (setq erc-networks--id
            (buffer-local-value 'erc-networks--id
                                (get-buffer "foonet/bob/office"))))

    (ert-info ("Extend our identity's canonical ID so that it's unique")
      (should-not (erc-networks--update-server-identity))
      (should (= (erc-networks--id-qualifying-len erc-networks--id) 3))))

  (erc-networks-tests--clean-bufs))

(ert-deftest erc-networks--determine ()
  (should (eq (erc-networks--determine "irc.libera.chat") 'Libera.Chat))
  (should (eq (erc-networks--determine "irc.oftc.net") 'OFTC))
  (should (eq (erc-networks--determine "irc.dal.net") 'DALnet))

  (let ((erc-server-announced-name "zirconium.libera.chat"))
    (should (eq (erc-networks--determine) 'Libera.Chat)))
  (let ((erc-server-announced-name "weber.oftc.net"))
    (should (eq (erc-networks--determine) 'OFTC)))
  (let ((erc-server-announced-name "redemption.ix.us.dal.net"))
    (should (eq (erc-networks--determine) 'DALnet)))

  ;; Failure
  (let ((erc-server-announced-name "irc-us2.alphachat.net"))
    (should (eq (erc-networks--determine)
                erc-networks--name-missing-sentinel))))

;;; erc-networks-tests.el ends here

debug log:

solving fc12bf7ce3 ...
found fc12bf7ce3 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this 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.