envision 0.16.0

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
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
#![cfg(feature = "full")]
//! Async integration tests exercising commands, message channels, subscriptions,
//! and error handling.

use std::time::Duration;

use envision::{App, Command, Runtime};
use ratatui::prelude::*;

// ===========================================================================
// Shared App: AsyncLoader (tests Command::perform_async)
// ===========================================================================

struct AsyncLoaderApp;

#[derive(Clone, Default)]
struct AsyncLoaderState {
    data: Option<String>,
    loading: bool,
}

#[derive(Clone, Debug)]
enum AsyncLoaderMsg {
    StartLoad,
    DataLoaded(String),
}

impl App for AsyncLoaderApp {
    type State = AsyncLoaderState;
    type Message = AsyncLoaderMsg;

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            AsyncLoaderMsg::StartLoad => {
                state.loading = true;
                Command::perform_async(async {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    Some(AsyncLoaderMsg::DataLoaded("hello world".into()))
                })
            }
            AsyncLoaderMsg::DataLoaded(data) => {
                state.loading = false;
                state.data = Some(data);
                Command::none()
            }
        }
    }

    fn view(state: &Self::State, frame: &mut Frame) {
        let text = if state.loading {
            "Loading...".to_string()
        } else if let Some(ref data) = state.data {
            format!("Data: {}", data)
        } else {
            "Idle".to_string()
        };
        frame.render_widget(ratatui::widgets::Paragraph::new(text), frame.area());
    }
}

// ===========================================================================
// Shared App: FallibleApp (tests try_perform_async)
// ===========================================================================

struct FallibleApp;

#[derive(Clone, Default)]
struct FallibleState {
    data: Option<String>,
}

#[derive(Clone, Debug)]
enum FallibleMsg {
    StartFailing,
    StartSucceeding,
    DataLoaded(String),
}

impl App for FallibleApp {
    type State = FallibleState;
    type Message = FallibleMsg;

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            FallibleMsg::StartFailing => Command::try_perform_async(
                async {
                    Err(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        "resource not found",
                    ))
                },
                |_: ()| Some(FallibleMsg::DataLoaded("unreachable".into())),
            ),
            FallibleMsg::StartSucceeding => Command::try_perform_async(
                async { Ok::<_, std::io::Error>("success data".to_string()) },
                |data| Some(FallibleMsg::DataLoaded(data)),
            ),
            FallibleMsg::DataLoaded(data) => {
                state.data = Some(data);
                Command::none()
            }
        }
    }

    fn view(state: &Self::State, frame: &mut Frame) {
        let text = state.data.as_deref().unwrap_or("no data");
        frame.render_widget(ratatui::widgets::Paragraph::new(text), frame.area());
    }
}

// ===========================================================================
// Shared App: TickCounter (tests message channel delivery)
// ===========================================================================

struct TickCounterApp;

#[derive(Clone, Default)]
struct TickCounterState {
    count: u32,
}

#[derive(Clone, Debug)]
enum TickCounterMsg {
    Tick,
}

impl App for TickCounterApp {
    type State = TickCounterState;
    type Message = TickCounterMsg;

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            TickCounterMsg::Tick => state.count += 1,
        }
        Command::none()
    }

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

// ===========================================================================
// Shared App: ChainedApp (tests command chaining: one async triggers another)
// ===========================================================================

struct ChainedApp;

#[derive(Clone, Default)]
struct ChainedState {
    step1_done: bool,
    step2_done: bool,
    final_result: Option<String>,
}

#[derive(Clone, Debug)]
enum ChainedMsg {
    StartChain,
    Step1Complete,
    Step2Complete(String),
}

impl App for ChainedApp {
    type State = ChainedState;
    type Message = ChainedMsg;

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

    fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
        match msg {
            ChainedMsg::StartChain => {
                // Step 1: async operation
                Command::perform_async(async {
                    tokio::time::sleep(Duration::from_millis(5)).await;
                    Some(ChainedMsg::Step1Complete)
                })
            }
            ChainedMsg::Step1Complete => {
                state.step1_done = true;
                // Step 2: another async triggered by step 1
                Command::perform_async(async {
                    tokio::time::sleep(Duration::from_millis(5)).await;
                    Some(ChainedMsg::Step2Complete("chain complete".into()))
                })
            }
            ChainedMsg::Step2Complete(result) => {
                state.step2_done = true;
                state.final_result = Some(result);
                Command::none()
            }
        }
    }

    fn view(state: &Self::State, frame: &mut Frame) {
        let text = state.final_result.as_deref().unwrap_or("pending");
        frame.render_widget(ratatui::widgets::Paragraph::new(text), frame.area());
    }
}

// ===========================================================================
// Tests: Command::perform_async
// ===========================================================================

#[tokio::test]
async fn test_command_perform_async_updates_state() {
    let mut vt = Runtime::<AsyncLoaderApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    assert!(vt.state().data.is_none());
    assert!(!vt.state().loading);

    // Dispatch the load command
    vt.dispatch(AsyncLoaderMsg::StartLoad);
    assert!(vt.state().loading);

    // Wait for the async task to complete
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    assert!(!vt.state().loading);
    assert_eq!(vt.state().data, Some("hello world".to_string()));
}

#[tokio::test]
async fn test_command_perform_async_chained() {
    let mut vt = Runtime::<ChainedApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    // Start the chain
    vt.dispatch(ChainedMsg::StartChain);

    // Wait for step 1 (longer sleep for CI timing variance)
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    assert!(vt.state().step1_done);

    // Wait for step 2 (triggered by step 1)
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    assert!(vt.state().step2_done);
    assert_eq!(vt.state().final_result, Some("chain complete".to_string()));
}

// ===========================================================================
// Tests: try_perform_async
// ===========================================================================

#[tokio::test]
async fn test_try_perform_async_error_reporting() {
    let mut vt = Runtime::<FallibleApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    vt.dispatch(FallibleMsg::StartFailing);
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    // State should NOT have been updated
    assert!(vt.state().data.is_none());

    // Error should be collected
    let errors = vt.take_errors();
    assert_eq!(errors.len(), 1);
    assert!(errors[0].to_string().contains("resource not found"));
}

#[tokio::test]
async fn test_try_perform_async_success_updates_state() {
    let mut vt = Runtime::<FallibleApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    vt.dispatch(FallibleMsg::StartSucceeding);
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    assert_eq!(vt.state().data, Some("success data".to_string()));
}

#[tokio::test]
async fn test_try_perform_async_error_then_success() {
    let mut vt = Runtime::<FallibleApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    // First: a failing command
    vt.dispatch(FallibleMsg::StartFailing);
    tokio::time::sleep(Duration::from_millis(20)).await;
    vt.process_pending();

    assert!(vt.state().data.is_none());
    let errors = vt.take_errors();
    assert_eq!(errors.len(), 1);

    // Second: a succeeding command — state should update despite previous error
    vt.dispatch(FallibleMsg::StartSucceeding);
    tokio::time::sleep(Duration::from_millis(20)).await;
    vt.process_pending();

    assert_eq!(vt.state().data, Some("success data".to_string()));
    // No new errors
    assert!(vt.take_errors().is_empty());
}

// ===========================================================================
// Tests: Message channel
// ===========================================================================

#[tokio::test]
async fn test_message_channel_delivers_messages() {
    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();
    let tx = vt.message_sender();

    // Send messages via the channel
    tx.send(TickCounterMsg::Tick).await.unwrap();
    tx.send(TickCounterMsg::Tick).await.unwrap();
    tx.send(TickCounterMsg::Tick).await.unwrap();

    vt.process_pending();
    assert_eq!(vt.state().count, 3);
}

#[tokio::test]
async fn test_message_channel_interleaved_with_dispatch() {
    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();
    let tx = vt.message_sender();

    // Direct dispatch
    vt.dispatch(TickCounterMsg::Tick);
    assert_eq!(vt.state().count, 1);

    // Channel message
    tx.send(TickCounterMsg::Tick).await.unwrap();
    vt.process_pending();
    assert_eq!(vt.state().count, 2);

    // Another direct dispatch
    vt.dispatch(TickCounterMsg::Tick);
    assert_eq!(vt.state().count, 3);
}

#[tokio::test]
async fn test_message_channel_from_spawned_task() {
    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();
    let tx = vt.message_sender();

    // Spawn a task that sends messages
    tokio::spawn(async move {
        for _ in 0..10 {
            tx.send(TickCounterMsg::Tick).await.unwrap();
        }
    });

    // Wait for spawned task to complete
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    assert_eq!(vt.state().count, 10);
}

// ===========================================================================
// Tests: Render after async operations
// ===========================================================================

#[tokio::test]
async fn test_render_reflects_async_state() {
    let mut vt = Runtime::<AsyncLoaderApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    vt.render().unwrap();
    assert!(vt.contains_text("Idle"));

    vt.dispatch(AsyncLoaderMsg::StartLoad);
    vt.render().unwrap();
    assert!(vt.contains_text("Loading..."));

    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    vt.render().unwrap();
    assert!(vt.contains_text("Data: hello world"));
    assert!(!vt.contains_text("Loading..."));
}

#[tokio::test]
async fn test_render_after_chained_async() {
    let mut vt = Runtime::<ChainedApp, _>::virtual_builder(60, 10)
        .build()
        .unwrap();

    vt.render().unwrap();
    assert!(vt.contains_text("pending"));

    vt.dispatch(ChainedMsg::StartChain);
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    vt.render().unwrap();
    assert!(vt.contains_text("chain complete"));
}

// Tests: AppHarness async methods (test-utils feature)
// ===========================================================================

#[cfg(feature = "test-utils")]
mod app_harness_tests {
    use std::time::Duration;

    use envision::harness::AppHarness;
    use envision::{App, Command};
    use ratatui::prelude::*;

    struct TimedApp;

    #[derive(Clone, Default)]
    struct TimedState {
        data: Option<String>,
        loading: bool,
    }

    #[derive(Clone, Debug)]
    enum TimedMsg {
        StartLoad,
        DataLoaded(String),
    }

    impl App for TimedApp {
        type State = TimedState;
        type Message = TimedMsg;

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

        fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
            match msg {
                TimedMsg::StartLoad => {
                    state.loading = true;
                    Command::perform_async(async {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        Some(TimedMsg::DataLoaded("loaded".into()))
                    })
                }
                TimedMsg::DataLoaded(data) => {
                    state.loading = false;
                    state.data = Some(data);
                    Command::none()
                }
            }
        }

        fn view(state: &Self::State, frame: &mut Frame) {
            let text = if state.loading {
                "Loading...".to_string()
            } else if let Some(ref data) = state.data {
                format!("Data: {}", data)
            } else {
                "Idle".to_string()
            };
            frame.render_widget(ratatui::widgets::Paragraph::new(text), frame.area());
        }
    }

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

        harness.dispatch(TimedMsg::StartLoad);
        assert!(harness.state().loading);
        assert!(harness.state().data.is_none());

        // Advance time past the 100ms sleep
        harness.advance_time(Duration::from_millis(150)).await;

        assert!(!harness.state().loading);
        assert_eq!(harness.state().data, Some("loaded".to_string()));
    }

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

        harness.dispatch(TimedMsg::StartLoad);

        let found = harness
            .wait_for(|state| state.data.is_some(), Duration::from_secs(1))
            .await;

        assert!(found);
        assert_eq!(harness.state().data, Some("loaded".to_string()));
    }

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

        harness.dispatch(TimedMsg::StartLoad);

        let found = harness
            .wait_for_text("Data: loaded", Duration::from_secs(1))
            .await;

        assert!(found);
        assert!(harness.contains_text("Data: loaded"));
    }
}

// ===========================================================================
// Tests: Subscriptions
// ===========================================================================

#[tokio::test]
async fn test_tick_subscription_delivers_messages() {
    use envision::app::TickSubscription;

    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    // Subscribe with a 10ms tick interval
    let sub = TickSubscription::new(Duration::from_millis(10), || TickCounterMsg::Tick);
    vt.subscribe(sub);

    // Wait for several ticks to fire
    tokio::time::sleep(Duration::from_millis(55)).await;
    vt.process_pending();

    // Should have received multiple tick messages
    assert!(
        vt.state().count >= 3,
        "Expected at least 3 ticks, got {}",
        vt.state().count
    );
}

#[tokio::test]
async fn test_timer_subscription_fires_once() {
    use envision::app::TimerSubscription;

    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    let sub = TimerSubscription::after(Duration::from_millis(20), TickCounterMsg::Tick);
    vt.subscribe(sub);

    // Wait for the timer to fire
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();

    // Timer fires exactly once
    assert_eq!(vt.state().count, 1);

    // Wait more - should not fire again
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    assert_eq!(vt.state().count, 1);
}

#[tokio::test]
async fn test_channel_subscription_forwards_messages() {
    use tokio::sync::mpsc;

    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    let (tx, rx) = mpsc::channel::<TickCounterMsg>(10);
    let sub = envision::app::ChannelSubscription::new(rx);
    vt.subscribe(sub);

    // Send messages through the external channel
    tx.send(TickCounterMsg::Tick).await.unwrap();
    tx.send(TickCounterMsg::Tick).await.unwrap();
    tx.send(TickCounterMsg::Tick).await.unwrap();

    // Wait for forwarding
    tokio::time::sleep(Duration::from_millis(20)).await;
    vt.process_pending();

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

#[tokio::test]
async fn test_subscription_cancellation() {
    use envision::app::TickSubscription;

    let mut vt = Runtime::<TickCounterApp, _>::virtual_builder(40, 10)
        .build()
        .unwrap();

    let sub = TickSubscription::new(Duration::from_millis(10), || TickCounterMsg::Tick);
    vt.subscribe(sub);

    // Let some ticks fire
    tokio::time::sleep(Duration::from_millis(35)).await;
    vt.process_pending();
    let count_before_quit = vt.state().count;
    assert!(count_before_quit >= 1);

    // Quit cancels subscriptions
    vt.quit();

    // Drain any messages that were already in-flight before cancellation.
    // On Windows CI, timing jitter means several messages may already be
    // buffered in the channel, so we drain fully rather than asserting a
    // tight bound on in-flight count.
    tokio::time::sleep(Duration::from_millis(50)).await;
    vt.process_pending();
    let count_after_drain = vt.state().count;

    // The key invariant: after draining, no NEW messages should arrive
    // because the subscription has been cancelled.
    tokio::time::sleep(Duration::from_millis(100)).await;
    vt.process_pending();
    assert_eq!(
        vt.state().count,
        count_after_drain,
        "Expected no new messages after cancellation drain"
    );
}