vtcode-ui 0.143.4

Unified UI crate for VT Code: design system, theme registry, and TUI framework
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
#![allow(
    missing_docs,
    reason = "Intentional compatibility, platform, or test-only suppression."
)]
use super::super::*;
use super::helpers::*;
use unicode_width::UnicodeWidthStr;

#[test]
fn input_compact_preview_for_image_path() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    let image_path = "/tmp/Screenshot 2026-02-06 at 3.39.48 PM.png";

    session.insert_paste_text(image_path);

    let data = session.build_input_widget_data(VIEW_WIDTH, VIEW_ROWS);
    let rendered = text_content(&data.text);
    assert!(rendered.contains("[Image:"));
    assert!(rendered.contains("Screenshot 2026-02-06"));
}

#[test]
fn input_compact_preview_for_quoted_image_path() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    let image_path = "\"/tmp/Screenshot 2026-02-06 at 3.39.48 PM.png\"";

    session.insert_paste_text(image_path);

    let data = session.build_input_widget_data(VIEW_WIDTH, VIEW_ROWS);
    let rendered = text_content(&data.text);
    assert!(rendered.contains("[Image:"));
    assert!(rendered.contains("Screenshot 2026-02-06"));
}

#[test]
fn control_g_launches_editor() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);

    let event = KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL);
    let result = session.process_key(event);

    assert!(matches!(result, Some(InlineEvent::LaunchEditor { draft }) if draft.is_empty()));
}

#[test]
fn control_g_launches_editor_with_draft() {
    let text = "hello world";
    let mut session = session_with_input(text, 0);

    let result = session.process_key(KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL));

    assert!(matches!(result, Some(InlineEvent::LaunchEditor { draft }) if draft == text));
}

#[test]
fn control_g_launches_editor_from_plan_confirmation_modal() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.input_status_right = Some("model | 25% context".to_string());
    let plan = app_types::PlanContent::from_markdown(
        "Test Plan".to_string(),
        "## Plan of Work\n- Step 1",
        Some(".vtcode/plans/test-plan.md".to_string()),
    );
    show_plan_confirmation_overlay(&mut session, plan);

    let event = KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL);
    let result = session.process_key(event);

    assert!(matches!(
        result,
        Some(InlineEvent::Overlay(OverlayEvent::Submitted(OverlaySubmission::Hotkey(
            OverlayHotkeyAction::LaunchEditor
        ))))
    ));
    assert!(session.modal_state().is_none());
}

#[test]
fn plan_confirmation_modal_matches_four_way_gate_copy() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    let plan = app_types::PlanContent::from_markdown(
        "Test Plan".to_string(),
        "## Implementation Plan\n1. Step",
        Some(".vtcode/plans/test-plan.md".to_string()),
    );
    show_plan_confirmation_overlay(&mut session, plan);

    let modal = session.modal_state().expect("plan confirmation modal should be present");
    assert_eq!(modal.title, "Ready to code?");
    assert_eq!(
        modal.lines.first().map(String::as_str),
        Some("A plan is ready to execute. Would you like to proceed?")
    );

    let list = modal.list.as_ref().expect("plan confirmation should include list options");
    assert_eq!(list.items.len(), 3);

    assert_eq!(list.items[0].title, "Yes, auto-accept edits");
    assert_eq!(list.items[0].subtitle.as_deref(), Some("Execute with auto-approval."));
    assert_eq!(list.items[0].badge.as_deref(), Some("Recommended"));

    assert_eq!(list.items[1].title, "Yes, manually approve edits");
    assert_eq!(list.items[1].subtitle.as_deref(), Some("Keep context and confirm each edit before applying."));

    assert_eq!(list.items[2].title, "Type feedback to revise the plan");
    assert_eq!(list.items[2].subtitle.as_deref(), Some("Return to planning workflow and refine the plan."));
}

#[test]
fn arrow_keys_never_launch_editor() {
    let text = "hello world";
    let mut session = session_with_input(text, 0);

    // Test Right arrow with all possible modifier combinations
    for modifiers in [
        KeyModifiers::empty(),
        KeyModifiers::CONTROL,
        KeyModifiers::SHIFT,
        KeyModifiers::ALT,
        KeyModifiers::SUPER,
        KeyModifiers::META,
        KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        KeyModifiers::CONTROL | KeyModifiers::SUPER,
    ] {
        let event = KeyEvent::new(KeyCode::Right, modifiers);
        let result = session.process_key(event);
        assert!(
            !matches!(result, Some(InlineEvent::LaunchEditor { .. })),
            "Right arrow with modifiers {modifiers:?} should not launch editor"
        );
    }

    // Test other arrow keys for safety
    for key_code in [KeyCode::Left, KeyCode::Up, KeyCode::Down] {
        let event = KeyEvent::new(key_code, KeyModifiers::SUPER);
        let result = session.process_key(event);
        assert!(
            !matches!(result, Some(InlineEvent::LaunchEditor { .. })),
            "{key_code:?} with SUPER should not launch editor"
        );
    }
}

#[test]
fn timeline_hidden_keeps_navigation_unselected() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(InlineMessageKind::Agent, vec![make_segment("Response")]);

    let backend = TestBackend::new(VIEW_WIDTH, VIEW_ROWS);
    let mut terminal = Terminal::new(backend).expect("failed to create test terminal");
    terminal
        .draw(|frame| session.render(frame))
        .expect("failed to render session with hidden timeline");

    assert!(session.navigation_state.selected().is_none());
}

#[test]
fn info_warning_error_blocks_render_distinct_fieldset_fills() {
    // (kind, label, unicode fill, ascii-fallback fill)
    let cases = [
        (InlineMessageKind::Info, " Info ", '', '-'),
        (InlineMessageKind::Warning, " Warning ", '', '='),
        (InlineMessageKind::Error, " Error ", '/', '/'),
    ];

    for (kind, label, unicode_fill, ascii_fill) in cases {
        let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
        session.push_line(kind, vec![make_segment("Theme switched to Ciapre")]);

        let rendered = rendered_transcript_widget_lines(&mut session, VIEW_WIDTH, VIEW_ROWS);

        // Top rule carries a center-aligned label flanked by the kind's fill.
        // The fill glyph follows terminal Unicode capabilities.
        assert!(
            rendered
                .iter()
                .any(|line| { (line.contains(unicode_fill) || line.contains(ascii_fill)) && line.contains(label) }),
            "{label} block should render a fieldset rule with its fill, got: {rendered:?}"
        );
        // Fieldset blocks never draw vertical box sides.
        assert!(
            rendered.iter().all(|line| !line.contains('')),
            "{label} fieldset must not draw vertical sides, got: {rendered:?}"
        );
        // The content itself is preserved.
        assert!(
            rendered.iter().any(|line| line.contains("Theme switched to Ciapre")),
            "{label} block content should be preserved, got: {rendered:?}"
        );
    }
}

#[test]
fn active_file_operation_indicator_renders_spinner_frame() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(InlineMessageKind::Info, vec![make_segment("❋ Editing vtcode.toml...")]);
    session.handle_command(InlineCommand::SetInputStatus {
        left: Some("Running tool: edit_file".to_string()),
        right: None,
    });
    let rendered = rendered_transcript_widget_lines(&mut session, VIEW_WIDTH, VIEW_ROWS);
    let expected = format!("{} Editing vtcode.toml...", pulse_spinner_frame_for_phase(0.0));

    assert!(
        rendered.iter().any(|line| line.contains(&expected)),
        "active file operation indicator should show a spinner frame"
    );
    assert!(
        !rendered.iter().any(|line| line.contains("❋ Editing vtcode.toml...")),
        "spinner should replace the static file operation marker while active"
    );
}

#[test]
fn non_file_tool_status_keeps_static_file_operation_indicator() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(InlineMessageKind::Info, vec![make_segment("❋ Editing vtcode.toml...")]);
    session.handle_command(InlineCommand::SetInputStatus {
        left: Some("Running tool: code_search".to_string()),
        right: None,
    });

    let rendered = rendered_transcript_widget_lines(&mut session, VIEW_WIDTH, VIEW_ROWS);

    assert!(
        rendered.iter().any(|line| line.contains("❋ Editing vtcode.toml...")),
        "non-file tool activity should not animate stale file operation indicators"
    );
}

#[test]
fn empty_enter_with_active_pty_opens_jobs() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.active_pty_sessions = Some(Arc::new(AtomicUsize::new(1)));

    let event = session.process_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));

    assert!(matches!(event, Some(InlineEvent::Submit(ref value)) if value == "/jobs"));
}

#[test]
fn task_panel_visibility_is_independent_from_logs() {
    let mut session = AppSession::new(InlineTheme::default(), None, VIEW_ROWS);
    session.set_task_panel_visible(true);
    let initial_task_panel = session.show_task_panel;
    let initial_logs = session.core.show_logs;

    session.core.toggle_logs();

    assert_eq!(session.show_task_panel, initial_task_panel);
    assert_ne!(session.core.show_logs, initial_logs);
}

#[test]
fn stage_states_keep_input_enabled() {
    for state in [ActivityState::Planning, ActivityState::Building] {
        let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
        session.handle_command(InlineCommand::SetActivityState(state));

        assert!(session.input_enabled(), "{state:?} should keep input enabled");
        assert_eq!(session.status_left_text(), state.status());
        assert!(!session.is_running_activity(), "{state:?} with no tool should not spin");
    }
}

#[test]
fn stage_state_with_running_tool_shows_tool_status() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.handle_command(InlineCommand::SetActivityState(ActivityState::Planning));
    session.handle_command(InlineCommand::SetInputStatus {
        left: Some("Running tool: edit_file".to_string()),
        right: None,
    });

    // The live tool status must replace the stage label so the footer
    // reflects what the agent is actually doing, not a frozen "Planning...".
    assert_eq!(session.status_left_text(), Some("Running tool: edit_file"));
    assert!(session.has_status_spinner(), "stage label must animate while a tool runs");
    assert!(session.is_running_activity());
    assert!(session.input_enabled(), "stage states keep input usable mid-tool");
}

#[test]
fn stage_state_idle_shows_stage_label() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.handle_command(InlineCommand::SetActivityState(ActivityState::Planning));
    // No active tool — the status refresh may set a git-branch left text that
    // does not indicate active work. The stage label must still be shown.
    session.handle_command(InlineCommand::SetInputStatus { left: Some("main*".to_string()), right: None });

    assert_eq!(session.status_left_text(), Some("Planning..."));
    assert!(!session.has_status_spinner(), "idle stage must not spin");
    assert!(!session.is_running_activity());
}

#[test]
fn busy_state_disables_input_and_shows_busy_status() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.handle_command(InlineCommand::SetActivityState(ActivityState::StartingBuild));

    assert!(!session.input_enabled(), "busy states must block input");
    assert!(session.is_running_activity());
    assert!(session.has_status_spinner());
}

#[test]
fn timeline_visible_selects_latest_item() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(InlineMessageKind::Agent, vec![make_segment("First")]);
    session.push_line(InlineMessageKind::Agent, vec![make_segment("Second")]);

    let backend = TestBackend::new(VIEW_WIDTH, VIEW_ROWS);
    let mut terminal = Terminal::new(backend).expect("failed to create test terminal");
    terminal
        .draw(|frame| session.render(frame))
        .expect("failed to render session with timeline");

    assert!(session.navigation_state.selected().is_none());
}

#[test]
fn tool_detail_renders_with_border_and_body_style() {
    let theme = themed_inline_colors();
    let mut session = Session::new(theme, None, VIEW_ROWS);
    let detail_style = InlineTextStyle::default().italic();
    session.push_line(
        InlineMessageKind::Tool,
        vec![InlineSegment {
            text: "    result line".to_string(),
            style: Arc::new(detail_style),
        }],
    );

    let index = session.lines.len().checked_sub(1).expect("tool detail line should exist");
    let spans = session.render_message_spans(index);

    assert_eq!(spans.len(), 1);
    let body_span = &spans[0];
    assert!(body_span.style.add_modifier.contains(Modifier::ITALIC));
    assert_eq!(body_span.content.clone().into_owned(), "    result line");
}

#[test]
fn top_level_task_tree_tail_line_is_dimmed_in_tool_blocks() {
    let theme = themed_inline_colors();
    let mut session = Session::new(theme, None, VIEW_ROWS);
    session.push_line(
        InlineMessageKind::Tool,
        vec![InlineSegment {
            text: "└ Report actions taken, blockers, and required user input".to_string(),
            style: Arc::new(InlineTextStyle::default()),
        }],
    );

    let index = session.lines.len().checked_sub(1).expect("tool detail line should exist");
    let transcript_lines = session.reflow_message_lines(index, 100, false);
    let task_span = transcript_lines
        .iter()
        .flat_map(|line| line.line.spans.iter())
        .find(|span| span.content.contains("Report actions taken"))
        .expect("expected task span");

    assert!(task_span.style.add_modifier.contains(Modifier::DIM), "top-level task rows should render dimmed");
}

#[test]
fn tool_block_with_cjk_prefix_does_not_overflow_viewport() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(
        InlineMessageKind::Tool,
        vec![InlineSegment {
            text: "日本語のツール詳細情報".to_string(),
            style: Arc::new(InlineTextStyle::default()),
        }],
    );
    let index = session.lines.len() - 1;
    let transcript = session.reflow_message_lines(index, VIEW_WIDTH, false);
    for line in &transcript {
        let width: usize = line.line.spans.iter().map(|span| span.content.width()).sum();
        assert!(
            width <= VIEW_WIDTH as usize,
            "tool block line with CJK prefix overflowed viewport: {width} > {VIEW_WIDTH}"
        );
    }
}

#[test]
fn error_block_with_cjk_label_reserves_correct_content_width() {
    let mut session = Session::new(InlineTheme::default(), None, VIEW_ROWS);
    session.push_line(
        InlineMessageKind::Error,
        vec![InlineSegment {
            text: "エラーが発生しました".to_string(),
            style: Arc::new(InlineTextStyle::default()),
        }],
    );
    let index = session.lines.len() - 1;
    let transcript = session.reflow_message_lines(index, VIEW_WIDTH, false);
    for line in &transcript {
        let width: usize = line.line.spans.iter().map(|span| span.content.width()).sum();
        assert!(
            width <= VIEW_WIDTH as usize,
            "error block line with CJK label overflowed viewport: {width} > {VIEW_WIDTH}"
        );
    }
}