workmux 0.1.212

An opinionated workflow tool that orchestrates git worktrees and tmux
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
//! Per-row context: pre-computed token values for a single agent row.

use ratatui::style::{Color, Modifier, Style};

use crate::agent_display::{extract_project_name, extract_worktree_name, sanitize_pane_title};
use crate::agent_identity::AgentKind;
use crate::git::GitStatus;
use crate::github::PrSummary;
use crate::multiplexer::agent::resolve_profile_for_display;
use crate::multiplexer::{AgentPane, AgentStatus};
use crate::ui::theme::ThemePalette;

use super::super::app::{ResolvedAgentIcons, SidebarApp};
use super::TokenId;

/// Pre-computed values for every piece of row metadata.
pub struct RowContext<'a> {
    pub agent: &'a AgentPane,
    /// Resolved primary label.
    pub primary: String,
    /// Resolved secondary label.
    pub secondary: String,
    /// Pane suffix like " (1)" when multiple agents share a window.
    pub pane_suffix: String,
    /// Compact elapsed string (e.g. "5:23", "2h", "1d").
    pub elapsed: String,
    /// Status icon parsed into styled spans.
    pub status_icon_spans: Vec<(String, Style)>,
    /// Foreground color extracted from status icon style.
    pub status_color: Color,
    /// Sanitized pane title, filtered against primary/secondary duplicates.
    pub pane_title: Option<String>,
    /// Git status for this agent's path.
    pub git_status: Option<&'a GitStatus>,
    /// PR summary for this agent's path.
    pub pr_summary: Option<&'a PrSummary>,
    /// Row flags.
    pub is_stale: bool,
    pub is_active: bool,
    pub is_selected: bool,
    /// Theme palette for style resolution.
    pub palette: &'a ThemePalette,
    /// Pre-resolved agent icon string (empty when no profile matches).
    pub agent_icon: String,
    /// Pre-resolved foreground color for `{agent_icon}`. `None` means fall
    /// through to `palette.text`.
    pub agent_icon_color: Option<Color>,
    /// Pre-resolved agent label string (empty when no profile matches).
    pub agent_label: String,
    /// 0-based sidebar row index. Rendered as 1-based via the `idx` and
    /// `jump_key` tokens.
    pub idx: usize,
    /// Current spinner frame for animated PR checks.
    pub spinner_frame: u8,
}

impl<'a> RowContext<'a> {
    pub fn build(
        app: &'a SidebarApp,
        agent: &'a AgentPane,
        idx: usize,
        pane_suffixes: &[String],
        now_secs: u64,
        selected_idx: Option<usize>,
    ) -> Self {
        let (primary, secondary) = app.resolve_agent_labels(agent);
        let pane_suffix = pane_suffixes[idx].clone();

        let is_sleeping = app.sleeping_pane_ids.contains(&agent.pane_id);
        let is_interrupted = app.interrupted_pane_ids.contains(&agent.pane_id);
        let is_stale = is_agent_stale(
            agent.status_ts,
            agent.status,
            now_secs,
            app.stale_threshold_secs,
            is_sleeping,
            is_interrupted,
        );
        let is_active = app.host_agent_idx == Some(idx);
        let is_selected = selected_idx == Some(idx);

        let (status_icon_spans, status_icon_style) =
            super::super::ui::status_icon_and_style(app, agent.status, is_stale);
        let status_color = status_icon_style.fg.unwrap_or(Color::Reset);

        let elapsed = agent
            .status_ts
            .map(|ts| format_compact_elapsed(now_secs.saturating_sub(ts)))
            .unwrap_or_default();

        let pane_title = build_pane_title(agent, &primary, &secondary, app.window_prefix());
        let git_status = app.git_statuses.get(&agent.path);
        let pr_summary = app.pr_statuses.get(&agent.path);
        let kind =
            effective_agent_kind(agent.agent_kind.as_deref(), agent.agent_command.as_deref());
        let agent_icon = resolve_agent_icon(kind, &app.agent_icons);
        let agent_icon_color = resolve_agent_icon_color(kind, &app.agent_icons);
        let agent_label = resolve_agent_label(kind);

        Self {
            agent,
            primary,
            secondary,
            pane_suffix,
            elapsed,
            status_icon_spans,
            status_color,
            pane_title,
            git_status,
            pr_summary,
            is_stale,
            is_active,
            is_selected,
            palette: &app.palette,
            agent_icon,
            agent_icon_color,
            agent_label,
            idx,
            spinner_frame: app.spinner_frame,
        }
    }

    /// Resolve a token to its display string.
    pub fn resolve(&self, token: TokenId) -> String {
        match token {
            TokenId::Primary => self.primary.clone(),
            TokenId::Secondary => self.secondary.clone(),
            TokenId::Worktree => self.worktree_name(),
            TokenId::Project => self.project_name(),
            TokenId::Session => self.agent.session.clone(),
            TokenId::Window => self.agent.window_name.clone(),
            TokenId::PaneTitle => self.pane_title.clone().unwrap_or_default(),
            TokenId::AgentLabel => self.agent_label.clone(),
            TokenId::StatusIcon => self
                .status_icon_spans
                .iter()
                .map(|(t, _)| t.clone())
                .collect(),
            TokenId::AgentIcon => self.agent_icon.clone(),
            TokenId::PaneSuffix => self.pane_suffix.clone(),
            TokenId::Elapsed => self.elapsed.clone(),
            TokenId::GitStats
            | TokenId::GitCommitted
            | TokenId::GitUncommitted
            | TokenId::GitRebase
            | TokenId::PrChecks => {
                // Span-rendered tokens: empty string at resolution time;
                // layout engine calls segment span helpers for rendering.
                String::new()
            }
            TokenId::GitAhead => self
                .git_status
                .filter(|s| s.has_upstream && s.ahead > 0)
                .map(|s| format!("\u{2191}{}", s.ahead))
                .unwrap_or_default(),
            TokenId::GitBehind => self
                .git_status
                .filter(|s| s.has_upstream && s.behind > 0)
                .map(|s| format!("\u{2193}{}", s.behind))
                .unwrap_or_default(),
            TokenId::GitDirty => match self.git_status {
                Some(s) if s.is_dirty => crate::nerdfont::git_icons().diff.to_string(),
                _ => String::new(),
            },
            TokenId::GitConflict => match self.git_status {
                Some(s) if s.has_conflict => crate::nerdfont::git_icons().conflict.to_string(),
                _ => String::new(),
            },
            TokenId::GitBranch => self
                .git_status
                .and_then(|s| s.branch.clone())
                .unwrap_or_default(),
            TokenId::PrNumber => self
                .pr_summary
                .map(|pr| format!("#{}", pr.number))
                .unwrap_or_default(),
            TokenId::StatusLabel => match self.agent.status {
                Some(AgentStatus::Working) => "Working".to_string(),
                Some(AgentStatus::Waiting) => "Waiting".to_string(),
                Some(AgentStatus::Done) => "Done".to_string(),
                None => String::new(),
            },
            TokenId::Idx => (self.idx + 1).to_string(),
            TokenId::JumpKey => {
                if self.idx < 9 {
                    format!("M-{}", self.idx + 1)
                } else {
                    String::new()
                }
            }
        }
    }

    /// Natural display width of a token's resolved text.
    pub fn natural_width(&self, token: TokenId) -> usize {
        match token {
            TokenId::StatusIcon => self
                .status_icon_spans
                .iter()
                .map(|(t, _)| display_width(t))
                .sum(),
            TokenId::AgentIcon => display_width(&self.agent_icon),
            TokenId::AgentLabel => display_width(&self.agent_label),
            TokenId::GitStats
            | TokenId::GitCommitted
            | TokenId::GitUncommitted
            | TokenId::GitRebase => {
                let (_, width) = self.git_segment_spans(token, usize::MAX);
                width
            }
            TokenId::PrChecks => {
                let (_, width) = self.pr_check_spans(usize::MAX);
                width
            }
            other => display_width(&self.resolve(other)),
        }
    }

    /// Render git stats with a given allocated width, returning styled spans and actual width.
    pub fn git_stats_spans(&self, allocated_width: usize) -> (Vec<(String, Style)>, usize) {
        match self.git_status {
            Some(status) => super::super::ui::format_sidebar_git_stats(
                Some(status),
                self.palette,
                self.is_stale,
                allocated_width,
            ),
            None => (Vec::new(), 0),
        }
    }

    /// Render one git segment token (composite or split) with self-fitting.
    pub fn git_segment_spans(
        &self,
        token: TokenId,
        allocated_width: usize,
    ) -> (Vec<(String, Style)>, usize) {
        match token {
            TokenId::GitStats => self.git_stats_spans(allocated_width),
            TokenId::GitCommitted => super::super::ui::format_committed_spans(
                self.git_status,
                self.palette,
                self.is_stale,
                allocated_width,
            ),
            TokenId::GitUncommitted => super::super::ui::format_uncommitted_spans(
                self.git_status,
                self.palette,
                self.is_stale,
                allocated_width,
            ),
            TokenId::GitRebase => super::super::ui::format_rebase_spans(
                self.git_status,
                self.palette,
                self.is_stale,
                allocated_width,
            ),
            _ => (Vec::new(), 0),
        }
    }

    /// Render PR checks with a given allocated width, returning styled spans and actual width.
    pub fn pr_check_spans(&self, allocated_width: usize) -> (Vec<(String, Style)>, usize) {
        super::super::ui::format_sidebar_pr_status(
            self.pr_summary,
            self.palette,
            self.is_stale,
            self.spinner_frame,
            allocated_width,
        )
    }

    /// Intrinsic style for a token (before state/selection post-pass).
    pub fn intrinsic_style(&self, token: TokenId) -> Style {
        if self.is_stale {
            return Style::default()
                .fg(self.palette.dimmed)
                .add_modifier(Modifier::DIM);
        }
        match token {
            TokenId::Primary if self.is_active => Style::default()
                .fg(self.palette.current_worktree_fg)
                .add_modifier(Modifier::BOLD),
            TokenId::Primary => Style::default().fg(self.palette.text),
            TokenId::Secondary => Style::default()
                .fg(self.palette.text)
                .add_modifier(Modifier::DIM),
            TokenId::PaneTitle => Style::default().fg(self.palette.dimmed),
            TokenId::PaneSuffix => Style::default().fg(self.palette.dimmed),
            TokenId::Elapsed => Style::default()
                .fg(self.palette.text)
                .add_modifier(Modifier::DIM),
            TokenId::AgentLabel => Style::default().fg(self.palette.text),
            TokenId::GitAhead => Style::default().fg(self.palette.success),
            TokenId::GitBehind => Style::default().fg(self.palette.danger),
            TokenId::GitDirty => Style::default().fg(self.palette.warning),
            TokenId::GitConflict => Style::default().fg(self.palette.danger),
            TokenId::GitBranch => Style::default().fg(self.palette.text),
            TokenId::PrNumber => self
                .pr_summary
                .map(|pr| crate::ui::pr_status::pr_state_icon_color(pr, self.palette).1)
                .map(|color| Style::default().fg(color))
                .unwrap_or_else(|| Style::default().fg(self.palette.text)),
            TokenId::StatusLabel => Style::default().fg(self.status_color),
            TokenId::Idx => Style::default().fg(self.palette.dimmed),
            TokenId::JumpKey => Style::default().fg(self.palette.dimmed),
            TokenId::AgentIcon => {
                let fg = self.agent_icon_color.unwrap_or(self.palette.text);
                Style::default().fg(fg)
            }
            _ => Style::default().fg(self.palette.text),
        }
    }

    fn worktree_name(&self) -> String {
        let (wt, _) = extract_worktree_name(
            &self.agent.session,
            &self.agent.window_name,
            "",
            &self.agent.path,
        );
        wt
    }

    fn project_name(&self) -> String {
        extract_project_name(&self.agent.path)
    }
}

fn resolve_agent_label(kind: Option<AgentKind>) -> String {
    match kind {
        Some(k) => k.default_label().to_string(),
        None => String::new(),
    }
}

fn resolve_agent_icon(kind: Option<AgentKind>, icons: &ResolvedAgentIcons) -> String {
    let Some(kind) = kind else {
        return String::new();
    };
    if let Some(icon) = icons.icons.get(kind.as_str()) {
        return icon.clone();
    }
    kind.default_icon().to_string()
}

fn resolve_agent_icon_color(kind: Option<AgentKind>, icons: &ResolvedAgentIcons) -> Option<Color> {
    let kind = kind?;
    match icons.colors.get(kind.as_str()) {
        Some(Some(c)) => Some(*c),
        Some(None) => None, // explicit opt-out via `color: ''`
        None => kind.default_color(),
    }
}

/// Prefer the cached classification; fall back to today's stem-based resolver.
///
/// A malformed, hand-edited, or future-version state file with an unknown
/// `agent_kind` falls through to the command-based resolver instead of
/// shadowing a perfectly good `agent_command` with a meaningless icon/label.
fn effective_agent_kind(
    agent_kind: Option<&str>,
    agent_command: Option<&str>,
) -> Option<AgentKind> {
    if let Some(kind) = agent_kind.and_then(AgentKind::from_str) {
        return Some(kind);
    }
    AgentKind::from_str(resolve_profile_for_display(agent_command).name())
}

fn build_pane_title(
    agent: &AgentPane,
    primary: &str,
    secondary: &str,
    window_prefix: &str,
) -> Option<String> {
    let title_worktree = extract_worktree_name(
        &agent.session,
        &agent.window_name,
        window_prefix,
        &agent.path,
    )
    .0;
    let title_project = extract_project_name(&agent.path);
    sanitize_pane_title(agent.pane_title.as_deref(), &title_worktree, &title_project)
        .filter(|t| *t != primary && *t != secondary)
        .map(|s| s.to_string())
}

fn is_agent_stale(
    status_ts: Option<u64>,
    status: Option<AgentStatus>,
    now_secs: u64,
    stale_threshold_secs: u64,
    is_sleeping: bool,
    is_interrupted: bool,
) -> bool {
    if is_sleeping {
        return true;
    }

    if !is_interrupted
        && matches!(
            status,
            Some(AgentStatus::Working) | Some(AgentStatus::Waiting)
        )
    {
        return false;
    }

    status_ts
        .map(|ts| now_secs.saturating_sub(ts) > stale_threshold_secs)
        .unwrap_or(true)
}

fn display_width(s: &str) -> usize {
    s.chars()
        .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(1))
        .sum()
}

fn format_compact_elapsed(secs: u64) -> String {
    if secs < 3600 {
        format!("{}:{:02}", secs / 60, secs % 60)
    } else if secs < 86400 {
        format!("{}h", secs / 3600)
    } else {
        format!("{}d", secs / 86400)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn kind(agent_kind: Option<&str>, agent_command: Option<&str>) -> Option<AgentKind> {
        effective_agent_kind(agent_kind, agent_command)
    }

    #[test]
    fn missing_status_timestamp_is_stale() {
        assert!(is_agent_stale(None, None, 100, 60, false, false));
    }

    #[test]
    fn active_statuses_are_not_stale_without_timestamp() {
        assert!(!is_agent_stale(
            None,
            Some(AgentStatus::Working),
            100,
            60,
            false,
            false
        ));
        assert!(!is_agent_stale(
            None,
            Some(AgentStatus::Waiting),
            100,
            60,
            false,
            false
        ));
    }

    #[test]
    fn sleeping_agent_is_stale_even_with_active_status() {
        assert!(is_agent_stale(
            Some(100),
            Some(AgentStatus::Working),
            100,
            60,
            true,
            false
        ));
    }

    #[test]
    fn cached_kind_resolves_label_without_command() {
        // Command is a version string the stem-based resolver can't classify;
        // the cached kind must drive label/icon.
        assert_eq!(
            resolve_agent_label(kind(Some("claude"), Some("2.1.118"))),
            "Claude"
        );
    }

    #[test]
    fn cached_kind_renders_friendly_kiro_label() {
        assert_eq!(resolve_agent_label(kind(Some("kiro-cli"), None)), "Kiro");
    }

    #[test]
    fn cached_kind_renders_friendly_opencode_label() {
        assert_eq!(
            resolve_agent_label(kind(Some("opencode"), None)),
            "OpenCode"
        );
    }

    #[test]
    fn unknown_cached_kind_falls_back_to_command() {
        // Defensive: malformed cache must not shadow a valid agent_command.
        let icons = ResolvedAgentIcons::default();
        assert_eq!(
            resolve_agent_label(kind(Some("not-a-profile"), Some("claude"))),
            "Claude"
        );
        assert_eq!(
            resolve_agent_icon(kind(Some("not-a-profile"), Some("claude")), &icons),
            "CC"
        );
    }

    #[test]
    fn no_cache_falls_back_to_today_behavior() {
        let icons = ResolvedAgentIcons::default();
        assert_eq!(resolve_agent_label(kind(None, Some("gemini"))), "Gemini");
        assert_eq!(resolve_agent_icon(kind(None, Some("gemini")), &icons), "G");
    }

    #[test]
    fn custom_icon_override_still_honored_with_cached_kind() {
        let mut icons = ResolvedAgentIcons::default();
        icons.icons.insert("claude".to_string(), "X".to_string());
        assert_eq!(
            resolve_agent_icon(kind(Some("claude"), Some("2.1.118")), &icons),
            "X"
        );
    }

    use crate::config::{ThemeMode, ThemeScheme};
    use crate::github::{CheckState, PrSummary};
    use crate::multiplexer::AgentPane;
    use std::path::PathBuf;

    fn test_palette() -> &'static ThemePalette {
        Box::leak(Box::new(ThemePalette::for_scheme(
            ThemeScheme::Default,
            ThemeMode::Dark,
        )))
    }

    fn test_agent() -> AgentPane {
        AgentPane {
            session: "s".to_string(),
            window_name: "w".to_string(),
            pane_id: "%1".to_string(),
            window_id: "@1".to_string(),
            path: PathBuf::from("/tmp/x"),
            pane_title: None,
            status: None,
            status_ts: None,
            updated_ts: None,
            window_cmd: None,
            agent_command: None,
            agent_kind: None,
        }
    }

    fn make_context<'a>(
        agent: &'a AgentPane,
        git: Option<&'a GitStatus>,
        idx: usize,
    ) -> RowContext<'a> {
        make_context_with_pr(agent, git, None, idx)
    }

    fn make_context_with_pr<'a>(
        agent: &'a AgentPane,
        git: Option<&'a GitStatus>,
        pr: Option<&'a PrSummary>,
        idx: usize,
    ) -> RowContext<'a> {
        RowContext {
            agent,
            primary: String::new(),
            secondary: String::new(),
            pane_suffix: String::new(),
            elapsed: String::new(),
            status_icon_spans: vec![],
            status_color: Color::Reset,
            pane_title: None,
            git_status: git,
            pr_summary: pr,
            is_stale: false,
            is_active: false,
            is_selected: false,
            palette: test_palette(),
            agent_icon: String::new(),
            agent_icon_color: None,
            agent_label: String::new(),
            idx,
            spinner_frame: 0,
        }
    }

    #[test]
    fn interrupted_active_agent_can_become_stale() {
        assert!(is_agent_stale(
            Some(100),
            Some(AgentStatus::Working),
            500,
            60,
            false,
            true
        ));
        assert!(!is_agent_stale(
            Some(480),
            Some(AgentStatus::Working),
            500,
            60,
            false,
            true
        ));
    }

    #[test]
    fn resolve_idx_is_one_based() {
        let agent = test_agent();
        let ctx0 = make_context(&agent, None, 0);
        let ctx9 = make_context(&agent, None, 9);
        assert_eq!(ctx0.resolve(TokenId::Idx), "1");
        assert_eq!(ctx9.resolve(TokenId::Idx), "10");
    }

    #[test]
    fn resolve_jump_key_caps_at_nine() {
        let agent = test_agent();
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::JumpKey),
            "M-1"
        );
        assert_eq!(
            make_context(&agent, None, 8).resolve(TokenId::JumpKey),
            "M-9"
        );
        assert_eq!(make_context(&agent, None, 9).resolve(TokenId::JumpKey), "");
    }

    #[test]
    fn resolve_status_label_capitalised() {
        let mut agent = test_agent();
        agent.status = Some(AgentStatus::Working);
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::StatusLabel),
            "Working"
        );
        agent.status = Some(AgentStatus::Waiting);
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::StatusLabel),
            "Waiting"
        );
        agent.status = Some(AgentStatus::Done);
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::StatusLabel),
            "Done"
        );
        agent.status = None;
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::StatusLabel),
            ""
        );
    }

    #[test]
    fn resolve_git_ahead_behind() {
        let agent = test_agent();
        // No git status -> empty.
        assert_eq!(make_context(&agent, None, 0).resolve(TokenId::GitAhead), "");
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::GitBehind),
            ""
        );

        let mut status = GitStatus::default();
        status.has_upstream = true;
        status.ahead = 3;
        status.behind = 0;
        let ctx = make_context(&agent, Some(&status), 0);
        assert_eq!(ctx.resolve(TokenId::GitAhead), "\u{2191}3");
        assert_eq!(ctx.resolve(TokenId::GitBehind), "");

        let mut status = GitStatus::default();
        status.has_upstream = true;
        status.ahead = 0;
        status.behind = 5;
        let ctx = make_context(&agent, Some(&status), 0);
        assert_eq!(ctx.resolve(TokenId::GitAhead), "");
        assert_eq!(ctx.resolve(TokenId::GitBehind), "\u{2193}5");

        // No upstream: even nonzero counts collapse to empty.
        let mut status = GitStatus::default();
        status.has_upstream = false;
        status.ahead = 3;
        status.behind = 5;
        let ctx = make_context(&agent, Some(&status), 0);
        assert_eq!(ctx.resolve(TokenId::GitAhead), "");
        assert_eq!(ctx.resolve(TokenId::GitBehind), "");
    }

    #[test]
    fn resolve_git_dirty_conflict_glyphs() {
        let agent = test_agent();
        let icons = crate::nerdfont::git_icons();

        let clean = GitStatus::default();
        let ctx = make_context(&agent, Some(&clean), 0);
        assert_eq!(ctx.resolve(TokenId::GitDirty), "");
        assert_eq!(ctx.resolve(TokenId::GitConflict), "");

        let mut dirty = GitStatus::default();
        dirty.is_dirty = true;
        dirty.has_conflict = true;
        let ctx = make_context(&agent, Some(&dirty), 0);
        assert_eq!(ctx.resolve(TokenId::GitDirty), icons.diff);
        assert_eq!(ctx.resolve(TokenId::GitConflict), icons.conflict);
    }

    #[test]
    fn resolve_git_branch() {
        let agent = test_agent();
        let mut status = GitStatus::default();
        status.branch = Some("feature/x".to_string());
        assert_eq!(
            make_context(&agent, Some(&status), 0).resolve(TokenId::GitBranch),
            "feature/x"
        );
        // Detached HEAD -> empty.
        let detached = GitStatus::default();
        assert_eq!(
            make_context(&agent, Some(&detached), 0).resolve(TokenId::GitBranch),
            ""
        );
        // No git status -> empty.
        assert_eq!(
            make_context(&agent, None, 0).resolve(TokenId::GitBranch),
            ""
        );
    }

    #[test]
    fn resolve_pr_number_and_status() {
        let agent = test_agent();
        let pr = PrSummary {
            number: 123,
            title: "Add thing".to_string(),
            state: "OPEN".to_string(),
            is_draft: false,
            checks: Some(CheckState::Success),
            check_meta: None,
            url: None,
        };
        let ctx = make_context_with_pr(&agent, None, Some(&pr), 0);
        assert_eq!(ctx.resolve(TokenId::PrNumber), "#123");
        assert_eq!(ctx.resolve(TokenId::PrChecks), "");
        assert!(ctx.natural_width(TokenId::PrChecks) > 0);
    }

    #[test]
    fn resolve_pr_tokens_empty_without_pr() {
        let agent = test_agent();
        let ctx = make_context(&agent, None, 0);
        assert_eq!(ctx.resolve(TokenId::PrNumber), "");
        assert_eq!(ctx.natural_width(TokenId::PrChecks), 0);
    }

    #[test]
    fn intrinsic_style_assigns_palette_colors() {
        let agent = test_agent();
        let ctx = make_context(&agent, None, 0);
        let palette = ctx.palette;
        assert_eq!(
            ctx.intrinsic_style(TokenId::GitAhead).fg,
            Some(palette.success)
        );
        assert_eq!(
            ctx.intrinsic_style(TokenId::GitBehind).fg,
            Some(palette.danger)
        );
        assert_eq!(
            ctx.intrinsic_style(TokenId::GitConflict).fg,
            Some(palette.danger)
        );
        assert_eq!(
            ctx.intrinsic_style(TokenId::GitDirty).fg,
            Some(palette.warning)
        );
        assert_eq!(ctx.intrinsic_style(TokenId::Idx).fg, Some(palette.dimmed));
        assert_eq!(
            ctx.intrinsic_style(TokenId::JumpKey).fg,
            Some(palette.dimmed)
        );
    }

    #[test]
    fn intrinsic_style_dims_when_stale() {
        let agent = test_agent();
        let mut ctx = make_context(&agent, None, 0);
        ctx.is_stale = true;
        let palette = ctx.palette;
        assert_eq!(
            ctx.intrinsic_style(TokenId::GitAhead).fg,
            Some(palette.dimmed)
        );
        assert_eq!(
            ctx.intrinsic_style(TokenId::StatusLabel).fg,
            Some(palette.dimmed)
        );
    }

    #[test]
    fn default_color_for_claude_is_brand_orange() {
        let icons = ResolvedAgentIcons::default();
        assert_eq!(
            resolve_agent_icon_color(kind(Some("claude"), None), &icons),
            Some(Color::Rgb(0xd9, 0x77, 0x57))
        );
    }

    #[test]
    fn user_color_override_wins_over_default() {
        let mut icons = ResolvedAgentIcons::default();
        icons
            .colors
            .insert("claude".to_string(), Some(Color::Rgb(0, 255, 0)));
        assert_eq!(
            resolve_agent_icon_color(kind(Some("claude"), None), &icons),
            Some(Color::Rgb(0, 255, 0))
        );
    }

    #[test]
    fn explicit_empty_color_disables_default() {
        let mut icons = ResolvedAgentIcons::default();
        icons.colors.insert("claude".to_string(), None);
        assert_eq!(
            resolve_agent_icon_color(kind(Some("claude"), None), &icons),
            None
        );
    }

    #[test]
    fn unknown_agent_has_no_color() {
        let icons = ResolvedAgentIcons::default();
        assert_eq!(resolve_agent_icon_color(None, &icons), None);
    }
}