Lines
100 %
Functions
Branches
use forms::FormValues;
use sqlx::PgPool;
use uuid::Uuid;
use super::load_form;
use super::test_seed::{seed_account, seed_commodity, seed_user, submit_create};
use crate::ctx::ScriptCtx;
use crate::envelope::ResponsePayload;
use crate::forms::{Failure, merge_submit};
use crate::response::parse_response;
use crate::session::Session;
async fn call(user_id: Uuid, form: &str) -> anyhow::Result<ResponsePayload> {
let mut session = Session::new(ScriptCtx::new(user_id))?;
let wire = session.handle_form(&format!("(:id 1 :form {form})")).await;
Ok(parse_response(&wire)?.payload)
}
fn merge_call(into: &str, from: &str) -> String {
format!("(merge-accounts \"{into}\" \"{from}\")")
const POSTED: &str = "2026-10-27T12:00:00Z";
async fn transfer(user: Uuid, usd: Uuid, from: Uuid, to: Uuid) -> anyhow::Result<String> {
Ok(submit_create(
user,
&format!(
"(:date \"{POSTED}\" :splits ((:from \"{from}\" :to \"{to}\" :from-commodity \"{usd}\" :value \"3\")))"
),
)
.await?)
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn the_native_merges_and_the_transaction_still_loads(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let usd = seed_commodity(user, "USD").await?;
let cash = seed_account(user, "Cash").await?;
let into = seed_account(user, "Cafe").await?;
let from = seed_account(user, "Cafe").await?;
let outside = transfer(user, usd, cash, from).await?;
let inside = transfer(user, usd, into, from).await?;
let reply = call(user, &merge_call(&into.to_string(), &from.to_string())).await?;
assert_eq!(
reply,
ResponsePayload::Value(nomiscript::Value::String(into.to_string()))
);
for id in [outside, inside] {
let loaded = load_form(user, "transaction-edit", &id).await?;
assert!(loaded.contains(&format!("\\\"{into}\\\"")), "{loaded}");
assert!(!loaded.contains(&from.to_string()), "{loaded}");
Ok(())
async fn the_native_takes_uuids_only(pool: PgPool) -> anyhow::Result<()> {
seed_account(user, "Bar").await?;
let reply = call(user, &merge_call(&into.to_string(), "Bar")).await?;
assert!(
matches!(&reply, ResponsePayload::Error { code, message, .. }
if code.as_symbol() == "args" && message.contains("from must be an account UUID")),
"{reply:?}"
async fn a_refusal_is_an_args_error_with_its_reason(pool: PgPool) -> anyhow::Result<()> {
let cafe = seed_account(user, "Cafe").await?.to_string();
let reply = call(user, &merge_call(&cafe, &cafe)).await?;
if code.as_symbol() == "args" && message.contains("into itself")),
async fn the_form_refuses_names_and_unknown_ids_at_their_fields(
pool: PgPool,
) -> anyhow::Result<()> {
let unknown = Uuid::new_v4().to_string();
for (into, from, expected) in [
("Cafe", unknown.as_str(), vec!["into", "from"]),
(cafe.as_str(), "Bar", vec!["from"]),
(cafe.as_str(), "not-a-uuid", vec!["from"]),
] {
let mut values = FormValues::default();
values.set("into", into);
values.set("from", from);
let failure = merge_submit(user, &values).await.expect_err("refused");
let Failure::Invalid(errors) = failure else {
anyhow::bail!("expected field errors, got {failure:?}");
};
let fields: Vec<&str> = errors.iter().map(|e| e.field.as_str()).collect();
assert_eq!(fields, expected);
async fn merging_a_deleted_account_is_not_found(pool: PgPool) -> anyhow::Result<()> {
let into = seed_account(user, "Cafe").await?.to_string();
let gone = Uuid::new_v4().to_string();
let reply = call(user, &merge_call(&into, &gone)).await?;
matches!(&reply, ResponsePayload::Error { code, .. } if code.as_symbol() == "not-found"),
async fn handler_case_not_found_catches_a_deleted_account_merge(
let form = format!(
"(handler-case {} (not-found (e) \"caught\"))",
merge_call(&into, &gone)
let reply = call(user, &form).await?;
ResponsePayload::Value(nomiscript::Value::String("caught".to_string()))
async fn the_shared_merge_call_reports_a_deleted_account_as_missing(
let failure = crate::forms::merge_accounts(user, into, Uuid::new_v4())
.await
.expect_err("the account is gone");
let Failure::Internal(err) = failure else {
anyhow::bail!("expected a not-found condition, got {failure:?}");
crate::host_failure::condition_code(&err),
Some(crate::envelope::ErrorCode::NOT_FOUND)