envision 0.15.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
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
use super::*;

#[tokio::test]
async fn test_tick_subscription() {
    let cancel = CancellationToken::new();
    let sub = Box::new(TickSubscription::new(Duration::from_millis(10), || {
        TestMsg::Tick
    }));

    let mut stream = sub.into_stream(cancel.clone());

    // Get first tick
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Tick));

    // Cancel and verify stream ends
    cancel.cancel();
}

#[tokio::test]
async fn test_tick_builder() {
    let cancel = CancellationToken::new();
    let sub = Box::new(tick(Duration::from_millis(10)).with_message(|| TestMsg::Tick));

    let mut stream = sub.into_stream(cancel.clone());
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Tick));

    cancel.cancel();
}

#[tokio::test]
async fn test_timer_subscription() {
    let cancel = CancellationToken::new();
    let sub = Box::new(TimerSubscription::after(
        Duration::from_millis(10),
        TestMsg::Timer,
    ));

    let mut stream = sub.into_stream(cancel);

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Timer));

    // Timer should only fire once
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_timer_cancellation() {
    let cancel = CancellationToken::new();
    let sub = Box::new(TimerSubscription::after(
        Duration::from_secs(10),
        TestMsg::Timer,
    ));

    let mut stream = sub.into_stream(cancel.clone());

    // Cancel before timer fires
    cancel.cancel();

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_channel_subscription() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::channel(10);
    let sub = Box::new(ChannelSubscription::new(rx));

    let mut stream = sub.into_stream(cancel.clone());

    // Send messages
    tx.send(TestMsg::Value(1)).await.unwrap();
    tx.send(TestMsg::Value(2)).await.unwrap();

    // Receive messages
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(1)));

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(2)));

    // Drop sender to close channel
    drop(tx);

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_unbounded_channel_subscription() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::unbounded_channel();
    let sub = Box::new(UnboundedChannelSubscription::new(rx));

    let mut stream = sub.into_stream(cancel.clone());

    // Send messages (unbounded send is synchronous, never blocks)
    tx.send(TestMsg::Value(10)).unwrap();
    tx.send(TestMsg::Value(20)).unwrap();

    // Receive messages
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(10)));

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(20)));

    // Drop sender to close channel
    drop(tx);

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_unbounded_channel_subscription_cancellation() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::unbounded_channel();
    let sub = Box::new(UnboundedChannelSubscription::new(rx));

    let mut stream = sub.into_stream(cancel.clone());

    tx.send(TestMsg::Value(1)).unwrap();
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(1)));

    // Cancel the subscription
    cancel.cancel();

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);

    // Sender still alive but stream is done
    drop(tx);
}

#[tokio::test]
async fn test_stream_subscription() {
    let cancel = CancellationToken::new();
    let values = vec![TestMsg::Value(1), TestMsg::Value(2), TestMsg::Value(3)];
    let inner_stream = tokio_stream::iter(values);
    let sub = Box::new(StreamSubscription::new(inner_stream));

    let mut stream = sub.into_stream(cancel);

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(1)));

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(2)));

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(3)));

    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_mapped_subscription() {
    let cancel = CancellationToken::new();
    let inner = TickSubscription::new(Duration::from_millis(10), || 42i32);
    let sub = Box::new(inner.map(TestMsg::Value));

    let mut stream = sub.into_stream(cancel.clone());

    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(42)));

    cancel.cancel();
}

#[tokio::test]
async fn test_batch_subscription() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::channel(10);

    let timer = Box::new(TimerSubscription::after(
        Duration::from_millis(5),
        TestMsg::Timer,
    )) as BoxedSubscription<TestMsg>;
    let channel = Box::new(ChannelSubscription::new(rx)) as BoxedSubscription<TestMsg>;

    let sub = Box::new(batch(vec![timer, channel]));
    let mut stream = sub.into_stream(cancel.clone());

    // Send a channel message
    tx.send(TestMsg::Value(1)).await.unwrap();

    // Collect messages (order may vary)
    let mut received = Vec::new();
    for _ in 0..2 {
        if let Some(msg) = stream.next().await {
            received.push(msg);
        }
    }

    assert!(received.contains(&TestMsg::Timer));
    assert!(received.contains(&TestMsg::Value(1)));

    cancel.cancel();
}

#[test]
fn test_tick_builder_every() {
    let builder = TickSubscriptionBuilder::every(Duration::from_secs(1));
    let sub = builder.with_message(|| TestMsg::Tick);
    assert_eq!(sub.interval, Duration::from_secs(1));
}

#[test]
fn test_timer_after() {
    let timer = TimerSubscription::after(Duration::from_secs(5), TestMsg::Timer);
    assert_eq!(timer.delay, Duration::from_secs(5));
    assert_eq!(timer.message, TestMsg::Timer);
}

#[tokio::test]
async fn test_interval_immediate_subscription() {
    let cancel = CancellationToken::new();
    let sub = Box::new(IntervalImmediateSubscription::new(
        Duration::from_millis(100),
        || TestMsg::Tick,
    ));

    let mut stream = sub.into_stream(cancel.clone());

    // Should fire immediately without waiting for interval
    let start = std::time::Instant::now();
    let msg = stream.next().await;
    let elapsed = start.elapsed();

    assert_eq!(msg, Some(TestMsg::Tick));
    // First message should be immediate (less than the interval)
    assert!(
        elapsed < Duration::from_millis(50),
        "First message should be immediate, took {:?}",
        elapsed
    );

    cancel.cancel();
}

#[tokio::test]
async fn test_interval_immediate_builder() {
    let cancel = CancellationToken::new();
    let sub =
        Box::new(interval_immediate(Duration::from_millis(100)).with_message(|| TestMsg::Tick));

    let mut stream = sub.into_stream(cancel.clone());

    // Should fire immediately
    let start = std::time::Instant::now();
    let msg = stream.next().await;
    let elapsed = start.elapsed();

    assert_eq!(msg, Some(TestMsg::Tick));
    assert!(elapsed < Duration::from_millis(50));

    cancel.cancel();
}

#[tokio::test(start_paused = true)]
async fn test_interval_immediate_vs_tick() {
    // Both subscriptions produce their first message, but IntervalImmediate
    // yields before any async machinery while Tick goes through interval.tick().
    let cancel1 = CancellationToken::new();
    let cancel2 = CancellationToken::new();

    let immediate = Box::new(IntervalImmediateSubscription::new(
        Duration::from_millis(50),
        || TestMsg::Tick,
    ));
    let regular = Box::new(TickSubscription::new(Duration::from_millis(50), || {
        TestMsg::Tick
    }));

    let mut immediate_stream = immediate.into_stream(cancel1.clone());
    let mut regular_stream = regular.into_stream(cancel2.clone());

    // Both produce their first tick
    let immediate_first = immediate_stream.next().await;
    assert!(immediate_first.is_some());

    let regular_first = regular_stream.next().await;
    assert!(regular_first.is_some());

    // After the first tick, both require waiting for the interval
    // Advance time by the interval duration to get the second tick
    tokio::time::advance(Duration::from_millis(50)).await;

    let immediate_second = immediate_stream.next().await;
    assert!(immediate_second.is_some());

    let regular_second = regular_stream.next().await;
    assert!(regular_second.is_some());

    cancel1.cancel();
    cancel2.cancel();
}

#[tokio::test]
async fn test_empty_batch_subscription() {
    let cancel = CancellationToken::new();
    let sub = Box::new(batch::<TestMsg>(vec![]));

    let mut stream = sub.into_stream(cancel);

    // Empty batch should end immediately
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_channel_subscription_cancellation() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::channel(10);
    let sub = Box::new(ChannelSubscription::new(rx));

    let mut stream = sub.into_stream(cancel.clone());

    // Send a message
    tx.send(TestMsg::Value(1)).await.unwrap();
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(1)));

    // Cancel the subscription
    cancel.cancel();

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_stream_subscription_cancellation() {
    let cancel = CancellationToken::new();
    let (tx, rx) = mpsc::channel(10);
    let receiver_stream = tokio_stream::wrappers::ReceiverStream::new(rx);
    let sub = Box::new(StreamSubscription::new(receiver_stream));

    let mut stream = sub.into_stream(cancel.clone());

    // Send a message
    tx.send(TestMsg::Value(1)).await.unwrap();
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Value(1)));

    // Cancel the subscription
    cancel.cancel();

    // Stream should end
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[tokio::test]
async fn test_interval_immediate_cancellation() {
    let cancel = CancellationToken::new();
    let sub = Box::new(IntervalImmediateSubscription::new(
        Duration::from_millis(10),
        || TestMsg::Tick,
    ));

    let mut stream = sub.into_stream(cancel.clone());

    // Get the immediate first message
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Tick));

    // Get the second message after interval
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Tick));

    // Cancel the subscription
    cancel.cancel();

    // Yield to let cancellation propagate
    tokio::task::yield_now().await;

    // Stream should eventually end (might get one more buffered message on some platforms)
    let mut ended = false;
    for _ in 0..3 {
        let msg = stream.next().await;
        if msg.is_none() {
            ended = true;
            break;
        }
    }
    assert!(ended, "Stream should have ended after cancellation");
}

#[tokio::test]
async fn test_mapped_subscription_empty_stream() {
    let cancel = CancellationToken::new();
    let values: Vec<i32> = vec![];
    let inner = StreamSubscription::new(tokio_stream::iter(values));
    let sub = Box::new(MappedSubscription::new(inner, TestMsg::Value));

    let mut stream = sub.into_stream(cancel);

    // Mapped empty stream should end immediately
    let msg = stream.next().await;
    assert_eq!(msg, None);
}

#[test]
fn test_mapped_subscription_new() {
    let values = vec![42i32];
    let inner = StreamSubscription::new(tokio_stream::iter(values));
    let _sub = MappedSubscription::new(inner, TestMsg::Value);
    // Construction test - subscription created successfully
}

#[test]
fn test_batch_subscription_new() {
    let subs: Vec<BoxedSubscription<TestMsg>> = vec![];
    let sub = BatchSubscription::new(subs);
    assert!(sub.subscriptions.is_empty());
}

#[test]
fn test_interval_immediate_builder_every() {
    let builder = IntervalImmediateBuilder::every(Duration::from_secs(2));
    let sub = builder.with_message(|| TestMsg::Tick);
    assert_eq!(sub.interval, Duration::from_secs(2));
}

#[tokio::test]
async fn test_tick_cancellation() {
    let cancel = CancellationToken::new();
    let sub = Box::new(TickSubscription::new(Duration::from_millis(10), || {
        TestMsg::Tick
    }));

    let mut stream = sub.into_stream(cancel.clone());

    // Get first tick
    let msg = stream.next().await;
    assert_eq!(msg, Some(TestMsg::Tick));

    // Cancel before next tick
    cancel.cancel();

    // Yield to let cancellation propagate
    tokio::task::yield_now().await;

    // Stream should eventually end (might get one more buffered message on some platforms)
    let mut ended = false;
    for _ in 0..3 {
        let msg = stream.next().await;
        if msg.is_none() {
            ended = true;
            break;
        }
    }
    assert!(ended, "Stream should have ended after cancellation");
}