1
//! An open Help follows the keymap: untouched by a re-read, closed by a change; focus stays per window.
2

            
3
use super::options::{answer_config, attached_with};
4
use super::options_save::{OK, press_button};
5
use crate::app::{App, CliConfig};
6
use crate::event::{Intent, apply};
7
use crate::form::Form;
8
use crate::modal::Modal;
9
use crate::test_support::{app, help_open, key, press, viewer_title};
10
use crate::view::ViewId;
11
use crate::viewer::{Source, Viewer};
12
use ratatui::crossterm::event::KeyCode;
13

            
14
const UNSET: [Option<&str>; 5] = [None; 5];
15

            
16
2
fn help_lines(app: &App) -> Vec<String> {
17
2
    match app.front::<Viewer>() {
18
2
        Some(viewer) => viewer.lines.clone(),
19
        None => panic!("the front window is not a viewer"),
20
    }
21
2
}
22

            
23
2
fn reread_config_in_options(app: &mut App, texts: [Option<&str>; 5]) {
24
2
    apply(app, Intent::OpenOptions);
25
2
    answer_config(app, texts);
26
2
}
27

            
28
#[tokio::test]
29
1
async fn opening_and_cancelling_options_leaves_an_open_help_alone() {
30
1
    let (mut app, _) = attached_with(CliConfig::default(), UNSET);
31
1
    apply(&mut app, Intent::OpenHelp);
32
1
    let before = help_lines(&app);
33
1
    reread_config_in_options(&mut app, UNSET);
34
1
    apply(&mut app, Intent::CloseTopmost);
35
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
36
1
    apply(&mut app, Intent::CloseWindow);
37
1
    apply(&mut app, Intent::OpenHelp);
38
1
    assert_eq!(help_lines(&app), before);
39
1
}
40

            
41
#[tokio::test]
42
1
async fn a_changed_override_closes_helps_and_keeps_a_detail() {
43
1
    let (mut app, _) = attached_with(CliConfig::default(), UNSET);
44
1
    apply(&mut app, Intent::OpenHelp);
45
1
    let detail = Source::Detail {
46
1
        id: "tx".to_string(),
47
1
    };
48
1
    app.show_viewer(detail.clone(), vec!["a".to_string()]);
49
1
    reread_config_in_options(
50
1
        &mut app,
51
1
        [None, None, None, None, Some("global: F11 = open-help")],
52
1
    );
53
1
    assert!(app.viewer_of(&detail).is_some());
54
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
55
1
}
56

            
57
#[test]
58
1
fn a_console_keeps_its_prompt_focus_while_another_window_is_in_front() {
59
1
    let mut app = app();
60
1
    app.focus_console();
61
1
    let first = app.front_window().expect("the console");
62
1
    let second = app.open_extra(ViewId::Console);
63
1
    assert!(!app.console_input_focused());
64
1
    app.raise_window(first);
65
1
    assert!(app.console_input_focused());
66
1
    app.raise_window(second);
67
1
    assert!(!app.console_input_focused());
68
1
}
69

            
70
#[tokio::test]
71
1
async fn a_form_still_submits_to_its_origin_with_help_in_front() {
72
1
    let mut app = app();
73
1
    app.attach_echo();
74
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
75
1
    apply(&mut app, Intent::OpenHelp);
76
1
    assert!(help_open(&app));
77
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
78
1
    press(&mut app, key(KeyCode::Char('x')));
79
1
    press_button(&mut app, OK);
80
1
    assert!(app.overlays.is_empty());
81
1
    let tags = app.window_of(ViewId::Tags);
82
1
    let targets: Vec<_> = app
83
1
        .pending_routes
84
1
        .values()
85
2
        .map(|route| route.target)
86
        .collect();
87
1
    assert!(targets.contains(&Some(tags)), "{targets:?}");
88
1
}
89

            
90
#[test]
91
1
fn the_topic_names_the_menu_and_the_console_prompt() {
92
1
    let mut menu = app();
93
1
    menu.open_menu();
94
1
    apply(&mut menu, Intent::OpenHelp);
95
1
    assert_eq!(viewer_title(&menu), "Help: menu");
96
1
    let mut prompt = app();
97
1
    prompt.focus_console();
98
1
    apply(&mut prompt, Intent::OpenHelp);
99
1
    assert_eq!(viewer_title(&prompt), "Help: console-prompt");
100
1
}