envision 0.10.1

A ratatui framework for collaborative TUI development with headless testing 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
use super::*;
use crate::app::Command;
use ratatui::widgets::Paragraph;

struct TestApp;

#[derive(Clone, Default)]
struct TestState {
    count: i32,
    async_result: Option<i32>,
    quit: bool,
}

#[derive(Clone, Debug)]
enum TestMsg {
    Increment,
    SetAsyncResult(i32),
    StartAsyncOp,
    Quit,
}

impl App for TestApp {
    type State = TestState;
    type Message = TestMsg;

    fn init() -> (Self::State, Command<Self::Message>) {
        (TestState::default(), Command::none())
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            TestMsg::Increment => {
                state.count += 1;
                Command::none()
            }
            TestMsg::SetAsyncResult(v) => {
                state.async_result = Some(v);
                Command::none()
            }
            TestMsg::StartAsyncOp => Command::perform_async(async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Some(TestMsg::SetAsyncResult(42))
            }),
            TestMsg::Quit => {
                state.quit = true;
                Command::none()
            }
        }
    }

    fn view(state: &Self::State, frame: &mut ratatui::Frame) {
        let text = format!("Count: {}", state.count);
        frame.render_widget(Paragraph::new(text), frame.area());
    }

    fn should_quit(state: &Self::State) -> bool {
        state.quit
    }
}

#[test]
fn test_async_harness_new() {
    let harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    assert_eq!(harness.state().count, 0);
}

#[test]
fn test_async_harness_dispatch() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.dispatch(TestMsg::Increment);
    assert_eq!(harness.state().count, 1);

    harness.dispatch(TestMsg::Increment);
    assert_eq!(harness.state().count, 2);
}

#[test]
fn test_async_harness_dispatch_all() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.dispatch_all(vec![
        TestMsg::Increment,
        TestMsg::Increment,
        TestMsg::Increment,
    ]);

    assert_eq!(harness.state().count, 3);
}

#[test]
fn test_async_harness_render() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.dispatch(TestMsg::Increment);
    harness.render().unwrap();

    assert!(harness.contains_text("Count: 1"));
}

#[test]
fn test_async_harness_events() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.type_str("hello");
    harness.enter();

    assert_eq!(harness.events().len(), 6);
}

#[test]
fn test_async_harness_tick() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.dispatch(TestMsg::Increment);
    harness.tick().unwrap();

    assert!(harness.contains_text("Count: 1"));
}

#[test]
fn test_async_harness_quit() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    assert!(!harness.should_quit());

    harness.dispatch(TestMsg::Quit);
    harness.tick().unwrap();

    assert!(harness.should_quit());
}

#[test]
fn test_async_harness_assert_contains() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    harness.assert_contains("Count: 0");
    harness.assert_not_contains("Unknown");
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_async_command() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    // Start async operation
    harness.dispatch(TestMsg::StartAsyncOp);

    // Result not available yet
    assert!(harness.state().async_result.is_none());

    // Advance time past the delay
    harness.advance_time(Duration::from_secs(2)).await;

    // Now the result should be available
    assert_eq!(harness.state().async_result, Some(42));
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_wait_for() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.dispatch(TestMsg::StartAsyncOp);

    let success = harness
        .wait_for(
            |state| state.async_result.is_some(),
            Duration::from_secs(10),
        )
        .await;

    assert!(success);
    assert_eq!(harness.state().async_result, Some(42));
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_wait_for_timeout() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    // Don't start any async op, so condition will never be true
    let success = harness
        .wait_for(
            |state| state.async_result.is_some(),
            Duration::from_millis(100),
        )
        .await;

    assert!(!success);
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_wait_for_text() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();

    // Increment count
    harness.dispatch(TestMsg::Increment);

    let success = harness
        .wait_for_text("Count: 1", Duration::from_secs(1))
        .await;

    assert!(success);
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_sleep() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.dispatch(TestMsg::StartAsyncOp);
    harness.sleep(Duration::from_secs(5)).await;

    assert_eq!(harness.state().async_result, Some(42));
}

#[test]
fn test_async_harness_screen() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    let screen = harness.screen();
    assert!(screen.contains("Count: 0"));
}

#[test]
fn test_async_harness_row() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    let row = harness.row(0);
    assert!(row.contains("Count: 0"));
}

#[test]
fn test_async_harness_find_text() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    let positions = harness.find_text("Count");
    assert!(!positions.is_empty());
}

#[test]
fn test_async_harness_input_methods() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    harness.escape();
    harness.tab();
    harness.ctrl('c');
    harness.click(10, 20);
    harness.push_event(Event::char('x'));

    assert_eq!(harness.events().len(), 5);
}

#[test]
fn test_async_harness_cancellation_token() {
    let harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    let token = harness.cancellation_token();
    assert!(!token.is_cancelled());
}

#[test]
fn test_async_harness_manual_quit() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    assert!(!harness.should_quit());

    harness.quit();
    assert!(harness.should_quit());
}

#[test]
fn test_async_harness_with_config() {
    let config = RuntimeConfig::new()
        .tick_rate(Duration::from_millis(100))
        .with_history(5);

    let harness = AppHarness::<TestApp>::with_config(80, 24, config).unwrap();
    assert_eq!(harness.state().count, 0);
}

#[test]
fn test_async_harness_process_events() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    harness.type_str("abc");
    harness.process_events();
    // Events processed (but TestApp doesn't handle key events)
}

#[test]
fn test_async_harness_run_ticks() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.dispatch(TestMsg::Increment);
    harness.run_ticks(3).unwrap();

    assert!(harness.contains_text("Count: 1"));
}

#[test]
fn test_async_harness_state_mut() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    harness.state_mut().count = 42;
    assert_eq!(harness.state().count, 42);
}

#[test]
fn test_async_harness_screen_ansi() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    let screen_ansi = harness.screen_ansi();
    assert!(screen_ansi.contains("Count: 0"));
}

#[test]
fn test_async_harness_cell_at() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    // Cell at (0,0) should have the 'C' from "Count: 0"
    let cell = harness.cell_at(0, 0).unwrap();
    assert_eq!(cell.symbol(), "C");

    // Out of bounds should return None
    assert!(harness.cell_at(100, 100).is_none());
}

#[test]
fn test_async_harness_backend() {
    use ratatui::backend::Backend;
    let harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    let backend = harness.backend();
    assert_eq!(backend.size().unwrap().width, 80);
}

#[test]
fn test_async_harness_backend_mut() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    let _backend = harness.backend_mut();
    // Just verify we can get a mutable reference
}

#[tokio::test]
async fn test_async_harness_message_sender() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    let sender = harness.message_sender();

    // Send a message via the channel
    sender.send(TestMsg::Increment).await.unwrap();

    // Let the runtime process it
    tokio::time::sleep(Duration::from_millis(1)).await;
    harness.runtime.process_pending();

    assert_eq!(harness.state().count, 1);
}

#[tokio::test]
async fn test_async_harness_subscribe() {
    use crate::app::TickSubscription;

    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();
    let sub = TickSubscription::new(Duration::from_millis(10), || TestMsg::Increment);
    harness.subscribe(sub);
    // Just verify we can add a subscription (spawns a tokio task)
    harness.quit();
}

#[tokio::test]
async fn test_async_harness_subscribe_all() {
    use crate::app::{BoxedSubscription, TickSubscription};

    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    let sub1: BoxedSubscription<TestMsg> =
        Box::new(TickSubscription::new(Duration::from_millis(10), || {
            TestMsg::Increment
        }));
    let sub2: BoxedSubscription<TestMsg> =
        Box::new(TickSubscription::new(Duration::from_millis(10), || {
            TestMsg::Increment
        }));

    harness.subscribe_all(vec![sub1, sub2]);
    // Just verify we can add multiple subscriptions (spawns tokio tasks)
    harness.quit();
}

#[tokio::test(start_paused = true)]
async fn test_async_harness_wait_for_text_timeout() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();

    // Don't dispatch anything that changes text to "Count: 5"
    let success = harness
        .wait_for_text("Count: 5", Duration::from_millis(100))
        .await;

    assert!(!success);
}

#[test]
#[should_panic(expected = "Expected screen to contain 'MISSING'")]
fn test_async_harness_assert_contains_panic() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    // This should panic because 'MISSING' is not on screen
    harness.assert_contains("MISSING");
}

#[test]
#[should_panic(expected = "Expected screen to NOT contain 'Count'")]
fn test_async_harness_assert_not_contains_panic() {
    let mut harness = AppHarness::<TestApp>::new(40, 10).unwrap();
    harness.render().unwrap();

    // This should panic because 'Count' is on screen
    harness.assert_not_contains("Count");
}

#[test]
fn test_async_harness_events_direct() {
    let mut harness = AppHarness::<TestApp>::new(80, 24).unwrap();

    let events = harness.events();
    events.push(Event::char('a'));

    assert!(!harness.events().is_empty());
}