1
//! A new split starts as a copy of the one above it, ready to be edited.
2

            
3
use std::time::Duration;
4

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

            
8
use super::harness::{Harness, with_harness};
9
use super::page::{
10
    Pick, add_split, erase, login, open, pick, reference, reply_id, set_text, submit, wait_for,
11
};
12
use super::rows::{Split, fill_splits, named};
13
use super::store::{leg, legs, snapshot};
14

            
15
const FOCUSED_NAME: &str = "return document.activeElement.name;";
16

            
17
4
async fn first_split(h: &Harness) -> Result<()> {
18
4
    pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
19
4
    pick(
20
4
        h,
21
4
        "splits.0.from-commodity",
22
4
        "JPY",
23
4
        |l| l == "JPY",
24
4
        0,
25
4
        Pick::Mouse,
26
    )
27
4
    .await?;
28
4
    pick(
29
4
        h,
30
4
        "splits.0.to-commodity",
31
4
        "EUR",
32
4
        |l| l == "EUR",
33
4
        0,
34
4
        Pick::Keyboard,
35
    )
36
4
    .await?;
37
4
    Ok(())
38
4
}
39

            
40
1
async fn copied_cells(h: &Harness) -> Result<()> {
41
1
    login(h).await?;
42
1
    open(h, "/transaction/new").await?;
43
1
    first_split(h).await?;
44
1
    add_split(h).await?;
45
1
    for cell in ["from", "from-commodity", "to-commodity"] {
46
3
        let before = reference(h, &format!("splits.0.{cell}")).await?;
47
3
        let after = reference(h, &format!("splits.1.{cell}")).await?;
48
3
        ensure!(
49
3
            before == after && !after.0.is_empty(),
50
            "{cell}: {before:?} became {after:?}"
51
        );
52
    }
53
1
    let focused = h.driver.run(FOCUSED_NAME, vec![]).await?;
54
1
    ensure!(
55
1
        focused == json!("label:splits.1.to"),
56
        "the focus is on {focused}"
57
    );
58
1
    wait_for(
59
1
        &h.driver,
60
1
        "const i = document.querySelector('[name=\"splits.1.to-amount\"]');\
61
1
         return i && !i.disabled && i.offsetParent !== null;",
62
1
        vec![],
63
1
        "the copied commodities to offer the converted amount",
64
1
    )
65
1
    .await
66
1
    .map(drop)
67
1
}
68

            
69
1
async fn emptiness_is_copied(h: &Harness) -> Result<()> {
70
1
    login(h).await?;
71
1
    open(h, "/transaction/new").await?;
72
1
    pick(
73
1
        h,
74
1
        "splits.0.from-commodity",
75
1
        "JPY",
76
1
        |l| l == "JPY",
77
1
        0,
78
1
        Pick::Mouse,
79
    )
80
1
    .await?;
81
1
    wait_for(
82
1
        &h.driver,
83
1
        "return document.querySelector('[name=\"label:splits.0.to-commodity\"]').value !== '';",
84
1
        vec![],
85
1
        "the from commodity to fill the to commodity",
86
1
    )
87
1
    .await?;
88
1
    erase(h, "splits.0.to-commodity").await?;
89
1
    add_split(h).await?;
90
1
    let to_commodity = reference(h, "splits.1.to-commodity").await?;
91
1
    ensure!(
92
1
        to_commodity == (String::new(), String::new()),
93
        "{to_commodity:?}"
94
    );
95
1
    Ok(())
96
1
}
97

            
98
1
async fn copies_follow_a_new_pick(h: &Harness) -> Result<()> {
99
1
    login(h).await?;
100
1
    open(h, "/transaction/new").await?;
101
1
    pick(
102
1
        h,
103
1
        "splits.0.from-commodity",
104
1
        "JPY",
105
1
        |l| l == "JPY",
106
1
        0,
107
1
        Pick::Mouse,
108
    )
109
1
    .await?;
110
1
    wait_for(
111
1
        &h.driver,
112
1
        "return document.querySelector('[name=\"label:splits.0.to-commodity\"]').value === 'JPY';",
113
1
        vec![],
114
1
        "the from commodity to fill the to commodity",
115
1
    )
116
1
    .await?;
117
1
    add_split(h).await?;
118
1
    pick(
119
1
        h,
120
1
        "splits.1.from-commodity",
121
1
        "EUR",
122
1
        |l| l == "EUR",
123
1
        0,
124
1
        Pick::Mouse,
125
    )
126
1
    .await?;
127
1
    wait_for(
128
1
        &h.driver,
129
1
        "return document.querySelector('[name=\"label:splits.1.to-commodity\"]').value === 'EUR'\
130
1
         && document.querySelector('[name=\"splits.1.to-amount\"]').disabled;",
131
1
        vec![],
132
1
        "the copied to commodity to follow the new from commodity",
133
1
    )
134
1
    .await
135
1
    .map(drop)
136
1
}
137

            
138
1
async fn copies_submit_and_edit(h: &Harness) -> Result<()> {
139
1
    login(h).await?;
140
1
    open(h, "/transaction/new").await?;
141
1
    pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
142
1
    pick(h, "splits.0.to", "JR", named("JR"), 0, Pick::Mouse).await?;
143
1
    pick(
144
1
        h,
145
1
        "splits.0.from-commodity",
146
1
        "JPY",
147
1
        |l| l == "JPY",
148
1
        0,
149
1
        Pick::Mouse,
150
    )
151
1
    .await?;
152
1
    set_text(h, "splits.0.value", "199").await?;
153
1
    add_split(h).await?;
154
1
    pick(
155
1
        h,
156
1
        "splits.1.to",
157
1
        "McDonalds",
158
1
        named("McDonalds"),
159
1
        0,
160
1
        Pick::Mouse,
161
1
    )
162
1
    .await?;
163
1
    set_text(h, "splits.1.value", "600").await?;
164
1
    let reply = submit(h).await?;
165
1
    ensure!(reply.contains("created with ID"), "{reply}");
166
1
    let stored = legs(&snapshot(h, reply_id(&reply)?).await?);
167
1
    let (suica, jpy) = (h.account("Suica"), h.commodity("JPY"));
168
1
    for (to, value) in [("JR", "199"), ("McDonalds", "600")] {
169
2
        let expected = [
170
2
            leg(suica, jpy, &format!("-{value}/1")),
171
2
            leg(h.account(to), jpy, &format!("{value}/1")),
172
2
        ];
173
2
        ensure!(
174
4
            expected.iter().all(|l| stored.contains(l)),
175
            "{stored:#?} lacks {expected:?}"
176
        );
177
    }
178
1
    Ok(())
179
1
}
180

            
181
1
async fn copies_stay_editable(h: &Harness) -> Result<()> {
182
1
    login(h).await?;
183
1
    open(h, "/transaction/new").await?;
184
1
    first_split(h).await?;
185
1
    add_split(h).await?;
186
1
    pick(h, "splits.1.from", "JR", named("JR"), 0, Pick::Mouse).await?;
187
1
    let (shown, id) = reference(h, "splits.1.from").await?;
188
1
    ensure!(
189
1
        shown == "JR" && id == h.account("JR").to_string(),
190
        "the copy was not editable: {shown:?} {id:?}"
191
    );
192
1
    Ok(())
193
1
}
194

            
195
1
async fn copies_survive_a_pick_without_a_fill(h: &Harness) -> Result<()> {
196
1
    login(h).await?;
197
1
    open(h, "/transaction/new").await?;
198
1
    first_split(h).await?;
199
1
    add_split(h).await?;
200
1
    pick(h, "splits.1.from", "JR", named("JR"), 0, Pick::Mouse).await?;
201
1
    tokio::time::sleep(Duration::from_secs(1)).await;
202
1
    for (cell, expected) in [("from-commodity", "JPY"), ("to-commodity", "EUR")] {
203
2
        let (shown, _) = reference(h, &format!("splits.1.{cell}")).await?;
204
2
        ensure!(shown == expected, "{cell} was cleared to {shown:?}");
205
    }
206
1
    Ok(())
207
1
}
208

            
209
3
async fn commodities_are(h: &Harness, row: usize, shown: &str, what: &str) -> Result<()> {
210
3
    let script = format!(
211
        "return ['from-commodity', 'to-commodity'].every(c => \
212
3
         document.querySelector(`[name=\"label:splits.{row}.${{c}}\"]`).value === '{shown}');"
213
    );
214
3
    wait_for(&h.driver, &script, vec![], what).await.map(drop)
215
3
}
216

            
217
1
async fn copies_survive_a_rerender(h: &Harness) -> Result<()> {
218
1
    login(h).await?;
219
1
    open(h, "/transaction/new").await?;
220
1
    first_split(h).await?;
221
1
    add_split(h).await?;
222
1
    let reply = submit(h).await?;
223
1
    ensure!(
224
1
        reply.starts_with("INVALID"),
225
        "the submit was accepted: {reply}"
226
    );
227
1
    pick(h, "splits.1.from", "JR", named("JR"), 0, Pick::Mouse).await?;
228
1
    tokio::time::sleep(Duration::from_secs(1)).await;
229
1
    for (cell, expected) in [("from-commodity", "JPY"), ("to-commodity", "EUR")] {
230
2
        let (shown, _) = reference(h, &format!("splits.1.{cell}")).await?;
231
2
        ensure!(shown == expected, "{cell} was cleared to {shown:?}");
232
    }
233
1
    Ok(())
234
1
}
235

            
236
/// Suica and JR hold JPY only once this is stored; Wallet holds nothing.
237
1
async fn store_a_train_ride(h: &Harness) -> Result<()> {
238
1
    open(h, "/transaction/new").await?;
239
1
    fill_splits(h, &[Split::plain("Suica", "JR", "199")]).await?;
240
1
    let reply = submit(h).await?;
241
1
    ensure!(reply.contains("created with ID"), "{reply}");
242
1
    Ok(())
243
1
}
244

            
245
1
async fn a_moved_copy_is_an_ordinary_fill(h: &Harness) -> Result<()> {
246
1
    login(h).await?;
247
1
    store_a_train_ride(h).await?;
248
1
    open(h, "/transaction/new").await?;
249
1
    pick(
250
1
        h,
251
1
        "splits.0.from",
252
1
        "Wallet",
253
1
        named("Wallet"),
254
1
        0,
255
1
        Pick::Mouse,
256
1
    )
257
1
    .await?;
258
1
    pick(
259
1
        h,
260
1
        "splits.0.from-commodity",
261
1
        "EUR",
262
1
        |l| l == "EUR",
263
1
        0,
264
1
        Pick::Mouse,
265
    )
266
1
    .await?;
267
1
    commodities_are(h, 0, "EUR", "the from commodity to fill the to commodity").await?;
268
1
    add_split(h).await?;
269
1
    pick(h, "splits.1.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
270
1
    commodities_are(h, 1, "JPY", "Suica's commodity to move the copies").await?;
271
1
    pick(
272
1
        h,
273
1
        "splits.1.from",
274
1
        "Wallet",
275
1
        named("Wallet"),
276
1
        0,
277
1
        Pick::Mouse,
278
1
    )
279
1
    .await?;
280
1
    commodities_are(h, 1, "", "Wallet to clear Suica's fills").await
281
1
}
282

            
283
#[supp_macro::shared_runtime_test]
284
1
async fn a_new_split_copies_the_accounts_and_commodities_above() -> Result<()> {
285
1
    with_harness(|h| Box::pin(copied_cells(h))).await
286
}
287

            
288
#[supp_macro::shared_runtime_test]
289
1
async fn an_empty_to_commodity_is_copied_empty() -> Result<()> {
290
1
    with_harness(|h| Box::pin(emptiness_is_copied(h))).await
291
}
292

            
293
#[supp_macro::shared_runtime_test]
294
1
async fn a_copied_to_commodity_follows_a_new_from_commodity() -> Result<()> {
295
1
    with_harness(|h| Box::pin(copies_follow_a_new_pick(h))).await
296
}
297

            
298
#[supp_macro::shared_runtime_test]
299
1
async fn copied_ids_are_submitted_with_two_splits() -> Result<()> {
300
1
    with_harness(|h| Box::pin(copies_submit_and_edit(h))).await
301
}
302

            
303
#[supp_macro::shared_runtime_test]
304
1
async fn a_copied_cell_can_be_picked_again() -> Result<()> {
305
1
    with_harness(|h| Box::pin(copies_stay_editable(h))).await
306
}
307

            
308
#[supp_macro::shared_runtime_test]
309
1
async fn a_copied_cell_is_kept_by_a_pick_without_a_fill() -> Result<()> {
310
1
    with_harness(|h| Box::pin(copies_survive_a_pick_without_a_fill(h))).await
311
}
312

            
313
#[supp_macro::shared_runtime_test]
314
1
async fn a_copied_cell_is_kept_after_a_validation_rerender() -> Result<()> {
315
1
    with_harness(|h| Box::pin(copies_survive_a_rerender(h))).await
316
}
317

            
318
#[supp_macro::shared_runtime_test]
319
1
async fn a_copy_a_fill_moved_is_cleared_by_a_pick_without_a_fill() -> Result<()> {
320
1
    with_harness(|h| Box::pin(a_moved_copy_is_an_ordinary_fill(h))).await
321
}