opendev-tui 0.1.4

Ratatui-based terminal UI for OpenDev
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
use super::*;
use crate::app::DisplayMessage;

#[test]
fn test_empty_conversation() {
    let msgs: Vec<DisplayMessage> = vec![];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    // Welcome panel is now a separate widget; empty conversation returns no lines
    assert!(lines.is_empty());
}

#[test]
fn test_user_message_rendering() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Hello")];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    assert!(lines.len() >= 2); // message + blank line
}

#[test]
fn test_tool_call_display() {
    let msgs = vec![DisplayMessage {
        role: DisplayRole::Assistant,
        content: "Running tool...".into(),
        tool_call: Some(DisplayToolCall {
            name: "bash".into(),
            arguments: std::collections::HashMap::new(),
            summary: Some("ls -la".into()),
            success: true,
            collapsed: false,
            result_lines: vec!["file1.rs".into(), "file2.rs".into()],
            nested_calls: vec![],
        }),
        collapsed: false,
        thinking_started_at: None,
        thinking_duration_secs: None,
    }];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    // message line + tool line + 2 result lines + blank
    assert!(lines.len() >= 5);
}

#[test]
fn test_system_reminder_filtered() {
    let msgs = vec![DisplayMessage::new(
        DisplayRole::Assistant,
        "Hello<system-reminder>secret</system-reminder> world",
    )];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    // Should not contain "secret"
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    assert!(!text.contains("secret"));
    assert!(text.contains("Hello"));
    assert!(text.contains("world"));
}

#[test]
fn test_collapsed_tool_result() {
    let msgs = vec![DisplayMessage {
        role: DisplayRole::Assistant,
        content: "Done".into(),
        tool_call: Some(DisplayToolCall {
            name: "read_file".into(),
            arguments: std::collections::HashMap::new(),
            summary: Some("Read 100 lines".into()),
            success: true,
            collapsed: true,
            result_lines: vec!["line1".into(), "line2".into()],
            nested_calls: vec![],
        }),
        collapsed: false,
        thinking_started_at: None,
        thinking_duration_secs: None,
    }];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    // read_file shows "Read 2 lines" without parentheses or "Ctrl+O" hint
    assert!(text.contains("Read 2 lines"));
    assert!(!text.contains("Ctrl+O"));
    assert!(!text.contains("("));
}

#[test]
fn test_spinner_active_tools() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Do something")];
    let mut args = std::collections::HashMap::new();
    args.insert("command".into(), serde_json::Value::String("ls -la".into()));
    let tools = vec![ToolExecution {
        id: "t1".into(),
        name: "run_command".into(),
        output_lines: vec![],
        state: crate::app::ToolState::Running,
        elapsed_secs: 3,
        started_at: std::time::Instant::now(),
        tick_count: 0,
        parent_id: None,
        depth: 0,
        args,
    }];
    let widget = ConversationWidget::new(&msgs, 0).active_tools(&tools);
    let render_lines = widget.build_render_lines();
    let text: String = render_lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    // Should show display name like "Bash ls -la" not raw "run_command"
    assert!(text.contains("Bash"));
    assert!(text.contains("ls -la"));
    assert!(text.contains("3s"));
}

#[test]
fn test_spinner_thinking() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Hello")];
    let progress = TaskProgress {
        description: "Thinking".to_string(),
        elapsed_secs: 5,
        token_display: None,
        interrupted: false,
        started_at: std::time::Instant::now(),
    };
    let widget = ConversationWidget::new(&msgs, 0).task_progress(Some(&progress));
    let spinner = widget.build_spinner_lines();
    let text: String = spinner
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    assert!(text.contains("Thinking..."));
    assert!(text.contains("esc to interrupt"));
}

#[test]
fn test_spinner_tools_take_priority_over_thinking() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Hello")];
    let tools = vec![ToolExecution {
        id: "t1".into(),
        name: "read_file".into(),
        output_lines: vec![],
        state: crate::app::ToolState::Running,
        elapsed_secs: 1,
        started_at: std::time::Instant::now(),
        tick_count: 2,
        parent_id: None,
        depth: 0,
        args: Default::default(),
    }];
    let progress = TaskProgress {
        description: "Thinking".to_string(),
        elapsed_secs: 5,
        token_display: None,
        interrupted: false,
        started_at: std::time::Instant::now(),
    };
    let widget = ConversationWidget::new(&msgs, 0)
        .active_tools(&tools)
        .task_progress(Some(&progress));
    let spinner = widget.build_spinner_lines();
    let text: String = spinner
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    // Active tools shown with display name, not thinking
    assert!(text.contains("Read"));
    assert!(!text.contains("Thinking..."));
}

#[test]
fn test_no_spinner_when_idle() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Hello")];
    let widget = ConversationWidget::new(&msgs, 0);
    let spinner = widget.build_spinner_lines();
    assert!(spinner.is_empty());
    // Message lines: "> Hello" + blank separator
    let lines = widget.build_lines();
    assert_eq!(lines.len(), 2);
}

#[test]
fn test_nested_tool_calls() {
    let msgs = vec![DisplayMessage {
        role: DisplayRole::Assistant,
        content: "".into(),
        tool_call: Some(DisplayToolCall {
            name: "spawn_subagent".into(),
            arguments: std::collections::HashMap::new(),
            summary: Some("Exploring codebase".into()),
            success: true,
            collapsed: false,
            result_lines: vec![],
            nested_calls: vec![DisplayToolCall {
                name: "read_file".into(),
                arguments: std::collections::HashMap::new(),
                summary: Some("src/main.rs".into()),
                success: true,
                collapsed: false,
                result_lines: vec![],
                nested_calls: vec![],
            }],
        }),
        collapsed: false,
        thinking_started_at: None,
        thinking_duration_secs: None,
    }];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    // spawn_subagent renders as AgentName(task), nested calls show formatted verb
    assert!(text.contains("Agent"));
    assert!(text.contains("Read"));
}

#[test]
fn test_render_reserves_bottom_row_gap() {
    use ratatui::buffer::Buffer;

    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Hello")];
    let widget = ConversationWidget::new(&msgs, 0);

    // Render into a small area
    let area = Rect::new(0, 0, 40, 10);
    let mut buf = Buffer::empty(area);
    widget.render(area, &mut buf);

    // The last row (y=9) must be entirely blank — reserved gap
    for x in 0..40 {
        let cell = &buf[(x, 9)];
        assert_eq!(
            cell.symbol(),
            " ",
            "Bottom gap row should be blank at column {x}"
        );
    }
}

// ---------------------------------------------------------------
// TUI snapshot tests using TestBackend
// ---------------------------------------------------------------

/// Extract visible text from a ratatui Buffer, row by row.
fn buffer_text(buf: &ratatui::buffer::Buffer, area: Rect) -> Vec<String> {
    let mut rows = Vec::new();
    for y in area.y..area.bottom() {
        let mut row = String::new();
        for x in area.x..area.right() {
            row.push_str(buf[(x, y)].symbol());
        }
        rows.push(row.trim_end().to_string());
    }
    rows
}

#[test]
fn test_snapshot_empty_conversation() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();
    let msgs: Vec<DisplayMessage> = vec![];

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let rows = buffer_text(&buf, Rect::new(0, 0, 80, 24));
    // Empty conversation renders nothing — all rows should be blank
    for row in &rows {
        assert!(
            row.trim().is_empty(),
            "Expected blank row for empty conversation, got: {row:?}"
        );
    }
}

#[test]
fn test_snapshot_single_user_message() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "What is Rust?")];

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let rows = buffer_text(&buf, Rect::new(0, 0, 80, 24));
    // First row should contain the user prompt marker and message
    assert!(
        rows[0].contains(">") && rows[0].contains("What is Rust?"),
        "First row should show user message, got: {:?}",
        rows[0]
    );
}

#[test]
fn test_snapshot_multi_message_with_tool_call() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let msgs = vec![
        DisplayMessage::new(DisplayRole::User, "List files"),
        DisplayMessage {
            role: DisplayRole::Assistant,
            content: "I'll list the files.".into(),
            tool_call: Some(DisplayToolCall {
                name: "bash".into(),
                arguments: std::collections::HashMap::new(),
                summary: Some("ls -la".into()),
                success: true,
                collapsed: false,
                result_lines: vec!["main.rs".into(), "lib.rs".into()],
                nested_calls: vec![],
            }),
            collapsed: false,
            thinking_started_at: None,
            thinking_duration_secs: None,
        },
        DisplayMessage::new(DisplayRole::Assistant, "Here are the files."),
    ];

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let all_text: String = buffer_text(&buf, Rect::new(0, 0, 80, 24)).join("\n");

    // Verify key content appears in the rendered output
    assert!(all_text.contains("List files"), "Missing user message");
    assert!(
        all_text.contains("list the files"),
        "Missing assistant content"
    );
    assert!(
        all_text.contains("main.rs"),
        "Missing tool result line 'main.rs'"
    );
    assert!(
        all_text.contains("lib.rs"),
        "Missing tool result line 'lib.rs'"
    );
    assert!(
        all_text.contains("Here are the files"),
        "Missing second assistant message"
    );
}

#[test]
fn test_snapshot_thinking_block() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let msgs = vec![
        DisplayMessage::new(DisplayRole::User, "Explain closures"),
        DisplayMessage::new(
            DisplayRole::Assistant,
            "Closures capture variables from their scope.",
        ),
    ];

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let all_text: String = buffer_text(&buf, Rect::new(0, 0, 80, 24)).join("\n");

    assert!(
        all_text.contains("Explain closures"),
        "Missing user message"
    );
    assert!(
        all_text.contains("capture variables"),
        "Missing assistant response"
    );
}

#[test]
fn test_snapshot_scroll_indicator() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    // Create many messages to force scrolling in a small terminal
    let msgs: Vec<DisplayMessage> = (0..50)
        .map(|i| {
            DisplayMessage::new(
                if i % 2 == 0 {
                    DisplayRole::User
                } else {
                    DisplayRole::Assistant
                },
                format!("Message number {i} with enough text to occupy a line"),
            )
        })
        .collect();

    let backend = TestBackend::new(80, 10);
    let mut terminal = Terminal::new(backend).unwrap();

    // Render with scroll offset > 0
    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 5);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();

    // When scrolled, the rightmost column should contain scrollbar characters
    // (▲ at top, █ for thumb, ║ for track, ▼ at bottom)
    let last_col = 79u16;
    let right_col: String = (0..10u16)
        .map(|y| buf[(last_col, y)].symbol().to_string())
        .collect();
    let scrollbar_chars = ['', '', '', '', ''];
    assert!(
        right_col.chars().any(|c| scrollbar_chars.contains(&c)),
        "Expected scrollbar characters in rightmost column when scrolled, got: {right_col:?}"
    );
}

#[test]
fn test_diff_rendering_with_line_numbers() {
    let msgs = vec![DisplayMessage {
        role: DisplayRole::Assistant,
        content: "".into(),
        tool_call: Some(DisplayToolCall {
            name: "edit_file".into(),
            arguments: std::collections::HashMap::new(),
            summary: None,
            success: true,
            collapsed: false,
            result_lines: vec![
                "Edited file.rs: 1 replacement(s), 1 addition(s) and 1 removal(s)".into(),
                "--- a/file.rs".into(),
                "+++ b/file.rs".into(),
                "@@ -10,3 +10,3 @@".into(),
                " context".into(),
                "-old".into(),
                "+new".into(),
            ],
            nested_calls: vec![],
        }),
        collapsed: false,
        thinking_started_at: None,
        thinking_duration_secs: None,
    }];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();

    // Should contain right-aligned line numbers
    assert!(text.contains("  10 "), "Should contain line number 10");
    assert!(text.contains("  11 "), "Should contain line number 11");
    // Should contain operators
    assert!(text.contains("+ new"), "Should contain '+ new'");
    assert!(text.contains("- old"), "Should contain '- old'");
    // Should contain reformatted summary
    assert!(
        text.contains("Added 1 line, removed 1 line"),
        "Should contain reformatted summary, got: {text}"
    );
    // Should NOT contain raw diff markers
    assert!(!text.contains("@@"), "Should not contain @@ hunk headers");
    assert!(!text.contains("--- a/"), "Should not contain file headers");
}

#[test]
fn test_edit_tool_never_collapsed() {
    let msgs = vec![DisplayMessage {
        role: DisplayRole::Assistant,
        content: "".into(),
        tool_call: Some(DisplayToolCall {
            name: "edit_file".into(),
            arguments: std::collections::HashMap::new(),
            summary: None,
            success: true,
            collapsed: true, // Explicitly set collapsed
            result_lines: vec![
                "Edited file.rs: 1 replacement(s), 1 addition(s) and 0 removal(s)".into(),
                "--- a/file.rs".into(),
                "+++ b/file.rs".into(),
                "@@ -1,3 +1,4 @@".into(),
                " line1".into(),
                " line2".into(),
                "+new line".into(),
                " line3".into(),
            ],
            nested_calls: vec![],
        }),
        collapsed: false,
        thinking_started_at: None,
        thinking_duration_secs: None,
    }];
    let widget = ConversationWidget::new(&msgs, 0);
    let lines = widget.build_lines();
    let text: String = lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();

    // Even though collapsed=true, edit_file should always show expanded
    assert!(
        !text.contains("collapsed"),
        "edit_file should never show collapsed indicator"
    );
    assert!(
        text.contains("+ new line"),
        "edit_file should show diff content even when collapsed=true"
    );
}

/// Test that parallel subagents show "Done" when SubagentFinished arrives before ToolFinished.
/// This simulates the real event ordering: SubagentFinished#1,#2,#3 then ToolResult/ToolFinished.
#[test]
fn test_spinner_parallel_subagents_finished_before_tool() {
    use crate::widgets::nested_tool::SubagentDisplayState;

    let msgs = vec![DisplayMessage::new(
        DisplayRole::User,
        "Explore the codebase",
    )];

    // Create 3 spawn_subagent ToolExecutions (still active — not finished)
    let tasks = [
        "Search for authentication code",
        "Find database models",
        "Explore API endpoints",
    ];
    let tools: Vec<ToolExecution> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut args = std::collections::HashMap::new();
            args.insert("task".into(), serde_json::Value::String(task.to_string()));
            args.insert(
                "agent_type".into(),
                serde_json::Value::String("explore".into()),
            );
            ToolExecution {
                id: format!("t{i}"),
                name: "spawn_subagent".into(),
                output_lines: vec![],
                state: crate::app::ToolState::Running,
                elapsed_secs: 5,
                started_at: std::time::Instant::now(),
                tick_count: 0,
                parent_id: None,
                depth: 0,
                args,
            }
        })
        .collect();

    // All 3 subagents are finished (simulating SubagentFinished arriving first)
    let subagents: Vec<SubagentDisplayState> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut sa =
                SubagentDisplayState::new(format!("sa{i}"), "explore".into(), task.to_string());
            sa.parent_tool_id = Some(format!("t{i}"));
            sa.finished = true;
            sa.success = true;
            sa.tool_call_count = 3 + i;
            sa
        })
        .collect();

    let widget = ConversationWidget::new(&msgs, 0)
        .active_tools(&tools)
        .active_subagents(&subagents);
    let spinner = widget.build_spinner_lines();
    let text: String = spinner
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();

    // Each subagent should be rendered individually (no grouping)
    assert!(!text.contains("3 subagents"), "Should not group subagents");
    for task in &tasks {
        assert!(
            text.contains(task),
            "Expected task '{task}' in spinner output"
        );
    }
    // Each finished subagent should show Done individually
    assert!(text.contains("Done"));
}

/// Test that in-progress parallel subagents still show active tool calls (not "Done").
#[test]
fn test_spinner_parallel_subagents_in_progress() {
    use crate::widgets::nested_tool::{NestedToolCallState, SubagentDisplayState};

    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Explore")];

    let tasks = ["Search auth code", "Find models"];
    let tools: Vec<ToolExecution> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut args = std::collections::HashMap::new();
            args.insert("task".into(), serde_json::Value::String(task.to_string()));
            args.insert(
                "agent_type".into(),
                serde_json::Value::String("explore".into()),
            );
            ToolExecution {
                id: format!("t{i}"),
                name: "spawn_subagent".into(),
                output_lines: vec![],
                state: crate::app::ToolState::Running,
                elapsed_secs: 2,
                started_at: std::time::Instant::now(),
                tick_count: 0,
                parent_id: None,
                depth: 0,
                args,
            }
        })
        .collect();

    // Subagents are NOT finished — still running with active tools
    let subagents: Vec<SubagentDisplayState> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut sa =
                SubagentDisplayState::new(format!("sa{i}"), "explore".into(), task.to_string());
            sa.parent_tool_id = Some(format!("t{i}"));
            sa.active_tools.insert(
                format!("nested_t{i}"),
                NestedToolCallState {
                    tool_name: "read_file".into(),
                    tool_id: format!("nested_t{i}"),
                    args: Default::default(),
                    started_at: std::time::Instant::now(),
                    tick: 0,
                },
            );
            sa
        })
        .collect();

    let widget = ConversationWidget::new(&msgs, 0)
        .active_tools(&tools)
        .active_subagents(&subagents);
    let spinner = widget.build_spinner_lines();
    let text: String = spinner
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();

    // Should NOT show "Done" — subagents are still running
    assert!(
        !text.contains("Done"),
        "Should not show 'Done' for in-progress subagents"
    );
    // Each subagent rendered individually with its active tool
    assert!(
        text.contains("Read"),
        "active tool 'Read' should appear in individual spinner lines"
    );
    // No grouping
    assert!(!text.contains("2 subagents"), "Should not group subagents");
}

#[test]
fn test_render_lines_include_spinner_content() {
    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Do something")];
    let mut args = std::collections::HashMap::new();
    args.insert("command".into(), serde_json::Value::String("ls -la".into()));
    let tools = vec![ToolExecution {
        id: "t1".into(),
        name: "run_command".into(),
        output_lines: vec![],
        state: crate::app::ToolState::Running,
        elapsed_secs: 3,
        started_at: std::time::Instant::now(),
        tick_count: 0,
        parent_id: None,
        depth: 0,
        args,
    }];
    let widget = ConversationWidget::new(&msgs, 0).active_tools(&tools);
    let text: String = widget
        .build_render_lines()
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.to_string())
        .collect();
    assert!(text.contains("> Do something") || text.contains("Do something"));
    assert!(text.contains("Bash"));
    assert!(text.contains("3s"));
}

#[test]
fn test_snapshot_parallel_subagents_group_visible_in_tui() {
    use crate::widgets::nested_tool::SubagentDisplayState;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(60, 8);
    let mut terminal = Terminal::new(backend).unwrap();

    let msgs = vec![DisplayMessage::new(DisplayRole::User, "Explore the repo")];

    let tasks = [
        "Search auth code",
        "Find database models",
        "Explore API routes",
        "Trace background jobs",
    ];

    let tools: Vec<ToolExecution> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut args = std::collections::HashMap::new();
            args.insert("task".into(), serde_json::Value::String(task.to_string()));
            args.insert(
                "description".into(),
                serde_json::Value::String(task.to_string()),
            );
            ToolExecution {
                id: format!("t{i}"),
                name: "spawn_subagent".into(),
                output_lines: vec![],
                state: crate::app::ToolState::Running,
                elapsed_secs: 2,
                started_at: std::time::Instant::now(),
                tick_count: 0,
                parent_id: None,
                depth: 0,
                args,
            }
        })
        .collect();

    let subagents: Vec<SubagentDisplayState> = tasks
        .iter()
        .enumerate()
        .map(|(i, task)| {
            let mut sa =
                SubagentDisplayState::new(format!("sa{i}"), "explore".into(), task.to_string());
            sa.parent_tool_id = Some(format!("t{i}"));
            sa
        })
        .collect();

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0)
                .active_tools(&tools)
                .active_subagents(&subagents);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let all_text = buffer_text(&buf, Rect::new(0, 0, 60, 8)).join("\n");

    // Each subagent rendered individually (no grouping)
    assert!(
        !all_text.contains("4 subagents"),
        "Should not group subagents: {all_text}"
    );
    // At least some individual agents visible in the 8-row viewport
    let visible = tasks.iter().filter(|t| all_text.contains(*t)).count();
    assert!(
        visible >= 1,
        "At least one subagent should be visible: {all_text}"
    );
}

#[test]
fn test_reasoning_message_visible() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();

    let msgs = vec![
        DisplayMessage {
            role: DisplayRole::Reasoning,
            content:
                "Let me think step by step.\nFirst analyze the problem.\nThen find a solution."
                    .into(),
            tool_call: None,
            collapsed: false,
            thinking_started_at: None,
            thinking_duration_secs: Some(5),
        },
        DisplayMessage::new(DisplayRole::Assistant, "The answer is 42."),
    ];

    terminal
        .draw(|frame| {
            let widget = ConversationWidget::new(&msgs, 0);
            frame.render_widget(widget, frame.area());
        })
        .unwrap();

    let buf = terminal.backend().buffer().clone();
    let rows = buffer_text(&buf, Rect::new(0, 0, 80, 24));
    let all_text = rows.join("\n");

    // Thinking content should be visible
    assert!(
        all_text.contains("think step by step"),
        "Reasoning content missing from render. Rows:\n{}",
        rows.iter()
            .enumerate()
            .map(|(i, r)| format!("  [{i:2}] {r:?}"))
            .collect::<Vec<_>>()
            .join("\n")
    );
    // Assistant content should also be visible
    assert!(
        all_text.contains("answer is 42"),
        "Assistant content missing from render"
    );
    // Check for thinking icon
    assert!(
        all_text.contains(''),
        "Missing ⟡ thinking icon. Rows:\n{}",
        rows.iter()
            .enumerate()
            .map(|(i, r)| format!("  [{i:2}] {r:?}"))
            .collect::<Vec<_>>()
            .join("\n")
    );
}