satx-core 0.4.0

HP 48/49 calculator layer of satx: Kermit and XModem driver loops, calculator operations, HP objects; blocking or sans-I/O (wasm32)
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
//! Drives a [`kermit_proto::Client`] over a byte link.
//!
//! The loop is the one from `kermit-proto`'s docs: write each queued packet
//! with one write (the HP overruns on inter-byte gaps), handle the events,
//! read until the client's next deadline and feed input or a timeout back.
//! After `Done` or `Error` it keeps going until the final ACK or E packet is
//! out, and through the client's linger after a receive (re-ACKing a
//! repeated `B`). Input left over from the idle server (it NAKs once, a
//! server timeout after its last packet, and then stays silent) is drained
//! when the session is created.
//!
//! The loop is written once, as async code on the crate's link (see
//! [`machine`](crate::machine)); [`Session`] runs it over a [`Transport`]
//! with the system clock.

use kermit_proto::{Client, Command, Config, Event, StartError};

use crate::charset;
use crate::link::{self, Link, Progress};
use crate::time::{Duration, Instant};
use crate::transport::{self, Transport};
use crate::{Error, Result};

/// Read wait while a transaction runs but the client has no deadline.
const IDLE_WAIT: Duration = Duration::from_millis(100);
/// Shortest read wait.
const MIN_WAIT: Duration = Duration::from_millis(10);
/// Read wait while discarding input that is already waiting before a host
/// command ([`Session::transact_with`]): long enough to collect what the
/// link has buffered, short enough to cost nothing noticeable.
const PENDING_WAIT: Duration = Duration::from_millis(1);
/// Most time spent discarding waiting input before a host command (a link
/// that never goes quiet must not stall the command).
const PENDING_MAX: Duration = Duration::from_millis(50);

/// Tunables for a [`Session`].
#[derive(Clone, Debug)]
pub struct Options {
    /// Kermit client configuration. The default lingers after a receive
    /// ([`Config::linger`]) as long as the turnaround, which the linger
    /// counts toward: no extra delay between transactions.
    pub kermit: Config,
    /// How long to discard input after opening (stale NAKs).
    pub drain: Duration,
    /// Pause between the end of one transaction and the start of the next.
    /// The HP drops a command packet that arrives right after the final ACK
    /// of the previous transaction, and only answers after its own timeout
    /// (about 6 s) NAKs it; 100 ms was enough on the emulator. Counted
    /// from the `Done` or `Error`, so the client's linger runs inside it.
    pub turnaround: Duration,
}

impl Default for Options {
    fn default() -> Self {
        let turnaround = Duration::from_millis(200);
        let mut kermit = Config::default();
        kermit.linger = turnaround;
        Options {
            kermit,
            drain: Duration::from_millis(500),
            turnaround,
        }
    }
}

/// A file received with a GET.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReceivedFile {
    /// Name from the F packet, decoded from the HP character set.
    pub name: String,
    /// File contents.
    pub data: Vec<u8>,
}

/// What a transaction produced.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Transcript {
    /// Received files (GET); discarded files are left out.
    pub files: Vec<ReceivedFile>,
    /// Names the calculator stored sent files under (SEND), decoded.
    pub stored_names: Vec<String>,
    /// All server text (host command and directory replies), raw HP bytes.
    pub text: Vec<u8>,
}

/// The Kermit side of a link, without the I/O: the client, its
/// configuration and the pacing between transactions.
#[derive(Debug)]
pub(crate) struct KermitCore {
    pub(crate) link: Link,
    client: Client,
    config: Config,
    turnaround: Duration,
    /// When the last transaction ended.
    last_end: Option<Instant>,
    /// See [`Session::answered`].
    answered: bool,
    /// See [`Machine::set_emulated_link`](crate::Machine::set_emulated_link).
    pub(crate) emulated_link: bool,
}

impl KermitCore {
    pub(crate) fn new(link: Link, options: &Options) -> Self {
        KermitCore {
            link,
            client: Client::new(options.kermit.clone()),
            config: options.kermit.clone(),
            turnaround: options.turnaround,
            last_end: None,
            answered: false,
            emulated_link: false,
        }
    }

    pub(crate) fn config(&self) -> &Config {
        &self.config
    }

    pub(crate) fn set_config(&mut self, config: Config) {
        self.client = Client::new(config.clone());
        self.config = config;
    }

    pub(crate) fn answered(&self) -> bool {
        self.answered
    }

    /// Count a transaction as ended at `now` (an aborted one): the next
    /// waits for the turnaround.
    pub(crate) fn ended_at(&mut self, now: Instant) {
        self.last_end = Some(now);
    }

    /// Run one transaction to completion; every event goes to the link.
    pub(crate) async fn transact(&mut self, command: Command) -> Result<Transcript> {
        if let Some(end) = self.last_end {
            self.link.sleep_until(end + self.turnaround).await;
        }
        if matches!(command, Command::Host(_)) && !self.emulated_link {
            self.discard_pending().await?;
        }
        let result = self.run(command).await;
        self.answered = self.client.peer_params().is_some();
        if result.is_err() {
            self.client = Client::new(self.config.clone());
            self.last_end = Some(self.link.now());
        }
        result
    }

    /// Discard input that arrived between transactions, before a host
    /// command goes out: a stale NAK of the idle server (it NAKs once, a
    /// server timeout after its last packet, and then stays silent; measured
    /// on saturnus and saturnng, real hardware unverified) that would
    /// otherwise be read right after the `C`, inside the immediate-NAK
    /// window of host commands. Nothing of the coming transaction can be in
    /// it yet.
    async fn discard_pending(&mut self) -> Result<()> {
        let end = self.link.now() + PENDING_MAX;
        loop {
            let deadline = self.link.now() + PENDING_WAIT;
            let got = self.link.read(Some(deadline)).await?;
            if got.is_empty() || self.link.now() >= end {
                return Ok(());
            }
        }
    }

    async fn run(&mut self, command: Command) -> Result<Transcript> {
        let sending = matches!(command, Command::Send(_));
        let command_text = match &command {
            Command::Host(bytes) | Command::Get(bytes) => charset::decode(bytes),
            _ => String::new(),
        };
        self.client
            .start(self.link.now(), command)
            .map_err(|e| match e {
                StartError::TooLong { len, max } => Error::CommandTooLong {
                    command: command_text,
                    len,
                    max,
                },
                StartError::Busy => Error::Reply("kermit client busy".into()),
                other => Error::Reply(format!("kermit client: {other}")),
            })?;

        let mut transcript = Transcript::default();
        let mut current: Option<ReceivedFile> = None;
        let mut outcome: Option<Result<()>> = None;
        'run: loop {
            let now = self.link.now();
            while let Some(packet) = self.client.poll_output(now) {
                match self.link.write(packet) {
                    Ok(()) => {}
                    // A re-ACK during the linger: the transaction is
                    // complete, a failing link does not undo it.
                    Err(_) if matches!(outcome, Some(Ok(()))) => break 'run,
                    Err(e) => return Err(e.into()),
                }
            }
            // Written before the events count: a failed write of the final
            // ACK fails the transaction, a re-ACK's only ends the linger.
            match self.link.flush().await {
                Ok(()) => {}
                Err(_) if matches!(outcome, Some(Ok(()))) => break 'run,
                Err(e) => return Err(e.into()),
            }
            while let Some(event) = self.client.poll_event() {
                self.link.emit(Progress::Kermit(event.clone()));
                match event {
                    Event::FileStart { name } if sending => {
                        transcript.stored_names.push(charset::decode(&name));
                    }
                    Event::FileStart { name } => {
                        current = Some(ReceivedFile {
                            name: charset::decode(&name),
                            data: Vec::new(),
                        });
                    }
                    Event::Data(data) => {
                        if let Some(file) = current.as_mut() {
                            file.data.extend_from_slice(&data);
                        }
                    }
                    Event::FileEnd { discarded } => {
                        if let Some(file) = current.take()
                            && !discarded
                        {
                            transcript.files.push(file);
                        }
                    }
                    Event::ServerText(text) => transcript.text.extend_from_slice(&text),
                    Event::Progress { .. } => {}
                    Event::Done => {
                        outcome = Some(Ok(()));
                        // The final ACK is out (it is queued with Done):
                        // the turnaround starts, the linger runs inside it.
                        self.last_end = Some(now);
                    }
                    Event::Error(kermit_proto::Error::Remote(text)) => {
                        outcome = Some(Err(Error::Remote(charset::decode(&text))));
                    }
                    Event::Error(e) => outcome = Some(Err(Error::Kermit(e))),
                    _ => {}
                }
            }
            let deadline = match (self.client.next_timeout(), &outcome) {
                (None, Some(_)) => break,
                (Some(t), _) => t,
                (None, None) => now + IDLE_WAIT,
            };
            let input = match self.link.read(Some(deadline.max(now + MIN_WAIT))).await {
                Ok(input) => input,
                // Lingering after Done: the transcript is complete, a
                // failing link only ends the linger.
                Err(_) if matches!(outcome, Some(Ok(()))) => break,
                Err(e) => return Err(e.into()),
            };
            let now = self.link.now();
            if !input.is_empty() {
                self.client.handle_input(now, &input);
            }
            // Also after input: handle_input does not check the deadline, so
            // a peer that keeps sending garbage (noise, wrong speed) would
            // otherwise stall the transaction forever. A no-op until the
            // deadline has passed.
            self.client.handle_timeout(now);
        }
        match outcome {
            Some(Err(e)) => Err(e),
            _ => Ok(transcript),
        }
    }
}

/// A Kermit connection to a calculator in server mode, over a blocking
/// [`Transport`] with the system clock. For a link driven from outside (a
/// browser), see [`machine::Machine`](crate::machine::Machine).
pub struct Session {
    pub(crate) transport: Box<dyn Transport>,
    pub(crate) core: KermitCore,
}

// The transport is a trait object without `Debug`.
impl std::fmt::Debug for Session {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Session")
            .field("core", &self.core)
            .finish_non_exhaustive()
    }
}

impl Session {
    /// Wrap `transport`, discarding input for `options.drain` first.
    pub fn new(mut transport: Box<dyn Transport>, options: Options) -> Result<Self> {
        let link = Link::new(Instant::now());
        link::drive(
            transport.as_mut(),
            &link,
            link.drain(options.drain),
            &mut |_| {},
        )?;
        Ok(Session {
            transport,
            core: KermitCore::new(link, &options),
        })
    }

    /// Open `addr` (see [`transport::open`]) with default [`Options`].
    pub fn open(addr: &str) -> Result<Self> {
        Session::new(transport::open(addr)?, Options::default())
    }

    /// Give the link back, e.g. for an XModem transfer after
    /// [`Calculator::prepare_for_xmodem`](crate::Calculator::prepare_for_xmodem)
    /// ended server mode.
    pub fn into_transport(self) -> Box<dyn Transport> {
        self.transport
    }

    /// The Kermit configuration in use.
    pub fn config(&self) -> &Config {
        self.core.config()
    }

    /// Use `config` from the next transaction on.
    pub fn set_config(&mut self, config: Config) {
        self.core.set_config(config);
    }

    /// Whether the server sent its parameters (its `S`, or the ACK to an
    /// `I` or `S`) in the last transaction, also when it failed later: for
    /// a host command, whether the `C` was received and answered.
    pub fn answered(&self) -> bool {
        self.core.answered()
    }

    /// Run one transaction to completion.
    pub fn transact(&mut self, command: Command) -> Result<Transcript> {
        self.transact_with(command, &mut |_| {})
    }

    /// Run one transaction, passing every event to `progress`.
    pub fn transact_with(
        &mut self,
        command: Command,
        progress: &mut dyn FnMut(&Event),
    ) -> Result<Transcript> {
        let link = self.core.link.clone();
        link::drive(
            self.transport.as_mut(),
            &link,
            self.core.transact(command),
            &mut |p| {
                if let Progress::Kermit(event) = p {
                    progress(event);
                }
            },
        )
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::transport::MemoryTransport;
    use kermit_proto::OutgoingFile;
    use kermit_proto::codec::{BlockCheck, Deframer, Framing, Packet, parse_frame};
    use std::sync::{Arc, Mutex};

    const SX_HOST: &str = include_str!("../../kermit-proto/traces/48sx-host.trace");
    const SX_GET: &str = include_str!("../../kermit-proto/traces/48sx-get.trace");
    const SX_GET_MISSING: &str = include_str!("../../kermit-proto/traces/48sx-get-missing.trace");
    const SX_SEND: &str = include_str!("../../kermit-proto/traces/48sx-send.trace");
    const GX_GET_BINARY: &str = include_str!("../../kermit-proto/traces/48gx-get-binary.trace");

    /// No pauses: zero drain and turnaround, and no linger after a receive
    /// (the in-memory peers never repeat a B).
    fn options() -> Options {
        let mut kermit = Options::default().kermit;
        kermit.linger = Duration::ZERO;
        Options {
            kermit,
            drain: Duration::ZERO,
            turnaround: Duration::ZERO,
        }
    }

    fn session(trace: &str, options: Options) -> Session {
        Session::new(
            Box::new(MemoryTransport::from_trace(trace).unwrap()),
            options,
        )
        .unwrap()
    }

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

    #[test]
    fn host_command() {
        let mut s = session(SX_HOST, options());
        let mut events = 0;
        let t = s
            .transact_with(Command::Host(b"6 7 *".to_vec()), &mut |_| events += 1)
            .unwrap();
        assert_eq!(t.text, host_reply());
        assert!(t.files.is_empty() && t.stored_names.is_empty());
        assert_eq!(events, 2);
    }

    #[test]
    fn get_missing_is_remote_error() {
        let mut s = session(SX_GET_MISSING, options());
        let err = s.transact(Command::Get(b"NOSUCH".to_vec())).unwrap_err();
        assert!(
            matches!(&err, Error::Remote(m) if m == "Undefined Name"),
            "{err:?}"
        );
    }

    #[test]
    fn get_ascii() {
        let mut s = session(SX_GET, options());
        let t = s.transact(Command::Get(b"ALLB".to_vec())).unwrap();
        assert_eq!(t.files.len(), 1);
        assert_eq!(t.files[0].name, "ALLB");
        assert!(t.files[0].data.starts_with(b"%%HP: T(1)A(D)F(.);\r\n"));
    }

    #[test]
    fn get_binary() {
        let mut s = session(GX_GET_BINARY, options());
        let t = s.transact(Command::Get(b"ALLB".to_vec())).unwrap();
        assert_eq!(t.files.len(), 1);
        assert!(t.files[0].data.starts_with(b"HPHP48-R"));
    }

    #[test]
    fn send_reports_stored_name() {
        let mut s = session(SX_SEND, options());
        let file = OutgoingFile {
            name: b"ALLB".to_vec(),
            data: (0..=255).collect(),
        };
        let t = s.transact(Command::Send(vec![file])).unwrap();
        assert_eq!(t.stored_names, vec!["ALLB".to_string()]);
        assert!(t.files.is_empty());
    }

    #[test]
    fn turnaround_pause_between_transactions() {
        let trace = format!("{SX_HOST}\n{SX_HOST}");
        let options = Options {
            turnaround: Duration::from_millis(50),
            ..options()
        };
        let mut s = session(&trace, options);
        s.transact(Command::Host(b"6 7 *".to_vec())).unwrap();
        let start = Instant::now();
        s.transact(Command::Host(b"6 7 *".to_vec())).unwrap();
        assert!(start.elapsed() >= Duration::from_millis(50));
    }

    #[test]
    fn stale_nak_drained() {
        let trace = format!("< \\x01# N3\\r\n{SX_HOST}");
        let mut s = session(
            &trace,
            Options {
                drain: Duration::from_millis(50),
                turnaround: Duration::ZERO,
                ..options()
            },
        );
        let t = s.transact(Command::Host(b"6 7 *".to_vec())).unwrap();
        assert_eq!(t.text, host_reply());
    }

    /// PR #26 review, #1: input that arrived between transactions (here a
    /// stray packet after the first reply; on the link, the idle server's
    /// NAKs) is discarded before a host command goes out, so it is not read
    /// as the first answer to the `C`.
    #[test]
    fn input_between_host_commands_is_discarded() {
        use kermit_proto::codec::{Framing, Packet};
        let line = |dir: &str, kind: u8, data: &[u8]| {
            let p = Packet::new(0, kind, data.to_vec())
                .encode(kermit_proto::BlockCheck::Type1, &Framing::default())
                .unwrap();
            format!("{dir} {}\n", kermit_proto::trace::escape(&p))
        };
        let trace = line(">", b'C', b"A")
            + &line("<", b'Y', b"a")
            + &line("<", b'Y', b"stale")
            + &line(">", b'C', b"B")
            + &line("<", b'Y', b"b");
        let mut s = session(&trace, options());
        assert_eq!(s.transact(Command::Host(b"A".to_vec())).unwrap().text, b"a");
        assert_eq!(s.transact(Command::Host(b"B".to_vec())).unwrap().text, b"b");
    }

    #[test]
    fn error_resets_client() {
        let mut s = session(SX_HOST, options());
        let err = s.transact(Command::Host(b"6 7 +".to_vec())).unwrap_err();
        assert!(matches!(err, Error::Io(_)), "{err:?}");
        // The mismatching write was not consumed, so a clean client can
        // replay the trace from the start.
        let t = s.transact(Command::Host(b"6 7 *".to_vec())).unwrap();
        assert_eq!(t.text, host_reply());
    }

    #[test]
    fn command_too_long() {
        let mut s = session("", options());
        let err = s.transact(Command::Host(vec![b'1'; 200])).unwrap_err();
        assert!(
            matches!(&err, Error::CommandTooLong { command, len, .. } if command.len() == 200 && *len == 200),
            "{err:?}"
        );
    }

    /// A Kermit server that sends file `X` (two D packets) in reply to R and
    /// drops the first transmission of D seq 2.
    fn lossy_server(naks: Arc<Mutex<Vec<u8>>>) -> impl FnMut(&[u8]) -> Vec<Vec<u8>> + Send {
        let check = BlockCheck::Type1;
        let mut deframer = Deframer::new();
        let mut dropped = false;
        move |bytes| {
            let wire = |seq: u8, kind: u8, data: &[u8]| {
                Packet::new(seq, kind, data.to_vec())
                    .encode(check, &Framing::default())
                    .unwrap()
            };
            deframer.push(bytes);
            let mut replies = Vec::new();
            while let Some(frame) = deframer.next_frame() {
                let p = parse_frame(&frame, check).unwrap();
                match (p.kind, p.seq) {
                    (b'R', _) => replies.push(wire(0, b'S', b"~* @-#Y1 ")),
                    (b'Y', 0) => replies.push(wire(1, b'F', b"X")),
                    (b'Y', 1) if !dropped => dropped = true,
                    (b'Y', 1) => {}
                    (b'N', 2) => {
                        naks.lock().unwrap().push(2);
                        replies.push(wire(2, b'D', b"hello "));
                    }
                    (b'Y', 2) => replies.push(wire(3, b'D', b"world")),
                    (b'Y', 3) => replies.push(wire(4, b'Z', b"")),
                    (b'Y', 4) => replies.push(wire(5, b'B', b"")),
                    (b'Y', 5) => {}
                    (kind, seq) => panic!("unexpected packet {} seq {seq}", char::from(kind)),
                }
            }
            replies
        }
    }

    /// A server that sends file `X` ("hi") in reply to R, and a link that
    /// fails every read once our ACK to its B is out.
    struct LostAfterFinalAck {
        inner: MemoryTransport,
        acked: Arc<Mutex<bool>>,
    }

    impl Transport for LostAfterFinalAck {
        fn write_packet(&mut self, packet: &[u8]) -> std::io::Result<()> {
            self.inner.write_packet(packet)
        }

        fn read(&mut self, buf: &mut [u8], timeout: Duration) -> std::io::Result<usize> {
            if *self.acked.lock().unwrap() {
                return Err(std::io::Error::other("link lost"));
            }
            self.inner.read(buf, timeout)
        }
    }

    #[test]
    fn link_lost_while_lingering_keeps_the_transcript() {
        let acked = Arc::new(Mutex::new(false));
        let flag = Arc::clone(&acked);
        let check = BlockCheck::Type1;
        let mut deframer = Deframer::new();
        let server = move |bytes: &[u8]| {
            let wire = |seq: u8, kind: u8, data: &[u8]| {
                Packet::new(seq, kind, data.to_vec())
                    .encode(check, &Framing::default())
                    .unwrap()
            };
            deframer.push(bytes);
            let mut replies = Vec::new();
            while let Some(frame) = deframer.next_frame() {
                let p = parse_frame(&frame, check).unwrap();
                match (p.kind, p.seq) {
                    (b'R', _) => replies.push(wire(0, b'S', b"~* @-#Y1 ")),
                    (b'Y', 0) => replies.push(wire(1, b'F', b"X")),
                    (b'Y', 1) => replies.push(wire(2, b'D', b"hi")),
                    (b'Y', 2) => replies.push(wire(3, b'Z', b"")),
                    (b'Y', 3) => replies.push(wire(4, b'B', b"")),
                    (b'Y', 4) => *flag.lock().unwrap() = true,
                    (kind, seq) => panic!("unexpected packet {} seq {seq}", char::from(kind)),
                }
            }
            replies
        };
        let mut kermit = Options::default().kermit;
        kermit.linger = Duration::from_secs(5);
        let options = Options {
            kermit,
            ..options()
        };
        let transport = LostAfterFinalAck {
            inner: MemoryTransport::new(server),
            acked: Arc::clone(&acked),
        };
        let mut s = Session::new(Box::new(transport), options).unwrap();
        let start = Instant::now();
        let t = s.transact(Command::Get(b"X".to_vec())).unwrap();
        assert!(*acked.lock().unwrap());
        assert!(
            start.elapsed() < Duration::from_secs(5),
            "linger ended early"
        );
        assert_eq!(
            t.files,
            vec![ReceivedFile {
                name: "X".into(),
                data: b"hi".to_vec()
            }]
        );
    }

    /// A link that fails every write once the server's B is out: the ACK
    /// to the B, which comes with Done.
    struct FinalAckLost {
        inner: MemoryTransport,
        b_sent: Arc<Mutex<bool>>,
    }

    impl Transport for FinalAckLost {
        fn write_packet(&mut self, packet: &[u8]) -> std::io::Result<()> {
            if *self.b_sent.lock().unwrap() {
                return Err(std::io::Error::other("link lost"));
            }
            self.inner.write_packet(packet)
        }

        fn read(&mut self, buf: &mut [u8], timeout: Duration) -> std::io::Result<usize> {
            self.inner.read(buf, timeout)
        }
    }

    /// The final ACK's write fails the transaction: it is written before
    /// Done counts, as when the loop wrote to the transport directly.
    #[test]
    fn final_ack_lost_fails_the_transaction() {
        let b_sent = Arc::new(Mutex::new(false));
        let flag = Arc::clone(&b_sent);
        let check = BlockCheck::Type1;
        let mut deframer = Deframer::new();
        let server = move |bytes: &[u8]| {
            let wire = |seq: u8, kind: u8, data: &[u8]| {
                Packet::new(seq, kind, data.to_vec())
                    .encode(check, &Framing::default())
                    .unwrap()
            };
            deframer.push(bytes);
            let mut replies = Vec::new();
            while let Some(frame) = deframer.next_frame() {
                let p = parse_frame(&frame, check).unwrap();
                match (p.kind, p.seq) {
                    (b'R', _) => replies.push(wire(0, b'S', b"~* @-#Y1 ")),
                    (b'Y', 0) => replies.push(wire(1, b'F', b"X")),
                    (b'Y', 1) => replies.push(wire(2, b'D', b"hi")),
                    (b'Y', 2) => replies.push(wire(3, b'Z', b"")),
                    (b'Y', 3) => {
                        *flag.lock().unwrap() = true;
                        replies.push(wire(4, b'B', b""));
                    }
                    (kind, seq) => panic!("unexpected packet {} seq {seq}", char::from(kind)),
                }
            }
            replies
        };
        let transport = FinalAckLost {
            inner: MemoryTransport::new(server),
            b_sent,
        };
        let mut s = Session::new(Box::new(transport), options()).unwrap();
        let err = s.transact(Command::Get(b"X".to_vec())).unwrap_err();
        assert!(matches!(err, Error::Io(_)), "{err:?}");
    }

    /// Reads that never stop delivering noise: no SOH, so no frame ever
    /// completes. Fails the read after `MAX_READS` so a regression cannot
    /// hang the test.
    struct NoisyLine {
        reads: usize,
    }

    const MAX_READS: usize = 5_000;

    impl Transport for NoisyLine {
        fn write_packet(&mut self, _packet: &[u8]) -> std::io::Result<()> {
            Ok(())
        }

        fn read(&mut self, buf: &mut [u8], _timeout: Duration) -> std::io::Result<usize> {
            self.reads += 1;
            if self.reads > MAX_READS {
                return Err(std::io::Error::other("read cap hit: timeout never fired"));
            }
            std::thread::sleep(Duration::from_millis(1));
            let noise = b"zq~";
            buf[..noise.len()].copy_from_slice(noise);
            Ok(noise.len())
        }
    }

    #[test]
    fn continuous_garbage_still_times_out() {
        let mut kermit = Options::default().kermit;
        kermit.timeout = Duration::from_millis(20);
        kermit.retries = 2;
        let options = Options {
            kermit,
            drain: Duration::ZERO,
            turnaround: Duration::ZERO,
        };
        let mut s = Session {
            transport: Box::new(NoisyLine { reads: 0 }),
            core: KermitCore::new(Link::new(Instant::now()), &options),
        };
        let start = Instant::now();
        let err = s.transact(Command::Host(b"6 7 *".to_vec())).unwrap_err();
        assert!(
            matches!(err, Error::Kermit(kermit_proto::Error::Timeout)),
            "{err:?}"
        );
        assert!(
            start.elapsed() < Duration::from_secs(2),
            "{:?}",
            start.elapsed()
        );
    }

    #[test]
    fn lost_packet_is_recovered() {
        let naks = Arc::new(Mutex::new(Vec::new()));
        let transport = MemoryTransport::new(lossy_server(Arc::clone(&naks)));
        let mut kermit = options().kermit;
        kermit.timeout = Duration::from_millis(100);
        let options = Options {
            kermit,
            drain: Duration::ZERO,
            turnaround: Duration::ZERO,
        };
        let mut s = Session::new(Box::new(transport), options).unwrap();
        let start = Instant::now();
        let t = s.transact(Command::Get(b"X".to_vec())).unwrap();
        assert!(start.elapsed() < Duration::from_millis(800));
        assert_eq!(
            t.files,
            vec![ReceivedFile {
                name: "X".into(),
                data: b"hello world".to_vec()
            }]
        );
        assert_eq!(*naks.lock().unwrap(), vec![2]);
    }
}