1
//! Tests for DeleteRow/EditRow intents and the Confirm overlay flow.
2

            
3
use crate::event::{Intent, apply};
4
use crate::form::Form;
5
use crate::modal::{ConfirmAction, Modal};
6
use crate::tabs::fetch::Fetch;
7
use crate::view::Tab;
8
use cli_core::render::ListRow;
9

            
10
/// Wire frame that yields a single transaction row with the given uuid.
11
3
fn transactions_wire(reply_id: u64, tx_uuid: &str) -> String {
12
3
    format!(
13
3
        r#"(:id {reply_id} :value (:ROWS ((:ID "{tx_uuid}" :DATE "2026-01-01T00:00:00Z" :DATE-TEXT "2026-01-01 00:00" :NOTE "test" :AMOUNT "5")) :HAS-MORE nil :TOTAL 1))"#
14
    )
15
3
}
16

            
17
/// Wire frame that yields a single account row with the given uuid.
18
2
fn accounts_wire(reply_id: u64, uuid: &str) -> String {
19
2
    format!(r#"(:id {reply_id} :value ((:account :id "{uuid}" :name "Cash" :parent "")))"#)
20
2
}
21

            
22
2
fn load_transactions(app: &mut crate::app::App, uuid: &str) {
23
2
    app.transactions_mut().on_reply(&transactions_wire(1, uuid));
24
2
}
25

            
26
2
fn load_accounts(app: &mut crate::app::App, uuid: &str) {
27
2
    app.accounts_mut().on_reply(&accounts_wire(2, uuid));
28
2
}
29

            
30
#[test]
31
1
fn list_delete_on_transactions_with_selected_id_pushes_confirm() {
32
1
    let uuid = "550e8400-e29b-41d4-a716-446655440010";
33
1
    let mut app = crate::test_support::app();
34
1
    app.switch_tab(Tab::Transactions);
35
1
    load_transactions(&mut app, uuid);
36

            
37
1
    apply(&mut app, Intent::DeleteRow);
38

            
39
1
    let modal = app.overlays.top();
40
1
    assert!(
41
1
        matches!(modal, Some(Modal::Confirm { prompt, action: ConfirmAction::DeleteTransaction(id), .. })
42
1
            if id == uuid && prompt.contains(uuid)),
43
        "ListDelete must push a Confirm modal with the selected transaction id"
44
    );
45
1
}
46

            
47
#[test]
48
1
fn list_delete_without_loaded_list_does_nothing() {
49
1
    let mut app = crate::test_support::app();
50
1
    app.switch_tab(Tab::Transactions);
51
    // transactions is Idle — no selected id
52
1
    apply(&mut app, Intent::DeleteRow);
53
1
    assert!(
54
1
        app.overlays.is_empty(),
55
        "no confirm pushed when no row loaded"
56
    );
57
1
}
58

            
59
#[test]
60
1
fn list_delete_on_non_transactions_tab_does_nothing() {
61
1
    let uuid = "550e8400-e29b-41d4-a716-446655440011";
62
1
    let mut app = crate::test_support::app();
63
1
    app.switch_tab(Tab::Accounts);
64
    // Even with a loaded account list, d on Accounts must not trigger delete.
65
1
    load_accounts(&mut app, uuid);
66
1
    apply(&mut app, Intent::DeleteRow);
67
1
    assert!(
68
1
        app.overlays.is_empty(),
69
        "ListDelete on Accounts must not push a confirm"
70
    );
71
1
}
72

            
73
#[test]
74
1
fn confirm_cancel_esc_pops_overlay_without_dispatch() {
75
1
    let uuid = "550e8400-e29b-41d4-a716-446655440012";
76
1
    let mut app = crate::test_support::app();
77
1
    app.open_dialog_on_front(Modal::Confirm {
78
1
        focus: None,
79
1
        prompt: format!("Delete {uuid}?"),
80
1
        action: ConfirmAction::DeleteTransaction(uuid.to_string()),
81
1
    });
82

            
83
1
    apply(&mut app, Intent::CloseTopmost);
84

            
85
1
    assert!(app.overlays.is_empty(), "Esc must pop the confirm overlay");
86
    // No eval was dispatched (no eval worker attached), so status stays empty.
87
1
    assert!(app.status.is_empty(), "no side-effect on cancel");
88
1
}
89

            
90
#[test]
91
1
fn confirm_cancel_via_close_topmost_leaves_no_overlay() {
92
1
    let uuid = "550e8400-e29b-41d4-a716-446655440013";
93
1
    let mut app = crate::test_support::app();
94
1
    app.open_dialog_on_front(Modal::Confirm {
95
1
        focus: None,
96
1
        prompt: "Delete?".to_string(),
97
1
        action: ConfirmAction::DeleteTransaction(uuid.to_string()),
98
1
    });
99
1
    apply(&mut app, Intent::CloseTopmost);
100
1
    assert!(app.overlays.is_empty());
101
1
}
102

            
103
#[test]
104
1
fn confirm_yes_pops_overlay_and_sets_not_connected_status() {
105
    // With no eval worker attached, ConfirmYes removes the modal and sets an
106
    // error status — proving the dispatch path was taken rather than skipped.
107
1
    let uuid = "550e8400-e29b-41d4-a716-446655440014";
108
1
    let mut app = crate::test_support::app();
109
1
    app.open_dialog_on_front(Modal::Confirm {
110
1
        focus: None,
111
1
        prompt: format!("Delete {uuid}?"),
112
1
        action: ConfirmAction::DeleteTransaction(uuid.to_string()),
113
1
    });
114

            
115
1
    apply(&mut app, Intent::ConfirmYes);
116

            
117
1
    assert!(
118
1
        app.overlays.is_empty(),
119
        "ConfirmYes must pop the confirm modal"
120
    );
121
1
    assert!(
122
1
        app.status.contains("not connected"),
123
        "eval dispatch must have been attempted, status: {}",
124
        app.status
125
    );
126
1
}
127

            
128
#[test]
129
1
fn press_confirms_a_delete_prompt() {
130
1
    let uuid = "550e8400-e29b-41d4-a716-446655440015";
131
1
    let mut app = crate::test_support::app();
132
1
    app.open_dialog_on_front(Modal::Confirm {
133
1
        focus: None,
134
1
        prompt: format!("Delete {uuid}?"),
135
1
        action: ConfirmAction::DeleteTransaction(uuid.to_string()),
136
1
    });
137

            
138
1
    apply(&mut app, Intent::Press);
139

            
140
1
    assert!(app.overlays.is_empty());
141
1
    assert_eq!(app.status, "console not connected");
142
1
}
143

            
144
#[test]
145
1
fn press_on_the_discard_prompt_keeps_editing() {
146
1
    let mut app = crate::test_support::app();
147
1
    app.open_dialog_on_front(Modal::Form(Form::commodity_create()));
148
1
    app.open_dialog_on_front(Modal::Confirm {
149
1
        focus: None,
150
1
        prompt: "Discard changes?".to_string(),
151
1
        action: ConfirmAction::DiscardForm,
152
1
    });
153

            
154
1
    apply(&mut app, Intent::Press);
155

            
156
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
157
1
    assert!(app.status.is_empty());
158
1
}
159

            
160
#[test]
161
1
fn confirm_yes_with_form_overlay_does_not_submit() {
162
    // A Form on top must be untouched by ConfirmYes: no pop, no eval, no status.
163
1
    let mut app = crate::test_support::app();
164
1
    app.open_dialog_on_front(Modal::Form(Form::commodity_create()));
165

            
166
1
    apply(&mut app, Intent::ConfirmYes);
167

            
168
1
    assert!(
169
1
        matches!(app.overlays.top(), Some(Modal::Form(_))),
170
        "ConfirmYes must leave a Form overlay in place"
171
    );
172
1
    assert!(
173
1
        app.status.is_empty(),
174
        "ConfirmYes on a Form must not set status, got: {}",
175
        app.status
176
    );
177
1
}
178

            
179
#[test]
180
1
fn confirm_yes_no_eval_keeps_console_not_connected_status() {
181
    // submit_delete_transaction owns the failure status; execute_confirm must
182
    // not clobber the precise "console not connected" message.
183
1
    let uuid = "550e8400-e29b-41d4-a716-446655440099";
184
1
    let mut app = crate::test_support::app();
185
1
    app.open_dialog_on_front(Modal::Confirm {
186
1
        focus: None,
187
1
        prompt: format!("Delete {uuid}?"),
188
1
        action: ConfirmAction::DeleteTransaction(uuid.to_string()),
189
1
    });
190

            
191
1
    apply(&mut app, Intent::ConfirmYes);
192

            
193
1
    assert!(app.overlays.is_empty(), "ConfirmYes must pop the confirm");
194
1
    assert_eq!(
195
1
        app.status, "console not connected",
196
        "precise failure status must survive"
197
    );
198
1
}
199

            
200
#[test]
201
1
fn list_edit_on_transactions_dispatches_edit_open() {
202
    // EditRow on the Transactions tab now opens the transaction-edit form
203
    // (asynchronously, via load-form). With no eval worker attached,
204
    // the attempt sets "console not connected" instead of pushing a form.
205
1
    let uuid = "550e8400-e29b-41d4-a716-446655440015";
206
1
    let mut app = crate::test_support::app();
207
1
    app.switch_tab(Tab::Transactions);
208
1
    load_transactions(&mut app, uuid);
209

            
210
1
    apply(&mut app, Intent::EditRow);
211

            
212
1
    assert!(
213
1
        app.overlays.is_empty(),
214
        "no form pushed synchronously (edit open is async)"
215
    );
216
1
    assert_eq!(
217
1
        app.status, "console not connected",
218
        "dispatch attempt sets status when no eval worker"
219
    );
220
1
}
221

            
222
#[tokio::test]
223
1
async fn list_edit_on_accounts_loads_the_selected_account() {
224
1
    let uuid = "550e8400-e29b-41d4-a716-446655440016";
225
1
    let mut app = crate::test_support::app();
226
1
    let sent = app.attach_echo();
227
1
    app.switch_tab(Tab::Accounts);
228
1
    load_accounts(&mut app, uuid);
229

            
230
1
    apply(&mut app, Intent::EditRow);
231

            
232
1
    tokio::task::yield_now().await;
233
1
    app.drain_eval();
234
1
    let echoed = sent.frames().join("\n");
235
1
    assert!(
236
1
        echoed.contains(&format!(r#"(load-form "account-edit" "{uuid}")"#)),
237
1
        "frame: {echoed}"
238
1
    );
239
1
}
240

            
241
#[test]
242
1
fn list_edit_without_loaded_list_sets_status() {
243
1
    let mut app = crate::test_support::app();
244
1
    app.switch_tab(Tab::Accounts);
245
    // accounts is Idle — no selected id
246
1
    apply(&mut app, Intent::EditRow);
247
1
    assert!(
248
1
        app.overlays.is_empty(),
249
        "no form pushed when no account loaded"
250
    );
251
1
    assert!(
252
1
        app.status.contains("no account"),
253
        "status must indicate no selection, got: {}",
254
        app.status
255
    );
256
1
}
257

            
258
// ── Listing row selection helpers ───────────────────────────────────────────
259

            
260
#[test]
261
1
fn selected_id_returns_none_when_list_idle() {
262
1
    let mut app = crate::test_support::app();
263
1
    app.switch_tab(Tab::Transactions);
264
1
    assert!(app.transactions().selected_id().is_none());
265
1
}
266

            
267
#[test]
268
1
fn selected_id_returns_uuid_when_row_loaded() {
269
1
    let uuid = "550e8400-e29b-41d4-a716-446655440017";
270
1
    let mut app = crate::test_support::app();
271
1
    app.transactions_mut().on_reply(&transactions_wire(1, uuid));
272
1
    assert_eq!(app.transactions().selected_id(), Some(uuid));
273
1
}
274

            
275
// ── Manually inject a row without wire parsing ──────────────────────────────
276

            
277
#[test]
278
1
fn loaded_list_tab_with_no_id_does_not_push_confirm() {
279
1
    let mut app = crate::test_support::app();
280
1
    app.switch_tab(Tab::Transactions);
281
    // Row without an id
282
1
    app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
283
1
        id: None,
284
1
        cells: vec!["no-id-row".to_string()],
285
1
    }]);
286
1
    apply(&mut app, Intent::DeleteRow);
287
1
    assert!(
288
1
        app.overlays.is_empty(),
289
        "no confirm pushed when selected row has no id"
290
    );
291
1
}
292

            
293
#[test]
294
1
fn the_confirm_constructor_builds_the_box_and_y_runs_its_action() {
295
1
    let mut app = crate::test_support::app();
296
1
    app.overlays
297
1
        .push(Modal::confirm("Quit?", ConfirmAction::Quit));
298
1
    assert!(matches!(
299
1
        app.overlays.top(),
300
1
        Some(Modal::Confirm { prompt, focus: None, .. }) if prompt == "Quit?"
301
    ));
302
1
    apply(&mut app, Intent::ConfirmYes);
303
1
    assert!(app.should_quit);
304
1
    assert!(app.overlays.is_empty());
305
1
}
306

            
307
#[test]
308
1
fn n_closes_the_confirm_and_runs_nothing() {
309
1
    let mut app = crate::test_support::app();
310
1
    app.overlays
311
1
        .push(Modal::confirm("Quit?", ConfirmAction::Quit));
312
1
    apply(&mut app, Intent::CloseTopmost);
313
1
    assert!(app.overlays.is_empty());
314
1
    assert!(!app.should_quit);
315
1
}