1
//! The multi-line text area of the tag form's description, through the real key dispatch.
2

            
3
use crate::test_support::{key, press};
4
use forms::{FormValues, TAG_EDIT, write_payload};
5

            
6
use super::edit::{top_form, wire_of};
7
use crate::app::App;
8
use crate::tabs::fetch::Fetch;
9
use crate::view::Tab;
10
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
11

            
12
const TAG: &str = "aaaaaaaa-0000-0000-0000-000000000001";
13

            
14
11
fn type_text(app: &mut App, text: &str) {
15
35
    text.chars().for_each(|c| press(app, key(KeyCode::Char(c))));
16
11
}
17

            
18
6
fn description(app: &App) -> Option<String> {
19
6
    top_form(app)
20
6
        .values()
21
6
        .text("description")
22
6
        .map(str::to_string)
23
6
}
24

            
25
4
fn create_form_on_description() -> anyhow::Result<App> {
26
4
    let mut app = crate::test_support::emacs_app();
27
4
    app.attach_echo();
28
4
    app.switch_tab(Tab::Tags);
29
4
    let Fetch::Loading { id } = app.tags().state else {
30
        anyhow::bail!("tags not loading");
31
    };
32
4
    app.route_reply(id, &format!("(:id {id} :value ())"));
33
4
    press(&mut app, key(KeyCode::Char('c')));
34
4
    type_text(&mut app, "area");
35
4
    press(&mut app, key(KeyCode::Tab));
36
4
    type_text(&mut app, "home");
37
4
    press(&mut app, key(KeyCode::Tab));
38
4
    Ok(app)
39
4
}
40

            
41
2
fn source(separator: &str) -> String {
42
2
    (0..50)
43
100
        .map(|i| format!("(line {i})"))
44
        .collect::<Vec<_>>()
45
2
        .join(separator)
46
2
}
47

            
48
4
fn edit_form_on_description(text: &str) -> anyhow::Result<App> {
49
4
    let mut app = crate::test_support::emacs_app();
50
4
    app.attach_echo();
51
4
    app.switch_tab(Tab::Tags);
52
4
    let Fetch::Loading { id } = app.tags().state else {
53
        anyhow::bail!("tags not loading");
54
    };
55
4
    let entry = format!(r#"(:tag :id "{TAG}" :name "kind" :value "fiat" :description "")"#);
56
4
    app.route_reply(id, &format!("(:id {id} :value ({entry}))"));
57
4
    press(&mut app, key(KeyCode::Char('e')));
58
4
    let mut values = FormValues::default();
59
4
    values.set("id", TAG);
60
4
    values.set("revision", "rev-1");
61
4
    values.set("name", "kind");
62
4
    values.set("value", "fiat");
63
4
    values.set("description", text);
64
4
    app.deliver_tag_edit_reply(
65
4
        Some(app.window_of(crate::view::ViewId::Tags)),
66
4
        &wire_of(&write_payload(&TAG_EDIT, &values)),
67
4
    );
68
4
    press(&mut app, key(KeyCode::Tab));
69
4
    press(&mut app, key(KeyCode::Tab));
70
4
    Ok(app)
71
4
}
72

            
73
#[tokio::test]
74
1
async fn enter_types_a_newline_and_ctrl_s_submits() -> anyhow::Result<()> {
75
1
    let mut app = create_form_on_description()?;
76
1
    type_text(&mut app, "a");
77
1
    press(&mut app, key(KeyCode::Enter));
78
1
    type_text(&mut app, "b");
79
1
    assert_eq!(description(&app), Some("a\nb".to_string()));
80
1
    assert!(app.pending_form.is_none());
81

            
82
1
    press(
83
1
        &mut app,
84
1
        KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL),
85
1
    );
86
1
    assert!(app.pending_form.is_some());
87
2
    Ok(())
88
1
}
89

            
90
#[tokio::test]
91
1
async fn alt_enter_submits_instead_of_typing_a_newline() -> anyhow::Result<()> {
92
1
    let mut app = create_form_on_description()?;
93
1
    type_text(&mut app, "a");
94
1
    press(&mut app, KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT));
95
1
    assert!(app.pending_form.is_some());
96
2
    Ok(())
97
1
}
98

            
99
#[tokio::test]
100
1
async fn enter_still_submits_from_a_single_line_field() -> anyhow::Result<()> {
101
1
    let mut app = create_form_on_description()?;
102
1
    press(&mut app, key(KeyCode::BackTab));
103
1
    press(&mut app, key(KeyCode::Enter));
104
1
    assert!(app.pending_form.is_some());
105
2
    Ok(())
106
1
}
107

            
108
#[tokio::test]
109
1
async fn a_single_line_field_takes_horizontal_cursor_keys_only() -> anyhow::Result<()> {
110
1
    let mut app = create_form_on_description()?;
111
1
    press(&mut app, key(KeyCode::BackTab));
112
5
    let cursor = |app: &mut App| {
113
5
        app.overlays
114
5
            .form_mut()
115
5
            .and_then(|f| f.focused_editor_mut())
116
5
            .map(|e| e.cursor())
117
5
    };
118
1
    let before = cursor(&mut app);
119
1
    for code in [KeyCode::Up, KeyCode::PageDown] {
120
2
        press(&mut app, key(code));
121
2
        assert_eq!(cursor(&mut app), before, "{code:?}");
122
    }
123
1
    press(&mut app, key(KeyCode::Left));
124
1
    assert_eq!(cursor(&mut app), before.map(|c| c - 1));
125
1
    press(&mut app, key(KeyCode::Home));
126
1
    assert_eq!(cursor(&mut app), Some(0));
127
2
    Ok(())
128
1
}
129

            
130
#[tokio::test]
131
1
async fn a_loaded_source_survives_moving_around() -> anyhow::Result<()> {
132
1
    for separator in ["\n", "\r\n"] {
133
2
        let text = source(separator);
134
2
        let mut app = edit_form_on_description(&text)?;
135
2
        for code in [
136
2
            KeyCode::PageUp,
137
2
            KeyCode::Up,
138
2
            KeyCode::Down,
139
2
            KeyCode::Home,
140
2
            KeyCode::End,
141
2
            KeyCode::Left,
142
2
            KeyCode::Right,
143
2
            KeyCode::PageDown,
144
2
            KeyCode::PageUp,
145
2
            KeyCode::PageUp,
146
2
            KeyCode::PageUp,
147
2
            KeyCode::PageUp,
148
2
        ] {
149
24
            press(&mut app, key(code));
150
1
        }
151
2
        assert_eq!(description(&app), Some(text.clone()));
152
1

            
153
2
        press(
154
2
            &mut app,
155
2
            KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL),
156
2
        );
157
2
        assert!(app.pending_form.is_some());
158
1
    }
159
1
    Ok(())
160
1
}
161

            
162
#[tokio::test]
163
1
async fn a_typed_newline_in_a_crlf_source_is_lf() -> anyhow::Result<()> {
164
1
    let mut app = edit_form_on_description("a\r\nb")?;
165
1
    press(&mut app, key(KeyCode::PageUp));
166
1
    press(&mut app, key(KeyCode::End));
167
1
    press(&mut app, key(KeyCode::Enter));
168
1
    assert_eq!(description(&app), Some("a\n\r\nb".to_string()));
169
2
    Ok(())
170
1
}
171

            
172
#[tokio::test]
173
1
async fn ctrl_k_kills_only_the_rest_of_the_line() -> anyhow::Result<()> {
174
1
    let mut app = edit_form_on_description("abcd\nef")?;
175
1
    press(&mut app, key(KeyCode::PageUp));
176
1
    press(&mut app, key(KeyCode::Home));
177
1
    press(&mut app, key(KeyCode::Right));
178
1
    press(
179
1
        &mut app,
180
1
        KeyEvent::new(KeyCode::Char('k'), KeyModifiers::CONTROL),
181
1
    );
182
1
    assert_eq!(description(&app), Some("a\nef".to_string()));
183
1
    press(
184
1
        &mut app,
185
1
        KeyEvent::new(KeyCode::Char('k'), KeyModifiers::CONTROL),
186
1
    );
187
1
    assert_eq!(description(&app), Some("aef".to_string()));
188
2
    Ok(())
189
1
}