1
//! Editing and adding from the Config tab: selection, reloads and the new-key form.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::modal::Modal;
6
use crate::tabs::fetch::Fetch;
7
use crate::tabs::nms_eval::double::Sent;
8
use crate::view::Tab;
9

            
10
7
fn reply_with(app: &mut App, entries: &str) -> anyhow::Result<()> {
11
7
    let Fetch::Loading { id } = app.config().state else {
12
        anyhow::bail!("not loading");
13
    };
14
7
    app.route_reply(id, &format!("(:id {id} :value {entries})"));
15
7
    Ok(())
16
7
}
17

            
18
6
fn config_app() -> (App, Sent) {
19
6
    let mut app = crate::test_support::app();
20
6
    let sent = app.attach_echo();
21
6
    app.switch_tab(Tab::Config);
22
6
    (app, sent)
23
6
}
24

            
25
3
fn form_values(app: &App) -> anyhow::Result<(String, String)> {
26
3
    let Some(Modal::Form(form)) = app.overlays.top() else {
27
        anyhow::bail!("no form open");
28
    };
29
3
    let values = form.values();
30
3
    Ok((
31
3
        values.text("key").unwrap_or_default().to_string(),
32
3
        values.text("value").unwrap_or_default().to_string(),
33
3
    ))
34
3
}
35

            
36
#[tokio::test]
37
1
async fn only_the_stored_keys_are_listed_and_editable() -> anyhow::Result<()> {
38
1
    let (mut app, _) = config_app();
39
1
    reply_with(&mut app, r#"((:KEY "theme" :VALUE "dark"))"#)?;
40

            
41
1
    assert_eq!(app.config().rows().len(), 1);
42
1
    apply(&mut app, Intent::EditRow);
43
1
    assert_eq!(
44
1
        form_values(&app)?,
45
1
        ("theme".to_string(), "dark".to_string())
46
    );
47
2
    Ok(())
48
1
}
49

            
50
#[tokio::test]
51
1
async fn a_new_key_opens_an_empty_form_and_is_selected_once_set() -> anyhow::Result<()> {
52
1
    let (mut app, _) = config_app();
53
1
    reply_with(
54
1
        &mut app,
55
1
        r#"((:KEY "alpha" :VALUE "1") (:KEY "theme" :VALUE "dark"))"#,
56
1
    )?;
57

            
58
1
    apply(&mut app, Intent::NewRow);
59
1
    assert_eq!(form_values(&app)?, (String::new(), String::new()));
60
1
    let Some(Modal::Form(form)) = app.overlays.top_mut() else {
61
        anyhow::bail!("no form open");
62
    };
63
1
    form.fields[0].widget.set_text("mid");
64
1
    form.fields[1].widget.set_text("v");
65
1
    apply(&mut app, Intent::Submit);
66
1
    tokio::task::yield_now().await;
67
1
    app.route_reply(2, "(:id 2 :value t)");
68

            
69
1
    reply_with(
70
1
        &mut app,
71
1
        r#"((:KEY "alpha" :VALUE "1") (:KEY "mid" :VALUE "v") (:KEY "theme" :VALUE "dark"))"#,
72
1
    )?;
73
1
    assert_eq!(app.config().selected_key(), Some("mid"));
74
2
    Ok(())
75
1
}
76

            
77
#[tokio::test]
78
1
async fn the_selection_follows_the_edited_key_after_a_set() -> anyhow::Result<()> {
79
1
    let (mut app, _) = config_app();
80
1
    reply_with(&mut app, r#"((:KEY "theme" :VALUE "dark"))"#)?;
81
1
    apply(&mut app, Intent::EditRow);
82
1
    apply(&mut app, Intent::Submit);
83
1
    tokio::task::yield_now().await;
84
1
    app.route_reply(2, "(:id 2 :value t)");
85

            
86
1
    reply_with(
87
1
        &mut app,
88
1
        r#"((:KEY "aaa" :VALUE "1") (:KEY "theme" :VALUE "dark"))"#,
89
1
    )?;
90
1
    assert_eq!(app.config().selected_key(), Some("theme"));
91
2
    Ok(())
92
1
}
93

            
94
#[tokio::test]
95
1
async fn an_edit_during_a_reload_uses_the_previous_rows() -> anyhow::Result<()> {
96
1
    let (mut app, _) = config_app();
97
1
    reply_with(&mut app, r#"((:KEY "theme" :VALUE "dark"))"#)?;
98
1
    app.fetch_config(app.window_of(crate::view::ViewId::Config));
99
1
    assert!(app.config().any_loading());
100

            
101
1
    apply(&mut app, Intent::EditRow);
102

            
103
1
    assert_eq!(
104
1
        form_values(&app)?,
105
1
        ("theme".to_string(), "dark".to_string())
106
    );
107
2
    Ok(())
108
1
}
109

            
110
#[tokio::test]
111
1
async fn an_edit_with_no_rows_says_why() {
112
1
    let (mut app, _) = config_app();
113
1
    apply(&mut app, Intent::EditRow);
114
1
    assert!(app.overlays.is_empty());
115
1
    assert_eq!(app.status, "config is loading");
116
1
}
117

            
118
#[tokio::test]
119
1
async fn an_edit_on_an_empty_loaded_list_says_no_key_is_selected() -> anyhow::Result<()> {
120
1
    let (mut app, _) = config_app();
121
1
    reply_with(&mut app, "nil")?;
122
1
    apply(&mut app, Intent::EditRow);
123
1
    assert!(app.overlays.is_empty());
124
1
    assert_eq!(app.status, "no config key selected");
125
2
    Ok(())
126
1
}