1
//! Account and commodity create through `submit-form`: payload, field errors,
2
//! success.
3

            
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::form::{Form, FormKind};
7
use crate::modal::Modal;
8
use crate::tabs::fetch::Fetch;
9
use crate::tabs::nms_eval::double::Sent;
10
use crate::widgets::{Editor, SelectOption, Widget};
11
use cli_core::render::ListRow;
12

            
13
const PARENT: &str = "550e8400-e29b-41d4-a716-446655440001";
14

            
15
11
fn typed(form: &mut Form, field: usize, text: &str) {
16
11
    if let Widget::Text(editor) = &mut form.fields[field].widget {
17
11
        *editor = Editor::with_buffer(text.to_string());
18
    }
19
11
}
20

            
21
5
fn account_form() -> Form {
22
5
    let mut form = Form::account_create();
23
5
    typed(&mut form, 0, "Cash");
24
5
    if let Widget::Select(select) = &mut form.fields[1].widget {
25
5
        select.set_options(vec![
26
5
            SelectOption {
27
5
                id: String::new(),
28
5
                label: "(none)".to_string(),
29
5
            },
30
5
            SelectOption {
31
5
                id: PARENT.to_string(),
32
5
                label: "Assets".to_string(),
33
5
            },
34
5
        ]);
35
5
        select.next();
36
    }
37
5
    form
38
5
}
39

            
40
3
fn commodity_form() -> Form {
41
3
    let mut form = Form::commodity_create();
42
3
    typed(&mut form, 0, "EUR");
43
3
    typed(&mut form, 1, "Euro");
44
3
    form
45
3
}
46

            
47
8
async fn submitted(form: Form) -> (App, Sent) {
48
8
    let mut app = crate::test_support::app();
49
8
    let sent = app.attach_echo();
50
8
    app.open_dialog_on_front(Modal::Form(form));
51
8
    apply(&mut app, Intent::Submit);
52
8
    tokio::task::yield_now().await;
53
8
    (app, sent)
54
8
}
55

            
56
3
fn echoed(app: &mut App, sent: &Sent) -> String {
57
3
    app.drain_eval();
58
3
    sent.frames().join("\n")
59
3
}
60

            
61
2
fn loaded(app: &mut App, tab: crate::view::Tab) {
62
2
    let rows = vec![ListRow {
63
2
        id: None,
64
2
        cells: vec!["old".into()],
65
2
    }];
66
2
    match tab {
67
1
        crate::view::Tab::Accounts => app.accounts_mut().state = Fetch::Loaded(rows),
68
1
        _ => app.commodities_mut().state = Fetch::Loaded(rows),
69
    }
70
2
}
71

            
72
#[tokio::test]
73
1
async fn commodity_create_dispatches_submit_form() {
74
1
    let (mut app, sent) = submitted(commodity_form()).await;
75
1
    assert!(app.pending_form.is_some());
76
1
    let frame = echoed(&mut app, &sent);
77
1
    assert!(
78
1
        frame.contains(r#"(submit-form "commodity-create" "(:symbol \"EUR\" :name \"Euro\")")"#),
79
1
        "frame: {frame}"
80
1
    );
81
1
}
82

            
83
#[tokio::test]
84
1
async fn account_create_dispatches_submit_form_with_the_parent() {
85
1
    let (mut app, sent) = submitted(account_form()).await;
86
1
    let frame = echoed(&mut app, &sent);
87
1
    assert!(
88
1
        frame.contains(&format!(
89
1
            r#"(submit-form "account-create" "(:name \"Cash\" :parent \"{PARENT}\")")"#
90
1
        )),
91
1
        "frame: {frame}"
92
1
    );
93
1
}
94

            
95
#[tokio::test]
96
1
async fn a_none_parent_is_sent_empty() {
97
1
    let mut form = account_form();
98
1
    if let Widget::Select(select) = &mut form.fields[1].widget {
99
1
        select.prev();
100
    }
101
1
    let (mut app, sent) = submitted(form).await;
102
1
    let frame = echoed(&mut app, &sent);
103
1
    assert!(
104
1
        frame.contains(r#"(:name \"Cash\" :parent \"\")"#),
105
1
        "frame: {frame}"
106
1
    );
107
1
}
108

            
109
#[tokio::test]
110
1
async fn a_server_field_error_marks_the_commodity_field() {
111
1
    let (mut app, _) = submitted(commodity_form()).await;
112
1
    app.route_reply(
113
1
        1,
114
1
        r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "name" :MESSAGE "too long"))))"#,
115
1
    );
116
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
117
        panic!("form must come back");
118
    };
119
1
    assert_eq!(form.kind, FormKind::CommodityCreate);
120
1
    assert_eq!(form.focus, 1);
121
1
    assert_eq!(form.errors, vec!["Name: too long"]);
122
1
}
123

            
124
#[tokio::test]
125
1
async fn a_server_field_error_marks_the_account_field() {
126
1
    let (mut app, _) = submitted(account_form()).await;
127
1
    app.route_reply(
128
1
        1,
129
1
        r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "name" :MESSAGE "required"))))"#,
130
1
    );
131
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
132
        panic!("form must come back");
133
    };
134
1
    assert_eq!(form.focus, 0);
135
1
    assert_eq!(form.errors, vec!["Name: required"]);
136
1
}
137

            
138
#[tokio::test]
139
1
async fn success_closes_the_commodity_form_and_refreshes() {
140
1
    let (mut app, _) = submitted(commodity_form()).await;
141
1
    loaded(&mut app, crate::view::Tab::Commodities);
142
1
    app.route_reply(1, r#"(:id 1 :value "id")"#);
143
1
    assert!(app.pending_form.is_none());
144
1
    assert!(app.overlays.is_empty());
145
1
    assert!(
146
1
        !matches!(app.commodities().state, Fetch::Loaded(_)),
147
1
        "list reloads"
148
1
    );
149
1
}
150

            
151
#[tokio::test]
152
1
async fn success_closes_the_account_form_and_refreshes() {
153
1
    let (mut app, _) = submitted(account_form()).await;
154
1
    loaded(&mut app, crate::view::Tab::Accounts);
155
1
    app.route_reply(1, r#"(:id 1 :value "id")"#);
156
1
    assert!(app.pending_form.is_none());
157
1
    assert!(app.overlays.is_empty());
158
1
    assert!(
159
1
        !matches!(app.accounts().state, Fetch::Loaded(_)),
160
        "list reloads"
161
    );
162
1
    assert!(app.status.is_empty(), "{}", app.status);
163
1
}
164

            
165
#[tokio::test]
166
1
async fn reply_notices_show_in_the_status_on_success() {
167
1
    let (mut app, _) = submitted(account_form()).await;
168
1
    app.route_reply(
169
1
        1,
170
1
        r#"(:id 1 :value "id" :notices ("Another account named 'Cash' already exists under the same parent"))"#,
171
1
    );
172
1
    assert!(app.overlays.is_empty());
173
1
    assert_eq!(
174
1
        app.status,
175
1
        "Another account named 'Cash' already exists under the same parent"
176
1
    );
177
1
}