Lines
97.09 %
Functions
100 %
Branches
//! Pending-form lifecycle: lock on dispatch, one terminal outcome per request.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::Form;
use crate::modal::Modal;
use crate::tabs::nms_eval::double::Sent;
const FORM_INVALID: &str = r#"(:id 1 :error (:code form-invalid :message "2 fields invalid" :detail ((:FIELD "symbol" :MESSAGE "too long") (:FIELD "rows.0.value" :MESSAGE "must be positive"))))"#;
fn filled_form() -> Form {
let mut form = Form::commodity_create();
for (idx, text) in ["EUR", "Euro"].into_iter().enumerate() {
form.focus = idx;
if let Some(editor) = form.focused_editor_mut() {
text.chars().for_each(|c| editor.insert_char(c));
}
form
fn buffers(form: &Form) -> (String, String) {
(
form.fields[0].widget.value().to_string(),
form.fields[1].widget.value().to_string(),
)
async fn submitted_app() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.open_dialog_on_front(Modal::Form(filled_form()));
apply(&mut app, Intent::Submit);
(app, sent)
fn unlocked_form(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("form must be restored, got {other:?}"),
#[tokio::test]
async fn dispatched_form_stays_locked_with_its_request_id() {
let (app, _) = submitted_app().await;
assert!(app.overlays.is_empty(), "the form leaves the overlay stack");
let pending = app.pending_form.as_ref().expect("form is pending");
assert_eq!(pending.request_id, 1);
assert_eq!(buffers(&pending.form), ("EUR".into(), "Euro".into()));
async fn success_closes_the_form() {
let (mut app, _) = submitted_app().await;
app.route_reply(1, r#"(:id 1 :value "ok")"#);
assert!(app.pending_form.is_none());
assert!(app.overlays.is_empty());
async fn form_invalid_restores_values_and_shows_field_messages() {
app.route_reply(1, FORM_INVALID);
let form = unlocked_form(&app);
assert_eq!(buffers(form), ("EUR".into(), "Euro".into()));
assert_eq!(
form.errors,
vec!["Symbol: too long", "rows.0.value: must be positive"]
);
async fn other_error_restores_with_the_server_message() {
app.route_reply(1, r#"(:id 1 :error (:code db :message "query failed"))"#);
assert_eq!(form.errors, vec!["query failed"]);
async fn stale_reply_leaves_the_pending_form_alone() {
app.route_reply(99, r#"(:id 99 :error (:code db :message "other"))"#);
assert!(app.pending_form.is_some(), "only its own reply settles it");
async fn input_while_pending_is_ignored_and_never_dispatches_twice() {
apply(&mut app, Intent::CloseTopmost);
apply(&mut app, Intent::InsertChar('x'));
let pending = app.pending_form.as_ref().expect("still pending");
async fn unlocked_form_can_be_resubmitted() {
app.route_reply(1, r#"(:id 1 :error (:code db :message "boom"))"#);
let pending = app.pending_form.as_ref().expect("resubmitted");
assert_eq!(pending.request_id, 2);
async fn quit_is_honoured_while_pending() {
apply(&mut app, Intent::QuitNow);
assert!(app.should_quit);
async fn resubmit_clears_the_previous_rejection() {
assert!(pending.form.errors.is_empty());
#[test]
fn local_validation_failure_drops_stale_rejection_lines() {
form.errors = vec!["old".to_string()];
app.open_dialog_on_front(Modal::Form(form));
let Some(Modal::Form(form)) = app.overlays.top() else {
panic!("form must stay open");
};
assert!(form.errors.is_empty());