1
//! The category report's descriptor form: change the sort and the tag,
2
//! generate, and see the table follow while the form keeps its values.
3

            
4
use anyhow::{Result, anyhow, ensure};
5
use chrono::Utc;
6
use finance::split::Split;
7
use serde_json::json;
8
use server::command::FinanceEntity;
9
use server::command::account::SetAccountTag;
10
use server::command::split::SetSplitTag;
11
use server::command::transaction::CreateTransaction;
12
use uuid::Uuid;
13

            
14
use super::harness::{Harness, with_harness};
15
use super::page::{choose, login, open, set_text, wait_for};
16

            
17
const FIRST_CELLS: &str = ".report-data table.report-table tbody tr td:first-child";
18

            
19
/// An expense of `amount` into `Cafe`, its leg tagged with each of `tags`.
20
2
async fn spend(h: &Harness, amount: i64, tags: &[(&str, &str)]) -> Result<()> {
21
2
    let tx_id = Uuid::new_v4();
22
    let leg = |account: &str, value_num| Split {
23
4
        id: Uuid::new_v4(),
24
4
        tx_id,
25
4
        account_id: h.account(account),
26
4
        commodity_id: h.commodity("JPY"),
27
4
        value_num,
28
4
        value_denom: 1,
29
4
        reconcile_state: None,
30
4
        reconcile_date: None,
31
4
        lot_id: None,
32
4
    };
33
2
    let (from, to) = (leg("Wallet", -amount), leg("Cafe", amount));
34
2
    let to_id = to.id;
35
2
    let now = Utc::now();
36
2
    CreateTransaction::new()
37
2
        .user_id(h.tenant.id)
38
2
        .splits([from, to].into_iter().map(FinanceEntity::Split).collect())
39
2
        .id(tx_id)
40
2
        .post_date(now)
41
2
        .enter_date(now)
42
2
        .run()
43
2
        .await
44
        .map_err(|e| anyhow!("{e:?}"))?;
45
2
    for (name, value) in tags {
46
4
        SetSplitTag::new()
47
4
            .user_id(h.tenant.id)
48
4
            .split_id(to_id)
49
4
            .tag_name((*name).to_string())
50
4
            .tag_value((*value).to_string())
51
4
            .run()
52
4
            .await
53
            .map_err(|e| anyhow!("{e:?}"))?;
54
    }
55
2
    Ok(())
56
2
}
57

            
58
1
async fn seed(h: &Harness) -> Result<()> {
59
1
    SetAccountTag::new()
60
1
        .user_id(h.tenant.id)
61
1
        .account_id(h.account("Cafe"))
62
1
        .tag_name("type".to_string())
63
1
        .tag_value("expense".to_string())
64
1
        .run()
65
1
        .await
66
        .map_err(|e| anyhow!("{e:?}"))?;
67
1
    spend(h, 600, &[("category", "Rent"), ("kind", "Zed")]).await?;
68
1
    spend(h, 300, &[("category", "Fun"), ("kind", "Abe")]).await
69
1
}
70

            
71
2
async fn generate(h: &Harness) -> Result<()> {
72
2
    let button = h
73
2
        .driver
74
2
        .find("form.nomi-form button[type=\"submit\"]")
75
2
        .await?;
76
2
    h.driver.click(&button).await
77
2
}
78

            
79
1
async fn names(h: &Harness) -> Result<serde_json::Value> {
80
1
    h.driver
81
1
        .run(
82
1
            "return [...document.querySelectorAll(arguments[0])].map(c => c.textContent.trim());",
83
1
            vec![json!(FIRST_CELLS)],
84
1
        )
85
1
        .await
86
1
}
87

            
88
3
async fn wait_names(h: &Harness, want: &serde_json::Value, what: &str) -> Result<()> {
89
3
    wait_for(
90
3
        &h.driver,
91
3
        "const got = [...document.querySelectorAll(arguments[0])].map(c => c.textContent.trim());\
92
3
         return JSON.stringify(got) === JSON.stringify(arguments[1]) ? true : null;",
93
3
        vec![json!(FIRST_CELLS), want.clone()],
94
3
        what,
95
3
    )
96
3
    .await
97
3
    .map(drop)
98
3
}
99

            
100
1
async fn kept(h: &Harness) -> Result<serde_json::Value> {
101
1
    h.driver
102
1
        .run(
103
1
            "return [document.querySelector('select[name=\"sort\"]').value,\
104
1
             document.querySelector('input[name=\"tag\"]').value,\
105
1
             document.querySelector('.report-data table.report-table th').textContent.trim()];",
106
1
            vec![],
107
1
        )
108
1
        .await
109
1
}
110

            
111
1
async fn change_and_generate(h: &Harness) -> Result<()> {
112
1
    seed(h).await?;
113
1
    login(h).await?;
114
1
    open(h, "/report/category-breakdown").await?;
115
1
    wait_names(h, &json!(["Rent", "Fun"]), "the default order").await?;
116

            
117
1
    choose(h, "sort", "name_asc").await?;
118
1
    generate(h).await?;
119
1
    wait_names(h, &json!(["Fun", "Rent"]), "the name order").await?;
120

            
121
1
    set_text(h, "tag", "kind").await?;
122
1
    generate(h).await?;
123
1
    wait_names(h, &json!(["Abe", "Zed"]), "the other tag's rows").await?;
124
1
    let after = kept(h).await?;
125
1
    ensure!(after == json!(["name_asc", "kind", "kind"]), "kept {after}");
126
1
    ensure!(names(h).await? == json!(["Abe", "Zed"]), "rows");
127
1
    Ok(())
128
1
}
129

            
130
#[supp_macro::shared_runtime_test]
131
1
async fn changing_the_sort_and_tag_regenerates_the_category_table() -> Result<()> {
132
1
    with_harness(|h| Box::pin(change_and_generate(h))).await
133
}