Lines
95.21 %
Functions
100 %
Branches
//! Enter in a report form fetches or says why not, whichever field is focused.
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::tabs::nms_eval::double::Sent;
use crate::tabs::reports::ReportKind;
use crate::test_support::{alt, key, press};
use crate::view::Tab;
use crate::widgets::{SelectOption, Widget};
use ratatui::crossterm::event::KeyCode;
const KINDS: [(char, ReportKind); 3] = [
('b', ReportKind::Balance),
('a', ReportKind::Activity),
('c', ReportKind::Breakdown),
];
fn opened(letter: char) -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Reports);
press(&mut app, key(KeyCode::Char(letter)));
(app, sent)
}
fn fill(app: &mut App, key: &str, text: &str) {
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
panic!("form expected");
};
let field = form
.fields
.iter_mut()
.find(|f| f.key == Some(key))
.expect("descriptor field");
field.widget.set_text(text);
fn fetched(app: &App) -> bool {
app.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::Reports { .. }))
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 enter_on_the_first_select_field_fetches_a_valid_form() {
for (letter, kind) in KINDS {
let end = if kind == ReportKind::Balance {
"as-of"
} else {
"to"
let (mut app, _) = opened(letter);
fill(&mut app, "from", "2026-01-01");
fill(&mut app, end, "2026-02-01");
press(&mut app, alt(KeyCode::Char('o')));
assert!(fetched(&app), "{kind:?}");
assert!(app.pending_form.is_some(), "{kind:?}");
async fn enter_with_empty_dates_fetches_the_balance_report() {
let (mut app, _) = opened('b');
assert!(fetched(&app));
fn typed(app: &mut App, text: &str) {
text.chars().for_each(|c| press(app, key(KeyCode::Char(c))));
fn target_select(app: &mut App) -> &mut crate::widgets::SelectWidget {
match &mut form.fields[0].widget {
Widget::Select(select) => select,
other => panic!("select expected, got {other:?}"),
fn currencies() -> Vec<SelectOption> {
[("", "(none)"), ("usd-id", "Dollar"), ("eur-id", "Euro")]
.into_iter()
.map(|(id, label)| SelectOption {
id: id.to_string(),
label: label.to_string(),
})
.collect()
async fn every_report_form_starts_on_a_select() {
assert!(
matches!(
top_form(&app).focused_widget_kind(),
Some(crate::widgets::WidgetKind::Select)
),
"{kind:?}"
);
target_select(&mut app);
async fn enter_confirms_a_typed_query_and_the_next_enter_submits() {
target_select(&mut app).set_options(currencies());
press(&mut app, key(KeyCode::Enter));
typed(&mut app, "eu");
assert!(!fetched(&app), "{kind:?}");
assert!(app.pending_form.is_none(), "{kind:?}");
assert_eq!(target_select(&mut app).value(), "eur-id");
assert_eq!(target_select(&mut app).query(), "");
async fn backspacing_a_query_keeps_the_chosen_target_on_enter() {
target_select(&mut app).select_id("eur-id");
typed(&mut app, "d");
press(&mut app, key(KeyCode::Backspace));
let pending = app.pending_form.as_ref().expect("form is pending");
assert_eq!(pending.form.values().text("target"), Some("eur-id"));
fn open_flip_cell(app: &mut App) {
form.focus = form
.iter()
.position(|f| f.key == Some("groups"))
.expect("groups field");
let Widget::Rows(rows) = &mut form.fields[form.focus].widget else {
panic!("rows expected");
rows.add_row();
rows.focus_cell(0, "flip");
fn flip_select(app: &mut App) -> &mut crate::widgets::SelectWidget {
rows.focused_select_mut().expect("select cell")
async fn backspace_on_an_empty_query_keeps_the_top_level_value() {
let steps: [fn(&mut App); 3] = [
|_| {},
|app| {
press(app, key(KeyCode::Enter));
typed(app, "d");
},
press(app, key(KeyCode::Down));
for step in steps {
step(&mut app);
let before = target_select(&mut app).value().to_string();
assert_eq!(target_select(&mut app).value(), before);
async fn backspace_on_an_empty_query_keeps_a_rows_cell_value() {
for step in [KeyCode::Down, KeyCode::Up] {
let (mut app, _) = opened('a');
open_flip_cell(&mut app);
press(&mut app, key(step));
let before = flip_select(&mut app).value().to_string();
assert_eq!(flip_select(&mut app).value(), before);
async fn submitting_after_a_rows_select_query_sends_the_matched_value() {
typed(&mut app, "y");
let payload = forms::write_payload(ReportKind::Activity.descriptor(), &pending.form.values());
assert!(payload.contains(r#":flip "yes""#), "{payload}");