Lines
100 %
Functions
Branches
//! `transaction list` in the palette and `t` on the Accounts tab filter the Transactions tab.
use super::edit::options_wire;
use crate::app::App;
use crate::event::{Intent, apply};
use crate::route::RouteCtx;
use crate::tabs::fetch::Fetch;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use crate::view::Tab;
use cli_core::render::ListRow;
use ratatui::crossterm::event::KeyCode;
const CASH: &str = "550e8400-e29b-41d4-a716-446655440061";
const BANK: &str = "550e8400-e29b-41d4-a716-446655440062";
const OTHER_CASH: &str = "550e8400-e29b-41d4-a716-446655440063";
const REJECTED: &str = r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "from" :MESSAGE "not a date"))))"#;
fn run(app: &mut App, command: &str) {
apply(app, Intent::OpenCommandLine);
command
.chars()
.for_each(|c| apply(app, Intent::InsertChar(c)));
apply(app, Intent::Submit);
}
fn started() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.accounts_mut().state = Fetch::Loaded(Vec::new());
app.switch_tab(Tab::Accounts);
(app, sent)
fn page_wire(id: i64) -> String {
format!(r#"(:id {id} :value (:ROWS () :HAS-MORE nil :TOTAL 0))"#)
fn accounts_wire() -> String {
options_wire(&[(CASH, "Assets:Cash"), (BANK, "Assets:Bank")])
fn pending_id(app: &App, wanted: fn(&RouteCtx) -> bool) -> Option<i64> {
app.pending_routes
.iter()
.find_map(|(id, route)| wanted(&route.ctx).then_some(*id))
fn account_fetch(app: &App) -> Option<i64> {
pending_id(app, |ctx| matches!(ctx, RouteCtx::Lookup { .. }))
fn filter_query(app: &App) -> Option<i64> {
pending_id(app, |ctx| matches!(ctx, RouteCtx::TransactionFilter { .. }))
fn accept_filter(app: &mut App) {
let id = filter_query(app).unwrap_or_default();
assert!(id > 0, "a filter query is in flight");
app.route_reply(id, &page_wire(id));
fn apply_filter(app: &mut App, command: &str) {
run(app, command);
if let Some(id) = account_fetch(app) {
app.route_reply(id, &accounts_wire());
accept_filter(app);
fn assert_untouched(app: &App, tab: Tab) {
assert_eq!(app.front_kind(), Some(tab));
assert_eq!(app.transactions().filter().text("account"), None);
assert_eq!(app.transactions().filter().text("from"), None);
assert!(filter_query(app).is_none());
assert!(
app.console().scrollback.is_empty(),
"nothing in the console"
);
#[tokio::test]
async fn the_palette_sets_the_filter_and_switches_to_the_tab() {
let (mut app, _) = started();
apply_filter(
&mut app,
"transaction list account=Assets:Cash from=2026-01-01 to=2026-02-01",
let filter = app.transactions().filter();
assert_eq!(filter.text("account"), Some(CASH));
assert_eq!(filter.text("from"), Some("2026-01-01"));
assert_eq!(filter.text("to"), Some("2026-02-01"));
assert_eq!(app.front_kind(), Some(Tab::Transactions));
assert_eq!(
app.transactions().title("Transactions"),
"Transactions [Assets:Cash 2026-01-01\u{2013}2026-02-01]"
async fn an_account_uuid_is_accepted() {
apply_filter(&mut app, &format!("transaction list account={BANK}"));
assert_eq!(app.transactions().filter().text("account"), Some(BANK));
async fn dates_alone_need_no_account_lookup() {
run(&mut app, "transaction list from=2026-01-01");
assert!(account_fetch(&app).is_none());
accept_filter(&mut app);
assert_eq!(app.transactions().filter().text("from"), Some("2026-01-01"));
async fn an_unknown_key_is_a_command_error() {
run(&mut app, "transaction list colour=red");
assert!(app.status.starts_with("command error"), "{}", app.status);
assert_untouched(&app, Tab::Accounts);
assert!(app.pending_routes.is_empty());
async fn an_unknown_label_is_a_command_error() {
run(&mut app, "transaction list account=Nowhere");
let id = account_fetch(&app).unwrap_or_default();
assert!(app.status.contains("Nowhere"), "{}", app.status);
async fn an_ambiguous_label_is_a_command_error() {
run(&mut app, "transaction list account=Cash");
let twins = options_wire(&[(CASH, "Cash"), (OTHER_CASH, "Cash")]);
app.route_reply(id, &twins);
assert!(app.status.contains("ambiguous"), "{}", app.status);
async fn no_args_clears_the_filter_and_switches() {
apply_filter(&mut app, "transaction list from=2026-01-01");
run(&mut app, "transaction list");
assert!(matches!(app.transactions().state, Fetch::Loading { .. }));
async fn a_server_rejection_keeps_the_old_filter() {
run(&mut app, "transaction list from=garbage");
let id = filter_query(&app).unwrap_or_default();
app.route_reply(id, REJECTED);
assert_eq!(app.status, "filter failed: From: not a date");
assert_eq!(app.front_kind(), Some(Tab::Accounts));
fn select_account(app: &mut App) {
app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
id: Some(CASH.to_string()),
cells: vec!["Cash".to_string()],
}]);
async fn t_on_accounts_filters_by_the_selected_account() {
select_account(&mut app);
press(&mut app, key(KeyCode::Char('t')));
assert_eq!(app.transactions().filter().text("account"), Some(CASH));
"Transactions [Assets:Cash]"
async fn t_titles_twin_names_by_their_unique_labels() {
apply(&mut app, Intent::ListAccountTransactions);
let twins = options_wire(&[(CASH, "Bank:Cash"), (OTHER_CASH, "Wallet:Cash")]);
"Transactions [Bank:Cash]"
async fn t_without_a_selection_says_so() {
assert_eq!(app.status, "no account selected");
async fn a_late_account_lookup_does_not_override_a_newer_filter() {
run(&mut app, "transaction list account=Assets:Cash");
let lookup = account_fetch(&app).unwrap_or_default();
app.route_reply(lookup, &accounts_wire());
assert!(filter_query(&app).is_none(), "the late lookup sent nothing");
async fn a_late_filter_reply_does_not_undo_a_clear_or_switch_the_tab() {
let late = filter_query(&app).unwrap_or_default();
app.switch_tab(Tab::Transactions);
apply(&mut app, Intent::ListFilterClear);
app.switch_tab(Tab::Commodities);
app.route_reply(late, &page_wire(late));
assert_eq!(app.front_kind(), Some(Tab::Commodities));
async fn a_failed_account_lookup_shows_the_servers_error() {
app.route_reply(id, r#"(:id 1 :error (:code denied :message "no access"))"#);
assert!(app.status.contains("no access"), "{}", app.status);