1
//! Tests for the transaction-create modal and the list edit intent.
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::view::Tab;
9
use crate::widgets::splits::COL_FROM;
10
use crate::widgets::{DateWidget, SelectOption, Widget};
11
use cli_core::render::ListRow;
12

            
13
1
fn loaded_transactions_app() -> App {
14
1
    let mut app = crate::test_support::app();
15
1
    app.switch_tab(Tab::Transactions);
16
1
    let uuid = "550e8400-e29b-41d4-a716-446655440000";
17
1
    app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
18
1
        id: Some(uuid.to_string()),
19
1
        cells: vec!["2024-06-01".to_string(), "Groceries".to_string()],
20
1
    }]);
21
1
    app
22
1
}
23

            
24
#[tokio::test]
25
1
async fn palette_transaction_create_opens_modal_and_fetches_options() {
26
1
    let mut app = crate::test_support::app();
27
1
    let sent = app.attach_echo();
28
1
    apply(&mut app, Intent::OpenCommandLine);
29
1
    for c in "transaction create".chars() {
30
18
        apply(&mut app, Intent::InsertChar(c));
31
    }
32
1
    apply(&mut app, Intent::Submit);
33
1
    match app.overlays.top() {
34
        Some(Modal::Form(f)) => {
35
1
            assert_eq!(f.kind, FormKind::TransactionCreate);
36
1
            assert_eq!(f.fields.len(), 4, "date + note + tags + splits");
37
        }
38
        other => panic!("expected TransactionCreate modal, got {other:?}"),
39
    }
40
    // The open form requested both option lists; the echo worker reflects
41
    // both frames, so draining yields no error notices (the worker is alive).
42
1
    tokio::task::yield_now().await;
43
1
    app.drain_eval();
44
1
    assert!(
45
3
        !sent.frames().iter().any(|l| l.contains("worker stopped")),
46
1
        "both option fetches must dispatch without stopping the worker"
47
1
    );
48
1
}
49

            
50
#[test]
51
1
fn split_row_select_widget_first_navigates_options() {
52
1
    let mut app = crate::test_support::app();
53
1
    let mut form = Form::transaction_create();
54
1
    form.focus = 3;
55
1
    if let Widget::Splits(ref mut sw) = form.fields[3].widget {
56
1
        sw.col_focus = COL_FROM;
57
1
        sw.set_account_options(vec![
58
1
            SelectOption {
59
1
                id: "acc-0".to_string(),
60
1
                label: "Cash".to_string(),
61
1
            },
62
1
            SelectOption {
63
1
                id: "acc-1".to_string(),
64
1
                label: "Food".to_string(),
65
1
            },
66
1
        ]);
67
    }
68
1
    app.overlays.push(Modal::Form(form));
69
    // SelectNext drives the focused row's from-account Select (nested widget-first).
70
1
    apply(&mut app, Intent::SelectNext);
71
1
    match app.overlays.top() {
72
        Some(Modal::Form(f)) => {
73
1
            if let Widget::Splits(sw) = &f.fields[3].widget {
74
1
                assert_eq!(sw.rows()[0].from.value(), "acc-1");
75
            } else {
76
                panic!("field 2 must be Splits");
77
            }
78
        }
79
        other => panic!("expected form modal, got {other:?}"),
80
    }
81
1
}
82

            
83
#[test]
84
1
fn list_edit_on_loaded_transactions_tab_dispatches_edit_async() {
85
    // With no eval worker attached, open_transaction_edit_async sets status
86
    // "console not connected" — proving the dispatch path was taken.
87
1
    let mut app = loaded_transactions_app();
88
1
    apply(&mut app, Intent::EditRow);
89
1
    assert_eq!(
90
1
        app.status, "console not connected",
91
        "EditRow on Transactions should attempt to load the transaction"
92
    );
93
1
    assert!(
94
1
        app.overlays.is_empty(),
95
        "no form pushed synchronously (async open)"
96
    );
97
1
}
98

            
99
#[test]
100
1
fn list_edit_on_transactions_with_no_selection_sets_status() {
101
1
    let mut app = crate::test_support::app();
102
1
    app.switch_tab(Tab::Transactions);
103
    // transactions list is Idle (no loaded rows).
104
1
    apply(&mut app, Intent::EditRow);
105
1
    assert_eq!(app.status, "no transaction selected");
106
1
    assert!(app.overlays.is_empty());
107
1
}
108

            
109
#[test]
110
1
fn date_step_forward_intent_advances_date_field_by_one_day() {
111
1
    let mut app = crate::test_support::app();
112
1
    let mut form = Form::transaction_create();
113
    // Seed a known date in the date field so the step outcome is deterministic.
114
1
    if let Widget::Date(ref mut dw) = form.fields[0].widget {
115
1
        *dw = DateWidget::with_value("2024-06-15T10:00");
116
    }
117
1
    form.focus = 0;
118
1
    app.overlays.push(Modal::Form(form));
119
1
    apply(&mut app, Intent::DateStepForward);
120
1
    match app.overlays.top() {
121
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "2024-06-16T10:00"),
122
        other => panic!("expected form modal, got {other:?}"),
123
    }
124
1
}
125

            
126
#[test]
127
1
fn date_step_back_intent_retreats_date_field_by_one_day() {
128
1
    let mut app = crate::test_support::app();
129
1
    let mut form = Form::transaction_create();
130
1
    if let Widget::Date(ref mut dw) = form.fields[0].widget {
131
1
        *dw = DateWidget::with_value("2024-06-15T10:00");
132
    }
133
1
    form.focus = 0;
134
1
    app.overlays.push(Modal::Form(form));
135
1
    apply(&mut app, Intent::DateStepBack);
136
1
    match app.overlays.top() {
137
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "2024-06-14T10:00"),
138
        other => panic!("expected form modal, got {other:?}"),
139
    }
140
1
}
141

            
142
2
fn stepped_date_only(mut form: Form, key: &str, intent: Intent) -> String {
143
2
    let index = form
144
2
        .fields
145
2
        .iter()
146
4
        .position(|field| field.key == Some(key))
147
2
        .expect("date field");
148
2
    form.fields[index].widget = Widget::Date(DateWidget::with_value("2024-01-31"));
149
2
    form.focus = index;
150
2
    let mut app = crate::test_support::app();
151
2
    app.overlays.push(Modal::Form(form));
152
2
    apply(&mut app, intent);
153
2
    match app.overlays.top() {
154
2
        Some(Modal::Form(f)) => f.fields[index].widget.value().to_string(),
155
        other => panic!("expected form modal, got {other:?}"),
156
    }
157
2
}
158

            
159
#[test]
160
1
fn stepping_a_post_date_or_a_report_day_keeps_it_date_only() {
161
1
    let post = Form::transaction_create();
162
1
    assert_eq!(
163
1
        stepped_date_only(post, "date", Intent::DateStepForward),
164
1
        "2024-02-01"
165
    );
166
1
    let report = Form::report(
167
1
        crate::tabs::reports::ReportKind::Activity,
168
1
        &forms::FormValues::default(),
169
1
    );
170
1
    assert_eq!(
171
1
        stepped_date_only(report, "to", Intent::DateStepBack),
172
1
        "2024-01-30"
173
    );
174
1
}