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
use super::*;
use crate::component::test_utils;

fn focused_state() -> ConversationViewState {
    ConversationViewState::new()
}

fn state_with_messages() -> ConversationViewState {
    let mut state = focused_state();
    state.push_system("Welcome to the conversation.");
    state.push_user("Hello, can you help me?");
    state.push_assistant("Of course! What do you need?");
    state
}

// =============================================================================
// Rendering
// =============================================================================

#[test]
fn test_render_empty() {
    let state = ConversationViewState::new();
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_with_messages() {
    let state = state_with_messages();
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_focused() {
    let state = focused_state();
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_disabled() {
    let state = ConversationViewState::new();
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(
                &state,
                &mut RenderContext::new(frame, frame.area(), &theme).disabled(true),
            );
        })
        .unwrap();
}

#[test]
fn test_render_with_title() {
    let state = ConversationViewState::new().with_title("Session 1");
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_with_timestamps() {
    let mut state = ConversationViewState::new().with_show_timestamps(true);
    state.push_message(
        ConversationMessage::new(ConversationRole::User, "Hello").with_timestamp("14:30"),
    );
    state.push_message(
        ConversationMessage::new(ConversationRole::Assistant, "Hi!").with_timestamp("14:31"),
    );
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_without_role_labels() {
    let mut state = ConversationViewState::new().with_show_role_labels(false);
    state.push_user("Hello");
    state.push_assistant("Hi!");
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_code_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![
            MessageBlock::text("Here is the code:"),
            MessageBlock::code("fn main() {\n    println!(\"hello\");\n}", Some("rust")),
        ],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_tool_use_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![
            MessageBlock::text("I'll search for that."),
            MessageBlock::tool_use("web_search").with_input("query: rust TUI frameworks"),
        ],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_thinking_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![
            MessageBlock::thinking("Let me reason through this problem..."),
            MessageBlock::text("The answer is 42."),
        ],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_error_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Tool,
        vec![MessageBlock::error("Connection timeout")],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_streaming_message() {
    let mut state = ConversationViewState::new();
    state.push_message(
        ConversationMessage::new(ConversationRole::Assistant, "Generating...").with_streaming(true),
    );
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_collapsed_thinking() {
    let mut state = ConversationViewState::new();
    state.collapse("thinking");
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![
            MessageBlock::thinking("Hidden reasoning"),
            MessageBlock::text("Visible answer"),
        ],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_collapsed_tool_use() {
    let mut state = ConversationViewState::new();
    state.collapse("tool:search");
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![MessageBlock::tool_use("search").with_input("query: test")],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_small_area() {
    let state = state_with_messages();
    let (mut terminal, theme) = test_utils::setup_render(60, 4);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_tiny_area_no_panic() {
    let state = state_with_messages();
    let (mut terminal, theme) = test_utils::setup_render(4, 2);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_mixed_blocks() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![
            MessageBlock::thinking("Analyzing the problem..."),
            MessageBlock::text("I found the answer."),
            MessageBlock::code("x = 42", Some("python")),
            MessageBlock::tool_use("calculator").with_input("42 * 2"),
            MessageBlock::error("Rate limit exceeded"),
        ],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 30);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

// =============================================================================
// Empty code/tool blocks
// =============================================================================

#[test]
fn test_render_empty_code_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![MessageBlock::code("", None)],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_empty_tool_input() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::Assistant,
        vec![MessageBlock::tool_use("noop")],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

#[test]
fn test_render_empty_text_block() {
    let mut state = ConversationViewState::new();
    state.push_message(ConversationMessage::with_blocks(
        ConversationRole::User,
        vec![MessageBlock::text("")],
    ));
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    terminal
        .draw(|frame| {
            ConversationView::view(&state, &mut RenderContext::new(frame, frame.area(), &theme));
        })
        .unwrap();
}

// =============================================================================
// Annotation
// =============================================================================

#[test]
fn test_annotation_emitted() {
    use crate::annotation::with_annotations;
    let state = ConversationViewState::new();
    let (mut terminal, theme) = test_utils::setup_render(60, 20);
    let registry = with_annotations(|| {
        terminal
            .draw(|frame| {
                ConversationView::view(
                    &state,
                    &mut RenderContext::new(frame, frame.area(), &theme),
                );
            })
            .unwrap();
    });
    assert!(registry.get_by_id("conversation_view").is_some());
}

#[cfg(feature = "markdown")]
#[test]
fn test_role_style_override_in_rendering() {
    use ratatui::style::{Color, Modifier, Style};

    let mut state = ConversationViewState::new().with_markdown(true);
    state.set_role_style(ConversationRole::User, Style::default().fg(Color::Cyan));
    state.push_user("Hello from user");
    state.push_assistant("Hello from assistant");

    let theme = crate::theme::Theme::default();
    let lines = super::render::build_display_lines(state.source_messages(), &state, 80, &theme);

    // Find user body spans — should use overridden Cyan color
    let mut found_user_cyan = false;
    let mut in_user_section = false;
    for line in &lines {
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        if text.contains("User")
            && line
                .spans
                .iter()
                .any(|s| s.style.add_modifier.contains(Modifier::BOLD))
        {
            in_user_section = true;
            continue;
        }
        if text.contains("Assistant")
            && line
                .spans
                .iter()
                .any(|s| s.style.add_modifier.contains(Modifier::BOLD))
        {
            in_user_section = false;
            continue;
        }
        if in_user_section {
            for span in &line.spans {
                if span.content.contains("Hello") && span.style.fg == Some(Color::Cyan) {
                    found_user_cyan = true;
                }
            }
        }
    }
    assert!(
        found_user_cyan,
        "User message body should use the overridden Cyan color"
    );

    // Assistant should still use default Blue (no override set)
    let mut found_assistant_blue = false;
    let mut in_assistant_section = false;
    for line in &lines {
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        if text.contains("Assistant")
            && line
                .spans
                .iter()
                .any(|s| s.style.add_modifier.contains(Modifier::BOLD))
        {
            in_assistant_section = true;
            continue;
        }
        if in_assistant_section {
            for span in &line.spans {
                if span.content.contains("Hello") && span.style.fg == Some(Color::Blue) {
                    found_assistant_blue = true;
                }
            }
        }
    }
    assert!(
        found_assistant_blue,
        "Assistant message body should use default Blue (no override set)"
    );
}

#[cfg(feature = "markdown")]
#[test]
fn test_markdown_role_style_propagation() {
    use ratatui::style::{Color, Modifier, Style};

    let mut state = ConversationViewState::new().with_markdown(true);
    state.push_user("plain text and **bold** and `inline code`");
    state.push_assistant("plain text and **bold** and `inline code`");

    let theme = crate::theme::Theme::default();
    let lines = super::render::build_display_lines(state.source_messages(), &state, 80, &theme);

    // Partition lines into user-section and assistant-section.
    // The header line for each message contains the role label.
    let mut user_lines: Vec<&Line> = Vec::new();
    let mut assistant_lines: Vec<&Line> = Vec::new();
    let mut current_section: Option<&str> = None;

    for line in &lines {
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        if text.contains("User")
            && line
                .spans
                .iter()
                .any(|s| s.style.add_modifier.contains(Modifier::BOLD))
        {
            current_section = Some("user");
            continue;
        }
        if text.contains("Assistant")
            && line
                .spans
                .iter()
                .any(|s| s.style.add_modifier.contains(Modifier::BOLD))
        {
            current_section = Some("assistant");
            continue;
        }
        match current_section {
            Some("user") => user_lines.push(line),
            Some("assistant") => assistant_lines.push(line),
            _ => {}
        }
    }

    assert!(
        !user_lines.is_empty(),
        "should have user message body lines"
    );
    assert!(
        !assistant_lines.is_empty(),
        "should have assistant message body lines"
    );

    // Helper: find a span containing `needle` across a set of lines.
    let find_span = |lines: &[&Line], needle: &str| -> Option<Style> {
        for line in lines {
            for span in &line.spans {
                if span.content.contains(needle) {
                    return Some(span.style);
                }
            }
        }
        None
    };

    // -- User assertions (role color: Green) --
    let user_plain =
        find_span(&user_lines, "plain").expect("user section should contain a span with 'plain'");
    assert_eq!(
        user_plain.fg,
        Some(Color::Green),
        "user plain-text span should have fg=Green (role color), got {:?}",
        user_plain.fg,
    );

    let user_bold =
        find_span(&user_lines, "bold").expect("user section should contain a span with 'bold'");
    assert!(
        user_bold.add_modifier.contains(Modifier::BOLD),
        "user bold span should retain BOLD modifier from markdown",
    );
    assert_eq!(
        user_bold.fg,
        Some(Color::Green),
        "user bold span should have fg=Green (role color fills in unset fg)",
    );

    let user_code = find_span(&user_lines, "inline code")
        .expect("user section should contain a span with 'inline code'");
    assert_ne!(
        user_code.fg,
        Some(Color::Green),
        "user inline-code span should NOT have role color — markdown's code styling wins",
    );
    assert_eq!(
        user_code.fg,
        Some(Color::Yellow),
        "user inline-code span should retain markdown's Yellow code color",
    );

    // -- Assistant assertions (role color: Blue) --
    let asst_plain = find_span(&assistant_lines, "plain")
        .expect("assistant section should contain a span with 'plain'");
    assert_eq!(
        asst_plain.fg,
        Some(Color::Blue),
        "assistant plain-text span should have fg=Blue (role color), got {:?}",
        asst_plain.fg,
    );

    let asst_bold = find_span(&assistant_lines, "bold")
        .expect("assistant section should contain a span with 'bold'");
    assert!(
        asst_bold.add_modifier.contains(Modifier::BOLD),
        "assistant bold span should retain BOLD modifier from markdown",
    );
    assert_eq!(
        asst_bold.fg,
        Some(Color::Blue),
        "assistant bold span should have fg=Blue (role color fills in unset fg)",
    );

    let asst_code = find_span(&assistant_lines, "inline code")
        .expect("assistant section should contain a span with 'inline code'");
    assert_ne!(
        asst_code.fg,
        Some(Color::Blue),
        "assistant inline-code span should NOT have role color — markdown's code styling wins",
    );

    // -- Cross-role differentiation (the original complaint) --
    assert_ne!(
        user_plain.fg, asst_plain.fg,
        "user and assistant plain-text spans must have DIFFERENT fg colors",
    );
}