Lines
100 %
Functions
Branches
//! Submitting the merge form asks the server what moves; only `y` on the
//! confirmation merges.
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use forms::FormValues;
use crate::app::App;
use crate::event::{Intent, apply};
use crate::modal::{ConfirmAction, Draft, Modal};
use crate::route::{Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::view::ViewId;
use ratatui::crossterm::event::KeyCode;
const ACCOUNT: &str = "550e8400-e29b-41d4-a716-446655440000";
const OTHER: &str = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
const PREVIEW_ID: i64 = 2;
const MERGE_ID: i64 = 3;
fn preview_wire(id: i64, splits: i32) -> String {
format!(r#"(:id {id} :value (:INTO "{OTHER}" :FROM "{ACCOUNT}" :SPLITS {splits}))"#)
}
fn merge_form_app() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.accounts_mut().state = Fetch::Loaded(Vec::new());
let mut values = FormValues::default();
values.set("from", ACCOUNT);
app.open_account_merge(values);
app.route_reply(
1,
&format!(
r#"(:id 1 :value "((:id \"{OTHER}\" :label \"Assets:Bank\" :alias \"x\") (:id \"{ACCOUNT}\" :label \"Assets:Cash\" :alias \"y\"))")"#
),
);
(app, sent)
fn previewing_app() -> (App, Sent) {
let (mut app, sent) = merge_form_app();
apply(&mut app, Intent::Submit);
fn confirm_prompt(app: &App) -> Option<&str> {
match app.overlays.top() {
Some(Modal::Confirm {
prompt,
action: ConfirmAction::MergeAccounts(_),
..
}) => Some(prompt),
_ => None,
fn merge_in_flight(app: &App) -> bool {
app.pending_routes
.get(&MERGE_ID)
.is_some_and(|route| matches!(route.ctx, RouteCtx::Mutation { .. }))
#[tokio::test]
async fn submit_previews_before_anything_is_confirmed() {
let (app, _) = previewing_app();
assert!(app.overlays.is_empty());
assert!(app.pending_form.is_some(), "the form waits for the preview");
assert!(matches!(
app.pending_routes.get(&PREVIEW_ID).map(|route| &route.ctx),
Some(RouteCtx::MergePreview)
));
async fn the_preview_reply_shows_what_moves() {
let (mut app, _) = previewing_app();
app.route_reply(PREVIEW_ID, &preview_wire(PREVIEW_ID, 3));
let prompt = confirm_prompt(&app).unwrap_or_default();
assert!(
prompt.starts_with("Merge account Assets:Cash into"),
"{prompt}"
assert!(prompt.contains("3 splits move"), "{prompt}");
assert!(prompt.ends_with("Confirm?"), "{prompt}");
assert!(app.pending_form.is_none());
assert!(app.pending_routes.is_empty(), "nothing merged yet");
async fn n_merges_nothing() {
press(&mut app, key(KeyCode::Char('n')));
assert!(app.pending_routes.is_empty(), "no merge was sent");
async fn y_sends_the_merge() {
press(&mut app, key(KeyCode::Char('y')));
assert!(merge_in_flight(&app), "the merge is the next request");
app.pending_form.is_some(),
"the form awaits the merge reply"
async fn a_preview_error_is_shown_and_merges_nothing() {
PREVIEW_ID,
r#"(:id 2 :error (:code args :message "cannot merge an account into itself"))"#,
assert!(confirm_prompt(&app).is_none());
assert!(app.pending_routes.is_empty());
assert!(matches!(app.overlays.top(), Some(Modal::Form(form)) if form.errors.len() == 1));
assert!(app.status.contains("into itself"), "{}", app.status);
async fn a_preview_reply_without_its_form_is_dropped() {
app.pending_routes.insert(
9,
Route {
target: Some(app.window_of(ViewId::Accounts)),
ctx: RouteCtx::MergePreview,
},
app.route_reply(9, &preview_wire(9, 3));
"the real preview is still awaited"
assert!(!app.pending_routes.contains_key(&9));
async fn a_replayed_preview_reply_changes_nothing() {
app.route_reply(PREVIEW_ID, &preview_wire(PREVIEW_ID, 9));
assert!(confirm_prompt(&app).is_some_and(|prompt| prompt.contains("3 splits")));
async fn a_preview_reply_under_another_dialog_is_dropped() {
app.open_dialog_on_front(Modal::Draft(Draft::new(Vec::new())));
assert!(matches!(app.overlays.top(), Some(Modal::Draft(_))));
app.status.contains("another dialog is open"),
"{}",
app.status
async fn a_malformed_preview_reopens_the_form() {
app.route_reply(PREVIEW_ID, r#"(:id 2 :value (:INTO "x"))"#);
app.status.contains("malformed merge preview"),
async fn the_confirmed_merge_form_is_drawn_where_it_stood() {
use crate::draw::pending_form_tests::rect_at;
use crate::overlay::Placement;
let (mut app, _) = merge_form_app();
crate::test_support::draw_into(&mut app, 100, 30);
let auto = app.hits.dialog().expect("the form was drawn").auto;
let moved = tvui::Place {
x: 9,
y: 3,
..tvui::Place::from(auto)
};
app.overlays.set_placement(Placement::of(moved, auto));
let rect = rect_at(&app, 100, 30).expect("the moved form is drawn");
assert!(app.pending_form.is_some());
assert_eq!(rect_at(&app, 100, 30), Some(rect));