1
//! Convert and config-set through `submit-form`: payload, result, field errors.
2

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

            
12
const USD: &str = "550e8400-e29b-41d4-a716-446655440001";
13
const EUR: &str = "550e8400-e29b-41d4-a716-446655440002";
14

            
15
10
fn pick(form: &mut Form, field: usize, id: &str, label: &str) {
16
10
    if let Widget::Select(select) = &mut form.fields[field].widget {
17
10
        select.set_options(vec![SelectOption {
18
10
            id: id.to_string(),
19
10
            label: label.to_string(),
20
10
        }]);
21
    }
22
10
}
23

            
24
5
fn convert_form() -> Form {
25
5
    let mut form = Form::commodity_convert();
26
5
    form.fields[0].widget.set_text("9/2");
27
5
    pick(&mut form, 1, USD, "USD");
28
5
    pick(&mut form, 2, EUR, "EUR");
29
5
    form
30
5
}
31

            
32
7
async fn submitted(form: Form) -> (App, Sent) {
33
7
    let mut app = crate::test_support::app();
34
7
    let sent = app.attach_echo();
35
7
    app.config_mut().state = Fetch::Loaded(());
36
7
    app.commodities_mut().state = Fetch::Loaded(Vec::new());
37
7
    let kind = match form.kind {
38
3
        FormKind::ConfigSet => ViewId::Config,
39
4
        _ => ViewId::Commodities,
40
    };
41
7
    app.open_dialog_for(kind, Modal::Form(form));
42
7
    apply(&mut app, Intent::Submit);
43
7
    tokio::task::yield_now().await;
44
7
    (app, sent)
45
7
}
46

            
47
2
fn echoed(app: &mut App, sent: &Sent) -> String {
48
2
    app.drain_eval();
49
2
    sent.frames().join("\n")
50
2
}
51

            
52
#[tokio::test]
53
1
async fn convert_dispatches_submit_form() {
54
1
    let (mut app, sent) = submitted(convert_form()).await;
55
1
    assert!(app.pending_form.is_some());
56
1
    let frame = echoed(&mut app, &sent);
57
1
    assert!(
58
1
        frame.contains(&format!(
59
1
            r#"(submit-form "convert" "(:amount \"9/2\" :from \"{USD}\" :to \"{EUR}\")")"#
60
1
        )),
61
1
        "frame: {frame}"
62
1
    );
63
1
}
64

            
65
#[tokio::test]
66
1
async fn the_ratio_reply_closes_the_form_and_shows_the_result() {
67
1
    let (mut app, _) = submitted(convert_form()).await;
68
1
    app.route_reply(1, "(:id 1 :value 9/10)");
69
1
    assert!(app.pending_form.is_none());
70
1
    assert!(app.overlays.is_empty());
71
1
    assert_eq!(app.status, "9/2 USD = 9/10 EUR");
72
1
}
73

            
74
#[tokio::test]
75
1
async fn a_server_field_error_marks_the_convert_field() {
76
1
    let (mut app, _) = submitted(convert_form()).await;
77
1
    app.route_reply(
78
1
        1,
79
1
        r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "to" :MESSAGE "must differ from From"))))"#,
80
1
    );
81
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
82
        panic!("form must come back");
83
    };
84
1
    assert_eq!(form.kind, FormKind::CommodityConvert);
85
1
    assert_eq!(form.focus, 2);
86
1
    assert_eq!(form.errors, vec!["To: must differ from From"]);
87
1
}
88

            
89
#[tokio::test]
90
1
async fn a_missing_price_comes_back_as_a_form_error() {
91
1
    let (mut app, _) = submitted(convert_form()).await;
92
1
    app.route_reply(
93
1
        1,
94
1
        r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "" :MESSAGE "no price USD to EUR"))))"#,
95
1
    );
96
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
97
        panic!("form must come back");
98
    };
99
1
    assert_eq!(form.errors, vec!["no price USD to EUR"]);
100
1
    assert!(app.status.contains("no price USD to EUR"), "{}", app.status);
101
1
}
102

            
103
#[test]
104
1
fn convert_without_a_console_sets_the_status() {
105
1
    let mut app = crate::test_support::app();
106
1
    app.open_dialog_on_front(Modal::Form(convert_form()));
107
1
    apply(&mut app, Intent::Submit);
108
1
    assert_eq!(app.status, "console not connected");
109
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
110
1
}
111

            
112
3
fn config_form(key: &str, value: &str) -> Form {
113
3
    let mut form = Form::config_set();
114
3
    form.fields[0].widget.set_text(key);
115
3
    form.fields[1].widget.set_text(value);
116
3
    form
117
3
}
118

            
119
#[tokio::test]
120
1
async fn config_set_dispatches_submit_form() {
121
1
    let (mut app, sent) = submitted(config_form("locale", "en")).await;
122
1
    assert!(app.pending_form.is_some());
123
1
    let frame = echoed(&mut app, &sent);
124
1
    assert!(
125
1
        frame.contains(r#"(submit-form "config-set" "(:key \"locale\" :value \"en\")")"#),
126
1
        "frame: {frame}"
127
1
    );
128
1
}
129

            
130
#[tokio::test]
131
1
async fn config_set_success_closes_the_form_and_reloads_the_key() {
132
1
    let (mut app, _) = submitted(config_form("locale", "en")).await;
133
1
    app.route_reply(1, "(:id 1 :value t)");
134
1
    assert!(app.pending_form.is_none());
135
1
    assert!(app.overlays.is_empty());
136
1
    assert!(app.config().any_loading());
137
1
}
138

            
139
#[tokio::test]
140
1
async fn config_set_field_error_marks_the_key() {
141
1
    let (mut app, _) = submitted(config_form("", "en")).await;
142
1
    app.route_reply(
143
1
        1,
144
1
        r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "key" :MESSAGE "required"))))"#,
145
1
    );
146
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
147
        panic!("form must come back");
148
    };
149
1
    assert_eq!(form.kind, FormKind::ConfigSet);
150
1
    assert_eq!(form.focus, 0);
151
1
    assert_eq!(form.errors, vec!["Key: required"]);
152
1
}