Lines
96.52 %
Functions
81.4 %
Branches
100 %
//! The script create, edit and toggle flows through the real key dispatch.
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{ctrl, key, press};
use forms::{FormValues, SCRIPT_EDIT, write_payload};
use super::edit::{top_form, wire_of};
use super::scripts::{OLD, SYNC, load, load_states, pending, scripts_app};
use super::template_forms::{pending_request, rejected};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::FormKind;
use crate::route::ScriptRoute;
use ratatui::crossterm::event::KeyCode;
const SOURCE: &str = "(+ 1 2)";
fn type_text(app: &mut App, text: &str) {
text.chars().for_each(|c| apply(app, Intent::InsertChar(c)));
}
fn edit_values(id: &str, enabled: &str) -> FormValues {
let mut values = FormValues::default();
values.set("id", id);
values.set("revision", "rev-1");
values.set("name", "sync");
values.set("source", SOURCE);
values.set("enabled", enabled);
values
fn edit_wire(values: &FormValues, notice: Option<&str>) -> String {
let mut wire = wire_of(&write_payload(&SCRIPT_EDIT, values));
if let Some(notice) = notice {
wire.truncate(wire.len() - 1);
wire.push_str(&format!(r#" :notices ("{notice}"))"#));
wire
fn created_wire(request: i64, id: &str) -> String {
format!(r#"(:id {request} :value "{id}")"#)
async fn echoed(app: &mut App, sent: &Sent) -> String {
tokio::task::yield_now().await;
app.drain_eval();
sent.frames().join("\n")
#[tokio::test]
async fn c_opens_the_create_form_and_a_saved_script_is_selected() -> anyhow::Result<()> {
let (mut app, sent) = scripts_app();
load(&mut app, &[(SYNC, "sync")])?;
press(&mut app, key(KeyCode::Char('c')));
assert_eq!(top_form(&app).kind, FormKind::ScriptCreate);
type_text(&mut app, "tags");
apply(&mut app, Intent::NextField);
type_text(&mut app, SOURCE);
press(&mut app, ctrl(KeyCode::Char('s')));
let request = pending_request(&app)?;
let echoed = echoed(&mut app, &sent).await;
assert!(
echoed.contains(r#"(submit-form "script-create""#),
"{echoed}"
);
app.route_reply(request, &created_wire(request, OLD));
assert!(app.pending_form.is_none());
load(&mut app, &[(SYNC, "sync"), (OLD, "tags")])?;
assert_eq!(app.scripts().selected_key(), Some(OLD));
Ok(())
async fn a_source_that_does_not_compile_shows_at_the_source_field() -> anyhow::Result<()> {
let (mut app, _) = scripts_app();
load(&mut app, &[])?;
type_text(&mut app, "(oops");
let message = "unexpected end\\n(oops\\n ^";
app.route_reply(request, &rejected(request, "source", message));
assert_eq!(
top_form(&app).errors,
["Source: unexpected end\n(oops\n ^"]
assert_eq!(top_form(&app).values().text("source"), Some("(oops"));
async fn e_loads_the_script_with_its_notice_and_submits_it_with_its_revision() -> anyhow::Result<()>
{
press(&mut app, key(KeyCode::Char('e')));
assert!(pending(&app, |r| matches!(r, ScriptRoute::Edit)).is_some());
let text = echoed(&mut app, &sent).await;
text.contains(&format!(r#"(load-form "script-edit" "{SYNC}")"#)),
"{text}"
let notice = "uploaded as bytecode and has no source";
app.deliver_script_edit_reply(
Some(app.window_of(crate::view::ViewId::Scripts)),
&edit_wire(&edit_values(SYNC, "true"), Some(notice)),
assert_eq!(top_form(&app).kind, FormKind::ScriptEdit);
assert_eq!(top_form(&app).notices, [notice]);
assert_eq!(top_form(&app).hidden[1], ("revision", "rev-1".to_string()));
assert_eq!(top_form(&app).values().text("source"), Some(SOURCE));
apply(&mut app, Intent::Submit);
assert!(app.pending_form.is_some());
text.contains(r#"(submit-form "script-edit" "(:id "#),
assert!(text.contains(r#":revision \"rev-1\""#), "{text}");
async fn a_saved_edit_refreshes_the_list() -> anyhow::Result<()> {
&edit_wire(&edit_values(SYNC, "true"), None),
app.route_reply(request, &created_wire(request, SYNC));
assert!(app.scripts().any_loading());
async fn a_stale_revision_keeps_the_form_and_shows_the_reload_hint() -> anyhow::Result<()> {
let message = "the script 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)"),
"{}",
assert_eq!(top_form(&app).values().text("name"), Some("sync"));
async fn a_script_that_vanished_shows_not_found_instead_of_the_form() {
r#"(:id 1 :error (:code not-found :message "no such script"))"#,
app.status.starts_with("can't edit this script"),
assert!(app.overlays.is_empty());
async fn t_loads_the_script_then_saves_it_with_the_other_state() -> anyhow::Result<()> {
press(&mut app, key(KeyCode::Char('t')));
let loading = pending(&app, |r| matches!(r, ScriptRoute::Toggle { .. }))
.ok_or_else(|| anyhow::anyhow!("load not sent"))?;
app.route_reply(loading, &edit_wire(&edit_values(SYNC, "true"), None));
let saving = pending(&app, |r| matches!(r, ScriptRoute::Toggled { .. }))
.ok_or_else(|| anyhow::anyhow!("save not sent"))?;
assert!(text.contains(r#":enabled \"false\""#), "{text}");
app.route_reply(saving, &created_wire(saving, SYNC));
assert_eq!(app.status, "sync disabled");
async fn t_on_a_disabled_script_switches_it_on() -> anyhow::Result<()> {
load_states(&mut app, &[(SYNC, "sync", false, true)])?;
app.route_reply(loading, &edit_wire(&edit_values(SYNC, "false"), None));
assert!(text.contains(r#":enabled \"true\""#), "{text}");
assert_eq!(app.status, "sync enabled");
async fn a_stale_toggle_shows_the_servers_refusal() -> anyhow::Result<()> {
app.route_reply(saving, &rejected(saving, "revision", message));
app.status,
format!("can't switch sync: revision: {message}")
assert!(!app.scripts().any_loading());
async fn a_toggle_whose_load_fails_says_so() -> anyhow::Result<()> {
app.route_reply(
loading,
&format!(r#"(:id {loading} :error (:code not-found :message "no such script"))"#),
app.status.starts_with("can't switch sync: no such script"),
assert!(pending(&app, |r| matches!(r, ScriptRoute::Toggled { .. })).is_none());