1
//! The transaction-create form against its shared descriptor.
2

            
3
use cli_core::render::FieldError;
4
use cli_core::transaction::submit_call;
5
use forms::TRANSACTION_CREATE;
6

            
7
use crate::form::{Field, Form};
8
use crate::widgets::splits::{COL_FROM_COMM, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE};
9
use crate::widgets::{DateWidget, FocusedSubWidget, SelectOption, SplitsWidget, Widget};
10

            
11
16
fn opt(id: &str, label: &str) -> SelectOption {
12
16
    SelectOption {
13
16
        id: id.to_string(),
14
16
        label: label.to_string(),
15
16
    }
16
16
}
17

            
18
7
fn error(field: &str, message: &str) -> FieldError {
19
7
    FieldError {
20
7
        field: field.to_string(),
21
7
        message: message.to_string(),
22
7
    }
23
7
}
24

            
25
5
fn splits_mut(form: &mut Form) -> &mut SplitsWidget {
26
20
    match form.fields.iter_mut().find(|f| f.key == Some("splits")) {
27
        Some(Field {
28
            widget: Widget::Splits(sw),
29
            ..
30
5
        }) => sw,
31
        other => panic!("splits field must hold Splits, got {other:?}"),
32
    }
33
5
}
34

            
35
12
fn type_in(sw: &mut SplitsWidget, col: usize, text: &str) {
36
12
    sw.col_focus = col;
37
12
    if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
38
24
        text.chars().for_each(|c| aw.insert_char(c));
39
    }
40
12
}
41

            
42
4
fn pick_eur_to_commodity(sw: &mut SplitsWidget) {
43
4
    sw.col_focus = COL_TO_COMM;
44
4
    if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
45
4
        select.next();
46
4
        select.next();
47
    }
48
4
}
49

            
50
8
fn pick_usd_from_commodity(sw: &mut SplitsWidget) {
51
8
    sw.col_focus = COL_FROM_COMM;
52
8
    if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
53
8
        select.next();
54
    }
55
8
}
56

            
57
4
fn two_row_form() -> Form {
58
4
    let mut form = Form::transaction_create();
59
4
    form.fields[0].widget = Widget::Date(DateWidget::with_value("2024-06-15T10:00"));
60
4
    if let Widget::Text(note) = &mut form.fields[1].widget {
61
20
        "lunch".chars().for_each(|c| note.insert_char(c));
62
    }
63
4
    let sw = splits_mut(&mut form);
64
4
    sw.set_account_options(vec![opt("acc-1", "Cash"), opt("acc-2", "Food")]);
65
4
    sw.set_commodity_options(vec![opt("c-1", "USD"), opt("c-2", "EUR")]);
66
4
    pick_usd_from_commodity(sw);
67
4
    type_in(sw, COL_VALUE, "50");
68
4
    sw.add_row();
69
4
    pick_usd_from_commodity(sw);
70
4
    pick_eur_to_commodity(sw);
71
4
    type_in(sw, COL_VALUE, "50");
72
4
    type_in(sw, COL_TO_AMOUNT, "45");
73
4
    form
74
4
}
75

            
76
#[test]
77
1
fn fields_are_the_descriptor_fields() {
78
1
    let form = Form::transaction_create();
79
4
    let labels: Vec<&str> = form.fields.iter().map(|f| f.label).collect();
80
1
    let expected: Vec<&str> = TRANSACTION_CREATE
81
1
        .fields
82
1
        .iter()
83
5
        .filter(|f| !f.is_nested())
84
4
        .map(|f| f.label)
85
        .collect();
86
1
    assert_eq!(labels, expected);
87
1
}
88

            
89
#[test]
90
1
fn submit_payload_of_a_two_row_cross_currency_entry_is_exact() {
91
1
    let values = two_row_form().values();
92
1
    let expected = concat!(
93
        r#"(submit-form "transaction-create" "(:date \"2024-06-15T10:00\" :note \"lunch\" :splits ("#,
94
        r#"(:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :value \"50\") "#,
95
        r#"(:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :to-commodity \"c-2\" :value \"50\" :to-amount \"45\")"#,
96
        r#"))")"#
97
    );
98
1
    assert_eq!(submit_call(&TRANSACTION_CREATE, &values), expected);
99
1
}
100

            
101
#[test]
102
1
fn empty_cells_are_left_out_of_the_values() {
103
1
    let values = Form::transaction_create().values();
104
1
    let row = &values.rows("splits").expect("splits rows")[0];
105
1
    assert!(!row.contains_key("value"));
106
1
    assert!(!row.contains_key("to-amount"));
107
1
}
108

            
109
#[test]
110
1
fn rejections_are_listed_with_labels_and_focus_the_first_cell() {
111
1
    let mut form = two_row_form();
112
1
    form.show_errors(&[
113
1
        error("splits.1.to-amount", "is required"),
114
1
        error("note", "too long"),
115
1
        error("splits.0", "bad hidden data"),
116
1
        error("", "unreadable payload"),
117
1
        error("elsewhere", "kept raw"),
118
1
    ]);
119
1
    assert_eq!(
120
1
        form.errors,
121
1
        [
122
1
            "Row 2 \u{b7} To amount: is required",
123
1
            "Note: too long",
124
1
            "Row 1: bad hidden data",
125
1
            "unreadable payload",
126
1
            "elsewhere: kept raw",
127
1
        ]
128
    );
129
1
    assert_eq!(form.focus, 3);
130
1
    let sw = splits_mut(&mut form);
131
1
    assert_eq!((sw.row_focus, sw.col_focus), (1, COL_TO_AMOUNT));
132
1
}
133

            
134
#[test]
135
1
fn a_field_rejection_focuses_the_field() {
136
1
    let mut form = two_row_form();
137
1
    form.focus = 3;
138
1
    form.show_errors(&[error("date", "invalid date")]);
139
1
    assert_eq!(form.errors, ["Date: invalid date"]);
140
1
    assert_eq!(form.focus, 0);
141
1
}
142

            
143
#[test]
144
1
fn a_form_level_rejection_keeps_the_focus() {
145
1
    let mut form = two_row_form();
146
1
    form.focus = 1;
147
1
    form.show_errors(&[error("", "unreadable payload")]);
148
1
    assert_eq!(form.errors, ["unreadable payload"]);
149
1
    assert_eq!(form.focus, 1);
150
1
}