Lines
99.44 %
Functions
100 %
Branches
//! The Options dialog saves the changed settings through `config-set`, and rolls back a refusal.
use super::options::{attached_with, choose, dialog, label, next_field, opened, saves};
use super::report_chart::echoed;
use crate::app::{App, CliConfig};
use crate::event::{Intent, apply};
use crate::modal::Modal;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{alt, key, press};
use ratatui::crossterm::event::KeyCode;
use tvui::{ColorDepth, Role, Theme};
pub(super) const OK: usize = 0;
const APPLY: usize = 2;
const UNSET: [Option<&str>; 5] = [None; 5];
/// Moves the focus from the first field to the button `index` of the dialog.
fn focus_button(app: &mut App, index: usize) {
for _ in 0..10 {
if app.overlays.top().and_then(Modal::focused_button) == Some(index) {
break;
}
press(app, key(KeyCode::Tab));
assert_eq!(
app.overlays.top().and_then(Modal::focused_button),
Some(index)
);
pub(super) fn press_button(app: &mut App, index: usize) {
focus_button(app, index);
press(app, key(KeyCode::Enter));
/// The last request sent: the save, after the read of the config.
pub(super) async fn saved_form(app: &mut App, sent: &Sent) -> String {
echoed(app, sent).await;
sent.frames().pop().unwrap_or_default()
pub(super) fn answer_save(app: &mut App, reply: &str) {
let [id] = saves(app)[..] else {
panic!("one save");
};
app.route_reply(id, &format!("(:id {id} {reply})"));
fn reopened_theme(app: &mut App) -> String {
apply(app, Intent::OpenOptions);
label(app, "theme").to_string()
#[tokio::test]
async fn ok_sends_exactly_the_changed_key_and_closes_when_saved() {
let (mut app, sent) = opened(UNSET);
choose(&mut app, KeyCode::Down, 1);
press_button(&mut app, OK);
assert!(app.pending_form.is_some(), "the dialog waits for the reply");
let echoed = saved_form(&mut app, &sent).await;
assert!(echoed.contains("tui-theme"), "{echoed}");
assert!(echoed.contains("turbo"), "{echoed}");
for other in ["tui-keymap", "tui-colors", "overrides"] {
assert!(!echoed.contains(other), "{echoed}");
answer_save(&mut app, ":value t");
assert!(app.pending_form.is_none());
assert!(app.overlays.is_empty());
assert_eq!(reopened_theme(&mut app), "turbo");
async fn ok_without_a_change_closes_and_sends_nothing() {
assert!(saves(&app).is_empty());
assert!(!saved_form(&mut app, &sent).await.contains("config-set"));
async fn apply_saves_and_keeps_the_dialog_with_the_new_state_as_its_cancel_target() {
next_field(&mut app);
choose(&mut app, KeyCode::Down, 2);
press_button(&mut app, APPLY);
assert_eq!(app.overlays.len(), 1);
assert!(
echoed.contains("tui-keymap") && echoed.contains("vim"),
"{echoed}"
assert!(!echoed.contains("tui-theme"), "{echoed}");
press(&mut app, key(KeyCode::Esc));
app.keys.indicator(),
"vim:insert",
"Cancel keeps what Apply saved"
async fn a_refused_ok_rolls_the_ui_back_and_shows_the_message() {
let (mut app, _) = opened(UNSET);
let dn = app.theme.bg(Role::Desktop);
assert_ne!(app.theme.bg(Role::Desktop), dn);
answer_save(&mut app, r#":error (:code eval :message "boom")"#);
assert_eq!(app.theme.bg(Role::Desktop), dn);
label(&app, "theme"),
"turbo",
"the dialog keeps what was picked"
let errors = dialog(&app).errors.join("\n");
assert!(errors.contains("boom"), "{errors}");
assert!(app.status.contains("boom"), "{}", app.status);
async fn a_refused_apply_rolls_the_ui_back_and_shows_the_message() {
errors.contains("save failed") && errors.contains("boom"),
"{errors}"
async fn a_flag_wins_and_is_never_saved() {
let cli = CliConfig {
preset: Some("turbo".to_string()),
depth: Some(ColorDepth::Ansi16),
keymap: None,
let (mut app, sent) = attached_with(cli, UNSET);
apply(&mut app, Intent::OpenOptions);
let turbo = Theme::preset("turbo")
.expect("preset")
.with_depth(ColorDepth::Ansi16);
assert_eq!(app.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
assert!(echoed.contains("tui-keymap"), "{echoed}");
!echoed.contains("tui-theme") && !echoed.contains("tui-colors"),
async fn a_partly_saved_refusal_ends_on_what_the_server_holds() {
assert_eq!(app.keys.indicator(), "cua");
super::options::answer_config(&mut app, [Some("turbo"), None, None, None, None]);
let turbo = Theme::preset("turbo").expect("preset");
assert_eq!(label(&app, "theme"), "turbo");
label(&app, "keymap"),
"emacs",
async fn a_picked_keymap_takes_over_only_once_saved() {
let (mut app, _) = opened([None, None, None, Some("emacs"), None]);
choose(&mut app, KeyCode::Up, 1);
assert_eq!(label(&app, "keymap"), "cua");
"the dialog keeps the saved keys"
press(&mut app, key(KeyCode::F(1)));
assert_eq!(app.overlays.len(), 1, "F1 is not bound in emacs");
assert!(crate::test_support::help_open(&app), "F1 opens help in cua");
async fn apply_makes_the_picked_keymap_the_one_in_use() {
assert_eq!(app.keys.indicator(), "vim:insert");
async fn alt_p_applies_like_the_apply_button() {
press(&mut app, alt(KeyCode::Char('p')));
assert_eq!(saves(&app).len(), 1);
assert_eq!(app.overlays.len(), 1, "Apply keeps the dialog");
let form = saved_form(&mut app, &sent).await;
form.contains("tui-keymap") && form.contains("emacs"),
"{form}"