1
//! Transaction delete: confirm dispatches `delete-transaction`; the reply
2
//! refreshes the list or reports the failure.
3

            
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::modal::{ConfirmAction, Modal};
7
use crate::route::{Route, RouteCtx};
8
use crate::tabs::fetch::Fetch;
9
use crate::view::{Tab, ViewId};
10
use cli_core::render::ListRow;
11

            
12
const UUID: &str = "550e8400-e29b-41d4-a716-446655440016";
13

            
14
1
fn delete_route(app: &App) -> Route {
15
1
    Route {
16
1
        target: Some(app.window_of(ViewId::Transactions)),
17
1
        ctx: RouteCtx::Mutation {
18
1
            refresh: ViewId::Transactions,
19
1
        },
20
1
    }
21
1
}
22

            
23
#[tokio::test]
24
1
async fn confirm_yes_dispatches_delete_transaction_and_success_reloads_the_list() {
25
1
    let mut app = crate::test_support::app();
26
1
    let sent = app.attach_echo();
27
1
    app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
28
1
        id: Some(UUID.to_string()),
29
1
        cells: vec!["2024-06-01".to_string()],
30
1
    }]);
31
1
    app.switch_tab(Tab::Transactions);
32
1
    app.open_dialog_on_front(Modal::Confirm {
33
1
        focus: None,
34
1
        prompt: format!("Delete {UUID}?"),
35
1
        action: ConfirmAction::DeleteTransaction(UUID.to_string()),
36
1
    });
37

            
38
1
    apply(&mut app, Intent::ConfirmYes);
39
1
    assert_eq!(app.pending_routes.len(), 1, "the delete is in flight");
40
1
    tokio::task::yield_now().await;
41
1
    app.drain_eval();
42
1
    let echoed = sent.frames().join("\n");
43
1
    assert!(
44
1
        echoed.contains(&format!(r#"(delete-transaction "{UUID}")"#)),
45
        "frame: {echoed}"
46
    );
47

            
48
1
    let id = *app.pending_routes.keys().next().expect("route");
49
1
    app.route_reply(id, &format!(r#"(:id {id} :value "t")"#));
50
1
    assert!(
51
1
        matches!(app.transactions().state, Fetch::Loading { .. }),
52
1
        "a successful delete reloads the transactions list"
53
1
    );
54
1
}
55

            
56
#[test]
57
1
fn a_rejected_delete_reports_the_failure() {
58
1
    let mut app = crate::test_support::app();
59
1
    app.pending_routes.insert(7, delete_route(&app));
60
1
    app.route_reply(
61
1
        7,
62
1
        r#"(:id 7 :error (:code not-found :message "Transaction not found"))"#,
63
1
    );
64
1
    assert!(
65
1
        app.status.contains("Transaction not found"),
66
        "{}",
67
        app.status
68
    );
69
1
}
70

            
71
2
async fn single_delete_of_marked_row() -> (crate::app::App, i64) {
72
2
    let mut app = crate::test_support::app();
73
2
    app.attach_echo();
74
2
    app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
75
2
        id: Some(UUID.to_string()),
76
2
        cells: vec!["2024-06-01".to_string()],
77
2
    }]);
78
2
    app.switch_tab(Tab::Transactions);
79
2
    assert!(app.transactions_mut().toggle_mark());
80
2
    app.open_dialog_on_front(Modal::Confirm {
81
2
        focus: None,
82
2
        prompt: format!("Delete {UUID}?"),
83
2
        action: ConfirmAction::DeleteTransaction(UUID.to_string()),
84
2
    });
85
2
    apply(&mut app, Intent::ConfirmYes);
86
2
    let id = *app.pending_routes.keys().next().expect("route");
87
2
    (app, id)
88
2
}
89

            
90
#[tokio::test]
91
1
async fn a_failed_single_delete_keeps_the_mark() {
92
1
    let (mut app, id) = single_delete_of_marked_row().await;
93
1
    assert_eq!(
94
1
        app.transactions().marked_ids(),
95
1
        [UUID],
96
        "kept while in flight"
97
    );
98
1
    app.route_reply(
99
1
        id,
100
1
        &format!(r#"(:id {id} :error (:code busy :message "try later"))"#),
101
1
    );
102
1
    assert_eq!(app.transactions().marked_ids(), [UUID]);
103
1
}
104

            
105
#[tokio::test]
106
1
async fn a_successful_single_delete_drops_the_mark() {
107
1
    let (mut app, id) = single_delete_of_marked_row().await;
108
1
    app.route_reply(id, &format!("(:id {id} :value t)"));
109
1
    assert!(app.transactions().marked_ids().is_empty());
110
1
}