ratatui-testlib 0.1.0

Integration testing library for terminal user interface applications with Sixel and Bevy support
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# Phase 2: Event Simulation & Async Support - Implementation Checklist

**Status**: Ready for Implementation
**Priority**: P0 (Critical - MVP Blocker)
**Dependencies**: Phase 1 Complete (100%)
**Target Duration**: 1-2 weeks
**Assignee**: Implementation Agents

## Overview

Phase 2 delivers rich event simulation and async runtime integration to enable interactive TUI testing. This phase transforms the harness from passive observation to active interaction.

### Goals

1. Implement keyboard event simulation (single keys, sequences, modifiers)
2. Create smart wait conditions with timeout and polling
3. Integrate Tokio async runtime support
4. Enable comprehensive input testing for dgx-pixels navigation

### Success Criteria

- Can simulate keyboard input (keys and text)
- Can wait for screen state conditions with timeout
- Async harness works with Tokio runtime
- Can test dgx-pixels navigation (Tab, number keys, Esc)
- Examples demonstrate all event simulation patterns

---

## Task Breakdown

### 1. Event Simulation Foundation

#### 1.1 Key Event Types

**Priority**: P0
**Estimated Effort**: 2-4 hours

**Tasks**:
- [ ] Create `KeyCode` enum for all keyboard keys
  - Alphanumeric: `Char(char)` for a-z, A-Z, 0-9
  - Special: `Enter`, `Esc`, `Tab`, `Backspace`, `Delete`
  - Navigation: `Up`, `Down`, `Left`, `Right`
  - Function: `F1-F12`
  - Other: `Home`, `End`, `PageUp`, `PageDown`, `Insert`
- [ ] Create `Modifiers` bitflags struct
  - `SHIFT`, `CTRL`, `ALT`, `META`
- [ ] Create `KeyEvent` struct combining `KeyCode` and `Modifiers`
- [ ] Implement `Debug` and `Display` for all types
- [ ] Add unit tests for type construction

**Files to Create**:
- `src/events.rs` - Event types module

**API Design**:
```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyCode {
    Char(char),
    Enter,
    Esc,
    Tab,
    Backspace,
    Delete,
    Up,
    Down,
    Left,
    Right,
    Home,
    End,
    PageUp,
    PageDown,
    Insert,
    F(u8), // F1-F12
}

bitflags::bitflags! {
    pub struct Modifiers: u8 {
        const SHIFT = 0b0001;
        const CTRL  = 0b0010;
        const ALT   = 0b0100;
        const META  = 0b1000;
    }
}

#[derive(Debug, Clone, Copy)]
pub struct KeyEvent {
    pub code: KeyCode,
    pub modifiers: Modifiers,
}
```

**Success Criteria**:
- All key types can be constructed
- Modifiers can be combined with bitflags
- Types have clear Debug output

---

#### 1.2 Escape Sequence Generation

**Priority**: P0
**Estimated Effort**: 4-6 hours

**Tasks**:
- [ ] Implement `KeyCode::to_escape_sequence()` method
  - Map each key to its VT100/ANSI escape sequence
  - Handle standard keys (letters, numbers, symbols)
  - Handle special keys (Enter = `\n`, Tab = `\t`, Esc = `\x1b`)
  - Handle navigation keys (Up = `\x1b[A`, Down = `\x1b[B`, etc.)
  - Handle function keys (F1 = `\x1bOP`, etc.)
- [ ] Implement `KeyEvent::to_escape_sequence()` with modifiers
  - Apply modifier prefixes (Ctrl+C = `\x03`, etc.)
  - Handle Alt combinations (Alt+A = `\x1ba`, etc.)
  - Handle complex modifier sequences
- [ ] Create comprehensive test suite
  - Test all key codes
  - Test all modifier combinations
  - Test edge cases (Ctrl+[, etc.)
- [ ] Document escape sequence mapping

**Reference Implementation**:
```rust
impl KeyCode {
    pub fn to_escape_sequence(&self) -> &'static [u8] {
        match self {
            KeyCode::Char(c) => /* return char as bytes */,
            KeyCode::Enter => b"\n",
            KeyCode::Tab => b"\t",
            KeyCode::Esc => b"\x1b",
            KeyCode::Up => b"\x1b[A",
            KeyCode::Down => b"\x1b[B",
            KeyCode::Right => b"\x1b[C",
            KeyCode::Left => b"\x1b[D",
            KeyCode::Home => b"\x1b[H",
            KeyCode::End => b"\x1b[F",
            KeyCode::PageUp => b"\x1b[5~",
            KeyCode::PageDown => b"\x1b[6~",
            KeyCode::F(n) => /* map F1-F12 */,
            // ... more mappings
        }
    }
}
```

**Success Criteria**:
- All keys generate correct escape sequences
- Modifiers are applied correctly
- Tests verify against reference sequences

---

#### 1.3 Harness Event Methods

**Priority**: P0
**Estimated Effort**: 3-4 hours

**Tasks**:
- [ ] Add `send_key(KeyCode)` method to `TuiTestHarness`
  - Convert KeyCode to escape sequence
  - Write to PTY master
  - Update screen state
  - Return Result with proper error handling
- [ ] Add `send_key_with_modifiers(KeyCode, Modifiers)` method
  - Construct KeyEvent
  - Generate modified escape sequence
  - Write to PTY
- [ ] Add `send_keys(&str)` convenience method
  - Convert string to sequence of Char(c) events
  - Send each character individually
  - Alternative name: `type_text(&str)`
- [ ] Add integration tests for each method
  - Test single key sending
  - Test modifier key combinations
  - Test text string typing
  - Verify screen state updates

**API Addition to `harness.rs`**:
```rust
impl TuiTestHarness {
    /// Sends a single key event to the PTY.
    pub fn send_key(&mut self, key: KeyCode) -> Result<()> {
        let sequence = key.to_escape_sequence();
        self.terminal.write(sequence)?;
        self.update_state()?;
        Ok(())
    }

    /// Sends a key with modifiers.
    pub fn send_key_with_modifiers(
        &mut self,
        key: KeyCode,
        modifiers: Modifiers,
    ) -> Result<()> {
        let event = KeyEvent { code: key, modifiers };
        let sequence = event.to_escape_sequence();
        self.terminal.write(sequence)?;
        self.update_state()?;
        Ok(())
    }

    /// Types a text string (sends each character as a key event).
    pub fn send_keys(&mut self, text: &str) -> Result<()> {
        for ch in text.chars() {
            self.send_key(KeyCode::Char(ch))?;
        }
        Ok(())
    }
}
```

**Success Criteria**:
- Methods compile and work correctly
- Integration tests pass
- Error handling is robust

---

### 2. Smart Wait Conditions

#### 2.1 Enhanced Wait API

**Priority**: P0
**Estimated Effort**: 3-4 hours

**Tasks**:
- [ ] Review existing `wait_for()` implementation
- [ ] Add `wait_for_text(text: &str)` convenience method (already exists, verify)
- [ ] Add `wait_for_cursor(row, col)` method
  - Poll until cursor reaches position
  - Timeout if not reached
- [ ] Add `wait_for_state<F>(predicate: F, timeout: Duration)` variant
  - Allow custom timeout per call
- [ ] Improve timeout error messages
  - Include what was being waited for
  - Show current screen state
  - Show elapsed time
- [ ] Add debug logging option
  - Log each polling iteration
  - Log screen state changes
  - Controlled by environment variable or builder option

**API Enhancements**:
```rust
impl TuiTestHarness {
    /// Waits for cursor to reach a specific position.
    pub fn wait_for_cursor(&mut self, row: u16, col: u16) -> Result<()> {
        self.wait_for_with_context(
            |state| state.cursor_position() == (row, col),
            &format!("cursor at ({}, {})", row, col),
        )
    }

    /// Waits for a condition with custom timeout.
    pub fn wait_for_timeout<F>(
        &mut self,
        condition: F,
        timeout: Duration,
    ) -> Result<()>
    where
        F: Fn(&ScreenState) -> bool,
    {
        // Similar to wait_for but uses custom timeout
    }
}
```

**Success Criteria**:
- All wait methods work correctly
- Timeout errors are informative
- Debug logging helps troubleshooting

---

#### 2.2 Common Wait Patterns

**Priority**: P1
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Create module `src/wait_patterns.rs` for common conditions
- [ ] Implement `wait_for_text_at(row, col, text)` helper
- [ ] Implement `wait_for_text_contains(text)` helper (alias to existing)
- [ ] Implement `wait_for_no_text(text)` helper
  - Wait until text disappears
  - Useful for loading spinners
- [ ] Implement `wait_for_change()` helper
  - Wait until screen contents change
  - Compare snapshots
- [ ] Add examples demonstrating each pattern
- [ ] Document common wait patterns in cookbook

**Example API**:
```rust
/// Common wait pattern helpers
pub mod wait {
    pub fn text_at(row: u16, col: u16, text: &str) -> impl Fn(&ScreenState) -> bool {
        move |state| {
            // Check if text appears at position
        }
    }

    pub fn text_disappears(text: &str) -> impl Fn(&ScreenState) -> bool {
        move |state| !state.contains(text)
    }

    pub fn screen_changed(prev: &str) -> impl Fn(&ScreenState) -> bool {
        move |state| state.contents() != prev
    }
}
```

**Success Criteria**:
- Helpers simplify common test patterns
- Examples show practical usage
- Documentation is clear

---

### 3. Tokio Async Integration

#### 3.1 Async Feature Setup

**Priority**: P0
**Estimated Effort**: 1-2 hours

**Tasks**:
- [ ] Verify Cargo.toml has correct tokio dependency (already exists)
- [ ] Verify `async-tokio` feature flag (already exists)
- [ ] Add tokio to dev-dependencies with test feature
- [ ] Configure async runtime in examples
- [ ] Add async examples to CI pipeline

**Cargo.toml verification**:
```toml
[dependencies]
tokio = { version = "1.35", optional = true, features = ["full"] }

[dev-dependencies]
tokio = { version = "1.35", features = ["rt-multi-thread", "macros", "test"] }

[features]
async-tokio = ["tokio"]
```

**Success Criteria**:
- Dependencies are correct
- Feature flags work
- Examples compile with `--features async-tokio`

---

#### 3.2 Async Harness Implementation

**Priority**: P0 (Critical for dgx-pixels)
**Estimated Effort**: 6-8 hours

**Decision**: Implement native async harness vs sync harness in async context

**Option A: Native Async Harness (Recommended)**
- Create `AsyncTuiTestHarness` with async methods
- Use tokio::time for delays
- Use tokio::fs for reading PTY asynchronously
- Better ergonomics for async tests

**Option B: Sync Harness in Async (Current)**
- Use existing `TuiTestHarness` in async functions
- Wrap blocking calls with `tokio::task::spawn_blocking`
- Simpler implementation but less ergonomic

**Recommended: Option A**

**Tasks**:
- [ ] Create `src/async_harness.rs` module
- [ ] Implement `AsyncTuiTestHarness` struct
  - Wrap `TestTerminal` with async I/O
  - Make all methods async
- [ ] Implement async spawn
  ```rust
  pub async fn spawn(&mut self, cmd: CommandBuilder) -> Result<()>
  ```
- [ ] Implement async send methods
  ```rust
  pub async fn send_key(&mut self, key: KeyCode) -> Result<()>
  pub async fn send_keys(&mut self, text: &str) -> Result<()>
  ```
- [ ] Implement async wait methods
  ```rust
  pub async fn wait_for<F>(&mut self, condition: F) -> Result<()>
  where F: Fn(&ScreenState) -> bool
  ```
- [ ] Use `tokio::time::sleep` instead of `std::thread::sleep`
- [ ] Use `tokio::time::timeout` for timeout handling
- [ ] Add async update_state method
- [ ] Write async integration tests using `#[tokio::test]`
- [ ] Create async example demonstrating usage
- [ ] Document async patterns in examples

**API Design**:
```rust
#[cfg(feature = "async-tokio")]
pub struct AsyncTuiTestHarness {
    inner: TuiTestHarness,
}

#[cfg(feature = "async-tokio")]
impl AsyncTuiTestHarness {
    pub async fn new(width: u16, height: u16) -> Result<Self> {
        Ok(Self {
            inner: TuiTestHarness::new(width, height)?,
        })
    }

    pub async fn spawn(&mut self, cmd: CommandBuilder) -> Result<()> {
        // Spawn in blocking task
        let terminal = &mut self.inner.terminal;
        tokio::task::spawn_blocking(move || {
            terminal.spawn(cmd)
        }).await?
    }

    pub async fn send_key(&mut self, key: KeyCode) -> Result<()> {
        self.inner.send_key(key)?;
        Ok(())
    }

    pub async fn wait_for<F>(&mut self, condition: F) -> Result<()>
    where
        F: Fn(&ScreenState) -> bool,
    {
        let timeout = self.inner.timeout;
        tokio::time::timeout(timeout, async {
            loop {
                self.inner.update_state()?;
                if condition(&self.inner.state) {
                    return Ok(());
                }
                tokio::time::sleep(self.inner.poll_interval).await;
            }
        })
        .await
        .map_err(|_| TermTestError::Timeout {
            timeout_ms: timeout.as_millis() as u64,
        })?
    }
}
```

**Success Criteria**:
- Async harness compiles with tokio feature
- All async methods work correctly
- Async tests pass with `#[tokio::test]`
- Examples demonstrate async usage

---

#### 3.3 Async Testing Patterns

**Priority**: P1
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Create comprehensive async testing example
  - Basic async test structure
  - Concurrent test execution
  - Timeout handling patterns
  - Integration with tokio::select!
- [ ] Document async testing best practices
  - When to use sync vs async harness
  - Handling timeouts
  - Concurrent testing strategies
- [ ] Add async tests to test suite
  - Test async spawn
  - Test async wait conditions
  - Test concurrent harness usage
- [ ] Update async_test.rs example with new API

**Example Test Pattern**:
```rust
#[cfg(test)]
mod async_tests {
    use super::*;
    use tokio::test;

    #[tokio::test]
    async fn test_async_wait() -> Result<()> {
        let mut harness = AsyncTuiTestHarness::new(80, 24).await?;
        let mut cmd = CommandBuilder::new("echo");
        cmd.arg("Hello!");

        harness.spawn(cmd).await?;
        harness.wait_for(|state| {
            state.contains("Hello!")
        }).await?;

        Ok(())
    }

    #[tokio::test]
    async fn test_concurrent_harnesses() -> Result<()> {
        let tasks = (0..3).map(|i| {
            tokio::spawn(async move {
                let mut harness = AsyncTuiTestHarness::new(80, 24).await?;
                // ... test logic
                Ok::<(), TermTestError>(())
            })
        });

        for task in tasks {
            task.await??;
        }

        Ok(())
    }
}
```

**Success Criteria**:
- Async patterns are well documented
- Examples cover common scenarios
- Tests demonstrate async capabilities

---

### 4. Integration & Testing

#### 4.1 Event Simulation Tests

**Priority**: P0
**Estimated Effort**: 3-4 hours

**Tasks**:
- [ ] Create `tests/integration/events.rs`
- [ ] Test single key sending
  - Test alphanumeric keys
  - Test special keys (Enter, Tab, Esc)
  - Test navigation keys (arrows)
- [ ] Test key combinations with modifiers
  - Ctrl+C, Ctrl+D
  - Alt+key combinations
  - Multiple modifiers
- [ ] Test text typing
  - Simple strings
  - Strings with special characters
  - Multi-line text
- [ ] Test error handling
  - Invalid key codes
  - PTY write failures
- [ ] Verify screen state updates after input

**Test Structure**:
```rust
#[test]
fn test_send_single_key() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("cat");
    harness.spawn(cmd)?;

    harness.send_key(KeyCode::Char('a'))?;
    harness.wait_for_text("a")?;

    Ok(())
}

#[test]
fn test_send_key_with_ctrl() -> Result<()> {
    // Test Ctrl+D sends EOF
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("cat");
    harness.spawn(cmd)?;

    harness.send_key_with_modifiers(
        KeyCode::Char('d'),
        Modifiers::CTRL
    )?;

    // cat should exit on Ctrl+D
    harness.wait_exit()?;
    Ok(())
}
```

**Success Criteria**:
- All event tests pass
- Coverage includes edge cases
- Tests are reliable and fast

---

#### 4.2 Wait Condition Tests

**Priority**: P0
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Create `tests/integration/wait.rs`
- [ ] Test wait_for with simple condition
- [ ] Test wait_for_text
- [ ] Test wait_for_cursor
- [ ] Test timeout behavior
  - Ensure timeout fires
  - Verify error message quality
- [ ] Test polling interval
  - Verify condition checked multiple times
  - Verify sleep between checks
- [ ] Test immediate success (condition already true)

**Test Examples**:
```rust
#[test]
fn test_wait_for_text() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("sh");
    cmd.arg("-c").arg("sleep 0.1 && echo 'Ready'");
    harness.spawn(cmd)?;

    harness.wait_for_text("Ready")?;
    Ok(())
}

#[test]
fn test_wait_timeout() {
    let mut harness = TuiTestHarness::new(80, 24).unwrap();
    let mut cmd = CommandBuilder::new("cat");
    harness.spawn(cmd).unwrap();

    let result = harness.wait_for(|state| {
        state.contains("NeverAppears")
    });

    assert!(matches!(result, Err(TermTestError::Timeout { .. })));
}
```

**Success Criteria**:
- Wait conditions work reliably
- Timeouts are accurate
- Tests complete quickly

---

#### 4.3 Async Integration Tests

**Priority**: P0
**Estimated Effort**: 3-4 hours

**Tasks**:
- [ ] Create `tests/async_integration.rs` (requires tokio feature)
- [ ] Test async harness creation
- [ ] Test async spawn
- [ ] Test async send_key
- [ ] Test async wait_for
- [ ] Test async timeout handling
- [ ] Test concurrent harness usage
- [ ] Add tests to CI pipeline with async-tokio feature

**Test Examples**:
```rust
#[cfg(feature = "async-tokio")]
mod async_tests {
    use term_test::AsyncTuiTestHarness;

    #[tokio::test]
    async fn test_async_basic() -> term_test::Result<()> {
        let mut harness = AsyncTuiTestHarness::new(80, 24).await?;
        // ... async test logic
        Ok(())
    }

    #[tokio::test]
    async fn test_async_concurrent() -> term_test::Result<()> {
        tokio::join!(
            async { /* harness 1 */ },
            async { /* harness 2 */ },
        );
        Ok(())
    }
}
```

**Success Criteria**:
- Async tests pass with tokio
- Concurrent execution works
- Tests are fast and reliable

---

### 5. Documentation & Examples

#### 5.1 API Documentation

**Priority**: P0
**Estimated Effort**: 3-4 hours

**Tasks**:
- [ ] Document all new public APIs with rustdoc
  - KeyCode enum and variants
  - Modifiers bitflags
  - KeyEvent struct
  - send_key methods
  - wait_for methods
  - AsyncTuiTestHarness (if implemented)
- [ ] Add code examples to each method
- [ ] Document common patterns
- [ ] Add links between related methods
- [ ] Run `cargo doc` and verify output

**Documentation Standards**:
- Each public item must have summary
- Complex items need examples
- Examples must compile and run
- Links to related functionality

**Success Criteria**:
- `cargo doc` builds without warnings
- Documentation is clear and helpful
- Examples demonstrate usage

---

#### 5.2 Usage Examples

**Priority**: P0
**Estimated Effort**: 4-5 hours

**Tasks**:
- [ ] Update `examples/basic_test.rs` with event simulation
- [ ] Create `examples/keyboard_events.rs`
  - Demonstrate all key types
  - Show modifier usage
  - Show text typing
- [ ] Create `examples/wait_patterns.rs`
  - Show all wait conditions
  - Demonstrate timeout handling
  - Show debugging failed waits
- [ ] Update `examples/async_test.rs` with AsyncTuiTestHarness
- [ ] Create `examples/dgx_pixels_navigation.rs`
  - Simulate Tab navigation
  - Simulate number key screen switching
  - Simulate Esc to exit
- [ ] Verify all examples run successfully
- [ ] Add examples to README

**Example Structure**:
```rust
//! examples/keyboard_events.rs
//!
//! Demonstrates keyboard event simulation patterns.

use term_test::{TuiTestHarness, KeyCode, Modifiers};

fn main() -> term_test::Result<()> {
    // Example 1: Single keys
    example_single_keys()?;

    // Example 2: Modifiers
    example_modifiers()?;

    // Example 3: Text typing
    example_text_typing()?;

    Ok(())
}
```

**Success Criteria**:
- Examples cover all major features
- Examples run without errors
- Examples are instructive

---

#### 5.3 User Guide

**Priority**: P1
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Create `docs/EVENT_SIMULATION.md`
  - Keyboard event basics
  - Available key codes
  - Using modifiers
  - Common patterns
- [ ] Create `docs/WAIT_CONDITIONS.md`
  - How wait_for works
  - Built-in wait helpers
  - Custom conditions
  - Timeout strategies
  - Debugging tips
- [ ] Create `docs/ASYNC_TESTING.md`
  - When to use async
  - Async harness vs sync harness
  - Tokio integration
  - Concurrent testing
- [ ] Update main README with Phase 2 features
- [ ] Add cookbook entries for common scenarios

**Documentation Coverage**:
- Getting started tutorials
- API reference
- Common patterns
- Troubleshooting

**Success Criteria**:
- Documentation is comprehensive
- Examples are clear
- Troubleshooting helps resolve issues

---

### 6. dgx-pixels Integration Validation

#### 6.1 Navigation Testing

**Priority**: P0 (MVP Requirement)
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Create dgx-pixels-specific test example
- [ ] Test Tab key navigation between UI elements
- [ ] Test number keys (1-8) for screen switching
- [ ] Test Esc key to exit/cancel
- [ ] Test text input in generation prompts
- [ ] Document dgx-pixels testing patterns

**Example Test**:
```rust
// Example dgx-pixels navigation test
#[test]
fn test_dgx_pixels_screen_navigation() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    // Spawn dgx-pixels (or mock)
    harness.spawn(/* ... */)?;

    // Wait for main menu
    harness.wait_for_text("Gallery")?;

    // Navigate to Gallery screen with '2'
    harness.send_key(KeyCode::Char('2'))?;
    harness.wait_for_text("Gallery Screen")?;

    // Use Tab to navigate
    harness.send_key(KeyCode::Tab)?;
    // Verify focus change

    // Press Esc to return
    harness.send_key(KeyCode::Esc)?;
    harness.wait_for_text("Main Menu")?;

    Ok(())
}
```

**Success Criteria**:
- All dgx-pixels navigation patterns work
- Tests are reliable
- Documentation helps dgx-pixels integration

---

### 7. Polish & Release

#### 7.1 Error Handling

**Priority**: P0
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Review all error paths
- [ ] Ensure error messages are actionable
- [ ] Add context to errors (what operation failed)
- [ ] Test error scenarios
  - Invalid key codes
  - Timeout errors
  - PTY write failures
- [ ] Document error handling best practices

**Error Message Quality**:
- What went wrong
- Why it went wrong (if knowable)
- How to fix it
- Current state information

**Success Criteria**:
- Error messages help debugging
- Error types are appropriate
- Error handling is consistent

---

#### 7.2 Performance

**Priority**: P1
**Estimated Effort**: 2-3 hours

**Tasks**:
- [ ] Profile wait condition polling overhead
- [ ] Optimize hot paths
  - Escape sequence generation
  - Screen state updates
  - Polling loops
- [ ] Benchmark event sending speed
- [ ] Benchmark wait condition performance
- [ ] Document performance characteristics

**Performance Targets**:
- Event sending: < 1ms per event
- Wait condition check: < 10ms per iteration
- Overall test overhead: < 10% of test time

**Success Criteria**:
- Performance is acceptable
- No obvious bottlenecks
- Benchmarks document performance

---

#### 7.3 CI/CD Integration

**Priority**: P0
**Estimated Effort**: 1-2 hours

**Tasks**:
- [ ] Add Phase 2 tests to CI pipeline
- [ ] Test with async-tokio feature enabled
- [ ] Test without async-tokio feature
- [ ] Verify examples run in CI
- [ ] Check code coverage for Phase 2 code
- [ ] Ensure tests are stable and don't flake

**CI Configuration**:
```yaml
# .github/workflows/ci.yml
- name: Test Phase 2 (sync)
  run: cargo test --lib

- name: Test Phase 2 (async)
  run: cargo test --lib --features async-tokio

- name: Run examples
  run: |
    cargo run --example keyboard_events
    cargo run --example wait_patterns
    cargo run --example async_test --features async-tokio
```

**Success Criteria**:
- All CI tests pass
- Tests run on Linux (primary target)
- No flaky tests
- Coverage maintained > 70%

---

## Timeline Estimate

### Week 1 (Days 1-5)

**Focus**: Core Event Simulation

- Day 1-2: Event types and escape sequence generation (1.1, 1.2)
- Day 3: Harness event methods (1.3)
- Day 4-5: Wait conditions and patterns (2.1, 2.2)

### Week 2 (Days 6-10)

**Focus**: Async Support & Integration

- Day 6-7: Async harness implementation (3.1, 3.2)
- Day 8: Async patterns and testing (3.3)
- Day 9: Integration tests (4.1, 4.2, 4.3)
- Day 10: Documentation and polish (5.1, 5.2, 5.3)

### Buffer (Days 11-14)

**Focus**: Validation & Polish

- Day 11: dgx-pixels integration validation (6.1)
- Day 12: Error handling and performance (7.1, 7.2)
- Day 13: CI/CD integration (7.3)
- Day 14: Final testing and bug fixes

**Total**: 10-14 days (2 weeks nominal, 2.8 weeks with buffer)

---

## Risk Assessment

### High Risk

**Risk**: Escape sequence mapping incompatibilities
- **Mitigation**: Test against multiple terminals, reference VT100 spec
- **Fallback**: Provide escape sequence customization API

**Risk**: Async harness complexity
- **Mitigation**: Start with Option B (sync in async), implement Option A if needed
- **Fallback**: Document sync harness async patterns thoroughly

### Medium Risk

**Risk**: Flaky wait condition tests
- **Mitigation**: Use generous timeouts in tests, mock PTY behavior where possible
- **Fallback**: Add retry logic to tests

**Risk**: Performance overhead from polling
- **Mitigation**: Optimize polling interval, consider event-driven approach
- **Fallback**: Document performance characteristics, let users tune

### Low Risk

**Risk**: Missing key codes
- **Mitigation**: Start with common keys, add more as needed
- **Fallback**: Provide escape sequence pass-through API

---

## Dependencies

### Internal Dependencies (Phase 1)

- [x] TuiTestHarness implemented
- [x] TestTerminal with write capability
- [x] ScreenState with update mechanism
- [x] wait_for infrastructure exists
- [x] Error types defined

### External Dependencies

- [x] tokio = "1.35" (optional, already in Cargo.toml)
- [x] portable-pty = "0.8" (already in use)
- [ ] bitflags = "2.4" (for Modifiers, need to add)

**Action**: Add bitflags to Cargo.toml

---

## Acceptance Criteria

Phase 2 is complete when:

1. **Event Simulation**
   - [ ] Can send single keys with `send_key()`
   - [ ] Can send keys with modifiers
   - [ ] Can type text strings with `send_keys()`
   - [ ] All key codes generate correct escape sequences
   - [ ] Integration tests verify event handling

2. **Wait Conditions**
   - [ ] `wait_for()` works reliably
   - [ ] `wait_for_text()` convenience method works
   - [ ] `wait_for_cursor()` works
   - [ ] Timeouts work correctly
   - [ ] Error messages are helpful

3. **Async Support**
   - [ ] AsyncTuiTestHarness compiles with async-tokio feature
   - [ ] Async methods work correctly
   - [ ] Can use with `#[tokio::test]`
   - [ ] Concurrent harness usage works
   - [ ] Async examples demonstrate patterns

4. **Testing**
   - [ ] All unit tests pass
   - [ ] All integration tests pass
   - [ ] All async tests pass
   - [ ] Code coverage > 70%
   - [ ] CI/CD passes

5. **Documentation**
   - [ ] All public APIs documented
   - [ ] Examples demonstrate all features
   - [ ] User guides complete
   - [ ] dgx-pixels patterns documented

6. **dgx-pixels Validation**
   - [ ] Can simulate Tab navigation
   - [ ] Can simulate number key screen switching
   - [ ] Can simulate Esc key
   - [ ] Can test text input
   - [ ] Example tests written

---

## Next Phase Preview

**Phase 3: Sixel Graphics Support with Position Tracking**

After Phase 2, we'll implement:
- Sixel sequence detection via DCS callbacks
- Position tracking for Sixel graphics
- Bounds checking assertions
- Integration with dgx-pixels preview areas

Phase 3 depends on Phase 2 for:
- Event simulation to trigger Sixel rendering
- Wait conditions to detect Sixel appearance
- Async support for Tokio-based apps

---

## Resources

### Reference Documentation

- VT100 escape sequences: https://vt100.net/docs/vt100-ug/chapter3.html
- ANSI escape codes: https://en.wikipedia.org/wiki/ANSI_escape_code
- Tokio async: https://tokio.rs/tokio/tutorial
- bitflags crate: https://docs.rs/bitflags/

### Related Code

- crossterm KeyEvent: https://docs.rs/crossterm/latest/crossterm/event/struct.KeyEvent.html
- termion Key enum: https://docs.rs/termion/latest/termion/event/enum.Key.html
- term-transcript: https://github.com/slowli/term-transcript (CLI testing reference)

---

**Document Version**: 1.0
**Last Updated**: 2025-11-20
**Status**: Ready for Implementation
**Estimated Duration**: 1-2 weeks (10-14 days)