1
//! Forms opened with values: a template prefill and an edit show labels,
2
//! carry what was loaded, and submit it unchanged.
3

            
4
use anyhow::{Result, ensure};
5

            
6
use super::checks::check_page;
7
use super::harness::{Harness, with_harness};
8
use super::page::{login, open, reference, reply_id, submit};
9
use super::rows::{Split, fill_splits};
10
use super::store::{leg_tags, legs, snapshot};
11

            
12
const TRAIN: &str = r#"
13
    (set-draft-note "Train")
14
    (draft-split (get-account "Suica") (get-commodity "JPY") -199/1)
15
    (draft-split (get-account "JR") (get-commodity "JPY") 199/1)
16
    (draft-split-tag 1 "category" "transportation")
17
"#;
18

            
19
2
async fn shows(h: &Harness, address: &str, label: &str) -> Result<()> {
20
2
    let (shown, _) = reference(h, address).await?;
21
2
    ensure!(shown == label, "{address} shows {shown:?}, not {label:?}");
22
2
    Ok(())
23
2
}
24

            
25
2
async fn side_of(h: &Harness) -> Result<String> {
26
2
    let side = h
27
2
        .driver
28
2
        .run(
29
2
            "return document.querySelector('select[name=\"split-tags.0.side\"]').value;",
30
2
            vec![],
31
2
        )
32
2
        .await?;
33
2
    Ok(side.as_str().unwrap_or_default().to_string())
34
2
}
35

            
36
1
async fn template(h: &Harness) -> Result<()> {
37
1
    let id = server::user::User { id: h.tenant.id }
38
1
        .create_template(TRAIN.to_string(), Some("train".to_string()))
39
1
        .await?;
40
1
    login(h).await?;
41
1
    open(h, &format!("/transaction/new?template={id}")).await?;
42
1
    shows(h, "splits.0.from", "Suica").await?;
43
1
    shows(h, "splits.0.to", "JR").await?;
44
1
    let side = side_of(h).await?;
45
1
    ensure!(side == "to", "the template tag sits on {side:?}");
46
1
    check_page(h, "new-from-template", &[]).await?;
47
1
    let reply = submit(h).await?;
48
1
    ensure!(reply.contains("created with ID"), "{reply}");
49
1
    let stored = snapshot(h, reply_id(&reply)?).await?;
50
1
    let jr = h.account("JR");
51
1
    ensure!(
52
1
        leg_tags(&stored) == [format!("{jr} 199/1 category transportation")],
53
        "{stored:#?}"
54
    );
55
1
    Ok(())
56
1
}
57

            
58
#[supp_macro::shared_runtime_test]
59
1
async fn a_one_leg_template_shows_names_and_keeps_its_tag_leg() -> Result<()> {
60
1
    with_harness(|h| Box::pin(template(h))).await
61
}
62

            
63
1
async fn unchanged_edit(h: &Harness) -> Result<()> {
64
1
    login(h).await?;
65
1
    open(h, "/transaction/new").await?;
66
1
    let splits = [
67
1
        Split::plain("Suica", "JR", "199").tagged("to"),
68
1
        Split::plain("Wallet", "McDonalds", "500"),
69
1
        Split::plain("Bank", "Cafe", "300"),
70
1
    ];
71
1
    fill_splits(h, &splits).await?;
72
1
    let reply = submit(h).await?;
73
1
    ensure!(reply.contains("created with ID"), "{reply}");
74
1
    let id = reply_id(&reply)?;
75
1
    let before = snapshot(h, id).await?;
76
1
    open(h, &format!("/transaction/{id}/edit")).await?;
77
1
    let side = side_of(h).await?;
78
1
    ensure!(side == "to", "the saved tag shows {side:?} after a reload");
79
1
    let mut shown = Vec::new();
80
1
    for row in 0..splits.len() {
81
3
        for cell in ["from", "to"] {
82
6
            shown.push(reference(h, &format!("splits.{row}.{cell}")).await?.0);
83
        }
84
    }
85
1
    shown.sort();
86
1
    let cafe = shown.get(1).cloned().unwrap_or_default();
87
1
    ensure!(cafe.starts_with("Cafe ‹"), "the duplicate shows {cafe:?}");
88
1
    shown.remove(1);
89
1
    ensure!(
90
1
        shown == ["Bank", "JR", "McDonalds", "Suica", "Wallet"],
91
        "the edit shows {shown:?}"
92
    );
93
1
    check_page(h, "edit-loaded", &[]).await?;
94
1
    let reply = submit(h).await?;
95
1
    ensure!(reply.contains("updated with ID"), "{reply}");
96
1
    let after = snapshot(h, id).await?;
97
1
    ensure!(
98
1
        after == before,
99
        "an unchanged save changed the transaction:\n{before:#?}\n{after:#?}"
100
    );
101
1
    ensure!(legs(&after).len() == 6, "{after:#?}");
102
1
    Ok(())
103
1
}
104

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