1
//! The list windows own the ids of their requests: a reply is applied only by the window that awaits it.
2

            
3
use forms::FormValues;
4

            
5
use crate::modal::Modal;
6
use crate::route::{Route, RouteCtx, ScriptRoute, TemplateRoute};
7
use crate::tabs::config::ConfigTab;
8
use crate::tabs::fetch::Fetch;
9
use crate::tabs::reports::ReportKind;
10
use crate::tabs::scripts::ScriptsTab;
11
use crate::tabs::seq::Seq;
12
use crate::tabs::ssh_keys::SshKeysTab;
13
use crate::tabs::tags::TagsTab;
14
use crate::tabs::templates::TemplatesTab;
15
use crate::view::ViewId;
16

            
17
const EMPTY_BALANCES: &str =
18
    "(:id 1 :value \"(:balance-report :periods ((:label nil :roots ())))\")";
19

            
20
#[test]
21
1
fn a_balances_reply_is_applied_only_when_the_accounts_window_awaits_it() {
22
1
    let mut app = crate::test_support::app();
23
1
    let first = app.accounts_mut().balances_seq.advance();
24
1
    let second = app.accounts_mut().balances_seq.advance();
25
1
    assert!(app.accounts().balances_loading());
26
1
    app.deliver_account_balances(
27
1
        app.window_of(crate::view::ViewId::Accounts),
28
1
        first,
29
1
        EMPTY_BALANCES,
30
1
    );
31
1
    assert!(app.accounts().balances_loading());
32
1
    app.deliver_account_balances(
33
1
        app.window_of(crate::view::ViewId::Accounts),
34
1
        second,
35
1
        EMPTY_BALANCES,
36
1
    );
37
1
    assert!(!app.accounts().balances_loading());
38
1
}
39

            
40
#[test]
41
1
fn a_filter_reply_is_applied_only_when_the_transactions_window_awaits_it() {
42
1
    let mut app = crate::test_support::app();
43
1
    let mut filter = FormValues::default();
44
1
    filter.set("account", "a1");
45
1
    let first = app.transactions_mut().filter_seq.advance();
46
1
    let second = app.transactions_mut().filter_seq.advance();
47
1
    let wire = "(:id 1 :value nil)";
48
1
    app.deliver_filter_reply(
49
1
        app.window_of(crate::view::ViewId::Transactions),
50
1
        first,
51
1
        &filter,
52
1
        "stale".into(),
53
1
        wire,
54
1
    );
55
1
    assert_eq!(app.transactions().filter().text("account"), None);
56
1
    app.deliver_filter_reply(
57
1
        app.window_of(crate::view::ViewId::Transactions),
58
1
        second,
59
1
        &filter,
60
1
        "fresh".into(),
61
1
        wire,
62
1
    );
63
1
    assert_eq!(app.transactions().filter().text("account"), Some("a1"));
64
1
}
65

            
66
const NIL_REPLY: &str = "(:id 1 :value nil)";
67

            
68
14
fn reply(app: &mut crate::app::App, view: ViewId, ctx: RouteCtx, wire: &str) {
69
14
    app.deliver_reply(
70
14
        Route {
71
14
            target: Some(app.window_of(view)),
72
14
            ctx,
73
14
        },
74
14
        wire,
75
14
    );
76
14
}
77

            
78
macro_rules! list_window_ownership {
79
    ($name:ident, $tab:ty, $view:expr, $ctx:expr) => {
80
        #[test]
81
5
        fn $name() {
82
5
            let mut app = crate::test_support::app();
83
5
            let window = app.window_of($view);
84
            let (first, second) = {
85
5
                let seq = &mut app.win_mut::<$tab>(window).expect("tab").seq;
86
5
                (seq.advance(), seq.advance())
87
            };
88
5
            reply(&mut app, $view, $ctx(first), NIL_REPLY);
89
5
            assert!(app.win::<$tab>(window).expect("tab").is_idle());
90
5
            reply(&mut app, $view, $ctx(second), NIL_REPLY);
91
5
            assert!(!app.win::<$tab>(window).expect("tab").is_idle());
92
5
        }
93
    };
94
}
95

            
96
list_window_ownership!(
97
    a_tags_reply_is_applied_only_when_the_tags_window_awaits_it,
98
    TagsTab,
99
    ViewId::Tags,
100
2
    |seq| RouteCtx::Tags { seq }
101
);
102
list_window_ownership!(
103
    an_ssh_keys_reply_is_applied_only_when_the_keys_window_awaits_it,
104
    SshKeysTab,
105
    ViewId::SshKeys,
106
2
    |seq| RouteCtx::SshKeys { seq }
107
);
108
list_window_ownership!(
109
    a_config_reply_is_applied_only_when_the_config_window_awaits_it,
110
    ConfigTab,
111
    ViewId::Config,
112
2
    |seq| RouteCtx::Config { seq }
113
);
114
list_window_ownership!(
115
    a_templates_reply_is_applied_only_when_the_templates_window_awaits_it,
116
    TemplatesTab,
117
    ViewId::Templates,
118
2
    |seq| RouteCtx::Template(TemplateRoute::List { seq })
119
);
120
list_window_ownership!(
121
    a_scripts_reply_is_applied_only_when_the_scripts_window_awaits_it,
122
    ScriptsTab,
123
    ViewId::Scripts,
124
2
    |seq| RouteCtx::Script(ScriptRoute::List { seq })
125
);
126

            
127
2
fn two_ids(seq: &mut Seq) -> (u64, u64) {
128
2
    (seq.advance(), seq.advance())
129
2
}
130

            
131
#[test]
132
1
fn a_report_reply_is_applied_only_when_the_reports_window_awaits_it() {
133
1
    let mut app = crate::test_support::app();
134
1
    let (first, second) = two_ids(&mut app.reports_mut().run_seq);
135
    let ctx = |seq| RouteCtx::Reports {
136
2
        kind: ReportKind::Balance,
137
2
        seq,
138
2
        values: FormValues::default(),
139
2
    };
140
1
    reply(&mut app, ViewId::Reports, ctx(first), NIL_REPLY);
141
1
    assert!(matches!(app.reports().state, Fetch::Idle));
142
1
    reply(&mut app, ViewId::Reports, ctx(second), NIL_REPLY);
143
1
    assert!(!matches!(app.reports().state, Fetch::Idle));
144
1
}
145

            
146
#[test]
147
1
fn a_csv_reply_is_applied_only_when_the_reports_window_awaits_it() {
148
1
    let mut app = crate::test_support::app();
149
1
    let (first, second) = two_ids(&mut app.reports_mut().csv_seq);
150
1
    let wire = "(:id 1 :value \"a,b\")";
151
    let ctx = |seq| RouteCtx::ReportCsv {
152
2
        seq,
153
2
        file_name: "r.csv".into(),
154
2
    };
155
1
    reply(&mut app, ViewId::Reports, ctx(first), wire);
156
1
    assert!(!matches!(app.overlays.top(), Some(Modal::Csv(_))));
157
1
    reply(&mut app, ViewId::Reports, ctx(second), wire);
158
1
    assert!(matches!(app.overlays.top(), Some(Modal::Csv(_))));
159
1
}