1
//! The lazy row detail and delete of the list page (plan 0016 W7).
2

            
3
use axum::http::{StatusCode, header};
4

            
5
use crate::common::create_mock_user;
6
use crate::common::list_page::{add, delete, detail, list};
7
use crate::common::new_page::seeded_user;
8
use crate::common::seed::{commodity_with_name, named_account};
9
use crate::common::tenant::provision_tenant;
10

            
11
#[supp_macro::shared_runtime_test]
12
1
async fn the_list_carries_no_splits_but_a_lazy_detail_link() {
13
1
    let user = seeded_user().await;
14
1
    let id = add(
15
1
        &user,
16
1
        "2020-01-10T10:00:00Z",
17
1
        "lunch",
18
1
        "Expenses:Food",
19
1
        "12.5",
20
1
    )
21
1
    .await;
22
1
    let page = list(&user, "").await;
23

            
24
1
    let body = &page.body;
25
1
    assert!(!body.contains("<table class=\"splits-table\">"), "{body}");
26
1
    assert_eq!(body.matches("hx-trigger=\"toggle once\"").count(), 1);
27
1
    assert!(body.contains(&format!("hx-get=\"/api/transaction/detail/{id}\"")));
28
1
    assert!(body.contains(&format!("href=\"/transaction/{id}/edit\"")));
29
1
    assert!(body.contains("12.5"), "{body}");
30
}
31

            
32
#[supp_macro::shared_runtime_test]
33
1
async fn the_detail_fragment_shows_splits_with_exact_amounts_and_tags() {
34
1
    let user = seeded_user().await;
35
1
    let id = add(
36
1
        &user,
37
1
        "2020-01-10T10:00:00Z",
38
1
        "lunch",
39
1
        "Expenses:Food",
40
1
        "1/3",
41
1
    )
42
1
    .await;
43
1
    let reply = detail(&user, &id.to_string()).await;
44

            
45
1
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
46
1
    let body = &reply.body;
47
1
    assert!(body.contains("Assets:Checking"), "{body}");
48
1
    assert!(body.contains("Expenses:Food"), "{body}");
49
1
    assert_eq!(body.matches("1/3").count(), 2, "{body}");
50
1
    assert_eq!(body.matches("USD").count(), 2, "{body}");
51
1
    assert!(body.contains("memo: lunch"), "{body}");
52
1
    assert!(!body.contains("<html"), "{body}");
53
}
54

            
55
#[supp_macro::shared_runtime_test]
56
1
async fn the_detail_of_a_missing_or_foreign_id_is_not_found() {
57
1
    let owner = seeded_user().await;
58
1
    let stranger = seeded_user().await;
59
1
    let id = add(
60
1
        &owner,
61
1
        "2020-01-10T10:00:00Z",
62
1
        "lunch",
63
1
        "Expenses:Food",
64
1
        "5",
65
1
    )
66
1
    .await;
67
1
    let foreign = detail(&stranger, &id.to_string()).await;
68
1
    let missing = detail(&owner, &uuid::Uuid::new_v4().to_string()).await;
69
1
    let malformed = detail(&owner, "nope").await;
70

            
71
1
    assert_eq!(foreign.status, StatusCode::NOT_FOUND);
72
1
    assert_eq!(missing.status, StatusCode::NOT_FOUND);
73
1
    assert_eq!(malformed.status, StatusCode::BAD_REQUEST);
74
}
75

            
76
#[supp_macro::shared_runtime_test]
77
1
async fn a_listed_delete_asks_first_posts_and_redirects_back() {
78
1
    let user = seeded_user().await;
79
1
    let id = add(&user, "2020-01-10T10:00:00Z", "lunch", "Expenses:Food", "5").await;
80
1
    let page = list(&user, "").await;
81
1
    let removed = delete(&user, &id.to_string()).await;
82
1
    let after = list(&user, "").await;
83
1
    let again = delete(&user, &id.to_string()).await;
84

            
85
1
    let form = page
86
1
        .body
87
1
        .split("class=\"delete-form\"")
88
1
        .nth(1)
89
1
        .and_then(|tail| tail.split("</form>").next())
90
1
        .expect("a delete form");
91
1
    assert!(form.contains("method=\"post\""), "{form}");
92
1
    assert!(form.contains(&format!("action=\"/api/transaction/delete/{id}\"")));
93
1
    assert!(form.contains("onsubmit=\"return confirm("), "{form}");
94
1
    assert!(removed.status.is_redirection(), "{}", removed.status);
95
1
    assert_eq!(removed.headers[header::LOCATION], "/transaction/list");
96
1
    assert!(!after.body.contains("lunch"));
97
1
    assert_eq!(again.status, StatusCode::NOT_FOUND);
98
}
99

            
100
#[supp_macro::shared_runtime_test]
101
1
async fn a_split_tag_name_with_two_values_shows_both() {
102
1
    let user = seeded_user().await;
103
1
    let id = add(&user, "2020-01-10T10:00:00Z", "lunch", "Expenses:Food", "5").await;
104
1
    let server_user = server::user::User { id: user.id };
105
1
    let splits = server_user
106
1
        .get_transaction_splits(id)
107
1
        .await
108
1
        .expect("splits");
109
1
    for split in &splits {
110
2
        server_user
111
2
            .create_split_tag(split.id, "memo".into(), "second".into(), None)
112
2
            .await
113
2
            .expect("second value");
114
    }
115
1
    let reply = detail(&user, &id.to_string()).await;
116

            
117
1
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
118
1
    assert!(reply.body.contains("memo: lunch"), "{}", reply.body);
119
1
    assert!(reply.body.contains("memo: second"), "{}", reply.body);
120
}
121

            
122
#[supp_macro::shared_runtime_test]
123
1
async fn the_amount_column_shows_the_commodity_symbol_not_its_name() {
124
1
    let user = create_mock_user();
125
1
    provision_tenant(user.id).await;
126
1
    commodity_with_name(user.id, "USD", "US Dollar").await;
127
1
    named_account(user.id, "Assets:Checking").await;
128
1
    named_account(user.id, "Expenses:Food").await;
129
1
    let id = add(&user, "2020-01-10T10:00:00Z", "lunch", "Expenses:Food", "5").await;
130
1
    let reply = detail(&user, &id.to_string()).await;
131

            
132
1
    assert!(reply.body.contains("USD"), "{}", reply.body);
133
1
    assert!(!reply.body.contains("US Dollar"), "{}", reply.body);
134
1
    assert!(reply.body.contains("Assets:Checking"), "{}", reply.body);
135
}