1
use forms::{REPORT_CATEGORY, TRANSACTION_CREATE, TRANSACTION_QUERY};
2

            
3
use super::tests::{all_controls, filled};
4
use super::*;
5

            
6
32
fn html_of(form: &'static Form) -> String {
7
32
    render_form(
8
32
        form,
9
32
        &filled(form, Some(1)),
10
32
        &[],
11
32
        "/x",
12
32
        &References::default(),
13
32
    )
14
32
    .unwrap()
15
32
}
16

            
17
78
fn attr_of(html: &str, name: &str, attr: &str) -> Option<String> {
18
78
    all_controls(html)
19
78
        .iter()
20
802
        .find(|c| c.attr("name") == Some(name))
21
78
        .and_then(|c| c.attr(attr).map(str::to_string))
22
78
}
23

            
24
#[test]
25
2
fn tag_cells_name_their_suggestion_endpoint() {
26
2
    let html = html_of(&TRANSACTION_CREATE);
27
2
    for (name, url, by) in [
28
2
        (
29
2
            "tags.0.name",
30
2
            "/api/forms/suggestions/transaction-tag-names",
31
2
            None,
32
2
        ),
33
2
        (
34
2
            "tags.0.value",
35
2
            "/api/forms/suggestions/transaction-tag-values",
36
2
            Some("name"),
37
2
        ),
38
2
        (
39
2
            "split-tags.0.name",
40
2
            "/api/forms/suggestions/split-tag-names",
41
2
            None,
42
2
        ),
43
2
        (
44
2
            "split-tags.0.value",
45
2
            "/api/forms/suggestions/split-tag-values",
46
2
            Some("name"),
47
2
        ),
48
2
    ] {
49
8
        assert_eq!(
50
8
            attr_of(&html, name, "data-suggest").as_deref(),
51
8
            Some(url),
52
            "{name}"
53
        );
54
8
        assert_eq!(
55
8
            attr_of(&html, name, "data-suggest-by").as_deref(),
56
8
            by,
57
            "{name}"
58
        );
59
    }
60
2
}
61

            
62
#[test]
63
2
fn pickers_and_suggest_inputs_suppress_browser_autofill() {
64
2
    let html = html_of(&TRANSACTION_CREATE);
65
2
    for name in [
66
2
        "label:splits.0.from",
67
2
        "label:splits.0.to",
68
2
        "tags.0.name",
69
2
        "tags.0.value",
70
2
        "split-tags.0.name",
71
2
        "split-tags.0.value",
72
2
    ] {
73
12
        assert_eq!(
74
12
            attr_of(&html, name, "autocomplete").as_deref(),
75
12
            Some("off"),
76
            "{name}"
77
        );
78
    }
79
2
}
80

            
81
#[test]
82
2
fn other_cells_carry_no_suggestion_endpoint() {
83
2
    let html = html_of(&TRANSACTION_CREATE);
84
2
    for name in [
85
2
        "note",
86
2
        "tags.0.description",
87
2
        "split-tags.0.row",
88
2
        "splits.0.value",
89
2
        "splits.0.key",
90
2
    ] {
91
10
        let found = all_controls(&html)
92
10
            .into_iter()
93
108
            .find(|c| c.attr("name") == Some(name));
94
10
        assert!(
95
10
            found.is_some_and(|c| c.attr("data-suggest").is_none()),
96
            "{name}"
97
        );
98
    }
99
2
}
100

            
101
#[test]
102
2
fn only_the_from_cells_fill() {
103
2
    let html = html_of(&TRANSACTION_CREATE);
104
10
    let fills = |name: &str| attr_of(&html, name, "data-fills");
105
2
    assert_eq!(
106
2
        fills("label:splits.0.from").as_deref(),
107
2
        Some("from-commodity")
108
    );
109
2
    assert_eq!(
110
2
        fills("label:splits.0.from-commodity").as_deref(),
111
2
        Some("to-commodity")
112
    );
113
2
    assert_eq!(html.matches("data-fills=").count(), 4);
114
2
    for name in [
115
2
        "label:splits.0.to",
116
2
        "label:splits.0.to-commodity",
117
2
        "splits.0.value",
118
2
    ] {
119
6
        assert_eq!(fills(name), None, "{name}");
120
    }
121
2
}
122

            
123
#[test]
124
2
fn select_cells_keep_their_source() {
125
2
    let html = html_of(&TRANSACTION_QUERY);
126
2
    assert_eq!(
127
2
        attr_of(&html, "label:account", "data-source").as_deref(),
128
2
        Some("accounts")
129
    );
130
2
    assert_eq!(attr_of(&html, "label:account", "data-fills"), None);
131
2
    assert_eq!(attr_of(&html, "account", "type").as_deref(), Some("hidden"));
132
2
}
133

            
134
#[test]
135
2
fn the_report_filter_is_a_textarea_in_the_builder_container() {
136
2
    let mut values = FormValues::default();
137
2
    values.set("filter", "(and)");
138
2
    let html = render_form(
139
2
        &forms::REPORT_BALANCE,
140
2
        &values,
141
2
        &[],
142
2
        "/x",
143
2
        &References::default(),
144
2
    )
145
2
    .unwrap();
146

            
147
2
    assert!(html.contains("tag-filter-container"), "{html}");
148
2
    assert!(html.contains("tag-filter-visual"), "{html}");
149
2
    assert!(
150
2
        html.contains("name=\"filter\"\n    class=\"tag-filter-sexpr\""),
151
        "{html}"
152
    );
153
2
    assert!(html.contains(">\n(and)</textarea>"), "{html}");
154
2
}
155

            
156
#[test]
157
2
fn report_and_list_dates_are_whole_days_and_transaction_dates_stay_instants() {
158
2
    let report = html_of(&forms::REPORT_BALANCE);
159
2
    let list = html_of(&forms::TRANSACTION_QUERY);
160
2
    let transaction = html_of(&TRANSACTION_CREATE);
161

            
162
2
    for name in ["from", "to"] {
163
4
        assert!(
164
4
            list.contains(&format!("type=\"date\" name=\"{name}\"")),
165
            "{list}"
166
        );
167
    }
168
2
    assert!(!list.contains("datetime-local"), "{list}");
169
2
    assert!(report.contains("type=\"date\" name=\"as-of\""), "{report}");
170
2
    assert!(!report.contains("datetime-local"), "{report}");
171
2
    assert!(transaction.contains("datetime-local"), "{transaction}");
172
2
}
173

            
174
#[test]
175
2
fn entity_edit_tag_cells_suggest_that_entitys_tags() {
176
2
    for (form, owner) in [
177
2
        (&forms::ACCOUNT_EDIT, "account"),
178
2
        (&forms::COMMODITY_EDIT, "commodity"),
179
2
    ] {
180
4
        let html = html_of(form);
181
4
        assert_eq!(
182
4
            attr_of(&html, "tags.0.name", "data-suggest").as_deref(),
183
4
            Some(format!("/api/forms/suggestions/{owner}-tag-names").as_str()),
184
            "{owner}"
185
        );
186
4
        assert_eq!(
187
4
            attr_of(&html, "tags.0.value", "data-suggest").as_deref(),
188
4
            Some(format!("/api/forms/suggestions/{owner}-tag-values").as_str()),
189
            "{owner}"
190
        );
191
4
        assert_eq!(attr_of(&html, "tags.0.name", "data-suggest-by"), None);
192
4
        assert_eq!(
193
4
            attr_of(&html, "tags.0.value", "data-suggest-by").as_deref(),
194
4
            Some("name"),
195
            "{owner}"
196
        );
197
    }
198
2
}
199

            
200
#[test]
201
2
fn every_form_with_tag_rows_suggests_tag_names() {
202
2
    let tagged: Vec<_> = forms::all()
203
2
        .iter()
204
42
        .filter(|form| {
205
42
            form.fields
206
42
                .iter()
207
154
                .any(|f| f.key == "tags" && matches!(f.kind, forms::Kind::Rows { .. }))
208
42
        })
209
        .collect();
210
2
    assert!(tagged.len() >= 4, "{}", tagged.len());
211
2
    for form in tagged {
212
8
        let html = html_of(form);
213
8
        assert!(
214
8
            attr_of(&html, "tags.0.name", "data-suggest").is_some(),
215
            "{}",
216
            form.name
217
        );
218
    }
219
2
}
220

            
221
#[test]
222
2
fn the_report_category_tag_name_suggests_split_tag_names() {
223
2
    let html = html_of(&REPORT_CATEGORY);
224
2
    assert_eq!(
225
2
        attr_of(&html, "tag", "data-suggest").as_deref(),
226
2
        Some("/api/forms/suggestions/split-tag-names")
227
    );
228
2
    assert_eq!(attr_of(&html, "tag", "data-suggest-by"), None);
229
2
}
230

            
231
#[test]
232
2
fn only_the_to_commodity_cell_carries_its_empty_meaning() {
233
2
    let html = html_of(&TRANSACTION_CREATE);
234
6
    let hint = |name: &str| attr_of(&html, name, "placeholder");
235
2
    assert_eq!(hint("label:splits.0.to-commodity").as_deref(), Some("same"));
236
2
    for name in ["label:splits.0.from-commodity", "label:splits.0.to"] {
237
4
        assert_eq!(hint(name), None, "{name}");
238
    }
239
2
}