Lines
100 %
Functions
Branches
//! Marking transactions and deleting the marked ones together.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::modal::Modal;
use crate::route::{Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use crate::view::{Tab, ViewId};
use forms::FormValues;
use ratatui::crossterm::event::KeyCode;
fn page_wire(ids: &[&str], has_more: bool) -> String {
let rows: String = ids
.iter()
.map(|i| format!(r#"(:ID "{i}" :DATE "2026-10-01T00:00:00Z" :DATE-TEXT "2026-10-01 00:00" :NOTE "n" :AMOUNT "5")"#))
.collect::<Vec<_>>()
.join(" ");
let more = if has_more { "t" } else { "nil" };
format!(r#"(:id 1 :value (:ROWS ({rows}) :HAS-MORE {more} :TOTAL 9))"#)
}
fn deliver(app: &mut App, ids: &[&str], has_more: bool) {
app.deliver_reply(
Route {
target: Some(app.window_of(ViewId::Transactions)),
ctx: RouteCtx::None,
},
&page_wire(ids, has_more),
);
fn loaded_app(ids: &[&str]) -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Transactions);
deliver(&mut app, ids, true);
(app, sent)
fn mark_all(app: &mut App, count: usize) {
(0..count).for_each(|_| press(app, key(KeyCode::Char('m'))));
fn bulk_routes(app: &App) -> Vec<(i64, String)> {
let mut routes: Vec<_> = app
.pending_routes
.filter_map(|(request, route)| match &route.ctx {
RouteCtx::BulkDelete { id, .. } => Some((*request, id.clone())),
_ => None,
})
.collect();
routes.sort();
routes
fn confirm_bulk(app: &mut App) {
press(app, key(KeyCode::Char('x')));
apply(app, Intent::ConfirmYes);
fn ok_reply(app: &mut App, request: i64) {
app.route_reply(request, &format!("(:id {request} :value t)"));
#[tokio::test]
async fn m_marks_the_row_and_moves_down_and_unmark_all_clears() {
let (mut app, _) = loaded_app(&["a", "b", "c"]);
press(&mut app, key(KeyCode::Char('m')));
assert_eq!(app.transactions().marked_ids(), ["a"]);
assert_eq!(app.transactions().selected_id(), Some("b"));
press(&mut app, key(KeyCode::Char('k')));
assert!(app.transactions().marked_ids().is_empty(), "m toggles");
assert_eq!(app.transactions().marked_ids(), ["b", "c"]);
press(&mut app, key(KeyCode::Char('U')));
assert!(app.transactions().marked_ids().is_empty());
async fn marks_survive_paging_and_refresh() {
let (mut app, _) = loaded_app(&["a", "b"]);
apply(&mut app, Intent::ListPageNext);
deliver(&mut app, &["c", "d"], false);
assert_eq!(app.transactions().marked_ids(), ["a", "c"]);
apply(&mut app, Intent::RefreshTab);
async fn a_filter_change_clears_the_marks() {
mark_all(&mut app, 2);
let mut filter = FormValues::default();
filter.set("from", "2026-01-01");
let window = app.window_of(crate::view::ViewId::Transactions);
let request = app
.submit_transaction_filter(window, &filter, "from".to_string())
.expect("dispatched");
assert_eq!(
app.transactions().marked_ids().len(),
2,
"not before the reply"
app.route_reply(request, &page_wire(&["a"], false));
async fn clearing_the_filter_clears_the_marks() {
press(&mut app, key(KeyCode::Char('F')));
async fn x_asks_one_confirmation_with_the_count() {
press(&mut app, key(KeyCode::Char('x')));
match app.overlays.top() {
Some(Modal::Confirm { prompt, .. }) => {
assert!(prompt.contains("Delete 2 marked"), "{prompt}");
other => panic!("confirm expected, got {other:?}"),
assert!(
bulk_routes(&app).is_empty(),
"nothing sent before the answer"
async fn x_without_marks_says_so_and_opens_nothing() {
let (mut app, _) = loaded_app(&["a"]);
assert!(app.overlays.is_empty());
assert_eq!(app.status, "no marked transactions");
async fn confirm_sends_one_delete_per_marked_id() {
let (mut app, sent) = loaded_app(&["a", "b", "c"]);
confirm_bulk(&mut app);
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(echoed.contains(r#"(delete-transaction "a")"#), "{echoed}");
assert!(echoed.contains(r#"(delete-transaction "b")"#), "{echoed}");
assert!(!echoed.contains(r#"(delete-transaction "c")"#), "{echoed}");
assert_eq!(bulk_routes(&app).len(), 2);
async fn all_replies_in_report_the_count_and_reload_the_page() {
for (request, _) in bulk_routes(&app) {
ok_reply(&mut app, request);
assert_eq!(app.status, "deleted 2 of 2 transactions");
assert!(matches!(app.transactions().state, Fetch::Loading { .. }));
let reload = *app.pending_routes.keys().max().expect("reload route");
app.route_reply(reload, &page_wire(&["c"], false));
app.status, "deleted 2 of 2 transactions",
"the reload keeps it"
async fn a_partial_failure_names_the_id_and_reason_and_keeps_its_mark() {
let routes = bulk_routes(&app);
let (bad, bad_id) = routes[0].clone();
let message = r#"(:code busy :message "Transaction busy")"#;
app.route_reply(bad, &format!("(:id {bad} :error {message})"));
ok_reply(&mut app, routes[1].0);
let status = app.status.clone();
status.starts_with("deleted 1 of 2 transactions; failed: "),
"{status}"
assert!(status.contains(&bad_id), "{status}");
assert!(status.contains("Transaction busy"), "{status}");
assert_eq!(app.transactions().marked_ids(), [bad_id]);
async fn a_nil_reply_counts_as_not_deleted() {
let (request, _) = bulk_routes(&app)[0].clone();
app.route_reply(request, &format!("(:id {request} :value nil)"));
assert!(app.status.contains("a: not deleted"), "{}", app.status);
async fn a_late_reply_of_an_older_bulk_delete_is_dropped() {
let old_seq = app
.values()
.find_map(|route| match route.ctx {
RouteCtx::BulkDelete { seq, .. } => Some(seq),
.expect("bulk route");
app.bulk.abandon();
app.pending_routes.clear();
app.attach_echo();
let ok = "(:id 9 :value t)";
app.deliver_bulk_delete_reply(old_seq, "a", ok);
!app.status.starts_with("deleted"),
"the old reply settles nothing: {}",
app.status
async fn x_while_a_bulk_delete_is_pending_is_refused_and_sends_nothing() {
let (mut app, sent) = loaded_app(&["a", "b"]);
let before = sent.frames().len();
assert!(app.overlays.is_empty(), "no second confirm");
assert_eq!(app.status, "a bulk delete is still running");
assert_eq!(sent.frames().len(), before, "no further evals");
assert_eq!(bulk_routes(&app).len(), 1);
async fn a_row_that_no_longer_exists_is_named_and_loses_its_mark() {
let (gone, gone_id) = routes[0].clone();
let (failing, failing_id) = routes[1].clone();
let missing = r#"(:code not-found :message "delete-transaction: not found")"#;
app.route_reply(gone, &format!("(:id {gone} :error {missing})"));
let busy = r#"(:code busy :message "try later")"#;
app.route_reply(failing, &format!("(:id {failing} :error {busy})"));
app.status.contains(&format!("{gone_id}: not found")),
"{}",
assert_eq!(app.transactions().marked_ids(), [failing_id]);
async fn cancel_deletes_nothing() {
apply(&mut app, Intent::CloseTopmost);
assert!(bulk_routes(&app).is_empty());
assert_eq!(app.transactions().marked_ids(), ["a", "b"]);
async fn x_is_ignored_off_the_transactions_tab() {
app.switch_tab(Tab::Accounts);