1
//! A stale bookmark with an invalid query still shows the report's form,
2
//! with the field errors, in the place the table would have loaded, and the
3
//! corrected form loads the report into the page.
4

            
5
use anyhow::{Result, ensure};
6
use serde_json::json;
7

            
8
use super::harness::{Harness, with_harness};
9
use super::page::{login, open, wait_for};
10

            
11
const GROUPS: &str = "groups.0.name=Spend&groups.0.filter=%28account-tag%3D%20%22type%22%20%22expense%22%29&groups.0.flip=yes";
12

            
13
const REPORTS: [(&str, &str); 3] = [
14
    ("/report/balance?as-of=2026-13-01", "as-of"),
15
    ("/report/activity?from=2026-13-01", "from"),
16
    ("/report/category-breakdown?from=2026-13-01", "from"),
17
];
18

            
19
4
async fn shows_form_with_errors(h: &Harness, path: &str) -> Result<()> {
20
4
    open(h, path).await?;
21
4
    wait_for(
22
4
        &h.driver,
23
4
        "return document.querySelector('.report-data .report-filters form.nomi-form .field-errors') ? true : null;",
24
4
        vec![],
25
4
        &format!("{path} to show its form with errors"),
26
4
    )
27
4
    .await
28
4
    .map(drop)
29
4
}
30

            
31
4
async fn correct_and_generate(h: &Harness, field: &str) -> Result<()> {
32
4
    h.driver
33
4
        .run(
34
4
            "const input = document.querySelector(`.report-data input[name=\"${arguments[0]}\"]`);\
35
4
             input.value = '2026-01-15';\
36
4
             input.dispatchEvent(new Event('input', { bubbles: true }));",
37
4
            vec![json!(field)],
38
4
        )
39
4
        .await?;
40
4
    let button = h
41
4
        .driver
42
4
        .find(".report-data form.nomi-form button[type=\"submit\"]")
43
4
        .await?;
44
4
    h.driver.click(&button).await
45
4
}
46

            
47
4
async fn shows_the_report_in_the_shell(h: &Harness, path: &str) -> Result<()> {
48
4
    wait_for(
49
4
        &h.driver,
50
4
        "return document.querySelector('.report-container > .report-data .report-filters .report-table-controls') \
51
4
         && !document.querySelector('.report-data form.nomi-form .field-errors') \
52
4
         && location.pathname.startsWith('/report/') ? true : null;",
53
4
        vec![],
54
4
        &format!("{path} to load its report inside the page"),
55
4
    )
56
4
    .await
57
4
    .map(drop)
58
4
}
59

            
60
1
async fn bad_queries(h: &Harness) -> Result<()> {
61
1
    login(h).await?;
62
1
    for (path, field) in REPORTS {
63
3
        shows_form_with_errors(h, path).await?;
64
3
        correct_and_generate(h, field).await?;
65
3
        shows_the_report_in_the_shell(h, path).await?;
66
    }
67
1
    Ok(())
68
1
}
69

            
70
1
async fn bad_groups(h: &Harness) -> Result<()> {
71
1
    login(h).await?;
72
1
    let path = "/report/activity?groups.0.name=A&groups.0.filter=%28and";
73
1
    open(h, path).await?;
74
1
    wait_for(
75
1
        &h.driver,
76
1
        "return document.querySelector('.report-data .activity-group-row .field-error') ? true : null;",
77
1
        vec![],
78
1
        "the bad group's row error",
79
1
    )
80
1
    .await
81
1
    .map(drop)
82
1
}
83

            
84
1
async fn groups_survive_a_bad_date(h: &Harness) -> Result<()> {
85
1
    login(h).await?;
86
1
    let path = format!("/report/activity?{GROUPS}&sort_order=name_asc&from=2026-13-01");
87
1
    shows_form_with_errors(h, &path).await?;
88
1
    correct_and_generate(h, "from").await?;
89
1
    shows_the_report_in_the_shell(h, &path).await?;
90
1
    let kept = wait_for(
91
1
        &h.driver,
92
1
        "const label = document.querySelector('h4.activity-group-label');\
93
1
         const name = document.querySelector('.activity-group-name');\
94
1
         return label && name ? [label.textContent.trim().split(/\\s+/)[0], name.value] : null;",
95
1
        vec![],
96
1
        "the group after the corrected resubmit",
97
1
    )
98
1
    .await?;
99
1
    ensure!(kept == json!(["Spend", "Spend"]), "groups kept: {kept}");
100
1
    let query = h.driver.run("return location.search;", vec![]).await?;
101
1
    ensure!(
102
1
        query
103
1
            .as_str()
104
1
            .is_some_and(|q| q.contains("sort_order=name_asc")),
105
        "sort order kept: {query}"
106
    );
107
1
    Ok(())
108
1
}
109

            
110
#[supp_macro::shared_runtime_test]
111
1
async fn an_invalid_report_query_shows_the_form_and_the_corrected_one_loads() -> Result<()> {
112
1
    with_harness(|h| Box::pin(bad_queries(h))).await
113
}
114

            
115
#[supp_macro::shared_runtime_test]
116
1
async fn a_bad_group_bookmark_shows_its_row_error() -> Result<()> {
117
1
    with_harness(|h| Box::pin(bad_groups(h))).await
118
}
119

            
120
#[supp_macro::shared_runtime_test]
121
1
async fn a_groups_bookmark_with_a_bad_date_keeps_its_groups() -> Result<()> {
122
1
    with_harness(|h| Box::pin(groups_survive_a_bad_date(h))).await
123
}