Lines
92.62 %
Functions
100 %
Branches
//! Tests for the transaction-create modal and the list edit intent.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::{Form, FormKind};
use crate::modal::Modal;
use crate::tabs::fetch::Fetch;
use crate::view::Tab;
use crate::widgets::splits::COL_FROM;
use crate::widgets::{DateWidget, SelectOption, Widget};
use cli_core::render::ListRow;
fn loaded_transactions_app() -> App {
let mut app = crate::test_support::app();
app.switch_tab(Tab::Transactions);
let uuid = "550e8400-e29b-41d4-a716-446655440000";
app.transactions_mut().state = Fetch::Loaded(vec![ListRow {
id: Some(uuid.to_string()),
cells: vec!["2024-06-01".to_string(), "Groceries".to_string()],
}]);
app
}
#[tokio::test]
async fn palette_transaction_create_opens_modal_and_fetches_options() {
let sent = app.attach_echo();
apply(&mut app, Intent::OpenCommandLine);
for c in "transaction create".chars() {
apply(&mut app, Intent::InsertChar(c));
apply(&mut app, Intent::Submit);
match app.overlays.top() {
Some(Modal::Form(f)) => {
assert_eq!(f.kind, FormKind::TransactionCreate);
assert_eq!(f.fields.len(), 4, "date + note + tags + splits");
other => panic!("expected TransactionCreate modal, got {other:?}"),
// The open form requested both option lists; the echo worker reflects
// both frames, so draining yields no error notices (the worker is alive).
tokio::task::yield_now().await;
app.drain_eval();
assert!(
!sent.frames().iter().any(|l| l.contains("worker stopped")),
"both option fetches must dispatch without stopping the worker"
);
#[test]
fn split_row_select_widget_first_navigates_options() {
let mut form = Form::transaction_create();
form.focus = 3;
if let Widget::Splits(ref mut sw) = form.fields[3].widget {
sw.col_focus = COL_FROM;
sw.set_account_options(vec![
SelectOption {
id: "acc-0".to_string(),
label: "Cash".to_string(),
},
id: "acc-1".to_string(),
label: "Food".to_string(),
]);
app.overlays.push(Modal::Form(form));
// SelectNext drives the focused row's from-account Select (nested widget-first).
apply(&mut app, Intent::SelectNext);
if let Widget::Splits(sw) = &f.fields[3].widget {
assert_eq!(sw.rows()[0].from.value(), "acc-1");
} else {
panic!("field 2 must be Splits");
other => panic!("expected form modal, got {other:?}"),
fn list_edit_on_loaded_transactions_tab_dispatches_edit_async() {
// With no eval worker attached, open_transaction_edit_async sets status
// "console not connected" — proving the dispatch path was taken.
let mut app = loaded_transactions_app();
apply(&mut app, Intent::EditRow);
assert_eq!(
app.status, "console not connected",
"EditRow on Transactions should attempt to load the transaction"
app.overlays.is_empty(),
"no form pushed synchronously (async open)"
fn list_edit_on_transactions_with_no_selection_sets_status() {
// transactions list is Idle (no loaded rows).
assert_eq!(app.status, "no transaction selected");
assert!(app.overlays.is_empty());
fn date_step_forward_intent_advances_date_field_by_one_day() {
// Seed a known date in the date field so the step outcome is deterministic.
if let Widget::Date(ref mut dw) = form.fields[0].widget {
*dw = DateWidget::with_value("2024-06-15T10:00");
form.focus = 0;
apply(&mut app, Intent::DateStepForward);
Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "2024-06-16T10:00"),
fn date_step_back_intent_retreats_date_field_by_one_day() {
apply(&mut app, Intent::DateStepBack);
Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "2024-06-14T10:00"),
fn stepped_date_only(mut form: Form, key: &str, intent: Intent) -> String {
let index = form
.fields
.iter()
.position(|field| field.key == Some(key))
.expect("date field");
form.fields[index].widget = Widget::Date(DateWidget::with_value("2024-01-31"));
form.focus = index;
apply(&mut app, intent);
Some(Modal::Form(f)) => f.fields[index].widget.value().to_string(),
fn stepping_a_post_date_or_a_report_day_keeps_it_date_only() {
let post = Form::transaction_create();
stepped_date_only(post, "date", Intent::DateStepForward),
"2024-02-01"
let report = Form::report(
crate::tabs::reports::ReportKind::Activity,
&forms::FormValues::default(),
stepped_date_only(report, "to", Intent::DateStepBack),
"2024-01-30"