cship 1.7.1

A beautiful, fully customizable statusline for Claude Code — Starship-style TOML config, themeable colours, Nerd Font glyphs, and tunable cost/context/usage thresholds
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
#[cfg(test)]
use crate::config::CostSubfieldConfig;
/// Render the `[cship.cost]` family of modules.
///
/// `$cship.cost` — convenience alias: formats total_cost_usd with threshold styling.
/// Display currency and conversion rate are configurable via `currency_symbol` and
/// `conversion_rate`; the underlying context value is always `total_cost_usd` (USD).
/// Threshold styling (`warn_threshold`, `critical_threshold`) is evaluated against
/// the converted display value (`total_cost_usd * conversion_rate`); configure
/// thresholds in your display currency.
/// `$cship.cost.total_cost_usd` — raw USD value, 4 decimal places.
/// `$cship.cost.total_duration_ms` / `$cship.cost.total_api_duration_ms` — integer milliseconds.
/// `$cship.cost.total_lines_added` / `$cship.cost.total_lines_removed` — integer counts.
///
/// [Source: epics.md#Story 2.1, architecture.md#Structure Patterns]
use crate::config::{CostConfig, CshipConfig};
use crate::context::Context;

/// Renders `$cship.cost` — total cost with threshold color escalation.
/// Display format is `{currency_symbol}{value:.2}` (default: `$X.XX`).
/// `currency_symbol` and `conversion_rate` are configurable; thresholds are
/// evaluated against the converted display value, so configure them in your
/// display currency.
pub fn render(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();

    // Respect disabled flag — return None silently
    if cost_cfg.and_then(|c| c.disabled).unwrap_or(false) {
        return None;
    }

    // total_cost_usd absent → warn and return None (AC9 requires tracing::warn!)
    let val = match ctx.cost.as_ref().and_then(|c| c.total_cost_usd) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost: total_cost_usd absent from context");
            return None;
        }
    };

    let symbol = cost_cfg.and_then(|c| c.symbol.as_deref());
    let style = cost_cfg.and_then(|c| c.style.as_deref());
    let currency_symbol = cost_cfg
        .and_then(|c| c.currency_symbol.as_deref())
        .unwrap_or("$");
    let conversion_rate = cost_cfg.and_then(|c| c.conversion_rate).unwrap_or(1.0);
    let converted_val = val * conversion_rate;
    let formatted = format!("{}{:.2}", currency_symbol, converted_val);

    // Extract threshold variables FIRST (before format check)
    let warn_threshold = cost_cfg.and_then(|c| c.warn_threshold);
    let warn_style = cost_cfg.and_then(|c| c.warn_style.as_deref());
    let critical_threshold = cost_cfg.and_then(|c| c.critical_threshold);
    let critical_style = cost_cfg.and_then(|c| c.critical_style.as_deref());

    // Format string takes priority if configured (AC1)
    if let Some(fmt) = cost_cfg.and_then(|c| c.format.as_deref()) {
        let effective_style = crate::ansi::resolve_threshold_style(
            Some(converted_val),
            style,
            warn_threshold,
            warn_style,
            critical_threshold,
            critical_style,
        );
        return crate::format::apply_module_format(fmt, Some(&formatted), symbol, effective_style);
    }

    // Default behavior — unchanged (AC5): threshold-style logic
    let symbol_str = symbol.unwrap_or("");
    let content = format!("{symbol_str}{formatted}");

    Some(crate::ansi::apply_style_with_threshold(
        &content,
        Some(converted_val),
        style,
        warn_threshold,
        warn_style,
        critical_threshold,
        critical_style,
    ))
}

/// Renders `$cship.cost.total_cost_usd` — raw USD value to 4 decimal places.
pub fn render_total_cost_usd(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();
    let sub_cfg = cost_cfg.and_then(|c| c.total_cost_usd.as_ref());
    if is_subfield_disabled(sub_cfg, cost_cfg) {
        return None;
    }
    let val = match ctx.cost.as_ref().and_then(|c| c.total_cost_usd) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost.total_cost_usd: value absent from context");
            return None;
        }
    };
    let val_str = format!("{:.4}", val);
    crate::format::render_styled_value(&val_str, Some(val), sub_cfg, None)
}

/// Renders `$cship.cost.total_duration_ms` — total wall time in milliseconds.
pub fn render_total_duration_ms(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();
    let sub_cfg = cost_cfg.and_then(|c| c.total_duration_ms.as_ref());
    if is_subfield_disabled(sub_cfg, cost_cfg) {
        return None;
    }
    let val = match ctx.cost.as_ref().and_then(|c| c.total_duration_ms) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost.total_duration_ms: value absent from context");
            return None;
        }
    };
    let val_str = val.to_string();
    crate::format::render_styled_value(&val_str, Some(val as f64), sub_cfg, None)
}

/// Renders `$cship.cost.total_api_duration_ms` — API-only duration in milliseconds.
pub fn render_total_api_duration_ms(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();
    let sub_cfg = cost_cfg.and_then(|c| c.total_api_duration_ms.as_ref());
    if is_subfield_disabled(sub_cfg, cost_cfg) {
        return None;
    }
    let val = match ctx.cost.as_ref().and_then(|c| c.total_api_duration_ms) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost.total_api_duration_ms: value absent from context");
            return None;
        }
    };
    let val_str = val.to_string();
    crate::format::render_styled_value(&val_str, Some(val as f64), sub_cfg, None)
}

/// Renders `$cship.cost.total_lines_added` — cumulative lines added this session.
pub fn render_total_lines_added(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();
    let sub_cfg = cost_cfg.and_then(|c| c.total_lines_added.as_ref());
    if is_subfield_disabled(sub_cfg, cost_cfg) {
        return None;
    }
    let val = match ctx.cost.as_ref().and_then(|c| c.total_lines_added) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost.total_lines_added: value absent from context");
            return None;
        }
    };
    let val_str = val.to_string();
    crate::format::render_styled_value(&val_str, Some(val as f64), sub_cfg, None)
}

/// Renders `$cship.cost.total_lines_removed` — cumulative lines removed this session.
pub fn render_total_lines_removed(ctx: &Context, cfg: &CshipConfig) -> Option<String> {
    let cost_cfg = cfg.cost.as_ref();
    let sub_cfg = cost_cfg.and_then(|c| c.total_lines_removed.as_ref());
    if is_subfield_disabled(sub_cfg, cost_cfg) {
        return None;
    }
    let val = match ctx.cost.as_ref().and_then(|c| c.total_lines_removed) {
        Some(v) => v,
        None => {
            tracing::warn!("cship.cost.total_lines_removed: value absent from context");
            return None;
        }
    };
    let val_str = val.to_string();
    crate::format::render_styled_value(&val_str, Some(val as f64), sub_cfg, None)
}

fn is_subfield_disabled(
    sub_cfg: Option<&crate::config::SubfieldConfig>,
    cost_cfg: Option<&CostConfig>,
) -> bool {
    // Sub-field explicit disabled takes precedence
    if let Some(d) = sub_cfg.and_then(|c| c.disabled) {
        return d;
    }
    // Fall through to parent disabled
    cost_cfg.and_then(|c| c.disabled).unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{CostConfig, CshipConfig};
    use crate::context::{Context, Cost};

    fn ctx_with_cost(usd: f64) -> Context {
        Context {
            cost: Some(Cost {
                total_cost_usd: Some(usd),
                total_duration_ms: Some(45000),
                total_api_duration_ms: Some(2300),
                total_lines_added: Some(156),
                total_lines_removed: Some(23),
            }),
            ..Default::default()
        }
    }

    #[test]
    fn test_cost_renders_dollar_formatted() {
        let ctx = ctx_with_cost(0.01234);
        let result = render(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("$0.01".to_string()));
    }

    #[test]
    fn test_cost_renders_custom_currency_symbol() {
        let ctx = ctx_with_cost(1.50);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                currency_symbol: Some("£".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg);
        assert_eq!(result, Some("£1.50".to_string()));
    }

    #[test]
    fn test_cost_renders_with_conversion_rate() {
        let ctx = ctx_with_cost(1.00);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                currency_symbol: Some("£".to_string()),
                conversion_rate: Some(0.79),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg);
        assert_eq!(result, Some("£0.79".to_string()));
    }

    #[test]
    fn test_cost_format_path_uses_converted_value() {
        // Regression: conversion_rate and currency_symbol must flow into $value
        // when a format string is configured (apply_module_format branch).
        let ctx = ctx_with_cost(1.00);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                currency_symbol: Some("".to_string()),
                conversion_rate: Some(0.92),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains("€0.92"),
            "expected converted value in format path: {result:?}"
        );
    }

    #[test]
    fn test_cost_disabled_returns_none() {
        let ctx = ctx_with_cost(5.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                disabled: Some(true),
                ..Default::default()
            }),
            ..Default::default()
        };
        assert_eq!(render(&ctx, &cfg), None);
    }

    #[test]
    fn test_cost_absent_returns_none_and_warns() {
        let ctx = Context::default(); // no cost field
        let result = render(&ctx, &CshipConfig::default());
        assert_eq!(result, None);
    }

    #[test]
    fn test_cost_below_warn_uses_base_style() {
        let ctx = ctx_with_cost(3.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        // No ANSI codes when base style is None and value is below warn
        assert!(
            !result.contains('\x1b'),
            "should not have ANSI when below warn: {result:?}"
        );
        assert!(result.contains("$3.00"));
    }

    #[test]
    fn test_cost_above_warn_applies_warn_style() {
        let ctx = ctx_with_cost(6.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected warn ANSI codes: {result:?}"
        );
    }

    #[test]
    fn test_cost_above_critical_applies_critical_style() {
        let ctx = ctx_with_cost(12.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected critical ANSI codes: {result:?}"
        );
    }

    #[test]
    fn test_cost_default_threshold_uses_converted_value() {
        // Raw USD 1.0 is below warn_threshold 4.0, but converted = 5.0 trips the warn style.
        let ctx = ctx_with_cost(1.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                conversion_rate: Some(5.0),
                warn_threshold: Some(4.0),
                warn_style: Some("yellow".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected warn ANSI from converted value: {result:?}"
        );
        assert!(result.contains("$5.00"));
    }

    #[test]
    fn test_cost_default_below_converted_threshold_keeps_base_style() {
        // Converted = 5.0, below warn_threshold 10.0 → no escalation.
        let ctx = ctx_with_cost(1.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                conversion_rate: Some(5.0),
                warn_threshold: Some(10.0),
                warn_style: Some("yellow".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            !result.contains('\x1b'),
            "should not escalate when converted value below threshold: {result:?}"
        );
        assert!(result.contains("$5.00"));
    }

    #[test]
    fn test_cost_format_threshold_uses_converted_value() {
        // Same converted-value comparison must apply on the format-string code path.
        let ctx = ctx_with_cost(1.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                conversion_rate: Some(5.0),
                warn_threshold: Some(4.0),
                warn_style: Some("yellow".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected warn ANSI in format path from converted value: {result:?}"
        );
    }

    #[test]
    fn test_cost_default_critical_uses_converted_value() {
        // Raw USD 3.0 is below critical_threshold 5.0; converted = 6.0 trips critical.
        let ctx = ctx_with_cost(3.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                conversion_rate: Some(2.0),
                warn_threshold: Some(4.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(5.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected critical ANSI from converted value: {result:?}"
        );
        assert!(result.contains("$6.00"));
    }

    #[test]
    fn test_subfield_inherits_parent_disabled() {
        let ctx = ctx_with_cost(5.0);
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                disabled: Some(true),
                ..Default::default()
            }),
            ..Default::default()
        };
        // Sub-fields should inherit parent disabled when not explicitly overridden
        assert_eq!(render_total_cost_usd(&ctx, &cfg), None);
        assert_eq!(render_total_duration_ms(&ctx, &cfg), None);
        assert_eq!(render_total_api_duration_ms(&ctx, &cfg), None);
        assert_eq!(render_total_lines_added(&ctx, &cfg), None);
        assert_eq!(render_total_lines_removed(&ctx, &cfg), None);
    }

    #[test]
    fn test_render_total_cost_usd_four_decimal_places() {
        let ctx = ctx_with_cost(0.01234);
        let result = render_total_cost_usd(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("0.0123".to_string()));
    }

    #[test]
    fn test_render_total_duration_ms() {
        let ctx = ctx_with_cost(0.01);
        let result = render_total_duration_ms(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("45000".to_string()));
    }

    #[test]
    fn test_render_total_api_duration_ms() {
        let ctx = ctx_with_cost(0.01);
        let result = render_total_api_duration_ms(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("2300".to_string()));
    }

    #[test]
    fn test_render_total_lines_added() {
        let ctx = ctx_with_cost(0.01);
        let result = render_total_lines_added(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("156".to_string()));
    }

    #[test]
    fn test_render_total_lines_removed() {
        let ctx = ctx_with_cost(0.01);
        let result = render_total_lines_removed(&ctx, &CshipConfig::default());
        assert_eq!(result, Some("23".to_string()));
    }

    #[test]
    fn test_cost_format_below_threshold_uses_base_style() {
        // AC1: format + value below all thresholds → base style (None) used
        let ctx = ctx_with_cost(3.0); // below warn_threshold of 5.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            !result.contains('\x1b'),
            "expected NO ANSI codes below threshold with no base style: {result:?}"
        );
        assert!(
            result.contains("$3.00"),
            "expected formatted value: {result:?}"
        );
    }

    #[test]
    fn test_cost_format_with_warn_threshold_uses_warn_style() {
        // AC1: format + warn_threshold → warn_style flows into format renderer
        let ctx = ctx_with_cost(6.0); // above warn_threshold of 5.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected ANSI codes for warn style: {result:?}"
        );
        assert!(
            result.contains("$6.00"),
            "expected formatted value: {result:?}"
        );
    }

    #[test]
    fn test_cost_format_with_critical_threshold_uses_critical_style() {
        // AC1: format + critical_threshold → critical_style flows into format renderer
        let ctx = ctx_with_cost(12.0); // above critical_threshold of 10.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(10.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected ANSI codes for critical style: {result:?}"
        );
        assert!(
            result.contains("$12.00"),
            "expected formatted value: {result:?}"
        );
    }

    #[test]
    fn test_cost_format_warn_and_critical_produce_different_styles() {
        // M1 fix: verify warn and critical styles are distinguishable
        let warn_cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(100.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let crit_cfg = CshipConfig {
            cost: Some(CostConfig {
                format: Some("[$value]($style)".to_string()),
                warn_threshold: Some(5.0),
                warn_style: Some("yellow".to_string()),
                critical_threshold: Some(5.0),
                critical_style: Some("bold red".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        let ctx = ctx_with_cost(6.0);
        let warn_result = render(&ctx, &warn_cfg).unwrap();
        let crit_result = render(&ctx, &crit_cfg).unwrap();
        assert_ne!(
            warn_result, crit_result,
            "warn and critical styles must produce different output"
        );
    }

    // --- Story 9.1: Subfield threshold tests ---

    #[test]
    fn test_subfield_total_cost_usd_above_warn_applies_warn_style() {
        // AC2, AC6: value above warn_threshold → warn_style applied in default path
        let ctx = ctx_with_cost(5.0); // total_cost_usd = 5.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_cost_usd: Some(CostSubfieldConfig {
                    warn_threshold: Some(3.0),
                    warn_style: Some("yellow".to_string()),
                    critical_threshold: Some(10.0),
                    critical_style: Some("bold red".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_cost_usd(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected ANSI for warn: {result:?}"
        );
        assert!(result.contains("5.0000"), "expected 4dp value: {result:?}");
    }

    #[test]
    fn test_subfield_total_cost_usd_above_critical_applies_critical_style() {
        // AC2, AC6: value above critical_threshold → critical_style applied
        let ctx = ctx_with_cost(12.0); // total_cost_usd = 12.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_cost_usd: Some(CostSubfieldConfig {
                    warn_threshold: Some(3.0),
                    warn_style: Some("yellow".to_string()),
                    critical_threshold: Some(10.0),
                    critical_style: Some("bold red".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_cost_usd(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected ANSI for critical: {result:?}"
        );
        assert!(result.contains("12.0000"), "expected 4dp value: {result:?}");
    }

    #[test]
    fn test_subfield_total_cost_usd_below_warn_uses_base_style() {
        // AC5, AC6: value below warn_threshold → no ANSI when base style is None
        let ctx = ctx_with_cost(1.0); // below warn_threshold of 3.0
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_cost_usd: Some(CostSubfieldConfig {
                    warn_threshold: Some(3.0),
                    warn_style: Some("yellow".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_cost_usd(&ctx, &cfg).unwrap();
        assert!(
            !result.contains('\x1b'),
            "no ANSI expected below warn: {result:?}"
        );
        assert!(result.contains("1.0000"), "expected value: {result:?}");
    }

    #[test]
    fn test_subfield_total_duration_ms_above_warn_applies_warn_style() {
        // AC4: 45000ms > 30000ms warn threshold → warn_style applied
        let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_duration_ms = 45000
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_duration_ms: Some(CostSubfieldConfig {
                    warn_threshold: Some(30000.0),
                    warn_style: Some("yellow".to_string()),
                    critical_threshold: Some(60000.0),
                    critical_style: Some("bold red".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_duration_ms(&ctx, &cfg).unwrap();
        assert!(result.contains('\x1b'), "expected warn ANSI: {result:?}");
        assert!(result.contains("45000"), "expected value: {result:?}");
    }

    #[test]
    fn test_subfield_format_with_warn_threshold_uses_warn_style() {
        // AC3, AC6: format path + threshold → threshold-resolved style in apply_module_format
        let ctx = ctx_with_cost(0.01); // total_duration_ms = 45000 > 30000
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_duration_ms: Some(CostSubfieldConfig {
                    format: Some("[$value ms]($style)".to_string()),
                    warn_threshold: Some(30000.0),
                    warn_style: Some("yellow".to_string()),
                    critical_threshold: Some(60000.0),
                    critical_style: Some("bold red".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_duration_ms(&ctx, &cfg).unwrap();
        assert!(
            result.contains('\x1b'),
            "expected ANSI in format path: {result:?}"
        );
        assert!(
            result.contains("45000"),
            "expected value in format: {result:?}"
        );
    }

    #[test]
    fn test_subfield_total_api_duration_ms_above_warn_applies_warn_style() {
        // AC2: u64 threshold wiring for total_api_duration_ms (2300 > 2000)
        let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_api_duration_ms = 2300
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_api_duration_ms: Some(CostSubfieldConfig {
                    warn_threshold: Some(2000.0),
                    warn_style: Some("yellow".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_api_duration_ms(&ctx, &cfg).unwrap();
        assert!(result.contains('\x1b'), "expected warn ANSI: {result:?}");
        assert!(result.contains("2300"), "expected value: {result:?}");
    }

    #[test]
    fn test_subfield_total_lines_added_above_warn_applies_warn_style() {
        // AC2: i64 threshold wiring for total_lines_added (156 > 100)
        let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_lines_added = 156
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_lines_added: Some(CostSubfieldConfig {
                    warn_threshold: Some(100.0),
                    warn_style: Some("yellow".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_lines_added(&ctx, &cfg).unwrap();
        assert!(result.contains('\x1b'), "expected warn ANSI: {result:?}");
        assert!(result.contains("156"), "expected value: {result:?}");
    }

    #[test]
    fn test_subfield_total_lines_removed_above_warn_applies_warn_style() {
        // AC2: i64 threshold wiring for total_lines_removed (23 > 10)
        let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_lines_removed = 23
        let cfg = CshipConfig {
            cost: Some(CostConfig {
                total_lines_removed: Some(CostSubfieldConfig {
                    warn_threshold: Some(10.0),
                    warn_style: Some("yellow".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result = render_total_lines_removed(&ctx, &cfg).unwrap();
        assert!(result.contains('\x1b'), "expected warn ANSI: {result:?}");
        assert!(result.contains("23"), "expected value: {result:?}");
    }

    #[test]
    fn test_subfield_no_threshold_unchanged() {
        // AC5, AC6: no threshold fields → output identical to baseline (no regression)
        let ctx = ctx_with_cost(0.01234);
        let result_default = render_total_cost_usd(&ctx, &CshipConfig::default());
        let cfg_no_thresh = CshipConfig {
            cost: Some(CostConfig {
                total_cost_usd: Some(CostSubfieldConfig {
                    ..Default::default() // all None
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let result_explicit = render_total_cost_usd(&ctx, &cfg_no_thresh);
        assert_eq!(
            result_default, result_explicit,
            "no-threshold config should match default"
        );
    }
}