1
//! `m` on the Accounts tab and the `account merge` palette leaf open the
2
//! merge form with `from` prefilled and fetch only the account options.
3

            
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::form::{Form, FormKind};
7
use crate::modal::Modal;
8
use crate::tabs::fetch::Fetch;
9
use crate::tabs::nms_eval::double::Sent;
10
use crate::test_support::{key, press};
11
use crate::view::Tab;
12
use cli_core::render::ListRow;
13
use ratatui::crossterm::event::KeyCode;
14

            
15
const ACCOUNT: &str = "550e8400-e29b-41d4-a716-446655440000";
16
const OTHER: &str = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
17

            
18
17
fn accounts_app() -> App {
19
17
    let mut app = crate::test_support::app();
20
17
    app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
21
17
        id: Some(ACCOUNT.to_string()),
22
17
        cells: vec!["Assets:Cash".to_string()],
23
17
    }]);
24
17
    app.switch_tab(Tab::Accounts);
25
17
    app
26
17
}
27

            
28
11
fn top_form(app: &App) -> &Form {
29
11
    match app.overlays.top() {
30
11
        Some(Modal::Form(form)) => form,
31
        other => panic!("expected a form, got {other:?}"),
32
    }
33
11
}
34

            
35
14
fn field_value<'a>(form: &'a Form, key: &str) -> &'a str {
36
14
    form.fields
37
14
        .iter()
38
25
        .find(|field| field.key == Some(key))
39
14
        .map_or("", |field| field.widget.value())
40
14
}
41

            
42
2
fn options_reply(id: i64) -> String {
43
2
    format!(
44
2
        r#"(:id {id} :value "((:id \"{OTHER}\" :label \"Assets:Bank\" :alias \"x\") (:id \"{ACCOUNT}\" :label \"Assets:Cash\" :alias \"y\"))")"#
45
    )
46
2
}
47

            
48
13
fn run_palette(app: &mut App, line: &str) {
49
13
    super::super::palette_run::submit_palette(app, line);
50
13
}
51

            
52
14
fn echo_app() -> (App, Sent) {
53
14
    let mut app = accounts_app();
54
14
    let sent = app.attach_echo();
55
14
    (app, sent)
56
14
}
57

            
58
12
fn reply_with(app: &mut App, options: &[(&str, &str)]) {
59
12
    let list = options
60
12
        .iter()
61
22
        .map(|(id, label)| format!(r#"(:id \"{id}\" :label \"{label}\" :alias \"x\")"#))
62
        .collect::<Vec<_>>()
63
12
        .join(" ");
64
12
    app.route_reply(1, &format!(r#"(:id 1 :value "({list})")"#));
65
12
}
66

            
67
4
async fn merge_via_palette(line: &str) -> App {
68
4
    let (mut app, _) = echo_app();
69
4
    app.transactions_mut().state = Fetch::Loaded(Vec::new());
70
4
    app.switch_tab(Tab::Transactions);
71
4
    run_palette(&mut app, line);
72
4
    reply_with(
73
4
        &mut app,
74
4
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
75
4
    );
76
4
    app
77
4
}
78

            
79
#[tokio::test]
80
1
async fn m_on_the_accounts_tab_opens_the_merge_form_from_the_selection() {
81
1
    let (mut app, _) = echo_app();
82
1
    press(&mut app, key(KeyCode::Char('m')));
83
1
    reply_with(
84
1
        &mut app,
85
1
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
86
1
    );
87
1
    let form = top_form(&app);
88
1
    assert_eq!(form.kind, FormKind::AccountMerge);
89
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
90
1
}
91

            
92
#[test]
93
1
fn m_is_not_bound_on_other_tabs() {
94
1
    let mut app = accounts_app();
95
1
    app.switch_tab(Tab::Commodities);
96
1
    press(&mut app, key(KeyCode::Char('m')));
97
1
    assert!(app.overlays.is_empty());
98
1
    assert!(app.status.is_empty());
99
1
}
100

            
101
#[tokio::test]
102
1
async fn merge_from_the_selection_prefills_from() {
103
1
    let (mut app, _) = echo_app();
104
1
    apply(&mut app, Intent::ListMergeAccount);
105
1
    reply_with(
106
1
        &mut app,
107
1
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
108
1
    );
109
1
    let form = top_form(&app);
110
1
    assert_eq!(form.kind, FormKind::AccountMerge);
111
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
112
1
    assert_eq!(field_value(form, "into"), "");
113
1
}
114

            
115
#[tokio::test]
116
1
async fn the_prefill_survives_the_options_reply() {
117
1
    let mut app = accounts_app();
118
1
    app.attach_echo();
119
1
    apply(&mut app, Intent::ListMergeAccount);
120
1
    app.route_reply(1, &options_reply(1));
121
1
    let form = top_form(&app);
122
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
123
1
    assert_eq!(form.select_label("from"), Some("Assets:Cash"));
124
1
}
125

            
126
#[test]
127
1
fn nothing_selected_sets_the_status() {
128
1
    let mut app = crate::test_support::app();
129
1
    app.switch_tab(Tab::Accounts);
130
1
    apply(&mut app, Intent::ListMergeAccount);
131
1
    assert!(app.overlays.is_empty());
132
1
    assert_eq!(app.status, "no account selected");
133
1
}
134

            
135
#[tokio::test]
136
1
async fn the_merge_form_fetches_only_the_accounts() {
137
1
    let mut app = accounts_app();
138
1
    let sent = app.attach_echo();
139
1
    apply(&mut app, Intent::ListMergeAccount);
140
1
    tokio::task::yield_now().await;
141
1
    app.drain_eval();
142
1
    let frames = sent.frames().join("\n");
143
1
    assert!(frames.contains(r#"(form-options "accounts")"#), "{frames}");
144
1
    assert!(!frames.contains("commodities"), "{frames}");
145
1
}
146

            
147
#[tokio::test]
148
1
async fn the_palette_leaf_without_args_uses_the_selected_account() {
149
1
    let (mut app, _) = echo_app();
150
1
    run_palette(&mut app, "account merge");
151
1
    reply_with(
152
1
        &mut app,
153
1
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
154
1
    );
155
1
    assert_eq!(field_value(top_form(&app), "from"), ACCOUNT);
156
1
}
157

            
158
#[tokio::test]
159
1
async fn the_palette_leaf_without_args_is_blank_off_the_accounts_tab() {
160
1
    let (mut app, _) = echo_app();
161
1
    app.transactions_mut().state = Fetch::Loaded(Vec::new());
162
1
    app.switch_tab(Tab::Transactions);
163
1
    run_palette(&mut app, "account merge");
164
1
    reply_with(
165
1
        &mut app,
166
1
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
167
1
    );
168
1
    let form = top_form(&app);
169
1
    assert_eq!(form.kind, FormKind::AccountMerge);
170
1
    assert_eq!(field_value(form, "from"), "");
171
1
}
172

            
173
#[tokio::test]
174
1
async fn a_label_resolves_to_its_account() {
175
1
    let app = merge_via_palette("account merge from=Assets:Cash").await;
176
1
    let form = top_form(&app);
177
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
178
1
    assert_eq!(form.select_label("from"), Some("Assets:Cash"));
179
1
}
180

            
181
#[tokio::test]
182
1
async fn an_uppercase_uuid_resolves() {
183
1
    let line = format!("account merge from={}", OTHER.to_uppercase());
184
1
    let app = merge_via_palette(&line).await;
185
1
    assert_eq!(field_value(top_form(&app), "from"), OTHER);
186
1
}
187

            
188
#[tokio::test]
189
1
async fn into_is_prefilled_too() {
190
1
    let app = merge_via_palette("account merge from=Assets:Cash into=Assets:Bank").await;
191
1
    let form = top_form(&app);
192
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
193
1
    assert_eq!(field_value(form, "into"), OTHER);
194
1
}
195

            
196
#[tokio::test]
197
1
async fn into_alone_keeps_the_selection_as_from_on_the_accounts_tab() {
198
1
    let (mut app, _) = echo_app();
199
1
    run_palette(&mut app, "account merge into=Assets:Bank");
200
1
    reply_with(
201
1
        &mut app,
202
1
        &[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
203
1
    );
204
1
    let form = top_form(&app);
205
1
    assert_eq!(field_value(form, "from"), ACCOUNT);
206
1
    assert_eq!(field_value(form, "into"), OTHER);
207
1
}
208

            
209
#[tokio::test]
210
1
async fn an_unknown_label_is_a_command_error_and_opens_nothing() {
211
1
    let app = merge_via_palette("account merge from=Assets:Nowhere").await;
212
1
    assert!(app.overlays.is_empty());
213
1
    assert!(app.status.starts_with("command error:"), "{}", app.status);
214
1
    assert!(app.status.contains("Assets:Nowhere"), "{}", app.status);
215
1
}
216

            
217
#[tokio::test]
218
1
async fn an_ambiguous_label_is_a_command_error_and_opens_nothing() {
219
1
    let (mut app, _) = echo_app();
220
1
    run_palette(&mut app, "account merge from=Assets:Cash");
221
1
    reply_with(
222
1
        &mut app,
223
1
        &[(OTHER, "Assets:Cash"), (ACCOUNT, "Assets:Cash")],
224
1
    );
225
1
    assert!(app.overlays.is_empty());
226
1
    assert!(app.status.contains("ambiguous"), "{}", app.status);
227
1
}
228

            
229
#[tokio::test]
230
1
async fn an_unknown_key_is_a_command_error_and_fetches_nothing() {
231
1
    let (mut app, _) = echo_app();
232
1
    run_palette(&mut app, "account merge form=Assets:Cash");
233
1
    app.drain_eval();
234
1
    assert!(app.overlays.is_empty());
235
1
    assert_eq!(
236
1
        app.status,
237
1
        "command error: Arguments error: unknown arg: form"
238
    );
239
1
    assert!(app.console().scrollback.is_empty());
240
1
}
241

            
242
#[tokio::test]
243
1
async fn a_duplicate_key_is_a_command_error() {
244
1
    let (mut app, _) = echo_app();
245
1
    run_palette(&mut app, "account merge from=Assets:Cash from=Assets:Bank");
246
1
    assert!(app.overlays.is_empty());
247
1
    assert_eq!(
248
1
        app.status,
249
1
        "command error: Arguments error: duplicate arg: from"
250
1
    );
251
1
}
252

            
253
#[tokio::test]
254
1
async fn a_stale_options_reply_is_dropped() {
255
1
    let (mut app, _) = echo_app();
256
1
    run_palette(&mut app, "account merge from=Assets:Cash");
257
1
    run_palette(&mut app, "account merge from=Assets:Bank");
258
1
    reply_with(&mut app, &[(ACCOUNT, "Assets:Cash")]);
259
1
    assert!(app.overlays.is_empty());
260
1
    app.route_reply(2, &options_reply(2));
261
1
    assert_eq!(field_value(top_form(&app), "from"), OTHER);
262
1
}
263

            
264
#[tokio::test]
265
1
async fn an_unrelated_options_fetch_does_not_drop_the_merge_lookup() {
266
1
    let (mut app, _) = echo_app();
267
1
    run_palette(&mut app, "account merge from=Assets:Cash");
268
1
    app.fetch_account_form_options();
269
1
    reply_with(&mut app, &[(ACCOUNT, "Assets:Cash")]);
270
1
    assert_eq!(field_value(top_form(&app), "from"), ACCOUNT);
271
1
}