Lines
99.5 %
Functions
100 %
Branches
//! `:reports <kind> key=value` through the palette: prefill, lookup and run rules.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::FormKind;
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::tabs::fetch::Fetch;
use crate::tabs::nms_eval::double::Sent;
use crate::tabs::reports::ReportKind;
use cli_core::transaction::submit_call;
use forms::FormValues;
const USD: &str = "550e8400-e29b-41d4-a716-446655440001";
fn connected() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
(app, sent)
}
pub(super) 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);
pub(super) fn options(labels: &[&str]) -> String {
let rows: String = labels
.iter()
.map(|label| format!(r#"(:id \"{USD}\" :label \"{label}\")"#))
.collect();
format!(r#"(:id 1 :value "({rows})")"#)
fn open_form(app: &App) -> &crate::form::Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("report form expected, got {other:?}"),
/// The ids of the report lookups awaiting a reply, oldest first.
pub(super) fn lookup_ids(app: &App) -> Vec<i64> {
let mut ids: Vec<i64> = app
.pending_routes
.filter(|(_, route)| matches!(route.ctx, RouteCtx::Lookup { .. }))
.map(|(id, _)| *id)
ids.sort_unstable();
ids
fn lookup_pending(app: &App) -> bool {
app.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::Lookup { .. }))
#[tokio::test]
async fn a_non_date_arg_prefills_the_form_it_opens() {
let (mut app, _) = connected();
run(
&mut app,
"reports breakdown from=2026-01-01 tag=category grouping=Month sort=name_asc",
);
let form = open_form(&app);
assert_eq!(form.kind, FormKind::Report(ReportKind::Breakdown));
let values = form.values();
assert_eq!(values.text("tag"), Some("category"));
assert_eq!(values.text("grouping"), Some("month"));
assert_eq!(values.text("sort"), Some("name_asc"));
assert_eq!(values.text("from"), Some("2026-01-01"));
assert_eq!(values.text("to"), Some(""));
async fn an_unknown_key_errors_and_opens_nothing() {
"reports activity from=2026-01-01 to=2026-02-01 chart=bar",
assert!(
app.status.starts_with("command error: ") && app.status.ends_with("unknown arg: chart")
assert!(app.overlays.is_empty());
assert!(matches!(app.reports().state, Fetch::Idle));
async fn echoed(app: &mut App, sent: &Sent) -> String {
tokio::task::yield_now().await;
app.drain_eval();
sent.frames().join("\n")
fn expected_call(kind: ReportKind, pairs: &[(&str, &str)]) -> String {
let mut values = FormValues::default();
pairs.iter().for_each(|(key, value)| values.set(key, value));
submit_call(kind.descriptor(), &values)
async fn all_required_fields_run_the_report_with_every_arg() {
let (mut app, sent) = connected();
"reports breakdown from=2026-01-01 to=2026-02-01 tag=category grouping=Year",
assert!(matches!(app.reports().state, Fetch::Loading { .. }));
let call = expected_call(
ReportKind::Breakdown,
&[
("from", "2026-01-01"),
("to", "2026-02-01"),
("grouping", "year"),
("tag", "category"),
],
assert!(echoed(&mut app, &sent).await.contains(&call));
async fn a_bare_command_opens_the_form_with_defaults() {
for (kind, name) in [
(ReportKind::Balance, "balance"),
(ReportKind::Activity, "activity"),
(ReportKind::Breakdown, "breakdown"),
] {
run(&mut app, &format!("reports {name}"));
assert_eq!(open_form(&app).kind, FormKind::Report(kind));
async fn balance_with_args_and_no_required_field_runs() {
run(&mut app, "reports balance filter=x");
async fn a_duplicate_key_errors_and_opens_nothing() {
run(&mut app, "reports balance from=2026-01-01 from=2026-02-01");
app.status.ends_with("duplicate arg: from"),
"{}",
app.status
async fn a_missing_required_field_opens_the_form_prefilled() {
run(&mut app, "reports activity from=2026-01-01 grouping=year");
let values = open_form(&app).values();
assert_eq!(values.text("grouping"), Some("year"));
async fn a_bad_choice_errors_and_opens_nothing() {
run(&mut app, "reports activity grouping=decade");
assert!(app.status.contains("grouping"), "{}", app.status);
async fn a_target_label_is_looked_up_before_the_report_runs() {
run(&mut app, "reports balance target=usd");
assert!(lookup_pending(&app));
let id = *app.pending_routes.keys().next().expect("lookup request");
app.route_reply(id, &options(&["USD"]));
let call = expected_call(ReportKind::Balance, &[("target", USD)]);
async fn another_options_fetch_does_not_drop_the_report_lookup() {
app.fetch_commodity_form_options();
async fn a_target_label_prefills_the_form_by_id() {
run(&mut app, "reports activity target=USD from=2026-01-01");
assert_eq!(open_form(&app).values().text("target"), Some(USD));
async fn an_ambiguous_target_is_a_command_error() {
run(&mut app, "reports balance target=USD");
app.route_reply(id, &options(&["USD", "USD"]));
assert!(app.status.contains("ambiguous"), "{}", app.status);
async fn an_unknown_target_is_a_command_error() {
run(&mut app, "reports balance target=XYZ");
assert!(app.status.contains("matches no entry"), "{}", app.status);
async fn a_newer_direct_run_supersedes_a_pending_lookup() {
run(&mut app, "reports breakdown from=2026-01-01 to=2026-02-01");
let expected = FormKind::Report(ReportKind::Breakdown);
assert_eq!(app.reports().kind, ReportKind::Breakdown, "{expected:?}");
async fn a_newer_bare_form_supersedes_a_pending_lookup() {
run(&mut app, "reports activity");
assert_eq!(open_form(&app).kind, FormKind::Report(ReportKind::Activity));
async fn the_tui_error_names_the_key_without_dashes() {
assert!(app.status.contains("target: 'XYZ'"), "{}", app.status);
assert!(!app.status.contains("--target"), "{}", app.status);