Lines
99.52 %
Functions
100 %
Branches
//! `m` on the Accounts tab and the `account merge` palette leaf open the
//! merge form with `from` prefilled and fetch only the account options.
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::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use crate::view::Tab;
use cli_core::render::ListRow;
use ratatui::crossterm::event::KeyCode;
const ACCOUNT: &str = "550e8400-e29b-41d4-a716-446655440000";
const OTHER: &str = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
fn accounts_app() -> App {
let mut app = crate::test_support::app();
app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
id: Some(ACCOUNT.to_string()),
cells: vec!["Assets:Cash".to_string()],
}]);
app.switch_tab(Tab::Accounts);
app
}
fn top_form(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("expected a form, got {other:?}"),
fn field_value<'a>(form: &'a Form, key: &str) -> &'a str {
form.fields
.iter()
.find(|field| field.key == Some(key))
.map_or("", |field| field.widget.value())
fn options_reply(id: i64) -> String {
format!(
r#"(:id {id} :value "((:id \"{OTHER}\" :label \"Assets:Bank\" :alias \"x\") (:id \"{ACCOUNT}\" :label \"Assets:Cash\" :alias \"y\"))")"#
)
fn run_palette(app: &mut App, line: &str) {
super::super::palette_run::submit_palette(app, line);
fn echo_app() -> (App, Sent) {
let mut app = accounts_app();
let sent = app.attach_echo();
(app, sent)
fn reply_with(app: &mut App, options: &[(&str, &str)]) {
let list = options
.map(|(id, label)| format!(r#"(:id \"{id}\" :label \"{label}\" :alias \"x\")"#))
.collect::<Vec<_>>()
.join(" ");
app.route_reply(1, &format!(r#"(:id 1 :value "({list})")"#));
async fn merge_via_palette(line: &str) -> App {
let (mut app, _) = echo_app();
app.transactions_mut().state = Fetch::Loaded(Vec::new());
app.switch_tab(Tab::Transactions);
run_palette(&mut app, line);
reply_with(
&mut app,
&[(OTHER, "Assets:Bank"), (ACCOUNT, "Assets:Cash")],
);
#[tokio::test]
async fn m_on_the_accounts_tab_opens_the_merge_form_from_the_selection() {
press(&mut app, key(KeyCode::Char('m')));
let form = top_form(&app);
assert_eq!(form.kind, FormKind::AccountMerge);
assert_eq!(field_value(form, "from"), ACCOUNT);
#[test]
fn m_is_not_bound_on_other_tabs() {
app.switch_tab(Tab::Commodities);
assert!(app.overlays.is_empty());
assert!(app.status.is_empty());
async fn merge_from_the_selection_prefills_from() {
apply(&mut app, Intent::ListMergeAccount);
assert_eq!(field_value(form, "into"), "");
async fn the_prefill_survives_the_options_reply() {
app.attach_echo();
app.route_reply(1, &options_reply(1));
assert_eq!(form.select_label("from"), Some("Assets:Cash"));
fn nothing_selected_sets_the_status() {
assert_eq!(app.status, "no account selected");
async fn the_merge_form_fetches_only_the_accounts() {
tokio::task::yield_now().await;
app.drain_eval();
let frames = sent.frames().join("\n");
assert!(frames.contains(r#"(form-options "accounts")"#), "{frames}");
assert!(!frames.contains("commodities"), "{frames}");
async fn the_palette_leaf_without_args_uses_the_selected_account() {
run_palette(&mut app, "account merge");
assert_eq!(field_value(top_form(&app), "from"), ACCOUNT);
async fn the_palette_leaf_without_args_is_blank_off_the_accounts_tab() {
assert_eq!(field_value(form, "from"), "");
async fn a_label_resolves_to_its_account() {
let app = merge_via_palette("account merge from=Assets:Cash").await;
async fn an_uppercase_uuid_resolves() {
let line = format!("account merge from={}", OTHER.to_uppercase());
let app = merge_via_palette(&line).await;
assert_eq!(field_value(top_form(&app), "from"), OTHER);
async fn into_is_prefilled_too() {
let app = merge_via_palette("account merge from=Assets:Cash into=Assets:Bank").await;
assert_eq!(field_value(form, "into"), OTHER);
async fn into_alone_keeps_the_selection_as_from_on_the_accounts_tab() {
run_palette(&mut app, "account merge into=Assets:Bank");
async fn an_unknown_label_is_a_command_error_and_opens_nothing() {
let app = merge_via_palette("account merge from=Assets:Nowhere").await;
assert!(app.status.starts_with("command error:"), "{}", app.status);
assert!(app.status.contains("Assets:Nowhere"), "{}", app.status);
async fn an_ambiguous_label_is_a_command_error_and_opens_nothing() {
run_palette(&mut app, "account merge from=Assets:Cash");
&[(OTHER, "Assets:Cash"), (ACCOUNT, "Assets:Cash")],
assert!(app.status.contains("ambiguous"), "{}", app.status);
async fn an_unknown_key_is_a_command_error_and_fetches_nothing() {
run_palette(&mut app, "account merge form=Assets:Cash");
assert_eq!(
app.status,
"command error: Arguments error: unknown arg: form"
assert!(app.console().scrollback.is_empty());
async fn a_duplicate_key_is_a_command_error() {
run_palette(&mut app, "account merge from=Assets:Cash from=Assets:Bank");
"command error: Arguments error: duplicate arg: from"
async fn a_stale_options_reply_is_dropped() {
run_palette(&mut app, "account merge from=Assets:Bank");
reply_with(&mut app, &[(ACCOUNT, "Assets:Cash")]);
app.route_reply(2, &options_reply(2));
async fn an_unrelated_options_fetch_does_not_drop_the_merge_lookup() {
app.fetch_account_form_options();