coding-agent-search 0.5.1

Unified TUI search over local coding agent histories
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
//! Interactive terminal prompts for the remote sources setup wizard.
//!
//! This module provides rich interactive components using dialoguer, including:
//! - Multi-select host picker with multi-line item display
//! - Confirmation prompts for destructive operations
//!
//! # Design Decision: dialoguer vs inquire
//!
//! We chose dialoguer because:
//! 1. It integrates well with indicatif (already used for progress bars)
//! 2. It's actively maintained and widely used
//! 3. It supports ANSI styling in items via the console crate
//!
//! # Multi-line Item Display
//!
//! Standard dialoguer MultiSelect shows single-line items. We achieve multi-line
//! display by embedding ANSI escape sequences and newlines directly in item strings:
//!
//! ```text
//! [x] css
//!     209.145.54.164 • ubuntu
//!     ✓ cass v0.1.50 installed • 1,234 sessions
//!     Claude ✓  Codex ✓  Cursor ✓
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use coding_agent_search::sources::interactive::{
//!     HostSelector, HostDisplayInfo, HostState, CassStatusDisplay
//! };
//!
//! let hosts = vec![
//!     HostDisplayInfo {
//!         name: "css".into(),
//!         hostname: "209.145.54.164".into(),
//!         username: "ubuntu".into(),
//!         cass_status: CassStatusDisplay::Installed { version: "0.1.50".into(), sessions: 1234 },
//!         detected_agents: vec!["claude".into(), "codex".into()],
//!         reachable: true,
//!         error: None,
//!         state: HostState::ReadyToSync,
//!         system_info: Some("ubuntu 22.04 • 45GB free".into()),
//!     },
//!     // ... more hosts
//! ];
//!
//! let selector = HostSelector::new(hosts);
//! let selected = selector.prompt()?;
//! ```

use std::collections::HashSet;
use std::fmt;
use std::io::IsTerminal;

use colored::Colorize;
use dialoguer::{Confirm, MultiSelect, theme::ColorfulTheme};

use super::probe::{CassStatus, HostProbeResult};

// =============================================================================
// Types
// =============================================================================

/// State of a host for selection purposes.
///
/// Determines how the host appears in the UI and whether it's selectable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HostState {
    /// cass installed and indexed - ready to sync immediately.
    ReadyToSync,
    /// cass installed but needs indexing.
    NeedsIndexing,
    /// cass not found - needs installation.
    NeedsInstall,
    /// SSH connection failed.
    Unreachable,
    /// Already configured in sources.toml.
    AlreadyConfigured,
}

impl HostState {
    /// Get the status badge for display (right-aligned).
    pub fn status_badge(&self) -> String {
        match self {
            HostState::ReadyToSync => format!("{} Ready to sync", "".green()),
            HostState::NeedsIndexing => format!("{} Needs indexing", "".yellow()),
            HostState::NeedsInstall => format!("{} Needs install", "".yellow()),
            HostState::Unreachable => format!("{} Unreachable", "".red()),
            HostState::AlreadyConfigured => format!("{} Already setup", "".cyan()),
        }
    }

    /// Check if this host state is selectable.
    pub fn is_selectable(&self) -> bool {
        matches!(
            self,
            HostState::ReadyToSync | HostState::NeedsIndexing | HostState::NeedsInstall
        )
    }

    /// Check if this host should be pre-selected.
    pub fn should_preselect(&self) -> bool {
        // Pre-select ready and needs-indexing hosts; don't pre-select needs-install
        matches!(self, HostState::ReadyToSync | HostState::NeedsIndexing)
    }
}

/// Display information for a remote host in the selection UI.
#[derive(Debug, Clone)]
pub struct HostDisplayInfo {
    /// SSH config name (e.g., "css", "laptop")
    pub name: String,
    /// IP address or hostname
    pub hostname: String,
    /// SSH username
    pub username: String,
    /// cass installation status on this host
    pub cass_status: CassStatusDisplay,
    /// Detected coding agents on this host
    pub detected_agents: Vec<String>,
    /// Whether this host is reachable
    pub reachable: bool,
    /// Optional error message if unreachable
    pub error: Option<String>,
    /// Host state for selection purposes
    pub state: HostState,
    /// OS and free disk space info
    pub system_info: Option<String>,
}

/// cass installation status for display purposes.
#[derive(Debug, Clone)]
pub enum CassStatusDisplay {
    /// cass is installed with known version and session count
    Installed { version: String, sessions: u64 },
    /// cass is installed but not indexed
    InstalledNotIndexed { version: String },
    /// cass is not installed but agent data was detected
    NotInstalled,
    /// Could not determine status (e.g., probe failed)
    Unknown,
}

/// Result of host selection.
#[derive(Debug, Clone)]
pub struct HostSelectionResult {
    /// Indices of selected hosts in the original hosts list.
    pub selected_indices: Vec<usize>,
    /// Hosts that need cass installation.
    pub needs_install: Vec<usize>,
    /// Hosts that have cass but need indexing.
    pub needs_indexing: Vec<usize>,
    /// Hosts ready for sync (cass installed and indexed).
    pub ready_for_sync: Vec<usize>,
}

// =============================================================================
// Host Selector
// =============================================================================

/// Interactive multi-select host picker with rich display.
pub struct HostSelector {
    hosts: Vec<HostDisplayInfo>,
    theme: ColorfulTheme,
}

impl HostSelector {
    /// Create a new host selector with the given hosts.
    pub fn new(hosts: Vec<HostDisplayInfo>) -> Self {
        Self {
            hosts,
            theme: ColorfulTheme::default(),
        }
    }

    /// Format a single host for multi-line display.
    ///
    /// Returns a string with ANSI formatting suitable for terminal display.
    /// Format matches the mockup in the bead spec:
    /// ```text
    /// [x] css                                                    ✓ Ready to sync
    ///     209.145.54.164 • ubuntu 22.04 • 45GB free
    ///     ✓ cass v0.1.50 • 1,234 sessions indexed
    ///     Claude ✓  Codex ✓  Cursor ✓  Gemini ✓
    /// ```
    fn format_host(&self, host: &HostDisplayInfo) -> String {
        let mut lines = Vec::new();

        // Line 1: Host name with right-aligned status badge
        // Note: ANSI codes make length calculation tricky, so we use a fixed width
        let status_badge = host.state.status_badge();
        let name_line = format!("{}  {}", host.name.bold(), status_badge);
        lines.push(name_line);

        // Line 2: Hostname, OS, disk space (dimmed)
        let system_info = host.system_info.as_deref().unwrap_or("");
        let host_info = if system_info.is_empty() {
            format!(
                "    {}{}",
                host.hostname.dimmed(),
                host.username.dimmed()
            )
        } else {
            format!(
                "    {}{}{}",
                host.hostname.dimmed(),
                host.username.dimmed(),
                system_info.dimmed()
            )
        };
        lines.push(host_info);

        // Line 3: cass status
        let status_line = match &host.cass_status {
            CassStatusDisplay::Installed { version, sessions } => {
                format!(
                    "    {} cass v{}{} sessions indexed",
                    "".green(),
                    version,
                    sessions
                )
            }
            CassStatusDisplay::InstalledNotIndexed { version } => {
                format!(
                    "    {} cass v{}{} (will index)",
                    "".yellow(),
                    version,
                    "not indexed".yellow()
                )
            }
            CassStatusDisplay::NotInstalled => {
                format!(
                    "    {} cass not installed (will install via cargo)",
                    "".yellow()
                )
            }
            CassStatusDisplay::Unknown => {
                format!("    {} status unknown", "?".dimmed())
            }
        };
        lines.push(status_line);

        // Line 4: Detected agents (if any)
        if !host.detected_agents.is_empty() {
            let agents: Vec<String> = host
                .detected_agents
                .iter()
                .map(|a| {
                    // Capitalize first letter for display
                    let display_name = if a.is_empty() {
                        a.clone()
                    } else {
                        let mut chars = a.chars();
                        match chars.next() {
                            Some(first) => first.to_uppercase().chain(chars).collect(),
                            None => a.clone(),
                        }
                    };
                    format!("{} {}", display_name.cyan(), "".green())
                })
                .collect();
            let agents_line = format!("    {}", agents.join("  "));
            lines.push(agents_line);
        }

        // Line 5: Error if unreachable
        if !host.reachable {
            let error_msg = host.error.as_deref().unwrap_or("unreachable");
            let error_line = format!("    {} {}", "".red(), error_msg.red());
            lines.push(error_line);
        }

        // Line 6: Already configured message
        if host.state == HostState::AlreadyConfigured {
            lines.push(format!(
                "    {}",
                "Use 'cass sources edit' to modify".dimmed()
            ));
        }

        lines.join("\n")
    }

    /// Show the interactive multi-select prompt.
    ///
    /// Returns the selection result or an error if the prompt was cancelled.
    pub fn prompt(&self) -> Result<HostSelectionResult, InteractiveError> {
        if self.hosts.is_empty() {
            return Err(InteractiveError::NoHosts);
        }

        // Only show selectable hosts (filter out unreachable and already-configured)
        let selectable_hosts: Vec<(usize, &HostDisplayInfo)> = self
            .hosts
            .iter()
            .enumerate()
            .filter(|(_, h)| h.state.is_selectable())
            .collect();

        if selectable_hosts.is_empty() {
            return Err(InteractiveError::NoSelectableHosts);
        }

        // Format selectable hosts for display
        let items: Vec<String> = selectable_hosts
            .iter()
            .map(|(_, h)| self.format_host(h))
            .collect();

        // Pre-select based on HostState
        let defaults: Vec<bool> = selectable_hosts
            .iter()
            .map(|(_, h)| h.state.should_preselect())
            .collect();

        // Show the prompt
        println!();
        println!(
            "{}",
            "Select hosts to configure as sources:".bold().underline()
        );
        println!(
            "{}",
            "[space] toggle  [a] all  [enter] confirm  [q] quit".dimmed()
        );
        println!();

        let selected_in_filtered = MultiSelect::with_theme(&self.theme)
            .items(&items)
            .defaults(&defaults)
            .interact_opt()
            .map_err(|e| InteractiveError::IoError(e.to_string()))?
            .ok_or(InteractiveError::Cancelled)?;

        // Map filtered indices back to original indices
        let selected: Vec<usize> = selected_in_filtered
            .iter()
            .filter_map(|&i| selectable_hosts.get(i).map(|(orig_idx, _)| *orig_idx))
            .collect();

        // Categorize selections by state
        let mut needs_install = Vec::new();
        let mut needs_indexing = Vec::new();
        let mut ready_for_sync = Vec::new();

        for &idx in &selected {
            if let Some(host) = self.hosts.get(idx) {
                match host.state {
                    HostState::ReadyToSync => ready_for_sync.push(idx),
                    HostState::NeedsIndexing => needs_indexing.push(idx),
                    HostState::NeedsInstall => needs_install.push(idx),
                    _ => {} // Unreachable and AlreadyConfigured are not selectable
                }
            }
        }

        Ok(HostSelectionResult {
            selected_indices: selected,
            needs_install,
            needs_indexing,
            ready_for_sync,
        })
    }

    /// Get host info by index.
    pub fn get_host(&self, index: usize) -> Option<&HostDisplayInfo> {
        self.hosts.get(index)
    }
}

// =============================================================================
// Confirmation Prompts
// =============================================================================

/// Ask for confirmation before a destructive operation.
pub fn confirm_action(message: &str, default: bool) -> Result<bool, InteractiveError> {
    Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt(message)
        .default(default)
        .interact()
        .map_err(|e| InteractiveError::IoError(e.to_string()))
}

/// Ask for confirmation with a detailed explanation.
pub fn confirm_with_details(
    action: &str,
    details: &[&str],
    default: bool,
) -> Result<bool, InteractiveError> {
    println!();
    println!("{}", action.bold());
    for detail in details {
        println!("{}", detail);
    }
    println!();

    confirm_action("Proceed?", default)
}

// =============================================================================
// Probe Result Conversion
// =============================================================================

/// Convert a probe result to display info for the selection UI.
///
/// # Arguments
/// * `probe` - The probe result from SSH probing
/// * `already_configured` - Set of normalized source-name keys already configured
pub fn probe_to_display_info(
    probe: &HostProbeResult,
    already_configured: &HashSet<String>,
) -> HostDisplayInfo {
    let generated_name = super::config::normalize_generated_remote_source_name(&probe.host_name);

    // Determine host state
    let state = if already_configured.contains(&super::config::source_name_key(&generated_name)) {
        HostState::AlreadyConfigured
    } else if !probe.reachable {
        HostState::Unreachable
    } else {
        match &probe.cass_status {
            CassStatus::Indexed { session_count, .. } if *session_count > 0 => {
                HostState::ReadyToSync
            }
            CassStatus::Indexed { .. } => HostState::NeedsIndexing, // 0 sessions
            CassStatus::InstalledNotIndexed { .. } => HostState::NeedsIndexing,
            CassStatus::NotFound | CassStatus::Unknown => HostState::NeedsInstall,
        }
    };

    // Convert cass status
    let cass_status = match &probe.cass_status {
        CassStatus::Indexed {
            version,
            session_count,
            ..
        } => CassStatusDisplay::Installed {
            version: version.clone(),
            sessions: *session_count,
        },
        CassStatus::InstalledNotIndexed { version } => CassStatusDisplay::InstalledNotIndexed {
            version: version.clone(),
        },
        CassStatus::NotFound => CassStatusDisplay::NotInstalled,
        CassStatus::Unknown => CassStatusDisplay::Unknown,
    };

    // Format system info string
    let system_info = probe.system_info.as_ref().map(|si| {
        // Use distro if present and non-empty, otherwise fall back to OS
        let os_info = si
            .distro
            .as_deref()
            .filter(|d| !d.is_empty())
            .unwrap_or(&si.os);
        if let Some(res) = &probe.resources {
            let disk_gb = res.disk_available_mb / 1024;
            format!("{}{}GB free", os_info, disk_gb)
        } else {
            os_info.to_string()
        }
    });

    // Extract detected agent names
    let detected_agents: Vec<String> = probe
        .detected_agents
        .iter()
        .map(|a| a.agent_type.clone())
        .collect();

    // Use probe host name as the display hostname
    let hostname = probe.host_name.clone();

    let username = probe
        .system_info
        .as_ref()
        .and_then(|si| {
            // Extract username from remote_home path like "/home/ubuntu"
            // Filter out empty results from paths like "/" or ""
            si.remote_home
                .rsplit('/')
                .find(|s| !s.is_empty())
                .map(String::from)
        })
        .unwrap_or_else(|| "user".to_string());

    HostDisplayInfo {
        name: probe.host_name.clone(),
        hostname,
        username,
        cass_status,
        detected_agents,
        reachable: probe.reachable,
        error: probe.error.clone(),
        state,
        system_info,
    }
}

/// Run the interactive host selection flow.
///
/// This is the main entry point for host selection. It:
/// 1. Converts probe results to display info
/// 2. Shows the interactive multi-select prompt
/// 3. Returns the selection result
///
/// # Arguments
/// * `probed_hosts` - Results from probing SSH hosts
/// * `already_configured` - Set of normalized source-name keys already configured
///
/// # Returns
/// The selected hosts and their categorization, or an error if cancelled.
pub fn run_host_selection(
    probed_hosts: &[HostProbeResult],
    already_configured: &HashSet<String>,
) -> Result<(HostSelectionResult, Vec<HostDisplayInfo>), InteractiveError> {
    // Check for TTY
    if !std::io::stdin().is_terminal() {
        return Err(InteractiveError::NotATty);
    }

    // Convert probe results to display info
    let hosts: Vec<HostDisplayInfo> = probed_hosts
        .iter()
        .map(|p| probe_to_display_info(p, already_configured))
        .collect();

    // Show non-selectable hosts info
    let unreachable_count = hosts
        .iter()
        .filter(|h| h.state == HostState::Unreachable)
        .count();
    let configured_count = hosts
        .iter()
        .filter(|h| h.state == HostState::AlreadyConfigured)
        .count();

    if unreachable_count > 0 || configured_count > 0 {
        println!();
        if unreachable_count > 0 {
            println!(
                "{}",
                format!(
                    "  {} {} unreachable (check SSH config)",
                    "".yellow(),
                    unreachable_count
                )
                .dimmed()
            );
        }
        if configured_count > 0 {
            println!(
                "{}",
                format!("  {} {} already configured", "".cyan(), configured_count).dimmed()
            );
        }
    }

    // Run selection
    let selector = HostSelector::new(hosts.clone());
    let result = selector.prompt()?;

    // Show summary
    let install_count = result.needs_install.len();
    let index_count = result.needs_indexing.len();
    let sync_count = result.ready_for_sync.len();
    let total = result.selected_indices.len();

    if total > 0 {
        println!();
        let mut parts = Vec::new();
        if sync_count > 0 {
            parts.push(format!("{} ready to sync", sync_count));
        }
        if index_count > 0 {
            parts.push(format!("{} needs indexing", index_count));
        }
        if install_count > 0 {
            // Estimate install time: ~3 min per host for cargo install
            let est_mins = install_count * 3;
            parts.push(format!(
                "{} needs install (~{} min)",
                install_count, est_mins
            ));
        }
        println!(
            "  {} selected: {}",
            total.to_string().bold(),
            parts.join(", ")
        );
    }

    Ok((result, hosts))
}

// =============================================================================
// Errors
// =============================================================================

/// Errors from interactive prompts.
#[derive(Debug)]
pub enum InteractiveError {
    /// User cancelled the prompt.
    Cancelled,
    /// No hosts available to select.
    NoHosts,
    /// Hosts exist but none are selectable (all unreachable or already configured).
    NoSelectableHosts,
    /// Not running in a TTY (interactive mode required).
    NotATty,
    /// IO error during prompt.
    IoError(String),
}

impl fmt::Display for InteractiveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            InteractiveError::Cancelled => write!(f, "Operation cancelled by user"),
            InteractiveError::NoHosts => write!(f, "No hosts available for selection"),
            InteractiveError::NoSelectableHosts => {
                write!(
                    f,
                    "No selectable hosts (all unreachable or already configured)"
                )
            }
            InteractiveError::NotATty => {
                write!(
                    f,
                    "Interactive selection requires a terminal.\n\n\
                     For non-interactive use:\n  \
                     cass sources setup --hosts css,csd,yto\n  \
                     cass sources setup --non-interactive  # select all reachable"
                )
            }
            InteractiveError::IoError(msg) => write!(f, "IO error: {}", msg),
        }
    }
}

impl std::error::Error for InteractiveError {}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_host_display_info_creation() {
        let host = HostDisplayInfo {
            name: "laptop".into(),
            hostname: "192.168.1.100".into(),
            username: "user".into(),
            cass_status: CassStatusDisplay::Installed {
                version: "0.1.50".into(),
                sessions: 123,
            },
            detected_agents: vec!["claude".into(), "codex".into()],
            reachable: true,
            error: None,
            state: HostState::ReadyToSync,
            system_info: Some("ubuntu 22.04 • 45GB free".into()),
        };

        assert_eq!(host.name, "laptop");
        assert!(host.reachable);
        assert!(matches!(
            host.cass_status,
            CassStatusDisplay::Installed { .. }
        ));
        assert_eq!(host.state, HostState::ReadyToSync);
    }

    #[test]
    fn test_host_selector_format() {
        let hosts = vec![HostDisplayInfo {
            name: "test-host".into(),
            hostname: "10.0.0.1".into(),
            username: "testuser".into(),
            cass_status: CassStatusDisplay::NotInstalled,
            detected_agents: vec!["claude".into()],
            reachable: true,
            error: None,
            state: HostState::NeedsInstall,
            system_info: None,
        }];

        let selector = HostSelector::new(hosts);
        let formatted = selector.format_host(&selector.hosts[0]);

        // Check that formatting includes expected content
        assert!(formatted.contains("test-host"));
        assert!(formatted.contains("10.0.0.1"));
        assert!(formatted.contains("testuser"));
        assert!(formatted.contains("cass not installed"));
        // Agent names are capitalized for display (claude -> Claude)
        assert!(formatted.contains("Claude"));
        // Should contain status badge
        assert!(formatted.contains("Needs install"));
    }

    #[test]
    fn test_host_selector_empty() {
        let selector = HostSelector::new(vec![]);
        // Can't actually call prompt() in tests, but we can verify error handling
        assert!(selector.hosts.is_empty());
    }

    #[test]
    fn test_cass_status_display_variants() {
        let installed = CassStatusDisplay::Installed {
            version: "0.1.50".into(),
            sessions: 100,
        };
        let not_installed = CassStatusDisplay::NotInstalled;
        let unknown = CassStatusDisplay::Unknown;

        assert!(matches!(installed, CassStatusDisplay::Installed { .. }));
        assert!(matches!(not_installed, CassStatusDisplay::NotInstalled));
        assert!(matches!(unknown, CassStatusDisplay::Unknown));
    }

    #[test]
    fn test_host_selection_result() {
        let result = HostSelectionResult {
            selected_indices: vec![0, 2, 3],
            needs_install: vec![2],
            needs_indexing: vec![],
            ready_for_sync: vec![0, 3],
        };

        assert_eq!(result.selected_indices.len(), 3);
        assert_eq!(result.needs_install.len(), 1);
        assert_eq!(result.needs_indexing.len(), 0);
        assert_eq!(result.ready_for_sync.len(), 2);
    }

    #[test]
    fn test_interactive_error_display() {
        let cancelled = InteractiveError::Cancelled;
        let no_hosts = InteractiveError::NoHosts;
        let io_error = InteractiveError::IoError("test error".into());

        assert!(cancelled.to_string().contains("cancelled"));
        assert!(no_hosts.to_string().contains("No hosts"));
        assert!(io_error.to_string().contains("test error"));
    }

    #[test]
    fn test_unreachable_host_format() {
        let hosts = vec![HostDisplayInfo {
            name: "unreachable-host".into(),
            hostname: "10.0.0.99".into(),
            username: "user".into(),
            cass_status: CassStatusDisplay::Unknown,
            detected_agents: vec![],
            reachable: false,
            error: Some("Connection timed out".into()),
            state: HostState::Unreachable,
            system_info: None,
        }];

        let selector = HostSelector::new(hosts);
        let formatted = selector.format_host(&selector.hosts[0]);

        assert!(formatted.contains("unreachable-host"));
        assert!(formatted.contains("Connection timed out"));
        assert!(formatted.contains("Unreachable"));
    }

    #[test]
    fn test_host_state_properties() {
        // Test is_selectable
        assert!(HostState::ReadyToSync.is_selectable());
        assert!(HostState::NeedsIndexing.is_selectable());
        assert!(HostState::NeedsInstall.is_selectable());
        assert!(!HostState::Unreachable.is_selectable());
        assert!(!HostState::AlreadyConfigured.is_selectable());

        // Test should_preselect
        assert!(HostState::ReadyToSync.should_preselect());
        assert!(HostState::NeedsIndexing.should_preselect());
        assert!(!HostState::NeedsInstall.should_preselect());
        assert!(!HostState::Unreachable.should_preselect());
        assert!(!HostState::AlreadyConfigured.should_preselect());
    }

    #[test]
    fn test_host_state_status_badges() {
        let badge = HostState::ReadyToSync.status_badge();
        assert!(badge.contains("Ready to sync"));

        let badge = HostState::NeedsIndexing.status_badge();
        assert!(badge.contains("Needs indexing"));

        let badge = HostState::NeedsInstall.status_badge();
        assert!(badge.contains("Needs install"));

        let badge = HostState::Unreachable.status_badge();
        assert!(badge.contains("Unreachable"));

        let badge = HostState::AlreadyConfigured.status_badge();
        assert!(badge.contains("Already setup"));
    }

    #[test]
    fn test_probe_to_display_info() {
        let probe = HostProbeResult {
            host_name: "test-server".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::Indexed {
                version: "0.1.50".into(),
                session_count: 100,
                last_indexed: None,
            },
            detected_agents: vec![],
            system_info: None,
            resources: None,
            error: None,
        };

        let already_configured = HashSet::new();
        let display = probe_to_display_info(&probe, &already_configured);

        assert_eq!(display.name, "test-server");
        assert_eq!(display.state, HostState::ReadyToSync);
        assert!(matches!(
            display.cass_status,
            CassStatusDisplay::Installed { sessions: 100, .. }
        ));
    }

    #[test]
    fn test_probe_to_display_info_already_configured() {
        let probe = HostProbeResult {
            host_name: "configured-host".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::Indexed {
                version: "0.1.50".into(),
                session_count: 100,
                last_indexed: None,
            },
            detected_agents: vec![],
            system_info: None,
            resources: None,
            error: None,
        };

        let mut already_configured = HashSet::new();
        already_configured.insert("configured-host".into());
        let display = probe_to_display_info(&probe, &already_configured);

        assert_eq!(display.state, HostState::AlreadyConfigured);
    }

    #[test]
    fn test_probe_to_display_info_already_configured_case_insensitive() {
        let probe = HostProbeResult {
            host_name: "Configured-Host".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::Indexed {
                version: "0.1.50".into(),
                session_count: 100,
                last_indexed: None,
            },
            detected_agents: vec![],
            system_info: None,
            resources: None,
            error: None,
        };

        let mut already_configured = HashSet::new();
        already_configured.insert(super::super::config::source_name_key("configured-host"));
        let display = probe_to_display_info(&probe, &already_configured);

        assert_eq!(display.state, HostState::AlreadyConfigured);
    }

    #[test]
    fn test_probe_to_display_info_reserved_local_ssh_alias_already_configured() {
        let probe = HostProbeResult {
            host_name: "local".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::Indexed {
                version: "0.1.50".into(),
                session_count: 100,
                last_indexed: None,
            },
            detected_agents: vec![],
            system_info: None,
            resources: None,
            error: None,
        };

        let mut already_configured = HashSet::new();
        already_configured.insert(super::super::config::source_name_key("local-ssh"));
        let display = probe_to_display_info(&probe, &already_configured);

        assert_eq!(display.state, HostState::AlreadyConfigured);
    }

    #[test]
    fn test_installed_not_indexed_status() {
        let status = CassStatusDisplay::InstalledNotIndexed {
            version: "0.1.50".into(),
        };
        assert!(matches!(
            status,
            CassStatusDisplay::InstalledNotIndexed { .. }
        ));
    }

    #[test]
    fn test_probe_to_display_info_username_extraction() {
        use super::super::probe::SystemInfo;

        // Normal case: /home/ubuntu -> ubuntu
        let probe = HostProbeResult {
            host_name: "test".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::NotFound,
            detected_agents: vec![],
            system_info: Some(SystemInfo {
                os: "Linux".into(),
                arch: "x86_64".into(),
                distro: None,
                has_cargo: false,
                has_cargo_binstall: false,
                has_curl: false,
                has_wget: false,
                remote_home: "/home/ubuntu".into(),
                machine_id: None,
            }),
            resources: None,
            error: None,
        };
        let display = probe_to_display_info(&probe, &HashSet::new());
        assert_eq!(display.username, "ubuntu");

        // Edge case: root path "/" -> should fall back to "user"
        let probe_root = HostProbeResult {
            host_name: "test".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::NotFound,
            detected_agents: vec![],
            system_info: Some(SystemInfo {
                os: "Linux".into(),
                arch: "x86_64".into(),
                distro: None,
                has_cargo: false,
                has_cargo_binstall: false,
                has_curl: false,
                has_wget: false,
                remote_home: "/".into(),
                machine_id: None,
            }),
            resources: None,
            error: None,
        };
        let display_root = probe_to_display_info(&probe_root, &HashSet::new());
        assert_eq!(display_root.username, "user");

        // Edge case: empty path -> should fall back to "user"
        let probe_empty = HostProbeResult {
            host_name: "test".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::NotFound,
            detected_agents: vec![],
            system_info: Some(SystemInfo {
                os: "Linux".into(),
                arch: "x86_64".into(),
                distro: None,
                has_cargo: false,
                has_cargo_binstall: false,
                has_curl: false,
                has_wget: false,
                remote_home: "".into(),
                machine_id: None,
            }),
            resources: None,
            error: None,
        };
        let display_empty = probe_to_display_info(&probe_empty, &HashSet::new());
        assert_eq!(display_empty.username, "user");
    }

    #[test]
    fn test_probe_to_display_info_empty_distro_fallback() {
        use super::super::probe::SystemInfo;

        // When distro is Some(""), should fall back to OS name
        let probe = HostProbeResult {
            host_name: "test".into(),
            reachable: true,
            connection_time_ms: 50,
            cass_status: CassStatus::NotFound,
            detected_agents: vec![],
            system_info: Some(SystemInfo {
                os: "Linux".into(),
                arch: "x86_64".into(),
                distro: Some("".into()), // Empty string distro
                has_cargo: false,
                has_cargo_binstall: false,
                has_curl: false,
                has_wget: false,
                remote_home: "/home/user".into(),
                machine_id: None,
            }),
            resources: None,
            error: None,
        };
        let display = probe_to_display_info(&probe, &HashSet::new());
        // system_info should show "Linux" not empty string
        assert!(display.system_info.as_ref().unwrap().contains("Linux"));
    }
}