1
//! Picking an entity: the visible control only ever shows a label, and the
2
//! submit stores the entity picked, by mouse, keyboard or exact typed label.
3

            
4
use anyhow::{Result, ensure};
5
use serde_json::json;
6

            
7
use super::checks::check_page;
8
use super::driver::TAB;
9
use super::harness::{Harness, with_harness};
10
use super::page::{
11
    Pick, login, open, pick, reference, reply_id, set_text, submit, visible, wait_for,
12
};
13
use super::rows::named;
14
use super::store::{leg, legs, snapshot};
15

            
16
/// Whether `text` holds a UUID, dashed or not.
17
1
fn shows_an_id(text: &str) -> bool {
18
11
    text.split(|c: char| !(c.is_ascii_hexdigit() || c == '-'))
19
4
        .any(|word| uuid::Uuid::parse_str(word).is_ok() || word.replace('-', "").len() >= 32)
20
1
}
21

            
22
/// A label shown for the top-level `Cafe` accounts (not the child path).
23
5
fn top_cafe(label: &str) -> bool {
24
5
    label.starts_with("Cafe") && !label.contains(':') && !label.contains("McDonalds")
25
5
}
26

            
27
1
async fn mouse_pick_of_a_duplicate(h: &Harness) -> Result<()> {
28
1
    login(h).await?;
29
1
    open(h, "/transaction/new").await?;
30
1
    pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
31
1
    let label = pick(h, "splits.0.to", "Caf", top_cafe, 1, Pick::Mouse).await?;
32
1
    let (shown, id) = reference(h, "splits.0.to").await?;
33
1
    ensure!(
34
1
        !shows_an_id(&shown),
35
        "the To control shows an id: {shown:?}"
36
    );
37
1
    ensure!(shown == label, "shown {shown:?}, picked {label:?}");
38
1
    let cafes = [h.account("Cafe"), h.account("Cafe2")];
39
1
    ensure!(
40
1
        cafes.iter().any(|cafe| cafe.to_string() == id),
41
        "the To reference {id:?} is no Cafe"
42
    );
43
1
    pick(
44
1
        h,
45
1
        "splits.0.from-commodity",
46
1
        "JPY",
47
1
        |l| l == "JPY",
48
1
        0,
49
1
        Pick::Mouse,
50
    )
51
1
    .await?;
52
1
    set_text(h, "splits.0.value", "600").await?;
53
1
    check_page(h, "new-duplicate-picked", &[]).await?;
54
1
    let reply = submit(h).await?;
55
1
    ensure!(reply.contains("created with ID"), "{reply}");
56
1
    let stored = legs(&snapshot(h, reply_id(&reply)?).await?);
57
1
    let jpy = h.commodity("JPY");
58
1
    let to = leg(id.parse()?, jpy, "600/1");
59
1
    ensure!(stored.contains(&to), "{stored:#?} lacks {to}");
60
1
    Ok(())
61
1
}
62

            
63
#[supp_macro::shared_runtime_test]
64
1
async fn a_mouse_picked_duplicate_shows_its_label_and_stores_its_id() -> Result<()> {
65
1
    with_harness(|h| Box::pin(mouse_pick_of_a_duplicate(h))).await
66
}
67

            
68
1
async fn labels_are_names(h: &Harness) -> Result<()> {
69
1
    login(h).await?;
70
1
    open(h, "/transaction/new").await?;
71
1
    let input = visible(h, "splits.0.to").await?;
72
1
    h.driver.click(&input).await?;
73
1
    h.driver.type_into(&input, "Caf").await?;
74
1
    let labels = wait_for(
75
1
        &h.driver,
76
1
        "const l = [...document.querySelectorAll('.autocomplete-results .autocomplete-item')]\
77
1
         .map(i => i.firstChild.textContent.trim()); return l.length >= 3 ? l : null;",
78
1
        vec![],
79
1
        "the Cafe suggestions",
80
1
    )
81
1
    .await?;
82
1
    let labels: Vec<String> = serde_json::from_value(labels)?;
83
1
    let mut unique = labels.clone();
84
1
    unique.sort();
85
1
    unique.dedup();
86
1
    ensure!(unique.len() == labels.len(), "duplicate labels: {labels:?}");
87
1
    ensure!(labels.contains(&"McDonalds".to_string()), "{labels:?}");
88
1
    ensure!(
89
1
        labels
90
1
            .iter()
91
3
            .filter(|l| top_cafe(l))
92
2
            .all(|l| l.starts_with("Cafe ‹")),
93
        "{labels:?}"
94
    );
95
1
    h.driver.clear(&input).await?;
96
1
    pick(h, "splits.0.to", "JR", |l| l == "JR", 0, Pick::Keyboard).await?;
97
1
    let (shown, id) = reference(h, "splits.0.to").await?;
98
1
    ensure!(
99
1
        shown == "JR" && id == h.account("JR").to_string(),
100
        "{shown} {id}"
101
    );
102
1
    Ok(())
103
1
}
104

            
105
#[supp_macro::shared_runtime_test]
106
1
async fn picks_show_bare_names_and_duplicates_disambiguated() -> Result<()> {
107
1
    with_harness(|h| Box::pin(labels_are_names(h))).await
108
}
109

            
110
1
async fn typed_text(h: &Harness) -> Result<()> {
111
1
    login(h).await?;
112
1
    open(h, "/transaction/new").await?;
113
1
    let from = visible(h, "splits.0.from").await?;
114
1
    h.driver.click(&from).await?;
115
1
    h.driver.type_into(&from, &format!("Suica{TAB}")).await?;
116
1
    let suica = h.account("Suica").to_string();
117
1
    wait_for(
118
1
        &h.driver,
119
1
        "return document.querySelector('input[type=hidden][name=\"splits.0.from\"]').value \
120
1
         === arguments[0];",
121
1
        vec![json!(suica)],
122
1
        "the typed label Suica to pick its account",
123
1
    )
124
1
    .await?;
125
1
    let to = visible(h, "splits.0.to").await?;
126
1
    h.driver.click(&to).await?;
127
1
    h.driver.type_into(&to, &format!("Caf{TAB}")).await?;
128
1
    pick(
129
1
        h,
130
1
        "splits.0.from-commodity",
131
1
        "JPY",
132
1
        |l| l == "JPY",
133
1
        0,
134
1
        Pick::Mouse,
135
    )
136
1
    .await?;
137
1
    set_text(h, "splits.0.value", "5").await?;
138
1
    let reply = submit(h).await?;
139
1
    ensure!(
140
1
        reply.contains("pick an entry from the list"),
141
        "unpicked text was not refused: {reply}"
142
    );
143
1
    let typed = h
144
1
        .driver
145
1
        .run(
146
1
            "return document.querySelector('input[name=\"label:splits.0.to\"]').value;",
147
1
            vec![],
148
1
        )
149
1
        .await?;
150
1
    ensure!(typed == json!("Caf"), "the typed text is lost: {typed}");
151
1
    check_page(h, "new-unpicked-refused", &[422]).await?;
152
1
    pick(h, "splits.0.to", "JR", |l| l == "JR", 0, Pick::Mouse).await?;
153
1
    let reply = submit(h).await?;
154
1
    ensure!(reply.contains("created with ID"), "{reply}");
155
1
    let stored = legs(&snapshot(h, reply_id(&reply)?).await?);
156
1
    let jr = leg(h.account("JR"), h.commodity("JPY"), "5/1");
157
1
    ensure!(stored.contains(&jr), "{stored:#?}");
158
1
    Ok(())
159
1
}
160

            
161
#[supp_macro::shared_runtime_test]
162
1
async fn typed_text_resolves_only_as_an_exact_label() -> Result<()> {
163
1
    with_harness(|h| Box::pin(typed_text(h))).await
164
}
165

            
166
3
async fn field_errors(h: &Harness) -> Result<Vec<String>> {
167
3
    let found = h
168
3
        .driver
169
3
        .run(
170
3
            "return [...document.querySelectorAll('.field-error')].map(e => e.textContent.trim());",
171
3
            vec![],
172
3
        )
173
3
        .await?;
174
3
    Ok(serde_json::from_value(found)?)
175
3
}
176

            
177
1
async fn success_clears_field_errors(h: &Harness) -> Result<()> {
178
1
    login(h).await?;
179
1
    open(h, "/transaction/new").await?;
180
1
    pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
181
1
    let to = visible(h, "splits.0.to").await?;
182
1
    h.driver.click(&to).await?;
183
1
    h.driver.type_into(&to, &format!("Cafeteria{TAB}")).await?;
184
1
    pick(
185
1
        h,
186
1
        "splits.0.from-commodity",
187
1
        "JPY",
188
1
        |l| l == "JPY",
189
1
        0,
190
1
        Pick::Mouse,
191
    )
192
1
    .await?;
193
1
    set_text(h, "splits.0.value", "7").await?;
194
1
    let reply = submit(h).await?;
195
1
    ensure!(reply.contains("pick an entry from the list"), "{reply}");
196
1
    let shown = field_errors(h).await?;
197
1
    ensure!(shown.len() == 1, "expected one field error: {shown:?}");
198
1
    let reply = submit(h).await?;
199
1
    ensure!(reply.contains("pick an entry from the list"), "{reply}");
200
1
    let shown = field_errors(h).await?;
201
1
    ensure!(shown.len() == 1, "errors accumulated: {shown:?}");
202
1
    pick(h, "splits.0.to", "JR", |l| l == "JR", 0, Pick::Mouse).await?;
203
1
    let reply = submit(h).await?;
204
1
    ensure!(reply.contains("created with ID"), "{reply}");
205
1
    let stale = field_errors(h).await?;
206
1
    ensure!(
207
1
        stale.is_empty(),
208
        "stale field errors after success: {stale:?}"
209
    );
210
1
    Ok(())
211
1
}
212

            
213
#[supp_macro::shared_runtime_test]
214
1
async fn a_successful_submit_leaves_no_stale_field_errors() -> Result<()> {
215
1
    with_harness(|h| Box::pin(success_clears_field_errors(h))).await
216
}