1
//! The `ssh-key` palette commands reach the tab's own paths; `sql selcol` is gone.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::modal::{ConfirmAction, Modal};
6
use crate::view::Tab;
7
use cli_core::command_tree;
8
use cli_core::commands::CommandNode;
9

            
10
26
fn run(app: &mut App, command: &str) {
11
26
    apply(app, Intent::OpenCommandLine);
12
26
    command
13
26
        .chars()
14
395
        .for_each(|c| apply(app, Intent::InsertChar(c)));
15
26
    apply(app, Intent::Submit);
16
26
}
17

            
18
54
fn leaf_paths(nodes: &[CommandNode], prefix: &[String], out: &mut Vec<String>) {
19
54
    for node in nodes {
20
52
        let mut path = prefix.to_vec();
21
52
        path.push(node.name.clone());
22
52
        if node.is_leaf {
23
40
            out.push(path.join(" "));
24
12
        }
25
52
        leaf_paths(&node.subcommands, &path, out);
26
    }
27
54
}
28

            
29
#[test]
30
1
fn ssh_key_list_switches_to_the_tab() {
31
1
    let mut app = crate::test_support::app();
32
1
    run(&mut app, "ssh-key list");
33
1
    assert_eq!(app.front_kind(), Some(Tab::SshKeys));
34
1
}
35

            
36
#[test]
37
1
fn ssh_key_add_opens_the_form_and_prefills_a_given_key() -> anyhow::Result<()> {
38
1
    let mut app = crate::test_support::app();
39
1
    run(&mut app, "ssh-key add key=ssh-ed25519");
40
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
41
        anyhow::bail!("no form open");
42
    };
43
1
    assert_eq!(form.values().text("key"), Some("ssh-ed25519"));
44
1
    Ok(())
45
1
}
46

            
47
#[test]
48
1
fn ssh_key_remove_asks_about_the_named_fingerprint() -> anyhow::Result<()> {
49
1
    let mut app = crate::test_support::app();
50
1
    run(&mut app, "ssh-key remove fingerprint=SHA256:zzz");
51
1
    let Some(Modal::Confirm { prompt, action, .. }) = app.overlays.top() else {
52
        anyhow::bail!("no confirm open");
53
    };
54
1
    assert!(prompt.contains("SHA256:zzz"), "{prompt}");
55
1
    assert!(matches!(action, ConfirmAction::RemoveSshKey(fp) if fp == "SHA256:zzz"));
56
1
    Ok(())
57
1
}
58

            
59
#[test]
60
1
fn ssh_key_remove_without_a_fingerprint_or_selection_says_so() {
61
1
    let mut app = crate::test_support::app();
62
1
    run(&mut app, "ssh-key remove");
63
1
    assert!(app.overlays.is_empty());
64
1
    assert_eq!(app.status, "no SSH key selected");
65
1
}
66

            
67
#[test]
68
1
fn ssh_key_commands_reject_unknown_args() {
69
1
    let mut app = crate::test_support::app();
70
1
    run(&mut app, "ssh-key remove bogus=1");
71
1
    assert!(app.overlays.is_empty());
72
1
    assert!(app.status.contains("unknown arg: bogus"), "{}", app.status);
73
1
}
74

            
75
#[test]
76
1
fn sql_selcol_is_not_in_the_palette() {
77
1
    let mut app = crate::test_support::app();
78
1
    run(&mut app, "sql selcol");
79
1
    assert_eq!(app.status, "unknown command: sql selcol");
80
1
    let mut leaves = Vec::new();
81
1
    leaf_paths(&command_tree(), &[], &mut leaves);
82
20
    assert!(leaves.iter().all(|leaf| !leaf.starts_with("sql")));
83
1
}
84

            
85
#[test]
86
1
fn every_palette_leaf_has_a_tui_handler() {
87
1
    let mut leaves = Vec::new();
88
1
    leaf_paths(&command_tree(), &[], &mut leaves);
89
1
    assert!(leaves.contains(&"ssh-key add".to_string()));
90
1
    for leaf in leaves {
91
20
        let mut app = crate::test_support::app();
92
20
        run(&mut app, &leaf);
93
20
        assert!(
94
20
            !app.status.starts_with("unknown command"),
95
            "`{leaf}` has no handler: {}",
96
            app.status
97
        );
98
    }
99
1
}