libmoshpit 0.8.13

A Rust implementation of in the same vein as Mosh, the mobile shell.
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
// Copyright (c) 2025 moshpit developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Differential terminal renderer.
//!
//! Converts the output of the terminal emulator ([`vt100::Screen`]) plus any
//! prediction overlays into the minimal sequence of ANSI escape codes needed
//! to update the user's physical terminal from its last known state.
//!
//! # Strategy
//!
//! `vt100::Screen` exposes `contents_diff(prev: &vt100::Screen) -> Vec<u8>`
//! which returns the bytes that transition a terminal showing `prev` to one
//! showing `self`, including the final cursor position, SGR attributes and
//! cursor visibility.  Rather than diffing the server screen and then painting
//! predictions on top as a separate pass, we build a single *target*
//! framebuffer — the server screen with prediction overlays merged in as real
//! cells and the cursor placed at its final position — and diff that:
//!
//! 1. `frame = server_screen + predicted cells + final cursor` (a scratch
//!    [`vt100::Parser`]).
//! 2. `diff = frame.contents_diff(displayed)` – a fully self-contained update.
//!
//! Because predictions are first-class cells in `frame`, a prediction that is
//! later culled simply disappears from the next `frame`, and the diff repaints
//! the real cell automatically — no stale glyph can be left behind.
//!
//! We maintain a `vt100::Parser` (`displayed`) that is advanced with exactly
//! the bytes we emit, so subsequent diffs are always computed against what is
//! actually shown on the user's screen.

use std::{
    fmt,
    sync::{Arc, Mutex},
};

use super::emulator::Emulator;
use super::prediction::{OverlayCell, OverlayCursor, PredictionEngine};

/// A stateful differential renderer.
pub struct Renderer {
    /// Tracks what the user's physical terminal currently looks like.
    displayed: vt100::Parser,
    /// True after the first render — before that we must do a full refresh.
    initialized: bool,
}

impl fmt::Debug for Renderer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Renderer")
            .field("initialized", &self.initialized)
            .finish_non_exhaustive()
    }
}

impl Renderer {
    /// Create a new renderer with the given initial terminal dimensions.
    #[must_use]
    pub fn new(rows: u16, cols: u16) -> Self {
        Self {
            displayed: vt100::Parser::new(rows, cols, 0),
            initialized: false,
        }
    }

    /// Resize the renderer's view of the physical terminal.
    pub fn set_size(&mut self, rows: u16, cols: u16) {
        // Resizing forces a full refresh on the next render.
        self.displayed.screen_mut().set_size(rows, cols);
        self.initialized = false;
    }

    /// Produce the bytes to send to stdout that will bring the user's terminal
    /// from its current state to `screen` with `overlays` applied on top.
    ///
    /// Internally advances the "displayed" parser so the next call computes a
    /// correct differential.
    ///
    /// * `screen` – the current server-driven screen state.
    /// * `overlays` – predicted cells to paint on top of the real screen.
    /// * `cursor` – the predicted cursor position (overrides real cursor if
    ///   `Some`).
    #[must_use]
    pub fn render(
        &mut self,
        screen: &vt100::Screen,
        overlays: &[OverlayCell],
        cursor: Option<OverlayCursor>,
    ) -> Vec<u8> {
        let new_alt = screen.alternate_screen();
        let old_alt = self.displayed.screen().alternate_screen();
        // An alt-screen buffer swap discards the previous buffer's contents, so
        // a diff against `displayed` would be meaningless — force a full repaint.
        let alt_changed = new_alt != old_alt;

        // ── 1. build the target framebuffer (server screen + predictions) ──
        let (rows, cols) = screen.size();
        let mut frame = vt100::Parser::new(rows, cols, 0);
        frame.process(&screen.contents_formatted());

        // Merge predicted cells as real content so the diff treats them as
        // first-class cells (and self-heals when they are later culled).
        if !overlays.is_empty() {
            let mut paint: Vec<u8> = Vec::with_capacity(overlays.len() * 12);
            for cell in overlays {
                write_to_vec(
                    &mut paint,
                    format_args!("\x1b[{};{}H", cell.row + 1, cell.col + 1),
                );
                if cell.flagged {
                    paint.extend_from_slice(b"\x1b[4m");
                }
                let mut char_buf = [0u8; 4];
                paint.extend_from_slice(cell.ch.encode_utf8(&mut char_buf).as_bytes());
                if cell.flagged {
                    paint.extend_from_slice(b"\x1b[24m");
                }
            }
            frame.process(&paint);
        }

        // Place the cursor at its final resting position (predicted or real)
        // inside the frame so the diff carries the correct cursor move.
        let (cur_row, cur_col) = if let Some(oc) = cursor {
            (oc.row, oc.col)
        } else {
            screen.cursor_position()
        };
        let mut mv: Vec<u8> = Vec::with_capacity(12);
        write_to_vec(
            &mut mv,
            format_args!("\x1b[{};{}H", cur_row + 1, cur_col + 1),
        );
        frame.process(&mv);

        // ── 2. emit the update ────────────────────────────────────────────
        let mut out: Vec<u8> = Vec::with_capacity(4096);
        // Emit the alt-screen transition first so the terminal is in the
        // correct buffer before the content bytes are applied.
        if new_alt && !old_alt {
            out.extend_from_slice(b"\x1b[?1049h");
        } else if !new_alt && old_alt {
            out.extend_from_slice(b"\x1b[?1049l");
        }

        if self.initialized && !alt_changed {
            out.extend_from_slice(&frame.screen().contents_diff(self.displayed.screen()));
        } else {
            // First render, post-resize/invalidate, or an alt-screen swap.
            out.extend_from_slice(&frame.screen().contents_formatted());
            self.initialized = true;
        }

        // ── 3. advance the "displayed" parser ─────────────────────────────
        // Process exactly the bytes we emit so the next diff is computed
        // against what is physically on the user's screen.
        self.displayed.process(&out);

        out
    }

    /// Force a complete screen redraw on the next call to [`Renderer::render`].
    pub fn invalidate(&mut self) {
        self.initialized = false;
    }
}

/// Emit the ANSI sequences for `overlays` and `cursor` without touching any
/// renderer state.  Used by the stdin forwarder to preview predicted keystrokes
/// on top of whatever is currently displayed — without modifying the
/// differential-render baseline.
#[must_use]
pub fn paint_overlays_to_ansi(overlays: &[OverlayCell], cursor: Option<OverlayCursor>) -> Vec<u8> {
    if overlays.is_empty() && cursor.is_none() {
        return Vec::new();
    }

    let mut out: Vec<u8> = Vec::with_capacity(256);

    if !overlays.is_empty() {
        out.extend_from_slice(b"\x1b[s"); // save cursor
        for cell in overlays {
            write_to_vec(
                &mut out,
                format_args!("\x1b[{};{}H", cell.row + 1, cell.col + 1),
            );
            if cell.flagged {
                out.extend_from_slice(b"\x1b[4m");
            }
            let mut char_buf = [0u8; 4];
            let s = cell.ch.encode_utf8(&mut char_buf);
            out.extend_from_slice(s.as_bytes());
            if cell.flagged {
                out.extend_from_slice(b"\x1b[24m");
            }
        }
        out.extend_from_slice(b"\x1b[m"); // reset SGR
        out.extend_from_slice(b"\x1b[u"); // restore cursor to pre-paint position
    }

    if let Some(oc) = cursor {
        write_to_vec(
            &mut out,
            format_args!("\x1b[{};{}H", oc.row + 1, oc.col + 1),
        );
    }

    out
}

/// Render a single clean update to the terminal after the server has changed
/// the emulator's screen state.
///
/// Reconciles outstanding predictions against the new screen (`cull` +
/// `apply`) and renders the result through the shared [`Renderer`], so the
/// renderer remains the sole writer to the terminal and its `displayed`
/// baseline stays exact.  Pass `with_predictions = false` to skip the
/// prediction work entirely (e.g. in alternate-screen mode where a full-screen
/// app owns the display).
///
/// Locks are always acquired in the order **emulator → prediction → renderer**
/// to match [`render_prediction_update`] and avoid deadlocks.
///
/// # Panics
/// Never panics; poisoned mutexes are recovered via [`std::sync::PoisonError`].
#[must_use]
pub fn render_server_update(
    emulator: &Arc<Mutex<Emulator>>,
    prediction: &Arc<Mutex<PredictionEngine>>,
    renderer: &Arc<Mutex<Renderer>>,
    with_predictions: bool,
) -> Vec<u8> {
    let emu = emulator
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let screen = emu.screen();
    let (overlays, cursor) = if with_predictions {
        let mut pred = prediction
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        pred.cull(screen);
        pred.apply(screen)
    } else {
        (Vec::new(), None)
    };
    let mut rend = renderer
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    rend.render(screen, &overlays, cursor)
}

/// Render a single clean update reflecting a locally-predicted keystroke.
///
/// Feeds each byte of `new_bytes` to the prediction engine, then renders the
/// emulator's current screen with the resulting overlays through the shared
/// [`Renderer`].  Unlike [`render_server_update`] this does **not** cull, since
/// no new server data has arrived — it only adds the just-typed prediction.
///
/// Locks are always acquired in the order **emulator → prediction → renderer**.
///
/// # Panics
/// Never panics; poisoned mutexes are recovered via [`std::sync::PoisonError`].
#[must_use]
pub fn render_prediction_update(
    emulator: &Arc<Mutex<Emulator>>,
    prediction: &Arc<Mutex<PredictionEngine>>,
    renderer: &Arc<Mutex<Renderer>>,
    new_bytes: &[u8],
) -> Vec<u8> {
    let emu = emulator
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let screen = emu.screen();
    let (overlays, cursor) = {
        let mut pred = prediction
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        for &byte in new_bytes {
            pred.new_user_byte(byte, screen);
        }
        pred.apply(screen)
    };
    let mut rend = renderer
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    rend.render(screen, &overlays, cursor)
}

// Helper: write a `fmt::Arguments` into a `Vec<u8>` without allocation.
fn write_to_vec(buf: &mut Vec<u8>, args: fmt::Arguments<'_>) {
    use std::io::Write as _;
    drop(buf.write_fmt(args));
}

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

    #[test]
    fn set_size_resets_renderer_and_updates_dimensions() {
        let mut renderer = Renderer::new(24, 80);
        // Force initialized state by rendering once.
        let mut parser = vt100::Parser::new(24, 80, 0);
        parser.process(b"hello");
        drop(renderer.render(parser.screen(), &[], None));
        assert!(renderer.initialized);

        // set_size should clear initialized so next render is a full refresh.
        renderer.set_size(30, 100);
        assert!(!renderer.initialized);
    }

    #[test]
    fn renderer_new_is_not_initialized() {
        let r = Renderer::new(24, 80);
        assert!(!r.initialized);
    }

    #[test]
    fn renderer_first_render_sets_initialized() {
        let mut r = Renderer::new(24, 80);
        let parser = vt100::Parser::new(24, 80, 0);
        let out = r.render(parser.screen(), &[], None);
        assert!(r.initialized);
        assert!(!out.is_empty());
    }

    #[test]
    fn renderer_invalidate_clears_initialized() {
        let mut r = Renderer::new(24, 80);
        let parser = vt100::Parser::new(24, 80, 0);
        drop(r.render(parser.screen(), &[], None));
        assert!(r.initialized);
        r.invalidate();
        assert!(!r.initialized);
    }

    #[test]
    fn paint_overlays_to_ansi_empty_returns_empty() {
        let out = paint_overlays_to_ansi(&[], None);
        assert!(out.is_empty());
    }

    #[test]
    fn paint_overlays_to_ansi_with_cell_contains_escape_sequences() {
        use super::super::prediction::OverlayCell;
        let cells = vec![OverlayCell {
            row: 0,
            col: 0,
            ch: 'a',
            flagged: false,
        }];
        let out = paint_overlays_to_ansi(&cells, None);
        let s = String::from_utf8_lossy(&out);
        assert!(s.contains("\x1b[s"));
        assert!(s.contains("\x1b[1;1H"));
        assert!(s.contains('a'));
        assert!(s.contains("\x1b[u"));
    }

    // ── Phase 3: extended renderer tests ──────────────────────────────────────

    #[test]
    fn render_with_content_produces_nonempty_output() {
        let mut r = Renderer::new(24, 80);
        let mut parser = vt100::Parser::new(24, 80, 0);
        parser.process(b"hello");
        let out = r.render(parser.screen(), &[], None);
        assert!(!out.is_empty());
        let s = String::from_utf8_lossy(&out);
        assert!(s.contains("hello"));
    }

    #[test]
    fn render_second_call_with_no_change_produces_minimal_output() {
        let mut r = Renderer::new(24, 80);
        let mut parser = vt100::Parser::new(24, 80, 0);
        parser.process(b"hello");
        // First render (full refresh)
        let first = r.render(parser.screen(), &[], None);
        assert!(!first.is_empty());
        // Second render without changes: only cursor positioning
        let second = r.render(parser.screen(), &[], None);
        // Should be much shorter than the first (just cursor move, no cell redraws)
        assert!(
            second.len() < first.len(),
            "second render with no changes should be smaller"
        );
    }

    // A stand-in for the user's physical terminal: feed it every byte the
    // renderer emits and inspect the resulting screen state.  This mirrors the
    // renderer's internal `displayed` parser and lets tests assert on *visual*
    // outcomes instead of brittle escape-sequence substrings.
    fn apply(term: &mut vt100::Parser, bytes: &[u8]) {
        term.process(bytes);
    }

    #[test]
    fn render_with_overlay_cell_paints_overlay_character() {
        use super::super::prediction::OverlayCell;
        let mut r = Renderer::new(24, 80);
        let mut term = vt100::Parser::new(24, 80, 0);
        let parser = vt100::Parser::new(24, 80, 0);
        let overlays = vec![OverlayCell {
            row: 0,
            col: 0,
            ch: 'Z',
            flagged: false,
        }];
        apply(&mut term, &r.render(parser.screen(), &overlays, None));
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("Z"),
            "overlay character 'Z' must be painted at (0,0)"
        );
    }

    #[test]
    fn render_with_flagged_overlay_underlines_the_cell() {
        use super::super::prediction::OverlayCell;
        let mut r = Renderer::new(24, 80);
        let mut term = vt100::Parser::new(24, 80, 0);
        let parser = vt100::Parser::new(24, 80, 0);
        let overlays = vec![OverlayCell {
            row: 2,
            col: 5,
            ch: 'F',
            flagged: true,
        }];
        apply(&mut term, &r.render(parser.screen(), &overlays, None));
        let cell = term.screen().cell(2, 5).expect("cell exists");
        assert_eq!(cell.contents(), "F");
        assert!(cell.underline(), "flagged overlay cell must be underlined");
    }

    #[test]
    fn render_with_cursor_override_positions_cursor_at_override() {
        use super::super::prediction::OverlayCursor;
        let mut r = Renderer::new(24, 80);
        let mut term = vt100::Parser::new(24, 80, 0);
        let parser = vt100::Parser::new(24, 80, 0);
        let cursor_override = Some(OverlayCursor { row: 5, col: 10 });
        apply(&mut term, &r.render(parser.screen(), &[], cursor_override));
        assert_eq!(
            term.screen().cursor_position(),
            (5, 10),
            "cursor override must place the cursor at (5,10)"
        );
    }

    #[test]
    fn render_culled_prediction_self_heals() {
        use super::super::prediction::OverlayCell;
        // The server screen shows 'a' at (0,0).
        let mut server = vt100::Parser::new(24, 80, 0);
        server.process(b"a");

        let mut r = Renderer::new(24, 80);
        let mut term = vt100::Parser::new(24, 80, 0);

        // Frame 1: a prediction overlays 'X' over the real 'a'.
        let overlay = vec![OverlayCell {
            row: 0,
            col: 0,
            ch: 'X',
            flagged: false,
        }];
        apply(&mut term, &r.render(server.screen(), &overlay, None));
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("X"),
            "prediction must be visible in frame 1"
        );

        // Frame 2: the prediction is culled (no overlays) — the real cell must
        // be repainted with no leftover predicted glyph.
        apply(&mut term, &r.render(server.screen(), &[], None));
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("a"),
            "culled prediction must self-heal back to the real cell"
        );
    }

    #[test]
    fn render_no_sgr_bleed_after_flagged_overlay() {
        use super::super::prediction::OverlayCell;
        // Plain (non-underlined) server text "hi" at (0,0)-(0,1).
        let mut server = vt100::Parser::new(24, 80, 0);
        server.process(b"hi");

        let mut r = Renderer::new(24, 80);
        let mut term = vt100::Parser::new(24, 80, 0);

        // A flagged (underlined) prediction at (0,5) must not leave underline
        // state bleeding onto the plain server cells.
        let overlay = vec![OverlayCell {
            row: 0,
            col: 5,
            ch: 'P',
            flagged: true,
        }];
        apply(&mut term, &r.render(server.screen(), &overlay, None));
        assert!(
            !term.screen().cell(0, 0).expect("cell").underline(),
            "plain server cell must not inherit the overlay's underline"
        );
        assert!(
            term.screen().cell(0, 5).expect("cell").underline(),
            "flagged overlay cell should be underlined"
        );
    }

    #[test]
    fn paint_overlays_to_ansi_with_cursor_positions_cursor() {
        use super::super::prediction::OverlayCursor;
        let out = paint_overlays_to_ansi(&[], Some(OverlayCursor { row: 3, col: 7 }));
        let s = String::from_utf8_lossy(&out);
        assert!(
            s.contains("\x1b[4;8H"),
            "cursor overlay must produce ESC[4;8H: {s:?}"
        );
    }

    #[test]
    fn render_emits_alt_screen_enter_on_transition() {
        let mut r = Renderer::new(24, 80);
        // First render to initialise (main screen).
        let mut p1 = vt100::Parser::new(24, 80, 0);
        p1.process(b"hello");
        drop(r.render(p1.screen(), &[], None));

        // Switch to alt-screen.
        let mut p2 = vt100::Parser::new(24, 80, 0);
        p2.process(b"\x1b[?1049h");
        let out = r.render(p2.screen(), &[], None);
        let s = String::from_utf8_lossy(&out);
        assert!(
            s.contains("\x1b[?1049h"),
            "alt-screen enter must appear in output when transitioning to alt-screen: {s:?}"
        );
    }

    #[test]
    fn render_emits_alt_screen_exit_on_transition() {
        let mut r = Renderer::new(24, 80);
        // Start in alt-screen.
        let mut p1 = vt100::Parser::new(24, 80, 0);
        p1.process(b"\x1b[?1049h");
        drop(r.render(p1.screen(), &[], None));

        // Switch back to main screen.
        let mut p2 = vt100::Parser::new(24, 80, 0);
        p2.process(b"\x1b[?1049h\x1b[?1049l");
        let out = r.render(p2.screen(), &[], None);
        let s = String::from_utf8_lossy(&out);
        assert!(
            s.contains("\x1b[?1049l"),
            "alt-screen exit must appear in output when transitioning to main screen: {s:?}"
        );
    }

    #[test]
    fn render_size_change_triggers_full_refresh() {
        let mut r = Renderer::new(24, 80);
        let mut parser = vt100::Parser::new(24, 80, 0);
        parser.process(b"hello");
        // First render to initialise
        let first = r.render(parser.screen(), &[], None);
        assert!(!first.is_empty());
        // Change size
        r.set_size(30, 100);
        assert!(!r.initialized);
        // Next render should be a full refresh (larger output)
        let mut parser2 = vt100::Parser::new(30, 100, 0);
        parser2.process(b"world");
        let after_resize = r.render(parser2.screen(), &[], None);
        assert!(r.initialized);
        assert!(!after_resize.is_empty());
    }

    use super::super::prediction::DisplayPreference;

    /// The shared `(emulator, prediction, renderer)` trio the free-function
    /// renderers operate on.
    type RenderTrio = (
        Arc<Mutex<Emulator>>,
        Arc<Mutex<PredictionEngine>>,
        Arc<Mutex<Renderer>>,
    );

    // Build the render trio with a blank 24×80 screen.
    fn render_trio() -> RenderTrio {
        (
            Arc::new(Mutex::new(Emulator::new(24, 80))),
            Arc::new(Mutex::new(PredictionEngine::new(DisplayPreference::Always))),
            Arc::new(Mutex::new(Renderer::new(24, 80))),
        )
    }

    #[test]
    fn render_server_update_paints_emulator_screen() {
        let (emulator, prediction, renderer) = render_trio();
        emulator.lock().unwrap().process(b"server text");
        let mut term = vt100::Parser::new(24, 80, 0);
        apply(
            &mut term,
            &render_server_update(&emulator, &prediction, &renderer, true),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("s"),
            "server screen content must be rendered to the terminal"
        );
    }

    #[test]
    fn render_server_update_culls_stale_predictions() {
        let (emulator, prediction, renderer) = render_trio();
        // The emulator already shows 'a' at (0,0); register a prediction that
        // the server screen contradicts so cull must discard it.
        emulator.lock().unwrap().process(b"a");
        {
            let emu = emulator.lock().unwrap();
            let mut pred = prediction.lock().unwrap();
            pred.new_user_byte(b'Z', emu.screen());
        }
        let mut term = vt100::Parser::new(24, 80, 0);
        apply(
            &mut term,
            &render_server_update(&emulator, &prediction, &renderer, true),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("a"),
            "a prediction the server screen contradicts must be culled, leaving the real cell"
        );
    }

    #[test]
    fn render_server_update_without_predictions_skips_overlays() {
        let (emulator, prediction, renderer) = render_trio();
        emulator.lock().unwrap().process(b"hello");
        // Stash a prediction that would change the display if applied.
        {
            let emu = emulator.lock().unwrap();
            let mut pred = prediction.lock().unwrap();
            pred.new_user_byte(b'X', emu.screen());
        }
        let mut term = vt100::Parser::new(24, 80, 0);
        // with_predictions = false must ignore the pending prediction entirely.
        apply(
            &mut term,
            &render_server_update(&emulator, &prediction, &renderer, false),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("h"),
            "with_predictions=false must render only the raw server screen"
        );
    }

    #[test]
    fn render_prediction_update_paints_keystroke_once_confirmed() {
        let (emulator, prediction, renderer) = render_trio();
        // Prime the prediction engine with the terminal dimensions so the first
        // confirming cull doesn't reset everything.
        {
            let emu = emulator.lock().unwrap();
            prediction.lock().unwrap().cull(emu.screen());
        }
        let mut term = vt100::Parser::new(24, 80, 0);

        // Type 'k' at (0,0): a fresh prediction is tentative and not yet shown.
        apply(
            &mut term,
            &render_prediction_update(&emulator, &prediction, &renderer, b"k"),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some(""),
            "a fresh (tentative) prediction must not be painted yet"
        );

        // A server update advances the real cursor to the predicted column
        // (0,1) without echoing the keystroke yet — this confirms the epoch.
        emulator.lock().unwrap().process(b"\x1b[1;2H");
        apply(
            &mut term,
            &render_server_update(&emulator, &prediction, &renderer, true),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("k"),
            "once the epoch is confirmed the predicted keystroke must be painted"
        );
    }

    #[test]
    fn render_prediction_update_does_not_cull_between_keystrokes() {
        let (emulator, prediction, renderer) = render_trio();
        {
            let emu = emulator.lock().unwrap();
            prediction.lock().unwrap().cull(emu.screen());
        }
        let mut term = vt100::Parser::new(24, 80, 0);

        // Two predicted bytes in sequence; neither call culls, so both
        // predictions survive to be confirmed together.
        apply(
            &mut term,
            &render_prediction_update(&emulator, &prediction, &renderer, b"a"),
        );
        apply(
            &mut term,
            &render_prediction_update(&emulator, &prediction, &renderer, b"b"),
        );

        // Confirm both predictions: real cursor advances to (0,2).
        emulator.lock().unwrap().process(b"\x1b[1;3H");
        apply(
            &mut term,
            &render_server_update(&emulator, &prediction, &renderer, true),
        );
        assert_eq!(
            term.screen().cell(0, 0).map(vt100::Cell::contents),
            Some("a"),
            "first prediction must survive the second keystroke"
        );
        assert_eq!(
            term.screen().cell(0, 1).map(vt100::Cell::contents),
            Some("b"),
            "second prediction must be appended alongside the first"
        );
    }
}