ftui-backend 0.9.0

Backend traits for FrankenTUI: platform abstraction for input, presentation, and time.
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
#![forbid(unsafe_code)]
#![doc = "Backend traits for FrankenTUI: platform abstraction for input, presentation, and time."]
#![doc = ""]
#![doc = "This crate defines the boundary between the ftui runtime and platform-specific"]
#![doc = "implementations (native terminal via `ftui-tty`, WASM via `ftui-web`)."]
#![doc = ""]
#![doc = "See ADR-008 for the design rationale."]

use core::time::Duration;
use std::sync::Arc;

use ftui_core::event::Event;
use ftui_core::osc52::ClipboardSelection;
use ftui_core::terminal_capabilities::TerminalCapabilities;
use ftui_render::buffer::Buffer;
use ftui_render::diff::BufferDiff;
pub use ftui_render::diff_strategy::DiffStrategy;
use ftui_render::grapheme_pool::GraphemePool;
use ftui_render::link_registry::LinkRegistry;
pub use ftui_render::sanitize::SanitizeMode;

/// Presentation phase timing measurements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct PresentTimings {
    /// Time spent computing the buffer diff in microseconds.
    pub diff_us: u64,
}

/// Screen mode determines whether we use alternate screen or inline mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScreenMode {
    /// Inline mode preserves scrollback. UI is anchored at bottom/top.
    Inline {
        /// Height of the UI region in rows.
        ui_height: u16,
    },
    /// Inline mode with automatic UI height based on rendered content.
    ///
    /// The measured height is clamped between `min_height` and `max_height`.
    InlineAuto {
        /// Minimum UI height in rows.
        min_height: u16,
        /// Maximum UI height in rows.
        max_height: u16,
    },
    /// Alternate screen mode for full-screen applications.
    #[default]
    AltScreen,
}

/// Where the UI region is anchored in inline mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UiAnchor {
    /// UI at bottom of terminal (default for agent harness).
    #[default]
    Bottom,
    /// UI at top of terminal.
    Top,
}

/// Terminal feature toggles that backends must support.
///
/// These map to terminal modes that are enabled/disabled at session start/end.
/// Backends translate these into platform-specific escape sequences or API calls.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BackendFeatures {
    /// SGR mouse capture
    /// (`CSI ? 1000 h` + `CSI ? 1002 h` + `CSI ? 1003 h` + `CSI ? 1006 h`, and matching `l` disables).
    pub mouse_capture: bool,
    /// Bracketed paste mode (CSI ? 2004 h/l on native).
    pub bracketed_paste: bool,
    /// Focus-in/focus-out reporting (CSI ? 1004 h/l on native).
    pub focus_events: bool,
    /// Kitty keyboard protocol (CSI > 15 u on native).
    pub kitty_keyboard: bool,
}

/// Monotonic clock abstraction.
///
/// Native backends use `std::time::Instant`; WASM backends use `performance.now()`.
/// The runtime never calls `Instant::now()` directly — all time flows through this trait.
pub trait BackendClock {
    /// Returns elapsed time since an unspecified epoch, monotonically increasing.
    fn now_mono(&self) -> Duration;
}

/// Event source abstraction: terminal size queries, feature toggles, and event I/O.
///
/// This is the input half of the backend boundary. The runtime polls this for
/// canonical `Event` values without knowing whether they come from crossterm,
/// raw Unix reads, or DOM events.
pub trait BackendEventSource {
    /// Platform-specific error type.
    type Error: core::fmt::Debug + core::fmt::Display;

    /// Query current terminal dimensions (columns, rows).
    fn size(&self) -> Result<(u16, u16), Self::Error>;

    /// Enable or disable terminal features (mouse, paste, focus, kitty keyboard).
    ///
    /// Backends must track current state and only emit escape sequences for changes.
    fn set_features(&mut self, features: BackendFeatures) -> Result<(), Self::Error>;

    /// Poll for an available event, returning `true` if one is ready.
    ///
    /// Must not block longer than `timeout`. Returns `Ok(false)` on timeout.
    fn poll_event(&mut self, timeout: Duration) -> Result<bool, Self::Error>;

    /// Read the next available event, or `None` if none is ready.
    ///
    /// Call after `poll_event` returns `true`, or speculatively.
    ///
    /// A prior `poll_event` readiness result is a hint, not a guarantee:
    /// implementations may return `Ok(None)` even immediately after a `true`
    /// poll (e.g. the pending input was discarded by the parser, or, on
    /// Windows, console readiness was spurious — see frankentui#95).
    /// Implementations must not park indefinitely when no event is actually
    /// pending; callers must treat `Ok(None)` as "nothing decodable right
    /// now" and return to their poll loop.
    fn read_event(&mut self) -> Result<Option<Event>, Self::Error>;

    /// Whether [`suspend`](Self::suspend) can hand the terminal back for a
    /// job-control stop. The runtime claims SIGTSTP/SIGTTIN/SIGTTOU only for a
    /// source that says so; others keep the signals' default action.
    fn supports_suspend(&self) -> bool {
        false
    }

    /// Hand the terminal back to the shell ahead of a job-control stop:
    /// disable input modes, leave the alternate screen and restore the
    /// original terminal modes, keeping what is needed to undo it.
    ///
    /// Returns `Ok(false)` when this source owns no terminal session to
    /// release; the runtime then does not stop the process.
    fn suspend(&mut self) -> Result<bool, Self::Error> {
        Ok(false)
    }

    /// Undo [`suspend`](Self::suspend) after the process is continued.
    /// A no-op when nothing is suspended.
    fn resume(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

/// Presentation abstraction: UI rendering and log output.
///
/// This is the output half of the backend boundary. The runtime hands a `Buffer`
/// (and optional `BufferDiff`) to the presenter, which emits platform-specific
/// output (ANSI escape sequences on native, DOM mutations on web).
pub trait BackendPresenter {
    /// Platform-specific error type.
    type Error: core::fmt::Debug + core::fmt::Display;

    /// Terminal capabilities detected by this backend.
    fn capabilities(&self) -> &TerminalCapabilities;

    /// Write a log line to the scrollback region (inline mode) or stderr.
    fn write_log(&mut self, text: &str) -> Result<(), Self::Error>;

    /// Present a UI frame.
    ///
    /// - `buf`: the full rendered buffer for this frame.
    /// - `diff`: optional pre-computed diff (backends may recompute if `None`).
    /// - `full_repaint_hint`: if `true`, the backend should skip diffing and repaint everything.
    fn present_ui(
        &mut self,
        buf: &Buffer,
        diff: Option<&BufferDiff>,
        full_repaint_hint: bool,
    ) -> Result<(), Self::Error>;

    /// Optional: release resources held by the presenter (e.g., grapheme pool compaction).
    fn gc(&mut self) {}

    /// Resize the presentation surface.
    fn resize(&mut self, _cols: u16, _rows: u16) {}

    /// Current screen mode.
    fn screen_mode(&self) -> ScreenMode {
        ScreenMode::default()
    }

    /// Set screen mode.
    fn set_screen_mode(&mut self, _mode: ScreenMode) {}

    /// Configure an evidence-writer callback.
    fn set_evidence_writer(&mut self, _w: Option<Arc<dyn Fn(&str) + Send + Sync>>) {}

    /// Last diff strategy used, if any.
    fn last_diff_strategy(&self) -> Option<DiffStrategy> {
        None
    }

    /// Last diff elapsed duration, if any.
    fn last_diff_elapsed(&self) -> Option<Duration> {
        None
    }

    /// Begin shutting down the presenter.
    fn begin_shutdown(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Flush any pending output to the terminal or host.
    fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Release presenter-owned terminal state ahead of a job-control stop:
    /// close an open synchronized-output block, reset the scroll region, show
    /// the cursor. On resume the runtime calls [`resize`](Self::resize), which
    /// must drop any diff baseline, since the screen is no longer ours.
    fn suspend(&mut self) -> Result<(), Self::Error> {
        self.flush()
    }

    /// Take a reusable render buffer sized for the current frame.
    fn take_render_buffer(&mut self, width: u16, height: u16) -> Buffer {
        Buffer::new(width, height)
    }

    /// Mark the beginning of a frame for hyperlink tracking.
    fn begin_link_frame(&mut self) {}

    /// Mutably borrow the grapheme pool and link registry.
    fn pool_and_links_mut(&mut self) -> (&mut GraphemePool, &mut LinkRegistry);

    /// Mutably borrow the grapheme pool.
    fn pool_mut(&mut self) -> &mut GraphemePool;

    /// Configured bounds for inline auto mode, if active.
    fn inline_auto_bounds(&self) -> Option<(u16, u16)> {
        None
    }

    /// Current measured or auto-determined UI height for inline auto mode.
    fn auto_ui_height(&self) -> Option<u16> {
        None
    }

    /// Height hint to use when rendering a frame.
    fn render_height_hint(&self) -> u16 {
        0
    }

    /// Set auto UI height for inline auto mode.
    fn set_auto_ui_height(&mut self, _height: u16) {}

    /// Clear auto UI height for inline auto mode.
    fn clear_auto_ui_height(&mut self) {}

    /// Take presentation timings from the last rendered frame, if recorded.
    fn take_last_present_timings(&mut self) -> Option<PresentTimings> {
        None
    }

    /// Estimate memory usage of buffers and pools held by the presenter.
    fn estimate_memory_usage(&self) -> usize {
        0
    }

    /// Present a UI frame, taking ownership of the buffer.
    fn present_ui_owned(
        &mut self,
        buf: Buffer,
        cursor: Option<(u16, u16)>,
        cursor_visible: bool,
    ) -> Result<(), Self::Error> {
        let _ = (cursor, cursor_visible);
        self.present_ui(&buf, None, false)
    }

    /// Write a log line with a specific sanitization mode.
    fn write_log_line_with_mode(
        &mut self,
        text: &str,
        _mode: SanitizeMode,
    ) -> Result<(), Self::Error> {
        self.write_log(text)
    }

    /// Write an OSC 52 set clipboard sequence.
    fn write_osc52_set(
        &mut self,
        _selection: ClipboardSelection,
        _data: &[u8],
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Write an OSC 52 query clipboard sequence.
    fn write_osc52_query(&mut self, _selection: ClipboardSelection) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Enable or disable presentation timing measurements.
    fn set_timing_enabled(&mut self, _enabled: bool) {}

    /// Set hyperlink limit for tracking.
    fn set_hyperlink_limit(&mut self, _limit: usize) {}
}

/// Unified backend combining clock and event source.
///
/// The `Program` runtime drives event handling through this trait (or through
/// [`BackendEventSource`] directly), and presents through [`BackendPresenter`].
/// Concrete implementations:
/// - `ftui-tty`: native Unix/macOS terminal (and eventually Windows).
/// - `ftui-web`: WASM + DOM + WebGPU renderer.
pub trait Backend {
    /// Platform-specific error type shared across sub-traits.
    type Error: core::fmt::Debug + core::fmt::Display;

    /// Clock implementation.
    type Clock: BackendClock;

    /// Event source implementation.
    type Events: BackendEventSource<Error = Self::Error>;

    /// Access the monotonic clock.
    fn clock(&self) -> &Self::Clock;

    /// Access the event source (mutable for polling/reading).
    fn events(&mut self) -> &mut Self::Events;
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::fmt;
    use ftui_core::terminal_capabilities::TerminalCapabilities;

    // -----------------------------------------------------------------------
    // BackendFeatures tests
    // -----------------------------------------------------------------------

    #[test]
    fn backend_features_default_all_false() {
        let f = BackendFeatures::default();
        assert!(!f.mouse_capture);
        assert!(!f.bracketed_paste);
        assert!(!f.focus_events);
        assert!(!f.kitty_keyboard);
    }

    #[test]
    fn backend_features_equality() {
        let a = BackendFeatures {
            mouse_capture: true,
            bracketed_paste: false,
            focus_events: true,
            kitty_keyboard: false,
        };
        let b = BackendFeatures {
            mouse_capture: true,
            bracketed_paste: false,
            focus_events: true,
            kitty_keyboard: false,
        };
        assert_eq!(a, b);
    }

    #[test]
    fn backend_features_inequality() {
        let a = BackendFeatures::default();
        let b = BackendFeatures {
            mouse_capture: true,
            ..BackendFeatures::default()
        };
        assert_ne!(a, b);
    }

    #[test]
    fn backend_features_clone() {
        let a = BackendFeatures {
            mouse_capture: true,
            bracketed_paste: true,
            focus_events: true,
            kitty_keyboard: true,
        };
        let b = a;
        assert_eq!(a, b);
    }

    #[test]
    fn backend_features_debug() {
        let f = BackendFeatures::default();
        let debug = format!("{f:?}");
        assert!(debug.contains("BackendFeatures"));
        assert!(debug.contains("mouse_capture"));
    }

    // -----------------------------------------------------------------------
    // Mock implementations for trait testing
    // -----------------------------------------------------------------------

    struct TestClock {
        elapsed: Duration,
    }

    impl BackendClock for TestClock {
        fn now_mono(&self) -> Duration {
            self.elapsed
        }
    }

    #[derive(Debug)]
    struct TestError(String);

    impl fmt::Display for TestError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "TestError: {}", self.0)
        }
    }

    struct TestEventSource {
        features: BackendFeatures,
        events: Vec<Event>,
    }

    impl BackendEventSource for TestEventSource {
        type Error = TestError;

        fn size(&self) -> Result<(u16, u16), Self::Error> {
            Ok((80, 24))
        }

        fn set_features(&mut self, features: BackendFeatures) -> Result<(), Self::Error> {
            self.features = features;
            Ok(())
        }

        fn poll_event(&mut self, _timeout: Duration) -> Result<bool, Self::Error> {
            Ok(!self.events.is_empty())
        }

        fn read_event(&mut self) -> Result<Option<Event>, Self::Error> {
            Ok(if self.events.is_empty() {
                None
            } else {
                Some(self.events.remove(0))
            })
        }
    }

    struct TestPresenter {
        caps: TerminalCapabilities,
        logs: Vec<String>,
        present_count: usize,
        gc_count: usize,
        pool: GraphemePool,
        links: LinkRegistry,
    }

    impl BackendPresenter for TestPresenter {
        type Error = TestError;

        fn capabilities(&self) -> &TerminalCapabilities {
            &self.caps
        }

        fn write_log(&mut self, text: &str) -> Result<(), Self::Error> {
            self.logs.push(text.to_owned());
            Ok(())
        }

        fn present_ui(
            &mut self,
            _buf: &Buffer,
            _diff: Option<&BufferDiff>,
            _full_repaint_hint: bool,
        ) -> Result<(), Self::Error> {
            self.present_count += 1;
            Ok(())
        }

        fn gc(&mut self) {
            self.gc_count += 1;
        }

        fn pool_and_links_mut(&mut self) -> (&mut GraphemePool, &mut LinkRegistry) {
            (&mut self.pool, &mut self.links)
        }

        fn pool_mut(&mut self) -> &mut GraphemePool {
            &mut self.pool
        }
    }

    struct TestBackend {
        clock: TestClock,
        events: TestEventSource,
    }

    impl Backend for TestBackend {
        type Error = TestError;
        type Clock = TestClock;
        type Events = TestEventSource;

        fn clock(&self) -> &Self::Clock {
            &self.clock
        }

        fn events(&mut self) -> &mut Self::Events {
            &mut self.events
        }
    }

    fn make_test_backend() -> TestBackend {
        TestBackend {
            clock: TestClock {
                elapsed: Duration::from_millis(42),
            },
            events: TestEventSource {
                features: BackendFeatures::default(),
                events: Vec::new(),
            },
        }
    }

    // -----------------------------------------------------------------------
    // BackendClock tests
    // -----------------------------------------------------------------------

    #[test]
    fn clock_returns_elapsed() {
        let clock = TestClock {
            elapsed: Duration::from_secs(5),
        };
        assert_eq!(clock.now_mono(), Duration::from_secs(5));
    }

    #[test]
    fn clock_zero_duration() {
        let clock = TestClock {
            elapsed: Duration::ZERO,
        };
        assert_eq!(clock.now_mono(), Duration::ZERO);
    }

    // -----------------------------------------------------------------------
    // BackendEventSource tests
    // -----------------------------------------------------------------------

    #[test]
    fn event_source_size() {
        let src = TestEventSource {
            features: BackendFeatures::default(),
            events: Vec::new(),
        };
        assert_eq!(src.size().unwrap(), (80, 24));
    }

    #[test]
    fn event_source_set_features() {
        let mut src = TestEventSource {
            features: BackendFeatures::default(),
            events: Vec::new(),
        };
        let features = BackendFeatures {
            mouse_capture: true,
            bracketed_paste: true,
            focus_events: false,
            kitty_keyboard: false,
        };
        src.set_features(features).unwrap();
        assert!(src.features.mouse_capture);
        assert!(src.features.bracketed_paste);
    }

    #[test]
    fn event_source_poll_empty() {
        let mut src = TestEventSource {
            features: BackendFeatures::default(),
            events: Vec::new(),
        };
        assert!(!src.poll_event(Duration::from_millis(10)).unwrap());
    }

    #[test]
    fn event_source_read_none_when_empty() {
        let mut src = TestEventSource {
            features: BackendFeatures::default(),
            events: Vec::new(),
        };
        assert!(src.read_event().unwrap().is_none());
    }

    #[test]
    fn event_source_poll_with_events() {
        let mut src = TestEventSource {
            features: BackendFeatures::default(),
            events: vec![Event::Focus(true)],
        };
        assert!(src.poll_event(Duration::from_millis(10)).unwrap());
    }

    #[test]
    fn event_source_read_drains_events() {
        let mut src = TestEventSource {
            features: BackendFeatures::default(),
            events: vec![Event::Focus(true), Event::Focus(false)],
        };
        let e1 = src.read_event().unwrap();
        assert!(e1.is_some());
        let e2 = src.read_event().unwrap();
        assert!(e2.is_some());
        let e3 = src.read_event().unwrap();
        assert!(e3.is_none());
    }

    // -----------------------------------------------------------------------
    // BackendPresenter tests
    // -----------------------------------------------------------------------

    fn make_test_presenter() -> TestPresenter {
        TestPresenter {
            caps: TerminalCapabilities::default(),
            logs: Vec::new(),
            present_count: 0,
            gc_count: 0,
            pool: GraphemePool::new(),
            links: LinkRegistry::new(),
        }
    }

    #[test]
    fn presenter_capabilities() {
        let p = make_test_presenter();
        let _caps = p.capabilities();
    }

    #[test]
    fn presenter_write_log() {
        let mut p = make_test_presenter();
        p.write_log("hello").unwrap();
        p.write_log("world").unwrap();
        assert_eq!(p.logs.len(), 2);
        assert_eq!(p.logs[0], "hello");
        assert_eq!(p.logs[1], "world");
    }

    #[test]
    fn presenter_present_ui() {
        let mut p = make_test_presenter();
        let buf = Buffer::new(10, 5);
        p.present_ui(&buf, None, false).unwrap();
        p.present_ui(&buf, None, true).unwrap();
        assert_eq!(p.present_count, 2);
    }

    #[test]
    fn presenter_gc() {
        let mut p = make_test_presenter();
        p.gc();
        p.gc();
        assert_eq!(p.gc_count, 2);
    }

    #[test]
    fn presenter_defaults() {
        let mut p = make_test_presenter();
        assert_eq!(p.screen_mode(), ScreenMode::default());
        p.set_screen_mode(ScreenMode::AltScreen);
        p.resize(120, 40);
        assert_eq!(p.render_height_hint(), 0);
        assert_eq!(p.estimate_memory_usage(), 0);
        assert!(p.last_diff_strategy().is_none());
        assert!(p.last_diff_elapsed().is_none());
        assert!(p.begin_shutdown().is_ok());
        assert!(p.flush().is_ok());
    }

    // -----------------------------------------------------------------------
    // Unified Backend tests
    // -----------------------------------------------------------------------

    #[test]
    fn backend_clock_access() {
        let backend = make_test_backend();
        assert_eq!(backend.clock().now_mono(), Duration::from_millis(42));
    }

    #[test]
    fn backend_events_access() {
        let mut backend = make_test_backend();
        let size = backend.events().size().unwrap();
        assert_eq!(size, (80, 24));
    }

    #[test]
    fn backend_full_cycle() {
        let mut backend = make_test_backend();
        let mut presenter = make_test_presenter();

        // Clock
        let _now = backend.clock().now_mono();

        // Features
        backend
            .events()
            .set_features(BackendFeatures {
                mouse_capture: true,
                ..BackendFeatures::default()
            })
            .unwrap();
        assert!(backend.events.features.mouse_capture);

        // Present
        let buf = Buffer::new(80, 24);
        presenter.write_log("frame start").unwrap();
        presenter.present_ui(&buf, None, false).unwrap();
        presenter.gc();

        assert_eq!(presenter.logs.len(), 1);
        assert_eq!(presenter.present_count, 1);
        assert_eq!(presenter.gc_count, 1);
    }

    // -----------------------------------------------------------------------
    // Error type tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_error_display() {
        let err = TestError("something failed".into());
        assert_eq!(format!("{err}"), "TestError: something failed");
    }

    #[test]
    fn test_error_debug() {
        let err = TestError("oops".into());
        let debug = format!("{err:?}");
        assert!(debug.contains("oops"));
    }
}