ftui-extras 0.4.1

Feature-gated extras for FrankenTUI (markdown, charts, clipboard, themes).
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
//! Integration tests for the embedded terminal widget.
//!
//! Tests the full pipeline: PTY -> Parser -> State -> Widget -> Frame
//!
//! # Test Categories
//!
//! 1. **PTY Management**: Spawn/kill shell, environment inheritance
//! 2. **ANSI Rendering**: Colors, cursor, scrollback, wrapping
//! 3. **Input Forwarding**: Key sequences, modifiers, bracketed paste
//! 4. **Widget Rendering**: StatefulWidget render, cell conversion

#![cfg(all(feature = "terminal-widget", unix))]

use std::time::Duration;

use ftui_core::geometry::Rect;
use ftui_extras::terminal::{
    AnsiHandler, AnsiParser, CellAttrs, ClearRegion, TerminalEmulator, TerminalEmulatorState,
    TerminalModes, TerminalState,
};
use ftui_pty::input_forwarding::{BracketedPaste, Key, KeyEvent, Modifiers, key_to_sequence};
use ftui_pty::virtual_terminal::VirtualTerminal;
use ftui_pty::{PtyConfig, spawn_command};
use ftui_render::cell::StyleFlags;
use ftui_render::frame::Frame;
use ftui_render::grapheme_pool::GraphemePool;
use ftui_style::Color;
use ftui_widgets::StatefulWidget;
use portable_pty::CommandBuilder;
use vt100::Parser as Vt100Parser;

// ============================================================================
// PTY Management Tests
// ============================================================================

#[test]
fn pty_spawn_shell_success() {
    let config = PtyConfig::default()
        .with_size(80, 24)
        .with_test_name("pty_spawn_shell")
        .logging(false);

    let mut cmd = CommandBuilder::new("sh");
    cmd.args(["-c", "echo 'hello from shell'"]);

    let mut session = spawn_command(config, cmd).expect("spawn should succeed");
    let status = session.wait().expect("wait should succeed");

    assert!(status.success(), "shell should exit successfully");

    let output = session
        .read_until(b"hello from shell", Duration::from_secs(2))
        .expect("should capture output");
    assert!(
        output
            .windows(b"hello from shell".len())
            .any(|w| w == b"hello from shell"),
        "output should contain expected text"
    );
}

#[test]
fn pty_environment_inheritance() {
    let config = PtyConfig::default()
        .with_size(80, 24)
        .with_env("FTUI_TEST_VAR", "test_value_12345")
        .logging(false);

    let mut cmd = CommandBuilder::new("sh");
    cmd.args(["-c", "echo $FTUI_TEST_VAR"]);

    let mut session = spawn_command(config, cmd).expect("spawn should succeed");
    let _ = session.wait().expect("wait should succeed");

    let output = session
        .read_until(b"test_value_12345", Duration::from_secs(2))
        .expect("should capture env output");
    assert!(
        output
            .windows(b"test_value_12345".len())
            .any(|w| w == b"test_value_12345"),
        "environment variable should be inherited"
    );
}

#[test]
fn pty_term_variable_set() {
    let config = PtyConfig::default()
        .with_size(80, 24)
        .with_term("xterm-256color")
        .logging(false);

    let mut cmd = CommandBuilder::new("sh");
    cmd.args(["-c", "echo $TERM"]);

    let mut session = spawn_command(config, cmd).expect("spawn should succeed");
    let _ = session.wait().expect("wait should succeed");

    let output = session
        .read_until(b"xterm-256color", Duration::from_secs(2))
        .expect("should capture TERM output");
    assert!(
        output
            .windows(b"xterm-256color".len())
            .any(|w| w == b"xterm-256color"),
        "TERM should be set correctly"
    );
}

// ============================================================================
// ANSI Parser Integration Tests
// ============================================================================

struct TestHandler {
    state: TerminalState,
}

impl TestHandler {
    fn new(width: u16, height: u16) -> Self {
        Self {
            state: TerminalState::new(width, height),
        }
    }
}

impl AnsiHandler for TestHandler {
    fn print(&mut self, ch: char) {
        self.state.put_char(ch);
    }

    fn execute(&mut self, byte: u8) {
        match byte {
            0x08 => self.state.move_cursor_relative(-1, 0), // BS
            0x09 => {
                // Tab: move to next 8-col stop
                let x = self.state.cursor().x;
                let next = ((x / 8) + 1) * 8;
                self.state.move_cursor(next, self.state.cursor().y);
            }
            0x0A..=0x0C => {
                // LF, VT, FF
                let cursor = self.state.cursor();
                if cursor.y + 1 >= self.state.height() {
                    self.state.scroll_up(1);
                } else {
                    self.state.move_cursor_relative(0, 1);
                }
            }
            0x0D => {
                // CR
                self.state.move_cursor(0, self.state.cursor().y);
            }
            _ => {}
        }
    }

    fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], action: char) {
        match (action, intermediates) {
            ('H', []) | ('f', []) => {
                let row = params.first().copied().unwrap_or(1).max(1) as u16;
                let col = params.get(1).copied().unwrap_or(1).max(1) as u16;
                self.state
                    .move_cursor(col.saturating_sub(1), row.saturating_sub(1));
            }
            ('A', []) => {
                let n = params.first().copied().unwrap_or(1).max(1) as i16;
                self.state.move_cursor_relative(0, -n);
            }
            ('B', []) => {
                let n = params.first().copied().unwrap_or(1).max(1) as i16;
                self.state.move_cursor_relative(0, n);
            }
            ('C', []) => {
                let n = params.first().copied().unwrap_or(1).max(1) as i16;
                self.state.move_cursor_relative(n, 0);
            }
            ('D', []) => {
                let n = params.first().copied().unwrap_or(1).max(1) as i16;
                self.state.move_cursor_relative(-n, 0);
            }
            ('J', []) => {
                let mode = params.first().copied().unwrap_or(0);
                match mode {
                    0 => self.state.clear_region(ClearRegion::CursorToEnd),
                    1 => self.state.clear_region(ClearRegion::StartToCursor),
                    2 | 3 => self.state.clear_region(ClearRegion::All),
                    _ => {}
                }
            }
            ('K', []) => {
                let mode = params.first().copied().unwrap_or(0);
                match mode {
                    0 => self.state.clear_region(ClearRegion::LineFromCursor),
                    1 => self.state.clear_region(ClearRegion::LineToCursor),
                    2 => self.state.clear_region(ClearRegion::Line),
                    _ => {}
                }
            }
            ('m', []) => {
                let mut iter = params.iter().copied().peekable();
                if params.is_empty() {
                    self.state.pen_mut().reset();
                    return;
                }
                while let Some(code) = iter.next() {
                    match code {
                        0 => self.state.pen_mut().reset(),
                        1 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::BOLD)
                        }
                        2 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::DIM)
                        }
                        3 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::ITALIC)
                        }
                        4 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::UNDERLINE)
                        }
                        5 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::BLINK)
                        }
                        7 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::REVERSE)
                        }
                        8 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::HIDDEN)
                        }
                        9 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.with(CellAttrs::STRIKETHROUGH)
                        }
                        22 => {
                            self.state.pen_mut().attrs = self
                                .state
                                .pen_mut()
                                .attrs
                                .without(CellAttrs::BOLD)
                                .without(CellAttrs::DIM);
                        }
                        23 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::ITALIC)
                        }
                        24 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::UNDERLINE)
                        }
                        25 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::BLINK)
                        }
                        27 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::REVERSE)
                        }
                        28 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::HIDDEN)
                        }
                        29 => {
                            self.state.pen_mut().attrs =
                                self.state.pen_mut().attrs.without(CellAttrs::STRIKETHROUGH)
                        }
                        30..=37 => {
                            self.state.pen_mut().fg = Some(ansi_color_to_color(code - 30));
                        }
                        38 => {
                            if let Some(color) = parse_extended_color(&mut iter) {
                                self.state.pen_mut().fg = Some(color);
                            }
                        }
                        39 => self.state.pen_mut().fg = None,
                        40..=47 => {
                            self.state.pen_mut().bg = Some(ansi_color_to_color(code - 40));
                        }
                        48 => {
                            if let Some(color) = parse_extended_color(&mut iter) {
                                self.state.pen_mut().bg = Some(color);
                            }
                        }
                        49 => self.state.pen_mut().bg = None,
                        90..=97 => {
                            self.state.pen_mut().fg = Some(ansi_bright_color(code - 90));
                        }
                        100..=107 => {
                            self.state.pen_mut().bg = Some(ansi_bright_color(code - 100));
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
    }

    fn osc_dispatch(&mut self, _params: &[&[u8]]) {
        // OSC sequences not needed for these tests
    }

    fn esc_dispatch(&mut self, _intermediates: &[u8], _c: char) {
        // ESC sequences not needed for these tests
    }
}

fn ansi_color_to_color(index: i64) -> Color {
    match index {
        0 => Color::rgb(0, 0, 0),       // Black
        1 => Color::rgb(187, 0, 0),     // Red
        2 => Color::rgb(0, 187, 0),     // Green
        3 => Color::rgb(187, 187, 0),   // Yellow
        4 => Color::rgb(0, 0, 187),     // Blue
        5 => Color::rgb(187, 0, 187),   // Magenta
        6 => Color::rgb(0, 187, 187),   // Cyan
        7 => Color::rgb(187, 187, 187), // White
        _ => Color::rgb(187, 187, 187),
    }
}

fn ansi_bright_color(index: i64) -> Color {
    match index {
        0 => Color::rgb(85, 85, 85),    // Bright Black
        1 => Color::rgb(255, 85, 85),   // Bright Red
        2 => Color::rgb(85, 255, 85),   // Bright Green
        3 => Color::rgb(255, 255, 85),  // Bright Yellow
        4 => Color::rgb(85, 85, 255),   // Bright Blue
        5 => Color::rgb(255, 85, 255),  // Bright Magenta
        6 => Color::rgb(85, 255, 255),  // Bright Cyan
        7 => Color::rgb(255, 255, 255), // Bright White
        _ => Color::rgb(255, 255, 255),
    }
}

fn parse_extended_color(
    iter: &mut std::iter::Peekable<impl Iterator<Item = i64>>,
) -> Option<Color> {
    match iter.next() {
        Some(5) => {
            // 256-color mode
            let idx = iter.next()? as u8;
            Some(color_from_256(idx))
        }
        Some(2) => {
            // RGB mode
            let r = iter.next()? as u8;
            let g = iter.next()? as u8;
            let b = iter.next()? as u8;
            Some(Color::rgb(r, g, b))
        }
        _ => None,
    }
}

fn color_from_256(idx: u8) -> Color {
    match idx {
        0..=7 => ansi_color_to_color(idx as i64),
        8..=15 => ansi_bright_color((idx - 8) as i64),
        16..=231 => {
            // 6x6x6 color cube
            let idx = idx - 16;
            let r = (idx / 36) % 6;
            let g = (idx / 6) % 6;
            let b = idx % 6;
            let to_rgb = |v: u8| if v == 0 { 0 } else { 55 + v * 40 };
            Color::rgb(to_rgb(r), to_rgb(g), to_rgb(b))
        }
        232..=255 => {
            // Grayscale
            let gray = 8 + (idx - 232) * 10;
            Color::rgb(gray, gray, gray)
        }
    }
}

fn terminal_state_screen_text(state: &TerminalState) -> String {
    (0..state.height())
        .map(|y| {
            let mut row = String::with_capacity(usize::from(state.width()));
            for x in 0..state.width() {
                let ch = state.cell(x, y).map_or(' ', |cell| cell.ch);
                row.push(ch);
            }
            row.trim_end().to_string()
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn vt100_screen_text(screen: &vt100::Screen) -> String {
    let (_rows, cols) = screen.size();
    screen
        .rows(0, cols)
        .map(|row| row.trim_end().to_string())
        .collect::<Vec<_>>()
        .join("\n")
}

fn assert_differential_parity(width: u16, height: u16, stream: &[u8]) {
    let mut handler = TestHandler::new(width, height);
    let mut parser = AnsiParser::new();
    let mut reference = VirtualTerminal::new(width, height);
    let mut vt100 = Vt100Parser::new(height, width, 0);

    parser.parse(stream, &mut handler);
    reference.feed(stream);
    vt100.process(stream);

    let lhs = terminal_state_screen_text(&handler.state);
    let rhs_virtual = reference.screen_text();
    assert_eq!(
        lhs, rhs_virtual,
        "screen text must match VirtualTerminal reference emulator"
    );
    let rhs_vt100 = vt100_screen_text(vt100.screen());
    assert_eq!(
        lhs, rhs_vt100,
        "screen text must match vt100 reference emulator"
    );

    let lhs_cursor = (handler.state.cursor().x, handler.state.cursor().y);
    let rhs_cursor = reference.cursor();
    assert_eq!(
        lhs_cursor, rhs_cursor,
        "cursor must match VirtualTerminal reference emulator"
    );

    let (row, col) = vt100.screen().cursor_position();
    let rhs_cursor_vt100 = (col, row);
    assert_eq!(
        lhs_cursor, rhs_cursor_vt100,
        "cursor must match vt100 reference emulator"
    );
}

#[test]
fn parser_to_state_basic_text() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    parser.parse(b"Hello, World!", &mut handler);

    // Check that text was written
    for (i, ch) in "Hello, World!".chars().enumerate() {
        let cell = handler.state.cell(i as u16, 0).expect("cell should exist");
        assert_eq!(cell.ch, ch, "character at position {}", i);
    }
}

#[test]
fn parser_to_state_cursor_movement() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Move cursor to row 5, column 10 (1-indexed in ANSI)
    parser.parse(b"\x1b[5;10H", &mut handler);

    let cursor = handler.state.cursor();
    assert_eq!(cursor.x, 9, "cursor x should be 9 (0-indexed)");
    assert_eq!(cursor.y, 4, "cursor y should be 4 (0-indexed)");
}

#[test]
fn parser_to_state_sgr_colors() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Set red foreground, green background
    parser.parse(b"\x1b[31;42mX", &mut handler);

    let cell = handler.state.cell(0, 0).expect("cell should exist");
    assert_eq!(cell.ch, 'X');
    assert!(cell.fg.is_some(), "foreground should be set");
    assert!(cell.bg.is_some(), "background should be set");
}

#[test]
fn parser_to_state_256_colors() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Set 256-color foreground (color 196 = bright red)
    parser.parse(b"\x1b[38;5;196mR", &mut handler);

    let cell = handler.state.cell(0, 0).expect("cell should exist");
    assert_eq!(cell.ch, 'R');
    assert!(cell.fg.is_some(), "256-color foreground should be set");
}

#[test]
fn parser_to_state_rgb_colors() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Set RGB foreground (100, 150, 200)
    parser.parse(b"\x1b[38;2;100;150;200mB", &mut handler);

    let cell = handler.state.cell(0, 0).expect("cell should exist");
    assert_eq!(cell.ch, 'B');

    let fg = cell.fg.expect("RGB foreground should be set");
    let rgb = fg.to_rgb();
    assert_eq!(rgb.r, 100);
    assert_eq!(rgb.g, 150);
    assert_eq!(rgb.b, 200);
}

#[test]
fn parser_to_state_text_attributes() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Bold + Italic
    parser.parse(b"\x1b[1;3mB", &mut handler);

    let cell = handler.state.cell(0, 0).expect("cell should exist");
    assert_eq!(cell.ch, 'B');
    assert!(cell.attrs.contains(CellAttrs::BOLD), "should be bold");
    assert!(cell.attrs.contains(CellAttrs::ITALIC), "should be italic");
}

#[test]
fn parser_to_state_clear_screen() {
    let mut handler = TestHandler::new(80, 24);
    let mut parser = AnsiParser::new();

    // Write some text
    parser.parse(b"Hello", &mut handler);

    // Clear screen
    parser.parse(b"\x1b[2J", &mut handler);

    // All cells should be empty
    for x in 0..5 {
        let cell = handler.state.cell(x, 0).expect("cell should exist");
        assert_eq!(cell.ch, ' ', "cell at {} should be cleared", x);
    }
}

#[test]
fn parser_to_state_line_wrapping() {
    let mut handler = TestHandler::new(10, 5);
    let mut parser = AnsiParser::new();

    // Enable wrap mode
    handler.state.set_mode(TerminalModes::WRAP, true);

    // Write more than one line's worth
    parser.parse(b"1234567890ABC", &mut handler);

    // First 10 chars on line 0
    for (i, ch) in "1234567890".chars().enumerate() {
        let cell = handler.state.cell(i as u16, 0).expect("cell should exist");
        assert_eq!(cell.ch, ch, "line 0 position {}", i);
    }

    // Next chars on line 1
    for (i, ch) in "ABC".chars().enumerate() {
        let cell = handler.state.cell(i as u16, 1).expect("cell should exist");
        assert_eq!(cell.ch, ch, "line 1 position {}", i);
    }
}

#[test]
fn differential_replay_matches_virtual_terminal_for_basic_stream() {
    // Covers print + cursor moves + line clearing + final cursor position.
    let stream = b"HELLO\x1b[2;1Hworld\x1b[1;3HX\x1b[K\x1b[4;5H!";
    assert_differential_parity(12, 4, stream);
}

#[test]
fn differential_replay_matches_virtual_terminal_for_erase_sequences() {
    // Covers ED/EL combinations and explicit cursor repositioning.
    let stream = b"abcde\x1b[2;1H12345\x1b[1;3H\x1b[K\x1b[2J\x1b[1;1HZ";
    assert_differential_parity(10, 4, stream);
}

#[test]
fn differential_replay_matches_virtual_terminal_for_scroll_progression() {
    // Force scroll-up behavior on a small viewport.
    let stream = b"one\r\ntwo\r\nthree\r\nfour\r\n";
    assert_differential_parity(6, 3, stream);
}

#[test]
fn differential_replay_matches_virtual_terminal_for_sgr_heavy_stream() {
    // We still compare terminal text/cursor here; styling side-effects are
    // exercised in the parse path to catch control-flow regressions.
    let stream = b"\x1b[31mR\x1b[0m-\x1b[38;5;196mX\x1b[0m-\x1b[38;2;1;2;3mY\x1b[0m";
    assert_differential_parity(20, 4, stream);
}

// ============================================================================
// Input Forwarding Tests
// ============================================================================

#[test]
fn input_forward_simple_key() {
    let event = KeyEvent::plain(Key::Char('a'));
    let seq = key_to_sequence(event);
    assert_eq!(seq, b"a");
}

#[test]
fn input_forward_ctrl_c() {
    let event = KeyEvent::new(Key::Char('c'), Modifiers::CTRL);
    let seq = key_to_sequence(event);
    assert_eq!(seq, &[0x03]); // ETX
}

#[test]
fn input_forward_arrow_keys() {
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Up)), b"\x1b[A");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Down)), b"\x1b[B");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Right)), b"\x1b[C");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Left)), b"\x1b[D");
}

#[test]
fn input_forward_function_keys() {
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::F(1))), b"\x1bOP");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::F(5))), b"\x1b[15~");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::F(12))), b"\x1b[24~");
}

#[test]
fn input_forward_enter_and_tab() {
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Enter)), b"\r");
    assert_eq!(key_to_sequence(KeyEvent::plain(Key::Tab)), b"\t");
}

#[test]
fn input_forward_alt_modifier() {
    let event = KeyEvent::new(Key::Char('x'), Modifiers::ALT);
    let seq = key_to_sequence(event);
    assert_eq!(seq, b"\x1bx"); // ESC + x
}

#[test]
fn input_forward_bracketed_paste() {
    let mut paste = BracketedPaste::new();
    paste.enable();
    let seq = paste.wrap(b"Hello, World!");

    assert!(seq.starts_with(b"\x1b[200~"));
    assert!(seq.ends_with(b"\x1b[201~"));
    assert!(
        seq.windows(b"Hello, World!".len())
            .any(|w| w == b"Hello, World!")
    );
}

// ============================================================================
// Widget Rendering Tests
// ============================================================================

fn create_test_frame(width: u16, height: u16) -> (Frame<'static>, Box<GraphemePool>) {
    // SAFETY: We leak the pool to get a 'static lifetime for testing.
    // This is acceptable in tests where memory is cleaned up at process exit.
    let pool = Box::new(GraphemePool::default());
    let pool_ref: &'static mut GraphemePool = Box::leak(pool);
    let frame = Frame::new(width, height, pool_ref);
    (frame, Box::new(GraphemePool::default()))
}

#[test]
fn widget_renders_text() {
    let mut state = TerminalEmulatorState::new(80, 24);

    // Write some text to the terminal state
    for (i, ch) in "Hello!".chars().enumerate() {
        state.terminal.move_cursor(i as u16, 0);
        state.terminal.put_char(ch);
    }

    let widget = TerminalEmulator::new();
    let area = Rect::new(0, 0, 80, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    // Verify the buffer contains our text
    for (i, expected) in "Hello!".chars().enumerate() {
        let cell = frame.buffer.get(i as u16, 0).expect("cell should exist");
        assert_eq!(cell.content.as_char(), Some(expected), "position {}", i);
    }
}

#[test]
fn widget_renders_colors() {
    let mut state = TerminalEmulatorState::new(80, 24);

    // Set pen color and write
    state.terminal.pen_mut().fg = Some(Color::rgb(255, 0, 0));
    state.terminal.pen_mut().bg = Some(Color::rgb(0, 255, 0));
    state.terminal.move_cursor(0, 0);
    state.terminal.put_char('R');

    let widget = TerminalEmulator::new();
    let area = Rect::new(0, 0, 80, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    let cell = frame.buffer.get(0, 0).expect("cell should exist");
    assert_eq!(cell.content.as_char(), Some('R'));
    // Check that colors were applied (non-transparent)
    assert_ne!(cell.fg.0, 0, "foreground should be set");
    assert_ne!(cell.bg.0, 0, "background should be set");
}

#[test]
fn widget_renders_cursor() {
    let mut state = TerminalEmulatorState::new(80, 24);
    state.terminal.move_cursor(5, 3);

    // The cursor cell should get REVERSE style
    state.terminal.set_cursor_visible(true);

    let widget = TerminalEmulator::new().show_cursor(true).cursor_phase(true);
    let area = Rect::new(0, 0, 80, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    let cursor_cell = frame.buffer.get(5, 3).expect("cursor cell should exist");
    assert!(
        cursor_cell.attrs.flags().contains(StyleFlags::REVERSE),
        "cursor should have REVERSE style"
    );
}

#[test]
fn widget_cursor_hidden_when_disabled() {
    let mut state = TerminalEmulatorState::new(80, 24);
    state.terminal.move_cursor(5, 3);
    state.terminal.set_cursor_visible(true);

    // Disable cursor rendering
    let widget = TerminalEmulator::new().show_cursor(false);
    let area = Rect::new(0, 0, 80, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    let cursor_cell = frame.buffer.get(5, 3).expect("cursor cell should exist");
    assert!(
        !cursor_cell.attrs.flags().contains(StyleFlags::REVERSE),
        "cursor should NOT have REVERSE style when disabled"
    );
}

#[test]
fn widget_scroll_offset() {
    let mut state = TerminalEmulatorState::with_scrollback(10, 5, 100);

    // Add content to scrollback by scrolling
    for _ in 0..3 {
        state.terminal.scroll_up(1);
    }

    // Scroll the view up
    state.scroll_up(2);
    assert_eq!(state.scroll_offset, 2);

    // Scroll back down
    state.scroll_down(1);
    assert_eq!(state.scroll_offset, 1);

    // Reset
    state.reset_scroll();
    assert_eq!(state.scroll_offset, 0);
}

#[test]
fn widget_resize() {
    let mut state = TerminalEmulatorState::new(80, 24);

    state.resize(120, 40);

    assert_eq!(state.terminal.width(), 120);
    assert_eq!(state.terminal.height(), 40);
}

// ============================================================================
// Full Pipeline Integration Tests
// ============================================================================

#[test]
fn full_pipeline_pty_to_widget() {
    // Spawn a simple command
    let config = PtyConfig::default().with_size(40, 10).logging(false);

    let mut cmd = CommandBuilder::new("sh");
    cmd.args(["-c", "printf 'TESTOUTPUT'"]);

    let mut session = spawn_command(config, cmd).expect("spawn should succeed");
    let _ = session.wait().expect("wait should succeed");

    let output = session
        .read_until(b"TESTOUTPUT", Duration::from_secs(2))
        .expect("should capture output");

    // Parse through ANSI parser into terminal state
    let mut handler = TestHandler::new(40, 10);
    let mut parser = AnsiParser::new();
    parser.parse(&output, &mut handler);

    // Create widget and render
    let mut state = TerminalEmulatorState::new(40, 10);
    state.terminal = handler.state;

    let widget = TerminalEmulator::new();
    let area = Rect::new(0, 0, 40, 10);
    let (mut frame, _pool) = create_test_frame(40, 10);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    // Verify output appears in rendered buffer (somewhere)
    let mut found = false;
    for y in 0..10 {
        let mut line = String::new();
        for x in 0..40 {
            if let Some(cell) = frame.buffer.get(x, y)
                && let Some(c) = cell.content.as_char()
            {
                line.push(c);
            }
        }
        if line.contains("TESTOUTPUT") {
            found = true;
            break;
        }
    }
    assert!(found, "TESTOUTPUT should appear in rendered frame");
}

#[test]
fn input_forwarding_roundtrip() {
    let config = PtyConfig::default().with_size(80, 24).logging(false);

    // Use a command that terminates after consuming a single line so we don't rely on tty
    // line discipline or EOF semantics to end the process (important for CI/coverage stability).
    let mut cmd = CommandBuilder::new("sh");
    cmd.args(["-c", "IFS= read -r line; printf '%s' \"$line\""]);

    let mut session = spawn_command(config, cmd).expect("spawn should succeed");

    // Send input using the input forwarding module: 'X' + Enter.
    session
        .send_input(&key_to_sequence(KeyEvent::plain(Key::Char('X'))))
        .expect("send should succeed");
    session
        .send_input(&key_to_sequence(KeyEvent::plain(Key::Enter)))
        .expect("send should succeed");

    // Wait for the echoed character.
    let output = session
        .read_until(b"X", Duration::from_secs(2))
        .expect("should receive echo");

    let status = session.wait().expect("wait should succeed");
    assert!(status.success(), "child should exit successfully");

    assert!(
        output.windows(1).any(|w| w == b"X"),
        "output should contain echoed character"
    );
}

// ============================================================================
// Edge Cases and Error Handling
// ============================================================================

#[test]
fn empty_area_render_does_not_panic() {
    let mut state = TerminalEmulatorState::new(80, 24);
    let widget = TerminalEmulator::new();

    // Empty area (width=0)
    let area = Rect::new(0, 0, 0, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);
    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    // Empty area (height=0)
    let area = Rect::new(0, 0, 80, 0);
    StatefulWidget::render(&widget, area, &mut frame, &mut state);
}

#[test]
fn terminal_smaller_than_area() {
    // Terminal is 40x10 but we render in 80x24 area
    let mut state = TerminalEmulatorState::new(40, 10);
    state.terminal.move_cursor(0, 0);
    state.terminal.put_char('X');

    let widget = TerminalEmulator::new();
    let area = Rect::new(0, 0, 80, 24);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    // Content should appear at (0,0)
    let cell = frame.buffer.get(0, 0).expect("cell should exist");
    assert_eq!(cell.content.as_char(), Some('X'));
}

#[test]
fn area_offset_respected() {
    let mut state = TerminalEmulatorState::new(10, 5);
    state.terminal.move_cursor(0, 0);
    state.terminal.put_char('Z');

    let widget = TerminalEmulator::new();
    // Render at offset (5, 3)
    let area = Rect::new(5, 3, 10, 5);
    let (mut frame, _pool) = create_test_frame(80, 24);

    StatefulWidget::render(&widget, area, &mut frame, &mut state);

    // Content should appear at (5,3)
    let cell = frame.buffer.get(5, 3).expect("cell should exist");
    assert_eq!(cell.content.as_char(), Some('Z'));
}