1
//! A dialog raises the window it belongs to, keeps it from the request to the reply, and forgets
2
//! it once the dialog is over.
3

            
4
use super::*;
5
use crate::event::{Intent, apply};
6
use crate::form::Form;
7
use crate::modal::Modal;
8
use forms::FormValues;
9

            
10
const CASH: &str = "550e8400-e29b-41d4-a716-446655440071";
11

            
12
1
fn run(app: &mut App, command: &str) {
13
1
    apply(app, Intent::OpenCommandLine);
14
1
    command
15
1
        .chars()
16
26
        .for_each(|c| apply(app, Intent::InsertChar(c)));
17
1
    apply(app, Intent::Submit);
18
1
}
19

            
20
5
fn loading(app: &App, window: WindowId) -> bool {
21
5
    app.win_by(window).is_some_and(|win| win.view.loading())
22
5
}
23

            
24
4
fn origin_window(app: &App) -> Option<WindowId> {
25
4
    app.overlays.origin().map(|origin| origin.window)
26
4
}
27

            
28
3
fn the_only_request(app: &App) -> i64 {
29
3
    let mut ids = app.pending_routes.keys();
30
3
    match (ids.next(), ids.next()) {
31
3
        (Some(id), None) => *id,
32
        _ => panic!("one request expected"),
33
    }
34
3
}
35

            
36
2
fn account_options() -> String {
37
2
    super::edit::options_wire(&[(CASH, "Assets:Cash")])
38
2
}
39

            
40
#[tokio::test]
41
1
async fn a_dialog_raises_its_rear_origin() {
42
1
    let mut app = crate::test_support::app();
43
1
    app.attach_echo();
44
1
    let tags = app.window_of(ViewId::Tags);
45
1
    assert_ne!(app.front_window(), Some(tags));
46
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
47
1
    assert_eq!(app.front_window(), Some(tags));
48
1
    assert_eq!(origin_window(&app), Some(tags));
49
1
}
50

            
51
#[tokio::test]
52
1
async fn a_dialog_loads_the_rear_window_it_raises() {
53
1
    let mut app = crate::test_support::app();
54
1
    app.attach_echo();
55
1
    let tags = app.window_of(ViewId::Tags);
56
1
    assert!(!loading(&app, tags));
57
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
58
1
    assert!(loading(&app, tags));
59
1
}
60

            
61
#[tokio::test]
62
1
async fn a_dialog_for_a_missing_window_opens_it_loaded() {
63
1
    let mut app = crate::test_support::app();
64
1
    app.attach_echo();
65
1
    app.close_window(app.window_of(ViewId::Config));
66
1
    app.open_dialog_for(ViewId::Config, Modal::Form(Form::config_set()));
67
1
    let config = app.window_of(ViewId::Config);
68
1
    assert_eq!(app.front_window(), Some(config));
69
1
    assert!(loading(&app, config));
70
1
}
71

            
72
#[tokio::test]
73
1
async fn a_filter_for_a_missing_window_opens_it_loaded() {
74
1
    let mut app = crate::test_support::app();
75
1
    app.attach_echo();
76
1
    app.close_window(app.window_of(ViewId::Transactions));
77
1
    app.filter_transactions(FormValues::default());
78
1
    let window = app.window_of(ViewId::Transactions);
79
1
    let loads = app
80
1
        .pending_routes
81
1
        .values()
82
2
        .filter(|route| route.target == Some(window) && matches!(route.ctx, RouteCtx::None))
83
        .count();
84
1
    assert_eq!(loads, 1);
85
1
}
86

            
87
#[tokio::test]
88
1
async fn a_merge_reply_opens_its_form_over_the_window_that_asked() {
89
1
    let mut app = crate::test_support::app();
90
1
    app.attach_echo();
91
1
    let older = app.window_of(ViewId::Accounts);
92
1
    let asked = app.open_extra(ViewId::Accounts);
93
1
    let mut values = FormValues::default();
94
1
    values.set("from", CASH);
95
1
    app.open_account_merge(values);
96
1
    app.raise_window(older);
97
1
    app.route_reply(the_only_request(&app), &account_options());
98
1
    assert_eq!(origin_window(&app), Some(asked));
99
1
    assert_eq!(app.front_window(), Some(asked));
100
1
}
101

            
102
#[tokio::test]
103
1
async fn a_balance_reply_prints_in_the_console_that_asked() {
104
1
    let mut app = crate::test_support::app();
105
1
    app.attach_echo();
106
1
    let older = app.window_of(ViewId::Console);
107
1
    let asked = app.open_extra(ViewId::Console);
108
1
    app.show_account_balance("Assets:Cash");
109
1
    app.raise_window(older);
110
1
    app.route_reply(the_only_request(&app), &account_options());
111
2
    let printed = |window| {
112
2
        app.win::<ConsoleState>(window).is_some_and(|console| {
113
2
            console
114
2
                .scrollback
115
2
                .iter()
116
1
                .any(|l| l.contains("get-balances"))
117
2
        })
118
2
    };
119
1
    assert!(printed(asked));
120
1
    assert!(!printed(older));
121
1
}
122

            
123
#[tokio::test]
124
1
async fn a_report_lookup_runs_the_report_in_the_window_that_asked() {
125
1
    let mut app = crate::test_support::app();
126
1
    app.attach_echo();
127
1
    let older = app.window_of(ViewId::Reports);
128
1
    let asked = app.open_extra(ViewId::Reports);
129
1
    run(&mut app, "reports balance target=usd");
130
1
    app.raise_window(older);
131
    let options =
132
1
        r#"(:id 1 :value "((:id \"550e8400-e29b-41d4-a716-446655440001\" :label \"usd\"))")"#;
133
1
    app.route_reply(the_only_request(&app), options);
134
1
    assert!(loading(&app, asked));
135
1
    assert!(!loading(&app, older));
136
1
}
137

            
138
#[tokio::test]
139
1
async fn a_closed_dialog_leaves_no_origin_for_the_next_push() {
140
1
    let mut app = crate::test_support::app();
141
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
142
1
    assert!(origin_window(&app).is_some());
143
1
    app.overlays.pop();
144
1
    assert_eq!(app.overlays.origin(), None);
145
1
    app.overlays.push(Modal::Form(Form::tag_create()));
146
1
    assert_eq!(app.dialog_window(), None);
147
1
    assert_eq!(app.status, "no window");
148
1
}
149

            
150
#[tokio::test]
151
1
async fn an_unlocked_form_comes_back_with_its_window() {
152
1
    let mut app = crate::test_support::app();
153
1
    app.attach_echo();
154
1
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
155
1
    let tags = app.window_of(ViewId::Tags);
156
1
    apply(&mut app, Intent::Submit);
157
1
    let request = app.pending_form.as_ref().map(|pending| pending.request_id);
158
1
    let request = request.expect("the form is locked");
159
1
    assert_eq!(app.overlays.origin(), None);
160
1
    let rejected = format!(
161
1
        r#"(:id {request} :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "name" :MESSAGE "required"))))"#
162
    );
163
1
    app.route_reply(request, &rejected);
164
1
    assert_eq!(origin_window(&app), Some(tags));
165
1
    apply(&mut app, Intent::Submit);
166
1
    assert!(app.pending_form.is_some(), "{}", app.status);
167
1
}