1
//! The Options dialog saves the changed settings through `config-set`, and rolls back a refusal.
2

            
3
use super::options::{attached_with, choose, dialog, label, next_field, opened, saves};
4
use super::report_chart::echoed;
5
use crate::app::{App, CliConfig};
6
use crate::event::{Intent, apply};
7
use crate::modal::Modal;
8
use crate::tabs::nms_eval::double::Sent;
9
use crate::test_support::{alt, key, press};
10
use ratatui::crossterm::event::KeyCode;
11
use tvui::{ColorDepth, Role, Theme};
12

            
13
pub(super) const OK: usize = 0;
14
const APPLY: usize = 2;
15
const UNSET: [Option<&str>; 5] = [None; 5];
16

            
17
/// Moves the focus from the first field to the button `index` of the dialog.
18
14
fn focus_button(app: &mut App, index: usize) {
19
14
    for _ in 0..10 {
20
70
        if app.overlays.top().and_then(Modal::focused_button) == Some(index) {
21
14
            break;
22
56
        }
23
56
        press(app, key(KeyCode::Tab));
24
    }
25
14
    assert_eq!(
26
14
        app.overlays.top().and_then(Modal::focused_button),
27
14
        Some(index)
28
    );
29
14
}
30

            
31
14
pub(super) fn press_button(app: &mut App, index: usize) {
32
14
    focus_button(app, index);
33
14
    press(app, key(KeyCode::Enter));
34
14
}
35

            
36
/// The last request sent: the save, after the read of the config.
37
6
pub(super) async fn saved_form(app: &mut App, sent: &Sent) -> String {
38
6
    echoed(app, sent).await;
39
6
    sent.frames().pop().unwrap_or_default()
40
6
}
41

            
42
9
pub(super) fn answer_save(app: &mut App, reply: &str) {
43
9
    let [id] = saves(app)[..] else {
44
        panic!("one save");
45
    };
46
9
    app.route_reply(id, &format!("(:id {id} {reply})"));
47
9
}
48

            
49
1
fn reopened_theme(app: &mut App) -> String {
50
1
    apply(app, Intent::OpenOptions);
51
1
    label(app, "theme").to_string()
52
1
}
53

            
54
#[tokio::test]
55
1
async fn ok_sends_exactly_the_changed_key_and_closes_when_saved() {
56
1
    let (mut app, sent) = opened(UNSET);
57
1
    choose(&mut app, KeyCode::Down, 1);
58
1
    press_button(&mut app, OK);
59
1
    assert!(app.pending_form.is_some(), "the dialog waits for the reply");
60
1
    let echoed = saved_form(&mut app, &sent).await;
61
1
    assert!(echoed.contains("tui-theme"), "{echoed}");
62
1
    assert!(echoed.contains("turbo"), "{echoed}");
63
1
    for other in ["tui-keymap", "tui-colors", "overrides"] {
64
3
        assert!(!echoed.contains(other), "{echoed}");
65
    }
66
1
    answer_save(&mut app, ":value t");
67
1
    assert!(app.pending_form.is_none());
68
1
    assert!(app.overlays.is_empty());
69
1
    assert_eq!(reopened_theme(&mut app), "turbo");
70
1
}
71

            
72
#[tokio::test]
73
1
async fn ok_without_a_change_closes_and_sends_nothing() {
74
1
    let (mut app, sent) = opened(UNSET);
75
1
    press_button(&mut app, OK);
76
1
    assert!(app.overlays.is_empty());
77
1
    assert!(saves(&app).is_empty());
78
1
    assert!(!saved_form(&mut app, &sent).await.contains("config-set"));
79
1
}
80

            
81
#[tokio::test]
82
1
async fn apply_saves_and_keeps_the_dialog_with_the_new_state_as_its_cancel_target() {
83
1
    let (mut app, sent) = opened(UNSET);
84
1
    next_field(&mut app);
85
1
    choose(&mut app, KeyCode::Down, 2);
86
1
    press_button(&mut app, APPLY);
87
1
    assert_eq!(app.overlays.len(), 1);
88
1
    let echoed = saved_form(&mut app, &sent).await;
89
1
    assert!(
90
1
        echoed.contains("tui-keymap") && echoed.contains("vim"),
91
        "{echoed}"
92
    );
93
1
    assert!(!echoed.contains("tui-theme"), "{echoed}");
94
1
    answer_save(&mut app, ":value t");
95
1
    press(&mut app, key(KeyCode::Esc));
96
1
    assert_eq!(
97
1
        app.keys.indicator(),
98
1
        "vim:insert",
99
1
        "Cancel keeps what Apply saved"
100
1
    );
101
1
}
102

            
103
#[tokio::test]
104
1
async fn a_refused_ok_rolls_the_ui_back_and_shows_the_message() {
105
1
    let (mut app, _) = opened(UNSET);
106
1
    let dn = app.theme.bg(Role::Desktop);
107
1
    choose(&mut app, KeyCode::Down, 1);
108
1
    assert_ne!(app.theme.bg(Role::Desktop), dn);
109
1
    press_button(&mut app, OK);
110
1
    answer_save(&mut app, r#":error (:code eval :message "boom")"#);
111
1
    assert_eq!(app.theme.bg(Role::Desktop), dn);
112
1
    assert_eq!(
113
1
        label(&app, "theme"),
114
1
        "turbo",
115
        "the dialog keeps what was picked"
116
    );
117
1
    let errors = dialog(&app).errors.join("\n");
118
1
    assert!(errors.contains("boom"), "{errors}");
119
1
    assert!(app.status.contains("boom"), "{}", app.status);
120
1
}
121

            
122
#[tokio::test]
123
1
async fn a_refused_apply_rolls_the_ui_back_and_shows_the_message() {
124
1
    let (mut app, _) = opened(UNSET);
125
1
    let dn = app.theme.bg(Role::Desktop);
126
1
    choose(&mut app, KeyCode::Down, 1);
127
1
    press_button(&mut app, APPLY);
128
1
    answer_save(&mut app, r#":error (:code eval :message "boom")"#);
129
1
    assert_eq!(app.theme.bg(Role::Desktop), dn);
130
1
    assert_eq!(
131
1
        label(&app, "theme"),
132
1
        "turbo",
133
        "the dialog keeps what was picked"
134
    );
135
1
    let errors = dialog(&app).errors.join("\n");
136
1
    assert!(
137
1
        errors.contains("save failed") && errors.contains("boom"),
138
1
        "{errors}"
139
1
    );
140
1
}
141

            
142
#[tokio::test]
143
1
async fn a_flag_wins_and_is_never_saved() {
144
1
    let cli = CliConfig {
145
1
        preset: Some("turbo".to_string()),
146
1
        depth: Some(ColorDepth::Ansi16),
147
1
        keymap: None,
148
1
    };
149
1
    let (mut app, sent) = attached_with(cli, UNSET);
150
1
    apply(&mut app, Intent::OpenOptions);
151
1
    let turbo = Theme::preset("turbo")
152
1
        .expect("preset")
153
1
        .with_depth(ColorDepth::Ansi16);
154
1
    assert_eq!(app.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
155
1
    next_field(&mut app);
156
1
    choose(&mut app, KeyCode::Down, 1);
157
1
    press_button(&mut app, OK);
158
1
    let echoed = saved_form(&mut app, &sent).await;
159
1
    assert!(echoed.contains("tui-keymap"), "{echoed}");
160
1
    assert!(
161
1
        !echoed.contains("tui-theme") && !echoed.contains("tui-colors"),
162
        "{echoed}"
163
    );
164
1
    assert_eq!(app.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
165
1
}
166

            
167
#[tokio::test]
168
1
async fn a_partly_saved_refusal_ends_on_what_the_server_holds() {
169
1
    let (mut app, _) = opened(UNSET);
170
1
    choose(&mut app, KeyCode::Down, 1);
171
1
    next_field(&mut app);
172
1
    choose(&mut app, KeyCode::Down, 1);
173
1
    press_button(&mut app, OK);
174
1
    answer_save(&mut app, r#":error (:code eval :message "boom")"#);
175
1
    assert_eq!(app.keys.indicator(), "cua");
176
1
    super::options::answer_config(&mut app, [Some("turbo"), None, None, None, None]);
177
1
    let turbo = Theme::preset("turbo").expect("preset");
178
1
    assert_eq!(app.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
179
1
    assert_eq!(app.keys.indicator(), "cua");
180
1
    assert_eq!(label(&app, "theme"), "turbo");
181
1
    assert_eq!(
182
1
        label(&app, "keymap"),
183
1
        "emacs",
184
1
        "the dialog keeps what was picked"
185
1
    );
186
1
}
187

            
188
#[tokio::test]
189
1
async fn a_picked_keymap_takes_over_only_once_saved() {
190
1
    let (mut app, _) = opened([None, None, None, Some("emacs"), None]);
191
1
    next_field(&mut app);
192
1
    choose(&mut app, KeyCode::Up, 1);
193
1
    assert_eq!(label(&app, "keymap"), "cua");
194
1
    assert_eq!(
195
1
        app.keys.indicator(),
196
1
        "emacs",
197
        "the dialog keeps the saved keys"
198
    );
199
1
    press(&mut app, key(KeyCode::F(1)));
200
1
    assert_eq!(app.overlays.len(), 1, "F1 is not bound in emacs");
201
1
    press_button(&mut app, OK);
202
1
    answer_save(&mut app, ":value t");
203
1
    assert_eq!(app.keys.indicator(), "cua");
204
1
    press(&mut app, key(KeyCode::F(1)));
205
1
    assert!(crate::test_support::help_open(&app), "F1 opens help in cua");
206
1
}
207

            
208
#[tokio::test]
209
1
async fn apply_makes_the_picked_keymap_the_one_in_use() {
210
1
    let (mut app, _) = opened(UNSET);
211
1
    next_field(&mut app);
212
1
    choose(&mut app, KeyCode::Down, 2);
213
1
    assert_eq!(app.keys.indicator(), "cua");
214
1
    press_button(&mut app, APPLY);
215
1
    answer_save(&mut app, ":value t");
216
1
    assert_eq!(app.keys.indicator(), "vim:insert");
217
1
    assert_eq!(app.overlays.len(), 1);
218
1
}
219

            
220
#[tokio::test]
221
1
async fn alt_p_applies_like_the_apply_button() {
222
1
    let (mut app, sent) = opened(UNSET);
223
1
    next_field(&mut app);
224
1
    choose(&mut app, KeyCode::Down, 1);
225
1
    press(&mut app, alt(KeyCode::Char('p')));
226
1
    assert_eq!(saves(&app).len(), 1);
227
1
    assert_eq!(app.overlays.len(), 1, "Apply keeps the dialog");
228
1
    let form = saved_form(&mut app, &sent).await;
229
1
    assert!(
230
1
        form.contains("tui-keymap") && form.contains("emacs"),
231
1
        "{form}"
232
1
    );
233
1
}