1
//! Every page loads clean; buttons say what they do; a refusal is a page.
2

            
3
use anyhow::{Result, ensure};
4
use serde_json::json;
5

            
6
use super::checks::check_page;
7
use super::harness::{Harness, with_harness};
8
use super::page::{
9
    Pick, add_split, add_split_tag, button_texts, login, open, pick, submit, wait_for,
10
};
11
use super::report_filter::transfer_in;
12
use super::rows::named;
13

            
14
const PAGES: &[(&str, &str)] = &[
15
    ("/transaction/list", "list"),
16
    ("/transaction/new", "new"),
17
    ("/report/activity", "report-activity"),
18
    ("/report/category-breakdown", "report-category"),
19
];
20

            
21
/// Real-sized amounts and a two-commodity balance, which a phone table that
22
/// cannot wrap them pushes past the screen.
23
1
async fn long_rows(h: &Harness) -> Result<()> {
24
1
    let (bank, suica) = (h.account("Bank"), h.account("Suica"));
25
1
    transfer_in(h, "JPY", bank, suica, 1_234_567).await?;
26
1
    transfer_in(h, "EUR", bank, suica, 98_765).await
27
1
}
28

            
29
1
async fn pages_load(h: &Harness) -> Result<()> {
30
1
    long_rows(h).await?;
31
1
    login(h).await?;
32
1
    open(h, "/").await?;
33
1
    wait_for(
34
1
        &h.driver,
35
1
        "return document.querySelectorAll('.account-status .account-row').length > 0;",
36
1
        vec![],
37
1
        "the home account table",
38
1
    )
39
1
    .await?;
40
1
    check_page(h, "home", &[]).await?;
41
1
    for (path, name) in PAGES {
42
4
        open(h, path).await?;
43
4
        check_page(h, name, &[]).await?;
44
    }
45
1
    open(h, "/transaction/list").await?;
46
1
    pick(h, "account", "Suica", named("Suica"), 0, Pick::Mouse).await?;
47
1
    let query = h
48
1
        .driver
49
1
        .find("form.nomi-form button[type=\"submit\"]")
50
1
        .await?;
51
1
    h.driver.click(&query).await?;
52
1
    let url = wait_for(
53
1
        &h.driver,
54
1
        "return location.search.includes('account=') ? location.search : null;",
55
1
        vec![],
56
1
        "the filtered list",
57
1
    )
58
1
    .await?;
59
1
    let suica = h.account("Suica").to_string();
60
1
    ensure!(
61
1
        url.as_str()
62
1
            .is_some_and(|q| q.contains(&format!("account={suica}"))),
63
        "the filter posts {url}"
64
    );
65
1
    check_page(h, "list-filtered", &[]).await
66
1
}
67

            
68
#[supp_macro::shared_runtime_test]
69
1
async fn every_page_loads_without_errors() -> Result<()> {
70
1
    with_harness(|h| Box::pin(pages_load(h))).await
71
}
72

            
73
1
async fn buttons(h: &Harness) -> Result<()> {
74
1
    login(h).await?;
75
1
    open(h, "/transaction/new").await?;
76
1
    add_split_tag(h, 0).await?;
77
1
    add_split(h).await?;
78
1
    let texts = button_texts(h).await?;
79
1
    ensure!(
80
1
        texts
81
1
            == [
82
1
                "Remove split",
83
1
                "Remove split tag",
84
1
                "Add split tag",
85
1
                "Remove split",
86
1
                "Add split tag",
87
1
                "Add tag",
88
1
                "Add split",
89
1
                "Create transaction",
90
1
            ],
91
        "create page buttons: {texts:?}"
92
    );
93
1
    let legs = h
94
1
        .driver
95
1
        .run(
96
1
            "return [...document.querySelectorAll('select[name=\"split-tags.0.side\"] option')]\
97
1
             .map(o => o.textContent.trim());",
98
1
            vec![],
99
1
        )
100
1
        .await?;
101
1
    ensure!(
102
1
        legs == json!(["Both accounts", "From account only", "To account only"]),
103
        "leg choices: {legs}"
104
    );
105
1
    let titles = h
106
1
        .driver
107
1
        .run(
108
1
            "return [...document.querySelectorAll('[data-row=\"splits\"] > label > span:first-child')]\
109
1
             .slice(0, 6).map(s => s.textContent.trim());",
110
1
            vec![],
111
1
        )
112
1
        .await?;
113
1
    ensure!(
114
1
        titles
115
1
            == json!([
116
1
                "From",
117
1
                "To",
118
1
                "From commodity",
119
1
                "Value",
120
1
                "To commodity",
121
1
                "To amount"
122
1
            ]),
123
        "split titles: {titles}"
124
    );
125
1
    check_page(h, "new-buttons", &[]).await?;
126
1
    open(h, "/transaction/list").await?;
127
1
    let texts = button_texts(h).await?;
128
1
    ensure!(
129
1
        texts == ["Show transactions"],
130
        "list filter buttons: {texts:?}"
131
    );
132
1
    Ok(())
133
1
}
134

            
135
#[supp_macro::shared_runtime_test]
136
1
async fn buttons_name_what_they_do() -> Result<()> {
137
1
    with_harness(|h| Box::pin(buttons(h))).await
138
}
139

            
140
1
async fn refusals(h: &Harness) -> Result<()> {
141
1
    login(h).await?;
142
1
    for (path, status) in [
143
1
        ("/transaction/not-a-uuid/edit", 400),
144
1
        (
145
1
            "/transaction/00000000-0000-4000-8000-000000000000/edit",
146
1
            404,
147
1
        ),
148
1
    ] {
149
2
        h.driver.goto(&h.url(path)).await?;
150
2
        let page = h
151
2
            .driver
152
2
            .run(
153
2
                "return [document.contentType, !!document.querySelector('link[href$=\"/style.css\"]'),\
154
2
                 document.body.innerText];",
155
2
                vec![],
156
2
            )
157
2
            .await?;
158
2
        ensure!(page[0] == json!("text/html"), "{path}: {page}");
159
2
        ensure!(page[1] == json!(true), "{path} is not a full page: {page}");
160
2
        check_page(h, &format!("refusal-{status}"), &[status]).await?;
161
    }
162
1
    open(h, "/transaction/new").await?;
163
1
    let reply = submit(h).await?;
164
1
    ensure!(
165
1
        reply.starts_with("INVALID: "),
166
        "an empty create was not refused: {reply}"
167
    );
168
1
    check_page(h, "new-refused", &[422]).await
169
1
}
170

            
171
#[supp_macro::shared_runtime_test]
172
1
async fn refusals_are_html_pages_and_inline_errors() -> Result<()> {
173
1
    with_harness(|h| Box::pin(refusals(h))).await
174
}