Lines
100 %
Functions
Branches
//! Unit tests for the palette command form builder.
use super::{
build_account_balance, build_account_merge, build_form, build_report_request,
build_transaction_filter, parse_args, report_arg_keys, report_ready,
};
use crate::tabs::reports::ReportKind;
use cli_core::CommandError;
fn args(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}
fn path(segments: &[&str]) -> Vec<String> {
segments.iter().map(|s| s.to_string()).collect()
// ── version ──────────────────────────────────────────────────────────────────
#[test]
fn version_produces_version_form() {
let form = build_form(&path(&["version"]), &[]).unwrap().unwrap();
assert_eq!(form, "(get-version)");
// ── account ──────────────────────────────────────────────────────────────────
fn account_balance_takes_the_account_label_or_id() {
let account = build_account_balance(&args(&[("account", "Cash")])).unwrap();
assert_eq!(account, "Cash");
fn account_balance_without_an_account_is_error() {
for given in [vec![], vec![("account", "")]] {
let res = build_account_balance(&args(&given));
assert!(matches!(res, Err(CommandError::Argument(_))), "{res:?}");
fn account_balance_rejects_unknown_and_duplicate_args() {
let unknown = build_account_balance(&args(&[("account", "a"), ("x", "1")]));
assert_eq!(
unknown.unwrap_err().to_string(),
"Arguments error: unknown arg: x"
);
let dup = build_account_balance(&args(&[("account", "a"), ("account", "b")]));
dup.unwrap_err().to_string(),
"Arguments error: duplicate arg: account"
fn account_create_opens_modal_path() {
let result = build_form(&path(&["account", "create"]), &[]);
assert!(result.is_ok(), "account create must not error");
assert!(
result.unwrap().is_none(),
"account create must return Ok(None) for modal path"
// ── commodity ────────────────────────────────────────────────────────────────
fn list_and_edit_leaves_have_no_eval_form() {
for leaf in [
["account", "list"],
["commodity", "list"],
["account", "edit"],
["commodity", "edit"],
] {
assert!(build_form(&path(&leaf), &[]).unwrap().is_none(), "{leaf:?}");
fn commodity_create_opens_modal_path() {
let result = build_form(&path(&["commodity", "create"]), &[]);
assert!(result.is_ok(), "commodity create must not error");
"commodity create must return Ok(None) for modal path"
fn commodity_convert_opens_modal_path() {
let result = build_form(&path(&["commodity", "convert"]), &[]);
assert!(result.is_ok(), "commodity convert must not error");
"commodity convert must return Ok(None) for modal path"
// ── transaction ───────────────────────────────────────────────────────────────
fn transaction_list_is_not_an_eval_leaf() {
let res = build_form(&path(&["transaction", "list"]), &args(&[("account", "x")]));
assert!(matches!(res, Ok(None)), "got: {res:?}");
fn transaction_filter_takes_the_descriptor_keys() {
let filter = build_transaction_filter(&args(&[
("account", "Cash"),
("from", "2026-01-01"),
("to", "2026-02-01"),
]))
.unwrap();
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"));
fn transaction_filter_rejects_unknown_and_paging_keys() {
for key in ["bogus", "limit", "offset"] {
let res = build_transaction_filter(&args(&[(key, "1")]));
matches!(res, Err(CommandError::Argument(_))),
"{key}: {res:?}"
fn transaction_create_opens_modal_path() {
// transaction create always opens the form modal regardless of args.
let result = build_form(&path(&["transaction", "create"]), &[]);
assert!(result.is_ok(), "transaction create must not error");
"transaction create must return Ok(None) for modal path"
fn argless_leaves_reject_any_arg() {
&["version"][..],
&["account", "create"],
&["account", "edit"],
&["account", "list"],
&["commodity", "create"],
&["commodity", "edit"],
&["commodity", "convert"],
&["commodity", "list"],
&["transaction", "create"],
let res = build_form(&path(leaf), &args(&[("name", "x")]));
res.unwrap_err().to_string(),
"Arguments error: unknown arg: name",
"{leaf:?}"
fn transaction_filter_rejects_a_duplicate_key() {
let res = build_transaction_filter(&args(&[("from", "2026-01-01"), ("from", "2026-02-01")]));
"Arguments error: duplicate arg: from"
fn transaction_filter_treats_an_empty_value_as_not_given() {
let filter = build_transaction_filter(&args(&[("account", ""), ("to", "2026-02-01")])).unwrap();
assert_eq!(filter.text("account"), None);
fn account_merge_takes_from_and_into_once() {
let values = build_account_merge(&args(&[("from", "A"), ("into", "")])).unwrap();
assert_eq!(values.text("from"), Some("A"));
assert_eq!(values.text("into"), None);
let dup = build_account_merge(&args(&[("from", "A"), ("from", "B")]));
let unknown = build_account_merge(&args(&[("to", "B")]));
"Arguments error: unknown arg: to"
fn parse_args_keeps_given_pairs_and_drops_empty_values() {
let given = args(&[("a", "1"), ("b", "")]);
assert_eq!(parse_args(&["a", "b"], &given).unwrap(), vec![("a", "1")]);
// ── config ────────────────────────────────────────────────────────────────────
fn config_get_produces_get_config() {
let form = build_form(&path(&["config", "get"]), &args(&[("key", "theme")]))
.unwrap()
assert_eq!(form, r#"(get-config "theme")"#);
fn config_get_missing_key_is_error() {
assert!(build_form(&path(&["config", "get"]), &[]).is_err());
fn config_get_and_set_reject_unknown_and_duplicate_args() {
for leaf in ["get", "set"] {
let unknown = build_form(&path(&["config", leaf]), &args(&[("bogus", "1")]));
"Arguments error: unknown arg: bogus"
let dup = build_form(
&path(&["config", leaf]),
&args(&[("key", "a"), ("key", "b")]),
"Arguments error: duplicate arg: key"
fn config_set_returns_none_for_modal_path() {
build_form(&path(&["config", "set"]), &[])
.is_none()
// ── build_report_request ─────────────────────────────────────────────────────
fn reports_balance_requires_nothing_so_it_is_ready_without_args() {
let req = build_report_request(&path(&["reports", "balance"]), &[])
assert!(report_ready(req.kind, &req.values));
assert_eq!(req.values.text("from"), None);
fn reports_balance_with_a_from_date_returns_request() {
let req = build_report_request(
&path(&["reports", "balance"]),
&args(&[("from", "2026-01-01")]),
)
assert_eq!(req.kind, ReportKind::Balance);
assert_eq!(req.values.text("from"), Some("2026-01-01"));
assert_eq!(req.values.text("to"), None);
fn reports_activity_passes_the_typed_dates_to_the_form() {
&path(&["reports", "activity"]),
&args(&[("from", "2026-01-01"), ("to", "2026-03-31")]),
assert_eq!(req.kind, ReportKind::Activity);
assert_eq!(req.values.text("to"), Some("2026-03-31"));
fn reports_activity_with_only_from_is_not_ready_but_keeps_it() {
!report_ready(req.kind, &req.values),
"a missing end date must open the form"
fn a_path_outside_reports_is_not_a_report_request() {
let result = build_report_request(&path(&["account", "list"]), &[]).unwrap();
assert!(result.is_none());
fn reports_breakdown_passes_the_typed_dates_to_the_form() {
&path(&["reports", "breakdown"]),
&args(&[("from", "2026-01-01"), ("to", "2026-06-30")]),
assert_eq!(req.kind, ReportKind::Breakdown);
assert_eq!(req.values.text("to"), Some("2026-06-30"));
fn reports_activity_rfc3339_reaches_the_form_unchanged() {
let from = "2026-01-01T09:00:00+09:00";
let to = "2026-03-31T23:59:59.5+00:00";
&args(&[("from", from), ("to", to)]),
assert_eq!(req.values.text("from"), Some(from));
assert_eq!(req.values.text("to"), Some(to));
fn request(report: &str, pairs: &[(&str, &str)]) -> Result<super::ReportRequest, CommandError> {
build_report_request(&path(&["reports", report]), &args(pairs)).map(|req| req.unwrap())
fn every_report_carries_its_non_date_args() {
let balance = request("balance", &[("as-of", "2026-02-01"), ("filter", "x")]).unwrap();
assert_eq!(balance.values.text("as-of"), Some("2026-02-01"));
assert_eq!(balance.values.text("filter"), Some("x"));
let activity = request("activity", &[("grouping", "month")]).unwrap();
assert_eq!(activity.values.text("grouping"), Some("month"));
let breakdown = request("breakdown", &[("tag", "category"), ("sort", "name_asc")]).unwrap();
assert_eq!(breakdown.values.text("tag"), Some("category"));
assert_eq!(breakdown.values.text("sort"), Some("name_asc"));
fn an_unknown_key_is_an_error() {
for (report, key) in [
("balance", "chart"),
("balance", "to"),
("activity", "groups"),
("breakdown", "nonsense"),
let err = request(report, &[("from", "2026-01-01"), (key, "x")])
.err()
err.to_string().ends_with(&format!("unknown arg: {key}")),
"{report}"
fn a_choice_given_as_a_label_resolves_to_its_key() {
let req = request("breakdown", &[("sort", "Amount, smallest first")]).unwrap();
assert_eq!(req.values.text("sort"), Some("amount_asc"));
let req = request("activity", &[("grouping", "QUARTER")]).unwrap();
assert_eq!(req.values.text("grouping"), Some("quarter"));
fn an_unknown_choice_is_an_error() {
let err = request("activity", &[("grouping", "decade")])
assert!(err.to_string().contains("grouping"), "{err}");
fn a_select_arg_stays_as_typed() {
let req = request("balance", &[("target", "usd")]).unwrap();
assert_eq!(req.values.text("target"), Some("usd"));
fn a_duplicate_key_is_an_error() {
for pairs in [
[("from", "2026-01-01"), ("from", "2026-02-01")],
[("target", "usd"), ("target", "")],
let err = request("balance", &pairs).err().unwrap();
assert!(err.to_string().contains("duplicate arg"), "{err}");
fn only_a_bare_command_is_interactive() {
assert!(request("balance", &[]).unwrap().interactive);
!request("balance", &[("from", "2026-01-01")])
.interactive
fn arg_keys_are_the_descriptor_fields_without_rows() {
let keys: Vec<_> = report_arg_keys(ReportKind::Activity).collect();
assert_eq!(keys, ["target", "from", "to", "grouping", "filter"]);