1
//! The create submit over the shared forms (plan 0016 W5): plain
2
//! address-named fields in, `rpc::forms::submit` behind.
3

            
4
use axum::http::StatusCode;
5

            
6
use crate::common::new_page::{
7
    Ids, created_id, input_value, post_form, seeded_ids, seeded_user, snapshot, textarea,
8
};
9

            
10
const DATE: &str = "2026-10-27T09:15:42.123456Z";
11

            
12
9
fn entry<'a>(ids: &'a Ids, from: &'a str, note: &'a str) -> Vec<(&'a str, &'a str)> {
13
9
    vec![
14
9
        ("date", DATE),
15
9
        ("note", note),
16
9
        ("tags", ""),
17
9
        ("tags.0.name", "trip"),
18
9
        ("tags.0.value", "japan"),
19
9
        ("tags.0.description", "spring"),
20
9
        ("splits", ""),
21
9
        ("splits.0.key", "n1"),
22
9
        ("splits.0.from", from),
23
9
        ("splits.0.to", &ids.food),
24
9
        ("splits.0.from-commodity", &ids.usd),
25
9
        ("splits.0.value", "50"),
26
9
        ("splits.0.hidden", ""),
27
9
        ("split-tags", ""),
28
9
        ("split-tags.0.row", "n1"),
29
9
        ("split-tags.0.name", "memo"),
30
9
        ("split-tags.0.value", "lunch"),
31
    ]
32
9
}
33

            
34
6
fn has(facts: &[String], prefix: &str) -> bool {
35
33
    facts.iter().any(|fact| fact.starts_with(prefix))
36
6
}
37

            
38
#[supp_macro::shared_runtime_test]
39
1
async fn a_valid_create_stores_tags_and_split_tags() {
40
1
    let user = seeded_user().await;
41
1
    let ids = seeded_ids(&user).await;
42
1
    let reply = post_form(&user, &entry(&ids, &ids.checking, "lunch")).await;
43
1
    let id = created_id(&reply);
44
1
    let facts = snapshot(&user, id).await;
45

            
46
1
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
47
1
    assert!(
48
1
        reply.body.contains("New transaction created with ID"),
49
        "{}",
50
        reply.body
51
    );
52
1
    assert!(
53
1
        has(&facts, "date 2026-10-27 09:15:42.123456+00"),
54
        "{facts:?}"
55
    );
56
1
    assert!(has(&facts, "tag trip japan spring"), "{facts:?}");
57
1
    assert!(has(&facts, "tag note lunch"), "{facts:?}");
58
7
    let split_tags = facts.iter().filter(|f| f.starts_with("split-tag ")).count();
59
1
    assert_eq!(split_tags, 2, "{facts:?}");
60
7
    assert!(facts.iter().all(|f| !f.starts_with("price ")), "{facts:?}");
61
}
62

            
63
#[supp_macro::shared_runtime_test]
64
1
async fn an_invalid_create_rerenders_and_a_corrected_resubmit_succeeds() {
65
1
    let user = seeded_user().await;
66
1
    let ids = seeded_ids(&user).await;
67
1
    let invalid = post_form(&user, &entry(&ids, "Nowhere", "multi\r\nline")).await;
68
1
    let corrected = post_form(&user, &entry(&ids, &ids.checking, "multi\r\nline")).await;
69
1
    let id = created_id(&corrected);
70
1
    let facts = snapshot(&user, id).await;
71

            
72
1
    assert_eq!(
73
1
        invalid.status,
74
1
        StatusCode::UNPROCESSABLE_ENTITY,
75
        "{}",
76
        invalid.body
77
    );
78
1
    assert_eq!(
79
1
        invalid.headers.get("hx-retarget").map(|v| v.as_bytes()),
80
1
        Some(&b"#nf-transaction-create"[..])
81
    );
82
1
    assert_eq!(
83
1
        invalid.headers.get("hx-reswap").map(|v| v.as_bytes()),
84
1
        Some(&b"outerHTML"[..])
85
    );
86
1
    let html = &invalid.body;
87
1
    assert!(html.starts_with("<form"), "{html}");
88
1
    assert!(html.contains("field invalid"), "{html}");
89
1
    assert_eq!(input_value(html, "splits.0.from"), Some("Nowhere"));
90
1
    assert_eq!(input_value(html, "splits.0.key"), Some("n1"));
91
1
    assert_eq!(input_value(html, "splits.0.hidden"), Some(""));
92
1
    assert_eq!(input_value(html, "tags.0.name"), Some("trip"));
93
1
    assert_eq!(textarea(html, "tags.0.description"), Some("spring"));
94
1
    assert_eq!(input_value(html, "split-tags.0.row"), Some("n1"));
95
1
    assert_eq!(input_value(html, "split-tags.0.value"), Some("lunch"));
96
1
    assert_eq!(input_value(html, "date"), Some(DATE));
97
1
    assert_eq!(textarea(html, "note"), Some("multi\nline"));
98

            
99
1
    assert_eq!(corrected.status, StatusCode::OK, "{}", corrected.body);
100
1
    assert!(has(&facts, "tag note multi\nline"), "{facts:?}");
101
1
    assert!(has(&facts, "tag trip japan"), "{facts:?}");
102
}
103

            
104
/// A new row posts its hidden cell empty: that is no hidden data, the same
105
/// create as one without the cell.
106
#[supp_macro::shared_runtime_test]
107
1
async fn an_empty_hidden_cell_means_no_hidden_data() {
108
1
    let user = seeded_user().await;
109
1
    let ids = seeded_ids(&user).await;
110
1
    let with = post_form(&user, &entry(&ids, &ids.checking, "n")).await;
111
1
    let without: Vec<_> = entry(&ids, &ids.checking, "n")
112
1
        .into_iter()
113
17
        .filter(|(name, _)| *name != "splits.0.hidden")
114
        .collect();
115
1
    let without = post_form(&user, &without).await;
116
    let (with, without) = (
117
1
        snapshot(&user, created_id(&with)).await,
118
1
        snapshot(&user, created_id(&without)).await,
119
    );
120

            
121
2
    let stable = |facts: Vec<String>| -> Vec<String> {
122
2
        facts
123
2
            .into_iter()
124
14
            .filter(|fact| !fact.starts_with("date "))
125
            .collect()
126
2
    };
127
1
    assert_eq!(stable(with), stable(without));
128
}
129

            
130
#[supp_macro::shared_runtime_test]
131
1
async fn a_whitespace_only_note_is_dropped() {
132
1
    let user = seeded_user().await;
133
1
    let ids = seeded_ids(&user).await;
134
1
    let reply = post_form(&user, &entry(&ids, &ids.checking, " \t ")).await;
135
1
    let facts = snapshot(&user, created_id(&reply)).await;
136

            
137
1
    assert!(!has(&facts, "tag note"), "{facts:?}");
138
}
139

            
140
#[supp_macro::shared_runtime_test]
141
1
async fn an_absent_date_is_not_filled_in() {
142
1
    let user = seeded_user().await;
143
1
    let ids = seeded_ids(&user).await;
144
1
    let undated: Vec<_> = entry(&ids, &ids.checking, "n")
145
1
        .into_iter()
146
17
        .filter(|(name, _)| *name != "date")
147
        .collect();
148
1
    let reply = post_form(&user, &undated).await;
149

            
150
1
    assert_eq!(
151
1
        reply.status,
152
1
        StatusCode::UNPROCESSABLE_ENTITY,
153
        "{}",
154
        reply.body
155
    );
156
1
    assert!(reply.body.contains("is required"), "{}", reply.body);
157
}
158

            
159
#[supp_macro::shared_runtime_test]
160
1
async fn an_undecodable_body_is_refused_whole() {
161
1
    let user = seeded_user().await;
162
1
    let ids = seeded_ids(&user).await;
163
1
    let mut gap = entry(&ids, &ids.checking, "n");
164
1
    gap.push(("splits.2.value", "1"));
165
1
    let reply = post_form(&user, &gap).await;
166

            
167
1
    assert_eq!(
168
1
        reply.status,
169
1
        StatusCode::UNPROCESSABLE_ENTITY,
170
        "{}",
171
        reply.body
172
    );
173
1
    assert!(reply.headers.get("hx-retarget").is_none());
174
1
    assert!(!reply.body.contains("<form"), "{}", reply.body);
175
}
176

            
177
#[supp_macro::shared_runtime_test]
178
1
async fn an_old_clients_name_is_a_field_error_at_its_cell() {
179
1
    let user = seeded_user().await;
180
1
    let ids = seeded_ids(&user).await;
181
1
    let reply = post_form(&user, &entry(&ids, "Assets:Checking", "lunch")).await;
182

            
183
1
    assert_eq!(
184
1
        reply.status,
185
1
        StatusCode::UNPROCESSABLE_ENTITY,
186
        "{}",
187
        reply.body
188
    );
189
1
    assert!(reply.body.contains("unknown account"), "{}", reply.body);
190
1
    assert_eq!(
191
1
        input_value(&reply.body, "splits.0.from"),
192
1
        Some("Assets:Checking")
193
    );
194
}