1
//! Text typed into a select control that picks no entity is refused at its
2
//! address and echoed back; it is never submitted as a name.
3

            
4
use axum::http::StatusCode;
5

            
6
use crate::common::list_page::list;
7
use crate::common::new_page::{input_value, post_form, seeded_ids, seeded_user};
8

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

            
11
#[supp_macro::shared_runtime_test]
12
1
async fn an_unpicked_cell_is_a_field_error_with_its_text_kept() {
13
1
    let user = seeded_user().await;
14
1
    let ids = seeded_ids(&user).await;
15
1
    let reply = post_form(
16
1
        &user,
17
1
        &[
18
1
            ("date", DATE),
19
1
            ("splits", ""),
20
1
            ("splits.0.key", "n1"),
21
1
            ("splits.0.from", &ids.checking),
22
1
            ("label:splits.0.from", "Assets:Checking"),
23
1
            ("splits.0.to", ""),
24
1
            ("label:splits.0.to", "Expenses:Foo"),
25
1
            ("splits.0.from-commodity", &ids.usd),
26
1
            ("label:splits.0.from-commodity", "USD"),
27
1
            ("splits.0.to-commodity", ""),
28
1
            ("label:splits.0.to-commodity", ""),
29
1
            ("splits.0.value", "50"),
30
1
        ],
31
1
    )
32
1
    .await;
33

            
34
1
    assert_eq!(
35
1
        reply.status,
36
1
        StatusCode::UNPROCESSABLE_ENTITY,
37
        "{}",
38
        reply.body
39
    );
40
1
    let html = &reply.body;
41
1
    assert_eq!(
42
1
        html.matches("pick an entry from the list").count(),
43
1
        1,
44
        "{html}"
45
    );
46
1
    assert!(!html.contains("is required"), "{html}");
47
1
    assert_eq!(input_value(html, "label:splits.0.to"), Some("Expenses:Foo"));
48
1
    assert_eq!(input_value(html, "splits.0.to"), Some(""));
49
1
    assert_eq!(
50
1
        input_value(html, "label:splits.0.from"),
51
1
        Some("Assets:Checking")
52
    );
53
}
54

            
55
#[supp_macro::shared_runtime_test]
56
1
async fn an_unpicked_list_filter_is_refused_and_an_empty_one_lists() {
57
1
    let user = seeded_user().await;
58
1
    let refused = list(&user, "label%3Aaccount=Bank&account=").await;
59
1
    let empty = list(&user, "label%3Aaccount=&account=").await;
60

            
61
1
    assert_eq!(
62
1
        refused.status,
63
1
        StatusCode::UNPROCESSABLE_ENTITY,
64
        "{}",
65
        refused.body
66
    );
67
1
    assert!(
68
1
        refused.body.contains("pick an entry from the list"),
69
        "{}",
70
        refused.body
71
    );
72
1
    assert_eq!(empty.status, StatusCode::OK, "{}", empty.body);
73
}
74

            
75
/// The id of the seeded account labelled `label`.
76
1
async fn account_id(user: &web::model::User, label: &str) -> String {
77
1
    let options = rpc::forms::options(user.id, forms::Source::Accounts)
78
1
        .await
79
        .unwrap_or_default();
80
1
    options
81
1
        .into_iter()
82
2
        .find(|option| option.label == label)
83
1
        .map(|option| option.id.to_string())
84
        .unwrap_or_default()
85
1
}
86

            
87
/// A create whose `to` carries `id` while its control shows `shown`.
88
2
async fn post_to(user: &web::model::User, id: &str, shown: &str) -> crate::common::new_page::Reply {
89
2
    let ids = seeded_ids(user).await;
90
2
    post_form(
91
2
        user,
92
2
        &[
93
2
            ("date", DATE),
94
2
            ("splits", ""),
95
2
            ("splits.0.key", "n1"),
96
2
            ("splits.0.from", &ids.checking),
97
2
            ("label:splits.0.from", "Assets:Checking"),
98
2
            ("splits.0.to", id),
99
2
            ("label:splits.0.to", shown),
100
2
            ("splits.0.from-commodity", &ids.usd),
101
2
            ("label:splits.0.from-commodity", "USD"),
102
2
            ("splits.0.value", "50"),
103
2
        ],
104
2
    )
105
2
    .await
106
2
}
107

            
108
#[supp_macro::shared_runtime_test]
109
1
async fn an_id_whose_text_was_retyped_is_refused_and_a_matching_one_kept() {
110
1
    let user = seeded_user().await;
111
1
    let food = account_id(&user, "Expenses:Food").await;
112
1
    let retyped = post_to(&user, &food, "Assets:Checking").await;
113
1
    let matching = post_to(&user, &food, "Expenses:Food").await;
114

            
115
1
    assert!(!food.is_empty());
116
1
    assert_eq!(
117
1
        retyped.status,
118
1
        StatusCode::UNPROCESSABLE_ENTITY,
119
        "{}",
120
        retyped.body
121
    );
122
1
    assert_eq!(
123
1
        retyped.body.matches("pick an entry from the list").count(),
124
1
        1,
125
        "{}",
126
        retyped.body
127
    );
128
1
    assert_eq!(
129
1
        input_value(&retyped.body, "label:splits.0.to"),
130
1
        Some("Assets:Checking")
131
    );
132
1
    assert_eq!(matching.status, StatusCode::OK, "{}", matching.body);
133
}