1
//! `?from_account` leaves the commodity cells exactly as a pick of that account does.
2

            
3
use anyhow::{Result, anyhow, ensure};
4
use serde_json::{Value, json};
5
use server::command::{CmdResult, FinanceEntity, account::CreateAccount};
6
use uuid::Uuid;
7

            
8
use super::harness::{Harness, with_harness};
9
use super::page::{Pick, login, open, pick, reference, reply_id, set_text, submit, wait_for};
10
use super::rows::named;
11

            
12
const CELLS: [&str; 2] = ["splits.0.from-commodity", "splits.0.to-commodity"];
13

            
14
3
async fn account(h: &Harness, name: &str) -> Result<Uuid> {
15
3
    let created = CreateAccount::new()
16
3
        .name(name.to_string())
17
3
        .user_id(h.tenant.id)
18
3
        .run()
19
3
        .await
20
        .map_err(|e| anyhow!("{e:?}"))?;
21
3
    match created {
22
3
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
23
        other => Err(anyhow!("expected an account, got {other:?}")),
24
    }
25
3
}
26

            
27
6
async fn pick_account(h: &Harness, address: &str, name: &'static str) -> Result<()> {
28
6
    pick(h, address, name, named(name), 0, Pick::Mouse)
29
6
        .await
30
6
        .map(drop)
31
6
}
32

            
33
/// Makes `name` hold only `commodity`, by one posted transaction into `sink`.
34
2
async fn hold(h: &Harness, name: &'static str, commodity: &'static str) -> Result<()> {
35
2
    open(h, "/transaction/new").await?;
36
2
    pick_account(h, "splits.0.from", name).await?;
37
2
    pick_account(h, "splits.0.to", "PrefillSink").await?;
38
2
    pick(h, CELLS[0], commodity, |l| l == commodity, 0, Pick::Mouse).await?;
39
2
    set_text(h, "splits.0.value", "1").await?;
40
2
    let reply = submit(h).await?;
41
2
    ensure!(reply.contains("created with ID"), "{reply}");
42
2
    reply_id(&reply).map(drop)
43
2
}
44

            
45
/// What each commodity cell shows, carries and is marked as.
46
3
async fn cells(h: &Harness) -> Result<Vec<(String, String, bool)>> {
47
3
    let mut state = Vec::new();
48
3
    for cell in CELLS {
49
6
        let (shown, id) = reference(h, cell).await?;
50
6
        let filled = h
51
6
            .driver
52
6
            .run(
53
6
                "return document.querySelector(`input[data-source][name=\"label:${arguments[0]}\"]`)\
54
6
                 .hasAttribute('data-filled');",
55
6
                vec![json!(cell)],
56
6
            )
57
6
            .await?;
58
6
        state.push((shown, id, filled == true));
59
    }
60
3
    Ok(state)
61
3
}
62

            
63
2
async fn settled(h: &Harness, shown: &str) -> Result<Value> {
64
2
    wait_for(
65
2
        &h.driver,
66
2
        "const c = document.querySelector('[name=\"label:splits.0.to-commodity\"]');\
67
2
         return c && c.value === arguments[0];",
68
2
        vec![json!(shown)],
69
2
        "the commodity fill",
70
2
    )
71
2
    .await
72
2
}
73

            
74
1
async fn prefilled_like_a_pick(h: &Harness) -> Result<()> {
75
1
    let sink = account(h, "PrefillSink").await?;
76
    let (yen, euro) = (
77
1
        account(h, "PrefillYen").await?,
78
1
        account(h, "PrefillEuro").await?,
79
    );
80
1
    ensure!(sink != yen && yen != euro);
81
1
    login(h).await?;
82
1
    hold(h, "PrefillYen", "JPY").await?;
83
1
    hold(h, "PrefillEuro", "EUR").await?;
84

            
85
1
    open(h, "/transaction/new").await?;
86
1
    pick_account(h, "splits.0.from", "PrefillYen").await?;
87
1
    settled(h, "JPY").await?;
88
1
    let picked = cells(h).await?;
89
1
    ensure!(picked[0].0 == "JPY" && picked[0].2, "{picked:?}");
90

            
91
1
    open(h, &format!("/transaction/new?from_account={yen}")).await?;
92
1
    let prefilled = cells(h).await?;
93
1
    ensure!(
94
1
        prefilled == picked,
95
        "prefilled {prefilled:?}, picked {picked:?}"
96
    );
97

            
98
1
    pick_account(h, "splits.0.from", "PrefillEuro").await?;
99
1
    settled(h, "EUR").await?;
100
1
    let moved = cells(h).await?;
101
1
    ensure!(
102
2
        moved[0].0 == "EUR" && moved[1].0 == "EUR" && moved.iter().all(|c| c.2),
103
        "{moved:?}"
104
    );
105
1
    ensure!(moved[0].1 == h.commodity("EUR").to_string(), "{moved:?}");
106
1
    Ok(())
107
1
}
108

            
109
#[supp_macro::shared_runtime_test]
110
1
async fn a_prefilled_from_account_fills_the_commodities_like_a_pick() -> Result<()> {
111
1
    with_harness(|h| Box::pin(prefilled_like_a_pick(h))).await
112
}