1
//! A late reply never opens, fills or switches over a dialog that is already open.
2

            
3
use super::edit::options_wire;
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::form::Form;
7
use crate::modal::Modal;
8
use crate::route::{FormOptionsSource, RouteCtx};
9
use crate::tabs::fetch::Fetch;
10
use crate::tabs::nms_eval::double::Sent;
11
use crate::view::{Tab, ViewId};
12
use crate::widgets::Widget;
13

            
14
const CASH: &str = "550e8400-e29b-41d4-a716-446655440081";
15
const BANK: &str = "550e8400-e29b-41d4-a716-446655440082";
16
const USD: &str = "550e8400-e29b-41d4-a716-446655440083";
17
const DROPPED: &str = "another dialog is open";
18

            
19
5
fn connected() -> (App, Sent) {
20
5
    let mut app = crate::test_support::app();
21
5
    let sent = app.attach_echo();
22
5
    (app, sent)
23
5
}
24

            
25
7
fn run(app: &mut App, command: &str) {
26
7
    apply(app, Intent::OpenCommandLine);
27
7
    command
28
7
        .chars()
29
190
        .for_each(|c| apply(app, Intent::InsertChar(c)));
30
7
    apply(app, Intent::Submit);
31
7
}
32

            
33
8
fn pending_id(app: &App, wanted: fn(&RouteCtx) -> bool) -> i64 {
34
8
    app.pending_routes
35
8
        .iter()
36
10
        .find_map(|(id, route)| wanted(&route.ctx).then_some(*id))
37
        .unwrap_or_default()
38
8
}
39

            
40
5
fn lookup_id(app: &App) -> i64 {
41
7
    pending_id(app, |ctx| matches!(ctx, RouteCtx::Lookup { .. }))
42
5
}
43

            
44
2
fn account_options_id(app: &App) -> i64 {
45
2
    pending_id(app, |ctx| {
46
2
        matches!(
47
2
            ctx,
48
            RouteCtx::FormOptions {
49
                source: FormOptionsSource::FormAccounts,
50
                ..
51
            }
52
        )
53
2
    })
54
2
}
55

            
56
2
fn transaction_form(app: &App) -> &Form {
57
2
    match app.overlays.top() {
58
2
        Some(Modal::Form(form)) if form.is_transaction() => form,
59
        other => panic!("the transaction form expected on top, got {other:?}"),
60
    }
61
2
}
62

            
63
2
fn split_account_options(app: &App) -> usize {
64
2
    transaction_form(app)
65
2
        .fields
66
2
        .iter()
67
8
        .find_map(|field| match &field.widget {
68
2
            Widget::Splits(splits) => Some(splits.rows()[0].from.options().len()),
69
6
            _ => None,
70
8
        })
71
        .unwrap_or_default()
72
2
}
73

            
74
2
fn deliver_accounts_to_the_open_form(app: &mut App) {
75
2
    let id = account_options_id(app);
76
2
    assert!(id > 0, "the open form fetched its account options");
77
2
    app.route_reply(
78
2
        id,
79
2
        &options_wire(&[(CASH, "Assets:Cash"), (BANK, "Assets:Bank")]),
80
2
    );
81
2
}
82

            
83
#[tokio::test]
84
1
async fn a_merge_lookup_under_a_new_transaction_form_is_dropped() {
85
1
    let (mut app, _) = connected();
86
1
    run(&mut app, "account merge from=Assets:Cash");
87
1
    let lookup = lookup_id(&app);
88
1
    run(&mut app, "transaction create");
89
1
    app.route_reply(lookup, &options_wire(&[(CASH, "Assets:Cash")]));
90
1
    assert!(app.status.contains(DROPPED), "{}", app.status);
91
1
    assert!(app.status.starts_with("merge dropped"), "{}", app.status);
92
1
    deliver_accounts_to_the_open_form(&mut app);
93
1
    assert_eq!(split_account_options(&app), 3, "(none) + both accounts");
94
1
    app.overlays.pop();
95
1
    assert!(app.overlays.is_empty(), "no merge form opened");
96
1
}
97

            
98
#[tokio::test]
99
1
async fn a_late_report_lookup_keeps_the_open_forms_options() {
100
1
    let (mut app, _) = connected();
101
1
    run(&mut app, "reports activity target=usd");
102
1
    let lookup = lookup_id(&app);
103
1
    run(&mut app, "transaction create");
104
1
    app.route_reply(lookup, &options_wire(&[(USD, "USD")]));
105
1
    assert!(app.status.starts_with("report dropped"), "{}", app.status);
106
1
    assert!(matches!(app.reports().state, Fetch::Idle));
107
1
    deliver_accounts_to_the_open_form(&mut app);
108
1
    assert_eq!(split_account_options(&app), 3);
109
1
}
110

            
111
#[tokio::test]
112
1
async fn a_filter_reply_under_help_applies_the_filter_without_switching_tab() {
113
1
    let (mut app, _) = connected();
114
1
    app.switch_tab(Tab::Accounts);
115
1
    run(&mut app, "transaction list account=Assets:Cash");
116
1
    let lookup = lookup_id(&app);
117
1
    app.route_reply(lookup, &options_wire(&[(CASH, "Assets:Cash")]));
118
1
    let query = pending_id(&app, |ctx| {
119
1
        matches!(ctx, RouteCtx::TransactionFilter { .. })
120
1
    });
121
1
    assert!(query > 0, "the filter query is in flight");
122
1
    crate::test_support::open_overlay(&mut app);
123
1
    app.route_reply(query, r#"(:id 1 :value (:ROWS () :HAS-MORE nil :TOTAL 0))"#);
124
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
125
1
    assert_eq!(app.transactions().filter().text("account"), Some(CASH));
126
1
}
127

            
128
#[tokio::test]
129
1
async fn a_balance_lookup_under_help_prints_without_switching_tab() {
130
1
    let (mut app, _) = connected();
131
1
    app.switch_tab(Tab::Accounts);
132
1
    run(&mut app, "account balance account=Assets:Cash");
133
1
    let lookup = lookup_id(&app);
134
1
    crate::test_support::open_overlay(&mut app);
135
1
    app.route_reply(lookup, &options_wire(&[(CASH, "Assets:Cash")]));
136
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
137
1
    assert!(
138
1
        app.pending_routes
139
1
            .values()
140
2
            .any(|route| route.target == Some(app.window_of(ViewId::Console))),
141
1
        "the balances were requested"
142
1
    );
143
1
}
144

            
145
#[tokio::test]
146
1
async fn a_report_run_lookup_under_help_runs_without_switching_tab() {
147
1
    let (mut app, _) = connected();
148
1
    app.switch_tab(Tab::Accounts);
149
1
    run(&mut app, "reports balance target=usd");
150
1
    let lookup = lookup_id(&app);
151
1
    crate::test_support::open_overlay(&mut app);
152
1
    app.route_reply(lookup, &options_wire(&[(USD, "USD")]));
153
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
154
1
    assert!(matches!(app.reports().state, Fetch::Loading { .. }));
155
1
}