Lines
93.1 %
Functions
81.82 %
Branches
100 %
//! A bytecode-only script against the real server: the edit form's notice and
//! the refusal of a toggle, which has no source to save.
use crate::test_support::{key, press};
use cli_core::forms::parse_loaded_reply;
use cli_core::transaction::submit_call;
use forms::SCRIPT_EDIT;
use rpc::{ScriptCtx, Session, compile_script};
use server::user::User;
use sqlx::PgPool;
use sqlx::types::Uuid;
use super::scripts::pending;
use super::scripts::{load_states, scripts_app};
use crate::route::ScriptRoute;
use ratatui::crossterm::event::KeyCode;
async fn reply(session: &mut Session, form: &str) -> String {
session.handle_form(&format!("(:id 1 :form {form})")).await
}
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
let user_id = Uuid::new_v4();
sqlx::query(
"INSERT INTO users \
(id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
)
.bind(user_id)
.bind(format!("u{user_id}"))
.bind(format!("{user_id}@test.com"))
.execute(pool)
.await?;
Ok(user_id)
async fn bytecode_only(pool: &PgPool) -> anyhow::Result<(Session, String)> {
let user = seed_user(pool).await?;
let id = User { id: user }
.create_script(compile_script("(+ 1 2)")?, Some("old".to_string()))
Ok((Session::new(ScriptCtx::new(user))?, id.to_string()))
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn the_edit_form_shows_the_no_source_notice(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let (mut session, id) = bytecode_only(&pool).await?;
let loaded = reply(
&mut session,
&format!(r#"(load-form "script-edit" "{id}")"#),
.await;
let (mut app, _) = scripts_app();
app.deliver_script_edit_reply(Some(app.window_of(crate::view::ViewId::Scripts)), &loaded);
let Some(crate::modal::Modal::Form(form)) = app.overlays.top() else {
anyhow::bail!("the edit form is not open: {}", app.status);
};
assert!(
form.notices.iter().any(|n| n.contains("no source")),
"{:?}",
form.notices
);
assert_eq!(form.values().text("source"), Some(""));
Ok(())
async fn toggling_a_bytecode_only_script_shows_the_error_not_success(
pool: PgPool,
) -> anyhow::Result<()> {
load_states(&mut app, &[(&id, "old", true, false)])?;
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, &loaded);
let saving = pending(&app, |r| matches!(r, ScriptRoute::Toggled { .. }))
.ok_or_else(|| anyhow::anyhow!("save not sent"))?;
let mut values = parse_loaded_reply(&SCRIPT_EDIT, &loaded).map_err(anyhow::Error::msg)?;
values.set("enabled", "false");
let refused = reply(&mut session, &submit_call(&SCRIPT_EDIT, &values)).await;
app.route_reply(
saving,
&refused.replacen(":id 1", &format!(":id {saving}"), 1),
app.status.starts_with("can't switch old: source:"),
"{}",
app.status
assert!(!app.status.contains("disabled"), "{}", app.status);