1
//! `transaction list` in the palette and `t` on the Accounts tab filter the Transactions tab.
2

            
3
use super::edit::options_wire;
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::route::RouteCtx;
7
use crate::tabs::fetch::Fetch;
8
use crate::tabs::nms_eval::double::Sent;
9
use crate::test_support::{key, press};
10
use crate::view::Tab;
11
use cli_core::render::ListRow;
12
use ratatui::crossterm::event::KeyCode;
13

            
14
const CASH: &str = "550e8400-e29b-41d4-a716-446655440061";
15
const BANK: &str = "550e8400-e29b-41d4-a716-446655440062";
16
const OTHER_CASH: &str = "550e8400-e29b-41d4-a716-446655440063";
17
const REJECTED: &str = r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "from" :MESSAGE "not a date"))))"#;
18

            
19
14
fn run(app: &mut App, command: &str) {
20
14
    apply(app, Intent::OpenCommandLine);
21
14
    command
22
14
        .chars()
23
485
        .for_each(|c| apply(app, Intent::InsertChar(c)));
24
14
    apply(app, Intent::Submit);
25
14
}
26

            
27
14
fn started() -> (App, Sent) {
28
14
    let mut app = crate::test_support::app();
29
14
    let sent = app.attach_echo();
30
14
    app.accounts_mut().state = Fetch::Loaded(Vec::new());
31
14
    app.switch_tab(Tab::Accounts);
32
14
    (app, sent)
33
14
}
34

            
35
9
fn page_wire(id: i64) -> String {
36
9
    format!(r#"(:id {id} :value (:ROWS () :HAS-MORE nil :TOTAL 0))"#)
37
9
}
38

            
39
5
fn accounts_wire() -> String {
40
5
    options_wire(&[(CASH, "Assets:Cash"), (BANK, "Assets:Bank")])
41
5
}
42

            
43
27
fn pending_id(app: &App, wanted: fn(&RouteCtx) -> bool) -> Option<i64> {
44
27
    app.pending_routes
45
27
        .iter()
46
22
        .find_map(|(id, route)| wanted(&route.ctx).then_some(*id))
47
27
}
48

            
49
11
fn account_fetch(app: &App) -> Option<i64> {
50
11
    pending_id(app, |ctx| matches!(ctx, RouteCtx::Lookup { .. }))
51
11
}
52

            
53
16
fn filter_query(app: &App) -> Option<i64> {
54
16
    pending_id(app, |ctx| matches!(ctx, RouteCtx::TransactionFilter { .. }))
55
16
}
56

            
57
8
fn accept_filter(app: &mut App) {
58
8
    let id = filter_query(app).unwrap_or_default();
59
8
    assert!(id > 0, "a filter query is in flight");
60
8
    app.route_reply(id, &page_wire(id));
61
8
}
62

            
63
4
fn apply_filter(app: &mut App, command: &str) {
64
4
    run(app, command);
65
4
    if let Some(id) = account_fetch(app) {
66
2
        app.route_reply(id, &accounts_wire());
67
2
    }
68
4
    accept_filter(app);
69
4
}
70

            
71
5
fn assert_untouched(app: &App, tab: Tab) {
72
5
    assert_eq!(app.front_kind(), Some(tab));
73
5
    assert_eq!(app.transactions().filter().text("account"), None);
74
5
    assert_eq!(app.transactions().filter().text("from"), None);
75
5
    assert!(filter_query(app).is_none());
76
5
    assert!(
77
5
        app.console().scrollback.is_empty(),
78
        "nothing in the console"
79
    );
80
5
}
81

            
82
#[tokio::test]
83
1
async fn the_palette_sets_the_filter_and_switches_to_the_tab() {
84
1
    let (mut app, _) = started();
85
1
    apply_filter(
86
1
        &mut app,
87
1
        "transaction list account=Assets:Cash from=2026-01-01 to=2026-02-01",
88
1
    );
89
1
    let filter = app.transactions().filter();
90
1
    assert_eq!(filter.text("account"), Some(CASH));
91
1
    assert_eq!(filter.text("from"), Some("2026-01-01"));
92
1
    assert_eq!(filter.text("to"), Some("2026-02-01"));
93
1
    assert_eq!(app.front_kind(), Some(Tab::Transactions));
94
1
    assert_eq!(
95
1
        app.transactions().title("Transactions"),
96
1
        "Transactions [Assets:Cash 2026-01-01\u{2013}2026-02-01]"
97
    );
98
1
    assert!(
99
1
        app.console().scrollback.is_empty(),
100
1
        "nothing in the console"
101
1
    );
102
1
}
103

            
104
#[tokio::test]
105
1
async fn an_account_uuid_is_accepted() {
106
1
    let (mut app, _) = started();
107
1
    apply_filter(&mut app, &format!("transaction list account={BANK}"));
108
1
    assert_eq!(app.transactions().filter().text("account"), Some(BANK));
109
1
}
110

            
111
#[tokio::test]
112
1
async fn dates_alone_need_no_account_lookup() {
113
1
    let (mut app, _) = started();
114
1
    run(&mut app, "transaction list from=2026-01-01");
115
1
    assert!(account_fetch(&app).is_none());
116
1
    accept_filter(&mut app);
117
1
    assert_eq!(app.transactions().filter().text("from"), Some("2026-01-01"));
118
1
    assert_eq!(app.front_kind(), Some(Tab::Transactions));
119
1
}
120

            
121
#[tokio::test]
122
1
async fn an_unknown_key_is_a_command_error() {
123
1
    let (mut app, _) = started();
124
1
    run(&mut app, "transaction list colour=red");
125
1
    assert!(app.status.starts_with("command error"), "{}", app.status);
126
1
    assert_untouched(&app, Tab::Accounts);
127
1
    assert!(app.pending_routes.is_empty());
128
1
}
129

            
130
#[tokio::test]
131
1
async fn an_unknown_label_is_a_command_error() {
132
1
    let (mut app, _) = started();
133
1
    run(&mut app, "transaction list account=Nowhere");
134
1
    let id = account_fetch(&app).unwrap_or_default();
135
1
    app.route_reply(id, &accounts_wire());
136
1
    assert!(app.status.starts_with("command error"), "{}", app.status);
137
1
    assert!(app.status.contains("Nowhere"), "{}", app.status);
138
1
    assert_untouched(&app, Tab::Accounts);
139
1
}
140

            
141
#[tokio::test]
142
1
async fn an_ambiguous_label_is_a_command_error() {
143
1
    let (mut app, _) = started();
144
1
    run(&mut app, "transaction list account=Cash");
145
1
    let id = account_fetch(&app).unwrap_or_default();
146
1
    let twins = options_wire(&[(CASH, "Cash"), (OTHER_CASH, "Cash")]);
147
1
    app.route_reply(id, &twins);
148
1
    assert!(app.status.starts_with("command error"), "{}", app.status);
149
1
    assert!(app.status.contains("ambiguous"), "{}", app.status);
150
1
    assert_untouched(&app, Tab::Accounts);
151
1
}
152

            
153
#[tokio::test]
154
1
async fn no_args_clears_the_filter_and_switches() {
155
1
    let (mut app, _) = started();
156
1
    apply_filter(&mut app, "transaction list from=2026-01-01");
157
1
    app.switch_tab(Tab::Accounts);
158
1
    run(&mut app, "transaction list");
159
1
    assert_eq!(app.transactions().filter().text("from"), None);
160
1
    assert_eq!(app.front_kind(), Some(Tab::Transactions));
161
1
    assert!(matches!(app.transactions().state, Fetch::Loading { .. }));
162
1
}
163

            
164
#[tokio::test]
165
1
async fn a_server_rejection_keeps_the_old_filter() {
166
1
    let (mut app, _) = started();
167
1
    apply_filter(&mut app, "transaction list from=2026-01-01");
168
1
    app.switch_tab(Tab::Accounts);
169
1
    run(&mut app, "transaction list from=garbage");
170
1
    let id = filter_query(&app).unwrap_or_default();
171
1
    app.route_reply(id, REJECTED);
172
1
    assert_eq!(app.status, "filter failed: From: not a date");
173
1
    assert_eq!(app.transactions().filter().text("from"), Some("2026-01-01"));
174
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
175
1
}
176

            
177
2
fn select_account(app: &mut App) {
178
2
    app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
179
2
        id: Some(CASH.to_string()),
180
2
        cells: vec!["Cash".to_string()],
181
2
    }]);
182
2
}
183

            
184
#[tokio::test]
185
1
async fn t_on_accounts_filters_by_the_selected_account() {
186
1
    let (mut app, _) = started();
187
1
    select_account(&mut app);
188
1
    press(&mut app, key(KeyCode::Char('t')));
189
1
    let id = account_fetch(&app).unwrap_or_default();
190
1
    app.route_reply(id, &accounts_wire());
191
1
    accept_filter(&mut app);
192
1
    assert_eq!(app.transactions().filter().text("account"), Some(CASH));
193
1
    assert_eq!(app.transactions().filter().text("from"), None);
194
1
    assert_eq!(app.front_kind(), Some(Tab::Transactions));
195
1
    assert_eq!(
196
1
        app.transactions().title("Transactions"),
197
1
        "Transactions [Assets:Cash]"
198
1
    );
199
1
}
200

            
201
#[tokio::test]
202
1
async fn t_titles_twin_names_by_their_unique_labels() {
203
1
    let (mut app, _) = started();
204
1
    select_account(&mut app);
205
1
    apply(&mut app, Intent::ListAccountTransactions);
206
1
    let id = account_fetch(&app).unwrap_or_default();
207
1
    let twins = options_wire(&[(CASH, "Bank:Cash"), (OTHER_CASH, "Wallet:Cash")]);
208
1
    app.route_reply(id, &twins);
209
1
    accept_filter(&mut app);
210
1
    assert_eq!(
211
1
        app.transactions().title("Transactions"),
212
1
        "Transactions [Bank:Cash]"
213
1
    );
214
1
}
215

            
216
#[tokio::test]
217
1
async fn t_without_a_selection_says_so() {
218
1
    let (mut app, _) = started();
219
1
    apply(&mut app, Intent::ListAccountTransactions);
220
1
    assert_eq!(app.status, "no account selected");
221
1
    assert_untouched(&app, Tab::Accounts);
222
1
}
223

            
224
#[tokio::test]
225
1
async fn a_late_account_lookup_does_not_override_a_newer_filter() {
226
1
    let (mut app, _) = started();
227
1
    run(&mut app, "transaction list account=Assets:Cash");
228
1
    let lookup = account_fetch(&app).unwrap_or_default();
229
1
    run(&mut app, "transaction list from=2026-01-01");
230
1
    accept_filter(&mut app);
231
1
    app.route_reply(lookup, &accounts_wire());
232
1
    assert_eq!(app.transactions().filter().text("account"), None);
233
1
    assert_eq!(app.transactions().filter().text("from"), Some("2026-01-01"));
234
1
    assert!(filter_query(&app).is_none(), "the late lookup sent nothing");
235
1
}
236

            
237
#[tokio::test]
238
1
async fn a_late_filter_reply_does_not_undo_a_clear_or_switch_the_tab() {
239
1
    let (mut app, _) = started();
240
1
    run(&mut app, "transaction list from=2026-01-01");
241
1
    let late = filter_query(&app).unwrap_or_default();
242
1
    app.switch_tab(Tab::Transactions);
243
1
    apply(&mut app, Intent::ListFilterClear);
244
1
    app.switch_tab(Tab::Commodities);
245
1
    app.route_reply(late, &page_wire(late));
246
1
    assert_eq!(app.transactions().filter().text("from"), None);
247
1
    assert_eq!(app.front_kind(), Some(Tab::Commodities));
248
1
}
249

            
250
#[tokio::test]
251
1
async fn a_failed_account_lookup_shows_the_servers_error() {
252
1
    let (mut app, _) = started();
253
1
    run(&mut app, "transaction list account=Cash");
254
1
    let id = account_fetch(&app).unwrap_or_default();
255
1
    app.route_reply(id, r#"(:id 1 :error (:code denied :message "no access"))"#);
256
1
    assert!(app.status.starts_with("command error"), "{}", app.status);
257
1
    assert!(app.status.contains("no access"), "{}", app.status);
258
1
    assert_untouched(&app, Tab::Accounts);
259
1
}