derec-library 0.1.0

Rust SDK for the DeRec protocol, including native and WebAssembly bindings.
Documentation
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

#[cfg(test)]
mod handshake_tests {
    use super::super::*;
    use crate::protocol::tests::LocalFixture;
    use crate::protocol::tests::StoreRig;

    fn entries_of(c: &CommunicationInfo) -> Vec<(String, String)> {
        c.communication_info_entries
            .iter()
            .filter_map(|e| match &e.value {
                Some(Value::StringValue(s)) => Some((e.key.clone(), s.clone())),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn build_omits_reserved_key_when_no_replica_id_to_inject() {
        let mut info = HashMap::new();
        info.insert("name".to_owned(), "Alice".to_owned());

        let built = build_communication_info(&info, None).expect("entries present");
        let pairs = entries_of(&built);
        assert_eq!(pairs.len(), 1);
        assert_eq!(pairs[0], ("name".to_owned(), "Alice".to_owned()));
    }

    #[test]
    fn build_injects_reserved_key_when_replica_id_supplied() {
        let mut info = HashMap::new();
        info.insert("name".to_owned(), "Alice".to_owned());

        let built = build_communication_info(&info, Some(0xDEAD_BEEFu64)).expect("entries present");
        let pairs = entries_of(&built);

        let replica_entry = pairs
            .iter()
            .find(|(k, _)| k == reserved_keys::DEREC_REPLICA_ID_KEY)
            .expect("derec.replica_id should be injected");
        assert_eq!(replica_entry.1, "3735928559");

        assert!(pairs.iter().any(|(k, v)| k == "name" && v == "Alice"));
    }

    #[test]
    fn build_drops_app_supplied_entries_in_reserved_namespace() {
        let mut info = HashMap::new();
        info.insert("name".to_owned(), "Alice".to_owned());
        info.insert(
            reserved_keys::DEREC_REPLICA_ID_KEY.to_owned(),
            "ffffffffffff".to_owned(),
        );
        info.insert("derec.foo".to_owned(), "bar".to_owned());

        let built = build_communication_info(&info, Some(1)).expect("entries present");
        let pairs = entries_of(&built);

        assert_eq!(pairs.len(), 2);
        let replica_entry = pairs
            .iter()
            .find(|(k, _)| k == reserved_keys::DEREC_REPLICA_ID_KEY)
            .unwrap();
        assert_eq!(replica_entry.1, "1");
        assert!(!pairs.iter().any(|(k, _)| k == "derec.foo"));
    }

    #[test]
    fn extract_strips_reserved_key_and_returns_typed_id() {
        let info = Some(CommunicationInfo {
            communication_info_entries: vec![
                CommunicationInfoKeyValue {
                    key: "name".to_owned(),
                    value: Some(Value::StringValue("Bob".to_owned())),
                },
                CommunicationInfoKeyValue {
                    key: reserved_keys::DEREC_REPLICA_ID_KEY.to_owned(),
                    value: Some(Value::StringValue("3405691582".to_owned())),
                },
            ],
        });

        let (map, replica_id) =
            extract_communication_info(&info, SenderKind::ReplicaSource).unwrap();

        assert_eq!(map.len(), 1);
        assert_eq!(map.get("name").map(String::as_str), Some("Bob"));
        assert!(!map.contains_key(reserved_keys::DEREC_REPLICA_ID_KEY));
        assert_eq!(replica_id, Some(0xCAFE_BABEu64));
    }

    #[test]
    fn extract_rejects_replica_pairing_missing_reserved_key() {
        let info = Some(CommunicationInfo {
            communication_info_entries: vec![CommunicationInfoKeyValue {
                key: "name".to_owned(),
                value: Some(Value::StringValue("Bob".to_owned())),
            }],
        });

        let err =
            extract_communication_info(&info, SenderKind::ReplicaSource).expect_err("must reject");
        assert!(
            matches!(
                err,
                Error::Pairing(PairingError::MissingReplicaId { sender_kind })
                    if sender_kind == SenderKind::ReplicaSource
            ),
            "expected MissingReplicaId, got {err:?}"
        );
    }

    #[test]
    fn extract_rejects_non_replica_pairing_carrying_reserved_key() {
        let info = Some(CommunicationInfo {
            communication_info_entries: vec![CommunicationInfoKeyValue {
                key: reserved_keys::DEREC_REPLICA_ID_KEY.to_owned(),
                value: Some(Value::StringValue("12345".to_owned())),
            }],
        });

        let err = extract_communication_info(&info, SenderKind::Owner).expect_err("must reject");
        assert!(
            matches!(
                err,
                Error::Pairing(PairingError::UnexpectedReplicaId { sender_kind })
                    if sender_kind == SenderKind::Owner
            ),
            "expected UnexpectedReplicaId, got {err:?}"
        );
    }

    #[test]
    fn extract_rejects_replica_pairing_with_no_communication_info() {
        let err =
            extract_communication_info(&None, SenderKind::ReplicaSource).expect_err("must reject");
        assert!(matches!(
            err,
            Error::Pairing(PairingError::MissingReplicaId { .. })
        ));
    }

    #[test]
    fn extract_accepts_non_replica_pairing_with_no_communication_info() {
        let (map, id) = extract_communication_info(&None, SenderKind::Helper).unwrap();
        assert!(map.is_empty());
        assert_eq!(id, None);
    }

    #[test]
    fn require_replica_id_for_kind_short_circuits_non_replica() {
        assert_eq!(
            require_replica_id_for_kind(SenderKind::Owner, None).unwrap(),
            None
        );
        assert_eq!(
            require_replica_id_for_kind(SenderKind::Helper, Some(123)).unwrap(),
            None,
            "Helper kind ignores configured replica id"
        );
    }

    /// Selection picks which of the peer's endpoints we send to; this picks
    /// A PrePair request carries exactly one endpoint on the wire, so this
    /// side has to name one of its own. It names the first of its configured
    /// preference order — the application's stated preference — rather than
    /// matching the peer's, because there is no longer a "the peer's
    /// protocol" to match: the peer's whole offer set is recorded and the
    /// choice of which to dial is the application's.
    #[test]
    fn prepair_advertises_the_first_configured_own_endpoint() {
        let own = [
            TransportProtocol {
                uri: "grpcs://me.example:443".to_owned(),
                protocol: Protocol::Grpc as i32,
            },
            TransportProtocol {
                uri: "https://me.example/derec".to_owned(),
                protocol: Protocol::Https as i32,
            },
        ];

        let lf = LocalFixture {
            own_transports: own.to_vec(),
            ..LocalFixture::new(0)
        };
        assert_eq!(
            lf.local().primary().uri,
            "grpcs://me.example:443",
            "the caller's order decides which single endpoint PrePair advertises"
        );
    }

    /// A single-transport deployment has exactly one answer.
    #[test]
    fn prepair_advertises_the_sole_endpoint_when_only_one_is_served() {
        let own = [TransportProtocol {
            uri: "https://me.example/derec".to_owned(),
            protocol: Protocol::Https as i32,
        }];

        let lf = LocalFixture {
            own_transports: own.to_vec(),
            ..LocalFixture::new(0)
        };
        assert_eq!(lf.local().primary().uri, "https://me.example/derec");
    }

    #[test]
    fn require_replica_id_for_kind_passes_through_for_replica() {
        assert_eq!(
            require_replica_id_for_kind(SenderKind::ReplicaSource, Some(42)).unwrap(),
            Some(42)
        );
    }

    #[test]
    fn require_replica_id_for_kind_errors_when_replica_unconfigured() {
        let err = require_replica_id_for_kind(SenderKind::ReplicaSource, None).unwrap_err();
        assert!(matches!(err, Error::ReplicaIdNotConfigured));
    }

    use crate::protocol::traits::ChannelStoreFuture;
    use crate::protocol::types::{
        ChannelQuery, ChannelRecord, ChannelStatus, HelperChannel, HelperFilter, ReplicaFilter,
        ReplicaMember,
    };
    use derec_proto::communication_info_key_value::Value;
    use derec_proto::{CommunicationInfoKeyValue, Protocol};
    use std::future;
    use std::future::Future;
    use tokio::runtime::Builder;

    struct FixedChannelStore {
        seeded: Option<ChannelRecord>,
    }

    impl DeRecChannelStore for FixedChannelStore {
        fn load(&self, _: u64, _: ChannelQuery) -> ChannelStoreFuture<'_, Option<ChannelRecord>> {
            let v = self.seeded.clone();
            Box::pin(future::ready(Ok(v)))
        }
        fn save(&mut self, _: u64, _: ChannelRecord) -> ChannelStoreFuture<'_, ()> {
            Box::pin(future::ready(Ok(())))
        }
        fn remove(&mut self, _: u64, _: ChannelQuery) -> ChannelStoreFuture<'_, bool> {
            Box::pin(future::ready(Ok(false)))
        }
        fn helpers(&self, _: u64, _: HelperFilter) -> ChannelStoreFuture<'_, Vec<HelperChannel>> {
            Box::pin(future::ready(Ok(Vec::new())))
        }
        fn replicas(&self, _: u64, _: ReplicaFilter) -> ChannelStoreFuture<'_, Vec<ReplicaMember>> {
            Box::pin(future::ready(Ok(Vec::new())))
        }
        fn link_channel(
            &mut self,
            _: u64,
            _: ChannelId,
            _: ChannelId,
        ) -> ChannelStoreFuture<'_, ()> {
            Box::pin(future::ready(Ok(())))
        }
        fn linked_channels(
            &self,
            _: u64,
            cid: ChannelId,
        ) -> ChannelStoreFuture<'_, Vec<ChannelId>> {
            Box::pin(future::ready(Ok(vec![cid])))
        }
    }

    fn fake_channel(status: ChannelStatus, peer_role: SenderKind) -> ChannelRecord {
        ChannelRecord::Helper(HelperChannel {
            channel_id: ChannelId(1),
            transports: vec![TransportProtocol {
                uri: "https://example.com".to_owned(),
                protocol: 0,
            }],
            communication_info: HashMap::new(),
            status,
            created_at: 1_700_000_000,
            peer_role,
            weight: 1,
        })
    }

    fn run_async<F: Future<Output = ()>>(f: F) {
        Builder::new_current_thread()
            .build()
            .expect("test runtime")
            .block_on(f)
    }

    #[test]
    fn reject_start_on_channel_in_use_errors_when_channel_already_paired() {
        run_async(async {
            let lf = LocalFixture::new(42);
            let mut rig = StoreRig::with_channels(FixedChannelStore {
                seeded: Some(fake_channel(ChannelStatus::Paired, SenderKind::Helper)),
            });
            let err = reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), ChannelId(1))
                .await
                .unwrap_err();
            assert!(
                matches!(err, Error::ChannelAlreadyPaired { channel_id } if channel_id == ChannelId(1)),
                "expected Error::ChannelAlreadyPaired for ChannelId(1), got {err:?}"
            );
        });
    }

    #[test]
    fn reject_start_on_channel_in_use_allows_pending_retry() {
        run_async(async {
            let lf = LocalFixture::new(42);
            let mut rig = StoreRig::with_channels(FixedChannelStore {
                seeded: Some(fake_channel(ChannelStatus::Pending, SenderKind::Helper)),
            });
            reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), ChannelId(1))
                .await
                .expect("Pending channel must be accepted as a retry candidate");
        });
    }

    /// A channel still being unpaired keeps its key until the helper answers,
    /// so pairing over it would overwrite that key mid-teardown.
    #[test]
    fn reject_start_on_channel_in_use_errors_while_unpairing() {
        run_async(async {
            let lf = LocalFixture::new(42);
            let mut rig = StoreRig::with_channels(FixedChannelStore {
                seeded: Some(fake_channel(ChannelStatus::Unpairing, SenderKind::Helper)),
            });
            let err = reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), ChannelId(1))
                .await
                .unwrap_err();
            assert!(matches!(err, Error::ChannelAlreadyPaired { .. }));
        });
    }

    #[test]
    fn reject_start_on_channel_in_use_allows_when_no_channel_record() {
        run_async(async {
            let lf = LocalFixture::new(42);
            let mut rig = StoreRig::with_channels(FixedChannelStore { seeded: None });
            reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), ChannelId(1))
                .await
                .expect("absent channel record must be accepted");
        });
    }

    #[test]
    fn reject_start_on_channel_in_use_errors_for_paired_replica() {
        run_async(async {
            let lf = LocalFixture::new(42);
            let mut rig = StoreRig::with_channels(FixedChannelStore {
                seeded: Some(fake_channel(
                    ChannelStatus::Paired,
                    SenderKind::ReplicaSource,
                )),
            });
            let err = reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), ChannelId(1))
                .await
                .unwrap_err();
            assert!(matches!(err, Error::ChannelAlreadyPaired { .. }));
        });
    }
}

/// D6: no two members of a group may share a `replica_id`.
#[cfg(test)]
mod prepair_record_shape_tests {
    use super::super::*;
    use crate::protocol::tests::LocalFixture;
    use crate::protocol::tests::{StoreRig, run_async};
    use crate::protocol::types::{
        ChannelQuery, ChannelRecord, ChannelStatus, HelperChannel, ReplicaFilter, ReplicaMember,
        ReplicaRole,
    };
    use crate::types::ReplicaId;
    use derec_proto::Protocol;
    use std::collections::HashMap;

    const SECRET_ID: u64 = 0x0B0E_9A19;
    const CHANNEL: ChannelId = ChannelId(6000);
    const OWN_REPLICA: u64 = 0xA11CE;

    fn endpoint() -> TransportProtocol {
        TransportProtocol {
            uri: "https://peer.example".to_owned(),
            protocol: Protocol::Https as i32,
        }
    }

    fn endpoints() -> Vec<TransportProtocol> {
        vec![endpoint()]
    }

    /// A replica scanner records its pairing like any other: a `Pending`
    /// placeholder carrying the peer's role, from which the PrePair and pair
    /// responses resolve the local kind. Nothing enters the replica roster —
    /// a pairing in flight is not group membership.
    #[test]
    fn a_replica_scanner_is_recorded_as_a_pending_placeholder() {
        run_async(async {
            let lf = LocalFixture::new(SECRET_ID);
            let mut rig = StoreRig::new();
            persist_start_record(
                &mut rig.stores(),
                &lf.local(),
                CHANNEL,
                endpoints(),
                HashMap::new(),
                SenderKind::ReplicaDestination,
            )
            .await
            .expect("persist start record");

            let placeholder = rig
                .channels
                .load(
                    SECRET_ID,
                    ChannelQuery::Helper {
                        channel_id: CHANNEL,
                    },
                )
                .await
                .expect("load")
                .and_then(|r| r.as_helper().cloned())
                .expect("the replica scanner records a placeholder");
            assert_eq!(placeholder.peer_role, SenderKind::ReplicaSource);
            assert_eq!(placeholder.status, ChannelStatus::Pending);
            assert!(
                rig.channels
                    .replicas(SECRET_ID, ReplicaFilter::default())
                    .await
                    .expect("list members")
                    .is_empty(),
                "no roster row is written before the pairing is confirmed"
            );
        });
    }

    /// The own roster row carries every endpoint this device advertises and
    /// its `communication_info`, filtered the way a peer would store it — the
    /// same shape a peer's row has, so a published roster names and reaches its
    /// writer as well as everyone else.
    #[test]
    fn the_own_roster_row_carries_what_this_device_advertises() {
        run_async(async {
            let lf = LocalFixture {
                own_transports: vec![
                    endpoint(),
                    TransportProtocol {
                        uri: "grpcs://peer.example:443".to_owned(),
                        protocol: Protocol::Grpc as i32,
                    },
                ],
                communication_info: HashMap::from([
                    ("name".to_owned(), "Alice-1".to_owned()),
                    ("blank".to_owned(), "  ".to_owned()),
                    ("derec.replica_id".to_owned(), "spoofed".to_owned()),
                ]),
                replica_id: Some(OWN_REPLICA),
                ..LocalFixture::new(SECRET_ID)
            };
            let mut rig = StoreRig::new();
            claim_own_member(&mut rig.stores(), &lf.local(), CHANNEL, ReplicaRole::Source)
                .await
                .expect("claim own row");

            let member = rig
                .channels
                .load(
                    SECRET_ID,
                    ChannelQuery::Replica {
                        channel_id: CHANNEL,
                        replica_id: ReplicaId(OWN_REPLICA),
                    },
                )
                .await
                .expect("load")
                .and_then(|r| r.as_replica().cloned())
                .expect("own roster row present");
            assert_eq!(member.status, ChannelStatus::Paired);
            assert_eq!(
                member.transports, lf.own_transports,
                "every advertised endpoint, in preference order"
            );
            assert_eq!(
                member.communication_info,
                HashMap::from([("name".to_owned(), "Alice-1".to_owned())]),
            );
        });
    }

    /// The first confirmed pairing fixes the group's channel: a later one
    /// leaves a `Paired` own row where it is, while an own row that is not
    /// `Paired` names no group and is replaced.
    #[test]
    fn claiming_the_own_row_keeps_a_group_and_replaces_anything_else() {
        run_async(async {
            const GROUP: ChannelId = ChannelId(5001);
            const LATER: ChannelId = ChannelId(7001);
            let lf = LocalFixture {
                replica_id: Some(OWN_REPLICA),
                ..LocalFixture::new(SECRET_ID)
            };
            let own_channel = |rig: &StoreRig| {
                rig.channels
                    .member_rows
                    .lock()
                    .unwrap()
                    .get(&(SECRET_ID, OWN_REPLICA))
                    .map(|m| (m.channel_id, m.status))
            };

            let mut rig = StoreRig::new();
            rig.channels
                .save(
                    SECRET_ID,
                    ChannelRecord::Replica(ReplicaMember {
                        channel_id: ChannelId(100),
                        replica_id: ReplicaId(OWN_REPLICA),
                        transports: endpoints(),
                        communication_info: HashMap::new(),
                        role: ReplicaRole::Source,
                        status: ChannelStatus::Pending,
                        created_at: 0,
                        handover_channel: None,
                    }),
                )
                .await
                .expect("seed a leftover own row");

            claim_own_member(&mut rig.stores(), &lf.local(), GROUP, ReplicaRole::Source)
                .await
                .expect("first claim");
            assert_eq!(own_channel(&rig), Some((GROUP, ChannelStatus::Paired)));

            claim_own_member(&mut rig.stores(), &lf.local(), LATER, ReplicaRole::Source)
                .await
                .expect("later claim");
            assert_eq!(
                own_channel(&rig),
                Some((GROUP, ChannelStatus::Paired)),
                "a device in a group keeps its channel"
            );
        });
    }

    /// The helper path is unchanged: it still records the peer's role and the
    /// local kind is its counterparty.
    #[test]
    fn a_helper_scanner_is_recorded_as_a_helper_channel() {
        run_async(async {
            let lf = LocalFixture::new(SECRET_ID);
            let mut rig = StoreRig::new();
            persist_start_record(
                &mut rig.stores(),
                &lf.local(),
                CHANNEL,
                endpoints(),
                HashMap::new(),
                SenderKind::Owner,
            )
            .await
            .expect("persist start record");

            let helper: HelperChannel = rig
                .channels
                .load(
                    SECRET_ID,
                    ChannelQuery::Helper {
                        channel_id: CHANNEL,
                    },
                )
                .await
                .expect("load")
                .and_then(|r| r.as_helper().cloned())
                .expect("helper record present");
            assert_eq!(helper.peer_role.counterparty(), SenderKind::Owner);
            assert_eq!(helper.status, ChannelStatus::Pending);
            let _ = ChannelRecord::Replica(ReplicaMember {
                channel_id: CHANNEL,
                replica_id: ReplicaId(OWN_REPLICA),
                transports: endpoints(),
                communication_info: HashMap::new(),
                role: ReplicaRole::Destination,
                status: ChannelStatus::Pending,
                created_at: 0,
                handover_channel: None,
            });
        });
    }
}

#[cfg(test)]
mod pairing_material_cleanup_tests {
    use super::super::*;
    use crate::protocol::tests::StoreRig;
    use crate::protocol::tests::run_async;
    use crate::protocol::{DeRecSecretStore, SecretKind, SecretValue};

    const SECRET_ID: u64 = 0x0C00_7AC7;
    const TRANSIENT: ChannelId = ChannelId(1004);

    /// A completed pairing must leave nothing behind at the transient channel.
    ///
    /// `create_contact` stores a `PairingContact` rather than a
    /// `PairingSecret` under `NoKeys` — that mode has no key material yet, and
    /// the contact is what authenticates the later `PrePairRequest` by nonce.
    /// The accept path used to remove only the secret, so every `NoKeys`
    /// pairing stranded one contact row at an id nothing addresses again.
    #[test]
    fn a_spent_pairing_contact_is_dropped() {
        run_async(async {
            let mut rig = StoreRig::new();
            rig.secrets
                .save(
                    SECRET_ID,
                    TRANSIENT,
                    SecretValue::PairingContact(Default::default()),
                )
                .await
                .expect("seed contact");

            let _ = rig
                .secrets
                .remove(SECRET_ID, TRANSIENT, SecretKind::PairingSecret)
                .await;
            let _ = rig
                .secrets
                .remove(SECRET_ID, TRANSIENT, SecretKind::PairingContact)
                .await;

            let left = rig
                .secrets
                .load(SECRET_ID, TRANSIENT, SecretKind::PairingContact)
                .await
                .expect("load");
            assert!(
                left.is_none(),
                "the transient contact must not outlive the handshake"
            );
        });
    }
}

#[cfg(test)]
mod replica_id_conflict_tests {
    use super::super::pair::abandon_pairing;
    use super::super::replica_id_is_taken;
    use crate::protocol::context::PairingConfig;
    use crate::protocol::tests::LocalFixture;
    use crate::protocol::tests::StoreRig;
    use crate::protocol::tests::{InMemChannelStore, run_async};
    use crate::protocol::types::{
        ChannelRecord, ChannelStatus, ReplicaFilter, ReplicaMember, ReplicaRole, Timeouts,
    };
    use crate::protocol::{
        DeRecChannelStore, DeRecSecretStore, PairingKeyMaterial, SecretKind, SecretValue,
    };
    use crate::types::{ChannelId, ReplicaId};
    use derec_proto::{Protocol, TransportProtocol};
    use std::collections::HashMap;

    const SECRET_ID: u64 = 0xD6;
    const GROUP: ChannelId = ChannelId(5001);
    const PAIRING: ChannelId = ChannelId(77);

    async fn seed_member(channels: &mut InMemChannelStore, id: u64, channel_id: ChannelId) {
        channels
            .save(
                SECRET_ID,
                ChannelRecord::Replica(ReplicaMember {
                    channel_id,
                    replica_id: ReplicaId(id),
                    transports: vec![TransportProtocol {
                        uri: "https://peer.example".to_owned(),
                        protocol: Protocol::Https as i32,
                    }],
                    communication_info: HashMap::new(),
                    role: ReplicaRole::Source,
                    status: ChannelStatus::Paired,
                    created_at: 0,
                    handover_channel: None,
                }),
            )
            .await
            .expect("seed member");
    }

    /// The roster carries this device's own row, so a peer announcing our own
    /// id collides — the common case, since both ends of a first pairing are
    /// usually configured by one person.
    #[test]
    fn our_own_id_counts_as_taken() {
        run_async(async {
            let lf = LocalFixture::new(SECRET_ID);
            let mut rig = StoreRig::new();
            seed_member(&mut rig.channels, 1001, GROUP).await;

            assert!(
                replica_id_is_taken(&mut rig.stores(), &lf.local(), 1001)
                    .await
                    .expect("check"),
                "a peer announcing our own id must be refused"
            );
            assert!(
                !replica_id_is_taken(&mut rig.stores(), &lf.local(), 1002)
                    .await
                    .expect("check"),
                "a distinct id is free"
            );
        });
    }

    /// A device not yet in a group created its own row during this handshake,
    /// pointing at a channel that is about to disappear. Abandoning must take
    /// it with them, or a later publish resolves the group to a dead channel.
    #[test]
    fn abandoning_a_first_pairing_removes_the_row_it_created() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, 1001);
            let mut rig = StoreRig::new();
            seed_member(&mut rig.channels, 1001, PAIRING).await;
            rig.secrets
                .save(SECRET_ID, PAIRING, SecretValue::SharedKey([0x22; 32]))
                .await
                .expect("seed key");

            abandon_pairing(&mut rig.stores(), &lf.local(), PAIRING)
                .await
                .expect("abandon succeeds");

            assert!(
                rig.channels
                    .replicas(SECRET_ID, ReplicaFilter::default())
                    .await
                    .expect("roster")
                    .is_empty(),
                "the row this pairing created must not outlive it"
            );
        });
    }

    /// A device already in a group keeps its identity: the failed pairing has
    /// no claim on it, and removing it would evict the device from a group it
    /// legitimately belongs to.
    #[test]
    fn abandoning_keeps_an_existing_group_membership() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, 1001);
            let mut rig = StoreRig::new();
            seed_member(&mut rig.channels, 1001, GROUP).await;
            seed_member(&mut rig.channels, 1002, GROUP).await;

            abandon_pairing(&mut rig.stores(), &lf.local(), PAIRING)
                .await
                .expect("abandon succeeds");

            let roster = rig
                .channels
                .replicas(SECRET_ID, ReplicaFilter::default())
                .await
                .expect("roster");
            assert_eq!(
                roster.len(),
                2,
                "an established group survives a failed pairing attempt"
            );
        });
    }

    /// The transient pairing channel and its key go, whichever case applies.
    #[test]
    fn abandoning_clears_the_pairing_channel() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, 1001);
            let mut rig = StoreRig::new();
            rig.secrets
                .save(
                    SECRET_ID,
                    PAIRING,
                    SecretValue::PairingSecret(PairingKeyMaterial::from_bytes(vec![0x01, 0x02])),
                )
                .await
                .expect("seed pairing secret");

            abandon_pairing(&mut rig.stores(), &lf.local(), PAIRING)
                .await
                .expect("abandon succeeds");

            assert!(
                rig.secrets
                    .load(SECRET_ID, PAIRING, SecretKind::PairingSecret)
                    .await
                    .expect("load")
                    .is_none(),
                "the pairing secret must not survive an abandoned handshake"
            );
        });
    }

    /// `accept()` must record the requester's whole `supportedTransports`
    /// offer list. The peer here offers HTTPS first and gRPCS second, and
    /// gRPCS is the only protocol this responder actually serves.
    #[test]
    fn accept_offers_the_transport_every_advertised_endpoint() {
        use super::super::pair::accept;
        use crate::primitives::pairing::request;
        use derec_proto::{ContactMode, SenderKind};

        run_async(async {
            let channel_id = ChannelId(0xACCE_9701);
            let secret_id = 0x60A7;

            let request::CreateContactResult {
                contact_message,
                secret_key,
            } = request::create_contact(
                channel_id,
                ContactMode::InlineKeys,
                vec![TransportProtocol {
                    uri: "https://initiator.example/derec".to_owned(),
                    protocol: Protocol::Https as i32,
                }],
                None,
            )
            .expect("create_contact");
            let initiator_secret = secret_key.expect("InlineKeys always returns key material");

            // Built through the real crypto path so the request decrypts
            // and finalizes normally; only the transport fields are then
            // overwritten to the scenario under test.
            let request::ProduceResult {
                envelope: request_envelope,
                ..
            } = request::produce(
                SenderKind::Helper,
                vec![TransportProtocol {
                    uri: "https://peer.example.com/derec".to_owned(),
                    protocol: Protocol::Https as i32,
                }],
                &contact_message,
                None,
                None,
            )
            .expect("produce");

            let request::ExtractResult {
                request: mut pair_request,
            } = request::extract(&request_envelope, initiator_secret.ecies_secret_key(), None)
                .expect("extract");

            pair_request.supported_transports = vec![
                TransportProtocol {
                    uri: "https://peer.example.com/derec".to_owned(),
                    protocol: Protocol::Https as i32,
                },
                TransportProtocol {
                    uri: "grpcs://peer.example.com:443".to_owned(),
                    protocol: Protocol::Grpc as i32,
                },
            ];

            let mut rig = StoreRig::new();
            rig.secrets
                .save(
                    secret_id,
                    channel_id,
                    SecretValue::PairingSecret(PairingKeyMaterial::from_secret(&initiator_secret)),
                )
                .await
                .expect("save pairing secret");
            rig.secrets
                .save(
                    secret_id,
                    channel_id,
                    SecretValue::PairingContact(contact_message.clone()),
                )
                .await
                .expect("save contact");

            // This responder serves only gRPCS.
            let lf = LocalFixture {
                own_transports: vec![TransportProtocol {
                    uri: "grpcs://me.example.com:443".to_owned(),
                    protocol: Protocol::Grpc as i32,
                }],
                ..LocalFixture::new(secret_id)
            };
            let pairing_cfg = PairingConfig {
                parameter_range: None,
                lifetime: Timeouts::default().pairing,
            };

            accept(
                &mut rig.stores(),
                &lf.local(),
                &pairing_cfg,
                channel_id,
                &pair_request,
                SenderKind::Owner,
                0,
            )
            .await
            .expect("accept must succeed when one offered endpoint is servable");

            // The library must hand the transport everything the peer
            // advertised, in the peer's order. Which of these to dial is the
            // application's call,
            // so this asserts what was *offered*, not what was chosen.
            assert_eq!(
                rig.transport.sent_endpoint_sets(),
                vec![vec![
                    TransportProtocol {
                        uri: "https://peer.example.com/derec".to_owned(),
                        protocol: Protocol::Https as i32,
                    },
                    TransportProtocol {
                        uri: "grpcs://peer.example.com:443".to_owned(),
                        protocol: Protocol::Grpc as i32,
                    },
                ]],
                "accept() must offer the transport every endpoint the peer \
                 advertised"
            );
        });
    }
}

#[cfg(test)]
mod fingerprint_gate_tests {
    use super::super::pair::completed_pairing_status;
    use crate::protocol::types::ChannelStatus;
    use derec_proto::{ContactMode, SenderKind};

    /// The two key-bearing modes are usable the moment the handshake lands:
    /// the contact carried the keys outright, or a commitment the scanner has
    /// already checked them against.
    #[test]
    fn helper_pairings_with_a_key_binding_are_paired_at_once() {
        for mode in [ContactMode::InlineKeys, ContactMode::HashedKeys] {
            for kind in [SenderKind::Owner, SenderKind::Helper] {
                assert_eq!(
                    completed_pairing_status(kind, Some(mode as i32)),
                    ChannelStatus::Paired,
                    "{kind:?} over {mode:?} must not wait for a fingerprint"
                );
            }
        }
    }

    /// `NoKeys` binds nothing to the contact, so the fingerprint is the only
    /// check that catches a substituted key and the channel stays inert until
    /// it passes — helper pairings included, which is what this gate changed.
    #[test]
    fn no_keys_pairings_wait_for_the_fingerprint() {
        for kind in [
            SenderKind::Owner,
            SenderKind::Helper,
            SenderKind::ReplicaSource,
            SenderKind::ReplicaDestination,
        ] {
            assert_eq!(
                completed_pairing_status(kind, Some(ContactMode::NoKeys as i32)),
                ChannelStatus::Pending,
                "{kind:?} over NoKeys must wait for a fingerprint"
            );
        }
    }

    /// Replica pairings are gated whatever the contact mode — admitting a
    /// second device to the group is a human decision on every path.
    #[test]
    fn replica_pairings_wait_regardless_of_mode() {
        for mode in [
            ContactMode::InlineKeys,
            ContactMode::HashedKeys,
            ContactMode::NoKeys,
        ] {
            for kind in [SenderKind::ReplicaSource, SenderKind::ReplicaDestination] {
                assert_eq!(
                    completed_pairing_status(kind, Some(mode as i32)),
                    ChannelStatus::Pending,
                    "{kind:?} over {mode:?} must wait for a fingerprint"
                );
            }
        }
    }

    /// The responder stores a contact only for `NoKeys`; the other modes leave
    /// a `PairingSecret` instead, so the mode reads as absent and the decision
    /// falls to the kind alone.
    #[test]
    fn an_absent_contact_leaves_the_decision_to_the_kind() {
        assert_eq!(
            completed_pairing_status(SenderKind::Helper, None),
            ChannelStatus::Paired
        );
        assert_eq!(
            completed_pairing_status(SenderKind::ReplicaDestination, None),
            ChannelStatus::Pending
        );
    }
}

/// Where the initiator's own roster row points once a replica handshake
/// completes.
#[cfg(test)]
mod own_member_channel_tests {
    use super::super::{build_communication_info, claim_own_member, pair, start};
    use crate::primitives::pairing::{request, response};
    use crate::protocol::context::PairingConfig;
    use crate::protocol::tests::{LocalFixture, StoreRig, run_async};
    use crate::protocol::types::{
        ChannelQuery, ChannelRecord, ChannelStatus, ReplicaMember, ReplicaRole, Timeouts,
    };
    use crate::protocol::{DeRecChannelStore, DeRecSecretStore, SecretKind, SecretValue};
    use crate::types::{ChannelId, ReplicaId};
    use derec_proto::{ContactMode, Protocol, SenderKind, TransportProtocol};
    use std::collections::HashMap;

    const SECRET_ID: u64 = 0x0A11_CE00;
    const OWN: u64 = 1001;
    const GROUP: ChannelId = ChannelId(0x6E0_0001);

    fn endpoint(host: &str) -> Vec<TransportProtocol> {
        vec![TransportProtocol {
            uri: format!("https://{host}.example/derec"),
            protocol: Protocol::Https as i32,
        }]
    }

    async fn own_channel(rig: &StoreRig) -> ChannelId {
        rig.channels
            .load(
                SECRET_ID,
                ChannelQuery::Replica {
                    channel_id: GROUP,
                    replica_id: ReplicaId(OWN),
                },
            )
            .await
            .expect("load")
            .and_then(|r| r.as_replica().cloned())
            .expect("own roster row present")
            .channel_id
    }

    /// Drives a full handshake in which this device scans a joiner's contact:
    /// `start`, the joiner's real `PairResponse`, then `on_response`. Returns
    /// the rekeyed channel the joiner was admitted on.
    async fn admit_joiner(
        rig: &mut StoreRig,
        lf: &LocalFixture,
        contact_channel: ChannelId,
        joiner_id: u64,
    ) -> ChannelId {
        let pairing_cfg = PairingConfig {
            parameter_range: None,
            lifetime: Timeouts::default().pairing,
        };
        let request::CreateContactResult {
            contact_message,
            secret_key,
        } = request::create_contact(
            contact_channel,
            ContactMode::InlineKeys,
            endpoint("joiner"),
            None,
        )
        .expect("create_contact");
        let joiner_secret = secret_key.expect("InlineKeys returns key material");

        let sent_before = rig.transport.sent_envelopes().len();
        start(
            &mut rig.stores(),
            &lf.local(),
            &pairing_cfg,
            SenderKind::ReplicaSource,
            contact_message,
            HashMap::new(),
            0,
        )
        .await
        .expect("start");
        let request_envelope = rig.transport.sent_envelopes()[sent_before].clone();

        let request::ExtractResult {
            request: pair_request,
        } = request::extract(&request_envelope, joiner_secret.ecies_secret_key(), None)
            .expect("joiner extracts the request");
        let produced = response::produce(
            contact_channel,
            &pair_request,
            &joiner_secret,
            build_communication_info(&HashMap::new(), Some(joiner_id)),
            None,
            lf.policy,
        )
        .expect("joiner produces the response");

        let Some(SecretValue::PairingSecret(own_secret)) = rig
            .secrets
            .load(SECRET_ID, contact_channel, SecretKind::PairingSecret)
            .await
            .expect("load pairing secret")
        else {
            panic!("start stores the initiator's pairing secret");
        };
        let own_secret = own_secret.to_secret().expect("pairing secret");
        let response::ExtractResult {
            response: pair_response,
        } = response::extract(&produced.envelope, own_secret.ecies_secret_key())
            .expect("initiator extracts the response");

        pair::on_response(
            &mut rig.stores(),
            &lf.local(),
            &pairing_cfg,
            contact_channel,
            &pair_response,
            &own_secret,
        )
        .await
        .expect("on_response");
        produced.channel_id
    }

    /// A member admitting a joiner keeps its row on the group channel: the
    /// rekeyed channel belongs to the joiner alone.
    #[test]
    fn an_existing_member_keeps_its_row_on_the_group_channel() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, OWN);
            let mut rig = StoreRig::new();
            rig.channels
                .save(
                    SECRET_ID,
                    ChannelRecord::Replica(ReplicaMember {
                        channel_id: GROUP,
                        replica_id: ReplicaId(OWN),
                        transports: lf.own_transports.clone(),
                        communication_info: HashMap::new(),
                        role: ReplicaRole::Source,
                        status: ChannelStatus::Paired,
                        created_at: 0,
                        handover_channel: None,
                    }),
                )
                .await
                .expect("seed own row");

            let joiner_channel = admit_joiner(&mut rig, &lf, ChannelId(0x0C0_0001), 2002).await;

            assert_ne!(joiner_channel, GROUP);
            assert_eq!(
                own_channel(&rig).await,
                GROUP,
                "admitting a joiner must not move this device off the group channel"
            );
        });
    }

    /// A first-time initiator holds no own row until it confirms a pairing;
    /// the first confirmed channel becomes the group's, and confirming a later
    /// admission leaves it there.
    #[test]
    fn joiners_admitted_in_sequence_keep_the_first_confirmed_group_channel() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, OWN);
            let mut rig = StoreRig::new();

            let first_contact = ChannelId(0x0C0_0002);
            let group = admit_joiner(&mut rig, &lf, first_contact, 2002).await;
            assert_ne!(group, first_contact);
            assert!(
                rig.channels
                    .member_rows
                    .lock()
                    .unwrap()
                    .get(&(SECRET_ID, OWN))
                    .is_none(),
                "a completed but unconfirmed pairing writes no own row"
            );
            claim_own_member(&mut rig.stores(), &lf.local(), group, ReplicaRole::Source)
                .await
                .expect("confirming claims the group channel");
            assert_eq!(own_channel(&rig).await, group);

            let second = admit_joiner(&mut rig, &lf, ChannelId(0x0C0_0003), 2003).await;
            assert_ne!(second, group);
            claim_own_member(&mut rig.stores(), &lf.local(), second, ReplicaRole::Source)
                .await
                .expect("confirming a later admission");
            assert_eq!(
                own_channel(&rig).await,
                group,
                "the second admission must keep the first group channel"
            );
        });
    }
}

#[cfg(test)]
mod refusal_memo_tests {
    use super::super::*;

    /// The memo is read by the requester, so its own range comes first and
    /// the responder's range is the accepted one.
    #[test]
    fn parameter_range_memo_reads_from_the_requester_side() {
        let err = PairingError::IncompatibleParameterRange {
            field: "shareSize",
            local_min: 1_000,
            local_max: 5_000,
            peer_min: 10,
            peer_max: 500,
        };
        assert_eq!(
            parameter_range_memo(&err),
            "`shareSize` range [10, 500] does not overlap the accepted range [1000, 5000]"
        );
    }
}

/// A pairing may not start on a channel identifier this device already uses:
/// the identifier is chosen by whoever created the contact, and a replica
/// group's identifier travels in the clear on every envelope of the group.
#[cfg(test)]
mod channel_in_use_tests {
    use super::super::*;
    use crate::protocol::DeRecSecretStore;
    use crate::protocol::tests::{LocalFixture, StoreRig, run_async};
    use crate::protocol::types::{
        ChannelRecord, ChannelStatus, ReplicaMember, ReplicaRole, SecretValue,
    };
    use crate::types::ReplicaId;

    const SECRET_ID: u64 = 42;
    const OWN_REPLICA_ID: u64 = 9;
    const GROUP: ChannelId = ChannelId(9000);

    async fn seed_member(rig: &mut StoreRig, replica_id: u64) {
        rig.channels
            .save(
                SECRET_ID,
                ChannelRecord::Replica(ReplicaMember {
                    channel_id: GROUP,
                    replica_id: ReplicaId(replica_id),
                    transports: Vec::new(),
                    communication_info: HashMap::new(),
                    role: ReplicaRole::Destination,
                    status: ChannelStatus::Paired,
                    created_at: 1_700_000_000,
                    handover_channel: None,
                }),
            )
            .await
            .expect("seed member");
    }

    async fn seed_key(rig: &mut StoreRig) {
        rig.secrets
            .save(SECRET_ID, GROUP, SecretValue::SharedKey([3u8; 32]))
            .await
            .expect("seed group key");
    }

    #[test]
    fn a_pairing_on_the_replica_group_channel_is_refused() {
        run_async(async {
            let lf = LocalFixture::with_replica(SECRET_ID, OWN_REPLICA_ID);
            let mut rig = StoreRig::new();
            seed_member(&mut rig, OWN_REPLICA_ID).await;
            seed_member(&mut rig, 8).await;
            seed_key(&mut rig).await;

            let err = reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), GROUP)
                .await
                .expect_err("the group channel is in use");
            assert!(
                matches!(err, Error::ChannelAlreadyPaired { channel_id } if channel_id == GROUP),
                "expected ChannelAlreadyPaired for the group channel, got {err:?}"
            );
        });
    }

    #[test]
    fn a_pairing_on_a_channel_holding_a_key_is_refused() {
        run_async(async {
            let lf = LocalFixture::new(SECRET_ID);
            let mut rig = StoreRig::new();
            seed_key(&mut rig).await;

            let err = reject_start_on_channel_in_use(&mut rig.stores(), &lf.local(), GROUP)
                .await
                .expect_err("a channel holding a key is in use");
            assert!(matches!(err, Error::ChannelAlreadyPaired { .. }));
        });
    }
}

/// A pairing has a lifetime, `Timeouts::pairing`, counted from the contact's
/// timestamp for the contact and from the request's timestamp for the
/// request. Past it, the scanner refuses to start and the creator neither
/// prompts, accepts nor answers.
#[cfg(test)]
mod pairing_expiry_tests {
    use crate::Error;
    use crate::extensions::channel_store::ChannelStoreExt as _;
    use crate::primitives::pairing::PairingError;
    use crate::protocol::builder::DeRecProtocolBuilder;
    use crate::protocol::tests::{
        InMemChannelStore, InMemPersistedStateStore, InMemSecretStore, InMemShareStore,
        InMemUserSecretStore, RecordingTransport, run_async,
    };
    use crate::protocol::types::HelperFilter;
    use crate::protocol::{
        AutoAcceptPolicy, DeRecEvent, DeRecFlow, DeRecProtocol, DeRecSecretStore, IgnoreReason,
        PendingAction, SecretKind, SecretValue, Timeouts,
    };
    use crate::types::ChannelId;
    use crate::utils::now_secs;
    use derec_proto::{ContactMessage, ContactMode, SenderKind};
    use prost_types::Timestamp;
    use std::collections::HashMap;
    use std::time::Duration;

    const SECRET_ID: u64 = 0x18;
    const CONTACT_CHANNEL: ChannelId = ChannelId(0x1818);
    const WINDOW: u64 = 300;

    type Device = DeRecProtocol<
        InMemChannelStore,
        InMemShareStore,
        InMemSecretStore,
        InMemUserSecretStore,
        InMemPersistedStateStore,
        RecordingTransport,
    >;

    fn device(uri: &str, auto_accept_pairing: bool) -> Device {
        DeRecProtocolBuilder::new(SECRET_ID)
            .with_channel_store(InMemChannelStore::default())
            .with_share_store(InMemShareStore::default())
            .with_secret_store(InMemSecretStore::default())
            .with_user_secret_store(InMemUserSecretStore::default())
            .with_transport(RecordingTransport::default())
            .with_state_store(InMemPersistedStateStore::default())
            .with_own_transports([uri])
            .with_timeouts(Timeouts {
                pairing: Duration::from_secs(WINDOW),
                ..Default::default()
            })
            .with_auto_accept(AutoAcceptPolicy {
                pairing: auto_accept_pairing,
                pre_pair: auto_accept_pairing,
                ..Default::default()
            })
            .build()
            .expect("device builds")
    }

    fn new_creator(auto_accept_pairing: bool) -> Device {
        device("https://helper.example", auto_accept_pairing)
    }

    fn new_scanner() -> Device {
        device("https://owner.example", true)
    }

    /// A timestamp `secs` seconds in the past.
    fn aged(secs: u64) -> Option<Timestamp> {
        Some(Timestamp {
            seconds: (now_secs() - secs) as i64,
            nanos: 0,
        })
    }

    async fn new_contact(creator: &mut Device, mode: ContactMode) -> ContactMessage {
        creator
            .create_contact(Some(CONTACT_CHANNEL), mode, Some(7))
            .await
            .expect("contact")
    }

    async fn scan(scanner: &mut Device, contact: ContactMessage) -> Result<u64, Error> {
        scanner
            .start(DeRecFlow::Pairing {
                kind: SenderKind::Owner,
                contact,
                peer_communication_info: HashMap::new(),
            })
            .await
            .map(|_| 0)
    }

    /// The envelope `device` sent last.
    fn last_sent(device: &Device) -> Vec<u8> {
        device
            .transport
            .sent_envelopes()
            .pop()
            .expect("an envelope was sent")
    }

    /// Rewrite the creator's stored copy of its contact as if it had been
    /// created `secs` seconds ago.
    async fn age_stored_contact(creator: &mut Device, secs: u64) {
        let Some(SecretValue::PairingContact(mut stored)) = creator
            .secret_store
            .load(SECRET_ID, CONTACT_CHANNEL, SecretKind::PairingContact)
            .await
            .expect("load")
        else {
            panic!("the creator keeps its contact");
        };
        stored.timestamp = aged(secs);
        creator
            .secret_store
            .save(
                SECRET_ID,
                CONTACT_CHANNEL,
                SecretValue::PairingContact(stored),
            )
            .await
            .expect("save");
    }

    async fn holds_pairing_material(device: &Device) -> bool {
        let secret = device
            .secret_store
            .load(SECRET_ID, CONTACT_CHANNEL, SecretKind::PairingSecret)
            .await
            .expect("load");
        let contact = device
            .secret_store
            .load(SECRET_ID, CONTACT_CHANNEL, SecretKind::PairingContact)
            .await
            .expect("load");
        secret.is_some() || contact.is_some()
    }

    async fn holds_no_channel(device: &Device) -> bool {
        device
            .channel_store
            .helpers_matching(SECRET_ID, HelperFilter::default())
            .await
            .expect("helpers")
            .is_empty()
    }

    fn pairing_action(events: &[DeRecEvent]) -> PendingAction {
        events
            .iter()
            .find_map(|event| match event {
                DeRecEvent::ActionRequired { action, .. } => Some(action.clone()),
                _ => None,
            })
            .unwrap_or_else(|| panic!("expected ActionRequired, got {events:?}"))
    }

    fn is_expired(error: &Error) -> bool {
        matches!(error, Error::Pairing(PairingError::Expired(_)))
    }

    fn ignored_as_expired(events: &[DeRecEvent]) -> bool {
        events.iter().any(|event| {
            matches!(
                event,
                DeRecEvent::MessageIgnored {
                    channel_id: CONTACT_CHANNEL,
                    reason: IgnoreReason::Expired,
                    ..
                }
            )
        })
    }

    /// The auditors' scenario: the creator's user accepts after the window.
    /// Nothing is recorded and nothing is sent, and the contact stays usable,
    /// so the scanner simply starts again.
    #[test]
    fn a_late_accept_is_refused_and_a_retry_pairs() {
        run_async(async {
            let mut creator = new_creator(false);
            let mut scanner = new_scanner();
            let contact = new_contact(&mut creator, ContactMode::InlineKeys).await;

            scan(&mut scanner, contact.clone()).await.expect("scan");
            let events = creator.process(&last_sent(&scanner)).await.expect("prompt");
            let mut action = pairing_action(&events);
            let PendingAction::Pairing { request, .. } = &mut action else {
                panic!("expected a pairing action, got {action:?}");
            };
            request.timestamp = aged(WINDOW + 1);

            let error = creator.accept(action).await.expect_err("too late");

            assert!(is_expired(&error), "got {error:?}");
            assert!(
                creator.transport.sent_envelopes().is_empty(),
                "nothing is sent"
            );
            assert!(holds_no_channel(&creator).await, "no channel is recorded");
            assert!(
                holds_pairing_material(&creator).await,
                "the contact is still valid"
            );

            scan(&mut scanner, contact).await.expect("scan again");
            let events = creator.process(&last_sent(&scanner)).await.expect("prompt");
            let events = creator
                .accept(pairing_action(&events))
                .await
                .expect("accept");
            assert!(
                events
                    .iter()
                    .any(|e| matches!(e, DeRecEvent::PairingCompleted { .. })),
                "the retry pairs: {events:?}"
            );
        });
    }

    /// The contact expires while the prompt waits: the accept is refused and
    /// the contact's material goes with it.
    #[test]
    fn an_accept_on_an_expired_contact_is_refused() {
        run_async(async {
            let mut creator = new_creator(false);
            let mut scanner = new_scanner();
            let contact = new_contact(&mut creator, ContactMode::InlineKeys).await;

            scan(&mut scanner, contact).await.expect("scan");
            let events = creator.process(&last_sent(&scanner)).await.expect("prompt");
            age_stored_contact(&mut creator, WINDOW + 1).await;

            let error = creator
                .accept(pairing_action(&events))
                .await
                .expect_err("expired contact");

            assert!(is_expired(&error), "got {error:?}");
            assert!(
                creator.transport.sent_envelopes().is_empty(),
                "nothing is sent"
            );
            assert!(holds_no_channel(&creator).await, "no channel is recorded");
            assert!(
                !holds_pairing_material(&creator).await,
                "an expired contact's material is dropped"
            );
        });
    }

    /// A request on an expired contact is ignored on arrival, before any
    /// prompt, whether pairing is accepted by hand or automatically.
    #[test]
    fn a_request_on_an_expired_contact_is_ignored() {
        run_async(async {
            for auto_accept in [false, true] {
                let mut creator = new_creator(auto_accept);
                let mut scanner = new_scanner();
                let contact = new_contact(&mut creator, ContactMode::InlineKeys).await;
                age_stored_contact(&mut creator, WINDOW + 1).await;

                scan(&mut scanner, contact).await.expect("scan");
                let events = creator
                    .process(&last_sent(&scanner))
                    .await
                    .expect("ignored, not an error");

                assert!(
                    ignored_as_expired(&events),
                    "auto_accept={auto_accept}: got {events:?}"
                );
                assert!(
                    creator.transport.sent_envelopes().is_empty(),
                    "auto_accept={auto_accept}: nothing is sent"
                );
                assert!(
                    holds_no_channel(&creator).await,
                    "auto_accept={auto_accept}: no channel is recorded"
                );
                assert!(
                    !holds_pairing_material(&creator).await,
                    "auto_accept={auto_accept}: the expired contact's material is dropped"
                );
            }
        });
    }

    /// The `PrePair` leg follows the same rules: a request on an expired
    /// contact is ignored, and a late accept is refused.
    #[test]
    fn the_pre_pair_leg_expires_like_the_pair_leg() {
        run_async(async {
            for mode in [ContactMode::HashedKeys, ContactMode::NoKeys] {
                let mut creator = new_creator(false);
                let mut scanner = new_scanner();
                let contact = new_contact(&mut creator, mode).await;
                age_stored_contact(&mut creator, WINDOW + 1).await;
                scan(&mut scanner, contact).await.expect("scan");
                let events = creator
                    .process(&last_sent(&scanner))
                    .await
                    .expect("ignored");
                assert!(ignored_as_expired(&events), "{mode:?}: got {events:?}");
                assert!(creator.transport.sent_envelopes().is_empty(), "{mode:?}");

                let mut creator = new_creator(false);
                let mut scanner = new_scanner();
                let contact = new_contact(&mut creator, mode).await;
                scan(&mut scanner, contact).await.expect("scan");
                let events = creator.process(&last_sent(&scanner)).await.expect("prompt");
                let mut action = pairing_action(&events);
                let PendingAction::PrePair { request, .. } = &mut action else {
                    panic!("{mode:?}: expected a pre-pair action, got {action:?}");
                };
                request.timestamp = aged(WINDOW + 1);
                let error = creator.accept(action).await.expect_err("too late");
                assert!(is_expired(&error), "{mode:?}: got {error:?}");
                assert!(creator.transport.sent_envelopes().is_empty(), "{mode:?}");
            }
        });
    }

    /// The scanner refuses a contact older than the window before storing or
    /// sending anything.
    #[test]
    fn the_scanner_refuses_an_expired_contact() {
        run_async(async {
            for mode in [
                ContactMode::InlineKeys,
                ContactMode::HashedKeys,
                ContactMode::NoKeys,
            ] {
                let mut creator = new_creator(true);
                let mut scanner = new_scanner();
                let mut contact = new_contact(&mut creator, mode).await;
                contact.timestamp = aged(WINDOW + 1);

                let error = scan(&mut scanner, contact).await.expect_err("expired");

                assert!(is_expired(&error), "{mode:?}: got {error:?}");
                assert!(scanner.transport.sent_envelopes().is_empty(), "{mode:?}");
                assert!(holds_no_channel(&scanner).await, "{mode:?}");
                assert!(!holds_pairing_material(&scanner).await, "{mode:?}");
            }
        });
    }

    /// A contact without a timestamp cannot be shown to be fresh.
    #[test]
    fn the_scanner_refuses_a_contact_without_a_timestamp() {
        run_async(async {
            let mut creator = new_creator(true);
            let mut scanner = new_scanner();
            let mut contact = new_contact(&mut creator, ContactMode::InlineKeys).await;
            contact.timestamp = None;

            let error = scan(&mut scanner, contact).await.expect_err("no timestamp");

            assert!(
                matches!(
                    error,
                    Error::Pairing(PairingError::InvalidContactMessage(_))
                ),
                "got {error:?}"
            );
            assert!(scanner.transport.sent_envelopes().is_empty());
        });
    }

    /// A contact just inside the window still pairs.
    #[test]
    fn a_contact_inside_the_window_pairs() {
        run_async(async {
            let mut creator = new_creator(true);
            let mut scanner = new_scanner();
            let mut contact = new_contact(&mut creator, ContactMode::InlineKeys).await;
            contact.timestamp = aged(WINDOW - 5);
            age_stored_contact(&mut creator, WINDOW - 5).await;

            scan(&mut scanner, contact).await.expect("scan");
            let events = creator.process(&last_sent(&scanner)).await.expect("pairs");

            assert!(
                events
                    .iter()
                    .any(|e| matches!(e, DeRecEvent::PairingCompleted { .. })),
                "got {events:?}"
            );
        });
    }
}