1
//! A loaded or picked cell keeps the entity it carries: retyped text the
2
//! wasm never saw is refused by the server, and tabbing through a loaded
3
//! cell after its name moved to another entity does not rebind it, even
4
//! when its text was edited and put back.
5

            
6
use std::time::Duration;
7

            
8
use anyhow::{Result, anyhow, ensure};
9
use server::command::account::SetAccountTag;
10
use server::command::{CmdResult, FinanceEntity, account::CreateAccount};
11
use uuid::Uuid;
12

            
13
use super::checks::check_page;
14
use super::driver::{BACKSPACE, TAB};
15
use super::harness::{Harness, with_harness};
16
use super::page::{Pick, login, open, pick, reference, reply_id, submit, visible};
17
use super::rows::{Split, fill_splits, named};
18
use super::store::snapshot;
19

            
20
/// Creates Suica → JR through the page and opens its edit page.
21
2
async fn edit_page(h: &Harness) -> Result<Uuid> {
22
2
    login(h).await?;
23
2
    open(h, "/transaction/new").await?;
24
2
    fill_splits(h, &[Split::plain("Suica", "JR", "199")]).await?;
25
2
    let reply = submit(h).await?;
26
2
    ensure!(reply.contains("created with ID"), "{reply}");
27
2
    let id = reply_id(&reply)?;
28
2
    open(h, &format!("/transaction/{id}/edit")).await?;
29
2
    Ok(id)
30
2
}
31

            
32
1
async fn retyped_without_wasm(h: &Harness) -> Result<()> {
33
1
    let id = edit_page(h).await?;
34
1
    let before = snapshot(h, id).await?;
35
1
    pick(h, "splits.0.to", "McD", named("McDonalds"), 0, Pick::Mouse).await?;
36
1
    h.driver
37
1
        .run(
38
1
            "document.querySelector('input[name=\"label:splits.0.to\"]').value = 'Wallet';",
39
1
            vec![],
40
1
        )
41
1
        .await?;
42
1
    let reply = submit(h).await?;
43
1
    ensure!(
44
1
        reply.contains("pick an entry from the list"),
45
        "a retyped cell was saved: {reply}"
46
    );
47
1
    let (shown, _) = reference(h, "splits.0.to").await?;
48
1
    ensure!(shown == "Wallet", "the typed text is lost: {shown:?}");
49
1
    check_page(h, "edit-retyped-refused", &[422]).await?;
50
1
    let after = snapshot(h, id).await?;
51
1
    ensure!(after == before, "the refused edit changed the transaction");
52
1
    Ok(())
53
1
}
54

            
55
#[supp_macro::shared_runtime_test]
56
1
async fn a_retyped_cell_the_wasm_never_saw_is_refused() -> Result<()> {
57
1
    with_harness(|h| Box::pin(retyped_without_wasm(h))).await
58
}
59

            
60
1
async fn rename_jr_and_reuse_its_name(h: &Harness) -> Result<Uuid> {
61
1
    SetAccountTag::new()
62
1
        .user_id(h.tenant.id)
63
1
        .account_id(h.account("JR"))
64
1
        .tag_name("name".to_string())
65
1
        .tag_value("JR East".to_string())
66
1
        .run()
67
1
        .await
68
        .map_err(|e| anyhow!("{e:?}"))?;
69
1
    let created = CreateAccount::new()
70
1
        .name("JR".to_string())
71
1
        .user_id(h.tenant.id)
72
1
        .run()
73
1
        .await
74
        .map_err(|e| anyhow!("{e:?}"))?;
75
1
    match created {
76
1
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
77
        other => Err(anyhow!("expected an account, got {other:?}")),
78
    }
79
1
}
80

            
81
1
async fn tab_through_after_reuse(h: &Harness) -> Result<()> {
82
1
    let id = edit_page(h).await?;
83
1
    let before = snapshot(h, id).await?;
84
1
    let (_, loaded) = reference(h, "splits.0.to").await?;
85
1
    let impostor = rename_jr_and_reuse_its_name(h).await?;
86
1
    let to = visible(h, "splits.0.to").await?;
87
1
    h.driver.click(&to).await?;
88
1
    h.driver
89
1
        .type_into(&to, &format!("x{BACKSPACE}{TAB}"))
90
1
        .await?;
91
1
    tokio::time::sleep(Duration::from_secs(1)).await;
92
1
    let (_, carried) = reference(h, "splits.0.to").await?;
93
1
    ensure!(
94
1
        carried == loaded,
95
        "tabbing rebound the cell from {loaded:?} to {carried:?}"
96
    );
97
1
    let stale = submit(h).await?;
98
1
    ensure!(
99
1
        stale.contains("pick an entry from the list"),
100
        "a stale label was saved: {stale}"
101
    );
102
1
    ensure!(
103
1
        snapshot(h, id).await? == before,
104
        "the refused edit changed the transaction"
105
    );
106

            
107
1
    open(h, &format!("/transaction/{id}/edit")).await?;
108
1
    let (shown, reloaded) = reference(h, "splits.0.to").await?;
109
1
    ensure!(
110
1
        shown == "JR East" && reloaded == loaded,
111
        "{shown:?} {reloaded:?}"
112
    );
113
1
    let reply = submit(h).await?;
114
1
    ensure!(reply.contains("updated with ID"), "{reply}");
115
1
    let after = snapshot(h, id).await?;
116
1
    ensure!(
117
1
        !after
118
1
            .iter()
119
3
            .any(|fact| fact.contains(&impostor.to_string())),
120
        "the edit moved the split to the account that took the name: {after:#?}"
121
    );
122
1
    ensure!(after == before, "{before:#?}\n{after:#?}");
123
1
    Ok(())
124
1
}
125

            
126
#[supp_macro::shared_runtime_test]
127
1
async fn tabbing_through_a_loaded_cell_keeps_its_entity_after_a_rename() -> Result<()> {
128
1
    with_harness(|h| Box::pin(tab_through_after_reuse(h))).await
129
}