rab-agent 0.1.0

rab is a lightweight, extensible, Rust-based coding agent.
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
use crate::agent::types::Usage;
use crate::agent::ui::messages::pad_to_width;
use crate::agent::ui::theme::RabTheme;
use crate::tui::util::{truncate_to_width, visible_width};

// ── Helpers matching pi's footer.ts ──────────────────────────────

/// Sanitize text for display in a single-line status.
/// Removes newlines, tabs, carriage returns, and other control characters.
fn sanitize_status_text(text: &str) -> String {
    text.replace(['\r', '\n', '\t'], " ")
        .split(' ')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Format token count for compact footer display (pi-style).
pub fn format_tokens(count: u64) -> String {
    if count < 1000 {
        return count.to_string();
    }
    if count < 10000 {
        return format!("{:.1}k", count as f64 / 1000.0);
    }
    if count < 1_000_000 {
        return format!("{}k", (count as f64 / 1000.0).round() as u64);
    }
    if count < 10_000_000 {
        return format!("{:.1}M", count as f64 / 1_000_000.0);
    }
    format!("{}M", (count as f64 / 1_000_000.0).round() as u64)
}

/// Format cwd for footer display (pi-style `formatCwdForFooter`).
/// Resolves cwd relative to home directory, using `~` prefix.
pub fn format_cwd_for_footer(cwd: &str, home: Option<&str>) -> String {
    let home = match home {
        Some(h) => h,
        None => return cwd.to_string(),
    };

    let resolved_cwd = std::path::Path::new(cwd);
    let resolved_home = std::path::Path::new(home);

    // Try to make relative
    let relative = match resolved_cwd.strip_prefix(resolved_home) {
        Ok(rest) => {
            if rest.as_os_str().is_empty() {
                // cwd IS home
                return "~".to_string();
            }
            rest.to_string_lossy().to_string()
        }
        Err(_) => return cwd.to_string(),
    };

    format!("~/{}", relative)
}

// ── Footer Component ─────────────────────────────────────────────

/// Pi-style footer: 2-3 lines with dim styling.
/// Matches pi's `FooterComponent` in `footer.ts` exactly.
pub struct Footer {
    cwd: String,
    git_branch: Option<String>,
    session_name: Option<String>,

    /// Pre-computed stats (pi computes fresh from session entries each render).
    total_input: u64,
    total_output: u64,
    total_cache_read: u64,
    total_cache_write: u64,
    total_cost: f64,
    latest_cache_hit_rate: Option<f64>,

    context_percent: Option<f64>,
    context_window: u64,

    auto_compact: bool,

    model: String,
    /// Whether model supports reasoning (for showing thinking level).
    model_supports_reasoning: bool,
    thinking_level: Option<String>,

    /// Number of unique providers with available models (for footer display).
    available_provider_count: usize,

    /// Whether using OAuth subscription (shows "(sub)" after cost).
    using_subscription: bool,

    /// Experimental features enabled.
    experimental_enabled: bool,

    pub extension_statuses: Vec<(String, String)>, // (key, text) sorted by key

    theme: RabTheme,
}

impl Footer {
    pub fn new(cwd: impl Into<String>) -> Self {
        let theme = crate::agent::ui::theme::current_theme().clone();
        Self {
            cwd: cwd.into(),
            git_branch: None,
            session_name: None,
            total_input: 0,
            total_output: 0,
            total_cache_read: 0,
            total_cache_write: 0,
            total_cost: 0.0,
            latest_cache_hit_rate: None,
            context_percent: None,
            context_window: 0,
            auto_compact: true,
            model: String::new(),
            model_supports_reasoning: false,
            thinking_level: None,
            available_provider_count: 1,
            using_subscription: false,
            experimental_enabled: false,
            extension_statuses: Vec::new(),
            theme,
        }
    }

    // ── Setters (called from App) ──

    pub fn set_cwd(&mut self, cwd: impl Into<String>) {
        self.cwd = cwd.into();
    }

    pub fn set_git_branch(&mut self, branch: Option<String>) {
        self.git_branch = branch;
    }

    pub fn set_session_name(&mut self, name: Option<String>) {
        self.session_name = name;
    }

    pub fn set_model(&mut self, model: impl Into<String>) {
        self.model = model.into();
    }

    /// Set whether the model supports reasoning (for showing thinking level in footer).
    pub fn set_model_supports_reasoning(&mut self, supports: bool) {
        self.model_supports_reasoning = supports;
    }

    pub fn set_thinking_level(&mut self, level: Option<String>) {
        self.thinking_level = level;
    }

    pub fn set_auto_compact(&mut self, enabled: bool) {
        self.auto_compact = enabled;
    }

    pub fn set_available_provider_count(&mut self, count: usize) {
        self.available_provider_count = count;
    }

    pub fn set_using_subscription(&mut self, using: bool) {
        self.using_subscription = using;
    }

    pub fn set_experimental_enabled(&mut self, enabled: bool) {
        self.experimental_enabled = enabled;
    }

    /// Pi-style: accumulate usage from a single response's usage data.
    pub fn accumulate_usage(&mut self, usage: &Usage) {
        let input = usage.input_tokens.unwrap_or(0) as u64;
        let output = usage.output_tokens.unwrap_or(0) as u64;
        let cache_read = usage.cache_tokens.unwrap_or(0) as u64;

        self.total_input += input;
        self.total_output += output;
        self.total_cache_read += cache_read;

        // Compute cache hit rate from latest call
        let total_prompt = input + cache_read;
        if total_prompt > 0 {
            self.latest_cache_hit_rate = Some((cache_read as f64 / total_prompt as f64) * 100.0);
        }
    }

    /// Pi-style: set cumulative usage directly (replaces accumulated values).
    pub fn set_usage(
        &mut self,
        total_input: u64,
        total_output: u64,
        total_cache_read: u64,
        total_cache_write: u64,
        total_cost: f64,
        latest_cache_hit_rate: Option<f64>,
    ) {
        self.total_input = total_input;
        self.total_output = total_output;
        self.total_cache_read = total_cache_read;
        self.total_cache_write = total_cache_write;
        self.total_cost = total_cost;
        self.latest_cache_hit_rate = latest_cache_hit_rate;
    }

    /// Pi-style: cost is set separately (from usage.cost.total).
    pub fn set_cost(&mut self, cost: f64) {
        self.total_cost = cost;
    }

    /// Set cache write tokens separately.
    pub fn set_cache_write(&mut self, cache_write: u64) {
        self.total_cache_write = cache_write;
    }

    /// Pi-style: no streaming dot indicator in footer (handled by working indicator).
    /// Kept for compatibility with existing call sites.
    pub fn set_streaming(&mut self, _streaming: bool) {
        // No-op: pi footer doesn't show streaming dot
    }

    /// Pi-style set context / context window.
    pub fn set_context(&mut self, percent: Option<f64>, window: u64) {
        self.context_percent = percent;
        self.context_window = window;
    }

    /// Set an extension status (pi-style, key-value pair).
    pub fn set_extension_status(&mut self, key: String, text: Option<String>) {
        if let Some(text) = text {
            // Update existing or insert
            if let Some(pos) = self.extension_statuses.iter().position(|(k, _)| k == &key) {
                self.extension_statuses[pos].1 = text;
            } else {
                self.extension_statuses.push((key, text));
            }
        } else {
            // Remove
            self.extension_statuses.retain(|(k, _)| k != &key);
        }
        // Keep sorted by key (pi-style)
        self.extension_statuses.sort_by(|(a, _), (b, _)| a.cmp(b));
    }

    /// Clear all extension statuses (pi-style).
    pub fn clear_extension_statuses(&mut self) {
        self.extension_statuses.clear();
    }
}

impl crate::tui::Component for Footer {
    fn render(&self, width: usize) -> Vec<String> {
        let w = width;
        if w < 4 {
            return vec![]; // Too narrow to show anything
        }

        let theme = &self.theme;

        // ── Line 1: pwd (git branch) • session-name ──
        let home = std::env::var("HOME").ok();
        let mut pwd = format_cwd_for_footer(&self.cwd, home.as_deref());

        if let Some(ref branch) = self.git_branch {
            pwd = format!("{} ({})", pwd, branch);
        }
        if let Some(ref name) = self.session_name {
            pwd = format!("{}{}", pwd, name);
        }
        let pwd_line = truncate_to_width(&theme.fg("dim", &pwd), w, &theme.fg("dim", "..."), true);
        let pwd_line = if pwd_line.is_empty() {
            String::new()
        } else {
            pad_to_width(&pwd_line, w)
        };

        // ── Line 2: stats left, model right (both dimmed separately) ──
        // Build stats parts (pi-style order and content)
        let mut stats_parts: Vec<String> = Vec::new();

        if self.total_input > 0 {
            stats_parts.push(format!("{}", format_tokens(self.total_input)));
        }
        if self.total_output > 0 {
            stats_parts.push(format!("{}", format_tokens(self.total_output)));
        }
        if self.total_cache_read > 0 {
            stats_parts.push(format!("R{}", format_tokens(self.total_cache_read)));
        }
        if self.total_cache_write > 0 {
            stats_parts.push(format!("W{}", format_tokens(self.total_cache_write)));
        }
        if (self.total_cache_read > 0 || self.total_cache_write > 0)
            && let Some(hit_rate) = self.latest_cache_hit_rate
        {
            stats_parts.push(format!("CH{:.1}%", hit_rate));
        }

        // Cost with optional "(sub)" indicator (pi-style)
        if self.total_cost > 0.0 || self.using_subscription {
            let cost_str = if self.using_subscription {
                format!("${:.3} (sub)", self.total_cost)
            } else {
                format!("${:.3}", self.total_cost)
            };
            stats_parts.push(cost_str);
        }

        // Context percentage with color (pi-style: red > 90, yellow > 70)
        let context_percent_str = match self.context_percent {
            Some(p) => {
                let window_str = format_tokens(self.context_window);
                let display = if self.auto_compact {
                    format!("{:.1}%/{} (auto)", p, window_str)
                } else {
                    format!("{:.1}%/{}", p, window_str)
                };
                if p > 90.0 {
                    theme.fg("error", &display)
                } else if p > 70.0 {
                    theme.fg("warning", &display)
                } else {
                    display
                }
            }
            None => {
                let window_str = format_tokens(self.context_window);
                if self.auto_compact {
                    format!("?/{} (auto)", window_str)
                } else {
                    format!("?/{}", window_str)
                }
            }
        };
        if !context_percent_str.is_empty() {
            stats_parts.push(context_percent_str);
        }

        // Experimental features indicator (pi-style)
        if self.experimental_enabled {
            stats_parts.push(format!(
                "{} {}",
                theme.fg("dim", ""),
                theme.bold(&theme.fg("warning", "xp"))
            ));
        }

        let mut stats_left = stats_parts.join(" ");

        // Build right side: model name + thinking level (pi-style)
        let model_name = if self.model.is_empty() {
            "no-model".to_string()
        } else {
            self.model
                .strip_prefix("opencode_go::")
                .unwrap_or(&self.model)
                .to_string()
        };

        // Pi-style right side with thinking level indicator
        let right_side_without_provider = if self.model_supports_reasoning {
            match &self.thinking_level {
                Some(level) if level != "off" => format!("{}{}", model_name, level),
                _ => format!("{} • thinking off", model_name),
            }
        } else {
            model_name.clone()
        };

        // Prepend provider in parentheses if multiple providers (pi-style)
        let right_side = if self.available_provider_count > 1 && !self.model.is_empty() {
            let model_with_provider = format!("(?) {}", right_side_without_provider);
            // Only use provider prefix if it fits (checked below)
            model_with_provider
        } else {
            right_side_without_provider.clone()
        };

        // Compute widths and layout (pi-style)
        let mut stats_left_width = visible_width(&stats_left);

        // Pi-style: if statsLeft is too wide, truncate it
        if stats_left_width > w {
            stats_left = truncate_to_width(&stats_left, w, "", true);
            stats_left_width = visible_width(&stats_left);
        }

        let right_side_width = visible_width(&right_side);
        let min_padding: usize = 2;

        let stats_line = if stats_left_width + min_padding + right_side_width <= w {
            // Both fit
            let padding = " ".repeat(w - stats_left_width - right_side_width);
            format!("{}{}{}", stats_left, padding, right_side)
        } else if !self.model.is_empty()
            && self.available_provider_count > 1
            && stats_left_width + min_padding + visible_width(&right_side_without_provider) <= w
        {
            // Try without provider prefix
            let padding =
                " ".repeat(w - stats_left_width - visible_width(&right_side_without_provider));
            format!("{}{}{}", stats_left, padding, right_side_without_provider)
        } else {
            // Need to truncate right side
            let available_for_right = w.saturating_sub(stats_left_width + min_padding);
            if available_for_right > 0 {
                let truncated_right = truncate_to_width(&right_side, available_for_right, "", true);
                let truncated_right_width = visible_width(&truncated_right);
                let padding = " ".repeat(w - stats_left_width - truncated_right_width);
                format!("{}{}{}", stats_left, padding, truncated_right)
            } else {
                // Not enough space for right side at all
                stats_left.clone()
            }
        };

        // Pi-style: dim statsLeft and remainder separately (statsLeft may contain colored context %)
        let dim_stats_left = theme.fg("dim", &stats_left);
        let remainder = &stats_line[stats_left.len()..]; // padding + rightSide
        let dim_remainder = theme.fg("dim", remainder);

        let stats_line_formatted = format!("{}{}", dim_stats_left, dim_remainder);

        let mut lines = vec![pwd_line, stats_line_formatted];

        // ── Line 3: extension statuses (sorted by key, sanitized) ──
        if !self.extension_statuses.is_empty() {
            let status_text: Vec<String> = self
                .extension_statuses
                .iter()
                .map(|(_, text)| sanitize_status_text(text))
                .collect();
            let status_line = status_text.join(" ");
            let truncated = truncate_to_width(&status_line, w, &theme.fg("dim", "..."), true);
            let status_line = if truncated.is_empty() {
                String::new()
            } else {
                pad_to_width(&truncated, w)
            };
            if !status_line.trim().is_empty() {
                lines.push(status_line);
            }
        }

        lines
    }

    fn invalidate(&mut self) {
        // No cached state to invalidate
    }
}

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

    fn make_footer() -> Footer {
        crate::agent::ui::theme::init_theme(Some("dark"), false);
        let mut footer = Footer::new("/home/user/project");
        footer.set_model("test-model");
        footer.set_git_branch(Some("main".into()));
        footer
    }

    // ── format_cwd_for_footer tests ──

    #[test]
    fn test_format_cwd_home() {
        let result = format_cwd_for_footer("/home/user/project", Some("/home/user"));
        assert_eq!(result, "~/project");
    }

    #[test]
    fn test_format_cwd_home_exact() {
        let result = format_cwd_for_footer("/home/user", Some("/home/user"));
        assert_eq!(result, "~");
    }

    #[test]
    fn test_format_cwd_outside_home() {
        let result = format_cwd_for_footer("/opt/app", Some("/home/user"));
        assert_eq!(result, "/opt/app");
    }

    #[test]
    fn test_format_cwd_no_home() {
        let result = format_cwd_for_footer("/some/path", None::<&str>);
        assert_eq!(result, "/some/path");
    }

    // ── format_tokens tests ──

    #[test]
    fn test_format_tokens_under_1k() {
        assert_eq!(format_tokens(500), "500");
    }

    #[test]
    fn test_format_tokens_1k_to_10k() {
        assert_eq!(format_tokens(5500), "5.5k");
    }

    #[test]
    fn test_format_tokens_10k_to_1m() {
        assert_eq!(format_tokens(55500), "56k");
    }

    #[test]
    fn test_format_tokens_1m_to_10m() {
        assert_eq!(format_tokens(5_500_000), "5.5M");
    }

    #[test]
    fn test_format_tokens_over_10m() {
        assert_eq!(format_tokens(55_000_000), "55M");
    }

    // ── sanitize_status_text tests ──

    #[test]
    fn test_sanitize_status() {
        assert_eq!(sanitize_status_text("hello\nworld"), "hello world");
        assert_eq!(sanitize_status_text("hello\tworld"), "hello world");
        assert_eq!(sanitize_status_text("hello\r\nworld"), "hello world");
        assert_eq!(sanitize_status_text("  spaced  "), "spaced");
    }

    // ── Line 1 (cwd) tests ──

    #[test]
    fn test_footer_shows_cwd() {
        let footer = make_footer();
        let lines = footer.render(80);
        assert!(lines.len() >= 2, "Should have at least 2 lines");
        assert!(lines[0].contains("project"), "Should show cwd");
    }

    #[test]
    fn test_footer_shows_git_branch() {
        let footer = make_footer();
        let lines = footer.render(80);
        assert!(lines[0].contains("main"), "Should show git branch");
    }

    #[test]
    fn test_footer_shows_session_name() {
        let mut footer = make_footer();
        footer.set_session_name(Some("my-session".into()));
        let lines = footer.render(80);
        assert!(lines[0].contains("my-session"), "Should show session name");
    }

    #[test]
    fn test_cwd_truncated_to_width() {
        let mut footer = Footer::new("/very/long/path/that/exceeds/available/width/completely");
        footer.set_model("model");
        let lines = footer.render(30);
        assert!(lines.len() >= 2);
        for line in &lines {
            assert!(
                visible_width(line) <= 30,
                "Line '{}' exceeds width 30",
                line
            );
        }
    }

    // ── Line 2 (stats/model) tests ──

    #[test]
    fn test_footer_shows_model() {
        let footer = make_footer();
        let lines = footer.render(80);
        assert!(lines[1].contains("test-model"), "Should show model name");
    }

    #[test]
    fn test_footer_shows_no_model() {
        let mut footer = Footer::new("/path");
        footer.set_model("");
        let lines = footer.render(80);
        assert!(
            lines[1].contains("no-model"),
            "Should show 'no-model' when model not set"
        );
    }

    #[test]
    fn test_footer_shows_thinking_level() {
        let mut footer = make_footer();
        footer.set_model_supports_reasoning(true);
        footer.set_thinking_level(Some("high".into()));
        let lines = footer.render(80);
        assert!(lines[1].contains("high"), "Should show thinking level");
    }

    #[test]
    fn test_footer_thinking_off_with_reasoning() {
        let mut footer = make_footer();
        footer.set_model_supports_reasoning(true);
        footer.set_thinking_level(Some("off".into()));
        let lines = footer.render(80);
        assert!(
            lines[1].contains("thinking off"),
            "Should show 'thinking off' when reasoning model has level off"
        );
    }

    #[test]
    fn test_footer_shows_token_usage() {
        let mut footer = make_footer();
        let usage = Usage {
            input_tokens: Some(1500),
            output_tokens: Some(500),
            cache_tokens: None,
        };
        footer.accumulate_usage(&usage);
        let lines = footer.render(80);
        assert!(lines[1].contains(""), "Should show input tokens");
        assert!(lines[1].contains(""), "Should show output tokens");
    }

    #[test]
    fn test_footer_usage_multiple_calls() {
        let mut footer = make_footer();
        let u1 = Usage {
            input_tokens: Some(1000),
            output_tokens: Some(500),
            cache_tokens: None,
        };
        footer.accumulate_usage(&u1);
        let u2 = Usage {
            input_tokens: Some(2000),
            output_tokens: Some(300),
            cache_tokens: None,
        };
        footer.accumulate_usage(&u2);
        let lines = footer.render(80);
        assert!(lines[1].contains("↑3.0k"), "Should show accumulated input");
        assert!(lines[1].contains("↓800"), "Should show accumulated output");
    }

    #[test]
    fn test_footer_shows_cache_hit_rate() {
        let mut footer = make_footer();
        let usage = Usage {
            input_tokens: Some(1000),
            output_tokens: Some(500),
            cache_tokens: Some(200),
        };
        footer.accumulate_usage(&usage);
        let lines = footer.render(80);
        assert!(
            lines[1].contains("CH"),
            "Should show cache hit rate when cache tokens present"
        );
        // 200 / (1000 + 200) = 16.7%
        assert!(
            lines[1].contains("CH16.7%"),
            "Should show correct cache hit rate"
        );
    }

    #[test]
    fn test_footer_shows_cost() {
        let mut footer = make_footer();
        footer.set_cost(0.0123);
        let lines = footer.render(80);
        assert!(lines[1].contains("$0.012"), "Should show cost");
    }

    #[test]
    fn test_footer_shows_subscription_indicator() {
        let mut footer = make_footer();
        footer.set_cost(0.0);
        footer.set_using_subscription(true);
        let lines = footer.render(80);
        assert!(
            lines[1].contains("(sub)"),
            "Should show subscription indicator"
        );
    }

    // ── Auto-compact indicator tests ──

    #[test]
    fn test_footer_shows_auto_compact_next_to_context() {
        let mut footer = make_footer();
        footer.set_auto_compact(true);
        footer.set_context(Some(50.0), 128000);
        let lines = footer.render(80);
        assert!(
            lines[1].contains("(auto)"),
            "Should show (auto) next to context percentage"
        );
        assert!(
            lines[1].contains("50.0%/128k (auto)"),
            "Should show context percent with auto compact"
        );
    }

    #[test]
    fn test_footer_hides_auto_compact_when_disabled() {
        let mut footer = make_footer();
        footer.set_auto_compact(false);
        footer.set_context(Some(50.0), 128000);
        let lines = footer.render(80);
        assert!(
            !lines[1].contains("(auto)"),
            "Should NOT show (auto) when disabled"
        );
    }

    // ── Context percent colors ──

    #[test]
    fn test_footer_context_percent_high() {
        let mut footer = make_footer();
        footer.set_context(Some(95.0), 128000);
        let lines = footer.render(80);
        assert!(lines[1].contains("95"), "Should show context percent");
        assert!(
            lines[1].contains("128k"),
            "Should show formatted window size"
        );
        // High context should be in error color (wrapped in ANSI escape)
        assert!(
            lines[1].contains("\x1b[38;2;"),
            "Should have ANSI color for high context"
        );
    }

    #[test]
    fn test_footer_context_without_percent() {
        let mut footer = make_footer();
        footer.set_context(None, 64000);
        let lines = footer.render(80);
        assert!(lines[1].contains("?"), "Should show unknown context");
        assert!(lines[1].contains("64k"), "Should show context window size");
    }

    // ── Extension status line tests ──

    #[test]
    fn test_footer_shows_extension_statuses() {
        let mut footer = make_footer();
        footer.set_extension_status("ext1".into(), Some("ready".into()));
        let lines = footer.render(80);
        assert!(lines.len() >= 3, "Should have 3 lines");
        assert!(lines[2].contains("ready"), "Should show extension status");
    }

    #[test]
    fn test_footer_extension_status_sorted() {
        let mut footer = make_footer();
        footer.set_extension_status("z_last".into(), Some("last".into()));
        footer.set_extension_status("a_first".into(), Some("first".into()));
        let lines = footer.render(80);
        if lines.len() >= 3 {
            let first_idx = lines[2].find("first");
            let last_idx = lines[2].find("last");
            assert!(
                first_idx < last_idx,
                "Extension statuses should be sorted by key"
            );
        }
    }

    #[test]
    fn test_footer_extension_status_sanitized() {
        let mut footer = make_footer();
        footer.set_extension_status("ext1".into(), Some("hello\nworld\ttab".into()));
        let lines = footer.render(80);
        if lines.len() >= 3 {
            assert!(
                !lines[2].contains('\n'),
                "Extension status should not contain newlines"
            );
            assert!(
                !lines[2].contains('\t'),
                "Extension status should not contain tabs"
            );
        }
    }

    #[test]
    fn test_footer_extension_status_removed() {
        let mut footer = make_footer();
        footer.set_extension_status("ext1".into(), Some("ready".into()));
        footer.set_extension_status("ext1".into(), None);
        let lines = footer.render(80);
        assert!(
            lines.len() < 3 || !lines[2].contains("ready"),
            "Extension status should be removed"
        );
    }

    // ── Narrow terminal tests ──

    #[test]
    fn test_footer_handles_narrow_terminal() {
        let mut footer = make_footer();
        footer.set_model_supports_reasoning(true);
        footer.set_thinking_level(Some("high".into()));
        let usage = Usage {
            input_tokens: Some(100000),
            output_tokens: Some(50000),
            cache_tokens: Some(10000),
        };
        footer.accumulate_usage(&usage);
        footer.set_context(Some(45.0), 128000);
        let lines = footer.render(10);
        assert!(!lines.is_empty(), "Should render even at width 10");
        for line in &lines {
            assert!(
                visible_width(line) <= 10,
                "Line '{}' exceeds width 10",
                line
            );
        }
    }

    #[test]
    fn test_footer_handles_very_narrow_terminal() {
        let footer = make_footer();
        let lines = footer.render(3);
        assert!(lines.is_empty(), "Should return empty at width 3");
    }

    #[test]
    fn test_footer_stats_not_truncated_when_room() {
        let mut footer = make_footer();
        let usage = Usage {
            input_tokens: Some(100),
            output_tokens: Some(50),
            cache_tokens: None,
        };
        footer.accumulate_usage(&usage);
        let lines = footer.render(80);
        assert!(lines[1].contains("↑100"), "Should show full token count");
        assert!(lines[1].contains("↓50"), "Should show full token count");
    }

    #[test]
    fn test_footer_line2_exact_width() {
        let footer = make_footer();
        let lines = footer.render(80);
        for line in &lines {
            let vw = visible_width(line);
            assert!(vw <= 80, "Line width {} > 80", vw);
        }
    }

    #[test]
    fn test_footer_line2_padded_correctly() {
        let footer = make_footer();
        for w in [40, 60, 80, 120] {
            let lines = footer.render(w);
            for line in &lines {
                let vw = visible_width(line);
                assert!(vw <= w, "At width {}: line width {} exceeds", w, vw);
            }
        }
    }

    #[test]
    fn test_footer_model_strip_prefix() {
        let mut footer = make_footer();
        footer.set_model("opencode_go::claude-opus");
        let lines = footer.render(80);
        assert!(
            !lines[1].contains("opencode_go::"),
            "Should strip opencode_go:: prefix"
        );
        assert!(
            lines[1].contains("claude-opus"),
            "Should show model after prefix"
        );
    }

    #[test]
    fn test_footer_provider_prefix_when_multiple_providers() {
        let mut footer = make_footer();
        footer.set_model("test-model");
        footer.set_available_provider_count(2);
        let lines = footer.render(80);
        // Provider prefix has "(?)" placeholder since we don't know provider name
        assert!(
            lines[1].contains("(?)"),
            "Should show provider count-based prefix"
        );
    }

    #[test]
    fn test_footer_experimental_indicator() {
        let mut footer = make_footer();
        footer.set_experimental_enabled(true);
        let lines = footer.render(80);
        assert!(
            lines[1].contains("xp"),
            "Should show experimental indicator"
        );
    }
}