1
//! A mutation reply refreshes every window of its kind, whether or not the window that asked
2
//! is still open.
3

            
4
use super::*;
5
use crate::modal::{ConfirmAction, Modal};
6
use crate::route::{ScriptRoute, TemplateRoute};
7
use crate::tabs::list::{Balances, ListTab};
8

            
9
32
fn ok_wire(id: i64) -> String {
10
32
    format!(r#"(:id {id} :value "new-id")"#)
11
32
}
12

            
13
8
fn failed_wire(id: i64) -> String {
14
8
    format!(r#"(:id {id} :error (:code db :message "boom"))"#)
15
8
}
16

            
17
45
fn loading(app: &App, window: WindowId) -> bool {
18
45
    app.win_by(window).is_some_and(|win| win.view.loading())
19
45
}
20

            
21
29
fn ask(app: &mut App, id: i64, window: WindowId, ctx: RouteCtx) {
22
29
    app.pending_routes.insert(
23
29
        id,
24
29
        Route {
25
29
            target: Some(window),
26
29
            ctx,
27
29
        },
28
29
    );
29
29
}
30

            
31
/// One request of every kind of mutation, with the view it changes.
32
2
fn mutations() -> Vec<(ViewId, fn() -> RouteCtx)> {
33
2
    vec![
34
2
        (ViewId::Tags, || RouteCtx::Mutation {
35
2
            refresh: ViewId::Tags,
36
2
        }),
37
2
        (ViewId::Tags, || RouteCtx::TagCreate),
38
2
        (ViewId::Tags, || RouteCtx::TagDelete {
39
2
            label: "k=v".into(),
40
2
        }),
41
2
        (ViewId::Templates, || {
42
2
            RouteCtx::Template(TemplateRoute::Created)
43
2
        }),
44
2
        (ViewId::Templates, || {
45
2
            RouteCtx::Template(TemplateRoute::Delete { label: "t".into() })
46
2
        }),
47
2
        (ViewId::Scripts, || RouteCtx::Script(ScriptRoute::Created)),
48
2
        (ViewId::Scripts, || {
49
2
            RouteCtx::Script(ScriptRoute::Toggled {
50
2
                label: "s".into(),
51
2
                enabled: true,
52
2
            })
53
2
        }),
54
2
        (ViewId::Scripts, || {
55
2
            RouteCtx::Script(ScriptRoute::Delete { label: "s".into() })
56
2
        }),
57
2
        (ViewId::SshKeys, || RouteCtx::SshKeyAdd),
58
2
        (ViewId::SshKeys, || RouteCtx::SshKeyRemove {
59
2
            fingerprint: "fp".into(),
60
2
        }),
61
2
        (ViewId::Config, || RouteCtx::ConfigSet { key: "k".into() }),
62
2
        (ViewId::Config, || RouteCtx::OptionsSave {
63
2
            changes: vec![("tui-theme".into(), "turbo".into())],
64
2
        }),
65
2
        (ViewId::Transactions, || RouteCtx::DeleteTransaction {
66
2
            id: "x".into(),
67
2
        }),
68
    ]
69
2
}
70

            
71
#[tokio::test]
72
1
async fn a_mutation_refreshes_a_second_window_of_its_kind() {
73
1
    for (kind, ctx) in mutations() {
74
13
        let mut app = crate::test_support::app();
75
13
        app.attach_echo();
76
13
        let first = app.window_of(kind);
77
13
        let second = app.open_extra(kind);
78
13
        ask(&mut app, 900, first, ctx());
79
13
        app.route_reply(900, &ok_wire(900));
80
13
        assert!(loading(&app, first), "{kind:?}: the asking window");
81
13
        assert!(loading(&app, second), "{kind:?}: the second window");
82
1
    }
83
1
}
84

            
85
#[tokio::test]
86
1
async fn a_mutation_refreshes_the_other_window_after_the_asking_one_closed() {
87
1
    for (kind, ctx) in mutations() {
88
13
        let mut app = crate::test_support::app();
89
13
        app.attach_echo();
90
13
        let first = app.window_of(kind);
91
13
        let second = app.open_extra(kind);
92
13
        ask(&mut app, 900, first, ctx());
93
13
        app.close_window(first);
94
13
        app.route_reply(900, &ok_wire(900));
95
13
        assert!(loading(&app, second), "{kind:?}");
96
1
    }
97
1
}
98

            
99
#[tokio::test]
100
1
async fn a_failed_mutation_of_a_closed_window_refreshes_nothing() {
101
1
    let mut app = crate::test_support::app();
102
1
    app.attach_echo();
103
1
    let first = app.window_of(ViewId::Tags);
104
1
    let second = app.open_extra(ViewId::Tags);
105
1
    ask(&mut app, 900, first, RouteCtx::TagCreate);
106
1
    app.close_window(first);
107
1
    app.route_reply(900, &failed_wire(900));
108
1
    assert!(!loading(&app, second));
109
1
    assert!(app.status.is_empty(), "{}", app.status);
110
1
}
111

            
112
#[tokio::test]
113
1
async fn a_closed_window_read_reply_is_still_dropped_whole() {
114
1
    let mut app = crate::test_support::app();
115
1
    app.attach_echo();
116
1
    let first = app.window_of(ViewId::Tags);
117
1
    let second = app.open_extra(ViewId::Tags);
118
1
    ask(&mut app, 900, first, RouteCtx::None);
119
1
    app.close_window(first);
120
1
    app.route_reply(900, &ok_wire(900));
121
1
    assert!(!loading(&app, second));
122
1
    assert!(app.pending_routes.is_empty());
123
1
}
124

            
125
3
fn bulk_of_closed_window(app: &mut App) -> (WindowId, Vec<i64>) {
126
3
    let first = app.window_of(ViewId::Transactions);
127
3
    let second = app.open_extra(ViewId::Transactions);
128
3
    app.open_dialog_in(
129
3
        first,
130
3
        Modal::Confirm {
131
3
            focus: None,
132
3
            prompt: String::new(),
133
3
            action: ConfirmAction::DeleteMarked(vec![]),
134
3
        },
135
3
    );
136
3
    app.submit_delete_marked(vec!["a".into(), "b".into()]);
137
3
    app.close_window(first);
138
3
    let mut ids: Vec<i64> = app
139
3
        .pending_routes
140
3
        .iter()
141
12
        .filter(|(_, route)| matches!(route.ctx, RouteCtx::BulkDelete { .. }))
142
6
        .map(|(id, _)| *id)
143
        .collect();
144
3
    ids.sort_unstable();
145
3
    (second, ids)
146
3
}
147

            
148
#[tokio::test]
149
1
async fn a_bulk_delete_of_a_closed_window_refreshes_after_its_last_reply() {
150
1
    let mut app = crate::test_support::app();
151
1
    app.attach_echo();
152
1
    let (second, ids) = bulk_of_closed_window(&mut app);
153
    let Some(ListTab {
154
        state: Fetch::Loading { id: load },
155
        ..
156
1
    }) = app.win::<ListTab>(second)
157
    else {
158
        panic!("closing the window fronts and loads the next one");
159
    };
160
1
    let load = *load;
161
1
    app.route_reply(load, &failed_wire(load));
162
1
    let &[first_reply, last_reply] = ids.as_slice() else {
163
        panic!("two delete requests expected: {ids:?}");
164
    };
165
1
    app.route_reply(first_reply, &ok_wire(first_reply));
166
1
    assert!(!loading(&app, second), "refreshed before the last reply");
167
1
    app.route_reply(last_reply, &ok_wire(last_reply));
168
1
    assert!(loading(&app, second), "no refresh after the last reply");
169
1
}
170

            
171
#[tokio::test]
172
1
async fn a_transaction_mutation_feeds_the_balances_of_every_accounts_window() {
173
1
    let mut app = crate::test_support::app();
174
1
    app.attach_echo();
175
1
    let first = app.window_of(ViewId::Accounts);
176
1
    let second = app.open_extra(ViewId::Accounts);
177
1
    for window in [first, second] {
178
2
        if let Some(list) = app.win_mut::<ListTab>(window) {
179
2
            list.state = Fetch::Loaded(Vec::new());
180
2
            list.set_balances(Balances::Failed);
181
        }
182
    }
183
1
    let transactions = app.window_of(ViewId::Transactions);
184
1
    ask(
185
1
        &mut app,
186
1
        900,
187
1
        transactions,
188
1
        RouteCtx::Mutation {
189
1
            refresh: ViewId::Transactions,
190
1
        },
191
1
    );
192
1
    app.route_reply(900, &ok_wire(900));
193
1
    for window in [first, second] {
194
2
        let balances = app
195
2
            .win::<ListTab>(window)
196
2
            .is_some_and(ListTab::balances_loading);
197
2
        assert!(balances, "{window:?} was not fed");
198
1
    }
199
1
}
200

            
201
#[tokio::test]
202
1
async fn a_bulk_delete_of_a_closed_window_refreshes_when_only_the_last_delete_failed() {
203
1
    let mut app = crate::test_support::app();
204
1
    app.attach_echo();
205
1
    let (second, ids) = bulk_of_closed_window(&mut app);
206
    let Some(ListTab {
207
        state: Fetch::Loading { id: load },
208
        ..
209
1
    }) = app.win::<ListTab>(second)
210
    else {
211
        panic!("closing the window fronts and loads the next one");
212
    };
213
1
    let load = *load;
214
1
    app.route_reply(load, &failed_wire(load));
215
1
    let &[first_reply, last_reply] = ids.as_slice() else {
216
        panic!("two delete requests expected: {ids:?}");
217
    };
218
1
    app.route_reply(first_reply, &ok_wire(first_reply));
219
1
    app.route_reply(last_reply, &failed_wire(last_reply));
220
1
    assert!(loading(&app, second));
221
1
}
222

            
223
#[tokio::test]
224
1
async fn a_bulk_delete_of_a_closed_window_that_deleted_nothing_does_not_refresh() {
225
1
    let mut app = crate::test_support::app();
226
1
    app.attach_echo();
227
1
    let (second, ids) = bulk_of_closed_window(&mut app);
228
    let Some(ListTab {
229
        state: Fetch::Loading { id: load },
230
        ..
231
1
    }) = app.win::<ListTab>(second)
232
    else {
233
        panic!("closing the window fronts and loads the next one");
234
    };
235
1
    let load = *load;
236
1
    app.route_reply(load, &failed_wire(load));
237
1
    for id in ids {
238
2
        app.route_reply(id, &failed_wire(id));
239
1
    }
240
1
    assert!(!loading(&app, second));
241
1
}
242

            
243
#[tokio::test]
244
1
async fn a_mixed_bulk_delete_refreshes_and_keeps_its_summary() {
245
1
    let mut app = crate::test_support::app();
246
1
    app.attach_echo();
247
1
    let window = app.window_of(ViewId::Transactions);
248
1
    if let Some(list) = app.win_mut::<ListTab>(window) {
249
1
        list.state = Fetch::Loaded(Vec::new());
250
    }
251
1
    app.open_dialog_in(
252
1
        window,
253
1
        Modal::Confirm {
254
1
            focus: None,
255
1
            prompt: String::new(),
256
1
            action: ConfirmAction::DeleteMarked(vec![]),
257
1
        },
258
1
    );
259
1
    app.submit_delete_marked(vec!["a".into(), "b".into()]);
260
1
    let mut ids: Vec<i64> = app.pending_routes.keys().copied().collect();
261
1
    ids.sort_unstable();
262
1
    let &[first, last] = ids.as_slice() else {
263
        panic!("two delete requests expected: {ids:?}");
264
    };
265
1
    app.route_reply(first, &ok_wire(first));
266
1
    app.route_reply(last, &failed_wire(last));
267
1
    let summary = app.status.clone();
268
1
    assert!(summary.starts_with("deleted 1 of 2"), "{summary}");
269
    let Some(ListTab {
270
        state: Fetch::Loading { id: page },
271
        ..
272
1
    }) = app.win::<ListTab>(window)
273
    else {
274
        panic!("the list is fetched again");
275
    };
276
1
    let page = *page;
277
1
    app.route_reply(
278
1
        page,
279
1
        &format!("(:id {page} :value (:ROWS () :HAS-MORE nil :TOTAL 0))"),
280
1
    );
281
1
    assert_eq!(app.status, summary);
282
1
}