opentui_rust 0.2.1

High-performance terminal UI rendering engine with alpha blending and diffed buffers
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
//! E2E tests for input simulation and event flow.
//!
//! Tests complete input handling from raw bytes → parsed events → verification.
//! Covers keyboard, mouse, paste, and focus events.

#![allow(clippy::uninlined_format_args)] // Clarity over style in test code

mod common;

use common::harness::E2EHarness;
use common::input_sim::{
    InputSequence, TimingMode, key_to_ansi, mouse_to_sgr, paste_to_ansi, sequence_to_ansi,
};
use opentui::input::{
    Event, InputParser, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};
use opentui_rust as opentui;

// ============================================================================
// Keyboard Input Flow Tests
// ============================================================================

/// Test single keypress → `KeyEvent`.
#[test]
fn test_e2e_single_keypress() {
    let mut harness = E2EHarness::new("input_flow", "single_keypress", 80, 24);
    harness.log().info("init", "Testing single keypress");

    let mut parser = InputParser::new();

    // Generate ANSI for 'a' key
    let seq = InputSequence::keystroke(KeyCode::Char('a'), KeyModifiers::empty());
    let ansi = sequence_to_ansi(&seq);

    harness
        .log()
        .info("input", format!("ANSI bytes: {:?}", ansi));

    // Parse
    let (event, consumed) = parser.parse(&ansi).expect("Should parse");

    harness.log().info(
        "parse",
        format!("Consumed {} bytes, event: {:?}", consumed, event),
    );

    // Verify
    assert_eq!(consumed, 1);
    let key = event.key().expect("Should be key event");
    assert_eq!(key.code, KeyCode::Char('a'));
    assert!(key.modifiers.is_empty());

    harness.finish(true);
    eprintln!("[TEST] PASS: Single keypress flow works");
}

/// Test modifier combinations (Ctrl+X, Alt+Y, Ctrl+Alt+Z).
#[test]
fn test_e2e_modifier_combinations() {
    let mut harness = E2EHarness::new("input_flow", "modifier_combinations", 80, 24);
    harness.log().info("init", "Testing modifier combinations");

    let mut parser = InputParser::new();

    // Ctrl+C
    let ctrl_c = KeyEvent::with_ctrl(KeyCode::Char('c'));
    let ansi = key_to_ansi(&ctrl_c);
    harness
        .log()
        .info("input", format!("Ctrl+C ANSI: {:?}", ansi));

    let (event, _) = parser.parse(&ansi).expect("Should parse Ctrl+C");
    let key = event.key().expect("Should be key event");
    assert!(key.ctrl(), "Ctrl should be set");
    assert!(key.is_ctrl_c(), "Should be Ctrl+C");

    // Alt+X
    let alt_x = KeyEvent::with_alt(KeyCode::Char('x'));
    let ansi = key_to_ansi(&alt_x);
    harness
        .log()
        .info("input", format!("Alt+X ANSI: {:?}", ansi));

    let (event, _) = parser.parse(&ansi).expect("Should parse Alt+X");
    let key = event.key().expect("Should be key event");
    assert!(key.alt(), "Alt should be set");
    assert_eq!(key.code, KeyCode::Char('x'));

    harness.finish(true);
    eprintln!("[TEST] PASS: Modifier combinations work");
}

/// Test function keys (F1-F12).
#[test]
fn test_e2e_function_keys() {
    let mut harness = E2EHarness::new("input_flow", "function_keys", 80, 24);
    harness.log().info("init", "Testing function keys F1-F12");

    let mut parser = InputParser::new();

    // F1-F4 use SS3 sequences
    let (event, _) = parser.parse(b"\x1bOP").expect("Should parse F1");
    assert_eq!(event.key().unwrap().code, KeyCode::F(1));
    harness.log().info("verify", "F1 parsed correctly");

    // F5+ use CSI tilde sequences
    let (event, _) = parser.parse(b"\x1b[15~").expect("Should parse F5");
    assert_eq!(event.key().unwrap().code, KeyCode::F(5));
    harness.log().info("verify", "F5 parsed correctly");

    let (event, _) = parser.parse(b"\x1b[24~").expect("Should parse F12");
    assert_eq!(event.key().unwrap().code, KeyCode::F(12));
    harness.log().info("verify", "F12 parsed correctly");

    harness.finish(true);
    eprintln!("[TEST] PASS: Function keys work");
}

/// Test special keys (arrows, home, end, pgup, pgdn, insert, delete).
#[test]
fn test_e2e_special_keys() {
    let mut harness = E2EHarness::new("input_flow", "special_keys", 80, 24);
    harness.log().info("init", "Testing special keys");

    let mut parser = InputParser::new();

    let test_cases = [
        (b"\x1b[A".to_vec(), KeyCode::Up),
        (b"\x1b[B".to_vec(), KeyCode::Down),
        (b"\x1b[C".to_vec(), KeyCode::Right),
        (b"\x1b[D".to_vec(), KeyCode::Left),
        (b"\x1b[H".to_vec(), KeyCode::Home),
        (b"\x1b[F".to_vec(), KeyCode::End),
        (b"\x1b[5~".to_vec(), KeyCode::PageUp),
        (b"\x1b[6~".to_vec(), KeyCode::PageDown),
        (b"\x1b[2~".to_vec(), KeyCode::Insert),
        (b"\x1b[3~".to_vec(), KeyCode::Delete),
    ];

    for (ansi, expected_code) in test_cases {
        let (event, _) = parser.parse(&ansi).expect("Should parse");
        let key = event.key().expect("Should be key event");
        assert_eq!(key.code, expected_code, "Key code mismatch for {:?}", ansi);
        harness
            .log()
            .info("verify", format!("{:?} parsed correctly", expected_code));
    }

    harness.finish(true);
    eprintln!("[TEST] PASS: Special keys work");
}

/// Test Unicode character input (multi-byte UTF-8).
#[test]
fn test_e2e_unicode_input() {
    let mut harness = E2EHarness::new("input_flow", "unicode_input", 80, 24);
    harness
        .log()
        .info("init", "Testing Unicode character input");

    let mut parser = InputParser::new();

    // 2-byte UTF-8: ñ (U+00F1)
    let (event, consumed) = parser.parse("ñ".as_bytes()).expect("Should parse ñ");
    assert_eq!(consumed, 2);
    assert_eq!(event.key().unwrap().code, KeyCode::Char('ñ'));
    harness.log().info("verify", "2-byte UTF-8 (ñ) works");

    // 3-byte UTF-8: 日 (U+65E5)
    let (event, consumed) = parser.parse("".as_bytes()).expect("Should parse 日");
    assert_eq!(consumed, 3);
    assert_eq!(event.key().unwrap().code, KeyCode::Char(''));
    harness.log().info("verify", "3-byte UTF-8 (日) works");

    // 4-byte UTF-8: 🎉 (U+1F389)
    let (event, consumed) = parser.parse("🎉".as_bytes()).expect("Should parse 🎉");
    assert_eq!(consumed, 4);
    assert_eq!(event.key().unwrap().code, KeyCode::Char('🎉'));
    harness.log().info("verify", "4-byte UTF-8 (🎉) works");

    harness.finish(true);
    eprintln!("[TEST] PASS: Unicode input works");
}

/// Test escape key handling (vs Alt prefix).
#[test]
fn test_e2e_escape_handling() {
    let mut harness = E2EHarness::new("input_flow", "escape_handling", 80, 24);
    harness.log().info("init", "Testing escape key handling");

    let mut parser = InputParser::new();

    // Standalone escape (requires checking for incomplete)
    let result = parser.parse(b"\x1b");
    assert!(
        result.is_err(),
        "Standalone ESC should be Incomplete (could be start of sequence)"
    );
    harness
        .log()
        .info("verify", "Standalone ESC returns Incomplete");

    // Double escape
    let (event, consumed) = parser.parse(b"\x1b\x1b").expect("Should parse double ESC");
    assert_eq!(consumed, 1);
    assert_eq!(event.key().unwrap().code, KeyCode::Esc);
    harness.log().info("verify", "Double ESC returns Esc key");

    // Alt+letter (ESC followed by letter)
    let (event, consumed) = parser.parse(b"\x1bx").expect("Should parse Alt+x");
    assert_eq!(consumed, 2);
    let key = event.key().unwrap();
    assert!(key.alt());
    assert_eq!(key.code, KeyCode::Char('x'));
    harness.log().info("verify", "Alt+letter works");

    harness.finish(true);
    eprintln!("[TEST] PASS: Escape handling works");
}

// ============================================================================
// Mouse Input Flow Tests
// ============================================================================

/// Test click → `MouseEvent` with correct position.
#[test]
fn test_e2e_mouse_click_position() {
    let mut harness = E2EHarness::new("input_flow", "mouse_click_position", 80, 24);
    harness
        .log()
        .info("init", "Testing mouse click position accuracy");

    let mut parser = InputParser::new();

    // Left click at (15, 10)
    let click = MouseEvent::new(15, 10, MouseButton::Left, MouseEventKind::Press);
    let ansi = mouse_to_sgr(&click);

    harness.log().info(
        "input",
        format!("SGR bytes: {:?}", String::from_utf8_lossy(&ansi)),
    );

    let (event, _) = parser.parse(&ansi).expect("Should parse");
    let mouse = event.mouse().expect("Should be mouse event");

    assert_eq!(mouse.x, 15);
    assert_eq!(mouse.y, 10);
    assert_eq!(mouse.button, MouseButton::Left);
    assert_eq!(mouse.kind, MouseEventKind::Press);

    harness
        .log()
        .info("verify", format!("Position: ({}, {})", mouse.x, mouse.y));
    harness.finish(true);
    eprintln!("[TEST] PASS: Mouse click position works");
}

/// Test drag sequence (press → move → release).
#[test]
fn test_e2e_mouse_drag_sequence() {
    let mut harness = E2EHarness::new("input_flow", "mouse_drag_sequence", 80, 24);
    harness.log().info("init", "Testing mouse drag sequence");

    let mut parser = InputParser::new();

    // Build drag sequence
    let seq = InputSequence::mouse_drag((5, 5), (20, 15), MouseButton::Left);
    let ansi = sequence_to_ansi(&seq);

    harness
        .log()
        .info("input", format!("Drag sequence has {} events", seq.len()));

    // Parse all events
    let mut events = Vec::new();
    let mut offset = 0;
    while offset < ansi.len() {
        match parser.parse(&ansi[offset..]) {
            Ok((event, consumed)) => {
                events.push(event);
                offset += consumed;
            }
            Err(_) => break,
        }
    }

    harness
        .log()
        .info("parse", format!("Parsed {} events", events.len()));

    // Verify sequence: Press, Move(s), Release
    assert!(
        events.len() >= 3,
        "Should have at least press, move, release"
    );

    // First should be Press
    let first = events.first().unwrap().mouse().unwrap();
    assert_eq!(first.kind, MouseEventKind::Press);

    // Last should be Release
    let last = events.last().unwrap().mouse().unwrap();
    assert_eq!(last.kind, MouseEventKind::Release);

    harness.finish(true);
    eprintln!("[TEST] PASS: Mouse drag sequence works");
}

/// Test scroll wheel events (up/down).
#[test]
fn test_e2e_scroll_wheel() {
    let mut harness = E2EHarness::new("input_flow", "scroll_wheel", 80, 24);
    harness.log().info("init", "Testing scroll wheel events");

    let mut parser = InputParser::new();

    // Scroll up: button byte 64
    let scroll_up = b"\x1b[<64;10;5M";
    let (event, _) = parser.parse(scroll_up).expect("Should parse scroll up");
    let mouse = event.mouse().unwrap();
    assert_eq!(mouse.kind, MouseEventKind::ScrollUp);
    harness.log().info("verify", "Scroll up works");

    // Scroll down: button byte 65
    let scroll_down = b"\x1b[<65;10;5M";
    let (event, _) = parser.parse(scroll_down).expect("Should parse scroll down");
    let mouse = event.mouse().unwrap();
    assert_eq!(mouse.kind, MouseEventKind::ScrollDown);
    harness.log().info("verify", "Scroll down works");

    harness.finish(true);
    eprintln!("[TEST] PASS: Scroll wheel works");
}

/// Test mouse position accuracy at boundaries.
#[test]
fn test_e2e_mouse_boundary_positions() {
    let mut harness = E2EHarness::new("input_flow", "mouse_boundaries", 80, 24);
    harness
        .log()
        .info("init", "Testing mouse position at boundaries");

    let mut parser = InputParser::new();

    // Origin (0, 0) - SGR uses 1-indexed, so (1,1) in protocol
    let (event, _) = parser.parse(b"\x1b[<0;1;1M").expect("Should parse origin");
    let mouse = event.mouse().unwrap();
    assert_eq!((mouse.x, mouse.y), (0, 0));
    harness.log().info("verify", "Origin (0,0) works");

    // Large coordinates
    let (event, _) = parser
        .parse(b"\x1b[<0;1000;500M")
        .expect("Should parse large coords");
    let mouse = event.mouse().unwrap();
    assert_eq!((mouse.x, mouse.y), (999, 499));
    harness.log().info(
        "verify",
        format!("Large coords ({}, {}) work", mouse.x, mouse.y),
    );

    harness.finish(true);
    eprintln!("[TEST] PASS: Mouse boundary positions work");
}

/// Test mouse button combinations (modifiers).
#[test]
fn test_e2e_mouse_modifiers() {
    let mut harness = E2EHarness::new("input_flow", "mouse_modifiers", 80, 24);
    harness
        .log()
        .info("init", "Testing mouse with keyboard modifiers");

    let mut parser = InputParser::new();

    // Shift+Click: button 0 + shift(4) = 4
    let (event, _) = parser
        .parse(b"\x1b[<4;10;5M")
        .expect("Should parse shift+click");
    let mouse = event.mouse().unwrap();
    assert!(mouse.shift);
    assert!(!mouse.ctrl);
    assert!(!mouse.alt);
    harness.log().info("verify", "Shift+Click works");

    // Ctrl+Click: button 0 + ctrl(16) = 16
    let (event, _) = parser
        .parse(b"\x1b[<16;10;5M")
        .expect("Should parse ctrl+click");
    let mouse = event.mouse().unwrap();
    assert!(mouse.ctrl);
    harness.log().info("verify", "Ctrl+Click works");

    // Alt+Click: button 0 + alt(8) = 8
    let (event, _) = parser
        .parse(b"\x1b[<8;10;5M")
        .expect("Should parse alt+click");
    let mouse = event.mouse().unwrap();
    assert!(mouse.alt);
    harness.log().info("verify", "Alt+Click works");

    harness.finish(true);
    eprintln!("[TEST] PASS: Mouse modifiers work");
}

// ============================================================================
// Paste Input Tests
// ============================================================================

/// Test bracketed paste detection.
#[test]
fn test_e2e_bracketed_paste_detection() {
    let mut harness = E2EHarness::new("input_flow", "bracketed_paste", 80, 24);
    harness
        .log()
        .info("init", "Testing bracketed paste detection");

    let mut parser = InputParser::new();

    // Generate paste sequence
    let content = "Hello, World!";
    let ansi = paste_to_ansi(content);

    harness
        .log()
        .info("input", format!("Paste ANSI length: {} bytes", ansi.len()));

    // First parse enters paste mode
    let result = parser.parse(&ansi);
    assert!(result.is_err(), "First parse should enter paste mode");

    // Second parse returns paste event
    let (event, _) = parser.parse(&ansi).expect("Should parse paste");
    let paste = event.paste().expect("Should be paste event");
    assert_eq!(paste.content(), content);

    harness
        .log()
        .info("verify", format!("Paste content: {}", paste.content()));
    harness.finish(true);
    eprintln!("[TEST] PASS: Bracketed paste detection works");
}

/// Test large paste handling (>10KB).
#[test]
fn test_e2e_large_paste() {
    let mut harness = E2EHarness::new("input_flow", "large_paste", 80, 24);
    harness.log().info("init", "Testing large paste (>10KB)");

    let mut parser = InputParser::new();

    // Generate large content (15KB)
    let content: String = "x".repeat(15_000);
    let ansi = paste_to_ansi(&content);

    harness
        .log()
        .info("input", format!("Large paste: {} bytes", ansi.len()));

    // Parse
    let _ = parser.parse(&ansi); // Enter paste mode
    let (event, _) = parser.parse(&ansi).expect("Should parse large paste");
    let paste = event.paste().expect("Should be paste event");

    // Note: MAX_PASTE_BUFFER_SIZE is 10MB, so this should fit
    assert_eq!(paste.content().len(), 15_000);
    harness
        .log()
        .info("verify", format!("Paste length: {} chars", paste.len()));

    harness.finish(true);
    eprintln!("[TEST] PASS: Large paste handling works");
}

/// Test paste with special characters and Unicode.
#[test]
fn test_e2e_paste_with_unicode() {
    let mut harness = E2EHarness::new("input_flow", "paste_unicode", 80, 24);
    harness
        .log()
        .info("init", "Testing paste with Unicode content");

    let mut parser = InputParser::new();

    let content = "日本語テスト 🎉 emoji ñ special";
    let ansi = paste_to_ansi(content);

    let _ = parser.parse(&ansi);
    let (event, _) = parser.parse(&ansi).expect("Should parse unicode paste");
    let paste = event.paste().expect("Should be paste event");

    assert_eq!(paste.content(), content);
    harness
        .log()
        .info("verify", format!("Unicode paste: {}", paste.content()));

    harness.finish(true);
    eprintln!("[TEST] PASS: Paste with Unicode works");
}

/// Test binary data in paste (no corruption).
#[test]
fn test_e2e_paste_binary_data() {
    let mut harness = E2EHarness::new("input_flow", "paste_binary", 80, 24);
    harness.log().info("init", "Testing paste with binary data");

    let mut parser = InputParser::new();

    // Content with various byte values (avoiding paste end sequence)
    let content = "text\x00null\x01soh\x7fDEL";
    let ansi = paste_to_ansi(content);

    let _ = parser.parse(&ansi);
    let (event, _) = parser.parse(&ansi).expect("Should parse binary paste");
    let paste = event.paste().expect("Should be paste event");

    // Note: from_utf8_lossy may replace invalid UTF-8
    assert!(!paste.is_empty());
    harness
        .log()
        .info("verify", format!("Binary paste length: {}", paste.len()));

    harness.finish(true);
    eprintln!("[TEST] PASS: Paste binary data works");
}

// ============================================================================
// Focus and Resize Events
// ============================================================================

/// Test focus change events.
#[test]
fn test_e2e_focus_events() {
    let mut harness = E2EHarness::new("input_flow", "focus_events", 80, 24);
    harness.log().info("init", "Testing focus change events");

    let mut parser = InputParser::new();

    // Focus gained: CSI I
    let (event, _) = parser.parse(b"\x1b[I").expect("Should parse focus gained");
    assert_eq!(event, Event::FocusGained);
    harness.log().info("verify", "Focus gained works");

    // Focus lost: CSI O
    let (event, _) = parser.parse(b"\x1b[O").expect("Should parse focus lost");
    assert_eq!(event, Event::FocusLost);
    harness.log().info("verify", "Focus lost works");

    harness.finish(true);
    eprintln!("[TEST] PASS: Focus events work");
}

/// Test resize event.
#[test]
fn test_e2e_resize_event() {
    let mut harness = E2EHarness::new("input_flow", "resize_event", 80, 24);
    harness.log().info("init", "Testing resize event");

    let mut parser = InputParser::new();

    // Resize to 120x50: CSI 8;50;120 t
    let (event, _) = parser
        .parse(b"\x1b[8;50;120t")
        .expect("Should parse resize");
    let resize = event.resize().expect("Should be resize event");

    assert_eq!(resize.width, 120);
    assert_eq!(resize.height, 50);
    harness.log().info(
        "verify",
        format!("Resize: {}x{}", resize.width, resize.height),
    );

    harness.finish(true);
    eprintln!("[TEST] PASS: Resize event works");
}

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

/// Test partial escape sequences (interrupted input).
#[test]
fn test_e2e_partial_sequences() {
    let mut harness = E2EHarness::new("input_flow", "partial_sequences", 80, 24);
    harness
        .log()
        .info("init", "Testing partial/interrupted sequences");

    let mut parser = InputParser::new();

    // Incomplete CSI sequence
    let result = parser.parse(b"\x1b[");
    assert!(result.is_err());
    harness.log().info("verify", "Incomplete CSI returns error");

    // Incomplete CSI with params but no terminator
    let result = parser.parse(b"\x1b[1;2");
    assert!(result.is_err());
    harness
        .log()
        .info("verify", "Incomplete params returns error");

    harness.finish(true);
    eprintln!("[TEST] PASS: Partial sequences handled correctly");
}

/// Test invalid/malformed sequences.
#[test]
fn test_e2e_invalid_sequences() {
    let mut harness = E2EHarness::new("input_flow", "invalid_sequences", 80, 24);
    harness
        .log()
        .info("init", "Testing invalid/malformed sequences");

    let mut parser = InputParser::new();

    // Unknown CSI terminator
    let result = parser.parse(b"\x1b[999Z");
    assert!(result.is_err());
    harness
        .log()
        .info("verify", "Unknown CSI terminator returns error");

    // Invalid UTF-8 continuation
    let result = parser.parse(&[0x80]);
    assert!(result.is_err());
    harness.log().info("verify", "Invalid UTF-8 returns error");

    harness.finish(true);
    eprintln!("[TEST] PASS: Invalid sequences handled gracefully");
}

/// Test rapid input (stress test).
#[test]
fn test_e2e_rapid_input() {
    let mut harness = E2EHarness::new("input_flow", "rapid_input", 80, 24);
    harness
        .log()
        .info("init", "Testing rapid input (100+ events)");

    let mut parser = InputParser::new();

    // Generate 200 keystrokes
    let seq = InputSequence::type_text(&"a".repeat(200)).stress_mode();
    let ansi = sequence_to_ansi(&seq);

    harness
        .log()
        .info("input", format!("Rapid input: {} bytes", ansi.len()));

    // Parse all events
    let mut count = 0;
    let mut offset = 0;
    while offset < ansi.len() {
        match parser.parse(&ansi[offset..]) {
            Ok((_, consumed)) => {
                count += 1;
                offset += consumed;
            }
            Err(_) => break,
        }
    }

    assert_eq!(count, 200, "Should parse all 200 events");
    harness
        .log()
        .info("verify", format!("Parsed {} events", count));

    harness.finish(true);
    eprintln!("[TEST] PASS: Rapid input handled correctly");
}

/// Test typing speed simulation.
#[test]
fn test_e2e_typing_speed_simulation() {
    let mut harness = E2EHarness::new("input_flow", "typing_speed", 80, 24);
    harness
        .log()
        .info("init", "Testing typing speed simulation");

    // 60 WPM = 300 chars/min = 200ms per char average
    let seq = InputSequence::type_text("hello world").with_wpm(60);

    assert_eq!(seq.timing(), TimingMode::Realistic { wpm: 60 });

    // Total time should be approximately 11 chars * 200ms = 2200ms
    // But first char has no delay, so ~2000ms
    let total_time = seq.total_time_ms();
    harness
        .log()
        .info("timing", format!("Total simulated time: {}ms", total_time));

    // The actual time calculation depends on implementation
    // Just verify it's non-zero for realistic mode
    assert!(seq.len() == 11, "Should have 11 key events");

    harness.finish(true);
    eprintln!("[TEST] PASS: Typing speed simulation works");
}

/// Test input sequence building and chaining.
#[test]
fn test_e2e_sequence_chaining() {
    let mut harness = E2EHarness::new("input_flow", "sequence_chaining", 80, 24);
    harness
        .log()
        .info("init", "Testing sequence building and chaining");

    // Build complex sequence
    let seq = InputSequence::new()
        .key(KeyCode::Char('H'))
        .key(KeyCode::Char('i'))
        .ctrl_key(KeyCode::Char('s')) // Ctrl+S (save)
        .left_click(10, 5)
        .key(KeyCode::Enter);

    // Count events
    let events = seq.to_terminal_events();
    harness
        .log()
        .info("build", format!("Built {} events", events.len()));

    // Verify event types
    assert!(matches!(events[0], Event::Key(_)));
    assert!(matches!(events[1], Event::Key(_)));
    assert!(matches!(events[2], Event::Key(_))); // Ctrl+S
    assert!(matches!(events[3], Event::Mouse(_))); // Click press
    assert!(matches!(events[4], Event::Mouse(_))); // Click release
    assert!(matches!(events[5], Event::Key(_))); // Enter

    harness.finish(true);
    eprintln!("[TEST] PASS: Sequence chaining works");
}

/// Test modifier combinations for special keys.
#[test]
fn test_e2e_modified_special_keys() {
    let mut harness = E2EHarness::new("input_flow", "modified_special_keys", 80, 24);
    harness
        .log()
        .info("init", "Testing modified special keys (Shift+Arrow, etc.)");

    let mut parser = InputParser::new();

    // Shift+Up: ESC [ 1 ; 2 A
    let (event, _) = parser.parse(b"\x1b[1;2A").expect("Should parse Shift+Up");
    let key = event.key().unwrap();
    assert_eq!(key.code, KeyCode::Up);
    assert!(key.shift());
    harness.log().info("verify", "Shift+Up works");

    // Ctrl+Shift+End: ESC [ 1 ; 6 F
    let (event, _) = parser
        .parse(b"\x1b[1;6F")
        .expect("Should parse Ctrl+Shift+End");
    let key = event.key().unwrap();
    assert_eq!(key.code, KeyCode::End);
    assert!(key.shift());
    assert!(key.ctrl());
    harness.log().info("verify", "Ctrl+Shift+End works");

    // All modifiers: Ctrl+Shift+Alt+Home: ESC [ 1 ; 8 H
    let (event, _) = parser
        .parse(b"\x1b[1;8H")
        .expect("Should parse all mods+Home");
    let key = event.key().unwrap();
    assert_eq!(key.code, KeyCode::Home);
    assert!(key.shift());
    assert!(key.alt());
    assert!(key.ctrl());
    harness.log().info("verify", "All modifiers work");

    harness.finish(true);
    eprintln!("[TEST] PASS: Modified special keys work");
}