Lines
96.55 %
Functions
100 %
Branches
use forms::{FormValues, SCRIPT_CREATE, SCRIPT_EDIT};
use server::script_state::STALE_SCRIPT;
use server::user::User;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_seed::seed_user;
use crate::forms::{Entity, Failure, load, load_noticed, submit};
use crate::script::compile_script;
const SOURCE: &str = "(+ 1 2)";
fn created(name: Option<&str>, source: &str, enabled: Option<&str>) -> FormValues {
let mut values = FormValues::default();
if let Some(name) = name {
values.set("name", name);
}
values.set("source", source);
if let Some(enabled) = enabled {
values.set("enabled", enabled);
values
async fn create(user: Uuid, name: Option<&str>, enabled: Option<&str>) -> anyhow::Result<Uuid> {
let values = created(name, SOURCE, enabled);
Ok(submit(user, &SCRIPT_CREATE, &values).await?.id)
async fn loaded(user: Uuid, script: Uuid) -> anyhow::Result<FormValues> {
Ok(load(user, &SCRIPT_EDIT, &script.to_string()).await?)
async fn invalid(
user: Uuid,
form: &forms::Form,
values: &FormValues,
) -> anyhow::Result<Vec<(String, String)>> {
match submit(user, form, values).await {
Err(Failure::Invalid(errors)) => {
Ok(errors.into_iter().map(|e| (e.field, e.message)).collect())
other => anyhow::bail!("expected field errors, got {other:?}"),
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn create_stores_source_and_bytecode_and_loads_back(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let id = create(user, Some("sync"), None).await?;
let values = loaded(user, id).await?;
assert_eq!(values.text("id"), Some(id.to_string().as_str()));
assert_eq!(values.text("revision").map(str::len), Some(64));
assert_eq!(values.text("name"), Some("sync"));
assert_eq!(values.text("source"), Some(SOURCE));
assert_eq!(values.text("enabled"), Some("true"));
let stored = User { id: user }.get_script(id).await?;
assert_eq!(stored.bytecode, compile_script(SOURCE)?);
Ok(())
async fn a_script_created_disabled_loads_disabled(pool: PgPool) -> anyhow::Result<()> {
let id = create(user, None, Some("false")).await?;
assert_eq!(loaded(user, id).await?.text("enabled"), Some("false"));
async fn a_compile_error_is_a_field_error_at_source_and_stores_nothing(
pool: PgPool,
) -> anyhow::Result<()> {
let mut broken = loaded(user, id).await?;
broken.set("source", "(unclosed");
let on_edit = invalid(user, &SCRIPT_EDIT, &broken).await?;
let on_create = invalid(user, &SCRIPT_CREATE, &created(None, "(unclosed", None)).await?;
for errors in [on_edit, on_create] {
assert_eq!(errors.len(), 1, "{errors:?}");
assert_eq!(errors[0].0, "source");
assert!(!errors[0].1.is_empty(), "{errors:?}");
let scripts = User { id: user }.list_scripts().await?;
assert_eq!(scripts.len(), 1);
assert_eq!(loaded(user, id).await?.text("source"), Some(SOURCE));
async fn an_edit_replaces_name_source_and_state_together(pool: PgPool) -> anyhow::Result<()> {
let id = create(user, Some("old"), None).await?;
let mut values = loaded(user, id).await?;
let before = values.text("revision").map(str::to_string);
values.set("name", "new");
values.set("source", "(+ 2 3)");
values.set("enabled", "false");
let submitted = submit(user, &SCRIPT_EDIT, &values).await?;
assert_eq!(submitted.id, id);
let after = loaded(user, id).await?;
assert_eq!(after.text("name"), Some("new"));
assert_eq!(after.text("source"), Some("(+ 2 3)"));
assert_eq!(after.text("enabled"), Some("false"));
assert_ne!(after.text("revision").map(str::to_string), before);
assert_eq!(stored.bytecode, compile_script("(+ 2 3)")?);
async fn enabling_and_disabling_round_trips(pool: PgPool) -> anyhow::Result<()> {
for state in ["false", "true"] {
values.set("enabled", state);
submit(user, &SCRIPT_EDIT, &values).await?;
assert_eq!(loaded(user, id).await?.text("enabled"), Some(state));
async fn a_stale_revision_is_refused_at_revision(pool: PgPool) -> anyhow::Result<()> {
let stale = loaded(user, id).await?;
User { id: user }.set_script_enabled(id, false).await?;
let errors = invalid(user, &SCRIPT_EDIT, &stale).await?;
assert_eq!(errors, [("revision".to_string(), STALE_SCRIPT.to_string())]);
async fn a_bytecode_only_script_loads_with_no_source_and_a_notice(
let id = User { id: user }
.create_script(compile_script(SOURCE)?, Some("old".to_string()))
.await?;
let sourced = create(user, None, None).await?;
let (values, notice) = load_noticed(user, &SCRIPT_EDIT, &id.to_string()).await?;
let (_, sourced_notice) = load_noticed(user, &SCRIPT_EDIT, &sourced.to_string()).await?;
assert_eq!(values.text("source"), Some(""));
assert!(notice.is_some_and(|n| n.contains("no source")));
assert_eq!(sourced_notice, None);
async fn saving_a_bytecode_only_script_with_source_stores_both(pool: PgPool) -> anyhow::Result<()> {
.create_script(compile_script("(+ 9 9)")?, None)
let empty = invalid(user, &SCRIPT_EDIT, &values).await?;
values.set("source", SOURCE);
assert_eq!(empty.first().map(|e| e.0.as_str()), Some("source"));
assert_eq!(stored.source.as_deref(), Some(SOURCE));
async fn a_deleted_script_or_a_template_is_unknown(pool: PgPool) -> anyhow::Result<()> {
let id = create(user, None, None).await?;
let template = User { id: user }
.create_template("(set-draft-note \"x\")".to_string(), None)
User { id: user }.delete_script(id).await?;
let mut of_template = values.clone();
of_template.set("id", &template.to_string());
let gone = invalid(user, &SCRIPT_EDIT, &values).await?;
let on_template = invalid(user, &SCRIPT_EDIT, &of_template).await?;
let malformed = load(user, &SCRIPT_EDIT, "nope").await;
let missing = load(user, &SCRIPT_EDIT, &id.to_string()).await;
let template_load = load(user, &SCRIPT_EDIT, &template.to_string()).await;
let unknown = [("id".to_string(), "unknown script".to_string())];
assert_eq!(gone, unknown);
assert_eq!(on_template, unknown);
assert!(matches!(
malformed,
Err(Failure::MalformedId {
entity: Entity::Script,
..
})
));
assert!(matches!(missing, Err(Failure::Missing(Entity::Script))));
template_load,
Err(Failure::Missing(Entity::Script))
async fn an_empty_source_names_why_a_bytecode_only_script_cannot_be_saved(
let old = User { id: user }
let sourced = create(user, Some("sync"), None).await?;
let mut bytecode = loaded(user, old).await?;
bytecode.set("source", "");
let mut plain = loaded(user, sourced).await?;
plain.set("source", "");
let on_bytecode = invalid(user, &SCRIPT_EDIT, &bytecode).await?;
let on_sourced = invalid(user, &SCRIPT_EDIT, &plain).await?;
assert_eq!(
on_bytecode,
[(
"source".to_string(),
"this script was uploaded as bytecode and has no source; supply its source to save it"
.to_string()
)]
);
assert_eq!(on_sourced.len(), 1, "{on_sourced:?}");
assert_eq!(on_sourced[0].0, "source");
assert!(on_sourced[0].1.contains("required"), "{on_sourced:?}");