1
use forms::{
2
    ACCOUNT_CREATE, ACCOUNT_EDIT, ACCOUNT_MERGE, Action, COMMODITY_CREATE, COMMODITY_EDIT,
3
    CONFIG_SET, CONVERT, Field, Form, Kind, TRANSACTION_CREATE, TRANSACTION_EDIT,
4
    TRANSACTION_QUERY, all, by_name, descriptor_sexpr, descriptors_sexpr, escape_str,
5
};
6

            
7
const CREATE_SEXPR: &str = concat!(
8
    "(:name \"transaction-create\" :title \"Create transaction\" :action mutation :fields (",
9
    "(:key \"date\" :label \"Date\" :kind date :required t) ",
10
    "(:key \"note\" :label \"Note\" :kind text :multiline t :required nil) ",
11
    "(:key \"tags\" :label \"Tags\" :kind rows :fields (",
12
    "(:key \"name\" :label \"Name\" :kind text :suggest transaction-tag-names :required nil) ",
13
    "(:key \"value\" :label \"Value\" :kind text :suggest transaction-tag-values :required nil) ",
14
    "(:key \"description\" :label \"Description\" :kind text :multiline t :required nil)) ",
15
    ":required nil) ",
16
    "(:key \"splits\" :label \"Splits\" :kind rows :fields (",
17
    "(:key \"key\" :label \"Key\" :kind text :required nil) ",
18
    "(:key \"from\" :label \"From\" :kind select :source accounts :fills from-commodity :required t) ",
19
    "(:key \"to\" :label \"To\" :kind select :source accounts :required t) ",
20
    "(:key \"from-commodity\" :label \"From commodity\" :kind select :source commodities :fills to-commodity :required t) ",
21
    "(:key \"to-commodity\" :label \"To commodity\" :kind select :source commodities :empty \"same\" :required nil) ",
22
    "(:key \"value\" :label \"Value\" :kind amount :required t) ",
23
    "(:key \"to-amount\" :label \"To amount\" :kind amount :required nil) ",
24
    "(:key \"hidden\" :label \"Hidden\" :kind hidden :required nil)) :required t) ",
25
    "(:key \"split-tags\" :label \"Split tags\" :kind rows :fields (",
26
    "(:key \"row\" :label \"Row\" :kind text :required nil) ",
27
    "(:key \"name\" :label \"Name\" :kind text :suggest split-tag-names :required nil) ",
28
    "(:key \"value\" :label \"Value\" :kind text :suggest split-tag-values :required nil) ",
29
    "(:key \"description\" :label \"Description\" :kind text :multiline t :required nil) ",
30
    "(:key \"side\" :label \"Leg\" :kind choice :options ((\"\" \"Both accounts\") ",
31
    "(\"from\" \"From account only\") (\"to\" \"To account only\")) :default \"\" :required nil)) ",
32
    ":required nil :nested-under \"splits\")))"
33
);
34

            
35
#[test]
36
1
fn create_descriptor_text_is_exact() {
37
1
    assert_eq!(descriptor_sexpr(&TRANSACTION_CREATE), CREATE_SEXPR);
38
1
}
39

            
40
#[test]
41
1
fn edit_has_hidden_id_and_load_action() {
42
1
    let text = descriptor_sexpr(&TRANSACTION_EDIT);
43
1
    assert!(text.contains(
44
1
        ":action load-mutation :fields ((:key \"id\" :label \"Id\" :kind hidden :required t) (:key \"revision\" :label \"Revision\" :kind hidden :required t)"
45
1
    ));
46
1
}
47

            
48
#[test]
49
1
fn report_dates_are_marked_whole_day() {
50
1
    let text = descriptor_sexpr(&forms::REPORT_BALANCE);
51
1
    assert!(
52
1
        text.contains(":kind date :whole-day t :required nil)"),
53
        "{text}"
54
    );
55
1
    assert!(
56
1
        text.contains(":kind date :range-end t :whole-day t :required nil)"),
57
        "{text}"
58
    );
59
1
}
60

            
61
#[test]
62
1
fn query_marks_whole_days_and_the_upper_bound_range_end() {
63
1
    let text = descriptor_sexpr(&TRANSACTION_QUERY);
64
1
    assert!(text.contains(":action query"));
65
1
    assert!(text.contains(
66
1
        "(:key \"to\" :label \"To\" :kind date :range-end t :whole-day t :required nil)"
67
1
    ));
68
1
    assert!(text.contains("(:key \"from\" :label \"From\" :kind date :whole-day t :required nil)"));
69
1
}
70

            
71
#[test]
72
1
fn query_flags_exactly_the_paging_fields() {
73
1
    let text = descriptor_sexpr(&TRANSACTION_QUERY);
74
1
    assert!(text.contains("(:key \"limit\" :label \"Limit\" :kind text :required nil :paging t)"));
75
1
    assert!(
76
1
        text.contains("(:key \"offset\" :label \"Offset\" :kind text :required nil :paging t)")
77
    );
78
1
    assert_eq!(text.matches(":paging t").count(), 2, "{text}");
79
3
    let filtering: Vec<_> = TRANSACTION_QUERY.filter_fields().map(|f| f.key).collect();
80
1
    assert_eq!(filtering, ["account", "from", "to"]);
81
1
}
82

            
83
#[test]
84
1
fn merge_picks_two_required_accounts() {
85
1
    assert_eq!(
86
1
        descriptor_sexpr(&ACCOUNT_MERGE),
87
1
        "(:name \"account-merge\" :title \"Merge accounts\" :action mutation :fields (\
88
1
         (:key \"into\" :label \"Into\" :kind select :source accounts :required t) \
89
1
         (:key \"from\" :label \"From\" :kind select :source accounts :required t)))"
90
    );
91
1
}
92

            
93
#[test]
94
1
fn create_takes_a_name_and_an_optional_parent() {
95
1
    assert_eq!(
96
1
        descriptor_sexpr(&ACCOUNT_CREATE),
97
1
        "(:name \"account-create\" :title \"Create account\" :action mutation :fields (\
98
1
         (:key \"name\" :label \"Name\" :kind text :required t) \
99
1
         (:key \"parent\" :label \"Parent\" :kind select :source accounts :required nil)))"
100
    );
101
1
}
102

            
103
#[test]
104
1
fn account_edit_holds_hidden_ids_and_an_unnested_tags_field() {
105
1
    let text = descriptor_sexpr(&ACCOUNT_EDIT);
106
1
    assert!(text.contains(":action load-mutation :fields ((:key \"id\" :label \"Id\" :kind hidden :required t) (:key \"revision\" :label \"Revision\" :kind hidden :required t) (:key \"name\" :label \"Name\" :kind text :required t) (:key \"parent\""));
107
1
    assert!(text.ends_with("(:key \"description\" :label \"Description\" :kind text :multiline t :required nil)) :required nil)))"));
108
1
    assert_eq!(ACCOUNT_EDIT.nested_keys().count(), 0);
109
1
}
110

            
111
#[test]
112
1
fn commodity_create_takes_a_symbol_and_a_name() {
113
1
    assert_eq!(
114
1
        descriptor_sexpr(&COMMODITY_CREATE),
115
1
        "(:name \"commodity-create\" :title \"Create commodity\" :action mutation :fields (\
116
1
         (:key \"symbol\" :label \"Symbol\" :kind text :required t) \
117
1
         (:key \"name\" :label \"Name\" :kind text :required t)))"
118
    );
119
1
}
120

            
121
#[test]
122
1
fn commodity_edit_holds_hidden_ids_and_an_unnested_tags_field() {
123
1
    let text = descriptor_sexpr(&COMMODITY_EDIT);
124
1
    assert!(text.contains(":action load-mutation :fields ((:key \"id\" :label \"Id\" :kind hidden :required t) (:key \"revision\" :label \"Revision\" :kind hidden :required t) (:key \"symbol\" :label \"Symbol\" :kind text :required t) (:key \"name\" :label \"Name\" :kind text :required t) (:key \"tags\""));
125
1
    assert_eq!(COMMODITY_EDIT.nested_keys().count(), 0);
126
1
}
127

            
128
#[test]
129
1
fn registry_lists_and_finds_forms() {
130
21
    let names: Vec<&str> = all().iter().map(|f| f.name).collect();
131
1
    assert_eq!(
132
1
        names,
133
1
        [
134
1
            "transaction-create",
135
1
            "transaction-edit",
136
1
            "transaction-query",
137
1
            "account-merge",
138
1
            "account-create",
139
1
            "account-edit",
140
1
            "commodity-create",
141
1
            "commodity-edit",
142
1
            "tag-create",
143
1
            "tag-edit",
144
1
            "template-create",
145
1
            "template-edit",
146
1
            "template-test",
147
1
            "script-create",
148
1
            "script-edit",
149
1
            "report-balance",
150
1
            "report-activity",
151
1
            "report-category",
152
1
            "config-set",
153
1
            "convert",
154
1
            "ssh-key-add"
155
1
        ]
156
    );
157
1
    assert_eq!(by_name("transaction-edit"), Some(&TRANSACTION_EDIT));
158
1
    assert_eq!(by_name("account-merge"), Some(&ACCOUNT_MERGE));
159
1
    assert_eq!(by_name("nope"), None);
160
1
    let all_text = descriptors_sexpr(all());
161
1
    assert!(all_text.starts_with("((:name \"transaction-create\""));
162
1
    assert!(all_text.ends_with(')'));
163
1
}
164

            
165
#[test]
166
1
fn row_keys_match_the_logical_payload_keys() {
167
4
    let splits = TRANSACTION_CREATE.fields.iter().find(|f| f.key == "splits");
168
1
    let keys: Vec<&str> = match splits.map(|f| f.kind) {
169
8
        Some(Kind::Rows { fields, .. }) => fields.iter().map(|f| f.key).collect(),
170
        _ => Vec::new(),
171
    };
172
1
    assert_eq!(
173
1
        keys,
174
1
        [
175
1
            "key",
176
1
            "from",
177
1
            "to",
178
1
            "from-commodity",
179
1
            "to-commodity",
180
1
            "value",
181
1
            "to-amount",
182
1
            "hidden"
183
1
        ]
184
    );
185
1
}
186

            
187
#[test]
188
1
fn escape_covers_quote_backslash_and_newlines() {
189
1
    assert_eq!(escape_str("a\"b\\c\nd\re"), "\"a\\\"b\\\\c\\nd\\re\"");
190
1
    assert_eq!(escape_str(""), "\"\"");
191
1
}
192

            
193
#[test]
194
1
fn all_descriptors_match_the_shared_fixture() {
195
1
    let fixture = include_str!("fixtures/descriptors.sexp");
196
1
    assert_eq!(descriptors_sexpr(all()), fixture.trim_end_matches('\n'));
197
1
}
198

            
199
#[test]
200
1
fn a_nested_field_names_its_parent_in_its_sexpr() {
201
    const FIELDS: &[Field] = &[
202
        Field {
203
            key: "tags",
204
            label: "Tags",
205
            kind: Kind::Rows {
206
                fields: &[],
207
                nested_under: Some("splits"),
208
            },
209
            required: false,
210
        },
211
        Field {
212
            key: "note",
213
            label: "Note",
214
            kind: Kind::Text {
215
                multiline: false,
216
                suggest: None,
217
            },
218
            required: false,
219
        },
220
    ];
221
    const FORM: Form = Form {
222
        name: "t",
223
        title: "T",
224
        fields: FIELDS,
225
        action: Action::Mutation,
226
    };
227
1
    assert_eq!(
228
1
        descriptor_sexpr(&FORM),
229
1
        concat!(
230
            "(:name \"t\" :title \"T\" :action mutation :fields (",
231
            "(:key \"tags\" :label \"Tags\" :kind rows :fields () :required nil :nested-under \"splits\") ",
232
            "(:key \"note\" :label \"Note\" :kind text :required nil)))"
233
        )
234
    );
235
1
}
236

            
237
#[test]
238
1
fn a_source_round_trips_through_its_name() {
239
1
    for source in [
240
1
        forms::Source::Accounts,
241
1
        forms::Source::Commodities,
242
1
        forms::Source::Templates,
243
1
        forms::Source::Scripts,
244
1
    ] {
245
4
        assert_eq!(forms::Source::by_name(source.name()), Some(source));
246
    }
247
1
    assert_eq!(forms::Source::by_name("nope"), None);
248
1
}
249

            
250
#[test]
251
1
fn a_suggestion_source_round_trips_through_its_name() {
252
1
    for scope in [
253
1
        forms::Scope::Transaction,
254
1
        forms::Scope::Split,
255
1
        forms::Scope::Account,
256
1
        forms::Scope::Commodity,
257
1
    ] {
258
4
        for suggest in [
259
4
            forms::Suggest::TagNames(scope),
260
4
            forms::Suggest::TagValues(scope),
261
4
        ] {
262
8
            assert_eq!(forms::Suggest::by_name(suggest.name()), Some(suggest));
263
        }
264
    }
265
1
    assert_eq!(forms::Suggest::by_name("accounts"), None);
266
1
}
267

            
268
#[test]
269
1
fn a_suggesting_text_cell_names_its_source_in_its_sexpr() {
270
    const FORM: Form = Form {
271
        name: "t",
272
        title: "T",
273
        fields: &[Field {
274
            key: "name",
275
            label: "Name",
276
            kind: Kind::Text {
277
                multiline: true,
278
                suggest: Some(forms::Suggest::TagNames(forms::Scope::Split)),
279
            },
280
            required: false,
281
        }],
282
        action: Action::Mutation,
283
    };
284
1
    assert_eq!(
285
1
        descriptor_sexpr(&FORM),
286
1
        concat!(
287
            "(:name \"t\" :title \"T\" :action mutation :fields (",
288
            "(:key \"name\" :label \"Name\" :kind text :multiline t :suggest split-tag-names :required nil)))"
289
        )
290
    );
291
1
}
292

            
293
#[test]
294
1
fn a_rows_field_without_a_parent_has_no_flag_in_its_sexpr() {
295
    const FORM: Form = Form {
296
        name: "t",
297
        title: "T",
298
        fields: &[Field {
299
            key: "tags",
300
            label: "Tags",
301
            kind: Kind::Rows {
302
                fields: &[],
303
                nested_under: None,
304
            },
305
            required: false,
306
        }],
307
        action: Action::Mutation,
308
    };
309
1
    assert_eq!(FORM.nested_keys().count(), 0);
310
1
    assert_eq!(
311
1
        descriptor_sexpr(&FORM),
312
1
        "(:name \"t\" :title \"T\" :action mutation :fields (\
313
1
         (:key \"tags\" :label \"Tags\" :kind rows :fields () :required nil)))"
314
    );
315
1
}
316

            
317
#[test]
318
1
fn the_transaction_forms_nest_split_tags_only() {
319
1
    for form in [&TRANSACTION_CREATE, &TRANSACTION_EDIT] {
320
2
        assert_eq!(form.nested_keys().collect::<Vec<_>>(), ["split-tags"]);
321
    }
322
1
    assert_eq!(TRANSACTION_QUERY.nested_keys().count(), 0);
323
1
}
324

            
325
#[test]
326
1
fn config_set_takes_a_key_and_an_optional_value() {
327
1
    assert_eq!(CONFIG_SET.action, Action::Mutation);
328
1
    let required: Vec<_> = CONFIG_SET
329
1
        .fields
330
1
        .iter()
331
2
        .map(|f| (f.key, f.required))
332
        .collect();
333
1
    assert_eq!(required, [("key", true), ("value", false)]);
334
1
}
335

            
336
#[test]
337
1
fn convert_is_a_query_of_an_amount_and_two_commodities() {
338
1
    assert_eq!(CONVERT.action, Action::Query);
339
3
    let keys: Vec<_> = CONVERT.fields.iter().map(|f| (f.key, f.required)).collect();
340
1
    assert_eq!(keys, [("amount", true), ("from", true), ("to", true)]);
341
1
}
342

            
343
#[test]
344
1
fn only_forms_with_a_select_field_have_a_select() {
345
1
    assert!(TRANSACTION_CREATE.has_select());
346
1
    assert!(ACCOUNT_CREATE.has_select());
347
1
    assert!(!CONFIG_SET.has_select());
348
1
}
349

            
350
#[test]
351
1
fn a_form_has_the_sources_of_its_select_fields() {
352
1
    assert!(TRANSACTION_CREATE.has_source(forms::Source::Accounts));
353
1
    assert!(TRANSACTION_CREATE.has_source(forms::Source::Commodities));
354
1
    assert!(!TRANSACTION_CREATE.has_source(forms::Source::Templates));
355
1
    assert!(!CONFIG_SET.has_source(forms::Source::Accounts));
356
1
}
357

            
358
#[test]
359
1
fn the_report_category_tag_name_names_its_suggestion_source() {
360
1
    assert!(descriptor_sexpr(&forms::REPORT_CATEGORY).contains(
361
1
        "(:key \"tag\" :label \"Tag name\" :kind text :suggest split-tag-names :required nil)"
362
1
    ));
363
1
}
364

            
365
#[test]
366
1
fn the_from_account_and_from_commodity_of_a_split_declare_a_fill() {
367
1
    let split_fields = TRANSACTION_CREATE
368
1
        .fields
369
1
        .iter()
370
4
        .find_map(|f| match f.kind {
371
2
            Kind::Rows { fields, .. } if f.key == "splits" => Some(fields),
372
3
            _ => None,
373
4
        })
374
        .unwrap_or_default();
375
19
    let cell = |key: &str| split_fields.iter().find(|f| f.key == key).map(|f| f.kind);
376
1
    assert!(matches!(
377
1
        cell("from"),
378
        Some(Kind::Select {
379
            fills: Some("from-commodity"),
380
            ..
381
        })
382
    ));
383
1
    assert!(matches!(
384
1
        cell("from-commodity"),
385
        Some(Kind::Select {
386
            fills: Some("to-commodity"),
387
            ..
388
        })
389
    ));
390
1
    assert!(matches!(
391
1
        cell("to-commodity"),
392
        Some(Kind::Select {
393
            empty: Some("same"),
394
            ..
395
        })
396
    ));
397
1
    for key in ["to", "to-commodity"] {
398
2
        assert!(
399
2
            matches!(cell(key), Some(Kind::Select { fills: None, .. })),
400
            "{key}"
401
        );
402
    }
403
1
}