Lines
99.18 %
Functions
100 %
Branches
//! Reports tab keys: re-run, re-edit and open forms for the last report.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::{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, ReportsTab};
use crate::test_support::{ctrl, key, press};
use crate::view::Tab;
use crate::view::ViewId;
use cli_core::transaction::submit_call;
use forms::FormValues;
use ratatui::crossterm::event::KeyCode;
fn connected() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Reports);
(app, sent)
}
fn values(pairs: &[(&str, &str)]) -> FormValues {
let mut values = FormValues::default();
pairs.iter().for_each(|(key, value)| values.set(key, value));
values
fn activity() -> FormValues {
values(&[
("from", "2026-01-01"),
("to", "2026-02-01"),
("grouping", "year"),
])
fn report_run(app: &App) -> i64 {
app.pending_routes
.iter()
.find_map(|(id, route)| matches!(route.ctx, RouteCtx::Reports { .. }).then_some(*id))
.expect("report request")
fn accepted_run(app: &mut App, kind: ReportKind, values: &FormValues) {
app.submit_report(app.window_of(crate::view::ViewId::Reports), kind, values);
let id = report_run(app);
app.route_reply(id, EMPTY);
async fn echoed(app: &mut App, sent: &Sent) -> String {
tokio::task::yield_now().await;
app.drain_eval();
sent.frames().join("\n")
fn top_form(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("form expected, got {other:?}"),
#[tokio::test]
async fn r_reruns_the_last_report_with_its_values() {
let (mut app, sent) = connected();
accepted_run(&mut app, ReportKind::Activity, &activity());
echoed(&mut app, &sent).await;
app.console_mut().scrollback.clear();
press(&mut app, ctrl(KeyCode::Char('r')));
assert!(matches!(app.reports().state, Fetch::Loading { .. }));
let call = submit_call(ReportKind::Activity.descriptor(), &activity());
assert!(echoed(&mut app, &sent).await.contains(&call));
async fn r_with_nothing_run_says_so() {
let (mut app, _) = connected();
assert_eq!(app.status, "no report run yet");
assert!(matches!(app.reports().state, Fetch::Idle));
async fn e_with_nothing_run_says_so() {
press(&mut app, key(KeyCode::Char('e')));
assert!(app.overlays.is_empty());
async fn e_reopens_the_form_prefilled_with_the_last_values() {
let form = top_form(&app);
assert_eq!(form.kind, FormKind::Report(ReportKind::Activity));
let shown = form.values();
assert_eq!(shown.text("from"), Some("2026-01-01"));
assert_eq!(shown.text("to"), Some("2026-02-01"));
assert_eq!(shown.text("grouping"), Some("year"));
async fn a_form_submit_replaces_the_last_report() {
press(&mut app, key(KeyCode::Char('b')));
apply(&mut app, Intent::Submit);
let form = report_run(&app);
app.route_reply(form, EMPTY);
let (kind, _) = app.reports().last().cloned().expect("last report");
assert_eq!(kind, ReportKind::Balance);
async fn b_a_c_open_the_forms_with_defaults() {
for (letter, kind) in [
('b', ReportKind::Balance),
('a', ReportKind::Activity),
('c', ReportKind::Breakdown),
] {
press(&mut app, key(KeyCode::Char(letter)));
assert_eq!(top_form(&app).kind, FormKind::Report(kind));
assert!(app.reports().last().is_none());
async fn a_reply_older_than_the_latest_run_is_dropped() {
app.submit_report(
app.window_of(crate::view::ViewId::Reports),
ReportKind::Balance,
&values(&[("filter", "x")]),
);
let old = *app.pending_routes.keys().next().expect("first run");
&values(&[("filter", "y")]),
app.route_reply(old, r#"(:id 1 :error (:code boom :message "stale"))"#);
async fn the_title_names_the_last_report_and_its_dates() {
assert_eq!(
app.reports().title(),
"Activity from=2026-01-01 to=2026-02-01"
const EMPTY: &str = r#"(:id 1 :value "()")"#;
const REJECTED: &str = r#"(:id 1 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "from" :MESSAGE "not a date"))))"#;
fn usd_options() -> String {
let row = r#"(:id \"550e8400-e29b-41d4-a716-446655440001\" :label \"USD\")"#;
format!(r#"(:id 1 :value "({row})")"#)
fn loading_id(app: &App) -> i64 {
match app.reports().state {
Fetch::Loading { id } => id,
ref other => panic!("loading expected, got {other:?}"),
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);
async fn an_opened_form_does_not_drop_the_pending_reply() {
run(&mut app, "reports balance filter=x");
let id = loading_id(&app);
run(&mut app, "reports activity");
apply(&mut app, Intent::CloseTopmost);
assert!(!matches!(app.reports().state, Fetch::Loading { .. }));
async fn a_failed_lookup_does_not_drop_the_pending_reply() {
run(&mut app, "reports balance target=XYZ");
let lookup = *app
.pending_routes
.keys()
.find(|key| **key != id)
.expect("lookup request");
app.route_reply(lookup, &usd_options());
async fn a_rejected_form_over_a_loading_report_does_not_stick() {
press(&mut app, key(KeyCode::Char('a')));
app.route_reply(form, REJECTED);
async fn the_title_follows_the_shown_report_not_a_rejected_one() {
accepted_run(&mut app, ReportKind::Balance, &values(&[("filter", "x")]));
assert_eq!(app.reports().title(), "Balance");
async fn a_rejected_submit_leaves_the_last_report_to_rerun_and_edit() {
let balance = values(&[("filter", "x")]);
accepted_run(&mut app, ReportKind::Balance, &balance);
assert_eq!(app.reports().last(), Some(&(ReportKind::Balance, balance)));
assert_eq!(form.kind, FormKind::Report(ReportKind::Balance));
assert_eq!(form.values().text("filter"), Some("x"));
async fn a_rejected_palette_run_leaves_the_last_report_to_rerun_and_edit() {
let accepted = values(&[("filter", "x")]);
accepted_run(&mut app, ReportKind::Balance, &accepted);
run(&mut app, "reports balance filter=y");
let id = report_run(&app);
app.route_reply(id, REJECTED);
assert!(matches!(app.reports().state, Fetch::Error(_)));
app.reports().last(),
Some(&(ReportKind::Balance, accepted.clone()))
let call = submit_call(ReportKind::Balance.descriptor(), &accepted);
assert_eq!(top_form(&app).values().text("filter"), Some("x"));
async fn b_a_c_and_e_supersede_a_pending_lookup() {
for letter in ['b', 'a', 'c', 'e'] {
run(&mut app, "reports balance target=usd");
let lookup = *app.pending_routes.keys().next().expect("lookup request");
assert!(
!matches!(app.reports().state, Fetch::Loading { .. }),
"{letter}"
async fn a_refresh_reruns_only_the_windows_that_have_a_report() {
let ran = app.window_of(ViewId::Reports);
let fresh = app.open_extra(ViewId::Reports);
app.set_status("");
apply(&mut app, Intent::RefreshTab);
assert!(matches!(
app.win::<ReportsTab>(ran).map(|tab| tab.state.clone()),
Some(Fetch::Loading { .. })
));
app.win::<ReportsTab>(fresh).map(|tab| tab.state.clone()),
Some(Fetch::Idle)
assert_eq!(app.status, "");
async fn a_refresh_with_no_report_anywhere_says_so_once() {
app.open_extra(ViewId::Reports);
assert!(app.pending_routes.is_empty());