1
//! Help is a desktop window keyed by its topic: sizeable, behind any dialog.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::test_support::{app, help_open, key, press, viewer_title};
8
use crate::view::ViewId;
9
use crate::viewer::Source;
10
use crate::window::WindowId;
11
use ratatui::crossterm::event::KeyCode;
12
use ratatui::layout::Rect;
13
use tvui::{Caps, Change};
14

            
15
4
fn size_of(app: &App, id: WindowId) -> Rect {
16
4
    let place = app.desktop.restored_place(id, app.desktop_rect());
17
4
    crate::test_support::flat(place.expect("the window is open"))
18
4
}
19

            
20
5
fn help_with_list_behind() -> (App, WindowId, WindowId) {
21
5
    let mut app = app();
22
5
    let list = app.front_window().expect("a list window");
23
5
    apply(&mut app, Intent::OpenHelp);
24
5
    let help = app.front_window().expect("the help window");
25
5
    (app, list, help)
26
5
}
27

            
28
#[test]
29
1
fn help_opens_as_a_window_that_zooms_and_grows() {
30
1
    let (mut app, list, help) = help_with_list_behind();
31
1
    assert_ne!(help, list);
32
1
    assert!(help_open(&app));
33
1
    assert!(app.overlays.is_empty());
34
1
    let slot = app.desktop.window(help).expect("open");
35
1
    assert_eq!(slot.caps, Caps::ALL);
36
1
    let titles: Vec<String> = app.window_entries().into_iter().map(|(_, l)| l).collect();
37
11
    assert!(titles.iter().any(|t| t.starts_with("Help: ")), "{titles:?}");
38
1
    app.toggle_zoom();
39
1
    assert!(app.desktop.window(help).is_some_and(|slot| slot.zoomed));
40
1
    app.toggle_zoom();
41
1
    let before = size_of(&app, help);
42
1
    app.desktop
43
1
        .move_grow(help, app.desktop_rect(), Change::Grow(-3, -2));
44
1
    let after = size_of(&app, help);
45
1
    assert_eq!(
46
1
        (after.width, after.height),
47
1
        (before.width - 3, before.height - 2)
48
    );
49
1
}
50

            
51
#[test]
52
1
fn the_same_topic_raises_the_open_help_and_another_opens_a_second() {
53
1
    let (mut app, list, help) = help_with_list_behind();
54
1
    app.raise_window(list);
55
1
    apply(&mut app, Intent::OpenHelp);
56
1
    assert_eq!(app.front_window(), Some(help));
57
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
58
1
    apply(&mut app, Intent::OpenHelp);
59
1
    assert_eq!(app.windows_of(ViewId::Viewer), 2);
60
1
    assert_ne!(app.front_window(), Some(help));
61
1
}
62

            
63
#[test]
64
1
fn help_asked_for_under_a_form_opens_behind_it() {
65
1
    let mut app = app();
66
1
    app.open_dialog_on_front(Modal::Form(Form::config_set()));
67
1
    apply(&mut app, Intent::OpenHelp);
68
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
69
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
70
1
}
71

            
72
#[test]
73
1
fn esc_and_q_close_help() {
74
1
    for code in [KeyCode::Esc, KeyCode::Char('q')] {
75
2
        let (mut app, list, _) = help_with_list_behind();
76
2
        press(&mut app, key(code));
77
2
        assert_eq!(app.windows_of(ViewId::Viewer), 0, "{code:?}");
78
2
        assert_eq!(app.front_window(), Some(list), "{code:?}");
79
    }
80
1
}
81

            
82
#[test]
83
1
fn a_resized_help_keeps_its_size_on_re_raise() {
84
1
    let (mut app, list, help) = help_with_list_behind();
85
1
    app.desktop
86
1
        .move_grow(help, app.desktop_rect(), Change::Grow(-7, -3));
87
1
    let grown = size_of(&app, help);
88
1
    app.raise_window(list);
89
1
    apply(&mut app, Intent::OpenHelp);
90
1
    assert_eq!(app.front_window(), Some(help));
91
1
    assert_eq!(size_of(&app, help), grown);
92
1
}
93

            
94
#[test]
95
1
fn the_topic_is_the_innermost_scope_of_the_context() {
96
1
    let mut list = app();
97
1
    list.switch_tab(crate::view::Tab::Accounts);
98
1
    apply(&mut list, Intent::OpenHelp);
99
1
    assert_eq!(viewer_title(&list), "Help: tab-accounts");
100
1
    apply(&mut list, Intent::OpenHelp);
101
1
    assert_eq!(viewer_title(&list), "Help: viewer");
102
1
    let mut form = app();
103
1
    form.open_dialog_on_front(Modal::Form(Form::config_set()));
104
1
    apply(&mut form, Intent::OpenHelp);
105
1
    assert_eq!(viewer_title(&form), "Help: overlay");
106
1
    let mut cmdline = app();
107
1
    cmdline.open_command_line();
108
1
    apply(&mut cmdline, Intent::OpenHelp);
109
1
    assert_eq!(viewer_title(&cmdline), "Help: cmdline");
110
1
}
111

            
112
#[test]
113
1
fn closing_help_or_a_detail_keeps_the_console_prompt_focused() {
114
1
    let mut app = app();
115
1
    app.focus_console();
116
1
    press(&mut app, key(KeyCode::F(1)));
117
1
    assert!(help_open(&app));
118
1
    press(&mut app, key(KeyCode::Esc));
119
1
    assert!(app.console_input_focused());
120
1
    app.show_viewer(
121
1
        Source::Detail {
122
1
            id: "tx".to_string(),
123
1
        },
124
1
        vec!["a".to_string()],
125
1
    );
126
1
    assert!(!app.console_input_focused());
127
1
    press(&mut app, key(KeyCode::Esc));
128
1
    assert!(app.console_input_focused());
129
1
}