cargo-port 0.0.3

A TUI for inspecting and managing Rust projects
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
use std::collections::HashSet;
use std::path::Path;
use std::time::Duration;
use std::time::Instant;

use crate::ci::OwnerRepo;
use crate::project::AbsolutePath;
use crate::tui::constants::TOAST_LINE_REVEAL_MS;
use crate::tui::constants::TOAST_WIDTH;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ToastTaskId(pub u64);

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum ToastPersistence {
    #[default]
    Timed,
    Task,
    Permanent,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ToastStyle {
    #[default]
    Normal,
    Error,
}

#[derive(Clone, Debug)]
struct Toast {
    id:                 u64,
    title:              String,
    body:               String,
    timeout_at:         Option<Instant>,
    task_id:            Option<ToastTaskId>,
    dismissed:          bool,
    finished_task:      bool,
    finished_at:        Option<Instant>,
    linger_duration:    Option<Duration>,
    created_at:         Instant,
    exit_started_at:    Option<Instant>,
    persistence:        ToastPersistence,
    style:              ToastStyle,
    action_path:        Option<AbsolutePath>,
    target_height:      u16,
    min_interior_lines: u16,
    /// Per-item linger duration (used while toast is still active).
    item_linger:        Option<Duration>,
    /// Tracked items for task toasts — each item can linger after completion.
    tracked_items:      Vec<TrackedItem>,
}

#[derive(Clone, Debug)]
pub struct TrackedItemKey(String);

impl TrackedItemKey {
    pub fn as_str(&self) -> &str { &self.0 }
}

impl std::fmt::Display for TrackedItemKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) }
}

impl From<AbsolutePath> for TrackedItemKey {
    fn from(value: AbsolutePath) -> Self { Self(value.to_string()) }
}

impl From<&AbsolutePath> for TrackedItemKey {
    fn from(value: &AbsolutePath) -> Self { Self(value.to_string()) }
}

impl From<OwnerRepo> for TrackedItemKey {
    fn from(value: OwnerRepo) -> Self { Self(value.to_string()) }
}

impl From<&OwnerRepo> for TrackedItemKey {
    fn from(value: &OwnerRepo) -> Self { Self(value.to_string()) }
}

impl From<&str> for TrackedItemKey {
    fn from(value: &str) -> Self { Self(String::from(value)) }
}

#[derive(Clone, Debug)]
pub struct TrackedItem {
    pub label:        String,
    pub key:          TrackedItemKey,
    pub started_at:   Option<Instant>,
    pub completed_at: Option<Instant>,
}

impl Toast {
    /// A toast is alive while it is entering, fully visible, or animating
    /// out.  It becomes dead only once the exit animation finishes.
    fn is_alive(&self, now: Instant) -> bool {
        self.exit_started_at.map_or_else(
            || {
                if self.dismissed {
                    return false;
                }
                match self.persistence {
                    ToastPersistence::Permanent => true,
                    ToastPersistence::Task => !self.finished_task,
                    ToastPersistence::Timed => {
                        self.timeout_at.is_none_or(|deadline| deadline > now)
                    },
                }
            },
            |exit_start| exit_lines(now, exit_start, self.target_height) > 0,
        )
    }

    /// Should this toast begin its exit animation right now?
    fn should_exit(&self, now: Instant) -> bool {
        if self.exit_started_at.is_some() {
            return false;
        }
        if self.dismissed {
            return true;
        }
        match self.persistence {
            ToastPersistence::Permanent => false,
            ToastPersistence::Task => self.finished_task,
            ToastPersistence::Timed => self.timeout_at.is_some_and(|deadline| now >= deadline),
        }
    }

    fn current_visible_lines(&self, now: Instant) -> u16 {
        if let Some(exit_start) = self.exit_started_at {
            return exit_lines(now, exit_start, self.target_height);
        }
        let elapsed_lines = elapsed_line_count(now.duration_since(self.created_at));
        // Start at 2 (top+bottom border) — height=1 renders a stray
        // corner glyph that clashes with the window border.
        (2 + elapsed_lines).min(self.target_height)
    }
}

fn exit_lines(now: Instant, exit_start: Instant, target_height: u16) -> u16 {
    if now >= exit_start {
        let remaining =
            target_height.saturating_sub(elapsed_line_count(now.duration_since(exit_start)));
        // Skip height=1: a single-line Block renders a stray corner glyph
        // that clashes with the window border. Jump from 2 → 0.
        if remaining == 1 { 0 } else { remaining }
    } else {
        target_height
    }
}

/// How many full `TOAST_LINE_REVEAL_MS` intervals fit in `elapsed`.
///
/// Uses `as_secs()` + `subsec_millis()` to stay within u64 arithmetic
/// and avoid truncation casts from `as_millis() -> u128`.
fn elapsed_line_count(elapsed: Duration) -> u16 {
    let ms = elapsed
        .as_secs()
        .saturating_mul(1000)
        .saturating_add(u64::from(elapsed.subsec_millis()));
    u16::try_from(ms / TOAST_LINE_REVEAL_MS).unwrap_or(u16::MAX)
}

#[derive(Clone, Debug)]
pub struct ToastView<'a> {
    id:                 u64,
    title:              &'a str,
    body:               &'a str,
    visible_lines:      u16,
    style:              ToastStyle,
    action_path:        Option<&'a Path>,
    min_interior_lines: u16,
    target_height:      u16,
    /// 0.0 = just finished, 1.0 = linger complete, about to exit. None = not lingering.
    linger_progress:    Option<f64>,
    tracked_items:      Vec<TrackedItemView>,
    /// Seconds remaining before the toast begins its exit animation. `None`
    /// for permanent/task toasts that have no timeout.
    remaining_secs:     Option<u64>,
}

/// A tracked item in a toast, with per-item linger progress.
#[derive(Clone, Debug)]
pub struct TrackedItemView {
    pub label:           String,
    /// None = pending. Some(0.0..1.0) = lingering with strikethrough progress.
    pub linger_progress: Option<f64>,
    /// Elapsed duration since the item started. `None` if no start time recorded.
    pub elapsed:         Option<Duration>,
}

impl<'a> ToastView<'a> {
    pub const fn id(&self) -> u64 { self.id }

    pub const fn title(&self) -> &'a str { self.title }

    pub const fn body(&self) -> &'a str { self.body }

    pub const fn visible_lines(&self) -> u16 { self.visible_lines }

    pub const fn style(&self) -> ToastStyle { self.style }

    pub const fn action_path(&self) -> Option<&Path> { self.action_path }

    /// Linger progress: 0.0 = just finished, 1.0 = about to exit. None if not lingering.
    pub const fn linger_progress(&self) -> Option<f64> { self.linger_progress }

    /// Seconds remaining before exit. `None` for non-timed toasts.
    pub const fn remaining_secs(&self) -> Option<u64> { self.remaining_secs }

    /// Tracked items with per-item linger progress.
    pub fn tracked_items(&self) -> &[TrackedItemView] { &self.tracked_items }

    /// Minimum height: 2 (borders) + `min_interior_lines`.
    pub const fn min_height(&self) -> u16 { 2 + self.min_interior_lines }

    /// Full desired height based on body content.
    pub const fn desired_height(&self) -> u16 { self.target_height }
}

#[derive(Default)]
pub struct ToastManager {
    next_id: u64,
    toasts:  Vec<Toast>,
}

impl ToastManager {
    const fn alloc_id(&mut self) -> u64 {
        let id = self.next_id;
        self.next_id = self.next_id.saturating_add(1);
        id
    }

    pub fn push_timed(
        &mut self,
        title: impl Into<String>,
        body: impl Into<String>,
        timeout: Duration,
        min_interior_lines: u16,
    ) -> u64 {
        let id = self.alloc_id();
        let now = Instant::now();
        let body = body.into();
        let target_height = compute_target_height(&body, min_interior_lines);
        self.toasts.push(Toast {
            id,
            title: title.into(),
            body,
            timeout_at: Some(now + timeout),
            task_id: None,
            dismissed: false,
            finished_task: false,
            finished_at: None,
            linger_duration: None,
            created_at: now,
            exit_started_at: None,
            persistence: ToastPersistence::Timed,
            style: ToastStyle::Normal,
            action_path: None,
            target_height,
            min_interior_lines,
            item_linger: None,
            tracked_items: Vec::new(),
        });
        id
    }

    pub fn push_task(
        &mut self,
        title: impl Into<String>,
        body: impl Into<String>,
        min_interior_lines: u16,
    ) -> ToastTaskId {
        let id = self.alloc_id();
        let task_id = ToastTaskId(id);
        let now = Instant::now();
        let body = body.into();
        let target_height = compute_target_height(&body, min_interior_lines);
        self.toasts.push(Toast {
            id,
            title: title.into(),
            body,
            timeout_at: None,
            task_id: Some(task_id),
            dismissed: false,
            finished_task: false,
            finished_at: None,
            linger_duration: None,
            created_at: now,
            exit_started_at: None,
            persistence: ToastPersistence::Task,
            style: ToastStyle::Normal,
            action_path: None,
            target_height,
            min_interior_lines,
            item_linger: None,
            tracked_items: Vec::new(),
        });
        task_id
    }

    pub fn push_persistent(
        &mut self,
        title: impl Into<String>,
        body: impl Into<String>,
        style: ToastStyle,
        action_path: Option<AbsolutePath>,
        min_interior_lines: u16,
    ) -> u64 {
        let id = self.alloc_id();
        let now = Instant::now();
        let body = body.into();
        let target_height = compute_target_height(&body, min_interior_lines);
        self.toasts.push(Toast {
            id,
            title: title.into(),
            body,
            timeout_at: None,
            task_id: None,
            dismissed: false,
            finished_task: false,
            finished_at: None,
            linger_duration: None,
            created_at: now,
            exit_started_at: None,
            persistence: ToastPersistence::Permanent,
            style,
            action_path,
            target_height,
            min_interior_lines,
            item_linger: None,
            tracked_items: Vec::new(),
        });
        id
    }

    pub fn dismiss(&mut self, id: u64) {
        if let Some(toast) = self.toasts.iter_mut().find(|toast| toast.id == id) {
            toast.dismissed = true;
        }
    }

    pub fn finish_task(&mut self, task_id: ToastTaskId, linger: Duration) {
        let now = Instant::now();
        let deadline = now + linger;
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) && !toast.finished_task {
                toast.finished_task = true;
                toast.finished_at = Some(now);
                toast.linger_duration = Some(linger);
                toast.timeout_at = Some(deadline);
                toast.persistence = ToastPersistence::Timed;
            }
        }
    }

    /// Reactivate a finished or active task toast. If already active this is a
    /// no-op. Returns `true` if the toast was found (whether it needed
    /// reactivation or not).
    pub fn reactivate_task(&mut self, task_id: ToastTaskId) -> bool {
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                if toast.finished_task {
                    toast.finished_task = false;
                    toast.finished_at = None;
                    toast.linger_duration = None;
                    toast.timeout_at = None;
                    toast.exit_started_at = None;
                    toast.persistence = ToastPersistence::Task;
                }
                return true;
            }
        }
        false
    }

    #[cfg(test)]
    pub fn update_task_body(&mut self, task_id: ToastTaskId, body: impl Into<String>) {
        let body = body.into();
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                let target_height = compute_target_height(&body, toast.min_interior_lines);
                toast.body.clone_from(&body);
                toast.target_height = target_height;
            }
        }
    }

    /// Set tracked items for a task toast. Items are displayed instead of
    /// the plain body text. Completed items linger with strikethrough.
    pub fn set_tracked_items(
        &mut self,
        task_id: ToastTaskId,
        items: &[TrackedItem],
        item_linger: Duration,
    ) {
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                // Rebuild body from tracked items for height calc.
                let body: String = items
                    .iter()
                    .map(|item| item.label.as_str())
                    .collect::<Vec<_>>()
                    .join("\n");
                let target_height = compute_target_height(&body, toast.min_interior_lines);
                toast.body = body;
                toast.target_height = target_height;
                toast.item_linger = Some(item_linger);
                toast.tracked_items = items.to_vec();
            }
        }
    }

    /// Check if a task toast has been finished (countdown started).
    pub fn is_task_finished(&self, task_id: ToastTaskId) -> bool {
        self.toasts
            .iter()
            .find(|t| t.task_id == Some(task_id))
            .is_some_and(|t| t.finished_task)
    }

    /// Count remaining tracked items for a task toast.
    pub fn tracked_item_count(&self, task_id: ToastTaskId) -> usize {
        self.toasts
            .iter()
            .find(|t| t.task_id == Some(task_id))
            .map_or(0, |t| t.tracked_items.len())
    }

    /// Mark tracked items as completed if their key is NOT in the active set.
    pub fn complete_missing_items(&mut self, task_id: ToastTaskId, active_keys: &HashSet<String>) {
        let now = Instant::now();
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                for item in &mut toast.tracked_items {
                    if item.completed_at.is_none() && !active_keys.contains(item.key.as_str()) {
                        item.completed_at = Some(now);
                    }
                }
            }
        }
    }

    /// Add tracked items that aren't already tracked (matched by key).
    pub fn add_new_tracked_items(
        &mut self,
        task_id: ToastTaskId,
        new_items: &[TrackedItem],
        item_linger: Duration,
    ) {
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                let existing: HashSet<String> = toast
                    .tracked_items
                    .iter()
                    .map(|i| i.key.to_string())
                    .collect();
                for item in new_items {
                    if !existing.contains(item.key.as_str()) {
                        toast.tracked_items.push(item.clone());
                    }
                }
                toast.item_linger = Some(item_linger);
                let body: String = toast
                    .tracked_items
                    .iter()
                    .map(|i| i.label.as_str())
                    .collect::<Vec<_>>()
                    .join("\n");
                toast.target_height = compute_target_height(&body, toast.min_interior_lines);
                toast.body = body;
                break;
            }
        }
    }

    /// Restart a tracked item — reset its `started_at` and clear
    /// `completed_at` so the spinner and duration counter restart.
    pub fn restart_tracked_item(
        &mut self,
        task_id: ToastTaskId,
        key: &TrackedItemKey,
        started_at: Instant,
    ) {
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                for item in &mut toast.tracked_items {
                    if item.key.as_str() == key.as_str() {
                        item.started_at = Some(started_at);
                        item.completed_at = None;
                    }
                }
                break;
            }
        }
    }

    /// Mark a tracked item as completed by key.
    pub fn mark_item_completed(&mut self, task_id: ToastTaskId, key: &str) {
        let now = Instant::now();
        for toast in &mut self.toasts {
            if toast.task_id == Some(task_id) {
                for item in &mut toast.tracked_items {
                    if item.key.as_str() == key && item.completed_at.is_none() {
                        item.completed_at = Some(now);
                        break;
                    }
                }
            }
        }
    }

    /// Remove expired lingering items from tracked toasts.
    pub fn prune_tracked_items(&mut self, now: Instant, linger: Duration) {
        for toast in &mut self.toasts {
            if toast.tracked_items.is_empty() {
                continue;
            }
            let before = toast.tracked_items.len();
            toast.tracked_items.retain(|item| {
                item.completed_at
                    .is_none_or(|completed| now.duration_since(completed) < linger)
            });
            if toast.tracked_items.len() != before {
                let body: String = toast
                    .tracked_items
                    .iter()
                    .map(|item| item.label.as_str())
                    .collect::<Vec<_>>()
                    .join("\n");
                toast.target_height = compute_target_height(&body, toast.min_interior_lines);
                toast.body = body;
                // All tracked items gone — auto-exit the toast immediately.
                if toast.tracked_items.is_empty() && toast.exit_started_at.is_none() {
                    toast.persistence = ToastPersistence::Timed;
                    toast.timeout_at = Some(now);
                }
            }
        }
    }

    pub fn prune(&mut self, now: Instant) {
        // Start exit animations for toasts that should begin exiting.
        for toast in &mut self.toasts {
            if toast.should_exit(now) {
                toast.exit_started_at = Some(now);
            }
        }
        // Remove toasts whose exit animation has completed.
        self.toasts.retain(|toast| toast.is_alive(now));
    }

    pub fn active(&self, now: Instant) -> Vec<ToastView<'_>> {
        self.toasts
            .iter()
            .filter(|toast| toast.is_alive(now))
            .map(|toast| {
                let linger_progress =
                    toast
                        .finished_at
                        .zip(toast.linger_duration)
                        .map(|(finished_at, linger)| {
                            let elapsed = now.duration_since(finished_at).as_secs_f64();
                            let total = linger.as_secs_f64();
                            if total <= 0.0 {
                                1.0
                            } else {
                                (elapsed / total).clamp(0.0, 1.0)
                            }
                        });
                let tracked_items: Vec<TrackedItemView> = toast
                    .tracked_items
                    .iter()
                    .map(|item| {
                        let item_progress = item.completed_at.and_then(|completed| {
                            toast.item_linger.map(|linger| {
                                let elapsed = now.duration_since(completed).as_secs_f64();
                                let total = linger.as_secs_f64();
                                if total <= 0.0 {
                                    1.0
                                } else {
                                    (elapsed / total).clamp(0.0, 1.0)
                                }
                            })
                        });
                        let elapsed = item.started_at.map(|started| {
                            let end = item.completed_at.unwrap_or(now);
                            end.duration_since(started)
                        });
                        TrackedItemView {
                            label: item.label.clone(),
                            linger_progress: item_progress,
                            elapsed,
                        }
                    })
                    .collect();
                let remaining_secs = toast.timeout_at.and_then(|deadline| {
                    if deadline > now {
                        Some(deadline.duration_since(now).as_secs().saturating_add(1))
                    } else {
                        None
                    }
                });
                ToastView {
                    id: toast.id,
                    title: &toast.title,
                    body: &toast.body,
                    visible_lines: toast.current_visible_lines(now),
                    style: toast.style,
                    action_path: toast.action_path.as_deref(),
                    min_interior_lines: toast.min_interior_lines,
                    target_height: toast.target_height,
                    linger_progress,
                    tracked_items,
                    remaining_secs,
                }
            })
            .collect()
    }
}

/// Compute the target (fully-revealed) height for a toast based on its
/// body content.  Height = 2 (borders, title is on the top border) +
/// body lines, with a floor of `2 + min_interior_lines`.
fn compute_target_height(body: &str, min_interior_lines: u16) -> u16 {
    // Inner width is toast width minus 2 for borders.
    let inner_width = usize::from(TOAST_WIDTH.saturating_sub(2));
    let body_lines = if body.is_empty() {
        1
    } else {
        body.lines()
            .map(|line| {
                let width = unicode_width::UnicodeWidthStr::width(line);
                if width == 0 {
                    1
                } else {
                    width.div_ceil(inner_width.max(1))
                }
            })
            .sum::<usize>()
    };
    let raw = u16::try_from(2 + body_lines).unwrap_or(u16::MAX);
    raw.max(2 + min_interior_lines)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "tests")]
mod tests {
    use std::time::Duration;

    use super::*;

    /// Duration long enough for the full exit animation to complete.
    /// Use a generous upper bound since toast heights are now variable.
    const EXIT_ANIMATION: Duration = Duration::from_millis(20 * TOAST_LINE_REVEAL_MS + 1);

    #[test]
    fn timed_toast_expires() {
        let mut manager = ToastManager::default();
        manager.push_timed("settings", "updated", Duration::from_millis(10), 1);
        assert_eq!(manager.active(Instant::now()).len(), 1);

        // Prune after timeout — starts exit animation but toast is still alive.
        let after_timeout = Instant::now() + Duration::from_millis(20);
        manager.prune(after_timeout);
        assert_eq!(manager.active(after_timeout).len(), 1);

        // After the exit animation completes, the toast is fully removed.
        let after_exit = after_timeout + EXIT_ANIMATION;
        manager.prune(after_exit);
        assert!(manager.active(after_exit).is_empty());
    }

    #[test]
    fn task_toast_lingers_then_exits() {
        let mut manager = ToastManager::default();
        let linger = Duration::from_secs(1);
        let task = manager.push_task("cargo clean", "~/rust/bevy", 1);
        assert_eq!(manager.active(Instant::now()).len(), 1);

        manager.finish_task(task, linger);
        let now = Instant::now();
        manager.prune(now);
        // Still alive during linger period.
        assert_eq!(manager.active(now).len(), 1);

        // After linger, exit animation begins.
        let after_linger = now + linger + Duration::from_millis(10);
        manager.prune(after_linger);
        assert_eq!(manager.active(after_linger).len(), 1); // exit animation in progress

        // After linger + exit animation, toast is gone.
        let after_all = after_linger + EXIT_ANIMATION;
        manager.prune(after_all);
        assert!(manager.active(after_all).is_empty());
    }

    #[test]
    fn task_toast_body_can_be_updated() {
        let mut manager = ToastManager::default();
        let task = manager.push_task("startup git", "loading", 1);
        manager.update_task_body(task, "2 remaining");
        let active = manager.active(Instant::now());
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].body(), "2 remaining");
    }

    #[test]
    fn permanent_toast_stays_after_prune() {
        let mut manager = ToastManager::default();
        manager.push_persistent("error", "bad keymap", ToastStyle::Error, None, 1);

        // Prune many times — permanent toast stays.
        let later = Instant::now() + Duration::from_hours(1);
        manager.prune(later);
        assert_eq!(manager.active(later).len(), 1);
    }

    #[test]
    fn permanent_toast_dismissed_by_user() {
        let mut manager = ToastManager::default();
        let id = manager.push_persistent("error", "bad keymap", ToastStyle::Error, None, 1);
        manager.dismiss(id);

        let now = Instant::now();
        manager.prune(now);
        // Still alive during exit animation.
        assert_eq!(manager.active(now).len(), 1);

        let after_exit = now + EXIT_ANIMATION;
        manager.prune(after_exit);
        assert!(manager.active(after_exit).is_empty());
    }

    #[test]
    fn toast_view_exposes_style() {
        let mut manager = ToastManager::default();
        manager.push_persistent("error", "bad", ToastStyle::Error, None, 1);
        let active = manager.active(Instant::now());
        assert_eq!(active[0].style(), ToastStyle::Error);
    }

    #[test]
    fn toast_view_exposes_action_path() {
        let mut manager = ToastManager::default();
        let path: AbsolutePath = "/tmp/keymap.toml".into();
        manager.push_persistent("error", "bad", ToastStyle::Error, Some(path.clone()), 1);
        let active = manager.active(Instant::now());
        assert_eq!(active[0].action_path(), Some(path.as_path()));
    }

    #[test]
    fn variable_height_short_body() {
        // 2 borders + 1 body line = 3, min_interior_lines=1 floor is 3
        assert_eq!(compute_target_height("short", 1), 3);
    }

    #[test]
    fn variable_height_long_body_no_clamp() {
        let body = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10";
        // 2 + 10 = 12, no max clamp
        assert_eq!(compute_target_height(body, 1), 12);
    }

    #[test]
    fn variable_height_multiline_body() {
        let body = "line1\nline2\nline3";
        // 2 (borders) + 3 body lines = 5
        assert_eq!(compute_target_height(body, 1), 5);
    }

    #[test]
    fn min_interior_lines_raises_floor() {
        // Body has 1 line, but min_interior_lines=2 means floor is 4
        assert_eq!(compute_target_height("short", 2), 4);
    }

    #[test]
    fn min_interior_lines_does_not_shrink() {
        // Body has 5 lines, min_interior_lines=1 means floor is 3, actual is 7
        let body = "a\nb\nc\nd\ne";
        assert_eq!(compute_target_height(body, 1), 7);
    }
}