1
//! The status line: a fetch error is cleared by a user action or by a success of the view
2
//! that failed; the error of a mutation by a user action only.
3

            
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::{Route, RouteCtx};
9
use crate::view::{Tab, ViewId};
10

            
11
const OK: &str = r#"(:id 7 :value "nil")"#;
12
const FAILED: &str = r#"(:id 7 :error (:code db :message "down"))"#;
13

            
14
12
fn expect(app: &mut App, id: i64, target: ViewId, ctx: RouteCtx) {
15
12
    app.pending_routes.insert(
16
12
        id,
17
12
        Route {
18
12
            target: Some(app.window_of(target)),
19
12
            ctx,
20
12
        },
21
12
    );
22
12
}
23

            
24
3
fn expect_mutation(app: &mut App, id: i64, view: ViewId) {
25
3
    expect(app, id, view, RouteCtx::Mutation { refresh: view });
26
3
}
27

            
28
#[test]
29
1
fn a_success_of_the_same_view_clears_its_fetch_error() {
30
1
    let mut app = crate::test_support::app();
31
1
    let convert = RouteCtx::ConvertQuery {
32
1
        amount_str: "1".into(),
33
1
        from_label: "a".into(),
34
1
        to_label: "b".into(),
35
1
    };
36
1
    expect(&mut app, 7, ViewId::Transactions, convert);
37
1
    app.route_reply(7, FAILED);
38
1
    assert!(!app.status.is_empty());
39
1
    expect(&mut app, 8, ViewId::Transactions, RouteCtx::None);
40
1
    app.route_reply(8, &OK.replace('7', "8"));
41
1
    assert_eq!(app.status, "");
42
1
}
43

            
44
#[test]
45
1
fn no_reply_clears_the_error_of_a_failed_mutation() {
46
1
    for ctx in [
47
1
        RouteCtx::DeleteTransaction { id: "x".into() },
48
1
        RouteCtx::Mutation {
49
1
            refresh: ViewId::Transactions,
50
1
        },
51
1
    ] {
52
2
        let mut app = crate::test_support::app();
53
2
        expect(&mut app, 7, ViewId::Transactions, ctx);
54
2
        app.route_reply(7, FAILED);
55
2
        let shown = app.status.clone();
56
2
        assert!(shown.contains("failed"), "{shown}");
57
2
        expect(&mut app, 8, ViewId::Transactions, RouteCtx::None);
58
2
        app.route_reply(8, &OK.replace('7', "8"));
59
2
        assert_eq!(app.status, shown);
60
    }
61
1
}
62

            
63
#[test]
64
1
fn a_success_of_another_view_keeps_the_error() {
65
1
    let mut app = crate::test_support::app();
66
1
    expect_mutation(&mut app, 7, ViewId::Transactions);
67
1
    app.route_reply(7, FAILED);
68
1
    let shown = app.status.clone();
69
1
    expect(&mut app, 8, ViewId::Commodities, RouteCtx::None);
70
1
    app.route_reply(8, &OK.replace('7', "8"));
71
1
    assert_eq!(app.status, shown);
72
1
}
73

            
74
2
fn expect_dialog_mutation(app: &mut App, id: i64) {
75
2
    app.pending_routes.insert(
76
2
        id,
77
2
        Route {
78
2
            target: None,
79
2
            ctx: RouteCtx::Mutation {
80
2
                refresh: ViewId::Transactions,
81
2
            },
82
2
        },
83
2
    );
84
2
}
85

            
86
#[test]
87
1
fn a_dialog_error_survives_the_success_of_a_view() {
88
1
    let mut app = crate::test_support::app();
89
1
    expect_dialog_mutation(&mut app, 7);
90
1
    app.route_reply(7, FAILED);
91
1
    let shown = app.status.clone();
92
1
    assert!(shown.starts_with("mutation failed"), "{shown}");
93
1
    expect(&mut app, 8, ViewId::Transactions, RouteCtx::None);
94
1
    app.route_reply(8, &OK.replace('7', "8"));
95
1
    assert_eq!(app.status, shown);
96
1
}
97

            
98
#[test]
99
1
fn a_dialog_success_keeps_the_error_of_a_view() {
100
1
    let mut app = crate::test_support::app();
101
1
    expect_mutation(&mut app, 7, ViewId::Transactions);
102
1
    app.route_reply(7, FAILED);
103
1
    let shown = app.status.clone();
104
1
    expect_dialog_mutation(&mut app, 8);
105
1
    app.route_reply(8, &OK.replace('7', "8"));
106
1
    assert_eq!(app.status, shown);
107
1
}
108

            
109
#[test]
110
1
fn a_failed_reply_after_a_later_success_in_one_drain_survives() {
111
1
    let mut app = crate::test_support::app();
112
1
    expect_mutation(&mut app, 5, ViewId::Transactions);
113
1
    expect(&mut app, 6, ViewId::Commodities, RouteCtx::None);
114
1
    app.route_reply(5, &FAILED.replace('7', "5"));
115
1
    app.route_reply(6, &OK.replace('7', "6"));
116
1
    assert!(app.status.starts_with("mutation failed"), "{}", app.status);
117
1
}
118

            
119
#[test]
120
1
fn a_list_action_clears_the_error() {
121
1
    let mut app = crate::test_support::app();
122
1
    app.switch_tab(Tab::Accounts);
123
1
    app.set_status("mutation failed: down");
124
1
    apply(&mut app, Intent::RefreshTab);
125
1
    assert_eq!(app.status, "");
126
1
}
127

            
128
#[test]
129
1
fn submitting_a_form_clears_the_error() {
130
1
    let mut app = crate::test_support::app();
131
1
    app.overlays.push(Modal::Form(Form::config_set()));
132
1
    app.set_status("submit failed: name is required");
133
1
    apply(&mut app, Intent::Submit);
134
1
    assert!(!app.status.starts_with("mutation"));
135
1
    assert_ne!(app.status, "submit failed: name is required");
136
1
}
137

            
138
#[test]
139
1
fn editing_a_form_after_a_failed_submit_keeps_the_error() {
140
1
    let mut app = crate::test_support::app();
141
1
    app.overlays.push(Modal::Form(Form::config_set()));
142
1
    app.set_status("submit failed: name is required");
143
1
    for intent in [
144
1
        Intent::InsertChar('x'),
145
1
        Intent::DeleteBackward,
146
1
        Intent::MoveLeft,
147
1
        Intent::NextWindow,
148
1
        Intent::SelectNext,
149
1
    ] {
150
5
        apply(&mut app, intent);
151
5
        assert_eq!(app.status, "submit failed: name is required", "{intent:?}");
152
    }
153
1
}
154

            
155
#[test]
156
1
fn switching_tabs_clears_the_error() {
157
1
    let mut app = crate::test_support::app();
158
1
    app.set_status("can't edit this transaction: reconciled");
159
1
    app.switch_tab(Tab::Config);
160
1
    assert_eq!(app.status, "");
161
1
}
162

            
163
#[test]
164
1
fn a_palette_command_replaces_the_previous_error() {
165
1
    let mut app = crate::test_support::app();
166
1
    app.set_status("command error: boom");
167
1
    apply(&mut app, Intent::OpenCommandLine);
168
1
    "nonsense"
169
1
        .chars()
170
8
        .for_each(|c| apply(&mut app, Intent::InsertChar(c)));
171
1
    apply(&mut app, Intent::Submit);
172
1
    assert_eq!(app.status, "unknown command: nonsense");
173
1
}