Lines
99.38 %
Functions
96.55 %
Branches
100 %
//! The template create and edit forms through the real key dispatch.
use crate::test_support::{ctrl, key, press};
use forms::{FormValues, TEMPLATE_EDIT, write_payload};
use super::edit::{top_form, wire_of};
use super::templates::{RENT, SALARY, load, templates_app};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::FormKind;
use crate::route::{RouteCtx, TemplateRoute};
use ratatui::crossterm::event::KeyCode;
const SOURCE: &str = "(set-draft-note \"rent\")";
fn type_text(app: &mut App, text: &str) {
text.chars().for_each(|c| apply(app, Intent::InsertChar(c)));
}
pub(super) fn pending_request(app: &App) -> anyhow::Result<i64> {
app.pending_form
.as_ref()
.map(|p| p.request_id)
.ok_or_else(|| anyhow::anyhow!("form not submitted"))
pub(super) fn edit_payload() -> String {
let mut values = FormValues::default();
values.set("id", RENT);
values.set("revision", "rev-1");
values.set("name", "rent");
values.set("source", "(set-draft-note \"a\")\n(set-draft-note \"b\")");
write_payload(&TEMPLATE_EDIT, &values)
pub(super) fn rejected(request: i64, field: &str, message: &str) -> String {
format!(
r#"(:id {request} :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "{field}" :MESSAGE "{message}"))))"#
)
fn created_wire(request: i64, id: &str) -> String {
format!(r#"(:id {request} :value "{id}")"#)
#[tokio::test]
async fn c_opens_the_create_form_and_a_saved_template_is_selected() -> anyhow::Result<()> {
let (mut app, sent) = templates_app();
load(&mut app, &[(RENT, "rent")])?;
press(&mut app, key(KeyCode::Char('c')));
assert_eq!(top_form(&app).kind, FormKind::TemplateCreate);
type_text(&mut app, "salary");
apply(&mut app, Intent::NextField);
type_text(&mut app, SOURCE);
press(&mut app, ctrl(KeyCode::Char('s')));
let request = pending_request(&app)?;
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(
echoed.contains(r#"(submit-form "template-create""#),
"{echoed}"
);
app.route_reply(request, &created_wire(request, SALARY));
assert!(app.pending_form.is_none());
load(&mut app, &[(RENT, "rent"), (SALARY, "salary")])?;
assert_eq!(app.templates().selected_key(), Some(SALARY));
Ok(())
async fn a_source_that_does_not_compile_shows_at_the_source_field() -> anyhow::Result<()> {
let (mut app, _) = templates_app();
load(&mut app, &[])?;
type_text(&mut app, "(oops");
app.route_reply(request, &rejected(request, "source", "unclosed paren"));
assert_eq!(top_form(&app).errors, ["Source: unclosed paren"]);
assert_eq!(top_form(&app).values().text("source"), Some("(oops"));
assert!(app.status.contains("unclosed paren"), "{}", app.status);
async fn e_loads_the_selected_template_and_submits_it_with_its_revision() -> anyhow::Result<()> {
press(&mut app, key(KeyCode::Char('e')));
app.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::Template(TemplateRoute::Edit)))
echoed.contains(&format!(r#"(load-form "template-edit" "{RENT}")"#)),
app.deliver_template_edit_reply(
Some(app.window_of(crate::view::ViewId::Templates)),
&wire_of(&edit_payload()),
assert_eq!(top_form(&app).kind, FormKind::TemplateEdit);
assert_eq!(top_form(&app).hidden[1], ("revision", "rev-1".to_string()));
assert_eq!(top_form(&app).values().text("name"), Some("rent"));
apply(&mut app, Intent::Submit);
assert!(app.pending_form.is_some());
echoed.contains(r#"(submit-form "template-edit" "(:id "#),
assert!(echoed.contains(r#":revision \"rev-1\""#), "{echoed}");
async fn enter_on_the_list_edits_like_e() -> anyhow::Result<()> {
press(&mut app, key(KeyCode::Enter));
async fn a_saved_edit_refreshes_the_list() -> anyhow::Result<()> {
app.route_reply(request, &created_wire(request, RENT));
assert!(app.templates().any_loading());
async fn a_stale_revision_keeps_the_form_and_shows_the_reload_hint() -> anyhow::Result<()> {
let message = "the template changed since it was loaded; reload it";
app.route_reply(request, &rejected(request, "revision", message));
assert_eq!(top_form(&app).errors, [message]);
app.status
.ends_with("(close the form and open it again to reload)"),
"{}",
async fn a_template_that_vanished_shows_not_found_instead_of_the_form() {
r#"(:id 1 :error (:code not-found :message "no such template"))"#,
app.status.starts_with("can't edit this template"),
assert!(app.overlays.is_empty());
async fn e_without_a_selection_says_so() {
assert_eq!(app.status, "no template selected");
async fn a_list_reply_does_not_block_opening_the_edit_form() -> anyhow::Result<()> {
press(&mut app, ctrl(KeyCode::Char('r')));