crew-watch 0.3.0

Terminal monitor for firstmate fleets: htop-style system overview plus one row per running AI agent session, with subtree-aggregated CPU and memory, task and activity.
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
//! Pure quota-row line builder.
//!
//! Turns [`ProviderQuota`]s into styled-segment specs ([`RowSegment`]) that
//! `ui.rs` renders, and into a bar-free string for `--once` ([`once_line`]).
//! All width/label/bar decisions live here so the renderer stays a thin mapper
//! (the crate convention: pure logic is unit-tested, rendering is not).
//!
//! ## Canonical window order
//!
//! Windows always render in a provider-independent order (see
//! [`sorted_windows`]/[`window_rank`]): `session`, then `week`, then the
//! provider's extra windows (anything that is neither) sorted alphabetically by
//! displayed label. Classification is by the displayed label, so a new provider
//! whose windows are labelled `session`/`week` falls into order on its own — no
//! per-provider list. This applies to both the TUI rows and `--once`.
//!
//! ## Aligned block + tier ladder
//!
//! When several providers show at once, [`build_quota_rows`] renders them as one
//! aligned block: every row shares a tier and per-column widths (label and
//! reset fields padded to the block max), so bars line up down the rows even
//! when labels or reset widths differ. Alignment is the cheapest fidelity to
//! lose, so it is dropped *before* the tier ladder degrades: the block is used
//! only at a tier at least as good as the one every row would reach on its own.
//! The moment the padding would cost a row bar width or its reset, alignment
//! goes and each row degrades independently through the single-row
//! [`build_provider_line`] ladder — a cramped terminal renders exactly as it did
//! before alignment existed. Bars never shrink below 6 cells; reset is
//! sacrificed before the bar becomes decoration. A single provider is never
//! padded for a column no other row needs. See [`Tier`].

use crate::format_util::{format_reset, make_bar_min_one};
use crate::quota::{
    has_usage_windows, parse_iso8601_utc_epoch, ProviderQuota, ProviderStatus, QuotaWindow,
};

/// One piece of a rendered provider line. `pct` is `Some` when the segment
/// should be colored with crew-watch's `pct_color` (the bar and the percentage),
/// mirroring the memory/swap meters; `None` for plain text.
#[derive(Debug, Clone, PartialEq)]
pub struct RowSegment {
    pub text: String,
    pub pct: Option<f64>,
}

impl RowSegment {
    fn plain(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            pct: None,
        }
    }
    fn colored(text: impl Into<String>, pct: f64) -> Self {
        Self {
            text: text.into(),
            pct: Some(pct),
        }
    }
}

/// A degradation tier: bar width (if any), whether the reset countdown is shown,
/// and whether window labels are full or reduced to their first letter.
#[derive(Clone, Copy)]
struct Tier {
    bar: Option<usize>,
    reset: bool,
    labels: LabelMode,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum LabelMode {
    Full,
    FirstLetter,
}

/// The tuned ladder (descending fidelity). Order matters: the first tier whose
/// rendered width fits is used. Reset is dropped before the bar shrinks below 6;
/// the bar is dropped entirely before labels abbreviate.
const TIERS: &[Tier] = &[
    Tier {
        bar: Some(12),
        reset: true,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(10),
        reset: true,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(8),
        reset: true,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(6),
        reset: true,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(10),
        reset: false,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(8),
        reset: false,
        labels: LabelMode::Full,
    },
    Tier {
        bar: Some(6),
        reset: false,
        labels: LabelMode::Full,
    },
    Tier {
        bar: None,
        reset: false,
        labels: LabelMode::Full,
    },
    Tier {
        bar: None,
        reset: false,
        labels: LabelMode::FirstLetter,
    },
];

/// Window label rule (reproduces quota-axi's own TUI labels generically):
/// `model:fable` → `fable`; otherwise the window's `label` verbatim.
fn window_label<'a>(id: &'a str, label: &'a str) -> &'a str {
    if let Some(rest) = id.strip_prefix("model:") {
        rest
    } else {
        label
    }
}

/// Canonical rank for a window's *displayed* label: `session` (the short rolling
/// window) sorts first, then `week`, then every other window (the provider's
/// extras). Classification is by the displayed label so any provider whose
/// windows are labelled `session`/`week` orders correctly with no per-provider
/// special-casing — a new provider falls into order on its own.
fn window_rank(displayed_label: &str) -> u8 {
    match displayed_label {
        "session" => 0,
        "week" => 1,
        _ => 2,
    }
}

/// The provider's windows in canonical display order, by reference: `session`,
/// then `week`, then extras. Extras (any window that is neither `session` nor
/// `week`) are sorted alphabetically by displayed label for a stable order that
/// does not depend on the API's return order; ties keep source order (slice
/// `sort_by` is stable).
fn sorted_windows(windows: &[QuotaWindow]) -> Vec<&QuotaWindow> {
    let mut v: Vec<&QuotaWindow> = windows.iter().collect();
    v.sort_by(|a, b| {
        let la = window_label(&a.id, &a.label);
        let lb = window_label(&b.id, &b.label);
        window_rank(la)
            .cmp(&window_rank(lb))
            .then_with(|| la.cmp(lb))
    });
    v
}

/// Assign each window a block column index so the same kind lines up across
/// providers regardless of how many extras each carries: `session`→0, `week`→1,
/// an extra→2 + its alphabetical position among this provider's own extras.
/// Returns the windows in canonical order, tagged with their column.
fn provider_columns(windows: &[QuotaWindow]) -> Vec<(usize, &QuotaWindow)> {
    let sorted = sorted_windows(windows);
    let mut extras_seen = 0usize;
    sorted
        .into_iter()
        .map(|w| {
            let col = match window_label(&w.id, &w.label) {
                "session" => 0,
                "week" => 1,
                _ => {
                    let c = 2 + extras_seen;
                    extras_seen += 1;
                    c
                }
            };
            (col, w)
        })
        .collect()
}

/// Window label text for a tier: full, or reduced to its first letter on the
/// narrowest tier.
fn tier_label(full: &str, mode: LabelMode) -> String {
    match mode {
        LabelMode::Full => full.to_string(),
        LabelMode::FirstLetter => full
            .chars()
            .next()
            .map(|c| c.to_string())
            .unwrap_or_default(),
    }
}

/// Seconds until `resets_at`, relative to `now_epoch`, or `None` if the
/// timestamp is missing/unparseable.
fn reset_secs(resets_at: Option<&str>, now_epoch: u64) -> Option<u64> {
    let target = parse_iso8601_utc_epoch(resets_at?)?;
    Some(target.saturating_sub(now_epoch))
}

/// Render one provider line for the TUI at the given width. For a provider with
/// no usable windows this is a single dim status phrase; otherwise it picks the
/// best-fitting tier. Windows are always in canonical order (session, week,
/// extras). `now_epoch` (UTC seconds) makes reset countdowns pure and testable
/// without a system clock.
///
/// This is the single-row entry point; for aligned multi-provider rendering use
/// [`build_quota_rows`] (one provider alone is never padded for a column no
/// other row needs).
pub fn build_provider_line(p: &ProviderQuota, now_epoch: u64, width: usize) -> Vec<RowSegment> {
    if !has_usage_windows(p) {
        return dim_status_line(&p.id, &p.status, p.error.as_deref());
    }
    build_row_independent(p, now_epoch, width)
}

/// Render the quota block: one row per provider, in input order, with bars
/// aligned down the rows. Live providers (those with windows) share one tier and
/// per-column widths computed from the rows actually shown; providers without
/// windows render their dim status line independently (no bars to align).
///
/// At wide terminals every live row uses the same tier, so each window kind
/// starts at the same column and bars line up even when labels or reset widths
/// differ. Alignment is dropped before the ladder degrades: if the aligned block
/// would only fit at a tier worse than the one some row reaches on its own, the
/// padding goes and each live row degrades through the [`build_provider_line`]
/// ladder independently, exactly as a cramped terminal did before alignment
/// existed (no wrapping or overflow). With a single live provider the column
/// widths are that row's own, so nothing is padded for an absent neighbour.
pub fn build_quota_rows(
    providers: &[&ProviderQuota],
    now_epoch: u64,
    width: usize,
) -> Vec<Vec<RowSegment>> {
    let live: Vec<&ProviderQuota> = providers
        .iter()
        .filter(|p| has_usage_windows(p))
        .copied()
        .collect();
    let aligned = if live.is_empty() {
        None
    } else {
        build_aligned_block(&live, now_epoch, width)
    };
    let mut live_idx = 0;
    providers
        .iter()
        .map(|p| {
            if !has_usage_windows(p) {
                dim_status_line(&p.id, &p.status, p.error.as_deref())
            } else {
                let row = match &aligned {
                    Some(block) => block[live_idx].clone(),
                    // Aligned block did not fit: drop alignment and let each row
                    // degrade through the single-row ladder on its own.
                    None => build_provider_line(p, now_epoch, width),
                };
                live_idx += 1;
                row
            }
        })
        .collect()
}

/// A live provider's per-window bar/percent/reset, in the `--once` format
/// (bar-free): `claude   session 5% 1h45m  week 48% 3d22h  ...`. Windows are
/// emitted in canonical order (session, week, extras). For a provider without
/// windows: `{id}: {phrase}` (e.g. `copilot: sign-in required`). The caller
/// prefixes `quota ` and any per-line decoration.
pub fn once_line(p: &ProviderQuota, now_epoch: u64) -> String {
    if !has_usage_windows(p) {
        return format!("{}: {}", p.id, status_phrase(&p.status, p.error.as_deref()));
    }
    let mut out = format!("{:<7}  ", p.id);
    let mut first = true;
    for w in sorted_windows(&p.windows) {
        if !first {
            out.push_str("  ");
        }
        first = false;
        let label = window_label(&w.id, &w.label);
        out.push_str(label);
        out.push(' ');
        out.push_str(&format!("{:.0}%", w.percent_used));
        if let Some(secs) = reset_secs(w.resets_at.as_deref(), now_epoch) {
            out.push(' ');
            out.push_str(&format_reset(secs));
        }
    }
    out
}

/// Dialog status note: live providers show `{plan} · {status}`; non-live ones
/// show the short status phrase.
pub fn dialog_note(p: &ProviderQuota) -> String {
    if has_usage_windows(p) {
        let plan = p.plan.as_deref().unwrap_or("-");
        format!("{} · {}", plan, status_word(&p.status))
    } else {
        status_phrase(&p.status, p.error.as_deref())
    }
}

/// Short human phrase for a non-live provider's dim line.
fn status_phrase(status: &ProviderStatus, error: Option<&str>) -> String {
    match status {
        ProviderStatus::AuthRequired => "sign-in required".to_string(),
        ProviderStatus::Error => "unavailable".to_string(),
        ProviderStatus::Fresh => "no usage".to_string(),
        ProviderStatus::Unknown(_) => error.unwrap_or("unavailable").to_string(),
    }
}

/// Single status word for the dialog's live note (e.g. `fresh`).
fn status_word(status: &ProviderStatus) -> String {
    match status {
        ProviderStatus::Fresh => "fresh".to_string(),
        ProviderStatus::Error => "error".to_string(),
        ProviderStatus::AuthRequired => "auth_required".to_string(),
        ProviderStatus::Unknown(s) => s.clone(),
    }
}

fn dim_status_line(id: &str, status: &ProviderStatus, error: Option<&str>) -> Vec<RowSegment> {
    // `{id:<7}` field + one separator space keeps the phrase column aligned
    // across providers and guarantees a gap even for a 7-char id (e.g. copilot).
    let phrase = status_phrase(status, error);
    vec![RowSegment::plain(format!(" {:<7} {}", id, phrase))]
}

// ---------------------------------------------------------------------------
// Aligned block layout (Fault 2): shared tier + shared per-column widths so
// bars line up down the rows. Single-provider rendering goes through the same
// helpers with a one-row block, so it is never padded for an absent neighbour.
// ---------------------------------------------------------------------------

/// Per-column widths shared across every row in the block.
struct ColCols {
    /// Max displayed-label width at this column (labels are left-justified to
    /// it, so the bar that follows starts at the same x in every row).
    label_w: usize,
    /// Max reset-string width at this column (`0` if no row has a reset here).
    reset_w: usize,
}

struct BlockLayout {
    /// `{id:<id_w}` — the provider-id column width (floor 6 to keep the existing
    /// spacing for short ids).
    id_w: usize,
    /// One entry per block column `0..=block_max_col`.
    cols: Vec<ColCols>,
}

/// Compute the shared column widths for `provider_cols` at `tier`.
fn compute_layout(
    provider_cols: &[&[(usize, &QuotaWindow)]],
    id_w: usize,
    block_max_col: usize,
    tier: Tier,
    now_epoch: u64,
) -> BlockLayout {
    let mut cols: Vec<ColCols> = (0..=block_max_col)
        .map(|_| ColCols {
            label_w: 0,
            reset_w: 0,
        })
        .collect();
    for pc in provider_cols {
        for (c, w) in pc.iter() {
            let full = window_label(&w.id, &w.label);
            let label_text = tier_label(full, tier.labels);
            cols[*c].label_w = cols[*c].label_w.max(label_text.chars().count());
            if tier.reset {
                if let Some(secs) = reset_secs(w.resets_at.as_deref(), now_epoch) {
                    cols[*c].reset_w = cols[*c].reset_w.max(format_reset(secs).chars().count());
                }
            }
        }
    }
    BlockLayout { id_w, cols }
}

/// Render one provider's row into the shared `layout` at `tier`. Columns the
/// provider lacks are blank-padded only when the provider still has a later
/// column AND some other row actually fills that column (so the blank is
/// alignment padding, never gratuitous on a lone provider); trailing columns it
/// lacks are omitted (no needless blanks).
fn render_aligned_row(
    provider_cols: &[(usize, &QuotaWindow)],
    p: &ProviderQuota,
    layout: &BlockLayout,
    block_max_col: usize,
    column_used: &[bool],
    tier: Tier,
    now_epoch: u64,
) -> Vec<RowSegment> {
    let mut segs: Vec<RowSegment> = Vec::new();
    // id prefix: " {id:<id_w} "
    let id_field = format!("{:<w$}", p.id, w = layout.id_w);
    segs.push(RowSegment::plain(format!(" {} ", id_field)));

    let barpart = tier.bar.map(|bw| bw + 1).unwrap_or(0);
    // The inter-column separator belongs *between* columns, so it is keyed to
    // the block's first filled column rather than to column 0: a block whose
    // providers have no `session`/`week` window starts at column 2 and must not
    // gain two leading spaces after the id field. Every row agrees on this
    // column, so alignment is unaffected.
    let first_used = column_used.iter().position(|used| *used).unwrap_or(0);

    for c in 0..=block_max_col {
        let sep = if c == first_used { "" } else { "  " };
        let win = provider_cols
            .iter()
            .find(|(col, _)| *col == c)
            .map(|(_, w)| *w);
        let provider_has_later_col = provider_cols.iter().any(|(col, _)| *col > c);

        if let Some(w) = win {
            let full = window_label(&w.id, &w.label);
            let label_text = tier_label(full, tier.labels);
            let label_w = layout.cols[c].label_w;
            // label segment: "{sep}{label:<label_w} " — padding the label (not
            // the whole slot) is what makes the bar line up across rows whose
            // labels differ in width.
            segs.push(RowSegment::plain(format!(
                "{}{:<lw$} ",
                sep,
                label_text,
                lw = label_w
            )));
            if let Some(bw) = tier.bar {
                segs.push(RowSegment::colored(
                    format!("{} ", make_bar_min_one(w.percent_used, bw)),
                    w.percent_used,
                ));
            }
            segs.push(RowSegment::colored(
                format!("{:>3.0}%", w.percent_used),
                w.percent_used,
            ));
            if tier.reset {
                let reset_str = reset_secs(w.resets_at.as_deref(), now_epoch).map(format_reset);
                let max_reset_len = layout.cols[c].reset_w;
                // Pad the reset field only when this row still renders a later
                // column, so that column lines up; when nothing follows on this
                // row the reset is verbatim (nothing to align against, and this
                // avoids trailing spaces before the caller's own suffixes).
                let interior = c < block_max_col && max_reset_len > 0 && provider_has_later_col;
                match reset_str.as_ref() {
                    Some(r) if interior => {
                        segs.push(RowSegment::plain(format!(
                            " {:<rw$}",
                            r,
                            rw = max_reset_len
                        )));
                    }
                    Some(r) => segs.push(RowSegment::plain(format!(" {}", r))),
                    None if interior => {
                        // No reset for this window, but a later column needs the
                        // field width held constant: placeholder spaces.
                        segs.push(RowSegment::plain(format!(" {}", " ".repeat(max_reset_len))));
                    }
                    None => {}
                }
            }
        } else if provider_has_later_col && column_used.get(c).copied().unwrap_or(false) {
            // No window at this column for this provider, but it has a later
            // column AND another row fills this column: blank the whole column
            // (incl. its reset-field width) so the provider's next column still
            // starts at the shared x. (If no other row uses this column there is
            // nothing to align against, so a lone or short provider is not padded
            // for a column no other row needs.)
            let label_w = layout.cols[c].label_w;
            let max_reset_len = layout.cols[c].reset_w;
            let reset_field = if tier.reset && c < block_max_col && max_reset_len > 0 {
                1 + max_reset_len
            } else {
                0
            };
            let content_w = label_w + 1 + barpart + 4 + reset_field;
            segs.push(RowSegment::plain(format!(
                "{}{}",
                sep,
                " ".repeat(content_w)
            )));
        }
        // else: provider has no window here and no later column — omit (its line
        // ends; padding it would only add trailing spaces).
    }
    segs
}

/// Try the tiers best-first and return the first whose every aligned row fits
/// `width`. Only tiers at least as good as every row's *own* best tier are
/// considered: alignment padding is the cheapest fidelity to lose, so it is
/// dropped before any row would degrade below what it reaches unaligned.
/// `None` when no such aligned tier fits (caller falls back to independent
/// per-row rendering).
fn build_aligned_block(
    live: &[&ProviderQuota],
    now_epoch: u64,
    width: usize,
) -> Option<Vec<Vec<RowSegment>>> {
    let provider_cols: Vec<Vec<(usize, &QuotaWindow)>> =
        live.iter().map(|p| provider_columns(&p.windows)).collect();
    let block_max_col = provider_cols
        .iter()
        .flat_map(|cs| cs.iter().map(|(c, _)| *c))
        .max()
        .unwrap_or(0);
    // Columns that at least one provider fills — a provider only blank-pads an
    // absent column when another row actually uses it (so the padding is
    // alignment, not a gratuitous gap on a lone/short row).
    let mut column_used = vec![false; block_max_col + 1];
    for cs in &provider_cols {
        for (c, _) in cs {
            column_used[*c] = true;
        }
    }
    let id_w = live
        .iter()
        .map(|p| p.id.chars().count())
        .max()
        .unwrap_or(0)
        .max(6);
    // The strictest row wins: aligning at a tier worse than any row's own best
    // would make the padding cost bar width or a reset countdown, and alignment
    // is dropped before that happens. A row that fits at no tier at all (it will
    // be hard-truncated) can never be aligned, so drop alignment outright.
    let mut best_unaligned = TIERS.len() - 1;
    for (p, cols) in live.iter().zip(provider_cols.iter()) {
        let t = independent_tier_index(p, cols, now_epoch, width)?;
        best_unaligned = best_unaligned.min(t);
    }
    let col_slices: Vec<&[(usize, &QuotaWindow)]> =
        provider_cols.iter().map(|cs| cs.as_slice()).collect();
    for tier in TIERS.iter().take(best_unaligned + 1) {
        let layout = compute_layout(&col_slices, id_w, block_max_col, *tier, now_epoch);
        let rows: Vec<Vec<RowSegment>> = live
            .iter()
            .zip(provider_cols.iter())
            .map(|(p, cols)| {
                render_aligned_row(
                    cols,
                    p,
                    &layout,
                    block_max_col,
                    &column_used,
                    *tier,
                    now_epoch,
                )
            })
            .collect();
        if rows.iter().all(|r| line_width(r) <= width) {
            return Some(rows);
        }
    }
    None
}

/// Render one live provider on its own: best-fitting tier, with a hard-truncate
/// fallback at absurdly narrow widths. Identical to the single-row contract of
/// [`build_provider_line`], and the per-row shape the aligned block falls back
/// to. A single provider fills exactly the columns it has, so nothing is blanked
/// or padded for an absent neighbour.
fn build_row_independent(p: &ProviderQuota, now_epoch: u64, width: usize) -> Vec<RowSegment> {
    let provider_cols = provider_columns(&p.windows);
    for tier in TIERS {
        let segs = render_row_unaligned(p, &provider_cols, *tier, now_epoch);
        if line_width(&segs) <= width {
            return segs;
        }
    }
    // Exhausted every tier (absurdly narrow): hard-truncate the cheapest tier.
    let fallback = render_row_unaligned(p, &provider_cols, *TIERS.last().unwrap(), now_epoch);
    truncate_segments(fallback, width)
}

/// One provider's row at `tier` with no neighbours: the column widths are its
/// own, so nothing is padded or blanked for a column another row would need.
fn render_row_unaligned(
    p: &ProviderQuota,
    provider_cols: &[(usize, &QuotaWindow)],
    tier: Tier,
    now_epoch: u64,
) -> Vec<RowSegment> {
    let block_max_col = provider_cols.iter().map(|(c, _)| *c).max().unwrap_or(0);
    let mut column_used = vec![false; block_max_col + 1];
    for (c, _) in provider_cols {
        column_used[*c] = true;
    }
    let id_w = p.id.chars().count().max(6);
    let layout = compute_layout(&[provider_cols], id_w, block_max_col, tier, now_epoch);
    render_aligned_row(
        provider_cols,
        p,
        &layout,
        block_max_col,
        &column_used,
        tier,
        now_epoch,
    )
}

/// Index of the best tier at which this provider's unaligned row fits `width`,
/// i.e. the tier [`build_provider_line`] would pick for it on its own. `None`
/// when no tier fits (that row is hard-truncated and can never be aligned).
fn independent_tier_index(
    p: &ProviderQuota,
    provider_cols: &[(usize, &QuotaWindow)],
    now_epoch: u64,
    width: usize,
) -> Option<usize> {
    TIERS.iter().position(|tier| {
        line_width(&render_row_unaligned(p, provider_cols, *tier, now_epoch)) <= width
    })
}

fn line_width(segs: &[RowSegment]) -> usize {
    segs.iter().map(|s| s.text.chars().count()).sum()
}

fn truncate_segments(segs: Vec<RowSegment>, width: usize) -> Vec<RowSegment> {
    let mut out = Vec::new();
    let mut used = 0;
    for s in segs {
        let remaining = width.saturating_sub(used);
        if remaining == 0 {
            break;
        }
        let chars: Vec<char> = s.text.chars().collect();
        if chars.len() <= remaining {
            used += chars.len();
            out.push(s);
        } else {
            let truncated: String = chars.into_iter().take(remaining).collect();
            out.push(RowSegment {
                text: truncated,
                pct: s.pct,
            });
            break;
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::quota::{ProviderQuota, ProviderStatus, QuotaWindow};

    /// now_epoch for 2026-08-13T13:55:00Z — yields the plan's verified resets
    /// (session=1h45m, week/fable=3d22h) and is used by every width-vector test.
    const NOW: u64 = 1_786_629_300;

    fn claude() -> ProviderQuota {
        ProviderQuota {
            id: "claude".to_string(),
            label: "Claude".to_string(),
            plan: Some("max".to_string()),
            windows: vec![
                QuotaWindow {
                    id: "five_hour".to_string(),
                    label: "session".to_string(),
                    percent_used: 5.0,
                    resets_at: Some("2026-08-13T15:40:00.000000+00:00".to_string()),
                },
                QuotaWindow {
                    id: "seven_day".to_string(),
                    label: "week".to_string(),
                    percent_used: 48.0,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
                QuotaWindow {
                    id: "model:fable".to_string(),
                    label: "Fable week".to_string(),
                    percent_used: 45.0,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        }
    }

    /// Concatenate segment texts into the rendered string (for exact-string asserts).
    fn render(segs: &[RowSegment]) -> String {
        segs.iter().map(|s| s.text.as_str()).collect()
    }

    // --- width-degradation ladder (exact strings from the plan §4.2) ---

    #[test]
    fn tier1_full_fidelity_110cols() {
        let line = render(&build_provider_line(&claude(), NOW, 110));
        // Percentages are width-3 right-aligned (`{:>3.0}%`): a 1-digit pct
        // gets 2 pad spaces, a 2-digit pct gets 1. The bar carries one trailing
        // separator space, so session reads "   5%" and week/fable "  48%"/"  45%".
        assert_eq!(
            line,
            " claude session \u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}   5% 1h45m  week \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}  48% 3d22h  fable \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}  45% 3d22h"
        );
    }

    #[test]
    fn tier_ladder_80_drops_reset_shortens_bar() {
        // width 80: bars shrink to 10, reset dropped, full labels.
        let line = render(&build_provider_line(&claude(), NOW, 80));
        assert!(line.contains("session"), "labels full: {line}");
        assert!(!line.contains("1h45m"), "reset dropped: {line}");
        assert!(!line.contains("3d22h"), "reset dropped: {line}");
        assert_eq!(
            line,
            " claude session \u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}   5%  week \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}  48%  fable \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2591}\u{2591}\u{2591}\u{2591}\u{2591}  45%"
        );
    }

    #[test]
    fn tier8_no_bar_full_labels_55cols() {
        let line = render(&build_provider_line(&claude(), NOW, 55));
        assert_eq!(line, " claude session   5%  week  48%  fable  45%");
    }

    #[test]
    fn tier9_first_letter_40cols() {
        let line = render(&build_provider_line(&claude(), NOW, 40));
        assert_eq!(line, " claude s   5%  w  48%  f  45%");
    }

    #[test]
    fn fallback_hard_truncate_when_absurdly_narrow() {
        // 20 cols: tier-9 needs more, so it hard-truncates without panicking.
        let line = render(&build_provider_line(&claude(), NOW, 20));
        assert!(
            line.chars().count() <= 20,
            "truncated to width: {line} ({})",
            line.chars().count()
        );
    }

    // --- label rule, min-one-cell, clamp, missing reset ---

    #[test]
    fn long_provider_id_keeps_gap_before_first_label() {
        // `copilot` is exactly 7 chars — the id field must still separate it from
        // the first window label instead of running into it ("copilotsession").
        let mut p = claude();
        p.id = "copilot".to_string();
        p.windows.truncate(1);
        let line = render(&build_provider_line(&p, NOW, 110));
        assert!(line.starts_with(" copilot session "), "{line}");
    }

    #[test]
    fn short_provider_id_alignment_unchanged() {
        // Ids up to 6 chars keep the original fixed id column (prefix width 8).
        let mut p = claude();
        p.id = "zai".to_string();
        p.windows.truncate(1);
        let line = render(&build_provider_line(&p, NOW, 110));
        assert!(line.starts_with(" zai    session "), "{line}");
    }

    #[test]
    fn model_label_rule() {
        let line = render(&build_provider_line(&claude(), NOW, 110));
        // model:fable window renders as "fable", not "Fable week".
        assert!(line.contains("fable"), "{line}");
        assert!(!line.contains("Fable week"), "{line}");
    }

    #[test]
    fn min_one_filled_cell_when_pct_small() {
        // 5% at bar-8 would round to 0; min-one-cell forces a single fill.
        let mut p = claude();
        // Keep only the session window, force a tier with bar-8 by using width ~70.
        p.windows.truncate(1);
        let line = render(&build_provider_line(&p, NOW, 70));
        // The bar segment is the one with pct Some; it must contain a fill glyph.
        assert!(
            line.contains('\u{2588}'),
            "5% must show at least one filled cell: {line}"
        );
    }

    #[test]
    fn pct_above_100_clamped_for_bar_printed_raw() {
        let mut p = claude();
        p.windows[0].percent_used = 150.0;
        let line = render(&build_provider_line(&p, NOW, 110));
        // Bar fully filled, and the raw 150% is printed (not clamped to 100).
        assert!(line.contains("150%"), "raw pct printed: {line}");
    }

    #[test]
    fn missing_resets_at_omits_segment() {
        let mut p = claude();
        p.windows[0].resets_at = None;
        let line = render(&build_provider_line(&p, NOW, 110));
        // session window has no reset suffix; week/fable still do.
        assert!(
            line.contains("5%  week"),
            "session reset omitted, flows into week: {line}"
        );
    }

    // --- non-live dim lines ---

    #[test]
    fn dim_line_auth_required() {
        let p = ProviderQuota {
            id: "copilot".to_string(),
            label: "GitHub Copilot".to_string(),
            plan: None,
            windows: vec![],
            status: ProviderStatus::AuthRequired,
            stale: false,
            error: Some("GitHub Copilot sign-in required".to_string()),
        };
        let line = render(&build_provider_line(&p, NOW, 80));
        assert_eq!(line, " copilot sign-in required");
    }

    #[test]
    fn dim_line_error() {
        let p = ProviderQuota {
            id: "codex".to_string(),
            label: "Codex".to_string(),
            plan: None,
            windows: vec![],
            status: ProviderStatus::Error,
            stale: false,
            error: Some("Codex quota unavailable".to_string()),
        };
        let line = render(&build_provider_line(&p, NOW, 80));
        assert_eq!(line, " codex   unavailable");
    }

    // --- once_line ---

    #[test]
    fn once_line_live() {
        let line = once_line(&claude(), NOW);
        assert_eq!(
            line,
            "claude   session 5% 1h45m  week 48% 3d22h  fable 45% 3d22h"
        );
    }

    #[test]
    fn once_line_non_live() {
        let p = ProviderQuota {
            id: "copilot".to_string(),
            label: "GitHub Copilot".to_string(),
            plan: None,
            windows: vec![],
            status: ProviderStatus::AuthRequired,
            stale: false,
            error: Some("x".to_string()),
        };
        assert_eq!(once_line(&p, NOW), "copilot: sign-in required");
    }

    // --- dialog note ---

    #[test]
    fn dialog_note_live_and_non_live() {
        assert_eq!(dialog_note(&claude()), "max · fresh");
        let mut p = claude();
        p.status = ProviderStatus::Error;
        p.windows.clear();
        assert_eq!(dialog_note(&p), "unavailable");
    }

    // --- multi-provider independence ---

    #[test]
    fn each_line_fits_independently() {
        // A 2-window provider keeps its bars at a width where a 3-window one
        // would have already dropped resets — each line picks its own tier.
        let zai = ProviderQuota {
            id: "zai".to_string(),
            label: "Z.AI".to_string(),
            plan: None,
            windows: vec![
                QuotaWindow {
                    id: "five_hour".to_string(),
                    label: "5h".to_string(),
                    percent_used: 61.0,
                    resets_at: Some("2026-08-13T16:05:00Z".to_string()),
                },
                QuotaWindow {
                    id: "month".to_string(),
                    label: "month".to_string(),
                    percent_used: 12.0,
                    resets_at: Some("2026-08-30T16:00:00Z".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        };
        let line = render(&build_provider_line(&zai, NOW, 60));
        assert!(line.chars().count() <= 60, "fits: {line}");
        assert!(line.contains("5h"), "{line}");
        assert!(line.contains("month"), "{line}");
    }

    // --- Fault 1: canonical window order (session, week, extras) ---

    /// A provider whose API returns windows in non-canonical source order, with
    /// two unknown extra windows (neither `session` nor `week`, and not on any
    /// hardcoded list). Source order here is: extra, week, session, extra.
    fn scrambled_zai() -> ProviderQuota {
        ProviderQuota {
            id: "zai".to_string(),
            label: "Z.AI".to_string(),
            plan: None,
            windows: vec![
                QuotaWindow {
                    id: "mcp_month".to_string(),
                    label: "MCP month".to_string(),
                    percent_used: 0.0,
                    resets_at: Some("2026-08-24T21:55:00Z".to_string()),
                },
                QuotaWindow {
                    id: "seven_day".to_string(),
                    label: "week".to_string(),
                    percent_used: 51.0,
                    resets_at: Some("2026-08-14T21:55:00Z".to_string()),
                },
                QuotaWindow {
                    id: "five_hour".to_string(),
                    label: "session".to_string(),
                    percent_used: 42.0,
                    resets_at: Some("2026-08-13T14:38:00Z".to_string()),
                },
                QuotaWindow {
                    id: "model:zebra".to_string(),
                    label: "Zebra pool".to_string(),
                    percent_used: 7.0,
                    resets_at: Some("2026-08-24T21:55:00Z".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        }
    }

    /// Char index of the start of each maximal bar-glyph run (a bar). Used to
    /// assert bars line up across rows without hardcoding the whole string.
    fn bar_starts(s: &str) -> Vec<usize> {
        let mut out = Vec::new();
        let mut prev_bar = false;
        for (i, c) in s.chars().enumerate() {
            let is_bar = c == '\u{2588}' || c == '\u{2591}';
            if is_bar && !prev_bar {
                out.push(i);
            }
            prev_bar = is_bar;
        }
        out
    }

    #[test]
    fn canonical_order_in_once_line_when_source_differs() {
        // Source order is MCP month, week, session, zebra — the API returned the
        // monthly extra first (exactly the captain's zai fault). Canonical order
        // must win: session, week, then extras alphabetically (MCP month < zebra).
        let line = once_line(&scrambled_zai(), NOW);
        let s = line.find("session").unwrap();
        let w = line.find("week").unwrap();
        let m = line.find("MCP month").unwrap();
        let z = line.find("zebra").unwrap();
        assert!(s < w, "session before week: {line}");
        assert!(w < m, "week before extras: {line}");
        assert!(m < z, "extras alphabetical (MCP month < zebra): {line}");
        // And the extra that the API returned first is no longer leading.
        assert!(s < m, "session leads, not the API-first MCP month: {line}");
    }

    #[test]
    fn canonical_order_in_tui_row_when_source_differs() {
        let line = render(&build_provider_line(&scrambled_zai(), NOW, 130));
        let s = line.find("session").unwrap();
        let w = line.find("week").unwrap();
        let m = line.find("MCP month").unwrap();
        let z = line.find("zebra").unwrap();
        assert!(
            s < w && w < m && m < z,
            "canonical order in TUI row: {line}"
        );
    }

    #[test]
    fn unknown_extra_window_sorts_as_extra() {
        // A window labelled with a label no provider list knows about still
        // lands after week on its own (rank 2), not first as the API returned.
        let mut p = claude();
        p.windows = vec![
            QuotaWindow {
                id: "model:zebra".to_string(),
                label: "Zebra pool".to_string(),
                percent_used: 7.0,
                resets_at: None,
            },
            QuotaWindow {
                id: "five_hour".to_string(),
                label: "session".to_string(),
                percent_used: 5.0,
                resets_at: None,
            },
        ];
        let line = render(&build_provider_line(&p, NOW, 130));
        assert!(
            line.find("session").unwrap() < line.find("zebra").unwrap(),
            "{line}"
        );
    }

    // --- Fault 2: aligned columns across rows ---

    /// A second live provider with a session/week/extra layout whose extra label
    /// ("MCP month", 9 chars) is wider than claude's ("fable", 5) and whose
    /// resets are shorter, so alignment is non-trivial. Source order canonical.
    fn zai_aligned() -> ProviderQuota {
        ProviderQuota {
            id: "zai".to_string(),
            label: "Z.AI".to_string(),
            plan: None,
            windows: vec![
                QuotaWindow {
                    id: "five_hour".to_string(),
                    label: "session".to_string(),
                    percent_used: 42.0,
                    resets_at: Some("2026-08-13T14:38:00Z".to_string()),
                },
                QuotaWindow {
                    id: "seven_day".to_string(),
                    label: "week".to_string(),
                    percent_used: 51.0,
                    resets_at: Some("2026-08-14T21:55:00Z".to_string()),
                },
                QuotaWindow {
                    id: "mcp_month".to_string(),
                    label: "MCP month".to_string(),
                    percent_used: 0.0,
                    resets_at: Some("2026-08-24T21:55:00Z".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        }
    }

    #[test]
    fn aligned_block_bars_line_up_across_rows() {
        // Two rows whose labels AND reset widths differ. The bars of every
        // window kind must start at the same column down the rows — including
        // the extra column, where "fable" (5) and "MCP month" (9) differ.
        let claude = claude();
        let zai = zai_aligned();
        let providers = vec![&claude, &zai];
        let block = build_quota_rows(&providers, NOW, 140);
        assert_eq!(block.len(), 2, "two rows");
        let a = render(&block[0]);
        let b = render(&block[1]);
        let sa = bar_starts(&a);
        let sb = bar_starts(&b);
        assert_eq!(sa.len(), 3, "claude has 3 bars: {a}");
        assert_eq!(sb.len(), 3, "zai has 3 bars: {b}");
        assert_eq!(
            sa, sb,
            "bars must line up across rows:\n  claude: {a}\n  zai:    {b}"
        );
        for (row, label) in [(&a, "claude"), (&b, "zai")] {
            assert!(
                row.chars().count() <= 140,
                "{label} fits ({})",
                row.chars().count()
            );
        }
    }

    #[test]
    fn aligned_block_lines_up_label_columns_too() {
        // Shared column grid, not just the bars: each shared window's label
        // starts at the same x in every row, and the extra column (whose labels
        // differ: "fable" vs "MCP month") still starts its bar at the same x
        // because the shorter label is padded to the column width.
        let claude = claude();
        let zai = zai_aligned();
        let block = build_quota_rows(&[&claude, &zai], NOW, 140);
        let a = render(&block[0]);
        let b = render(&block[1]);
        for token in ["session", "week"] {
            assert_eq!(
                a.find(token),
                b.find(token),
                "{token} label aligned:\n  {a}\n  {b}"
            );
        }
        assert_eq!(
            bar_starts(&a)[2],
            bar_starts(&b)[2],
            "extra-column bar aligned despite differing labels:\n  {a}\n  {b}"
        );
    }

    #[test]
    fn single_provider_in_block_is_not_padded() {
        // One provider alone must render exactly as the single-row path: no
        // column padding for an absent neighbour.
        let block = build_quota_rows(&[&claude()], NOW, 110);
        assert_eq!(block.len(), 1);
        assert_eq!(
            render(&block[0]),
            render(&build_provider_line(&claude(), NOW, 110)),
            "single-provider block == single-row ladder"
        );
    }

    #[test]
    fn lone_provider_with_only_extras_is_not_leading_padded() {
        // A provider whose windows are all extras (no session/week) must not get
        // blank-padded for the absent session/week columns when it is alone —
        // there is no other row to align against, so no padding is warranted.
        let extras_only = ProviderQuota {
            id: "novel".to_string(),
            label: "Novel".to_string(),
            plan: None,
            windows: vec![
                QuotaWindow {
                    id: "model:alpha".to_string(),
                    label: "Alpha".to_string(),
                    percent_used: 10.0,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
                QuotaWindow {
                    id: "model:beta".to_string(),
                    label: "Beta".to_string(),
                    percent_used: 20.0,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        };
        let block = build_quota_rows(&[&extras_only], NOW, 120);
        assert_eq!(block.len(), 1);
        let line = render(&block[0]);
        // Alone, it renders exactly as the single-row path (no neighbour → no
        // padding for the absent session/week columns).
        assert_eq!(
            line,
            render(&build_provider_line(&extras_only, NOW, 120)),
            "lone extras-only provider == single-row path"
        );
        // Concretely, no blanked-column gap: a padded session/week column would
        // be ~20+ spaces; legitimate field/separator spacing never reaches 8.
        assert!(!line.contains("        "), "no blanked-column gap: {line}");
        // Extras land in alphabetical order.
        assert!(
            line.find("alpha").unwrap() < line.find("beta").unwrap(),
            "{line}"
        );
    }

    #[test]
    fn aligned_block_degrades_without_overflow_when_narrow() {
        // At a width where no aligned tier fits, alignment is dropped and each
        // row degrades independently (hard-truncate at absurd widths) — never
        // wraps or overflows.
        let claude = claude();
        let zai = zai_aligned();
        let providers = vec![&claude, &zai];
        let block = build_quota_rows(&providers, NOW, 20);
        assert_eq!(block.len(), 2);
        for (i, row) in block.iter().enumerate() {
            let line = render(row);
            assert!(
                line.chars().count() <= 20,
                "row {i} fits ({}): {line}",
                line.chars().count()
            );
        }
    }

    #[test]
    fn alignment_is_dropped_before_any_row_degrades() {
        // At a width that fits every row's own best tier but not the aligned
        // block, alignment is the fidelity that goes — not the bars. Each row
        // must render exactly as it does on its own (12-cell bars + resets),
        // rather than both rows dropping to a narrower shared tier.
        let claude = claude();
        let zai = zai_aligned();
        let solo_claude = render(&build_provider_line(&claude, NOW, 300));
        let solo_zai = render(&build_provider_line(&zai, NOW, 300));
        let width = solo_claude.chars().count().max(solo_zai.chars().count());
        let block = build_quota_rows(&[&claude, &zai], NOW, width);
        assert_eq!(
            (render(&block[0]), render(&block[1])),
            (solo_claude.clone(), solo_zai.clone()),
            "alignment dropped, each row keeps its own tier"
        );
        // Concretely: the top tier's 12-cell bars and resets survive.
        assert!(solo_claude.contains("1h45m"), "{solo_claude}");
        assert_eq!(
            bar_starts(&render(&block[0])).len(),
            3,
            "{}",
            render(&block[0])
        );
    }

    #[test]
    fn row_ending_at_an_interior_column_has_no_trailing_pad() {
        // zai stops at `week` while claude carries a third column, and zai's
        // week reset ("1d8h") is shorter than claude's ("3d22h"). The shared
        // reset width must not pad zai's *last* field: nothing follows it on
        // that row, and ui.rs appends its own suffixes (" stale") right after.
        let claude = claude();
        let mut zai = zai_aligned();
        zai.windows.retain(|w| w.label != "MCP month");
        let block = build_quota_rows(&[&claude, &zai], NOW, 200);
        let a = render(&block[0]);
        let b = render(&block[1]);
        assert!(b.ends_with("1d8h"), "no trailing pad: {b:?}");
        let sa = bar_starts(&a);
        let sb = bar_starts(&b);
        assert_eq!(
            sa[..2],
            sb[..],
            "shared columns still aligned:\n  {a}\n  {b}"
        );
    }

    #[test]
    fn block_without_session_or_week_has_no_leading_separator() {
        // Every window is an extra, so the block starts at column 2. The
        // inter-column separator belongs between columns: the first label must
        // follow the id field directly, not after two extra spaces.
        let extras = |id: &str, pct: f64| ProviderQuota {
            id: id.to_string(),
            label: id.to_string(),
            plan: None,
            windows: vec![
                QuotaWindow {
                    id: "model:alpha".to_string(),
                    label: "Alpha".to_string(),
                    percent_used: pct,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
                QuotaWindow {
                    id: "model:beta".to_string(),
                    label: "Beta".to_string(),
                    percent_used: pct,
                    resets_at: Some("2026-08-17T12:00:00.000000+00:00".to_string()),
                },
            ],
            status: ProviderStatus::Fresh,
            stale: false,
            error: None,
        };
        let lone = extras("novel", 10.0);
        let line = render(&build_provider_line(&lone, NOW, 120));
        assert!(line.starts_with(" novel  alpha "), "{line:?}");
        // And in a block, the rows still line up on that first extra column.
        let other = extras("zai", 20.0);
        let block = build_quota_rows(&[&lone, &other], NOW, 120);
        let a = render(&block[0]);
        let b = render(&block[1]);
        assert!(a.starts_with(" novel  alpha "), "{a:?}");
        assert!(b.starts_with(" zai    alpha "), "{b:?}");
        assert_eq!(bar_starts(&a), bar_starts(&b), "aligned:\n  {a}\n  {b}");
    }

    #[test]
    fn dim_provider_among_live_keeps_live_rows_aligned() {
        // An explicit selection may include a non-live provider; it renders its
        // dim line independently while the live rows still align their bars.
        let claude = claude();
        let zai = zai_aligned();
        let copilot = ProviderQuota {
            id: "copilot".to_string(),
            label: "GitHub Copilot".to_string(),
            plan: None,
            windows: vec![],
            status: ProviderStatus::AuthRequired,
            stale: false,
            error: Some("GitHub Copilot sign-in required".to_string()),
        };
        let providers = vec![&copilot, &claude, &zai];
        let block = build_quota_rows(&providers, NOW, 140);
        assert_eq!(block.len(), 3);
        assert_eq!(render(&block[0]), " copilot sign-in required");
        let a = render(&block[1]);
        let b = render(&block[2]);
        assert_eq!(bar_starts(&a), bar_starts(&b), "live rows still aligned");
    }
}