Lines
100 %
Functions
Branches
use scripting::runtime::EvalValue;
use server::user::User;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_edit::{Fx, fx};
use super::tests_transaction_tags::{TRIP, tagged};
use crate::compile_script;
use crate::ctx::ScriptCtx;
use crate::natives::script_store::run_run_script;
use crate::session::Session;
const TAG_SYNC: &str = include_str!("../../../../doc/scripts/tag-sync.nms");
const BOOM_ON_TRIP: &str = r#"
(do ((i 0 (+ i 1)))
((>= i (entity-count)))
(when (and (= (entity-type i) +entity-tag+)
(string= (tag-name i) "trip"))
(error 'boom "no")))
"#;
async fn stored(fx: &Fx, source: &str) -> anyhow::Result<Uuid> {
let user = User { id: fx.user };
Ok(user
.create_script(compile_script(source)?, Some("run".to_string()))
.await?)
}
fn plist(items: Vec<EvalValue>) -> EvalValue {
EvalValue::List(items)
fn symbol(name: &str) -> EvalValue {
EvalValue::Symbol(format!(":{name}"))
async fn run(fx: &Fx, script: Uuid) -> wasmtime::Result<EvalValue> {
run_run_script(fx.user, Some(script.to_string()), Some(fx.cash.to_string())).await
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn tag_sync_reports_what_ran_and_what_changed(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = fx(&pool).await?;
tagged(&fx, &[TRIP]).await?;
tagged(&fx, &[]).await?;
let script = stored(&fx, TAG_SYNC).await?;
let report = run(&fx, script).await?;
assert_eq!(
report,
plist(vec![
symbol("RAN"),
EvalValue::I32(2),
symbol("COMMITTED"),
EvalValue::I32(1),
symbol("FAILURES"),
plist(Vec::new()),
])
);
Ok(())
async fn a_script_failure_is_reported_and_nothing_is_committed(pool: PgPool) -> anyhow::Result<()> {
let boom = tagged(&fx, &[TRIP]).await?;
let script = stored(&fx, BOOM_ON_TRIP).await?;
let EvalValue::List(report) = run(&fx, script).await? else {
anyhow::bail!("expected a plist");
};
assert_eq!(report[3], EvalValue::I32(0));
let EvalValue::List(failures) = &report[5] else {
anyhow::bail!("expected failures, got {:?}", report[5]);
assert_eq!(failures.len(), 1);
let EvalValue::List(failure) = &failures[0] else {
anyhow::bail!("expected a failure plist");
assert_eq!(failure[1], EvalValue::String(boom.to_string()));
assert_eq!(failure[3], EvalValue::String(script.to_string()));
async fn a_missing_script_is_not_found_and_a_bad_id_is_args(pool: PgPool) -> anyhow::Result<()> {
let mut session = Session::new(ScriptCtx::new(fx.user))?;
let missing = format!(
"(:id 1 :form (run-script \"{}\" \"{}\"))",
Uuid::new_v4(),
fx.cash
let not_found = session.handle_form(&missing).await;
let malformed = session
.handle_form(&format!(
"(:id 2 :form (run-script \"nope\" \"{}\"))",
))
.await;
assert!(not_found.contains(":code not-found"), "got: {not_found}");
assert!(malformed.contains(":code args"), "got: {malformed}");
async fn the_session_answers_the_plist_on_the_wire(pool: PgPool) -> anyhow::Result<()> {
let response = session
"(:id 1 :form (run-script \"{script}\" \"{}\"))",
assert!(
response.contains("(:RAN 1 :COMMITTED 1 :FAILURES nil)"),
"got: {response}"