purple-ssh 3.11.0

Open-source terminal SSH manager that keeps ~/.ssh/config in sync with your cloud infra. Spin up a VM on AWS, GCP, Azure, Hetzner or 12 other cloud providers and it appears in your host list. Destroy it and the entry dims. Search hundreds of hosts, transfer files, manage Docker and Podman over SSH, sign Vault SSH certs. Rust TUI, MIT licensed.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
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
use super::build_update_label;
use crate::app::GroupBy;

#[test]
fn label_fits_fully() {
    let label = build_update_label("2.7.0", Some("New feature"), "purple update", 80);
    assert_eq!(label, " v2.7.0: New feature (run purple update) ");
}

#[test]
fn label_no_headline() {
    let label = build_update_label("2.7.0", None, "purple update", 80);
    assert_eq!(label, " v2.7.0 available, run purple update ");
}

#[test]
fn label_truncates_at_various_widths() {
    use unicode_width::UnicodeWidthStr;

    let hl = "Provider metadata uses provider-specific terminology (instance, vm_size, zone, location, image, specs)";
    let hint = "purple update";
    let full = " v2.7.0: Provider metadata uses provider-specific terminology (instance, vm_size, zone, location, image, specs) (run purple update) ";

    // Full label is 132 display columns; budget = width - 4
    assert_eq!(full.width(), 132);

    // 136+ cols: fits fully (budget >= 132)
    assert_eq!(build_update_label("2.7.0", Some(hl), hint, 136), full);

    // 80 cols: budget 76, headline truncated with ellipsis
    let label_80 = build_update_label("2.7.0", Some(hl), hint, 80);
    assert!(
        label_80.contains('\u{2026}'),
        "Should contain ellipsis: {}",
        label_80
    );
    assert!(label_80.contains("(run purple update)"));
    assert!(
        label_80.width() <= 76,
        "Should fit in budget: width={}",
        label_80.width()
    );

    // 60 cols: budget 56, headline truncated further
    let label_60 = build_update_label("2.7.0", Some(hl), hint, 60);
    assert!(label_60.contains('\u{2026}'));
    assert!(label_60.contains("(run purple update)"));
    assert!(
        label_60.width() <= 56,
        "Should fit in budget: width={}",
        label_60.width()
    );

    // Verify progressive truncation
    assert!(label_60.width() < label_80.width());

    // 30 cols: not enough room for headline, falls back to version-only
    assert_eq!(
        build_update_label("2.7.0", Some(hl), hint, 30),
        " v2.7.0 available, run purple update "
    );
}

#[test]
fn label_falls_back_when_very_narrow() {
    let label = build_update_label("2.7.0", Some("Headline"), "purple update", 30);
    assert_eq!(label, " v2.7.0 available, run purple update ");
}

#[test]
fn label_brew_hint() {
    let label = build_update_label(
        "2.7.0",
        Some("Fix"),
        "brew upgrade erickochen/purple/purple",
        80,
    );
    assert_eq!(
        label,
        " v2.7.0: Fix (run brew upgrade erickochen/purple/purple) "
    );
}

#[test]
fn label_zero_width() {
    let label = build_update_label("2.7.0", Some("Headline"), "purple update", 0);
    assert_eq!(label, " v2.7.0 available, run purple update ");
}

// =========================================================================
// Columns tests
// =========================================================================

use super::{Columns, HOST_MIN, MARKER_WIDTH, footer_spans, pattern_footer_spans};

#[test]
fn test_padded_zero() {
    assert_eq!(Columns::padded(0), 0);
}

#[test]
fn test_padded_nonzero() {
    // padded(10) = 10 + 10/10 + 1 = 12
    assert_eq!(Columns::padded(10), 12);
}

#[test]
fn test_columns_collapse_priority_last_then_tags_then_address() {
    // Set up widths that are too wide for content area.
    // LAST should be hidden first, then TAGS, then ADDRESS.
    // left = MARKER(2) + 1 + status(2) + alias(padded 12) + gap(2) + host(padded 23) = 42
    // right = tags(padded 12) + gap(2) + history(padded 7) = 21
    // total = 42 + 2 + 21 = 65. At 60, history hides but tags still fit (42+2+12=56).
    let cols = Columns::compute(
        10, // alias_w
        20, // host_w
        0,  // host_min_w (no IPs)
        10, // tags_w
        6,  // history_w
        60, // narrow enough to hide LAST but keep TAGS
        false,
    );
    assert_eq!(
        cols.history, 0,
        "LAST should be hidden first when too narrow"
    );
    assert!(
        cols.tags > 0,
        "Tags should still be present after LAST is hidden"
    );
}

#[test]
fn test_columns_compute_flex_gap() {
    let cols = Columns::compute(
        10,  // alias_w
        15,  // host_w
        0,   // host_min_w (no IPs)
        8,   // tags_w
        5,   // history_w
        200, // wide content
        false,
    );
    assert!(
        cols.flex_gap > 0,
        "flex_gap should be positive with wide content"
    );
    // Total consumed should not exceed content width
    let gap = if 200 >= 120 { 3 } else { 2 };
    let left = MARKER_WIDTH + 1 + 2 + cols.alias + gap + cols.host; // +2 for status indicator
    let mut right = 0;
    if cols.tags > 0 {
        right += cols.tags;
    }
    if cols.history > 0 {
        right += cols.history;
    }
    // Count gaps between right-cluster columns
    let right_cols = [cols.tags > 0, cols.history > 0]
        .iter()
        .filter(|&&b| b)
        .count();
    let right_gaps = if right_cols > 1 {
        (right_cols - 1) * gap
    } else {
        0
    };
    // flex_gap fills the remaining space
    assert_eq!(
        cols.flex_gap,
        200usize.saturating_sub(left + right + right_gaps)
    );
}

#[test]
fn test_columns_compute_host_shrinks() {
    // Narrow content: host shrinks but stays >= HOST_MIN.
    // left = MARKER(2) + 1 + status(2) + alias(padded 9) + gap(2) + host(padded 34) = 50
    // No right columns, so nothing to hide. Host won't be hidden since
    // left without host (14) + gap(2) + rw(0) = 14 < 40, but total with host = 50 > 40.
    // The shrink path reduces host by (50-40)=10, from 34 to 24 (>= HOST_MIN).
    let cols = Columns::compute(
        8,  // alias_w
        30, // host_w — should shrink
        0,  // host_min_w (no IPs)
        0,  // no tags
        0,  // no history
        40, // narrow enough to shrink host, but not hide it
        false,
    );
    assert!(
        cols.host >= HOST_MIN,
        "Host should stay >= HOST_MIN ({}), got {}",
        HOST_MIN,
        cols.host
    );
    assert!(
        cols.host < 34,
        "Host should have shrunk from padded value (34), got {}",
        cols.host
    );
}

#[test]
fn test_columns_host_floor_respects_ip_min_width() {
    // Same scenario as the shrink test (host_w=30, content=40) but with an
    // IPv6 host present (host_min_w=39). The shrink pass would normally cut
    // host to 24, but host_floor = max(host_min_w, HOST_MIN) = 39, so the
    // shrink branch must hide the column entirely instead of yielding a
    // truncated IP that the user cannot copy.
    let cols = Columns::compute(
        8,  // alias_w
        30, // host_w (desired)
        39, // host_min_w — widest IP in the list (full IPv6)
        0,  // no tags
        0,  // no history
        40, // narrow content
        false,
    );
    assert_eq!(
        cols.host, 0,
        "host column must hide rather than truncate an IP below its full width"
    );
}

#[test]
fn test_columns_host_min_w_zero_falls_back_to_host_min() {
    // No IPs in the list → host_min_w=0. Column must still shrink down
    // to the legacy HOST_MIN=12 floor (not below), preserving the pre-
    // feature behaviour for DNS-only host lists.
    let cols = Columns::compute(
        8,  // alias_w
        30, // host_w
        0,  // host_min_w — no IPs
        0,  // tags
        0,  // history
        40, // narrow
        false,
    );
    assert!(
        cols.host >= HOST_MIN,
        "host column must respect HOST_MIN floor when no IPs present, got {}",
        cols.host
    );
}

#[test]
fn test_columns_host_keeps_ip_floor_at_intermediate_width() {
    // Plenty of room for the full host_w (padded ~33) but if we pin host_min_w
    // higher than the desired padded width, the column should grow to fit.
    let cols = Columns::compute(8, 15, 20, 0, 0, 200, false);
    assert!(
        cols.host >= 20,
        "host column must be >= host_min_w ({}), got {}",
        20,
        cols.host
    );
}

#[test]
fn test_footer_no_grouped_indicator() {
    // "grouped" indicator was removed (redundant with group bar)
    let spans = footer_spans(false, false, false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(
        !text.contains("grouped"),
        "Footer should NOT contain 'grouped' indicator, got: {}",
        text
    );
}

#[test]
fn footer_shows_core_actions() {
    let spans = footer_spans(false, false, false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("Enter"));
    assert!(text.contains("connect"));
    assert!(text.contains("/"));
    assert!(text.contains("search"));
    assert!(text.contains("#"));
    assert!(text.contains("tag"));
    assert!(text.contains("v"));
}

#[test]
fn footer_view_label_detail_when_compact() {
    let spans = footer_spans(false, false, false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("detail"));
}

#[test]
fn footer_view_label_compact_when_detail() {
    let spans = footer_spans(true, false, false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("compact"));
}

#[test]
fn footer_down_only_indicator() {
    let spans = footer_spans(false, true, false);
    let text: String = spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.contains("DOWN ONLY"));
}

#[test]
fn footer_selection_active_replaces_view_hints_with_bulk_actions() {
    let spans = footer_spans(false, false, true);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("bulk tag"), "missing bulk tag hint: {text}");
    assert!(text.contains(" run "), "missing run hint: {text}");
    assert!(text.contains("clear"), "missing clear hint: {text}");
    assert!(text.contains("help"), "missing help hint: {text}");
    // Confirm we drop the noisy `:` and `v` keys to keep the line short.
    assert!(
        !text.contains(" cmds "),
        "selection footer should drop cmds"
    );
    assert!(
        !text.contains(" detail "),
        "selection footer should drop view label"
    );
}

#[test]
fn pattern_footer_shows_core_actions() {
    let spans = pattern_footer_spans(false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("/"));
    assert!(text.contains("search"));
    assert!(text.contains("#"));
    assert!(text.contains("v"));
}

#[test]
fn pattern_footer_detail_label_when_compact() {
    let spans = pattern_footer_spans(false);
    let text: String = spans.iter().map(|s| s.content.to_string()).collect();
    assert!(text.contains("detail"));
}

#[test]
fn layout_has_top_bar_and_footer() {
    use ratatui::layout::{Constraint, Layout, Rect};
    let area = Rect::new(0, 0, 120, 40);
    // Matches render() layout: top bar always visible, no search/tag bar.
    let chunks = Layout::vertical([
        Constraint::Length(3), // Top navigation bar
        Constraint::Min(5),    // Host list
        Constraint::Length(1), // Footer
    ])
    .split(area);
    assert_eq!(chunks[0].height, 3, "top bar should be 3 rows");
    assert_eq!(chunks[2].height, 1, "footer should be 1 row");
    assert!(chunks[2].y > chunks[1].y + chunks[1].height - 1);
}

#[test]
fn layout_with_search_has_top_bar() {
    use ratatui::layout::{Constraint, Layout, Rect};
    let area = Rect::new(0, 0, 120, 40);
    // Matches render() layout when searching: top bar still visible.
    let chunks = Layout::vertical([
        Constraint::Length(3), // Top navigation bar
        Constraint::Min(5),    // Host list
        Constraint::Length(1), // Search bar
        Constraint::Length(1), // Footer
    ])
    .split(area);
    assert_eq!(chunks[0].height, 3, "top bar should be 3 rows");
    assert_eq!(chunks[2].height, 1, "search bar should be 1 row");
    assert_eq!(chunks[3].height, 1, "footer should be 1 row");
}

// =========================================================================
// Column hide priority tests
// =========================================================================

#[test]
fn columns_hide_full_priority_chain() {
    // Wide enough for everything
    let cols_wide = Columns::compute(10, 15, 0, 8, 5, 200, false);
    assert!(cols_wide.history > 0, "history visible at 200");
    assert!(cols_wide.tags > 0, "tags visible at 200");
    assert!(cols_wide.host > 0, "host visible at 200");

    // Progressively narrower: LAST (history) hides first
    let cols_no_history = Columns::compute(10, 15, 0, 8, 5, 50, false);
    assert_eq!(cols_no_history.history, 0, "history should hide first");

    // Narrower still: TAGS hides next
    let cols_no_tags = Columns::compute(10, 15, 0, 8, 5, 40, false);
    assert_eq!(cols_no_tags.history, 0, "history still hidden");
    assert_eq!(cols_no_tags.tags, 0, "tags should hide second");

    // Extremely narrow: ADDRESS hides last
    let cols_no_host = Columns::compute(10, 15, 0, 8, 5, 20, false);
    assert_eq!(cols_no_host.history, 0);
    assert_eq!(cols_no_host.tags, 0);
    assert_eq!(cols_no_host.host, 0, "host should hide last");
}

#[test]
fn columns_detail_mode_no_host() {
    let cols = Columns::compute(10, 15, 0, 8, 5, 200, true);
    assert_eq!(cols.host, 0, "host should be 0 in detail_mode");
    assert!(cols.detail_mode, "detail_mode flag should be set");
    assert!(cols.tags > 0, "tags visible in detail_mode");
    assert!(cols.history > 0, "history visible in detail_mode");
}

#[test]
fn format_rtt_millis() {
    assert_eq!(super::format_rtt(42), "42ms");
}

#[test]
fn format_rtt_zero() {
    assert_eq!(super::format_rtt(0), "0ms");
}

#[test]
fn format_rtt_boundary_999() {
    assert_eq!(super::format_rtt(999), "999ms");
}

#[test]
fn format_rtt_boundary_1000() {
    assert_eq!(super::format_rtt(1000), "1.0s");
}

#[test]
fn format_rtt_seconds() {
    assert_eq!(super::format_rtt(1500), "1.5s");
}

#[test]
fn format_rtt_capped() {
    assert_eq!(super::format_rtt(12000), "10s+");
}

#[test]
fn format_rtt_boundary_9949() {
    assert_eq!(super::format_rtt(9949), "9.9s");
}

#[test]
fn format_rtt_boundary_9950() {
    assert_eq!(super::format_rtt(9950), "10s+");
}

#[test]
fn format_rtt_boundary_10000() {
    assert_eq!(super::format_rtt(10000), "10s+");
}

#[test]
fn format_rtt_u32_max() {
    assert_eq!(super::format_rtt(u32::MAX), "10s+");
}

// =========================================================================
// composite_host_label tests
// =========================================================================

#[test]
fn composite_host_label_hostname_only() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "example.com".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_label(&host), "example.com");
}

#[test]
fn composite_host_label_non_default_port() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "example.com".to_string(),
        port: 2222,
        ..Default::default()
    };
    assert_eq!(super::composite_host_label(&host), "example.com:2222");
}

#[test]
fn composite_host_label_no_user_prefix() {
    // User field is set but composite_host_label should NOT include user@
    let host = crate::ssh_config::model::HostEntry {
        hostname: "example.com".to_string(),
        user: "admin".to_string(),
        port: 22,
        ..Default::default()
    };
    let label = super::composite_host_label(&host);
    assert!(
        !label.contains('@'),
        "composite label should not include user@"
    );
    assert_eq!(label, "example.com");
}

// composite_host_width tests (allocation-free path)

#[test]
fn composite_host_width_default_port() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "example.com".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_width(&host), "example.com".len());
}

#[test]
fn composite_host_width_non_default_port() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "example.com".to_string(),
        port: 2222,
        ..Default::default()
    };
    // "example.com" (11) + ":" (1) + "2222" (4) = 16
    assert_eq!(super::composite_host_width(&host), 16);
}

#[test]
fn composite_host_width_port_zero() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "host".to_string(),
        port: 0,
        ..Default::default()
    };
    // "host" (4) + ":" (1) + "0" (1) = 6
    assert_eq!(super::composite_host_width(&host), 6);
}

#[test]
fn composite_host_width_port_max() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "h".to_string(),
        port: 65535,
        ..Default::default()
    };
    // "h" (1) + ":" (1) + "65535" (5) = 7
    assert_eq!(super::composite_host_width(&host), 7);
}

// composite_host_width_if_ip tests

#[test]
fn composite_host_width_if_ip_bare_ipv4() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "192.168.0.100".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_width_if_ip(&host), 13);
}

#[test]
fn composite_host_width_if_ip_bare_ipv6() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "2001:db8::1".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_width_if_ip(&host), 11);
}

#[test]
fn composite_host_width_if_ip_bracketed_ipv6() {
    // OpenSSH requires brackets around an IPv6 literal in HostName when
    // a non-default port is present. IpAddr::parse rejects brackets, so
    // the helper must strip them before parsing — otherwise the host
    // column will truncate a full IPv6 literal that the user cannot copy.
    let host = crate::ssh_config::model::HostEntry {
        hostname: "[2001:db8::1]".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(
        super::composite_host_width_if_ip(&host),
        "[2001:db8::1]".len(),
        "bracketed IPv6 must be recognised as an IP"
    );
}

#[test]
fn composite_host_width_if_ip_bracketed_ipv6_loopback() {
    let host = crate::ssh_config::model::HostEntry {
        hostname: "[::1]".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(
        super::composite_host_width_if_ip(&host),
        5,
        "[::1] must be recognised as an IP"
    );
}

#[test]
fn composite_host_width_if_ip_dns_returns_zero() {
    // DNS hostnames must remain shrinkable — helper returns 0 so they do
    // not inflate the column floor.
    let host = crate::ssh_config::model::HostEntry {
        hostname: "web-01.prod.example.com".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_width_if_ip(&host), 0);
}

#[test]
fn composite_host_width_if_ip_garbage_brackets_return_zero() {
    // Only well-formed bracketed IPv6 counts. Brackets around garbage
    // must not accidentally pass for an IP.
    let host = crate::ssh_config::model::HostEntry {
        hostname: "[not-an-ip]".to_string(),
        port: 22,
        ..Default::default()
    };
    assert_eq!(super::composite_host_width_if_ip(&host), 0);
}

// =========================================================================
// Columns detail_mode collapse priority tests
// =========================================================================

#[test]
fn columns_detail_mode_collapse_priority() {
    // detail_mode=true, progressively narrower
    // LAST hides first, then TAGS (ADDRESS already 0)
    let cols_wide = Columns::compute(10, 15, 0, 8, 5, 100, true);
    assert_eq!(cols_wide.host, 0, "detail_mode: no host");
    assert!(cols_wide.tags > 0, "tags visible at 100");
    assert!(cols_wide.history > 0, "history visible at 100");

    // Narrow: LAST hides first
    let cols_narrow = Columns::compute(10, 15, 0, 8, 5, 25, true);
    assert_eq!(cols_narrow.host, 0);
    assert_eq!(
        cols_narrow.history, 0,
        "history should hide first in detail_mode"
    );

    // Very narrow: TAGS hides next
    let cols_very_narrow = Columns::compute(10, 15, 0, 8, 5, 18, true);
    assert_eq!(cols_very_narrow.host, 0);
    assert_eq!(cols_very_narrow.history, 0);
    assert_eq!(cols_very_narrow.tags, 0, "tags should hide after history");
}

// --- tag_bar_spans tests ---

#[test]
fn tag_bar_empty_input_shows_cursor_then_placeholder() {
    let spans = super::tag_bar_spans("", &[]);
    let texts: Vec<&str> = spans.iter().map(|s| s.content.as_ref()).collect();
    // " tags: " then cursor "_" then placeholder hint
    assert_eq!(texts[0], " tags: ");
    assert_eq!(texts[1], "_");
    assert!(texts[2].contains("e.g."));
}

#[test]
fn tag_bar_with_input_shows_input_then_cursor() {
    let spans = super::tag_bar_spans("prod, staging", &[]);
    let texts: Vec<&str> = spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(texts[0], " tags: ");
    assert_eq!(texts[1], "prod, staging");
    assert_eq!(texts[2], "_");
    assert_eq!(texts.len(), 3);
}

#[test]
fn tag_bar_with_provider_tags_shows_prefix() {
    let ptags = vec!["cloud".to_string(), "eu".to_string()];
    let spans = super::tag_bar_spans("web", &ptags);
    let texts: Vec<&str> = spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(texts[0], " tags: ");
    assert_eq!(texts[1], "[cloud, eu] ");
    assert_eq!(texts[2], "web");
    assert_eq!(texts[3], "_");
}

// =========================================================================
// build_host_item detail-mode indicator tests
// =========================================================================

use super::{HostItemContext, build_host_item};
use crate::ssh_config::model::HostEntry;

fn detail_columns(alias: usize) -> Columns {
    Columns {
        alias,
        host: 0,
        tags: 0,
        history: 0,
        gap: 2,
        flex_gap: 0,
        detail_mode: true,
    }
}

fn full_columns(alias: usize, host: usize) -> Columns {
    Columns {
        alias,
        host,
        tags: 0,
        history: 0,
        gap: 2,
        flex_gap: 0,
        detail_mode: false,
    }
}

/// Render a ListItem to a plain string via a ratatui List + Buffer.
fn render_item_to_string(item: ratatui::widgets::ListItem<'_>, width: u16) -> String {
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;
    use ratatui::widgets::{List, Widget};
    let area = Rect::new(0, 0, width, 1);
    let mut buf = Buffer::empty(area);
    let list = List::new(vec![item]);
    list.render(area, &mut buf);
    let mut s = String::new();
    for x in 0..width {
        let cell = &buf[(x, 0)];
        s.push_str(cell.symbol());
    }
    s
}

fn make_ctx<'a>(cols: &'a Columns, tunnel_active: bool, detail_mode: bool) -> HostItemContext<'a> {
    let ping = Box::leak(Box::new(std::collections::HashMap::new()));
    let history = Box::leak(Box::new(crate::history::ConnectionHistory::default()));
    HostItemContext {
        ping_status: ping,
        history,
        tunnel_active,
        query: None,
        cols,
        multi_selected: false,
        group_by: &GroupBy::None,
        detail_mode,
        spinner_tick: 0,
    }
}

#[test]
fn detail_mode_no_indicators() {
    let cols = detail_columns(20);
    let host = HostEntry {
        alias: "web-server".into(),
        ..Default::default()
    };
    let ctx = make_ctx(&cols, false, true);
    let item = build_host_item(&host, &ctx);
    let rendered = render_item_to_string(item, 30);
    assert!(rendered.contains("web-server"));
    assert!(!rendered.contains('\u{2197}'), "no jump indicator expected");
    assert!(
        !rendered.contains('\u{21C4}'),
        "no tunnel indicator expected"
    );
}

#[test]
fn detail_mode_jump_indicator_visible() {
    let cols = detail_columns(20);
    let host = HostEntry {
        alias: "bastion".into(),
        proxy_jump: "gateway".into(),
        ..Default::default()
    };
    let ctx = make_ctx(&cols, false, true);
    let item = build_host_item(&host, &ctx);
    let rendered = render_item_to_string(item, 30);
    assert!(rendered.contains("bastion"));
    assert!(rendered.contains('\u{2197}'), "jump indicator missing");
    assert!(
        !rendered.contains('\u{21C4}'),
        "tunnel indicator should not appear"
    );
}

#[test]
fn detail_mode_tunnel_indicator_visible() {
    let cols = detail_columns(20);
    let host = HostEntry {
        alias: "db-primary".into(),
        tunnel_count: 1,
        ..Default::default()
    };
    let ctx = make_ctx(&cols, false, true);
    let item = build_host_item(&host, &ctx);
    let rendered = render_item_to_string(item, 30);
    assert!(rendered.contains("db-primary"));
    assert!(rendered.contains('\u{21C4}'), "tunnel indicator missing");
    assert!(
        !rendered.contains('\u{2197}'),
        "jump indicator should not appear"
    );
}

#[test]
fn detail_mode_both_indicators_truncate_alias() {
    let cols = detail_columns(20);
    let host = HostEntry {
        alias: "very-long-hostname-here".into(),
        proxy_jump: "gateway".into(),
        tunnel_count: 2,
        ..Default::default()
    };
    let ctx = make_ctx(&cols, false, true);
    let item = build_host_item(&host, &ctx);
    let rendered = render_item_to_string(item, 30);
    assert!(rendered.contains('\u{2197}'), "jump indicator missing");
    assert!(rendered.contains('\u{21C4}'), "tunnel indicator missing");
    // Full alias (22 chars) should be truncated to fit indicators (4 cols) in 20-col budget
    assert!(
        !rendered.contains("very-long-hostname-here"),
        "alias should be truncated"
    );
}

#[test]
fn non_detail_mode_indicators_in_address_column() {
    let cols = full_columns(20, 30);
    let host = HostEntry {
        alias: "bastion".into(),
        hostname: "10.0.0.1".into(),
        proxy_jump: "gateway".into(),
        tunnel_count: 1,
        ..Default::default()
    };
    let ctx = make_ctx(&cols, true, false);
    let item = build_host_item(&host, &ctx);
    let rendered = render_item_to_string(item, 60);
    assert!(rendered.contains('\u{2197}'), "jump indicator missing");
    assert!(rendered.contains('\u{21C4}'), "tunnel indicator missing");
    assert!(
        rendered.contains("10.0.0.1"),
        "hostname should appear in address column"
    );
}

// =========================================================================
// Top navigation bar tests
// =========================================================================

mod top_bar {
    use super::super::top_bar_spans;
    use crate::app::{App, TopPage};
    use crate::ssh_config::model::SshConfigFile;
    use crate::tunnel::{TunnelRule, TunnelType};
    use crate::ui::theme;

    fn make_test_app(content: &str) -> App {
        let scratch = tempfile::tempdir().expect("tempdir").keep();
        let config = SshConfigFile {
            elements: SshConfigFile::parse_content(content),
            path: scratch.join("test_config"),
            crlf: false,
            bom: false,
        };
        crate::preferences::set_path_override(scratch.join("preferences"));
        App::new(config)
    }

    fn add_tunnel_rule(app: &mut App, bind_port: u16) {
        app.tunnels.list.push(TunnelRule {
            tunnel_type: TunnelType::Local,
            bind_address: String::new(),
            bind_port,
            remote_host: "127.0.0.1".to_string(),
            remote_port: bind_port,
        });
    }

    #[test]
    fn brand_slot_is_first_and_uses_brand_badge() {
        let app = make_test_app("Host web1\n  HostName 1.1.1.1\n");
        let spans = top_bar_spans(&app);
        assert_eq!(
            spans[0].content.as_ref(),
            " purple ",
            "first slot must be the brand label"
        );
        assert_eq!(
            spans[0].style,
            theme::brand_badge(),
            "brand slot must use brand_badge style"
        );
    }

    #[test]
    fn labels_have_no_count_or_padding() {
        let mut app =
            make_test_app("Host web1\n  HostName 1.1.1.1\nHost web2\n  HostName 2.2.2.2\n");
        add_tunnel_rule(&mut app, 8080);
        add_tunnel_rule(&mut app, 8081);

        let spans = top_bar_spans(&app);
        let host_span = spans
            .iter()
            .find(|s| s.content.contains("hosts"))
            .expect("hosts span");
        let tunnels_span = spans
            .iter()
            .find(|s| s.content.contains("tunnels"))
            .expect("tunnels span");

        // Page labels are bare — no surrounding spaces, no parenthesised count.
        assert_eq!(host_span.content.as_ref(), "hosts");
        assert_eq!(tunnels_span.content.as_ref(), "tunnels");
    }

    #[test]
    fn brand_and_first_tab_are_separated_by_a_visible_gap() {
        let app = make_test_app("Host web1\n  HostName 1.1.1.1\n");
        let spans = top_bar_spans(&app);

        let brand_pos = spans
            .iter()
            .position(|s| s.content.contains("purple"))
            .expect("brand span");
        let host_pos = spans
            .iter()
            .position(|s| s.content.as_ref() == "hosts")
            .expect("hosts span");

        assert!(host_pos > brand_pos, "hosts must come after brand");
        let between: usize = spans[brand_pos + 1..host_pos]
            .iter()
            .map(|s| s.content.len())
            .sum();
        assert!(
            between >= 4,
            "need a visible gap between brand badge and first tab; got {between}"
        );
    }

    #[test]
    fn active_page_uses_nav_active_inactive_is_muted() {
        let app = make_test_app("Host web1\n  HostName 1.1.1.1\n");
        assert_eq!(app.top_page, TopPage::Hosts);
        let spans = top_bar_spans(&app);

        let host_span = spans
            .iter()
            .find(|s| s.content.contains("hosts"))
            .expect("hosts span");
        let tunnels_span = spans
            .iter()
            .find(|s| s.content.contains("tunnels"))
            .expect("tunnels span");

        assert_eq!(host_span.style, theme::nav_active());
        assert_eq!(tunnels_span.style, theme::muted());
    }

    #[test]
    fn switching_to_tunnels_swaps_active_style() {
        let mut app = make_test_app("Host web1\n  HostName 1.1.1.1\n");
        app.top_page = TopPage::Tunnels;
        let spans = top_bar_spans(&app);

        let host_span = spans
            .iter()
            .find(|s| s.content.contains("hosts"))
            .expect("hosts span");
        let tunnels_span = spans
            .iter()
            .find(|s| s.content.contains("tunnels"))
            .expect("tunnels span");

        assert_eq!(host_span.style, theme::muted());
        assert_eq!(tunnels_span.style, theme::nav_active());
    }

    #[test]
    fn nav_active_is_underlined_not_a_solid_badge() {
        use ratatui::style::Modifier;
        let _guard = THEME_CAPS_LOCK.lock().unwrap_or_else(|p| p.into_inner());
        theme::set_colored_underline_support(true);
        let style = theme::nav_active();
        assert!(
            style.add_modifier.contains(Modifier::UNDERLINED),
            "active tab must underline"
        );
        assert_ne!(
            style,
            theme::brand_badge(),
            "active tab must not use the brand badge style"
        );
    }

    /// Module-wide lock for tests that flip `theme::COLORED_UNDERLINE` so they
    /// do not race with each other under parallel `cargo test` execution.
    static THEME_CAPS_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    #[test]
    fn nav_active_uses_colored_underline_when_supported() {
        let _guard = THEME_CAPS_LOCK.lock().unwrap_or_else(|p| p.into_inner());
        // Pin to ANSI 16 so the accent.ansi16 branch is exercised (default in
        // most CI / dev shells) and the assertion is deterministic.
        theme::init_with_mode(1);
        theme::set_colored_underline_support(true);
        let style = theme::nav_active();
        assert!(
            style.underline_color.is_some(),
            "modern terminals: nav_active must set a styled underline color"
        );
        assert!(
            style.fg.is_none(),
            "modern terminals: foreground stays default so the label reads white"
        );
        // Restore the supported default for any unrelated tests that follow.
        theme::set_colored_underline_support(true);
    }

    #[test]
    fn nav_active_falls_back_to_plain_underline_when_unsupported() {
        let _guard = THEME_CAPS_LOCK.lock().unwrap_or_else(|p| p.into_inner());
        theme::init_with_mode(1);
        theme::set_colored_underline_support(false);
        let style = theme::nav_active();
        assert!(
            style.underline_color.is_none(),
            "legacy terminals: must not emit SGR 58 (no styled underline color)"
        );
        assert!(
            style.fg.is_none(),
            "legacy terminals: leave fg default so the label stays white instead of brand-coloured"
        );
        use ratatui::style::Modifier;
        assert!(
            style.add_modifier.contains(Modifier::UNDERLINED),
            "legacy terminals: keep SGR 4 so the active tab still has an underline"
        );
        // Restore for unrelated tests.
        theme::set_colored_underline_support(true);
    }

    #[test]
    fn nav_active_no_color_mode_skips_accent_in_both_paths() {
        let _guard = THEME_CAPS_LOCK.lock().unwrap_or_else(|p| p.into_inner());
        theme::init_with_mode(0);
        for supported in [true, false] {
            theme::set_colored_underline_support(supported);
            let style = theme::nav_active();
            assert!(
                style.underline_color.is_none(),
                "NO_COLOR: never emit a styled underline color (cap={supported})"
            );
            assert!(
                style.fg.is_none(),
                "NO_COLOR: never set a fg accent (cap={supported})"
            );
        }
        // Restore mode + cap for unrelated tests.
        theme::init_with_mode(1);
        theme::set_colored_underline_support(true);
    }

    #[test]
    fn no_provider_or_tag_labels_appear_in_top_bar() {
        let content = "\
Host aws-web
  HostName 1.1.1.1
  # purple:provider aws:i-1
  # purple:tags production

Host do-db
  HostName 2.2.2.2
  # purple:provider digitalocean:abc
  # purple:tags staging
";
        let app = make_test_app(content);
        let text: String = top_bar_spans(&app)
            .into_iter()
            .map(|s| s.content.into_owned())
            .collect();

        for legacy in [
            "AWS",
            "PROXMOX",
            "DIGITALOCEAN",
            "production",
            "staging",
            "All (",
        ] {
            assert!(
                !text.contains(legacy),
                "top bar must not contain legacy category {legacy:?}, got: {text}"
            );
        }
    }
}