1
//! Filling split rows the way a person does, and what they must store.
2

            
3
use anyhow::Result;
4

            
5
use super::harness::Harness;
6
use serde_json::json;
7

            
8
use super::page::{Pick, add_split, add_split_tag, choose, pick, set_text, wait_for};
9
use super::store::leg;
10

            
11
/// A split tag on one leg of its row.
12
#[derive(Clone, Copy)]
13
pub struct LegTag {
14
    pub name: &'static str,
15
    pub value: &'static str,
16
    pub side: &'static str,
17
}
18

            
19
/// One split row, by seeded account and commodity names.
20
#[derive(Clone, Copy)]
21
pub struct Split {
22
    pub from: &'static str,
23
    pub to: &'static str,
24
    pub commodity: &'static str,
25
    pub to_commodity: Option<&'static str>,
26
    pub value: &'static str,
27
    pub to_amount: Option<&'static str>,
28
    pub tag: Option<LegTag>,
29
}
30

            
31
impl Split {
32
30
    pub const fn plain(from: &'static str, to: &'static str, value: &'static str) -> Self {
33
30
        Self {
34
30
            from,
35
30
            to,
36
30
            commodity: "JPY",
37
30
            to_commodity: None,
38
30
            value,
39
30
            to_amount: None,
40
30
            tag: None,
41
30
        }
42
30
    }
43

            
44
13
    pub const fn tagged(self, side: &'static str) -> Self {
45
13
        Self {
46
13
            tag: Some(LegTag {
47
13
                name: "category",
48
13
                value: "e2e",
49
13
                side,
50
13
            }),
51
13
            ..self
52
13
        }
53
13
    }
54

            
55
    /// The two legs it stores, as `store::legs` lists them.
56
32
    pub fn legs(&self, h: &Harness) -> [String; 2] {
57
32
        let to_commodity = self.to_commodity.unwrap_or(self.commodity);
58
32
        let received = self.to_amount.unwrap_or(self.value);
59
32
        [
60
32
            leg(
61
32
                h.account(self.from),
62
32
                h.commodity(self.commodity),
63
32
                &format!("-{}/1", self.value),
64
32
            ),
65
32
            leg(
66
32
                h.account(self.to),
67
32
                h.commodity(to_commodity),
68
32
                &format!("{received}/1"),
69
32
            ),
70
32
        ]
71
32
    }
72

            
73
    /// The split tag it stores, as `store::leg_tags` lists it.
74
22
    pub fn leg_tag(&self, h: &Harness) -> Option<String> {
75
22
        let tag = self.tag?;
76
10
        let [from, to] = self.legs(h);
77
10
        let leg = if tag.side == "from" { from } else { to };
78
10
        let mut words = leg.split(' ');
79
10
        let account = words.next().unwrap_or_default();
80
10
        let value = words.nth(1).unwrap_or_default();
81
10
        Some(format!("{account} {value} {} {}", tag.name, tag.value))
82
22
    }
83
}
84

            
85
/// A label of the account `name`: the name, a path ending in it, or the
86
/// name with an id suffix.
87
105
pub fn named(name: &'static str) -> impl Fn(&str) -> bool {
88
105
    move |label: &str| {
89
105
        label == name
90
1
            || label.ends_with(&format!(":{name}"))
91
1
            || label.starts_with(&format!("{name} ‹"))
92
105
    }
93
105
}
94

            
95
/// Fills split row `row` (adding it first unless it is the first), and its
96
/// tag as split tag `tag_row`.
97
41
pub async fn fill_split(h: &Harness, row: usize, tag_row: usize, split: &Split) -> Result<()> {
98
41
    if row > 0 {
99
16
        add_split(h).await?;
100
25
    }
101
176
    let at = |cell: &str| format!("splits.{row}.{cell}");
102
41
    pick(
103
41
        h,
104
41
        &at("from"),
105
41
        split.from,
106
41
        named(split.from),
107
41
        0,
108
41
        Pick::Mouse,
109
41
    )
110
41
    .await?;
111
41
    pick(h, &at("to"), split.to, named(split.to), 0, Pick::Mouse).await?;
112
41
    let commodity = split.commodity;
113
41
    pick(
114
41
        h,
115
41
        &at("from-commodity"),
116
41
        commodity,
117
41
        |l| l == commodity,
118
41
        0,
119
41
        Pick::Mouse,
120
    )
121
41
    .await?;
122
41
    if let Some(to_commodity) = split.to_commodity {
123
4
        pick(
124
4
            h,
125
4
            &at("to-commodity"),
126
4
            to_commodity,
127
4
            |l| l == to_commodity,
128
4
            0,
129
4
            Pick::Keyboard,
130
        )
131
4
        .await?;
132
37
    }
133
41
    set_text(h, &at("value"), split.value).await?;
134
41
    if let Some(received) = split.to_amount {
135
4
        wait_for(
136
4
            &h.driver,
137
4
            "const i = document.querySelector(`[name=\"${arguments[0]}\"]`);\
138
4
             return i && !i.disabled && i.offsetParent !== null;",
139
4
            vec![json!(at("to-amount"))],
140
4
            "the converted amount to be offered",
141
4
        )
142
4
        .await?;
143
4
        set_text(h, &at("to-amount"), received).await?;
144
37
    }
145
41
    if let Some(tag) = split.tag {
146
13
        add_split_tag(h, row).await?;
147
39
        let cell = |name: &str| format!("split-tags.{tag_row}.{name}");
148
13
        set_text(h, &cell("name"), tag.name).await?;
149
13
        set_text(h, &cell("value"), tag.value).await?;
150
13
        choose(h, &cell("side"), tag.side).await?;
151
28
    }
152
41
    Ok(())
153
41
}
154

            
155
/// Fills every row of `splits` in order.
156
25
pub async fn fill_splits(h: &Harness, splits: &[Split]) -> Result<()> {
157
25
    let mut tag_row = 0;
158
25
    for (row, split) in splits.iter().enumerate() {
159
41
        fill_split(h, row, tag_row, split).await?;
160
41
        tag_row += usize::from(split.tag.is_some());
161
    }
162
25
    Ok(())
163
25
}