darkbio-wire 0.17.0

Encrypted protocol between Ark and host
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
// wire-rs: encrypted protocol between Ark and host
// Copyright 2026 Dark Bio AG. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//! Server scenario tests driven by mock client scripts.

use super::*;
use crate::testing;

/// Runs a script with logging enabled.
fn run_logged(steps: &[Step]) -> Summary {
    testing::init_tracing();
    run(steps)
}

/// Tests a successful handshake followed by two request and reply exchanges.
#[test]
fn test_scripted_round_trip() {
    let summary = run_logged(&[
        Step::ResetPair,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
        Step::Request(2),
    ]);
    assert_eq!(
        summary,
        Summary {
            state: State::Established,
            dropped: 0,
            fragments: 0,
            handshakes: 1,
            delivered: 2,
            replies: 2,
            reads: 5,
        }
    );
}

/// Tests that every connection supplies a working sender and that a reset
/// refuses the retained one.
#[test]
fn test_scripted_retained_sender() {
    let summary = run_logged(&[
        // Retaining before any session leaves no sender to use
        Step::Retain,
        Step::SendRetained(0),
        Step::SendOversized,
        // The first session's sender works before any request
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Send(1),
        // A retained copy of that sender works as well
        Step::Retain,
        Step::SendRetained(2),
        Step::Request(3),
        // A reset ends the session, refusing the retained sender
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::SendRetained(4),
        Step::Send(5),
        // Retaining the new sender restores retained sends
        Step::Retain,
        Step::SendRetained(6),
        Step::Request(7),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 2);
    assert_eq!(summary.replies, 6);
    assert_eq!(summary.dropped, 0);
}

/// Tests that a local disconnect emits an empty frame and ends the session's
/// senders without a disconnect event.
#[test]
fn test_scripted_local_disconnect() {
    let summary = run_logged(&[
        // Open a session and keep a copy of its sender
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
        Step::Retain,
        // Disconnecting ends both senders before the call returns
        Step::Disconnect,
        Step::SendRetained(2),
        Step::Send(3),
        // A new handshake on the same stream supplies a working sender
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::SendRetained(4),
        Step::Send(5),
        Step::Request(6),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 2);
    assert_eq!(summary.replies, 3);
    assert_eq!(summary.dropped, 1);
}

/// Tests that an oversized send keeps the session, while a failed send through
/// a retained sender ends it.
#[test]
fn test_scripted_retained_sender_failures() {
    let summary = run_logged(&[
        // An oversized send leaves both directions of the session usable
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::SendOversized,
        Step::Send(1),
        Step::Request(2),
        // A failed send through the retained sender ends the session for good
        Step::Retain,
        Step::Break,
        Step::SendRetained(3),
        Step::Heal,
        Step::SendRetained(4),
        // A new handshake supplies a fresh sender, and the old one stays refused
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::SendRetained(5),
        Step::Send(6),
        Step::Request(7),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 2);
    assert_eq!(summary.replies, 4);
    assert_eq!(summary.dropped, 1);
}

/// Tests that owner actions met during a handshake read abort that handshake,
/// like other read interruptions.
#[test]
fn test_scripted_actions_during_handshake() {
    let summary = run_logged(&[
        // An action while waiting for HostAck aborts that handshake
        Step::Reset,
        Step::Hello,
        Step::Retain,
        Step::Send(1),
        // Disconnects work without a session, even on a broken writer
        Step::Disconnect,
        Step::Break,
        Step::Disconnect,
        Step::Heal,
        Step::Disconnect,
        // A fresh handshake still succeeds afterwards
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Send(2),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.replies, 1);
    assert_eq!(summary.dropped, 3);

    // Actions between the reset's disconnect event and the next receive do
    // not cancel the handshake already scheduled by that reset
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Retain,
        Step::Reset,
        Step::Disconnect,
        Step::SendRetained(1),
        Step::Hello,
        Step::Ack,
        Step::Send(2),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.replies, 1);
    assert_eq!(summary.dropped, 1);
}

/// Tests that a reset starts a handshake in every state without a wire reply.
#[test]
fn test_scripted_reset_restarts() {
    // Repeated resets before a handshake draw no reply
    let summary = run_logged(&[
        Step::Reset,
        Step::Reset,
        Step::Reset,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 0);

    // A reset while waiting for HostAck restarts the handshake
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Reset,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.dropped, 0);

    // A reset pair inside a session starts the next handshake
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::ResetPair,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 0);

    // A reset ends the session, so a request after it is refused
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Reset,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 0);
    assert_eq!(summary.dropped, 1);
}

/// Tests that handshake frames are refused in the wrong state.
#[test]
fn test_scripted_frames_outside_state() {
    // A HostHello needs a preceding reset
    let summary = run_logged(&[Step::Hello]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 0);
    assert_eq!(summary.dropped, 1);

    // A second HostHello while waiting for HostAck aborts the handshake
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Hello]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // A HostAck needs a pending ArkHello
    let summary = run_logged(&[Step::Reset, Step::Ack]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // A request during the handshake aborts it
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Request(1)]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 1);

    // A HostHello inside a session ends it
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Ack, Step::Hello]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);
}

/// Tests that invalid handshake crypto and tampered requests are refused with a
/// session-end signal.
#[test]
fn test_scripted_bad_crypto_frames() {
    // A HostHello with an invalid key fails the handshake
    let summary = run_logged(&[Step::Reset, Step::HelloBadKey]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 0);
    assert_eq!(summary.dropped, 1);

    // Each flawed HostAck fails the handshake too
    for step in [
        Step::AckTampered,
        Step::AckBadAuth,
        Step::AckBadSigner,
        Step::AckBadPayload,
        Step::AckBadEncap,
    ] {
        let summary = run_logged(&[Step::Reset, Step::Hello, step.clone()]);
        assert_eq!(summary.state, State::Idle, "{step:?}");
        assert_eq!(summary.handshakes, 1, "{step:?}");
        assert_eq!(summary.dropped, 1, "{step:?}");

        // Without a pending ArkHello, the mock sends junk instead of an ack
        let summary = run_logged(&[Step::Reset, step.clone()]);
        assert_eq!(summary.state, State::Idle, "{step:?}");
        assert_eq!(summary.dropped, 1, "{step:?}");
    }

    // A tampered request ends the session, so the next request is refused too
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::RequestTampered,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 0);
    assert_eq!(summary.dropped, 2);
}

/// Tests that replayed HostAcks and requests are refused, while a replayed
/// HostHello works with a fresh ack.
#[test]
fn test_scripted_replays() {
    // A replayed HostAck inside a session ends it
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Ack, Step::AckReplay]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // A replayed request ends the session too
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
        Step::RequestReplay,
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 1);

    // An old HostAck cannot complete a new handshake
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Reset,
        Step::Hello,
        Step::AckReplay,
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.dropped, 1);

    // A replayed HostHello after a reset needs a fresh ack for the new ArkHello
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Reset,
        Step::HelloReplay,
        Step::Ack,
        Step::Request(3),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 0);
}

/// Tests that the transport delivers decrypted bytes without a protobuf check
/// and keeps the session usable.
#[test]
fn test_scripted_garbage_keeps_session() {
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Garbage,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 0);
}

/// Tests that invalid input in any state returns the server to idle with an
/// empty frame.
#[test]
fn test_scripted_junk_signals_dropped() {
    let junk = || Step::Junk(vec![0xde, 0xad, 0xbe, 0xef]);

    // Refuse junk while idle
    let summary = run_logged(&[junk()]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // Refuse junk while waiting for HostHello
    let summary = run_logged(&[Step::Reset, junk()]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // Refuse junk while waiting for HostAck
    let summary = run_logged(&[Step::Reset, Step::Hello, junk()]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 1);

    // Refuse junk inside a session
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Ack, junk()]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // Undecodable COBS is junk
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Junk(vec![0xff, 0x01]),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // So is a nonempty frame encoding the empty packet
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Ack, Step::Junk(vec![])]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // A fresh handshake recovers from junk inside a session
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        junk(),
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 1);

    // It also recovers from junk during a handshake
    let summary = run_logged(&[
        Step::Reset,
        junk(),
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 1);
}

/// Tests that each request after the session ends draws its own empty frame.
///
/// A client with many requests already in flight must drain that backlog of
/// signals.
#[test]
fn test_scripted_requests_into_dead_session() {
    // End the session with junk, then keep sending requests into it
    let stale = 40;
    let mut steps = vec![
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Junk(vec![0xde, 0xad]),
    ];
    steps.extend((0..stale).map(Step::Request));
    let summary = run_logged(&steps);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 0);
    assert_eq!(summary.dropped, usize::from(stale) + 1);
}

/// Tests that truncated copies of valid frames are refused as junk.
#[test]
fn test_scripted_truncated_frames() {
    for cut in [0u8, 1, 7, 255] {
        // Truncate the HostHello while the server waits for HostAck
        let summary = run_logged(&[Step::Reset, Step::Hello, Step::Truncated(cut)]);
        assert_eq!(summary.state, State::Idle, "cut {cut}");
        assert_eq!(summary.dropped, 1, "cut {cut}");

        // Truncate a request inside a session
        let summary = run_logged(&[
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
            Step::Truncated(cut),
        ]);
        assert_eq!(summary.state, State::Idle, "cut {cut}");
        assert_eq!(summary.dropped, 1, "cut {cut}");
    }
}

/// Tests that input after an unterminated frame either completes it or merges
/// with it into junk.
#[test]
fn test_scripted_partial_frames() {
    // The delimiter completes an idle partial hello instead of resetting, so
    // both hellos arrive without a reset and are refused
    let summary = run_logged(&[Step::Partial, Step::Reset, Step::Hello]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 0);
    assert_eq!(summary.dropped, 2);

    // A lone delimiter completes a partial hello after a reset
    let summary = run_logged(&[Step::Reset, Step::Partial, Step::Reset, Step::Ack]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 0);

    // The first zero of a reset pair completes the hello, and the second
    // starts a new handshake
    let summary = run_logged(&[
        Step::Reset,
        Step::Partial,
        Step::ResetPair,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.dropped, 0);

    // Two partial hellos merge into junk
    let summary = run_logged(&[Step::Reset, Step::Partial, Step::Partial, Step::Reset]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 1);

    // Without a prior reset, only the pair's second zero starts a handshake
    let summary = run_logged(&[Step::Partial, Step::ResetPair, Step::Hello, Step::Ack]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.dropped, 1);

    // A request merged into a partial hello is junk that ends the session
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Partial,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 0);
    assert_eq!(summary.dropped, 1);
}

/// Tests that a frame encoding the empty packet completes a partial hello
/// whose encoding ends in a full run.
///
/// That lone `0x01` then decodes to nothing, and for any other hello the
/// merge is junk.
#[test]
fn test_scripted_partial_hello_empty_packet() {
    // Pair a reset, a partial hello and an empty packet as often as a script allows
    let rounds = MAX_STEPS / 3;
    let steps: Vec<Step> = (0..rounds)
        .flat_map(|_| [Step::Reset, Step::Partial, Step::Junk(vec![])])
        .collect();

    // Rerun with fresh keys until one hello ends in a full run and completes
    for run in 0..1000 {
        let summary = run_logged(&steps);
        assert_eq!(summary.handshakes + summary.dropped, rounds, "run {run}");
        if summary.handshakes > 0 {
            return;
        }
    }
    panic!("no partial hello ended in a full run");
}

/// Tests that a `WouldBlock` read aborts an unfinished handshake without a
/// signal and keeps an established session.
#[test]
fn test_scripted_yield() {
    // A yield while idle leaves no session for the driver's probe
    let summary = run_logged(&[Step::Yield]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.replies, 0);

    // A yield while waiting for HostHello aborts the handshake
    let summary = run_logged(&[Step::Reset, Step::Yield, Step::Hello]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 0);
    assert_eq!(summary.dropped, 1);

    // A yield while waiting for HostAck aborts it too
    let summary = run_logged(&[Step::Reset, Step::Hello, Step::Yield, Step::Ack]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 1);

    // A yield inside a session keeps it, and the driver's probe gets through
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Yield,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.replies, 2);
}

/// Tests that persistent output failure ends the session or handshake it hits,
/// losing the failure signals as well.
#[test]
fn test_scripted_broken_transport() {
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
        // A failed reply ends the session, and every signal after it is lost
        Step::Break,
        Step::Request(2),
        Step::Request(3),
        // Once writes recover, the next output leads with a recovery delimiter
        Step::Heal,
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(4),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 3);
    assert_eq!(summary.replies, 2);
    assert_eq!(summary.dropped, 1);

    // A failed ArkHello aborts the handshake, and its signal is lost too
    let summary = run_logged(&[
        Step::Reset,
        Step::Break,
        Step::Hello,
        Step::Heal,
        Step::Ack,
        Step::Reset,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.dropped, 2);

    // A failed probe ends the session as well, and the next signal leads with
    // a recovery delimiter
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Break,
        Step::Yield,
        Step::Heal,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.delivered, 0);
    assert_eq!(summary.replies, 0);
    assert_eq!(summary.dropped, 2);
}

/// Tests that early adapter read timeouts leave every phase intact, from idle
/// to an established session.
///
/// They produce no notification or extra session transition.
#[test]
fn test_scripted_read_timeouts() {
    let summary = run_logged(&[
        Step::ReadTimeout,
        Step::Reset,
        Step::ReadTimeout,
        Step::Hello,
        Step::ReadTimeout,
        Step::Ack,
        Step::ReadTimeout,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.replies, 1);
    assert_eq!(summary.dropped, 0);
}

/// Tests an oversized frame arriving one byte per read, which ends the session
/// until a fresh handshake.
///
/// Advancing the mock input must not copy the unread frame of over 2 MiB for
/// every byte.
#[test]
fn test_scripted_bytewise_oversized_frame() {
    let summary = run_logged(&[
        // Open a session over single-byte reads and keep its sender
        Step::Chunk(1),
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Retain,
        // The oversized frame ends the session and refuses the retained sender
        Step::Oversized,
        Step::SendRetained(1),
        // A fresh handshake recovers the stream
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(2),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 1);
    assert!(summary.reads > MAX_FRAME_SIZE);
}

/// Tests that ArkHello write and flush timeouts fail the receive without a
/// failure notification.
#[test]
fn test_scripted_handshake_timeouts() {
    for point in [
        CutPoint::Start,
        CutPoint::Middle(7),
        CutPoint::Delimiter,
        CutPoint::Flush,
    ] {
        let summary = run_logged(&[
            // Time out the ArkHello, leaving no budget for a notification
            Step::Reset,
            Step::Timeout(point),
            Step::Hello,
            // The next handshake terminates whatever prefix reached the client
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
        ]);
        assert_eq!(summary.state, State::Established, "{point:?}");
        assert_eq!(summary.delivered, 1, "{point:?}");
        assert_eq!(summary.replies, 1, "{point:?}");
        assert_eq!(
            summary.dropped,
            usize::from(matches!(point, CutPoint::Start | CutPoint::Flush)),
            "{point:?}"
        );
    }
}

/// Tests that an expired send ends its session without a notification write.
#[test]
fn test_scripted_send_timeouts() {
    for point in [
        CutPoint::Start,
        CutPoint::Middle(7),
        CutPoint::Delimiter,
        CutPoint::Flush,
    ] {
        let summary = run_logged(&[
            // Open a session and keep a copy of its sender
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Retain,
            // The expired send starts no notification, and later retained
            // sends emit nothing
            Step::Timeout(point),
            Step::Send(1),
            Step::SendRetained(2),
            Step::SendRetained(3),
            // Only the next handshake's recovery delimiter ends any old prefix
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(4),
        ]);
        assert_eq!(summary.state, State::Established, "{point:?}");
        assert_eq!(summary.delivered, 1, "{point:?}");
        assert_eq!(
            summary.dropped,
            usize::from(matches!(point, CutPoint::Start | CutPoint::Flush)),
            "{point:?}"
        );
        assert_eq!(
            summary.replies,
            1 + usize::from(matches!(point, CutPoint::Delimiter | CutPoint::Flush)),
            "{point:?}"
        );
    }
}

/// Tests cuts in an ArkHello that carries a pending recovery delimiter.
///
/// Offset zero accepts only the recovery delimiter, while positive offsets
/// leave a fragment. The cut must fire even when ordinary writes are also
/// configured to fail.
#[test]
fn test_scripted_recovery_prefix_cuts() {
    for offset in [0, 1, u16::MAX] {
        for broken in [false, true] {
            let summary = run_logged(&[
                // Leave a recovery delimiter pending behind an expired send
                Step::Reset,
                Step::Hello,
                Step::Ack,
                Step::Timeout(CutPoint::Start),
                Step::Send(1),
                // Cut the ArkHello that carries that delimiter
                Step::Cut {
                    point: CutPoint::Middle(offset),
                    then_broken: broken,
                },
                Step::Reset,
                Step::Hello,
                // Recover with a fresh handshake once writes work
                Step::Heal,
                Step::Reset,
                Step::Hello,
                Step::Ack,
                Step::Request(2),
            ]);
            assert_eq!(
                summary.state,
                State::Established,
                "offset {offset}, broken {broken}"
            );
            assert_eq!(summary.handshakes, 2, "offset {offset}, broken {broken}");
            assert_eq!(summary.delivered, 1, "offset {offset}, broken {broken}");
            assert_eq!(
                summary.fragments,
                usize::from(offset != 0),
                "offset {offset}, broken {broken}"
            );
            assert_eq!(
                summary.dropped,
                1 + usize::from(offset == 0) + usize::from(!broken),
                "offset {offset}, broken {broken}"
            );
        }
    }
}

/// Tests cuts that hit a recovery delimiter and session-end signal written
/// together.
///
/// `Delimiter` accepts only the recovery delimiter, while `Flush` accepts both
/// zeros before failing.
#[test]
fn test_scripted_recovery_signal_cuts() {
    for point in [CutPoint::Delimiter, CutPoint::Flush] {
        let summary = run_logged(&[
            // Leave a recovery delimiter pending behind an expired send
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Timeout(CutPoint::Start),
            Step::Send(1),
            // Cut the delimiter and signal that answer the junk
            Step::Cut {
                point,
                then_broken: false,
            },
            Step::Junk(vec![0xde, 0xad]),
            // The next handshake recovers the same stream
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(2),
        ]);
        assert_eq!(summary.state, State::Established, "{point:?}");
        assert_eq!(summary.handshakes, 2, "{point:?}");
        assert_eq!(summary.delivered, 1, "{point:?}");
        assert_eq!(
            summary.dropped,
            2 + usize::from(point == CutPoint::Flush),
            "{point:?}"
        );
    }
}

/// Tests reply failures at each output boundary.
///
/// The recovery delimiter before the failure signal terminates any partial
/// body. A complete body becomes a valid reply, and without a pending body the
/// delimiter creates an extra empty frame.
#[test]
fn test_scripted_cut_replies() {
    /// Expected wire output after a reply fails at this boundary.
    struct TestCase {
        /// Point where the reply's write fails.
        point: CutPoint,
        /// Cut frames the server leaves behind.
        fragments: usize,
        /// Replies that reach the client.
        replies: usize,
        /// Empty frames the server emits.
        dropped: usize,
    }
    let tests = [
        TestCase {
            point: CutPoint::Middle(7),
            fragments: 1,
            replies: 0,
            dropped: 1,
        },
        TestCase {
            point: CutPoint::Delimiter,
            fragments: 0,
            replies: 1,
            dropped: 1,
        },
        TestCase {
            point: CutPoint::Start,
            fragments: 0,
            replies: 0,
            dropped: 2,
        },
        TestCase {
            point: CutPoint::Flush,
            fragments: 0,
            replies: 1,
            dropped: 2,
        },
    ];

    // Cut the reply to a single request at each boundary
    for (i, tt) in tests.into_iter().enumerate() {
        let summary = run_logged(&[
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Cut {
                point: tt.point,
                then_broken: false,
            },
            Step::Request(1),
        ]);
        assert_eq!(summary.state, State::Idle, "test {i}");
        assert_eq!(summary.delivered, 1, "test {i}");
        assert_eq!(summary.fragments, tt.fragments, "test {i}");
        assert_eq!(summary.replies, tt.replies, "test {i}");
        assert_eq!(summary.dropped, tt.dropped, "test {i}");
    }
}

/// Tests a partial reply whose failure signal is lost as well.
///
/// Its bytes stay pending until writes recover. The next ArkHello's recovery
/// delimiter must terminate them before the new response begins.
#[test]
fn test_scripted_cut_then_broken() {
    /// Expected output after a cut reply and a later successful reconnect.
    struct TestCase {
        /// Point where the reply's write fails.
        point: CutPoint,
        /// Cut frames the server leaves behind.
        fragments: usize,
        /// Replies that reach the client.
        replies: usize,
        /// Empty frames the server emits.
        dropped: usize,
    }
    let tests = [
        TestCase {
            point: CutPoint::Middle(7),
            fragments: 1,
            replies: 1,
            dropped: 0,
        },
        TestCase {
            point: CutPoint::Delimiter,
            fragments: 0,
            replies: 2,
            dropped: 0,
        },
        TestCase {
            point: CutPoint::Start,
            fragments: 0,
            replies: 1,
            dropped: 1,
        },
        TestCase {
            point: CutPoint::Flush,
            fragments: 0,
            replies: 2,
            dropped: 1,
        },
    ];

    // Cut the reply at each boundary, then heal and reconnect
    for (i, tt) in tests.into_iter().enumerate() {
        let summary = run_logged(&[
            Step::Reset,
            Step::Hello,
            Step::Ack,
            // Cut the reply to a request and break every write after it
            Step::Cut {
                point: tt.point,
                then_broken: true,
            },
            Step::Request(1),
            // Heal and reconnect, which terminates the pending bytes
            Step::Heal,
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(2),
        ]);
        assert_eq!(summary.state, State::Established, "test {i}");
        assert_eq!(summary.handshakes, 2, "test {i}");
        assert_eq!(summary.delivered, 2, "test {i}");
        assert_eq!(summary.fragments, tt.fragments, "test {i}");
        assert_eq!(summary.replies, tt.replies, "test {i}");
        assert_eq!(summary.dropped, tt.dropped, "test {i}");
    }
}

/// Tests failed ArkHello output and the refused ack that follows it.
///
/// The recovery delimiter before the failure signal completes any pending
/// body. Even when that produces a valid ArkHello, the server has already
/// abandoned its handshake.
#[test]
fn test_scripted_cut_handshakes() {
    /// Expected output after ArkHello fails and the client attempts an ack.
    struct TestCase {
        /// Point where the ArkHello's write fails.
        point: CutPoint,
        /// ArkHellos that reach the client.
        handshakes: usize,
        /// Cut frames the server leaves behind.
        fragments: usize,
        /// Empty frames the server emits.
        dropped: usize,
    }
    let tests = [
        TestCase {
            point: CutPoint::Middle(3),
            handshakes: 0,
            fragments: 1,
            dropped: 2,
        },
        TestCase {
            point: CutPoint::Delimiter,
            handshakes: 1,
            fragments: 0,
            dropped: 2,
        },
        TestCase {
            point: CutPoint::Start,
            handshakes: 0,
            fragments: 0,
            dropped: 3,
        },
        TestCase {
            point: CutPoint::Flush,
            handshakes: 1,
            fragments: 0,
            dropped: 3,
        },
    ];
    for (i, tt) in tests.into_iter().enumerate() {
        // The ack after a failed ArkHello is refused, whatever reached the client
        let cut = Step::Cut {
            point: tt.point,
            then_broken: false,
        };
        let summary = run_logged(&[Step::Reset, cut.clone(), Step::Hello, Step::Ack]);
        assert_eq!(summary.state, State::Idle, "test {i}");
        assert_eq!(summary.handshakes, tt.handshakes, "test {i}");
        assert_eq!(summary.fragments, tt.fragments, "test {i}");
        assert_eq!(summary.dropped, tt.dropped, "test {i}");

        // A fresh reset recovers the stream
        let summary = run_logged(&[
            Step::Reset,
            cut,
            Step::Hello,
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
        ]);
        assert_eq!(summary.state, State::Established, "test {i}");
        assert_eq!(summary.handshakes, tt.handshakes + 1, "test {i}");
        assert_eq!(summary.delivered, 1, "test {i}");
    }
}

/// Tests that body cuts skip a lone signal delimiter and stay armed for the
/// next frame.
#[test]
fn test_scripted_cut_signals() {
    /// Expected output when a body cut skips a signal and reaches ArkHello.
    struct TestCase {
        /// Point where the armed cut fires.
        point: CutPoint,
        /// ArkHellos that reach the client.
        handshakes: usize,
        /// Cut frames the server leaves behind.
        fragments: usize,
        /// Empty frames the server emits.
        dropped: usize,
    }
    let tests = [
        TestCase {
            point: CutPoint::Middle(1),
            handshakes: 1,
            fragments: 1,
            dropped: 2,
        },
        TestCase {
            point: CutPoint::Delimiter,
            handshakes: 2,
            fragments: 0,
            dropped: 2,
        },
        TestCase {
            point: CutPoint::Flush,
            handshakes: 2,
            fragments: 0,
            dropped: 3,
        },
    ];

    // A body cut skips the signal answering the junk and hits the next ArkHello
    for (i, tt) in tests.into_iter().enumerate() {
        let summary = run_logged(&[
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Cut {
                point: tt.point,
                then_broken: false,
            },
            Step::Junk(vec![1]),
            Step::Reset,
            Step::Hello,
        ]);
        assert_eq!(summary.state, State::Idle, "test {i}");
        assert_eq!(summary.handshakes, tt.handshakes, "test {i}");
        assert_eq!(summary.fragments, tt.fragments, "test {i}");
        assert_eq!(summary.dropped, tt.dropped, "test {i}");
    }

    // A cut at `Start` refuses the signal itself, so the next output leads
    // with a recovery delimiter
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Cut {
            point: CutPoint::Start,
            then_broken: false,
        },
        Step::Junk(vec![1]),
        Step::Reset,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.dropped, 1);
}

/// Tests frame assembly across reads as small as one byte.
#[test]
fn test_scripted_chunked_reads() {
    for chunk in [1u8, 7, 254, 255] {
        // Assemble handshakes, requests and a long junk frame from small reads
        let summary = run_logged(&[
            Step::Chunk(chunk),
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
            Step::Junk(vec![1; 300]),
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(2),
        ]);
        assert_eq!(summary.state, State::Established, "chunk {chunk}");
        assert_eq!(summary.delivered, 2, "chunk {chunk}");
        assert_eq!(summary.dropped, 1, "chunk {chunk}");

        // Complete a partial hello with a delimiter from a later step
        let summary = run_logged(&[
            Step::Chunk(chunk),
            Step::Reset,
            Step::Partial,
            Step::Reset,
            Step::Ack,
        ]);
        assert_eq!(summary.state, State::Established, "chunk {chunk}");
        assert_eq!(summary.handshakes, 1, "chunk {chunk}");
    }
}

/// Tests several frames arriving in one read, with the batch ending at each
/// receive event.
///
/// The driver then handles each request or session transition before the
/// model advances again.
#[test]
fn test_scripted_batched_reads() {
    // A reset pair and a HostHello share one read
    let summary = run_logged(&[
        Step::Batch(3),
        Step::Reset,
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Request(1),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.reads, 3);

    // The first invalid frame ends the session and this batch. The next two
    // arrive in separate reads after the driver handles `Disconnected`.
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Batch(3),
        Step::Junk(vec![1]),
        Step::Junk(vec![2]),
        Step::Junk(vec![3]),
    ]);
    assert_eq!(summary.state, State::Idle);
    assert_eq!(summary.dropped, 3);
    assert_eq!(summary.reads, 6);

    // Each delivered request ends the batch as well
    let summary = run_logged(&[
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Batch(3),
        Step::Request(1),
        Step::Request(2),
        Step::Request(3),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.delivered, 3);
    assert_eq!(summary.reads, 6);

    // A partial hello completes within one batched read
    let summary = run_logged(&[
        Step::Reset,
        Step::Batch(2),
        Step::Partial,
        Step::Reset,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.reads, 3);

    // Batches also work with small reads
    for chunk in [1u8, 7] {
        let summary = run_logged(&[
            Step::Chunk(chunk),
            Step::Batch(3),
            Step::Reset,
            Step::Reset,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
        ]);
        assert_eq!(summary.state, State::Established, "chunk {chunk}");
        assert_eq!(summary.delivered, 1, "chunk {chunk}");
    }
}

/// Tests that framing retries `Interrupted` reads in every state without a
/// server event or a session change.
#[test]
fn test_scripted_interrupted_reads() {
    let summary = run_logged(&[
        Step::Interrupt,
        Step::Reset,
        Step::Interrupt,
        Step::Hello,
        Step::Interrupt,
        Step::Ack,
        Step::Interrupt,
        Step::Request(1),
        Step::Interrupt,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 1);
    assert_eq!(summary.delivered, 1);
    assert_eq!(summary.dropped, 0);
}

/// Tests that oversized input is refused outside a session and ends an active
/// one.
#[test]
fn test_scripted_oversized_frames() {
    let summary = run_logged(&[
        // Oversized input outside a session is refused
        Step::Oversized,
        // Open a session and keep a copy of its sender
        Step::Reset,
        Step::Hello,
        Step::Ack,
        Step::Retain,
        Step::Request(1),
        // Oversized input ends the session, and its delimiter is no reset
        Step::Oversized,
        Step::SendRetained(2),
        Step::Request(3),
        // A fresh handshake restores requests but not the retained sender
        Step::ResetPair,
        Step::Hello,
        Step::Ack,
        Step::SendRetained(4),
        Step::Request(5),
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.handshakes, 2);
    assert_eq!(summary.delivered, 2);
    assert_eq!(summary.replies, 2);
    assert_eq!(summary.dropped, 3);
}

/// Tests that oversized input aborts a handshake waiting for HostHello or
/// HostAck.
#[test]
fn test_scripted_oversized_handshakes() {
    for awaiting_ack in [false, true] {
        // Wait for HostHello, or for HostAck after one
        let mut steps = vec![Step::Reset];
        if awaiting_ack {
            steps.push(Step::Hello);
        }

        // After oversized input, handshake frames need a fresh reset
        steps.extend([
            Step::Oversized,
            Step::Hello,
            Step::Ack,
            Step::ResetPair,
            Step::Hello,
            Step::Ack,
            Step::Request(1),
        ]);
        let summary = run_logged(&steps);
        assert_eq!(summary.state, State::Established);
        assert_eq!(summary.handshakes, 1 + usize::from(awaiting_ack));
        assert_eq!(summary.delivered, 1);
        assert_eq!(summary.replies, 1);
        assert_eq!(summary.dropped, 3);
    }
}

/// Tests early rejection of an oversized frame that a partial hello precedes.
///
/// The cases cover chunked input and batches holding its tail and a later
/// reset. The discarded frame's delimiter is consumed once, and the real reset
/// survives.
#[test]
fn test_scripted_oversized_partial_and_batches() {
    for chunk in [0, 255] {
        for established in [false, true] {
            // Start while waiting for HostHello, or inside a session
            let mut steps = vec![Step::Chunk(chunk), Step::Reset];
            if established {
                steps.extend([Step::Hello, Step::Ack, Step::Retain]);
            }

            // Batch a partial hello, an oversized frame and a real reset, which
            // lands in a later read if the oversized frame ends a session
            steps.extend([
                Step::Batch(3),
                Step::Partial,
                Step::Oversized,
                Step::ResetPair,
                // The real reset survives, so a fresh handshake succeeds
                Step::Hello,
                Step::Ack,
                Step::SendRetained(1),
                Step::Request(2),
            ]);
            let summary = run_logged(&steps);
            assert_eq!(summary.state, State::Established);
            assert_eq!(summary.handshakes, 1 + usize::from(established));
            assert_eq!(summary.delivered, 1);
            assert_eq!(summary.replies, 1);
            assert_eq!(summary.dropped, 1);
        }
    }
}

/// Tests that steps with nothing to replay or truncate yet send nothing.
#[test]
fn test_scripted_noops() {
    let summary = run_logged(&[
        Step::HelloReplay,
        Step::AckReplay,
        Step::RequestReplay,
        Step::Truncated(3),
        Step::Reset,
        Step::Hello,
        Step::Ack,
    ]);
    assert_eq!(summary.state, State::Established);
    assert_eq!(summary.dropped, 0);
}