ebman 0.31.0

k9s-style TUI for AWS Elastic Beanstalk
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
//! Render tests for the `ui` module tree.
//!
//! Lifted out of `src/ui.rs` in the 0.31 split, unchanged. They reach
//! their subjects through `super::*`, which the root's glob
//! re-exports resolve to whichever sibling now owns each item — so a
//! later move between view modules doesn't touch this file.

#![cfg(test)]

use super::*;

#[test]
fn warn_glyph_falls_back_in_ascii() {
    assert_eq!(super::warn_glyph(IconStyle::Ascii), "! ");
    assert_eq!(super::warn_glyph(IconStyle::Unicode), "");
    assert_eq!(super::warn_glyph(IconStyle::Powerline), "");
}

// Overlay-sizing invariants moved with the code to `tui-common::overlay`'s
// own test module; no need to keep parallel copies here.

#[test]
fn stale_glyph_falls_back_in_ascii() {
    assert_eq!(super::stale_glyph(IconStyle::Ascii), "^");
    assert_eq!(super::stale_glyph(IconStyle::Unicode), "");
}

#[test]
fn detail_keystrip_has_no_duplicate_keys() {
    for tab in [
        DetailTab::Health,
        DetailTab::Events,
        DetailTab::Instances,
        DetailTab::Metrics,
        DetailTab::Queue,
        DetailTab::Logs,
        DetailTab::Config,
    ] {
        assert!(
            !detail_tab_keys(tab).is_empty(),
            "no tab-specific keys for {tab:?}"
        );
        let mut seen = std::collections::HashSet::new();
        for (key, _) in detail_tab_keys(tab).iter().chain(DETAIL_GLOBAL_KEYS.iter()) {
            assert!(
                seen.insert(*key),
                "key {key:?} listed twice on the {tab:?} strip"
            );
        }
    }
}

#[test]
fn render_detail_keystrip_carries_tab_name_and_global_keys() {
    let theme = Theme::dark();
    let line = render_detail_keystrip(DetailTab::Config, &theme);
    let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.contains("CONFIG"), "missing tab name: {text:?}");
    assert!(
        text.contains("rename"),
        "missing tab-specific key: {text:?}"
    );
    assert!(text.contains("esc"), "missing global key: {text:?}");
}

#[test]
fn status_alert_tiers_by_health_and_dlq() {
    // Red / Severe → red, regardless of DLQ.
    assert_eq!(status_alert("Red", 0), StatusAlert::Red);
    assert_eq!(status_alert("Severe", 0), StatusAlert::Red);
    assert_eq!(status_alert("severe", 99), StatusAlert::Red);
    // Yellow band → yellow.
    assert_eq!(status_alert("Yellow", 0), StatusAlert::Yellow);
    assert_eq!(status_alert("Warning", 0), StatusAlert::Yellow);
    assert_eq!(status_alert("Degraded", 0), StatusAlert::Yellow);
    // Green env with worker DLQ items → yellow (warning, not alarm).
    assert_eq!(status_alert("Green", 1), StatusAlert::Yellow);
    assert_eq!(status_alert("Ok", 50), StatusAlert::Yellow);
    // Healthy env, no DLQ → no alert tier.
    assert_eq!(status_alert("Green", 0), StatusAlert::None);
    assert_eq!(status_alert("Ok", 0), StatusAlert::None);
    assert_eq!(status_alert("", 0), StatusAlert::None);
    // Red health *with* DLQ stays Red (Red wins over Yellow).
    assert_eq!(status_alert("Red", 200), StatusAlert::Red);
}

#[test]
fn format_instance_counts_tiers_by_ratio() {
    use crate::aws::EnvInstanceCounts;
    let theme = crate::theme::Theme::dark();

    // Missing data → em-dash + muted (avoids "0/0 = broken" misread).
    assert_eq!(format_instance_counts(None, &theme).0, "");
    assert_eq!(format_instance_counts(None, &theme).1, theme.muted);

    // Empty env (no instances right now) → muted, not red.
    let (text, color) = format_instance_counts(
        Some(EnvInstanceCounts {
            healthy: 0,
            total: 0,
        }),
        &theme,
    );
    assert_eq!(text, "0/0");
    assert_eq!(color, theme.muted);

    // All healthy → green.
    let (text, color) = format_instance_counts(
        Some(EnvInstanceCounts {
            healthy: 3,
            total: 3,
        }),
        &theme,
    );
    assert_eq!(text, "3/3");
    assert_eq!(color, theme.health_green);

    // Partial (some healthy, some not) → yellow.
    let (text, color) = format_instance_counts(
        Some(EnvInstanceCounts {
            healthy: 2,
            total: 3,
        }),
        &theme,
    );
    assert_eq!(text, "2/3");
    assert_eq!(color, theme.health_yellow);

    // None healthy with instances present → red.
    let (text, color) = format_instance_counts(
        Some(EnvInstanceCounts {
            healthy: 0,
            total: 1,
        }),
        &theme,
    );
    assert_eq!(text, "0/1");
    assert_eq!(color, theme.health_red);
}

#[test]
fn status_pill_for_alerting_returns_text_without_pill_bg() {
    // Pins the 0.6 visual fix (commit bf64ce0): when an env is in an
    // alert tier (Red / Yellow), the `Ready` pill must render as
    // styled text — fg = health colour, no bg — so the row's red /
    // yellow tint shows through. A solid-green pill on an alerting
    // row reads as "fine" at a glance, which the fix exists to stop.
    let theme = crate::theme::Theme::dark();

    let red = super::status_pill_for("Ready", &theme, super::StatusAlert::Red);
    assert_eq!(
        red.style.fg,
        Some(theme.health_red),
        "Red alert: fg = theme.health_red"
    );
    assert!(
        red.style.bg.is_none(),
        "Red alert: no bg (so row tint shows through), got {:?}",
        red.style.bg,
    );

    let yellow = super::status_pill_for("Ready", &theme, super::StatusAlert::Yellow);
    assert_eq!(yellow.style.fg, Some(theme.health_yellow));
    assert!(
        yellow.style.bg.is_none(),
        "Yellow alert: no bg, got {:?}",
        yellow.style.bg,
    );

    // No alert: the bright-green Ready pill (with explicit bg) is
    // still what we want.
    let none = super::status_pill_for("Ready", &theme, super::StatusAlert::None);
    assert_eq!(
        none.style.bg,
        Some(theme.status_ready),
        "No alert: solid green pill"
    );
}

#[test]
fn why_overlay_title_is_framed_by_health() {
    // Red / Severe → diagnostic framing.
    assert_eq!(why_overlay_title("prod", "Red"), "why is prod red?");
    assert_eq!(why_overlay_title("prod", "Severe"), "why is prod red?");
    // Yellow family → amber framing (matches operator vocabulary).
    assert_eq!(why_overlay_title("prod", "Yellow"), "why is prod amber?");
    assert_eq!(why_overlay_title("prod", "Warning"), "why is prod amber?");
    assert_eq!(why_overlay_title("prod", "Degraded"), "why is prod amber?");
    // Green / Ok / Info / NoData / Pending / Grey / unknown / blank
    // → neutral "recent activity" framing (operator's just looking).
    assert_eq!(why_overlay_title("prod", "Green"), "prod — recent activity");
    assert_eq!(why_overlay_title("prod", "Ok"), "prod — recent activity");
    assert_eq!(why_overlay_title("prod", ""), "prod — recent activity");
    // Health string is matched case-insensitively (EB's casing varies).
    assert_eq!(why_overlay_title("prod", "red"), "why is prod red?");
    assert_eq!(why_overlay_title("prod", "yellow"), "why is prod amber?");
}

#[test]
fn hint_glyph_falls_back_in_ascii() {
    assert_eq!(super::hint_glyph(IconStyle::Ascii), "? ");
    assert_eq!(super::hint_glyph(IconStyle::Unicode), "💡 ");
}

#[test]
fn stripe_glyph_falls_back_in_ascii() {
    assert_eq!(super::stripe_glyph(IconStyle::Ascii), "|");
    assert_eq!(super::stripe_glyph(IconStyle::Unicode), "");
}

#[test]
fn prune_pills_keeps_first_under_width() {
    let theme = crate::theme::Theme::dark();
    let mut pills: Vec<(String, ratatui::style::Color, ratatui::style::Color)> = vec![
        ("ALERTS".into(), Color::Black, theme.health_red),
        ("PENDING".into(), Color::Black, theme.health_yellow),
        ("READ-ONLY".into(), Color::Black, theme.health_green),
    ];
    super::prune_pills_to_width(&mut pills, &theme, 10);
    // First pill always kept even when nothing fits.
    assert!(!pills.is_empty());
    assert_eq!(pills[0].0.split(' ').next().unwrap(), "ALERTS");
}

#[test]
fn prune_pills_marks_overflow_count_on_last_pill() {
    let theme = crate::theme::Theme::dark();
    let mut pills: Vec<(String, ratatui::style::Color, ratatui::style::Color)> = vec![
        ("ALERTS".into(), Color::Black, theme.health_red),
        ("PENDING".into(), Color::Black, theme.health_yellow),
        ("READ-ONLY".into(), Color::Black, theme.health_green),
        ("UPDATE".into(), Color::Black, theme.title_alt),
    ];
    // Tight budget that fits one pill: marker appears on the survivor.
    super::prune_pills_to_width(&mut pills, &theme, 10);
    assert_eq!(pills.len(), 1);
    assert!(
        pills[0].0.contains("+3"),
        "expected last-pill marker '+3' on survivor, got {:?}",
        pills[0].0
    );
}

#[test]
fn prune_pills_noop_when_chain_fits() {
    let theme = crate::theme::Theme::dark();
    let mut pills: Vec<(String, ratatui::style::Color, ratatui::style::Color)> = vec![
        ("A".into(), Color::Black, theme.health_red),
        ("B".into(), Color::Black, theme.health_yellow),
    ];
    let before = pills.clone();
    super::prune_pills_to_width(&mut pills, &theme, 1_000);
    assert_eq!(pills, before, "wide budget should not trim or mark");
}

#[test]
fn hover_index_maps_column_to_point() {
    use ratatui::layout::Rect;
    let area = Rect::new(10, 0, 11, 5); // x=10..20 (inclusive of x=20-1)
                                        // x=10 → first point; x=20 → last point.
    assert_eq!(super::hover_index(10, area, 11), Some(0));
    assert_eq!(super::hover_index(20, area, 11), Some(10));
    assert_eq!(super::hover_index(15, area, 11), Some(5));
    // Out of range → None.
    assert_eq!(super::hover_index(9, area, 11), None);
    assert_eq!(super::hover_index(21, area, 11), None);
    // Empty series → None even when in range.
    assert_eq!(super::hover_index(10, area, 0), None);
}

#[test]
fn series_anomaly_flags_5xx_spike() {
    let v = vec![1.0, 1.0, 1.0, 1.0, 10.0];
    assert!(super::series_anomaly_label("req5xx", &v, IconStyle::Unicode).is_some());
}

#[test]
fn series_anomaly_quiet_when_stable() {
    let v = vec![5.0, 5.0, 5.0, 5.0, 5.5];
    assert!(super::series_anomaly_label("req5xx", &v, IconStyle::Unicode).is_none());
}

#[test]
fn series_anomaly_ignores_unrelated_id() {
    let v = vec![1.0, 1.0, 1.0, 1.0, 99.0];
    assert!(super::series_anomaly_label("health", &v, IconStyle::Unicode).is_none());
}

#[test]
fn series_anomaly_handles_short_series() {
    let v = vec![1.0, 9.0];
    assert!(super::series_anomaly_label("req5xx", &v, IconStyle::Unicode).is_none());
}

#[test]
fn age_color_fresh_uses_title_alt() {
    let t = Theme::dark();
    let now = chrono::Utc::now();
    let updated = now - chrono::Duration::hours(2);
    assert_eq!(super::age_color(Some(updated), now, &t), t.title_alt);
}

#[test]
fn age_color_normal_uses_text() {
    let t = Theme::dark();
    let now = chrono::Utc::now();
    let updated = now - chrono::Duration::days(5);
    assert_eq!(super::age_color(Some(updated), now, &t), t.text);
}

#[test]
fn age_color_stale_uses_muted() {
    let t = Theme::dark();
    let now = chrono::Utc::now();
    let updated = now - chrono::Duration::days(45);
    assert_eq!(super::age_color(Some(updated), now, &t), t.muted);
}

#[test]
fn age_color_missing_uses_muted() {
    let t = Theme::dark();
    let now = chrono::Utc::now();
    assert_eq!(super::age_color(None, now, &t), t.muted);
}

#[test]
fn age_color_future_clock_skew_is_fresh_not_stale() {
    // If `updated` is slightly in the future (clock drift between EB
    // and the local box), don't classify it as >30d — that would flip
    // the colour straight to muted on a brand-new env.
    let t = Theme::dark();
    let now = chrono::Utc::now();
    let updated = now + chrono::Duration::seconds(30);
    assert_eq!(super::age_color(Some(updated), now, &t), t.title_alt);
}

#[test]
fn age_color_boundary_at_24h_is_normal() {
    // Exactly 24h: dur < 24h is false, dur > 30d is false → normal (text).
    let t = Theme::dark();
    let now = chrono::Utc::now();
    let updated = now - chrono::Duration::hours(24);
    assert_eq!(super::age_color(Some(updated), now, &t), t.text);
}

#[test]
fn humanize_age_buckets() {
    use chrono::Duration;
    assert_eq!(humanize_age(Duration::seconds(45)), "45s");
    assert_eq!(humanize_age(Duration::seconds(120)), "2m");
    assert_eq!(humanize_age(Duration::seconds(3601)), "1h");
    assert_eq!(humanize_age(Duration::seconds(2 * 86_400)), "2d");
    // Negative durations clamp to 0.
    assert_eq!(humanize_age(Duration::seconds(-30)), "0s");
}

#[test]
fn format_event_time_renders_each_mode() {
    use crate::app::EventTimeFormat;
    use chrono::{TimeZone, Utc};
    let t = Utc.with_ymd_and_hms(2026, 5, 21, 22, 34, 56).unwrap();
    let now = Utc.with_ymd_and_hms(2026, 5, 21, 22, 39, 56).unwrap();
    // UTC: full stamp with trailing Z.
    assert_eq!(
        format_event_time(Some(t), EventTimeFormat::Utc, now),
        "2026-05-21 22:34:56Z"
    );
    // Age: 5 minutes elapsed.
    assert_eq!(format_event_time(Some(t), EventTimeFormat::Age, now), "5m");
    // Local: shape only (TZ-dependent) — assert length + no Z suffix.
    let local = format_event_time(Some(t), EventTimeFormat::Local, now);
    assert_eq!(local.len(), 19);
    assert!(!local.ends_with('Z'));
}

#[test]
fn format_event_time_handles_missing_timestamp() {
    use crate::app::EventTimeFormat;
    let now = chrono::Utc::now();
    for mode in [
        EventTimeFormat::Utc,
        EventTimeFormat::Local,
        EventTimeFormat::Age,
    ] {
        assert_eq!(format_event_time(None, mode, now), "");
    }
}

#[test]
fn every_status_glyph_has_an_ascii_form() {
    // `icons = "ascii"` exists for terminals without a usable
    // unicode font — a mode where an unconditional ▲ renders as a
    // replacement box, which for a SORT MARKER or a health delta
    // means the operator can't read what the glyph encodes.
    let ascii = IconStyle::Ascii;
    assert_eq!(super::glyph(ascii, "", "^"), "^");
    assert_eq!(super::glyph(IconStyle::Unicode, "", "^"), "");

    // The anomaly badge builds its text from the glyph, so the
    // whole label has to come through ascii-clean — a `▲` baked
    // into the message string is how this one hid.
    let spike = vec![1.0, 1.0, 1.0, 99.0];
    let label =
        super::series_anomaly_label("req5xx", &spike, ascii).expect("a 99x spike is an anomaly");
    assert!(!label.contains(''), "ascii mode still emitted ▲: {label}");
    assert!(label.starts_with('^'), "{label}");
    let label = super::series_anomaly_label("req5xx", &spike, IconStyle::Unicode)
        .expect("still fires in unicode mode");
    assert!(label.starts_with(''), "{label}");
}

#[test]
fn config_scroll_follow_keeps_cursor_in_viewport() {
    // Cursor in view → offset unchanged.
    assert_eq!(config_scroll_follow(0, Some(5), 20, 100), 0);
    // Cursor below the fold → scroll so cursor is the last visible row.
    assert_eq!(config_scroll_follow(0, Some(25), 20, 100), 6);
    // Cursor above the current offset → scroll up to it.
    assert_eq!(config_scroll_follow(30, Some(10), 20, 100), 10);
    // Never scroll past the end: max = total - viewport.
    assert_eq!(config_scroll_follow(0, Some(99), 20, 100), 80);
    // No editable row → offset just clamped, not moved by a cursor.
    assert_eq!(config_scroll_follow(50, None, 20, 100), 50);
    assert_eq!(config_scroll_follow(95, None, 20, 100), 80);
    // Content shorter than the viewport → no scroll at all.
    assert_eq!(config_scroll_follow(0, Some(3), 20, 8), 0);
}

#[test]
fn about_layout_picks_by_terminal_size() {
    // text_h ~15 — a representative project-text height.
    // ABOUT_SCENE_W is 40, ABOUT_TEXT_W is 58; thresholds:
    // Stacked needs w >= max(40, 58) + 6 = 64,
    // SideBySide needs w >= 40 + 58 + 8 = 106.
    let th = 15;
    // Roomy → scene stacked above text.
    assert_eq!(about_layout(120, 60, th), AboutLayout::Stacked);
    // Wide but short → scene beside text.
    assert_eq!(about_layout(140, 30, th), AboutLayout::SideBySide);
    // Small both ways → text only.
    assert_eq!(about_layout(40, 20, th), AboutLayout::TextOnly);
    // Wide enough to stack but too narrow for side-by-side → text only.
    assert_eq!(about_layout(44, 60, th), AboutLayout::TextOnly);
    // Tall enough but too narrow for the scene → text only.
    assert_eq!(about_layout(42, 60, th), AboutLayout::TextOnly);
    // 50 cols would have picked Stacked under the old 46-col
    // threshold, but the text lines need 58 cols to render
    // without mid-word wrap. Must fall through to TextOnly.
    assert_eq!(about_layout(50, 60, th), AboutLayout::TextOnly);
    // Right at the new Stacked boundary.
    assert_eq!(about_layout(64, 60, th), AboutLayout::Stacked);
    assert_eq!(about_layout(63, 60, th), AboutLayout::TextOnly);
}

#[test]
fn event_time_width_matches_rendered_stamp() {
    use crate::app::EventTimeFormat;
    // UTC width must fit "YYYY-MM-DD HH:MM:SSZ" (20 chars).
    assert_eq!(event_time_width(EventTimeFormat::Utc), 20);
    assert_eq!(event_time_width(EventTimeFormat::Local), 19);
    assert_eq!(event_time_width(EventTimeFormat::Age), 4);
}

#[test]
fn humanize_duration_buckets() {
    assert_eq!(humanize_duration(15), "15s");
    assert_eq!(humanize_duration(90), "1m");
    assert_eq!(humanize_duration(3700), "1h1m");
    assert_eq!(humanize_duration(2 * 86_400 + 3 * 3600), "2d3h");
}

#[test]
fn humanize_range_picks_unit() {
    assert_eq!(humanize_range(900), "15m");
    assert_eq!(humanize_range(3600), "1h");
    assert_eq!(humanize_range(2 * 86_400), "2d");
}

#[test]
fn short_caller_extracts_principal() {
    assert_eq!(
        short_caller("arn:aws:iam::123456789012:user/alice"),
        "user/alice"
    );
    assert_eq!(
        short_caller("arn:aws:sts::123456789012:assumed-role/Foo/session-name"),
        "assumed-role/Foo/session-name"
    );
    assert_eq!(short_caller("not-an-arn"), "not-an-arn");
}

#[test]
fn redact_passthrough_when_off() {
    assert_eq!(redact("hello", false), "hello");
}

#[test]
fn redact_blocks_chars_when_on() {
    let out = redact("hello", true);
    assert_eq!(out.chars().count(), 5);
    assert!(out.chars().all(|c| c == ''));
}

#[test]
fn redact_keeps_placeholder() {
    assert_eq!(redact("", true), "");
    assert_eq!(redact("", true), "");
}

#[test]
fn format_metric_branches() {
    assert_eq!(format_metric("health", 12.0), "12");
    assert_eq!(format_metric("p90", 0.250), "250ms");
    assert_eq!(format_metric("p90", 1.5), "1.50s");
    assert_eq!(format_metric("req4xx", 42.0), "42");
}

#[test]
fn micro_bar_renders() {
    assert_eq!(micro_bar(0, 100, 10), "");
    let half = micro_bar(50, 100, 10);
    // Should be 5 full blocks plus no remainder.
    assert!(half.chars().count() <= 10);
    assert!(half.chars().any(|c| c == ''));
    let full = micro_bar(100, 100, 10);
    assert_eq!(full.chars().count(), 10);
}

#[test]
fn micro_bar_guards_invalid_inputs() {
    assert_eq!(micro_bar(10, 0, 10), "");
    assert_eq!(micro_bar(10, 100, 0), "");
    assert_eq!(micro_bar(-5, 100, 10), "");
    // Above max clamps to full bar.
    assert_eq!(micro_bar(999, 100, 5).chars().count(), 5);
}

#[test]
fn spinner_cycles_through_frames() {
    // Same window → same frame.
    let a = spinner(150, IconStyle::Unicode);
    let b = spinner(199, IconStyle::Unicode);
    assert_eq!(a, b);
    // Next window → different frame.
    assert_ne!(a, spinner(250, IconStyle::Unicode));
    // ASCII fallback uses a different palette.
    assert!(SPINNER_FRAMES.contains(&a));
    let ascii = spinner(0, IconStyle::Ascii);
    assert!(ASCII_SPINNER.contains(&ascii));
}

#[test]
fn visible_window_anchors_to_top_when_items_fit() {
    // Items <= budget → window covers everything from 0.
    assert_eq!(visible_window(0, 5, 10), (0, 5));
    assert_eq!(visible_window(4, 5, 10), (0, 5));
}

#[test]
fn visible_window_slides_to_keep_cursor_visible() {
    // 20 items, budget 5: cursor near top anchors to 0.
    assert_eq!(visible_window(0, 20, 5), (0, 5));
    assert_eq!(visible_window(1, 20, 5), (0, 5));
    // Cursor in middle centres.
    let (s, e) = visible_window(10, 20, 5);
    assert!(s <= 10 && 10 < e, "expected cursor 10 inside [{s},{e})");
    assert_eq!(e - s, 5);
    // Cursor at end clamps so the window doesn't run off.
    assert_eq!(visible_window(19, 20, 5), (15, 20));
}

#[test]
fn visible_window_handles_empty_and_zero_budget() {
    assert_eq!(visible_window(0, 0, 10), (0, 0));
    // Zero budget: degenerate but must not crash; treat as 1.
    let (s, e) = visible_window(3, 10, 0);
    assert!(s <= 3 && 3 < e);
}

#[test]
fn cursor_marker_swaps_per_icon_style() {
    let mut t = Theme::dark();
    t.icons = IconStyle::Unicode;
    assert_eq!(cursor_marker(&t), "");
    t.icons = IconStyle::Ascii;
    assert_eq!(cursor_marker(&t), "");
    t.icons = IconStyle::Powerline;
    assert!(cursor_marker(&t).contains('\u{e0b0}'));
}

#[test]
fn highlight_env_in_summary_breaks_at_quoted_name() {
    let body = Style::default().fg(Color::White);
    let name = Style::default().fg(Color::Red);
    let line = highlight_env_in_summary(
        "Rebuild env 'prod-api'? (terminates and recreates)",
        "prod-api",
        body,
        name,
    );
    // Expect at least 3 spans: leading "  " padding + body prefix +
    // env-name + body suffix. The name span should not contain quotes.
    let env_spans: Vec<&Span> = line
        .spans
        .iter()
        .filter(|s| s.content.contains("prod-api"))
        .collect();
    assert_eq!(env_spans.len(), 1);
    assert!(
        !env_spans[0].content.contains('\''),
        "name span should not include the surrounding single quotes: {:?}",
        env_spans[0].content
    );
}

#[test]
fn highlight_env_in_summary_falls_back_when_name_missing() {
    let body = Style::default().fg(Color::White);
    let name = Style::default().fg(Color::Red);
    let line =
        highlight_env_in_summary("Some action with no env reference", "prod-api", body, name);
    // Should still render — just as one body span (plus the leading
    // "  " padding span).
    assert!(line.spans.iter().any(|s| s.content.contains("Some action")));
}

#[test]
fn summarize_in_flight_collapses_duplicates() {
    let s = summarize_in_flight(&["Rebuild env", "Rebuild env", "Deploy version"]);
    assert!(s.contains("rebuild ×2"), "got {s:?}");
    assert!(s.contains("deploy"), "got {s:?}");
}

#[test]
fn summarize_in_flight_truncates() {
    let s = summarize_in_flight(&[
        "Terminate env",
        "Rebuild env",
        "Restart env",
        "Deploy version",
        "Swap CNAMEs",
    ]);
    assert!(
        s.chars().count() <= 25,
        "got {} chars: {s:?}",
        s.chars().count()
    );
}

#[test]
fn summarize_in_flight_empty() {
    assert_eq!(summarize_in_flight(&[]), "");
}

#[test]
fn summarize_group_omits_empty_buckets() {
    // Build envs with the minimal fields we use in summarize_group.
    // The full Environment struct has many fields; spread defaults
    // for the others.
    fn e(tier: &str, health: &str) -> Environment {
        Environment {
            name: "n".into(),
            application: "a".into(),
            tier: tier.into(),
            status: "Ready".into(),
            health: health.into(),
            cname: "".into(),
            platform: "".into(),
            solution_stack: "".into(),
            version_label: "".into(),
            updated: None,
            id: None,
            region: None,
            arn: None,
        }
    }
    let envs = [e("Web", "Green"), e("Web", "Green"), e("Web", "Red")];
    let refs: Vec<&Environment> = envs.iter().collect();
    let s = summarize_group(&refs);
    // 3 envs, all web (no worker), 1 red — only the non-empty buckets
    // appear. Tier split omitted because everyone is web.
    assert!(s.contains("3 envs"));
    assert!(s.contains("1 red"));
    assert!(!s.contains("Worker"));
    assert!(!s.contains("yellow"));
}

#[test]
fn summarize_group_shows_tier_split_when_both_present() {
    fn e(tier: &str, health: &str) -> Environment {
        Environment {
            name: "n".into(),
            application: "a".into(),
            tier: tier.into(),
            status: "Ready".into(),
            health: health.into(),
            cname: "".into(),
            platform: "".into(),
            solution_stack: "".into(),
            version_label: "".into(),
            updated: None,
            id: None,
            region: None,
            arn: None,
        }
    }
    let envs = [e("Web", "Green"), e("Worker", "Yellow"), e("Worker", "Red")];
    let refs: Vec<&Environment> = envs.iter().collect();
    let s = summarize_group(&refs);
    assert!(s.contains("1 Web"));
    assert!(s.contains("2 Worker"));
    assert!(s.contains("1 red"));
    assert!(s.contains("1 yellow"));
}

#[test]
fn summarize_group_empty_input() {
    assert_eq!(summarize_group(&[]), "");
}

#[test]
fn action_glyph_is_distinct_per_action_per_icon_style() {
    use crate::app::ACTIONS;
    use std::collections::HashSet;
    // Within Powerline mode every action glyph should be distinct
    // (so Terminate doesn't share with Restart, etc.) modulo the
    // intentional Terminate / TerminateInstance / ConfigDelete reuse
    // of the trash icon. We assert "not all the same".
    for icons in [IconStyle::Unicode, IconStyle::Ascii, IconStyle::Powerline] {
        let glyphs: HashSet<&str> = ACTIONS.iter().map(|a| a.glyph(icons)).collect();
        assert!(
            glyphs.len() >= ACTIONS.len() / 2,
            "too many action-glyph collisions in {icons:?}: {glyphs:?}"
        );
    }
}

#[test]
fn format_refresh_label_relative_with_recent_refresh() {
    let interval = std::time::Duration::from_secs(15);
    let last = chrono::Utc::now() - chrono::Duration::seconds(3);
    let now = chrono::Utc::now();
    let label = format_refresh_label(Some(last), now, interval);
    // Tolerate ±1s clock jitter in the test.
    assert!(label.starts_with("3s ago") || label.starts_with("2s ago"));
    assert!(label.contains("next 1") || label.contains("next 12") || label.contains("next 13"));
}

#[test]
fn format_refresh_label_clamps_overdue_to_zero() {
    // Refresh was 30s ago with a 15s interval — countdown should
    // clamp to 0 not show a negative number.
    let interval = std::time::Duration::from_secs(15);
    let now = chrono::Utc::now();
    let last = now - chrono::Duration::seconds(30);
    let label = format_refresh_label(Some(last), now, interval);
    assert!(label.contains("next 0s"), "got {label:?}");
}

#[test]
fn format_refresh_label_handles_no_prior_refresh() {
    let label = format_refresh_label(None, chrono::Utc::now(), std::time::Duration::from_secs(15));
    assert_eq!(label, "— · next 15s");
}

#[test]
fn caret_glyph_falls_back_to_underscore_on_ascii() {
    let mut t = Theme::dark();
    t.icons = IconStyle::Ascii;
    assert_eq!(caret_glyph(&t), "_");
    t.icons = IconStyle::Unicode;
    assert_eq!(caret_glyph(&t), "\u{258e}");
    t.icons = IconStyle::Powerline;
    assert_eq!(caret_glyph(&t), "\u{258e}");
}

#[test]
fn scale_rgb_darkens_proportionally_and_passes_through_named() {
    // 0.5 of (200, 100, 50) → (100, 50, 25). Truncating float → u8 cast.
    assert_eq!(
        super::scale_rgb(Color::Rgb(200, 100, 50), 0.5),
        Color::Rgb(100, 50, 25)
    );
    // Factor 1.0 is identity.
    assert_eq!(
        super::scale_rgb(Color::Rgb(200, 100, 50), 1.0),
        Color::Rgb(200, 100, 50)
    );
    // Factor 0.0 → black.
    assert_eq!(
        super::scale_rgb(Color::Rgb(255, 255, 255), 0.0),
        Color::Rgb(0, 0, 0)
    );
    // Factor clamps to [0, 1] — overflowing values don't yield > 255.
    assert_eq!(
        super::scale_rgb(Color::Rgb(200, 100, 50), 2.0),
        Color::Rgb(200, 100, 50)
    );
    // Non-RGB colours pass through unchanged (no portable darken).
    assert_eq!(super::scale_rgb(Color::Red, 0.5), Color::Red);
}

#[test]
fn truncate_for_display_handles_short_long_and_multibyte() {
    // No truncation when under the cap.
    assert_eq!(super::truncate_for_display("hello", 10), "hello");
    // Exactly at the cap — also untouched.
    assert_eq!(super::truncate_for_display("0123456789", 10), "0123456789");
    // Over the cap — drops chars to fit `…`. max=5 means 4 chars + `…`.
    assert_eq!(super::truncate_for_display("0123456789", 5), "0123…");
    // Multi-byte (each char width 1 in unicode-width terms here) —
    // count by chars, not bytes.
    assert_eq!(super::truncate_for_display("éééééééé", 4), "ééé…");
}

#[test]
fn separator_glyph_falls_back_to_ascii_chevron() {
    assert_eq!(super::separator_glyph(IconStyle::Ascii), ">");
    assert_eq!(super::separator_glyph(IconStyle::Unicode), "");
    // Powerline mode never reaches the non-Powerline banner path in
    // practice (it has its own ribbon renderer) — but the glyph
    // should still be a sensible BMP chevron rather than panicking
    // or returning empty.
    assert_eq!(super::separator_glyph(IconStyle::Powerline), "");
}

#[test]
fn pill_chain_uses_left_wedge_for_lead_in_in_powerline_mode() {
    let mut t = Theme::dark();
    t.icons = IconStyle::Powerline;
    let pills = vec![("ALERT".to_string(), Color::White, Color::Red)];
    let spans = pill_chain(&pills, &t);
    // Expect: lead-in wedge (E0B2) + pill body + trailing wedge (E0B0).
    let first_glyph: String = spans[0].content.to_string();
    assert!(
        first_glyph.contains('\u{e0b2}'),
        "expected U+E0B2 left-pointing wedge as lead-in, got {first_glyph:?}"
    );
    // The trailing arrow at the end of the chain is still E0B0 (right-
    // pointing), so the pill's outline is symmetric: ◀ ALERT ▶.
    let last_glyph: String = spans.last().unwrap().content.to_string();
    assert!(
        last_glyph.contains('\u{e0b0}'),
        "expected U+E0B0 right-pointing wedge as trail-out, got {last_glyph:?}"
    );
}

#[test]
fn pill_chain_no_powerline_glyphs_in_unicode_mode() {
    let mut t = Theme::dark();
    t.icons = IconStyle::Unicode;
    let pills = vec![("ALERT".to_string(), Color::White, Color::Red)];
    let spans = pill_chain(&pills, &t);
    for s in &spans {
        let body = s.content.to_string();
        assert!(
            !body.contains('\u{e0b0}') && !body.contains('\u{e0b2}'),
            "non-Powerline mode emitted a Powerline triangle: {body:?}"
        );
    }
}

#[test]
fn sep_uses_powerline_glyph_when_opted_in() {
    let mut t = Theme::dark();
    t.icons = IconStyle::Unicode;
    let unicode_sep = sep(&t).content.to_string();
    assert!(unicode_sep.contains(''));
    t.icons = IconStyle::Powerline;
    let pl_sep = sep(&t).content.to_string();
    assert!(
        pl_sep.contains('\u{e0b1}'),
        "expected U+E0B1 thin separator, got {pl_sep:?}"
    );
    // ASCII path stays on the bullet — opting *out* of unicode shouldn't
    // accidentally trigger a powerline glyph.
    t.icons = IconStyle::Ascii;
    assert!(sep(&t).content.to_string().contains(''));
}

#[test]
fn tab_icon_is_distinct_per_tab() {
    for icons in [IconStyle::Unicode, IconStyle::Ascii, IconStyle::Powerline] {
        use std::collections::HashSet;
        let tabs = [
            DetailTab::Events,
            DetailTab::Instances,
            DetailTab::Metrics,
            DetailTab::Queue,
            DetailTab::Config,
        ];
        let seen: HashSet<&str> = tabs.iter().map(|t| tab_icon(*t, icons)).collect();
        assert_eq!(seen.len(), tabs.len(), "icons collide for {icons:?}");
    }
}

#[test]
fn titled_block_decorates_per_icon_style() {
    // Crude: render to a buffer and confirm the title text appears.
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;
    use ratatui::widgets::Widget;
    let mut t = Theme::dark();
    let b = titled_block(&t, "ebman", true, t.title);
    let mut buf = Buffer::empty(Rect::new(0, 0, 30, 3));
    b.render(buf.area, &mut buf);
    let rendered = buffer_to_string(&buf);
    assert!(rendered.contains(""));
    assert!(rendered.contains("ebman"));

    t.icons = IconStyle::Ascii;
    let b2 = titled_block(&t, "ebman", true, t.title);
    let mut buf = Buffer::empty(Rect::new(0, 0, 30, 3));
    b2.render(buf.area, &mut buf);
    let rendered = buffer_to_string(&buf);
    assert!(rendered.contains("[ ebman ]"));
    assert!(!rendered.contains(""));
}

#[test]
fn pill_wraps_text_with_padding() {
    let s = pill("READY", Color::Black, Color::Green);
    assert_eq!(s.content.as_ref(), " READY ");
}

#[test]
fn health_dot_falls_back_to_ascii() {
    let mut t = Theme::dark();
    let dot = health_dot("green", &t);
    assert_eq!(dot.content.as_ref(), "");
    t.icons = IconStyle::Ascii;
    let dot = health_dot("green", &t);
    assert_eq!(dot.content.as_ref(), "*");
}

#[test]
fn header_dimensions_merges_pills_when_room_to_spare() {
    // Info row 60w + 2 gap + 20w chain = 82, well inside 120w column.
    let (rows, merge) = header_dimensions(60, 20, 120, false);
    assert!(merge, "wide window should merge pills onto info row");
    assert_eq!(rows, 5, "merged layout uses 5 rows (2 borders + 3 content)");
}

#[test]
fn header_dimensions_keeps_pill_row_when_too_narrow() {
    // Info row 60w + 2 gap + 30w chain = 92 > 80w column — has to wrap.
    let (rows, merge) = header_dimensions(60, 30, 80, false);
    assert!(!merge, "narrow window should keep pills on their own row");
    assert_eq!(rows, 6, "split layout adds one row for the pill chain");
}

#[test]
fn header_dimensions_with_no_pills_uses_compact_layout() {
    // No pills present (chain_w == 0): never merges, never reserves a pill row.
    let (rows, merge) = header_dimensions(60, 0, 80, false);
    assert!(!merge);
    assert_eq!(rows, 5);
}

#[test]
fn header_dimensions_adds_row_for_saved_filters() {
    let (rows, _) = header_dimensions(60, 0, 200, true);
    assert_eq!(rows, 6, "saved-filter chip bar adds one row");

    let (rows_with_pills, merged) = header_dimensions(60, 20, 200, true);
    assert!(merged);
    assert_eq!(rows_with_pills, 6, "merged + filters = 5 + 1");
}

#[test]
fn header_dimensions_boundary_is_inclusive() {
    // info(50) + gap(2) + chain(48) == inner(100) → should merge (≤).
    let (_, merge) = header_dimensions(50, 48, 100, false);
    assert!(merge);
    // One column over → no longer merges.
    let (_, merge) = header_dimensions(50, 49, 100, false);
    assert!(!merge);
}

fn buffer_to_string(buf: &ratatui::buffer::Buffer) -> String {
    let mut out = String::new();
    for y in 0..buf.area.height {
        for x in 0..buf.area.width {
            let cell = &buf[(x, y)];
            out.push_str(cell.symbol());
        }
        out.push('\n');
    }
    out
}

/// Regression test pinning ratatui 0.29's broken OSC 8 behavior.
/// Verified by experiment: each byte of an escape sequence
/// (including the leading `\x1b`) is treated as a 1-cell-wide
/// printing character — there's no special handling for control
/// sequences in ratatui's `Buffer::set_stringn` path. The
/// consequences for OSC 8 hyperlinks:
///
/// - The 24-byte opener `\x1b]8;;https://example.com\x1b\\`
///   consumes 24 cells of layout space.
/// - The visible text `Click` gets pushed past the buffer width
///   (or past the column the caller intended).
/// - The escape bytes get rendered as visible control characters
///   in terminals that don't recognise them mid-cell.
///
/// This test pins the broken behavior so that if a future
/// ratatui upgrade adds OSC 8 (or zero-width control) support,
/// it will fail loudly and prompt us to revisit the feature.
/// Currently shipping OSC 8 would require a custom widget that
/// bypasses ratatui's diff renderer, which is too invasive
/// for the value — see BACKLOG.
#[test]
fn osc8_in_span_is_split_into_per_byte_cells_ratatui_0_29_limitation() {
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;
    use ratatui::text::{Line, Span};
    use ratatui::widgets::{Paragraph, Widget};
    let osc8 = "\x1b]8;;https://example.com\x1b\\Click\x1b]8;;\x1b\\";
    let para = Paragraph::new(Line::from(Span::raw(osc8)));
    let mut buf = Buffer::empty(Rect::new(0, 0, 20, 1));
    para.render(buf.area, &mut buf);
    // Cell 0 must hold the ESC byte (proves each escape byte
    // is taking a full cell, not being zero-width or merged).
    assert_eq!(buf[(0, 0)].symbol(), "\x1b");
    // The URL chars get spread across cells 5..19. "Click" never
    // makes it into the visible buffer — proof that ratatui treats
    // every escape byte as 1 cell of layout width.
    let rendered = buffer_to_string(&buf);
    assert!(
        !rendered.contains("Click"),
        "If this fails, ratatui learned about OSC 8 — revisit the BACKLOG entry. Got: {rendered:?}"
    );
    // The escape framing reaches the buffer (bytes are preserved
    // per cell) but spread across cells in a way that won't
    // assemble into a hyperlink at terminal render time.
    assert!(rendered.contains('\x1b'));
    assert!(rendered.contains("]8;;"));
}