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

fn make_items() -> Vec<TodoDisplayItem> {
    vec![
        TodoDisplayItem {
            id: 1,
            title: "Set up project".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Write code".into(),
            status: TodoDisplayStatus::InProgress,
            active_form: Some("Writing code".into()),
        },
        TodoDisplayItem {
            id: 3,
            title: "Write tests".into(),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        },
    ]
}

#[test]
fn test_build_lines_count() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items);
    let (done, in_progress, total) = widget.counts();
    let lines = widget.build_lines(done, in_progress, total);
    // 3 item lines (no progress bar)
    assert_eq!(lines.len(), 3);
}

#[test]
fn test_render_does_not_panic() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items).with_plan_name("bold-blazing-badger");
    let mut buf = Buffer::empty(Rect::new(0, 0, 80, 10));
    widget.render(Rect::new(0, 0, 80, 10), &mut buf);
}

#[test]
fn test_empty_items() {
    let items: Vec<TodoDisplayItem> = vec![];
    let widget = TodoPanelWidget::new(&items);
    let (done, in_progress, total) = widget.counts();
    let lines = widget.build_lines(done, in_progress, total);
    assert!(lines.is_empty());
}

#[test]
fn test_all_completed_green_border() {
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Also done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
    ];
    // Just verify no panic with all completed
    let widget = TodoPanelWidget::new(&items);
    let mut buf = Buffer::empty(Rect::new(0, 0, 60, 6));
    widget.render(Rect::new(0, 0, 60, 6), &mut buf);
}

#[test]
fn test_long_title_not_truncated() {
    let items = vec![TodoDisplayItem {
        id: 1,
        title: "A".repeat(100),
        status: TodoDisplayStatus::Pending,
        active_form: None,
    }];
    let widget = TodoPanelWidget::new(&items);
    let (done, in_progress, total) = widget.counts();
    let lines = widget.build_lines(done, in_progress, total);
    assert_eq!(lines.len(), 1);
    // Full title should be present (no truncation)
    let text: String = lines[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert!(text.contains(&"A".repeat(100)));
}

#[test]
fn test_collapsed_render() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items)
        .with_expanded(false)
        .with_spinner_tick(3);
    let mut buf = Buffer::empty(Rect::new(0, 0, 60, 3));
    widget.render(Rect::new(0, 0, 60, 3), &mut buf);
}

#[test]
fn test_collapsed_uses_active_form() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items).with_expanded(false);
    let (done, _, total) = widget.counts();
    let line = widget.build_collapsed_line(done, total);
    // Should contain the active_form text "Writing code"
    let text: String = line.spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("Writing code"));
}

#[test]
fn test_required_height_expanded() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items);
    // 3 items + 2 borders = 5
    assert_eq!(widget.required_height(), 5);
}

#[test]
fn test_collapsed_all_done_shows_checkmark() {
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Task A".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Task B".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
    ];
    let widget = TodoPanelWidget::new(&items).with_expanded(false);
    let (done, _, total) = widget.counts();
    let line = widget.build_collapsed_line(done, total);
    let text: String = line.spans.iter().map(|s| s.content.to_string()).collect();
    assert!(
        text.contains("All tasks complete"),
        "Expected 'All tasks complete', got: {text}"
    );
    assert!(text.contains('\u{2714}'), "Expected checkmark in: {text}");
    assert!(
        !text.contains("Working"),
        "Should not show 'Working' when all done"
    );
}

#[test]
fn test_required_height_zero_when_all_done() {
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Also done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
    ];
    let widget = TodoPanelWidget::new(&items);
    assert_eq!(
        widget.required_height(),
        0,
        "Panel should hide (height 0) when all items are completed"
    );
}

#[test]
fn test_required_height_collapsed() {
    let items = make_items();
    let widget = TodoPanelWidget::new(&items).with_expanded(false);
    assert_eq!(widget.required_height(), 3);
}

#[test]
fn test_expanded_title_no_spinner_in_header() {
    let items = make_items(); // has 1 in-progress item
    let widget = TodoPanelWidget::new(&items).with_spinner_tick(2);
    let mut buf = Buffer::empty(Rect::new(0, 0, 80, 10));
    widget.render(Rect::new(0, 0, 80, 10), &mut buf);
    // Extract top row text from buffer
    let top_row: String = (0..80)
        .map(|x| buf.cell((x, 0)).unwrap().symbol().to_string())
        .collect::<String>();
    // No spinner in the expanded header (spinners are per-item only)
    for frame in SPINNER_FRAMES {
        assert!(
            !top_row.contains(*frame),
            "Should not have spinner in expanded header, got: {top_row}"
        );
    }
    assert!(
        top_row.contains("Ctrl+T to toggle"),
        "Expected hint in title, got: {top_row}"
    );
}

#[test]
fn test_expanded_title_no_spinner_when_all_done() {
    let items = vec![TodoDisplayItem {
        id: 1,
        title: "Done".into(),
        status: TodoDisplayStatus::Completed,
        active_form: None,
    }];
    let widget = TodoPanelWidget::new(&items).with_spinner_tick(2);
    let mut buf = Buffer::empty(Rect::new(0, 0, 80, 6));
    widget.render(Rect::new(0, 0, 80, 6), &mut buf);
    let top_row: String = (0..80)
        .map(|x| buf.cell((x, 0)).unwrap().symbol().to_string())
        .collect::<String>();
    for frame in SPINNER_FRAMES {
        assert!(
            !top_row.contains(*frame),
            "Should not have spinner when all done, got: {top_row}"
        );
    }
    assert!(
        top_row.contains("Ctrl+T to toggle"),
        "Expected hint in title, got: {top_row}"
    );
}

#[test]
fn test_todo_panel_height_helper() {
    assert_eq!(todo_panel_height(0, true), 0);
    assert_eq!(todo_panel_height(0, false), 0);
    assert_eq!(todo_panel_height(3, true), 5); // 3 + 2 borders
    assert_eq!(todo_panel_height(3, false), 3); // collapsed
    assert_eq!(todo_panel_height(10, true), 12); // 10 + 2 = 12 (at cap)
    assert_eq!(todo_panel_height(15, true), 12); // capped at 12
}

// --- Widget rendering tests ---

#[test]
fn test_spinner_frame_changes_with_tick() {
    let items = vec![TodoDisplayItem {
        id: 1,
        title: "Active".into(),
        status: TodoDisplayStatus::InProgress,
        active_form: None,
    }];
    // Different ticks should produce different spinner characters
    let widget0 = TodoPanelWidget::new(&items).with_spinner_tick(0);
    let widget1 = TodoPanelWidget::new(&items).with_spinner_tick(1);
    let (d, ip, t) = widget0.counts();
    let line0 = widget0.build_lines(d, ip, t);
    let (d, ip, t) = widget1.counts();
    let line1 = widget1.build_lines(d, ip, t);

    let text0: String = line0[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    let text1: String = line1[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert_ne!(
        text0, text1,
        "Different ticks should produce different spinner chars"
    );
}

#[test]
fn test_pending_shows_circle_not_spinner() {
    let items = vec![TodoDisplayItem {
        id: 1,
        title: "Waiting".into(),
        status: TodoDisplayStatus::Pending,
        active_form: None,
    }];
    let widget = TodoPanelWidget::new(&items).with_spinner_tick(0);
    let (d, ip, t) = widget.counts();
    let lines = widget.build_lines(d, ip, t);
    let text: String = lines[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert!(
        text.contains('\u{25CB}'),
        "Pending should show â—‹, got: {text}"
    );
    for frame in SPINNER_FRAMES {
        assert!(
            !text.contains(*frame),
            "Pending should not show spinner {frame}, got: {text}"
        );
    }
}

#[test]
fn test_resume_shows_spinner() {
    // Simulate: item was Pending (after interrupt), then set back to InProgress
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Resumed task".into(),
            status: TodoDisplayStatus::InProgress,
            active_form: Some("Resuming task".into()),
        },
        TodoDisplayItem {
            id: 3,
            title: "Later".into(),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        },
    ];
    let widget = TodoPanelWidget::new(&items).with_spinner_tick(3);
    let (d, ip, t) = widget.counts();
    let lines = widget.build_lines(d, ip, t);

    // Item 2 (index 1) should show spinner
    let text1: String = lines[1]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    let expected_spinner = SPINNER_FRAMES[3 % SPINNER_FRAMES.len()];
    assert!(
        text1.contains(expected_spinner),
        "Resumed InProgress item should show spinner '{expected_spinner}', got: {text1}"
    );
}

#[test]
fn test_mixed_states_render() {
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Completed".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Active".into(),
            status: TodoDisplayStatus::InProgress,
            active_form: Some("Working".into()),
        },
        TodoDisplayItem {
            id: 3,
            title: "Waiting".into(),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        },
    ];
    let widget = TodoPanelWidget::new(&items).with_spinner_tick(0);
    let mut buf = Buffer::empty(Rect::new(0, 0, 80, 7));
    widget.render(Rect::new(0, 0, 80, 7), &mut buf);

    // Extract item rows (row 0 is title border, items start at row 1)
    let row1: String = (0..80)
        .map(|x| buf.cell((x, 1)).unwrap().symbol().to_string())
        .collect();
    let row2: String = (0..80)
        .map(|x| buf.cell((x, 2)).unwrap().symbol().to_string())
        .collect();
    let row3: String = (0..80)
        .map(|x| buf.cell((x, 3)).unwrap().symbol().to_string())
        .collect();

    assert!(
        row1.contains('\u{2714}'),
        "Completed should show ✔, got: {row1}"
    );
    assert!(
        row2.contains(SPINNER_FRAMES[0]),
        "InProgress should show spinner, got: {row2}"
    );
    assert!(
        row3.contains('\u{25CB}'),
        "Pending should show â—‹, got: {row3}"
    );
}

#[test]
fn test_collapsed_no_in_progress_shows_working() {
    let items = vec![TodoDisplayItem {
        id: 1,
        title: "Task A".into(),
        status: TodoDisplayStatus::Pending,
        active_form: None,
    }];
    let widget = TodoPanelWidget::new(&items).with_expanded(false);
    let (done, _, total) = widget.counts();
    let line = widget.build_collapsed_line(done, total);
    let text: String = line.spans.iter().map(|s| s.content.to_string()).collect();
    assert!(
        text.contains("Working..."),
        "Should show fallback 'Working...' when no InProgress item, got: {text}"
    );
}

#[test]
fn test_collapsed_after_resume_shows_active_form() {
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "Done".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "Build project".into(),
            status: TodoDisplayStatus::InProgress,
            active_form: Some("Building project".into()),
        },
    ];
    let widget = TodoPanelWidget::new(&items)
        .with_expanded(false)
        .with_spinner_tick(5);
    let (done, _, total) = widget.counts();
    let line = widget.build_collapsed_line(done, total);
    let text: String = line.spans.iter().map(|s| s.content.to_string()).collect();
    assert!(
        text.contains("Building project"),
        "Collapsed mode should show active_form after resume, got: {text}"
    );
    assert!(
        text.contains("(1/2)"),
        "Should show progress count, got: {text}"
    );
}

#[test]
fn test_panel_height_lifecycle() {
    // Empty — panel height governed by todo_panel_height() helper (returns 0)
    assert_eq!(todo_panel_height(0, true), 0);

    // Items added — panel visible
    let items = vec![
        TodoDisplayItem {
            id: 1,
            title: "A".into(),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "B".into(),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        },
    ];
    let w = TodoPanelWidget::new(&items);
    assert_eq!(w.required_height(), 4); // 2 items + 2 borders

    // All done — panel hides
    let done_items = vec![
        TodoDisplayItem {
            id: 1,
            title: "A".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
        TodoDisplayItem {
            id: 2,
            title: "B".into(),
            status: TodoDisplayStatus::Completed,
            active_form: None,
        },
    ];
    let w = TodoPanelWidget::new(&done_items);
    assert_eq!(w.required_height(), 0);

    // New pending items — panel visible again
    let new_items = vec![TodoDisplayItem {
        id: 3,
        title: "C".into(),
        status: TodoDisplayStatus::Pending,
        active_form: None,
    }];
    let w = TodoPanelWidget::new(&new_items);
    assert_eq!(w.required_height(), 3); // 1 item + 2 borders
}

#[test]
fn test_max_items_height_cap() {
    let items: Vec<TodoDisplayItem> = (1..=15)
        .map(|i| TodoDisplayItem {
            id: i,
            title: format!("Task {i}"),
            status: TodoDisplayStatus::Pending,
            active_form: None,
        })
        .collect();
    let w = TodoPanelWidget::new(&items);
    assert_eq!(
        w.required_height(),
        12,
        "15 items should be capped at 12 rows"
    );
}

#[test]
fn test_single_item_all_statuses() {
    for (status, expected_char) in [
        (TodoDisplayStatus::Pending, '\u{25CB}'),
        (TodoDisplayStatus::InProgress, SPINNER_FRAMES[0]),
        (TodoDisplayStatus::Completed, '\u{2714}'),
    ] {
        let items = vec![TodoDisplayItem {
            id: 1,
            title: "Solo".into(),
            status,
            active_form: None,
        }];
        let widget = TodoPanelWidget::new(&items).with_spinner_tick(0);
        let (d, ip, t) = widget.counts();
        let lines = widget.build_lines(d, ip, t);
        let text: String = lines[0]
            .spans
            .iter()
            .map(|s| s.content.to_string())
            .collect();
        assert!(
            text.contains(expected_char),
            "Status {status:?} should show '{expected_char}', got: {text}"
        );
    }
}

#[test]
fn test_status_transition_rendering() {
    // Simulate item going through Pending → InProgress → Completed
    let make = |status: TodoDisplayStatus| -> Vec<TodoDisplayItem> {
        vec![TodoDisplayItem {
            id: 1,
            title: "Evolving task".into(),
            status,
            active_form: Some("Evolving".into()),
        }]
    };

    // Pending phase
    let items = make(TodoDisplayStatus::Pending);
    let w = TodoPanelWidget::new(&items).with_spinner_tick(0);
    let (d, ip, t) = w.counts();
    let lines = w.build_lines(d, ip, t);
    let text: String = lines[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert!(text.contains('\u{25CB}'), "Pending phase should show â—‹");

    // InProgress phase
    let items = make(TodoDisplayStatus::InProgress);
    let w = TodoPanelWidget::new(&items).with_spinner_tick(2);
    let (d, ip, t) = w.counts();
    let lines = w.build_lines(d, ip, t);
    let text: String = lines[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert!(
        text.contains(SPINNER_FRAMES[2]),
        "InProgress phase should show spinner"
    );

    // Completed phase
    let items = make(TodoDisplayStatus::Completed);
    let w = TodoPanelWidget::new(&items).with_spinner_tick(0);
    let (d, ip, t) = w.counts();
    let lines = w.build_lines(d, ip, t);
    let text: String = lines[0]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    assert!(text.contains('\u{2714}'), "Completed phase should show ✔");
}

// --- Integration-level lifecycle tests ---

#[test]
fn test_interrupt_resume_display_flow() {
    // Simulate the full interrupt→resume flow at the display level
    use opendev_runtime::todo::TodoManager;

    let mut mgr = TodoManager::from_steps(&["Task A".into(), "Task B".into(), "Task C".into()]);
    mgr.complete(1);
    mgr.start(2);

    // Verify pre-interrupt state
    let display: Vec<TodoDisplayItem> = mgr
        .all()
        .iter()
        .map(|item| TodoDisplayItem {
            id: item.id,
            title: item.title.clone(),
            status: match item.status {
                opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
            },
            active_form: None,
        })
        .collect();
    assert_eq!(display[1].status, TodoDisplayStatus::InProgress);

    // Simulate interrupt: reset_stuck_todos + sync
    mgr.reset_stuck_todos();
    let display: Vec<TodoDisplayItem> = mgr
        .all()
        .iter()
        .map(|item| TodoDisplayItem {
            id: item.id,
            title: item.title.clone(),
            status: match item.status {
                opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
            },
            active_form: None,
        })
        .collect();
    assert_eq!(
        display[1].status,
        TodoDisplayStatus::Pending,
        "After interrupt, item should be Pending"
    );
    assert!(
        display
            .iter()
            .all(|i| i.status != TodoDisplayStatus::InProgress)
    );

    // Simulate resume: start next pending + sync
    if let Some(next) = mgr.next_pending() {
        let id = next.id;
        mgr.start(id);
    }
    let display: Vec<TodoDisplayItem> = mgr
        .all()
        .iter()
        .map(|item| TodoDisplayItem {
            id: item.id,
            title: item.title.clone(),
            status: match item.status {
                opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
            },
            active_form: None,
        })
        .collect();

    // Item 2 should be InProgress again (it's the first pending after item 1 which is completed)
    assert_eq!(
        display[1].status,
        TodoDisplayStatus::InProgress,
        "After resume, item should be InProgress"
    );

    // Widget should render spinner for the resumed item
    let widget = TodoPanelWidget::new(&display).with_spinner_tick(4);
    let (d, ip, t) = widget.counts();
    let lines = widget.build_lines(d, ip, t);
    let text: String = lines[1]
        .spans
        .iter()
        .map(|s| s.content.to_string())
        .collect();
    let expected = SPINNER_FRAMES[4 % SPINNER_FRAMES.len()];
    assert!(
        text.contains(expected),
        "Resumed item should show spinner '{expected}', got: {text}"
    );
}

#[test]
fn test_full_todo_lifecycle() {
    use opendev_runtime::todo::TodoManager;

    let mut mgr = TodoManager::new();
    assert_eq!(mgr.total(), 0);

    // Write todos
    mgr.write_todos(vec![
        (
            "Setup".into(),
            opendev_runtime::TodoStatus::Pending,
            "Setting up".into(),
            Vec::new(),
        ),
        (
            "Build".into(),
            opendev_runtime::TodoStatus::Pending,
            "Building".into(),
            Vec::new(),
        ),
        (
            "Test".into(),
            opendev_runtime::TodoStatus::Pending,
            "Testing".into(),
            Vec::new(),
        ),
    ]);
    assert_eq!(mgr.total(), 3);

    let to_display = |mgr: &TodoManager| -> Vec<TodoDisplayItem> {
        mgr.all()
            .iter()
            .map(|item| TodoDisplayItem {
                id: item.id,
                title: item.title.clone(),
                status: match item.status {
                    opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                    opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                    opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
                },
                active_form: if item.active_form.is_empty() {
                    None
                } else {
                    Some(item.active_form.clone())
                },
            })
            .collect()
    };

    // Start first
    mgr.start(1);
    let display = to_display(&mgr);
    assert_eq!(display[0].status, TodoDisplayStatus::InProgress);
    let w = TodoPanelWidget::new(&display);
    assert!(w.required_height() > 0, "Panel should be visible");

    // Complete first, start second
    mgr.complete(1);
    mgr.start(2);
    let display = to_display(&mgr);
    assert_eq!(display[0].status, TodoDisplayStatus::Completed);
    assert_eq!(display[1].status, TodoDisplayStatus::InProgress);

    // Complete all
    mgr.complete(2);
    mgr.complete(3);
    let display = to_display(&mgr);
    let w = TodoPanelWidget::new(&display);
    assert_eq!(w.required_height(), 0, "Panel should hide when all done");
}

#[test]
fn test_cancel_and_recreate() {
    use opendev_runtime::todo::TodoManager;

    let mut mgr = TodoManager::from_steps(&["Old A".into(), "Old B".into()]);
    mgr.start(1);

    // Interrupt
    mgr.reset_stuck_todos();

    // Recreate with entirely new todos
    mgr.write_todos(vec![
        (
            "New X".into(),
            opendev_runtime::TodoStatus::Pending,
            String::new(),
            Vec::new(),
        ),
        (
            "New Y".into(),
            opendev_runtime::TodoStatus::InProgress,
            "Doing Y".into(),
            Vec::new(),
        ),
        (
            "New Z".into(),
            opendev_runtime::TodoStatus::Pending,
            String::new(),
            Vec::new(),
        ),
    ]);

    let display: Vec<TodoDisplayItem> = mgr
        .all()
        .iter()
        .map(|item| TodoDisplayItem {
            id: item.id,
            title: item.title.clone(),
            status: match item.status {
                opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
            },
            active_form: if item.active_form.is_empty() {
                None
            } else {
                Some(item.active_form.clone())
            },
        })
        .collect();

    assert_eq!(display.len(), 3);
    assert_eq!(display[0].title, "New X");
    assert_eq!(display[1].status, TodoDisplayStatus::InProgress);
    assert_eq!(display[1].active_form.as_deref(), Some("Doing Y"));
    assert_eq!(display[2].status, TodoDisplayStatus::Pending);

    // Widget should render properly
    let w = TodoPanelWidget::new(&display).with_spinner_tick(0);
    let mut buf = Buffer::empty(Rect::new(0, 0, 80, 7));
    w.render(Rect::new(0, 0, 80, 7), &mut buf); // should not panic
}