1
//! Multi-split transactions created through the page: every row is one
2
//! independent pair, with and without a tag on one of its legs.
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, reply_id, submit};
9
use super::rows::{Split, fill_splits};
10
use super::store::{leg_tags, legs, snapshot};
11

            
12
/// Creates `splits` on a fresh create page and checks what was stored.
13
10
async fn create(h: &Harness, name: &str, splits: &[Split]) -> Result<()> {
14
10
    open(h, "/transaction/new").await?;
15
10
    fill_splits(h, splits).await?;
16
10
    check_page(h, name, &[]).await?;
17
10
    let reply = submit(h).await?;
18
10
    ensure!(reply.contains("created with ID"), "{name}: {reply}");
19
10
    let stored = snapshot(h, reply_id(&reply)?).await?;
20
22
    let mut expected: Vec<String> = splits.iter().flat_map(|split| split.legs(h)).collect();
21
10
    expected.sort();
22
10
    ensure!(legs(&stored) == expected, "{name}: {stored:#?}");
23
22
    let mut tags: Vec<String> = splits.iter().filter_map(|s| s.leg_tag(h)).collect();
24
10
    tags.sort();
25
10
    ensure!(leg_tags(&stored) == tags, "{name}: {stored:#?}");
26
10
    check_page(h, &format!("{name}-created"), &[]).await
27
10
}
28

            
29
/// `splits` as typed, then again with a tag on the `to` leg of the first
30
/// row and the `from` leg of the last.
31
5
async fn create_both(h: &Harness, name: &str, splits: Vec<Split>) -> Result<()> {
32
5
    login(h).await?;
33
5
    create(h, name, &splits).await?;
34
5
    let mut tagged = splits.clone();
35
5
    if let Some(first) = tagged.first_mut() {
36
5
        *first = first.tagged("to");
37
    }
38
5
    if let Some(last) = tagged.last_mut().filter(|_| splits.len() > 1) {
39
5
        *last = last.tagged("from");
40
    }
41
5
    create(h, &format!("{name}-tagged"), &tagged).await
42
5
}
43

            
44
#[supp_macro::shared_runtime_test]
45
1
async fn one_from_account_pays_two_to_accounts() -> Result<()> {
46
1
    with_harness(|h| {
47
1
        Box::pin(create_both(
48
1
            h,
49
1
            "new-one-from-two-to",
50
1
            vec![
51
1
                Split::plain("Suica", "JR", "199"),
52
1
                Split::plain("Suica", "McDonalds", "600"),
53
1
            ],
54
1
        ))
55
1
    })
56
1
    .await
57
}
58

            
59
#[supp_macro::shared_runtime_test]
60
1
async fn two_rows_with_different_from_accounts() -> Result<()> {
61
1
    with_harness(|h| {
62
1
        Box::pin(create_both(
63
1
            h,
64
1
            "new-two-from",
65
1
            vec![
66
1
                Split::plain("Suica", "JR", "199"),
67
1
                Split::plain("Wallet", "McDonalds", "500"),
68
1
            ],
69
1
        ))
70
1
    })
71
1
    .await
72
}
73

            
74
#[supp_macro::shared_runtime_test]
75
1
async fn three_rows_store_three_pairs() -> Result<()> {
76
1
    with_harness(|h| {
77
1
        Box::pin(create_both(
78
1
            h,
79
1
            "new-three-rows",
80
1
            vec![
81
1
                Split::plain("Suica", "JR", "199"),
82
1
                Split::plain("Wallet", "McDonalds", "500"),
83
1
                Split::plain("Bank", "Suica", "3000"),
84
1
            ],
85
1
        ))
86
1
    })
87
1
    .await
88
}
89

            
90
#[supp_macro::shared_runtime_test]
91
1
async fn a_converted_row_beside_a_same_currency_row() -> Result<()> {
92
1
    let converted = Split {
93
1
        commodity: "EUR",
94
1
        to_commodity: Some("JPY"),
95
1
        to_amount: Some("1600"),
96
1
        ..Split::plain("Bank", "Suica", "10")
97
1
    };
98
1
    with_harness(|h| {
99
1
        Box::pin(create_both(
100
1
            h,
101
1
            "new-mixed-currency",
102
1
            vec![converted, Split::plain("Suica", "JR", "199")],
103
1
        ))
104
1
    })
105
1
    .await
106
}
107

            
108
#[supp_macro::shared_runtime_test]
109
1
async fn rows_in_two_currencies_without_a_conversion() -> Result<()> {
110
1
    let euro = Split {
111
1
        commodity: "EUR",
112
1
        ..Split::plain("Bank", "Wallet", "20")
113
1
    };
114
1
    with_harness(|h| {
115
1
        Box::pin(create_both(
116
1
            h,
117
1
            "new-two-currencies",
118
1
            vec![Split::plain("Suica", "JR", "199"), euro],
119
1
        ))
120
1
    })
121
1
    .await
122
}