1
//! Transaction create through `submit-form`, and `c` on the list tabs.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::form::{Form, FormKind};
6
use crate::modal::Modal;
7
use crate::tabs::fetch::Fetch;
8
use crate::test_support::{key, press};
9
use crate::view::Tab;
10
use crate::widgets::splits::{COL_FROM_COMM, COL_VALUE};
11
use crate::widgets::{FocusedSubWidget, SelectOption, Widget};
12
use cli_core::render::ListRow;
13
use ratatui::crossterm::event::KeyCode;
14

            
15
const ACCOUNT: &str = "550e8400-e29b-41d4-a716-446655440000";
16

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

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

            
34
3
fn first_from(form: &Form) -> &crate::widgets::SelectWidget {
35
3
    match &form.fields[3].widget {
36
3
        Widget::Splits(sw) => &sw.rows()[0].from,
37
        other => panic!("field 2 must be Splits, got {other:?}"),
38
    }
39
3
}
40

            
41
#[test]
42
1
fn c_on_the_accounts_tab_opens_the_transaction_form() {
43
1
    let mut app = accounts_app();
44
1
    press(&mut app, key(KeyCode::Char('c')));
45
1
    assert!(matches!(
46
1
        app.overlays.top(),
47
1
        Some(Modal::Form(form)) if form.kind == FormKind::TransactionCreate
48
    ));
49
1
}
50

            
51
#[test]
52
1
fn c_on_the_transactions_tab_opens_a_blank_transaction_form() {
53
1
    let mut app = crate::test_support::app();
54
1
    app.switch_tab(Tab::Transactions);
55
1
    press(&mut app, key(KeyCode::Char('c')));
56
1
    assert!(matches!(
57
1
        app.overlays.top(),
58
1
        Some(Modal::Form(form)) if form.kind == FormKind::TransactionCreate
59
    ));
60
1
    assert!(first_from(top_form(&app)).value().is_empty());
61
1
}
62

            
63
#[test]
64
1
fn c_on_the_commodities_tab_opens_the_commodity_form() {
65
1
    let mut app = crate::test_support::app();
66
1
    app.switch_tab(Tab::Commodities);
67
1
    press(&mut app, key(KeyCode::Char('c')));
68
1
    assert!(matches!(
69
1
        app.overlays.top(),
70
1
        Some(Modal::Form(form)) if form.kind == FormKind::CommodityCreate
71
    ));
72
1
}
73

            
74
#[test]
75
1
fn new_transaction_from_an_account_prefills_from_by_uuid() {
76
1
    let mut app = accounts_app();
77
1
    apply(&mut app, Intent::NewRow);
78
1
    let form = top_form(&app);
79
1
    assert_eq!(form.kind, crate::form::FormKind::TransactionCreate);
80
1
    assert_eq!(first_from(form).value(), ACCOUNT);
81
1
}
82

            
83
#[test]
84
1
fn the_prefilled_account_keeps_its_uuid_when_options_arrive() {
85
1
    let mut app = accounts_app();
86
1
    apply(&mut app, Intent::NewRow);
87
1
    let Some(Modal::Form(form)) = app.overlays.top_mut() else {
88
        panic!("form must be open");
89
    };
90
1
    let Widget::Splits(sw) = &mut form.fields[3].widget else {
91
        panic!("field 2 must be Splits");
92
    };
93
1
    sw.set_account_options(vec![
94
1
        SelectOption {
95
1
            id: String::new(),
96
1
            label: "(none)".to_string(),
97
1
        },
98
1
        SelectOption {
99
1
            id: ACCOUNT.to_string(),
100
1
            label: "Assets:Cash".to_string(),
101
1
        },
102
1
    ]);
103
1
    let from = first_from(top_form(&app));
104
1
    assert_eq!(from.value(), ACCOUNT);
105
1
    assert_eq!(from.display(), "Assets:Cash");
106
1
}
107

            
108
#[test]
109
1
fn new_transaction_without_a_selected_account_says_so() {
110
1
    let mut app = crate::test_support::app();
111
1
    app.switch_tab(Tab::Accounts);
112
1
    apply(&mut app, Intent::NewRow);
113
1
    assert_eq!(app.status, "no account selected");
114
1
    assert!(app.overlays.is_empty());
115
1
}
116

            
117
#[tokio::test]
118
1
async fn submit_dispatches_submit_form_with_the_descriptor_payload() {
119
1
    let mut app = crate::test_support::app();
120
1
    let sent = app.attach_echo();
121
1
    let mut form = Form::transaction_create();
122
1
    if let Widget::Splits(sw) = &mut form.fields[3].widget {
123
1
        sw.set_account_options(vec![SelectOption {
124
1
            id: "acc-1".to_string(),
125
1
            label: "Cash".to_string(),
126
1
        }]);
127
1
        sw.set_commodity_options(vec![SelectOption {
128
1
            id: "c-1".to_string(),
129
1
            label: "USD".to_string(),
130
1
        }]);
131
1
        sw.col_focus = COL_FROM_COMM;
132
1
        if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
133
1
            select.next();
134
        }
135
1
        sw.col_focus = COL_VALUE;
136
1
        if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
137
1
            "7".chars().for_each(|c| aw.insert_char(c));
138
        }
139
    }
140
1
    app.open_dialog_on_front(Modal::Form(form));
141
1
    apply(&mut app, Intent::Submit);
142
1
    assert!(app.pending_form.is_some(), "the form waits for its reply");
143
1
    tokio::task::yield_now().await;
144
1
    app.drain_eval();
145
1
    let echoed = sent.frames().join("\n");
146
1
    assert!(
147
1
        echoed.contains(r#"(submit-form "transaction-create" "(:date "#),
148
        "frame: {echoed}"
149
    );
150
1
    assert!(
151
1
        echoed.contains(
152
1
            r#":splits ((:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :value \"7\"))"#
153
1
        ),
154
1
        "frame: {echoed}"
155
1
    );
156
1
}