kermit-proto 0.2.0

Sans-IO Kermit client (user Kermit) for the Kermit servers of HP 48 and 49 calculators
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
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
//! The Kermit client (user Kermit) state machine: we are the host, the
//! calculator is the server.

use crate::time::{Duration, Instant};
use std::collections::VecDeque;
use std::fmt;

use crate::codec::{BlockCheck, CR, Deframer, FrameError, Framing, Packet, parse_frame, unchar};
use crate::params::{InitParams, Negotiated, negotiate};
use crate::prefix::{self, Quoting};

/// Tunables of a [`Client`].
///
/// Non-exhaustive: start from [`Config::default`] and set the fields you
/// need.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Config {
    /// Block check we offer in our Send-Init (default type 3).
    pub block_check: BlockCheck,
    /// `true` (default): we offer QBIN `Y` (no 8th-bit prefixing unless the peer
    /// asks); `false`: we ask for 8th-bit prefixing with `&`.
    pub eight_bit_clean: bool,
    /// `true` (default): we offer REPT `~`; `false`: no repeat compression.
    pub repeat: bool,
    /// Our receive timeout per packet (default 20 s).
    pub timeout: Duration,
    /// Retransmissions per packet before giving up (default 5).
    pub retries: u32,
    /// Retransmissions of the packet that starts a server transaction (the
    /// `C`, `R`, `G` or `I`) while no reply to it has arrived; `None`
    /// (default) means [`Config::retries`]. Once the server answers (its
    /// `S`), [`Config::retries`] applies to the rest of the transaction.
    /// `Some(0)` sends a host command exactly once: a server cannot tell a
    /// resent `C` from a new one and runs it again, while a damaged reply
    /// packet is still re-requested.
    pub first_packet_retries: Option<u32>,
    /// Immediate-NAK window for the first packet (default `None`: off).
    /// When [`Config::first_packet_retries`] forbids another resend of the
    /// packet that starts a transaction, a NAK of it (seq 0) that arrives
    /// within this window after [`Client::poll_output`] handed the packet
    /// out allows exactly one more resend: the server rejected the packet
    /// (damaged on the line, sent while it was busy). The resend goes out
    /// after [`Config::first_packet_nak_grace`] unless the server's `S` (or
    /// another answer) arrives first. A later NAK, a second NAK and a
    /// timeout never allow it. Set it to the time the packet takes on the
    /// wire plus slack: an idle server's periodic NAK is byte-identical to a
    /// rejection, and one that crosses the packet inside the window (the
    /// server did take it) makes the command run twice if its answer takes
    /// longer than the grace.
    pub first_packet_nak_window: Option<Duration>,
    /// Wait after a NAK inside [`Config::first_packet_nak_window`] before
    /// the one resend it allows; `None` (default) means
    /// [`Config::nak_grace`]. Capped by the receive timeout. Further NAKs
    /// while the granted resend waits do not shorten it.
    pub first_packet_nak_grace: Option<Duration>,
    /// Lower bound of [`Config::first_packet_nak_window`]: the time one byte
    /// takes on the link (10 bits / baud, ~1.04 ms at 9600 baud). A NAK that
    /// arrives sooner after the packet was handed out than the packet's
    /// length times this cannot be the server's answer to it (the packet
    /// had not finished arriving): it is stale (input that was waiting, or
    /// the idle server's NAK sent while ours was on the wire). `None`
    /// (default): no lower bound.
    pub first_packet_nak_byte_time: Option<Duration>,
    /// Delay before each packet we send in reply to input (default 0).
    pub packet_pause: Duration,
    /// How long a NAK received while waiting for the reply to the first packet
    /// is tolerated before we resend (default 1 s); such a NAK is usually a
    /// stale one from the idle server.
    pub nak_grace: Duration,
    /// After we ACK the final `B` packet of a receive, keep answering a
    /// retransmitted `B` with the same ACK for this long (default 1 s), in
    /// case our ACK was damaged or lost. `Done` is emitted when the ACK is
    /// queued; the linger emits nothing. [`Client::is_idle`] is false while
    /// it lasts; [`Client::start`] ends it early. `Duration::ZERO` turns it
    /// off. A peer that only retransmits after its own timeout (the TIME
    /// we send, [`Config::timeout`]) is caught only by a linger that long.
    pub linger: Duration,
    /// Receive: most decoded data bytes accepted in one transaction (all
    /// files and server text together); more fails the transaction with
    /// [`Error::TooLarge`] and an E packet. Default 4 MiB, well above the
    /// largest HP object; `None` means no limit.
    pub max_size: Option<usize>,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            block_check: BlockCheck::Type3,
            eight_bit_clean: true,
            repeat: true,
            timeout: Duration::from_secs(20),
            retries: 5,
            first_packet_retries: None,
            first_packet_nak_window: None,
            first_packet_nak_grace: None,
            first_packet_nak_byte_time: None,
            packet_pause: Duration::ZERO,
            nak_grace: Duration::from_secs(1),
            linger: Duration::from_secs(1),
            max_size: Some(DEFAULT_MAX_SIZE),
        }
    }
}

/// Default [`Config::max_size`]: 4 MiB.
const DEFAULT_MAX_SIZE: usize = 4 << 20;

/// A file to send: the name the server should store it under, and its bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OutgoingFile {
    /// Name sent in the F packet.
    pub name: Vec<u8>,
    /// File contents.
    pub data: Vec<u8>,
}

/// One client transaction.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Command {
    /// I exchange (parameters only).
    Info,
    /// Send files: S F D.. Z [F D.. Z].. B.
    Send(Vec<OutgoingFile>),
    /// R name; receive the file(s) the server sends back.
    Get(Vec<u8>),
    /// C text (host command); the reply arrives as [`Event::ServerText`].
    Host(Vec<u8>),
    /// G D (directory); the reply arrives as [`Event::ServerText`].
    Directory,
    /// G F: end server mode.
    Finish,
    /// G L: end server mode and log out.
    Logout,
}

/// Something that happened during a transaction.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Event {
    /// A file starts. Receive: the name from the F packet. Send: the name from
    /// the ACK to F if non-empty, else the name we sent.
    FileStart {
        /// File name.
        name: Vec<u8>,
    },
    /// Decoded data of one received D packet (file transfer only, not X text).
    Data(Vec<u8>),
    /// Send progress after each acknowledged D packet.
    Progress {
        /// Input bytes of the current file acknowledged so far.
        sent: u64,
        /// Size of the current file.
        total: u64,
    },
    /// Z received or acknowledged.
    FileEnd {
        /// The file was discarded (Z data `D`, or we interrupted on an X/Z ACK).
        discarded: bool,
    },
    /// Reply text to Host/Directory.
    ServerText(Vec<u8>),
    /// Terminal failure; the client is idle.
    Error(Error),
    /// Terminal success. The client is idle, or lingers after the final ACK
    /// of a receive ([`Config::linger`]).
    Done,
}

/// Why a transaction failed.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// Decoded data of an E packet from the peer.
    Remote(Vec<u8>),
    /// Retries exhausted.
    Timeout,
    /// Unexpected packet or undecodable data.
    Protocol(String),
    /// [`Client::cancel`] was called.
    Cancelled,
    /// The received data exceeded [`Config::max_size`]; we sent an E packet.
    TooLarge {
        /// The limit that was exceeded.
        limit: usize,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Remote(text) => write!(f, "remote error: {}", String::from_utf8_lossy(text)),
            Error::Timeout => f.write_str("timed out: too many retries"),
            Error::Protocol(msg) => write!(f, "protocol error: {msg}"),
            Error::Cancelled => f.write_str("cancelled"),
            Error::TooLarge { limit } => {
                write!(f, "received data exceeds the limit of {limit} bytes")
            }
        }
    }
}

impl std::error::Error for Error {}

/// Default MAXL assumed for the peer before any Send-Init exchange.
const DEFAULT_MAXL: usize = 80;

/// Why [`Client::start`] refused a command.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum StartError {
    /// A transaction is already running.
    Busy,
    /// The encoded command data (R name, C text) does not fit in one packet
    /// under the default MAXL of 80 with block check type 1.
    TooLong {
        /// Encoded data length.
        len: usize,
        /// Largest encoded data length that fits.
        max: usize,
    },
}

impl fmt::Display for StartError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StartError::Busy => f.write_str("a transaction is already running"),
            StartError::TooLong { len, max } => write!(
                f,
                "command too long: {len} encoded bytes, at most {max} fit in one packet"
            ),
        }
    }
}

impl std::error::Error for StartError {}

/// What the reply to the command packet means.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
    Info,
    Get,
    Text,
    Generic,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Step {
    Init,
    File,
    Data { chunk: usize },
    Eof { discarded: bool, skip_rest: bool },
    Break,
}

#[derive(Debug)]
struct Sending {
    files: Vec<OutgoingFile>,
    idx: usize,
    pos: usize,
    step: Step,
}

#[derive(Debug)]
enum Phase {
    Idle,
    /// Waiting for the reply to the command packet (seq 0).
    Await(Kind),
    /// Receiving a transfer; `text` is Some while in X (text) mode, `file`
    /// is true between an F and its Z.
    Receive {
        text: Option<Vec<u8>>,
        file: bool,
    },
    Send(Sending),
    /// The final ACK of a receive is out; re-ACK a retransmitted B (seq
    /// `self.seq`) until `self.deadline`.
    Linger,
}

/// Sans-IO Kermit client. See the crate documentation for the driver loop.
#[derive(Debug)]
pub struct Client {
    config: Config,
    ours: InitParams,
    deframer: Deframer,
    phase: Phase,
    neg: Option<Negotiated>,
    peer: Option<InitParams>,
    /// Receive: the expected seq. Send: the seq of the packet awaiting its ACK.
    seq: u8,
    last_sent: Vec<u8>,
    queue: VecDeque<(Instant, Vec<u8>)>,
    events: VecDeque<Event>,
    deadline: Option<Instant>,
    retries: u32,
    /// Receive: decoded data bytes accepted in this transaction.
    received: usize,
    /// Await: when the first packet last went out ([`Client::poll_output`])
    /// and its length in bytes.
    first_sent: Option<(Instant, usize)>,
    /// Await: an immediate NAK granted the one extra resend
    /// ([`Config::first_packet_nak_window`]).
    nak_resend: bool,
}

impl Client {
    /// New idle client.
    pub fn new(config: Config) -> Self {
        let ours = InitParams {
            maxl: 94,
            time: config.timeout.as_secs().clamp(1, 94) as u8,
            npad: 0,
            padc: 0,
            eol: CR,
            qctl: b'#',
            qbin: if config.eight_bit_clean { b'Y' } else { b'&' },
            chkt: config.block_check.to_char(),
            rept: if config.repeat { b'~' } else { b' ' },
        };
        Client {
            config,
            ours,
            deframer: Deframer::new(),
            phase: Phase::Idle,
            neg: None,
            peer: None,
            seq: 0,
            last_sent: Vec::new(),
            queue: VecDeque::new(),
            events: VecDeque::new(),
            deadline: None,
            retries: 0,
            received: 0,
            first_sent: None,
            nak_resend: false,
        }
    }

    /// Begin a transaction. First packet is available from
    /// `poll_output(now)` immediately.
    ///
    /// Starts from a clean slate: buffered input, output and events of the
    /// previous transaction that were not polled yet are dropped, and
    /// [`Client::peer_params`] is cleared. A linger after the previous
    /// transaction ([`Config::linger`]) ends.
    ///
    /// Fails with [`StartError::Busy`] while a transaction is running and with
    /// [`StartError::TooLong`] if the encoded command data does not fit in one
    /// packet; the client is unchanged in both cases.
    pub fn start(&mut self, now: Instant, command: Command) -> Result<(), StartError> {
        if !matches!(self.phase, Phase::Idle | Phase::Linger) {
            return Err(StartError::Busy);
        }
        let q = Quoting::default();
        let (kind, data, phase) = match command {
            Command::Info => (b'I', self.ours.encode(), Phase::Await(Kind::Info)),
            Command::Send(files) => (
                b'S',
                self.ours.encode(),
                Phase::Send(Sending {
                    files,
                    idx: 0,
                    pos: 0,
                    step: Step::Init,
                }),
            ),
            Command::Get(name) => (b'R', prefix::encode_all(&name, &q), Phase::Await(Kind::Get)),
            Command::Host(text) => (
                b'C',
                prefix::encode_all(&text, &q),
                Phase::Await(Kind::Text),
            ),
            Command::Directory => (b'G', b"D".to_vec(), Phase::Await(Kind::Text)),
            Command::Finish => (b'G', b"F".to_vec(), Phase::Await(Kind::Generic)),
            Command::Logout => (b'G', b"L".to_vec(), Phase::Await(Kind::Generic)),
        };
        // Commands go out before negotiation: default MAXL, block check 1.
        let max = Packet::max_data(DEFAULT_MAXL, BlockCheck::Type1);
        if data.len() > max {
            return Err(StartError::TooLong {
                len: data.len(),
                max,
            });
        }
        self.deframer.clear();
        self.queue.clear();
        self.events.clear();
        self.neg = None;
        self.peer = None;
        self.seq = 0;
        self.retries = 0;
        self.received = 0;
        self.first_sent = None;
        self.nak_resend = false;
        self.deadline = None;
        self.phase = phase;
        let bytes = Packet::new(0, kind, data).wire(BlockCheck::Type1, &Framing::default());
        self.last_sent = bytes.clone();
        self.queue.push_back((now, bytes));
        Ok(())
    }

    /// Feed bytes read from the link.
    pub fn handle_input(&mut self, now: Instant, bytes: &[u8]) {
        if self.is_idle() {
            self.deframer.clear();
            return;
        }
        self.deframer.push(bytes);
        while !self.is_idle() {
            let Some(frame) = self.deframer.next_frame() else {
                break;
            };
            let check = match self.neg {
                Some(n) if frame.get(3) != Some(&b'S') => n.check,
                _ => BlockCheck::Type1,
            };
            let parsed = parse_frame(&frame, check);
            // SEQ as received, also for frames that fail the check.
            let raw_seq = frame.get(2).map(|&c| unchar(c));
            match self.phase {
                Phase::Idle => {}
                Phase::Await(kind) => self.on_await(now, kind, parsed),
                Phase::Receive { .. } => self.on_receive(now, parsed),
                Phase::Send(_) => self.on_send(now, parsed, raw_seq),
                Phase::Linger => self.on_linger(now, parsed),
            }
        }
        if self.is_idle() {
            self.deframer.clear();
        }
    }

    /// Call when the time from [`Client::next_timeout`] has passed.
    pub fn handle_timeout(&mut self, now: Instant) {
        if self.is_idle() {
            return;
        }
        if let Some(d) = self.deadline
            && now >= d
        {
            self.deadline = None;
            match self.phase {
                Phase::Linger => self.end_linger(),
                // Receiving: NAK the packet we expect (Kermit), not the last ACK.
                Phase::Receive { .. } => {
                    let nak = self.nak();
                    self.retry_with(now, now, nak);
                }
                _ => self.retry(now, now),
            }
        }
    }

    /// Next bytes to write, written atomically (one packet incl. padding and
    /// EOL). None if nothing is due yet at `now` (packet_pause). Keep calling
    /// after Done/Error until None: the final ACK or an E packet may still be
    /// queued.
    pub fn poll_output(&mut self, now: Instant) -> Option<Vec<u8>> {
        match self.queue.front() {
            Some((due, _)) if *due <= now => {}
            _ => return None,
        }
        let (_, bytes) = self.queue.pop_front()?;
        // The linger's deadline is fixed when it starts.
        if !matches!(self.phase, Phase::Idle | Phase::Linger) {
            // Overflow (absurdly large timeout) means no deadline.
            self.deadline = now.checked_add(self.config.timeout);
        }
        if matches!(self.phase, Phase::Await(_)) {
            self.first_sent = Some((now, bytes.len()));
        }
        Some(bytes)
    }

    /// Next event, in order.
    pub fn poll_event(&mut self) -> Option<Event> {
        self.events.pop_front()
    }

    /// Earliest of: when a queued packet becomes due, the retransmit deadline.
    pub fn next_timeout(&self) -> Option<Instant> {
        let due = self.queue.iter().map(|(t, _)| *t).min();
        match (due, self.deadline) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (a, b) => a.or(b),
        }
    }

    /// No transaction running and no linger after one (output may still be
    /// queued).
    pub fn is_idle(&self) -> bool {
        matches!(self.phase, Phase::Idle)
    }

    /// Abort: queue an E packet "Cancelled", emit Error(Cancelled), go idle.
    /// No-op when idle. During the linger after `Done` it only ends the
    /// linger: no E packet, no event.
    pub fn cancel(&mut self, now: Instant) {
        match self.phase {
            Phase::Idle => {}
            Phase::Linger => self.end_linger(),
            _ => self.fail(now, Error::Cancelled, Some(b"Cancelled")),
        }
    }

    /// Parameters the peer sent in its S, or ACK to our S/I, in the current
    /// (or last finished) transaction; cleared by [`Client::start`].
    pub fn peer_params(&self) -> Option<InitParams> {
        self.peer
    }

    // ---- helpers ----

    fn check(&self) -> BlockCheck {
        self.neg.map_or(BlockCheck::Type1, |n| n.check)
    }

    fn framing(&self) -> Framing {
        self.neg.map_or_else(Framing::default, |n| n.framing)
    }

    fn send_quoting(&self) -> Quoting {
        self.neg.map_or_else(Quoting::default, |n| n.send)
    }

    fn recv_quoting(&self) -> Quoting {
        self.neg.map_or_else(Quoting::default, |n| n.recv)
    }

    /// When a packet sent in reply to input at `now` becomes due. A pause that
    /// overflows `Instant` is ignored rather than delaying forever.
    fn paused(&self, now: Instant) -> Instant {
        now.checked_add(self.config.packet_pause).unwrap_or(now)
    }

    /// NAK for the expected seq with the negotiated check and framing.
    fn nak(&self) -> Vec<u8> {
        Packet::new(self.seq, b'N', Vec::new()).wire(self.check(), &self.framing())
    }

    /// Queue a new packet in reply to input; it becomes the retransmit copy.
    fn reply(&mut self, now: Instant, bytes: Vec<u8>) {
        self.last_sent = bytes.clone();
        self.deadline = None;
        let due = self.paused(now);
        self.queue.push_back((due, bytes));
    }

    fn reply_packet(&mut self, now: Instant, seq: u8, kind: u8, data: Vec<u8>) {
        let bytes = Packet::new(seq, kind, data).wire(self.check(), &self.framing());
        self.reply(now, bytes);
    }

    /// Count a retry and re-queue `last_sent` due at `due`, or give up.
    fn retry(&mut self, now: Instant, due: Instant) {
        let bytes = self.last_sent.clone();
        self.retry_with(now, due, bytes);
    }

    /// Count a retry and queue `bytes` (not stored as `last_sent`) due at
    /// `due`, or give up.
    fn retry_with(&mut self, now: Instant, due: Instant, bytes: Vec<u8>) {
        self.retries += 1;
        let limit = match (&self.phase, self.config.first_packet_retries) {
            (Phase::Await(_), Some(first)) => first.saturating_add(u32::from(self.nak_resend)),
            _ => self.config.retries,
        };
        if self.retries > limit {
            self.fail(now, Error::Timeout, Some(b"Too many retries"));
        } else {
            self.deadline = None;
            self.queue.push_back((due, bytes));
        }
    }

    fn stale_nak(&mut self, now: Instant) {
        self.nak_wait(now, self.config.nak_grace);
    }

    /// Resend (or give up) at `now + grace` at the latest.
    fn nak_wait(&mut self, now: Instant, grace: Duration) {
        // A grace period that overflows `Instant` leaves the deadline as is.
        if let Some(grace) = now.checked_add(grace) {
            self.deadline = Some(self.deadline.map_or(grace, |d| d.min(grace)));
        }
    }

    /// A NAK of seq `seq` while awaiting the answer to the first packet.
    /// Inside [`Config::first_packet_nak_window`] (and not before the
    /// packet's wire time, [`Config::first_packet_nak_byte_time`]), with the
    /// first-packet budget spent and no extra resend granted yet, it grants
    /// one; while that resend waits, a NAK changes nothing; otherwise it is
    /// a stale NAK.
    fn await_nak(&mut self, now: Instant, seq: u8) {
        let Some(first) = self.config.first_packet_retries else {
            return self.stale_nak(now);
        };
        if self.nak_resend && self.retries <= first {
            // The granted resend is pending: keep its deadline.
            return;
        }
        if let (Some(window), Some((sent, len))) =
            (self.config.first_packet_nak_window, self.first_sent)
            && seq == 0
            && !self.nak_resend
            && self.retries >= first
        {
            let since = now.saturating_duration_since(sent);
            // The packet's wire time; an overflow means it never ends.
            let wire = match self.config.first_packet_nak_byte_time {
                None => Some(Duration::ZERO),
                Some(byte) => u32::try_from(len).ok().and_then(|n| byte.checked_mul(n)),
            };
            if wire.is_some_and(|wire| since >= wire) && since <= window {
                self.nak_resend = true;
                let grace = self
                    .config
                    .first_packet_nak_grace
                    .unwrap_or(self.config.nak_grace);
                return self.nak_wait(now, grace);
            }
        }
        self.stale_nak(now);
    }

    fn finish(&mut self, event: Event) {
        self.events.push_back(event);
        self.phase = Phase::Idle;
        self.deadline = None;
    }

    /// Done after the final ACK to B (queued at `now`): linger for
    /// [`Config::linger`] after it goes out, or go idle at once.
    fn finish_lingering(&mut self, now: Instant) {
        self.finish(Event::Done);
        // A linger that overflows `Instant` would never end: skip it.
        if let Some(end) = self.paused(now).checked_add(self.config.linger)
            && !self.config.linger.is_zero()
        {
            self.phase = Phase::Linger;
            self.deadline = Some(end);
        }
    }

    fn end_linger(&mut self) {
        self.phase = Phase::Idle;
        self.deadline = None;
        self.deframer.clear();
    }

    /// Drop queued copies of `bytes` that have not gone out yet: the reply
    /// that made them obsolete has arrived.
    fn drop_queued(&mut self, bytes: &[u8]) {
        self.queue.retain(|(_, b)| b.as_slice() != bytes);
    }

    /// Queue an E packet (if `text`), emit Error, go idle. Output still queued
    /// (a held-back ACK, D or F) is dropped first so nothing follows the E.
    fn fail(&mut self, now: Instant, err: Error, text: Option<&[u8]>) {
        self.queue.clear();
        if let Some(text) = text {
            let data = prefix::encode_all(text, &self.send_quoting());
            let bytes = Packet::new(self.seq, b'E', data).wire(self.check(), &self.framing());
            self.queue.push_back((now, bytes));
        }
        self.finish(Event::Error(err));
    }

    fn protocol(&mut self, now: Instant, msg: String, text: &[u8]) {
        self.fail(now, Error::Protocol(msg), Some(text));
    }

    fn remote(&mut self, data: &[u8], q: &Quoting) {
        let text = prefix::decode(data, q).unwrap_or_else(|_| data.to_vec());
        self.finish(Event::Error(Error::Remote(text)));
    }

    /// Decode received data, failing the transaction on a prefix error.
    fn decode(&mut self, now: Instant, data: &[u8], q: &Quoting) -> Option<Vec<u8>> {
        match prefix::decode(data, q) {
            Ok(v) => Some(v),
            Err(e) => {
                self.protocol(now, e.to_string(), b"Bad prefix");
                None
            }
        }
    }

    // ---- receiving ----

    fn on_await(&mut self, now: Instant, kind: Kind, parsed: Result<Packet, FrameError>) {
        let Ok(p) = parsed else { return };
        match p.kind {
            b'N' => self.await_nak(now, p.seq),
            b'E' => {
                if kind == Kind::Info {
                    self.finish(Event::Done);
                } else {
                    self.remote(&p.data, &Quoting::default());
                }
            }
            b'S' if p.seq == 0 => {
                let theirs = InitParams::decode(&p.data);
                let n = negotiate(&self.ours, &theirs);
                self.peer = Some(theirs);
                // The ACK to S uses type 1 but already the peer's framing.
                let bytes =
                    Packet::new(0, b'Y', self.ours.encode()).wire(BlockCheck::Type1, &n.framing);
                self.reply(now, bytes);
                self.neg = Some(n);
                self.seq = 1;
                self.retries = 0;
                self.phase = Phase::Receive {
                    text: None,
                    file: false,
                };
            }
            b'Y' if p.seq == 0 => match kind {
                Kind::Info => {
                    self.peer = Some(InitParams::decode(&p.data));
                    self.finish(Event::Done);
                }
                Kind::Text => {
                    if !p.data.is_empty() {
                        let Some(text) = self.decode(now, &p.data, &Quoting::default()) else {
                            return;
                        };
                        self.events.push_back(Event::ServerText(text));
                    }
                    self.finish(Event::Done);
                }
                Kind::Generic => self.finish(Event::Done),
                Kind::Get => {
                    self.protocol(now, "unexpected ACK to R".to_string(), b"Unexpected packet")
                }
            },
            _ => {}
        }
    }

    fn on_receive(&mut self, now: Instant, parsed: Result<Packet, FrameError>) {
        let n = self.seq;
        let p = match parsed {
            Ok(p) => p,
            Err(FrameError::BadCheck) => {
                // Not stored as last_sent: a duplicate still gets the last ACK.
                let nak = self.nak();
                let due = self.paused(now);
                self.retry_with(now, due, nak);
                return;
            }
            Err(FrameError::BadLength) => return,
        };
        if p.kind == b'E' {
            let q = self.recv_quoting();
            self.remote(&p.data, &q);
            return;
        }
        if p.seq != n {
            if p.seq == (n + 63) % 64 && p.kind != b'N' {
                self.retries = 0;
                let due = self.paused(now);
                self.queue.push_back((due, self.last_sent.clone()));
                self.deadline = None;
            }
            return;
        }
        if p.kind != b'N' {
            // Packet n arrived: a NAK for it still queued is obsolete.
            let nak = self.nak();
            self.drop_queued(&nak);
        }
        let q = self.recv_quoting();
        match p.kind {
            b'F' | b'D' | b'Z' => {
                let Some(data) = self.decode(now, &p.data, &q) else {
                    return;
                };
                if p.kind == b'D' && !self.accept(now, data.len()) {
                    return;
                }
                let Phase::Receive { text, file } = &mut self.phase else {
                    return;
                };
                match (p.kind, text.as_mut()) {
                    (b'F', _) => {
                        *text = None;
                        *file = true;
                        self.events.push_back(Event::FileStart { name: data });
                    }
                    (b'D', Some(buf)) => buf.extend_from_slice(&data),
                    (b'D', None) => self.events.push_back(Event::Data(data)),
                    (_, Some(_)) => {
                        let buf = text.take().unwrap_or_default();
                        self.events.push_back(Event::ServerText(buf));
                    }
                    (_, None) => {
                        *file = false;
                        self.events.push_back(Event::FileEnd {
                            discarded: data == b"D",
                        });
                    }
                }
            }
            b'X' => {
                if let Phase::Receive { text, .. } = &mut self.phase {
                    *text = Some(Vec::new());
                }
            }
            b'A' => {}
            b'B' => {
                let open = match &self.phase {
                    Phase::Receive { text, file } => *file || text.is_some(),
                    _ => false,
                };
                if open {
                    // Acknowledging it would pass off a truncated file (or
                    // reply text) as a success.
                    self.protocol(
                        now,
                        "B before the Z of the current file".to_string(),
                        b"Unexpected B",
                    );
                    return;
                }
                self.reply_packet(now, n, b'Y', Vec::new());
                self.finish_lingering(now);
                return;
            }
            b'N' => return,
            other => {
                self.protocol(
                    now,
                    format!("unexpected packet {:?}", char::from(other)),
                    b"Unexpected packet",
                );
                return;
            }
        }
        self.reply_packet(now, n, b'Y', Vec::new());
        self.seq = (n + 1) % 64;
        self.retries = 0;
    }

    /// Count `len` received data bytes against [`Config::max_size`]; false
    /// (and the transaction failed) if they do not fit.
    fn accept(&mut self, now: Instant, len: usize) -> bool {
        self.received = self.received.saturating_add(len);
        match self.config.max_size {
            Some(limit) if self.received > limit => {
                self.fail(now, Error::TooLarge { limit }, Some(b"Too large"));
                false
            }
            _ => true,
        }
    }

    /// After the final ACK: answer a retransmitted B with the same ACK (one
    /// pending copy at most); anything else is ignored.
    fn on_linger(&mut self, now: Instant, parsed: Result<Packet, FrameError>) {
        if let Ok(p) = parsed
            && p.kind == b'B'
            && p.seq == self.seq
            && !self.queue.iter().any(|(_, b)| *b == self.last_sent)
        {
            let due = self.paused(now);
            self.queue.push_back((due, self.last_sent.clone()));
        }
    }

    // ---- sending ----

    fn on_send(&mut self, now: Instant, parsed: Result<Packet, FrameError>, raw_seq: Option<u8>) {
        let n = self.seq;
        let init = matches!(&self.phase, Phase::Send(s) if s.step == Step::Init);
        let p = match parsed {
            Ok(p) => p,
            Err(FrameError::BadCheck) => {
                // A damaged reply for the outstanding packet acts as a NAK.
                // Anything else (e.g. a late duplicate ACK(0) to S, which uses
                // check type 1) is ignored.
                if raw_seq == Some(n) {
                    let due = self.paused(now);
                    self.retry(now, due);
                }
                return;
            }
            Err(FrameError::BadLength) => return,
        };
        match p.kind {
            b'E' => {
                let q = self.recv_quoting();
                self.remote(&p.data, &q);
            }
            b'Y' if p.seq == n => {
                self.drop_obsolete_retry();
                self.on_ack(now, &p.data);
            }
            b'N' if p.seq == (n + 1) % 64 => {
                self.drop_obsolete_retry();
                self.on_ack(now, &[]);
            }
            b'N' if p.seq == n => {
                if init {
                    self.stale_nak(now);
                } else {
                    let due = self.paused(now);
                    self.retry(now, due);
                }
            }
            _ => {}
        }
    }

    /// Packet n is acknowledged: a retransmission of it still queued (from a
    /// NAK or damaged reply in the same input, or held back by
    /// `packet_pause`) would only delay the next packet.
    fn drop_obsolete_retry(&mut self) {
        let sent = std::mem::take(&mut self.last_sent);
        self.drop_queued(&sent);
        self.last_sent = sent;
    }

    fn send_next(&mut self, now: Instant, kind: u8, data: Vec<u8>) {
        self.seq = (self.seq + 1) % 64;
        self.reply_packet(now, self.seq, kind, data);
    }

    fn max_data(&self) -> usize {
        let maxl = self.neg.map_or(DEFAULT_MAXL, |n| usize::from(n.peer_maxl));
        Packet::max_data(maxl, self.check())
    }

    fn sending(&mut self) -> Option<&mut Sending> {
        match &mut self.phase {
            Phase::Send(s) => Some(s),
            _ => None,
        }
    }

    fn on_ack(&mut self, now: Instant, data: &[u8]) {
        self.retries = 0;
        let Some(step) = self.sending().map(|s| s.step) else {
            return;
        };
        match step {
            Step::Init => {
                let theirs = InitParams::decode(data);
                self.peer = Some(theirs);
                self.neg = Some(negotiate(&self.ours, &theirs));
                self.next_file(now);
            }
            Step::File => {
                let name = if data.is_empty() {
                    self.sending()
                        .and_then(|s| s.files.get(s.idx))
                        .map(|f| f.name.clone())
                        .unwrap_or_default()
                } else {
                    let q = self.recv_quoting();
                    let Some(name) = self.decode(now, data, &q) else {
                        return;
                    };
                    name
                };
                self.events.push_back(Event::FileStart { name });
                self.next_data(now);
            }
            Step::Data { chunk } => {
                let Some(s) = self.sending() else { return };
                s.pos += chunk;
                let sent = s.pos as u64;
                let total = s.files.get(s.idx).map_or(0, |f| f.data.len()) as u64;
                self.events.push_back(Event::Progress { sent, total });
                if data == b"X" || data == b"Z" {
                    if let Some(s) = self.sending() {
                        s.step = Step::Eof {
                            discarded: true,
                            skip_rest: data == b"Z",
                        };
                    }
                    let d = prefix::encode_all(b"D", &self.send_quoting());
                    self.send_next(now, b'Z', d);
                } else {
                    self.next_data(now);
                }
            }
            Step::Eof {
                discarded,
                skip_rest,
            } => {
                self.events.push_back(Event::FileEnd { discarded });
                if let Some(s) = self.sending() {
                    s.idx = if skip_rest { s.files.len() } else { s.idx + 1 };
                }
                self.next_file(now);
            }
            Step::Break => self.finish(Event::Done),
        }
    }

    /// Send F for the current file, or B when all files are done. Fails the
    /// transaction if the encoded name does not fit in one packet.
    fn next_file(&mut self, now: Instant) {
        let max = self.max_data();
        let q = self.send_quoting();
        let Some(s) = self.sending() else { return };
        let encoded = s.files.get(s.idx).map(|f| {
            let (enc, used) = prefix::encode(&f.name, &q, max);
            (enc, used == f.name.len())
        });
        match encoded {
            Some((_, false)) => {
                self.protocol(
                    now,
                    format!("file name does not fit in a packet of {max} data bytes"),
                    b"File name too long",
                );
            }
            Some((name, true)) => {
                s.pos = 0;
                s.step = Step::File;
                self.send_next(now, b'F', name);
            }
            None => {
                s.step = Step::Break;
                self.send_next(now, b'B', Vec::new());
            }
        }
    }

    /// Send the next D chunk of the current file, or Z at its end.
    fn next_data(&mut self, now: Instant) {
        let max = self.max_data();
        let q = self.send_quoting();
        let Some(s) = self.sending() else { return };
        let rest = s
            .files
            .get(s.idx)
            .and_then(|f| f.data.get(s.pos..))
            .unwrap_or_default();
        if rest.is_empty() {
            s.step = Step::Eof {
                discarded: false,
                skip_rest: false,
            };
            self.send_next(now, b'Z', Vec::new());
        } else {
            let (enc, used) = prefix::encode(rest, &q, max);
            s.step = Step::Data { chunk: used };
            self.send_next(now, b'D', enc);
        }
    }
}

/// Test helpers shared by trace tests.
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
pub(crate) mod test_support {
    use super::*;
    use crate::trace::{self, Direction};

    /// Replay `trace` at a fixed `now`: Out lines must match `poll_output`, In
    /// lines are fed to `handle_input`. Returns the client and all events.
    pub(crate) fn replay_client(cmd: Command, config: Config, text: &str) -> (Client, Vec<Event>) {
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, cmd).unwrap();
        let mut events = Vec::new();
        for (i, (dir, bytes)) in trace::parse(text).unwrap().into_iter().enumerate() {
            match dir {
                Direction::Out => {
                    let got = c.poll_output(now);
                    assert_eq!(
                        got.as_deref().map(trace::escape),
                        Some(trace::escape(&bytes)),
                        "trace entry {i}: expected output > {}",
                        trace::escape(&bytes)
                    );
                }
                Direction::In => c.handle_input(now, &bytes),
            }
            while let Some(e) = c.poll_event() {
                events.push(e);
            }
        }
        let extra = c.poll_output(now);
        assert_eq!(
            extra.as_deref().map(trace::escape),
            None,
            "unexpected trailing output"
        );
        (c, events)
    }

    /// [`replay_client`] returning only the events.
    pub(crate) fn replay(cmd: Command, config: Config, text: &str) -> Vec<Event> {
        replay_client(cmd, config, text).1
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::test_support::{replay, replay_client};
    use super::*;
    use crate::trace;

    const T1: BlockCheck = BlockCheck::Type1;
    const T3: BlockCheck = BlockCheck::Type3;
    /// Our params with the default config.
    const OURS: &[u8] = b"~4 @-#Y3~";

    fn wire(seq: u8, kind: u8, data: &[u8], check: BlockCheck) -> Vec<u8> {
        Packet::new(seq, kind, data.to_vec())
            .encode(check, &Framing::default())
            .unwrap()
    }

    fn out(seq: u8, kind: u8, data: &[u8], check: BlockCheck) -> String {
        format!("> {}\n", trace::escape(&wire(seq, kind, data, check)))
    }

    fn inp(seq: u8, kind: u8, data: &[u8], check: BlockCheck) -> String {
        format!("< {}\n", trace::escape(&wire(seq, kind, data, check)))
    }

    fn hp_text() -> Vec<u8> {
        let mut v = b"1:".to_vec();
        v.extend_from_slice(&[b' '; 20]);
        v.extend_from_slice(b"42\r\n");
        v
    }

    fn cfg() -> Config {
        Config::default()
    }

    /// The HP's server-side Send-Init reply prefix: S, our ACK.
    fn s_exchange(theirs: &[u8]) -> String {
        inp(0, b'S', theirs, T1) + &out(0, b'Y', OURS, T1)
    }

    #[test]
    fn our_params_encode() {
        assert_eq!(Client::new(cfg()).ours.encode(), OURS);
    }

    #[test]
    fn host_long_reply_real_bytes() {
        let mut t = String::from("> \\x01( C6 7 *C\\r\n< \\x01+ S~* @-#Y3$\\r\n");
        t += &out(0, b'Y', OURS, T1);
        t += &inp(1, b'X', b"", T3);
        t += &out(1, b'Y', b"", T3);
        t += &inp(2, b'D', b"1:                    42#M#J", T3);
        t += &out(2, b'Y', b"", T3);
        t += &inp(3, b'Z', b"", T3);
        t += &out(3, b'Y', b"", T3);
        t += &inp(4, b'B', b"", T3);
        t += &out(4, b'Y', b"", T3);
        let ev = replay(Command::Host(b"6 7 *".to_vec()), cfg(), &t);
        assert_eq!(ev, vec![Event::ServerText(hp_text()), Event::Done]);
    }

    #[test]
    fn host_short_reply() {
        let t = out(0, b'C', b"VARS", T1) + &inp(0, b'Y', b"{ A B }#M#J", T1);
        let ev = replay(Command::Host(b"VARS".to_vec()), cfg(), &t);
        assert_eq!(
            ev,
            vec![Event::ServerText(b"{ A B }\r\n".to_vec()), Event::Done]
        );
    }

    #[test]
    fn directory_and_finish() {
        let t = out(0, b'G', b"D", T1) + &inp(0, b'Y', b"X 12", T1);
        let ev = replay(Command::Directory, cfg(), &t);
        assert_eq!(ev, vec![Event::ServerText(b"X 12".to_vec()), Event::Done]);

        let t = out(0, b'G', b"F", T1) + &inp(0, b'Y', b"", T1);
        assert_eq!(replay(Command::Finish, cfg(), &t), vec![Event::Done]);
        let t = out(0, b'G', b"L", T1) + &inp(0, b'Y', b"", T1);
        assert_eq!(replay(Command::Logout, cfg(), &t), vec![Event::Done]);
    }

    #[test]
    fn info_exchange() {
        let mut i = b"\x01, I~4 @-#Y3~".to_vec();
        let chk = T1.compute(&i[1..]);
        i.extend_from_slice(&chk);
        i.push(CR);
        assert_eq!(wire(0, b'I', OURS, T1), i);
        let t = format!("> {}\n< \\x01+ Y~& @-# 3,\\r\n", trace::escape(&i));
        let (c, ev) = replay_client(Command::Info, cfg(), &t);
        assert_eq!(ev, vec![Event::Done]);
        assert!(c.is_idle());
        assert_eq!(c.peer_params(), Some(InitParams::decode(b"~& @-# 3")));
        assert_eq!(c.peer_params().map(|p| p.time), Some(6));

        let t = format!("> {}\n", trace::escape(&i)) + &inp(0, b'E', b"no I", T1);
        assert_eq!(replay(Command::Info, cfg(), &t), vec![Event::Done]);
    }

    fn get_trace(theirs: &[u8], d1: &[u8], d2: &[u8]) -> String {
        let mut t = out(0, b'R', b"FILE", T1);
        t += &s_exchange(theirs);
        t += &inp(1, b'F', b"FILE", T3);
        t += &out(1, b'Y', b"", T3);
        t += &inp(2, b'D', d1, T3);
        t += &out(2, b'Y', b"", T3);
        t += &inp(3, b'D', d2, T3);
        t += &out(3, b'Y', b"", T3);
        t += &inp(4, b'Z', b"", T3);
        t += &out(4, b'Y', b"", T3);
        t += &inp(5, b'B', b"", T3);
        t += &out(5, b'Y', b"", T3);
        t
    }

    fn get_events(d1: &[u8], d2: &[u8]) -> Vec<Event> {
        vec![
            Event::FileStart {
                name: b"FILE".to_vec(),
            },
            Event::Data(d1.to_vec()),
            Event::Data(d2.to_vec()),
            Event::FileEnd { discarded: false },
            Event::Done,
        ]
    }

    #[test]
    fn get_eight_bit_clean() {
        // Peer QBIN 'Y', we offer 'Y': no 8th-bit prefixing; repeat agreed.
        let d1: &[u8] = &[b'H', b'P', 0xC1, b'#', b'#', 0xA3, b'#', b'~'];
        let d2: &[u8] = &[b'#', 0xC1, b'#', b'M', b'~', b'%', b'x'];
        let t = get_trace(b"~* @-#Y3~", d1, d2);
        let ev = replay(Command::Get(b"FILE".to_vec()), cfg(), &t);
        assert_eq!(
            ev,
            get_events(
                &[b'H', b'P', 0xC1, b'#', 0xA3, b'~'],
                &[0x81, b'\r', b'x', b'x', b'x', b'x', b'x']
            )
        );
    }

    #[test]
    fn get_eight_bit_prefixed() {
        let config = Config {
            eight_bit_clean: false,
            ..cfg()
        };
        let mut t = out(0, b'R', b"FILE", T1);
        t += &inp(0, b'S', b"~* @-#Y3~", T1);
        t += &out(0, b'Y', b"~4 @-#&3~", T1);
        t += &get_trace(b"", b"&A&#A#&", b"&#?")
            .lines()
            .skip(3)
            .map(|l| format!("{l}\n"))
            .collect::<String>();
        let ev = replay(Command::Get(b"FILE".to_vec()), config, &t);
        assert_eq!(ev, get_events(&[0xC1, 0x81, b'&'], &[0xFF]));
    }

    #[test]
    fn get_ack_is_protocol_error() {
        let t = out(0, b'R', b"X", T1)
            + &inp(0, b'Y', b"", T1)
            + &out(0, b'E', b"Unexpected packet", T1);
        let ev = replay(Command::Get(b"X".to_vec()), cfg(), &t);
        assert!(matches!(ev.as_slice(), [Event::Error(Error::Protocol(_))]));
    }

    const PEER_SMALL: &[u8] = b"4* @-#Y3"; // maxl 20, no repeat

    fn file(name: &[u8], data: &[u8]) -> OutgoingFile {
        OutgoingFile {
            name: name.to_vec(),
            data: data.to_vec(),
        }
    }

    fn send_start() -> String {
        out(0, b'S', OURS, T1) + &inp(0, b'Y', PEER_SMALL, T1)
    }

    const DATA40: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";

    #[test]
    fn send_one_file_with_stored_name() {
        // Peer MAXL 20, type 3: 15 data chars per D packet.
        let mut t = send_start();
        t += &out(1, b'F', b"local", T3);
        t += &inp(1, b'Y', b"STORED", T3);
        t += &out(2, b'D', &DATA40[..15], T3);
        t += &inp(2, b'Y', b"", T3);
        t += &out(3, b'D', &DATA40[15..30], T3);
        t += &inp(3, b'Y', b"", T3);
        t += &out(4, b'D', &DATA40[30..], T3);
        t += &inp(4, b'Y', b"", T3);
        t += &out(5, b'Z', b"", T3);
        t += &inp(5, b'Y', b"", T3);
        t += &out(6, b'B', b"", T3);
        t += &inp(6, b'Y', b"", T3);
        let (c, ev) = replay_client(Command::Send(vec![file(b"local", DATA40)]), cfg(), &t);
        assert_eq!(
            ev,
            vec![
                Event::FileStart {
                    name: b"STORED".to_vec()
                },
                Event::Progress {
                    sent: 15,
                    total: 40
                },
                Event::Progress {
                    sent: 30,
                    total: 40
                },
                Event::Progress {
                    sent: 40,
                    total: 40
                },
                Event::FileEnd { discarded: false },
                Event::Done,
            ]
        );
        assert_eq!(c.peer_params(), Some(InitParams::decode(PEER_SMALL)));
    }

    #[test]
    fn send_naks_and_duplicates() {
        let mut t = send_start();
        t += &out(1, b'F', b"A", T3);
        // NAK for n+1 counts as the ACK for n (no stored name).
        t += &inp(2, b'N', b"", T3);
        t += &out(2, b'D', &DATA40[..15], T3);
        // NAK for n: immediate resend.
        t += &inp(2, b'N', b"", T3);
        t += &out(2, b'D', &DATA40[..15], T3);
        t += &inp(2, b'Y', b"", T3);
        t += &out(3, b'D', &DATA40[15..20], T3);
        // Duplicate ACK ignored.
        t += &inp(2, b'Y', b"", T3);
        t += &inp(3, b'Y', b"", T3);
        t += &out(4, b'Z', b"", T3);
        t += &inp(4, b'Y', b"", T3);
        t += &out(5, b'B', b"", T3);
        t += &inp(5, b'Y', b"", T3);
        let ev = replay(Command::Send(vec![file(b"A", &DATA40[..20])]), cfg(), &t);
        assert_eq!(
            ev,
            vec![
                Event::FileStart {
                    name: b"A".to_vec()
                },
                Event::Progress {
                    sent: 15,
                    total: 20
                },
                Event::Progress {
                    sent: 20,
                    total: 20
                },
                Event::FileEnd { discarded: false },
                Event::Done,
            ]
        );
    }

    #[test]
    fn send_bad_check_resends() {
        let mut t = send_start();
        t += &out(1, b'F', b"A", T3);
        let mut bad = wire(1, b'Y', b"", T3);
        bad[4] ^= 1;
        t += &format!("< {}\n", trace::escape(&bad));
        t += &out(1, b'F', b"A", T3);
        let ev = replay(Command::Send(vec![file(b"A", b"")]), cfg(), &t);
        assert!(ev.is_empty());
    }

    #[test]
    fn send_two_files_and_empty() {
        let mut t = send_start();
        t += &out(1, b'F', b"E", T3);
        t += &inp(1, b'Y', b"", T3);
        t += &out(2, b'Z', b"", T3);
        t += &inp(2, b'Y', b"", T3);
        t += &out(3, b'F', b"T", T3);
        t += &inp(3, b'Y', b"", T3);
        t += &out(4, b'D', b"hi#M#J", T3);
        t += &inp(4, b'Y', b"", T3);
        t += &out(5, b'Z', b"", T3);
        t += &inp(5, b'Y', b"", T3);
        t += &out(6, b'B', b"", T3);
        t += &inp(6, b'Y', b"", T3);
        let ev = replay(
            Command::Send(vec![file(b"E", b""), file(b"T", b"hi\r\n")]),
            cfg(),
            &t,
        );
        assert_eq!(
            ev,
            vec![
                Event::FileStart {
                    name: b"E".to_vec()
                },
                Event::FileEnd { discarded: false },
                Event::FileStart {
                    name: b"T".to_vec()
                },
                Event::Progress { sent: 4, total: 4 },
                Event::FileEnd { discarded: false },
                Event::Done,
            ]
        );
    }

    #[test]
    fn send_interrupted_by_x() {
        let mut t = send_start();
        t += &out(1, b'F', b"A", T3);
        t += &inp(1, b'Y', b"", T3);
        t += &out(2, b'D', &DATA40[..15], T3);
        t += &inp(2, b'Y', b"X", T3);
        t += &out(3, b'Z', b"D", T3);
        t += &inp(3, b'Y', b"", T3);
        t += &out(4, b'F', b"B", T3);
        t += &inp(4, b'Y', b"", T3);
        t += &out(5, b'Z', b"", T3);
        t += &inp(5, b'Y', b"", T3);
        t += &out(6, b'B', b"", T3);
        t += &inp(6, b'Y', b"", T3);
        let ev = replay(
            Command::Send(vec![file(b"A", DATA40), file(b"B", b"")]),
            cfg(),
            &t,
        );
        assert_eq!(
            ev,
            vec![
                Event::FileStart {
                    name: b"A".to_vec()
                },
                Event::Progress {
                    sent: 15,
                    total: 40
                },
                Event::FileEnd { discarded: true },
                Event::FileStart {
                    name: b"B".to_vec()
                },
                Event::FileEnd { discarded: false },
                Event::Done,
            ]
        );
    }

    #[test]
    fn send_interrupted_by_z_skips_rest() {
        let mut t = send_start();
        t += &out(1, b'F', b"A", T3);
        t += &inp(1, b'Y', b"", T3);
        t += &out(2, b'D', &DATA40[..15], T3);
        t += &inp(2, b'Y', b"Z", T3);
        t += &out(3, b'Z', b"D", T3);
        t += &inp(3, b'Y', b"", T3);
        t += &out(4, b'B', b"", T3);
        t += &inp(4, b'Y', b"", T3);
        let ev = replay(
            Command::Send(vec![file(b"A", DATA40), file(b"B", b"")]),
            cfg(),
            &t,
        );
        assert_eq!(ev[2], Event::FileEnd { discarded: true });
        assert_eq!(ev.len(), 4);
        assert_eq!(ev[3], Event::Done);
    }

    #[test]
    fn stale_nak_before_reply() {
        let mut t = String::from("> \\x01( C6 7 *C\\r\n< \\x01# N3\\r\n< \\x01+ S~* @-#Y3$\\r\n");
        t += &out(0, b'Y', OURS, T1);
        t += &inp(1, b'B', b"", T3);
        t += &out(1, b'Y', b"", T3);
        let ev = replay(Command::Host(b"6 7 *".to_vec()), cfg(), &t);
        assert_eq!(ev, vec![Event::Done]);
    }

    #[test]
    fn stale_nak_then_silence() {
        let config = cfg();
        let grace = config.nak_grace;
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"6 7 *".to_vec())).unwrap();
        let cmd = c.poll_output(now).unwrap();
        assert_eq!(cmd, b"\x01( C6 7 *C\r");
        c.handle_input(now, b"\x01# N3\r");
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.next_timeout(), Some(now + grace));
        c.handle_timeout(now + grace - Duration::from_millis(1));
        assert_eq!(c.poll_output(now + grace - Duration::from_millis(1)), None);
        c.handle_timeout(now + grace);
        assert_eq!(c.poll_output(now + grace), Some(cmd));
        assert_eq!(c.poll_event(), None);
    }

    #[test]
    fn stale_nak_while_waiting_for_s_ack() {
        let mut t = out(0, b'S', OURS, T1);
        t += "< \\x01# N3\\r\n";
        t += &inp(0, b'Y', PEER_SMALL, T1);
        t += &out(1, b'B', b"", T3);
        t += &inp(1, b'Y', b"", T3);
        assert_eq!(replay(Command::Send(vec![]), cfg(), &t), vec![Event::Done]);
    }

    #[test]
    fn input_while_idle_dropped() {
        let now = Instant::now();
        let mut c = Client::new(cfg());
        c.handle_input(now, b"\x01# N3\r\x01+ S~* @-#Y3$\r");
        c.handle_timeout(now + Duration::from_secs(100));
        assert_eq!(c.poll_event(), None);
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.next_timeout(), None);
        assert!(c.is_idle());
        // A partial packet before start does not leak into the transaction.
        c.handle_input(now, b"\x01+ S~*");
        c.start(now, Command::Finish).unwrap();
        c.handle_input(now, b" @-#Y3$\r");
        assert_eq!(c.poll_event(), None);
    }

    #[test]
    fn receive_duplicate_bad_check_and_error() {
        let mut t = out(0, b'R', b"F", T1);
        t += &s_exchange(b"~* @-#Y3");
        t += &inp(1, b'F', b"F", T3);
        t += &out(1, b'Y', b"", T3);
        t += &inp(2, b'D', b"abc", T3);
        t += &out(2, b'Y', b"", T3);
        // Duplicate: our ACK was lost, re-ACK.
        t += &inp(2, b'D', b"abc", T3);
        t += &out(2, b'Y', b"", T3);
        // Bad check: NAK the expected seq.
        let mut bad = wire(3, b'D', b"def", T3);
        bad[5] ^= 1;
        t += &format!("< {}\n", trace::escape(&bad));
        t += &out(3, b'N', b"", T3);
        t += &inp(3, b'E', b"Disk full", T3);
        let ev = replay(Command::Get(b"F".to_vec()), cfg(), &t);
        assert_eq!(
            ev,
            vec![
                Event::FileStart {
                    name: b"F".to_vec()
                },
                Event::Data(b"abc".to_vec()),
                Event::Error(Error::Remote(b"Disk full".to_vec())),
            ]
        );
    }

    #[test]
    fn receive_unexpected_and_bad_prefix() {
        let mut t = out(0, b'R', b"F", T1);
        t += &s_exchange(b"~* @-#Y3");
        t += &inp(1, b'Q', b"", T3);
        t += &out(1, b'E', b"Unexpected packet", T3);
        let ev = replay(Command::Get(b"F".to_vec()), cfg(), &t);
        assert!(matches!(ev.as_slice(), [Event::Error(Error::Protocol(_))]));

        let mut t = out(0, b'R', b"F", T1);
        t += &s_exchange(b"~* @-#Y3");
        t += &inp(1, b'F', b"ab#", T3);
        t += &out(1, b'E', b"Bad prefix", T3);
        let ev = replay(Command::Get(b"F".to_vec()), cfg(), &t);
        assert!(matches!(ev.as_slice(), [Event::Error(Error::Protocol(_))]));
    }

    #[test]
    fn remote_error_reply() {
        let t = out(0, b'C', b"X", T1) + &inp(0, b'E', b"Bad command", T1);
        let ev = replay(Command::Host(b"X".to_vec()), cfg(), &t);
        assert_eq!(
            ev,
            vec![Event::Error(Error::Remote(b"Bad command".to_vec()))]
        );
    }

    #[test]
    fn timeouts_and_retries() {
        let config = Config {
            retries: 2,
            ..cfg()
        };
        let timeout = config.timeout;
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Finish).unwrap();
        assert_eq!(c.next_timeout(), Some(now));
        let pkt = c.poll_output(now).unwrap();
        assert_eq!(c.next_timeout(), Some(now + timeout));
        let mut t = now;
        // Too early: nothing happens.
        c.handle_timeout(now + timeout - Duration::from_millis(1));
        assert_eq!(c.poll_output(now + timeout), None);
        for _ in 0..2 {
            t += timeout;
            c.handle_timeout(t);
            assert_eq!(c.poll_output(t), Some(pkt.clone()));
            assert_eq!(c.next_timeout(), Some(t + timeout));
        }
        t += timeout;
        c.handle_timeout(t);
        assert_eq!(
            c.poll_output(t),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(c.poll_output(t), None);
        assert_eq!(c.poll_event(), Some(Event::Error(Error::Timeout)));
        assert!(c.is_idle());
        assert_eq!(c.next_timeout(), None);
    }

    #[test]
    fn packet_pause_delays_replies() {
        let pause = Duration::from_millis(100);
        let config = Config {
            packet_pause: pause,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"6 7 *".to_vec())).unwrap();
        assert_eq!(c.poll_output(now).unwrap(), b"\x01( C6 7 *C\r");
        c.handle_input(now, b"\x01+ S~* @-#Y3$\r");
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.next_timeout(), Some(now + pause));
        assert_eq!(c.poll_output(now + pause), Some(wire(0, b'Y', OURS, T1)));
    }

    #[test]
    fn start_rejects_command_too_long() {
        let now = Instant::now();
        let mut c = Client::new(cfg());
        // Default MAXL 80, check type 1: 77 data bytes fit.
        assert_eq!(
            c.start(now, Command::Host(vec![b'x'; 78])),
            Err(StartError::TooLong { len: 78, max: 77 })
        );
        // Control characters double in size when encoded.
        assert_eq!(
            c.start(now, Command::Get(vec![b'\r'; 39])),
            Err(StartError::TooLong { len: 78, max: 77 })
        );
        assert!(c.is_idle());
        assert_eq!(c.poll_output(now), None);
        c.start(now, Command::Host(vec![b'x'; 77])).unwrap();
        let pkt = c.poll_output(now).unwrap();
        assert_eq!(pkt, wire(0, b'C', &[b'x'; 77], T1));
        assert_eq!(pkt[1], b'~' - 14); // LEN 80
    }

    /// Get: R out, S in, ACK out, F(1) in, ACK(1) out.
    fn get_started(config: Config) -> (Client, Instant) {
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Get(b"F".to_vec())).unwrap();
        assert_eq!(c.poll_output(now), Some(wire(0, b'R', b"F", T1)));
        c.handle_input(now, &wire(0, b'S', b"~* @-#Y3", T1));
        assert_eq!(c.poll_output(now), Some(wire(0, b'Y', OURS, T1)));
        (c, now)
    }

    /// `first_packet_retries = Some(0)` (satx's host commands, audit PR #18
    /// #1): the `C` is never resent, neither after a timeout nor after a
    /// NAK; once the server's `S` is in, a damaged reply packet is still
    /// NAKed and re-requested with the normal retries.
    #[test]
    fn first_packet_retries_only_cover_the_command() {
        let config = Config {
            first_packet_retries: Some(0),
            nak_grace: Duration::from_secs(20),
            ..cfg()
        };
        let timeout = config.timeout;
        let now = Instant::now();
        let mut c = Client::new(config.clone());
        c.start(now, Command::Host(b"X".to_vec())).unwrap();
        assert_eq!(c.poll_output(now), Some(wire(0, b'C', b"X", T1)));
        c.handle_input(now, &wire(0, b'N', b"", T1));
        c.handle_timeout(now + timeout);
        assert_eq!(
            c.poll_output(now + timeout),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(c.poll_output(now + timeout), None);
        assert_eq!(c.poll_event(), Some(Event::Error(Error::Timeout)));

        // The same command answered: S, X, then a damaged D is NAKed twice
        // and the good copy accepted.
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"X".to_vec())).unwrap();
        c.poll_output(now).unwrap();
        c.handle_input(now, &wire(0, b'S', b"~* @-#Y3", T1));
        assert_eq!(c.poll_output(now), Some(wire(0, b'Y', OURS, T1)));
        c.handle_input(now, &wire(1, b'X', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        let mut bad = wire(2, b'D', b"42", T3);
        bad[5] ^= 1;
        for _ in 0..2 {
            c.handle_input(now, &bad);
            assert_eq!(c.poll_output(now), Some(wire(2, b'N', b"", T3)));
        }
        // A lost packet: the timeout NAKs it too.
        c.handle_timeout(now + timeout);
        assert_eq!(c.poll_output(now + timeout), Some(wire(2, b'N', b"", T3)));
        c.handle_input(now + timeout, &wire(2, b'D', b"42", T3));
        assert_eq!(c.poll_output(now + timeout), Some(wire(2, b'Y', b"", T3)));
        assert!(
            !all_events(&mut c)
                .iter()
                .any(|e| matches!(e, Event::Error(_)))
        );
    }

    const NAK_WINDOW: Duration = Duration::from_millis(250);
    const NAK_GRACE: Duration = Duration::from_secs(3);
    /// One byte at 9600 baud (10 bits).
    const BYTE_TIME: Duration = Duration::from_micros(1042);

    /// satx's host-command config with the immediate-NAK window.
    fn nak_window_cfg() -> Config {
        Config {
            first_packet_retries: Some(0),
            first_packet_nak_window: Some(NAK_WINDOW),
            first_packet_nak_grace: Some(NAK_GRACE),
            first_packet_nak_byte_time: Some(BYTE_TIME),
            nak_grace: Duration::from_secs(20),
            ..cfg()
        }
    }

    /// The saturnus trace (48SX, link clock in seconds): our `C` at 0.000,
    /// the server's NAK seq 0 at 0.011, no `S` ever. Returns the client
    /// with the NAK fed, the start time and the `C` packet.
    fn nak_11ms_after_c(config: Config) -> (Client, Instant, Vec<u8>) {
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"2_m".to_vec())).unwrap();
        let cmd = c.poll_output(now).unwrap();
        assert_eq!(cmd, b"\x01& C2_m)\r");
        c.handle_input(now + Duration::from_millis(11), b"\x01# N3\r");
        assert_eq!(c.poll_output(now + Duration::from_millis(11)), None);
        (c, now, cmd)
    }

    /// Iteration 13: the saturnus trace. A NAK 11 ms after the `C` is a
    /// rejection: with `first_packet_retries = Some(0)` the `C` is resent
    /// once, after the immediate-NAK grace (not the 20 s `nak_grace`), and
    /// the server's answer to the resend completes the command.
    #[test]
    fn immediate_nak_allows_one_resend() {
        let (mut c, now, cmd) = nak_11ms_after_c(nak_window_cfg());
        let at = now + Duration::from_millis(11) + NAK_GRACE;
        assert_eq!(c.next_timeout(), Some(at));
        c.handle_timeout(at - Duration::from_millis(1));
        assert_eq!(c.poll_output(at), None);
        c.handle_timeout(at);
        assert_eq!(c.poll_output(at), Some(cmd));
        assert_eq!(c.poll_event(), None);
        c.handle_input(at, &wire(0, b'Y', b"{ A }", T1));
        assert_eq!(
            all_events(&mut c),
            vec![Event::ServerText(b"{ A }".to_vec()), Event::Done]
        );
    }

    /// Iteration 13: an immediate NAK followed by the server's `S` within
    /// the grace (the NAK was a stale one that crossed our `C`, and the
    /// server did take it): no resend, the reply is received as usual.
    #[test]
    fn immediate_nak_then_s_is_not_resent() {
        let (mut c, now, _) = nak_11ms_after_c(nak_window_cfg());
        let t = now + Duration::from_secs(1);
        c.handle_input(t, &wire(0, b'S', b"~* @-#Y3", T1));
        assert_eq!(c.poll_output(t), Some(wire(0, b'Y', OURS, T1)));
        c.handle_input(t, &wire(1, b'B', b"", T3));
        assert_eq!(c.poll_output(t), Some(wire(1, b'Y', b"", T3)));
        assert_eq!(c.poll_output(t + NAK_GRACE), None);
        assert_eq!(all_events(&mut c), vec![Event::Done]);
    }

    /// Iteration 13: a NAK after the window (here 251 ms after the `C`) is
    /// the idle server's periodic NAK, which may have crossed a `C` it took:
    /// `Some(0)` as before, no resend, neither after it nor after the
    /// timeout, and the wait is the full `nak_grace`.
    #[test]
    fn nak_after_the_window_is_not_resent() {
        let config = nak_window_cfg();
        let timeout = config.timeout;
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"2_m".to_vec())).unwrap();
        c.poll_output(now).unwrap();
        let late = now + NAK_WINDOW + Duration::from_millis(1);
        c.handle_input(late, b"\x01# N3\r");
        assert_eq!(c.next_timeout(), Some(now + timeout));
        c.handle_timeout(now + timeout);
        assert_eq!(
            c.poll_output(now + timeout),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(c.poll_output(now + timeout), None);
        assert_eq!(all_events(&mut c), vec![Event::Error(Error::Timeout)]);
    }

    /// Iteration 13: only one resend per command. Two immediate NAKs before
    /// the resend grant one, and an immediate NAK of the resend grants no
    /// second: the client waits the full `nak_grace` (timeout) and gives up.
    #[test]
    fn two_immediate_naks_allow_only_one_resend() {
        let config = nak_window_cfg();
        let timeout = config.timeout;
        let (mut c, now, cmd) = nak_11ms_after_c(config);
        c.handle_input(now + Duration::from_millis(20), b"\x01# N3\r");
        let at = now + Duration::from_millis(11) + NAK_GRACE;
        assert_eq!(c.next_timeout(), Some(at));
        c.handle_timeout(at);
        assert_eq!(c.poll_output(at), Some(cmd));
        c.handle_input(at + Duration::from_millis(11), b"\x01# N3\r");
        assert_eq!(c.next_timeout(), Some(at + timeout));
        c.handle_timeout(at + timeout);
        assert_eq!(
            c.poll_output(at + timeout),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(all_events(&mut c), vec![Event::Error(Error::Timeout)]);
    }

    /// PR #26 review, #1: a NAK sooner than our packet's wire time (9
    /// bytes at 9600 baud, 9.4 ms) cannot answer it: input that was
    /// waiting in the buffer, or the idle server's NAK sent while our `C`
    /// was on the wire. It is stale: no resend, the full `nak_grace`.
    #[test]
    fn nak_before_the_wire_time_is_stale() {
        let config = nak_window_cfg();
        let timeout = config.timeout;
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"2_m".to_vec())).unwrap();
        let cmd = c.poll_output(now).unwrap();
        assert_eq!(cmd.len(), 9);
        for ms in [0, 9] {
            c.handle_input(now + Duration::from_millis(ms), b"\x01# N3\r");
        }
        assert_eq!(c.next_timeout(), Some(now + timeout));
        c.handle_timeout(now + timeout);
        assert_eq!(
            c.poll_output(now + timeout),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(all_events(&mut c), vec![Event::Error(Error::Timeout)]);
    }

    /// PR #26 review, #3: once an immediate NAK granted the resend, a
    /// further NAK (here a stale one with a `nak_grace` shorter than the
    /// immediate-NAK grace) does not bring the resend forward.
    #[test]
    fn nak_after_the_grant_keeps_the_grace() {
        let config = Config {
            nak_grace: Duration::from_secs(1),
            ..nak_window_cfg()
        };
        let (mut c, now, cmd) = nak_11ms_after_c(config);
        let at = now + Duration::from_millis(11) + NAK_GRACE;
        c.handle_input(now + Duration::from_millis(500), b"\x01# N3\r");
        assert_eq!(c.next_timeout(), Some(at));
        c.handle_timeout(now + Duration::from_millis(1500));
        assert_eq!(c.poll_output(now + Duration::from_millis(1500)), None);
        c.handle_timeout(at);
        assert_eq!(c.poll_output(at), Some(cmd));
    }

    /// Iteration 13: without a NAK, a timeout keeps `Some(0)`: the window
    /// never turns silence into a resend.
    #[test]
    fn nak_window_does_not_resend_after_a_timeout() {
        let config = nak_window_cfg();
        let timeout = config.timeout;
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"2_m".to_vec())).unwrap();
        c.poll_output(now).unwrap();
        c.handle_timeout(now + timeout);
        assert_eq!(
            c.poll_output(now + timeout),
            Some(wire(0, b'E', b"Too many retries", T1))
        );
        assert_eq!(all_events(&mut c), vec![Event::Error(Error::Timeout)]);
    }

    #[test]
    fn receive_timeout_naks_expected_packet() {
        let config = Config {
            retries: 2,
            ..cfg()
        };
        let timeout = config.timeout;
        let (mut c, mut t) = get_started(config);
        // Timeout right after the S ACK: NAK(1), which the sender reads as ACK(0).
        t += timeout;
        c.handle_timeout(t);
        assert_eq!(c.poll_output(t), Some(wire(1, b'N', b"", T3)));
        c.handle_input(t, &wire(1, b'F', b"F", T3));
        assert_eq!(c.poll_output(t), Some(wire(1, b'Y', b"", T3)));
        // Timeouts while waiting for packet 2: NAK(2), counted as retries.
        for _ in 0..2 {
            t += timeout;
            c.handle_timeout(t);
            assert_eq!(c.poll_output(t), Some(wire(2, b'N', b"", T3)));
        }
        // A duplicate still gets the last ACK, not the NAK.
        c.handle_input(t, &wire(1, b'F', b"F", T3));
        assert_eq!(c.poll_output(t), Some(wire(1, b'Y', b"", T3)));
        // The duplicate reset the retry count: two more NAKs, then give up.
        for _ in 0..2 {
            t += timeout;
            c.handle_timeout(t);
            assert_eq!(c.poll_output(t), Some(wire(2, b'N', b"", T3)));
        }
        t += timeout;
        c.handle_timeout(t);
        assert_eq!(
            c.poll_output(t),
            Some(wire(2, b'E', b"Too many retries", T3))
        );
        assert_eq!(
            c.poll_event(),
            Some(Event::FileStart {
                name: b"F".to_vec()
            })
        );
        assert_eq!(c.poll_event(), Some(Event::Error(Error::Timeout)));
    }

    #[test]
    fn send_ignores_bad_check_for_other_seq() {
        let mut t = send_start();
        t += &out(1, b'F', b"A", T3);
        // Late duplicate ACK(0) to S: type 1, fails the type 3 check, ignored.
        t += &inp(0, b'Y', PEER_SMALL, T1);
        // Damaged frame with an unrelated seq: ignored.
        let mut bad = wire(5, b'Y', b"", T3);
        bad[4] ^= 1;
        t += &format!("< {}\n", trace::escape(&bad));
        t += &inp(1, b'Y', b"", T3);
        t += &out(2, b'Z', b"", T3);
        let (c, ev) = replay_client(Command::Send(vec![file(b"A", b"")]), cfg(), &t);
        assert_eq!(
            ev,
            vec![Event::FileStart {
                name: b"A".to_vec()
            }]
        );
        assert_eq!(c.retries, 0);
    }

    #[test]
    fn send_file_name_too_long_fails() {
        // Peer MAXL 20, type 3: 15 data bytes; the name needs 16.
        let mut t = send_start();
        t += &out(0, b'E', b"File name too long", T3);
        let ev = replay(
            Command::Send(vec![file(b"ABCDEFGHIJKLMNOP", b"x")]),
            cfg(),
            &t,
        );
        assert!(matches!(ev.as_slice(), [Event::Error(Error::Protocol(_))]));
        // 15 bytes fit.
        let mut t = send_start();
        t += &out(1, b'F', b"ABCDEFGHIJKLMNO", T3);
        let ev = replay(
            Command::Send(vec![file(b"ABCDEFGHIJKLMNO", b"")]),
            cfg(),
            &t,
        );
        assert!(ev.is_empty());
    }

    #[test]
    fn cancel_drops_queued_output() {
        let pause = Duration::from_millis(100);
        let config = Config {
            packet_pause: pause,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"6 7 *".to_vec())).unwrap();
        assert!(c.poll_output(now).is_some());
        // The ACK to S is held back by the pause when we cancel.
        c.handle_input(now, b"\x01+ S~* @-#Y3$\r");
        c.cancel(now);
        assert_eq!(c.poll_output(now), Some(wire(1, b'E', b"Cancelled", T3)));
        assert_eq!(c.poll_output(now + pause), None);
        assert_eq!(c.next_timeout(), None);
        assert_eq!(c.poll_event(), Some(Event::Error(Error::Cancelled)));
    }

    #[test]
    fn huge_durations_do_not_overflow() {
        let config = Config {
            timeout: Duration::MAX,
            packet_pause: Duration::MAX,
            nak_grace: Duration::MAX,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Host(b"6 7 *".to_vec())).unwrap();
        assert!(c.poll_output(now).is_some());
        // Deadline overflows: none.
        assert_eq!(c.next_timeout(), None);
        // Grace overflows: no deadline either.
        c.handle_input(now, b"\x01# N3\r");
        assert_eq!(c.next_timeout(), None);
        // Pause overflows: the reply is not delayed.
        c.handle_input(now, b"\x01+ S~* @-#Y3$\r");
        let ack = wire(0, b'Y', &c.ours.encode(), T1);
        assert_eq!(c.poll_output(now), Some(ack));
        c.handle_timeout(now);
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.poll_event(), None);
    }

    /// [`get_started`], then F(1) "F" with its ACK.
    fn get_file_open(config: Config) -> (Client, Instant) {
        let (mut c, now) = get_started(config);
        c.handle_input(now, &wire(1, b'F', b"F", T3));
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        (c, now)
    }

    fn all_events(c: &mut Client) -> Vec<Event> {
        std::iter::from_fn(|| c.poll_event()).collect()
    }

    #[test]
    fn lost_final_ack_is_answered_while_lingering() {
        let config = cfg();
        let linger = config.linger;
        let (mut c, now) = get_file_open(config);
        c.handle_input(now, &wire(2, b'Z', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'Y', b"", T3)));
        c.handle_input(now, &wire(3, b'B', b"", T3));
        let ack = wire(3, b'Y', b"", T3);
        assert_eq!(c.poll_output(now), Some(ack.clone()));
        assert_eq!(
            all_events(&mut c),
            vec![
                Event::FileStart {
                    name: b"F".to_vec()
                },
                Event::FileEnd { discarded: false },
                Event::Done
            ]
        );
        assert!(!c.is_idle());
        assert_eq!(c.next_timeout(), Some(now + linger));
        // Our ACK was lost: the server sends B again and gets the same ACK,
        // without a second Done. Other packets are ignored.
        let t = now + linger / 2;
        c.handle_input(t, &wire(3, b'B', b"", T3));
        assert_eq!(c.poll_output(t), Some(ack.clone()));
        c.handle_input(t, &wire(2, b'Z', b"", T3));
        c.handle_input(t, b"\x01# N3\r");
        assert_eq!(c.poll_output(t), None);
        assert_eq!(c.poll_event(), None);
        // The re-ACK does not extend the linger.
        assert_eq!(c.next_timeout(), Some(now + linger));
        c.handle_timeout(now + linger);
        assert!(c.is_idle());
        assert_eq!(c.next_timeout(), None);
        c.handle_input(now + linger, &wire(3, b'B', b"", T3));
        assert_eq!(c.poll_output(now + linger), None);
        assert_eq!(c.poll_event(), None);
    }

    #[test]
    fn repeated_bs_while_lingering_queue_one_ack() {
        let (mut c, now) = get_started(cfg());
        c.handle_input(now, &wire(1, b'B', b"", T3));
        let ack = wire(1, b'Y', b"", T3);
        assert_eq!(c.poll_output(now), Some(ack.clone()));
        let burst: Vec<u8> = (0..10).flat_map(|_| wire(1, b'B', b"", T3)).collect();
        c.handle_input(now, &burst);
        assert_eq!(c.poll_output(now), Some(ack.clone()));
        assert_eq!(c.poll_output(now), None);
        // Once it is out, the next repeat gets another.
        c.handle_input(now, &wire(1, b'B', b"", T3));
        assert_eq!(c.poll_output(now), Some(ack));
        assert_eq!(c.poll_output(now), None);
    }

    #[test]
    fn start_and_cancel_end_the_linger() {
        let (mut c, now) = get_started(cfg());
        c.handle_input(now, &wire(1, b'B', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        assert_eq!(all_events(&mut c), vec![Event::Done]);
        c.cancel(now);
        assert!(c.is_idle());
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.poll_event(), None);

        let (mut c, now) = get_started(cfg());
        c.handle_input(now, &wire(1, b'B', b"", T3));
        assert!(!c.is_idle());
        c.start(now, Command::Finish).unwrap();
        assert_eq!(c.poll_output(now), Some(wire(0, b'G', b"F", T1)));
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.poll_event(), None);
    }

    #[test]
    fn zero_linger_goes_idle_at_once() {
        let config = Config {
            linger: Duration::ZERO,
            ..cfg()
        };
        let (mut c, now) = get_started(config);
        c.handle_input(now, &wire(1, b'B', b"", T3));
        assert!(c.is_idle());
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        assert_eq!(c.next_timeout(), None);
    }

    #[test]
    fn b_before_z_is_a_protocol_error() {
        let (mut c, now) = get_file_open(cfg());
        c.handle_input(now, &wire(2, b'D', b"abc", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'Y', b"", T3)));
        c.handle_input(now, &wire(3, b'B', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(3, b'E', b"Unexpected B", T3)));
        let ev = all_events(&mut c);
        assert!(matches!(
            ev.as_slice(),
            [_, Event::Data(_), Event::Error(Error::Protocol(_))]
        ));
        assert!(c.is_idle());
    }

    #[test]
    fn b_with_text_buffered_is_a_protocol_error() {
        let (mut c, now) = get_started(cfg());
        c.handle_input(now, &wire(1, b'X', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        c.handle_input(now, &wire(2, b'D', b"partial", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'Y', b"", T3)));
        c.handle_input(now, &wire(3, b'B', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(3, b'E', b"Unexpected B", T3)));
        assert!(matches!(
            all_events(&mut c).as_slice(),
            [Event::Error(Error::Protocol(_))]
        ));
    }

    #[test]
    fn ack_drops_a_queued_retransmission() {
        let pause = Duration::from_millis(100);
        let config = Config {
            packet_pause: pause,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Send(vec![file(b"A", b"x")])).unwrap();
        assert_eq!(c.poll_output(now), Some(wire(0, b'S', OURS, T1)));
        c.handle_input(now, &wire(0, b'Y', PEER_SMALL, T1));
        let t = now + pause;
        assert_eq!(c.poll_output(t), Some(wire(1, b'F', b"A", T3)));
        // A NAK and the ACK for F in one read: the resend the NAK queued
        // (held back by the pause) is dropped, D goes out next.
        let mut input = wire(1, b'N', b"", T3);
        input.extend(wire(1, b'Y', b"", T3));
        c.handle_input(t, &input);
        let t = t + pause;
        assert_eq!(c.poll_output(t), Some(wire(2, b'D', b"x", T3)));
        assert_eq!(c.poll_output(t), None);
        // Same with a NAK for n + 1 as the ACK.
        let mut input = wire(2, b'N', b"", T3);
        input.extend(wire(3, b'N', b"", T3));
        c.handle_input(t, &input);
        let t = t + pause;
        assert_eq!(c.poll_output(t), Some(wire(3, b'Z', b"", T3)));
        assert_eq!(c.poll_output(t), None);
    }

    #[test]
    fn packet_drops_a_queued_nak() {
        let pause = Duration::from_millis(100);
        let config = Config {
            packet_pause: pause,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Get(b"F".to_vec())).unwrap();
        assert!(c.poll_output(now).is_some());
        c.handle_input(now, &wire(0, b'S', b"~* @-#Y3", T1));
        assert_eq!(c.poll_output(now + pause), Some(wire(0, b'Y', OURS, T1)));
        // A damaged F, then the good retransmission in the same read.
        let mut bad = wire(1, b'F', b"F", T3);
        bad[5] ^= 1;
        bad.extend(wire(1, b'F', b"F", T3));
        c.handle_input(now + pause, &bad);
        let t = now + pause * 2;
        assert_eq!(c.poll_output(t), Some(wire(1, b'Y', b"", T3)));
        assert_eq!(c.poll_output(t), None);
    }

    #[test]
    fn bad_check_naks_count_as_retries() {
        let config = Config {
            retries: 2,
            ..cfg()
        };
        let (mut c, now) = get_file_open(config);
        let mut bad = wire(2, b'D', b"abc", T3);
        bad[5] ^= 1;
        for _ in 0..2 {
            c.handle_input(now, &bad);
            assert_eq!(c.poll_output(now), Some(wire(2, b'N', b"", T3)));
        }
        c.handle_input(now, &bad);
        assert_eq!(
            c.poll_output(now),
            Some(wire(2, b'E', b"Too many retries", T3))
        );
        assert_eq!(
            all_events(&mut c).last(),
            Some(&Event::Error(Error::Timeout))
        );
        // A good packet in between resets the count.
        let config = Config {
            retries: 1,
            ..cfg()
        };
        let (mut c, now) = get_file_open(config);
        c.handle_input(now, &bad);
        assert_eq!(c.poll_output(now), Some(wire(2, b'N', b"", T3)));
        c.handle_input(now, &wire(2, b'D', b"abc", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'Y', b"", T3)));
        let mut bad = wire(3, b'D', b"def", T3);
        bad[5] ^= 1;
        c.handle_input(now, &bad);
        assert_eq!(c.poll_output(now), Some(wire(3, b'N', b"", T3)));
    }

    #[test]
    fn receive_size_cap() {
        let config = Config {
            max_size: Some(5),
            ..cfg()
        };
        let (mut c, now) = get_file_open(config);
        c.handle_input(now, &wire(2, b'D', b"abc", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'Y', b"", T3)));
        c.handle_input(now, &wire(3, b'D', b"de", T3));
        assert_eq!(c.poll_output(now), Some(wire(3, b'Y', b"", T3)));
        c.handle_input(now, &wire(4, b'D', b"f", T3));
        assert_eq!(c.poll_output(now), Some(wire(4, b'E', b"Too large", T3)));
        assert_eq!(
            all_events(&mut c).last(),
            Some(&Event::Error(Error::TooLarge { limit: 5 }))
        );
        assert!(c.is_idle());
        // Server text counts too.
        let config = Config {
            max_size: Some(2),
            ..cfg()
        };
        let (mut c, now) = get_started(config);
        c.handle_input(now, &wire(1, b'X', b"", T3));
        assert_eq!(c.poll_output(now), Some(wire(1, b'Y', b"", T3)));
        c.handle_input(now, &wire(2, b'D', b"abc", T3));
        assert_eq!(c.poll_output(now), Some(wire(2, b'E', b"Too large", T3)));
        assert_eq!(Config::default().max_size, Some(4 << 20));
    }

    #[test]
    fn start_clears_the_previous_transaction() {
        let pause = Duration::from_millis(100);
        let config = Config {
            packet_pause: pause,
            ..cfg()
        };
        let now = Instant::now();
        let mut c = Client::new(config);
        c.start(now, Command::Info).unwrap();
        assert!(c.poll_output(now).is_some());
        c.handle_input(now, b"\x01+ Y~& @-# 3,\r");
        assert!(c.peer_params().is_some());
        assert!(c.is_idle());
        // Done not polled; a failed transaction leaves an E packet queued.
        c.start(now, Command::Host(b"X".to_vec())).unwrap();
        assert_eq!(c.peer_params(), None);
        assert_eq!(c.poll_event(), None);
        assert!(c.poll_output(now).is_some());
        c.handle_input(now, b"\x01+ S~* @-#Y3$\r");
        c.cancel(now);
        c.start(now, Command::Finish).unwrap();
        assert_eq!(c.poll_output(now + pause), Some(wire(0, b'G', b"F", T1)));
        assert_eq!(c.poll_output(now + pause), None);
        assert_eq!(c.poll_event(), None);
    }

    #[test]
    fn busy_and_cancel() {
        let now = Instant::now();
        let mut c = Client::new(cfg());
        c.start(now, Command::Directory).unwrap();
        assert_eq!(c.start(now, Command::Finish), Err(StartError::Busy));
        assert!(c.poll_output(now).is_some());
        c.cancel(now);
        assert_eq!(c.poll_output(now), Some(wire(0, b'E', b"Cancelled", T1)));
        assert_eq!(c.poll_event(), Some(Event::Error(Error::Cancelled)));
        assert!(c.is_idle());
        c.cancel(now);
        assert_eq!(c.poll_output(now), None);
        assert_eq!(c.poll_event(), None);
        assert!(c.start(now, Command::Finish).is_ok());
    }
}