Lines
96.15 %
Functions
87.5 %
Branches
100 %
//! Error codes and messages of `submit-form` that the typed `submit` must keep.
use finance::split::Split;
use server::command::transaction::UpdateTransaction;
use server::command::{CmdError, FinanceEntity};
use server::logical::STALE_REVISION;
use server::user::User;
use sqlx::PgPool;
use uuid::Uuid;
use super::load_form;
use super::submit_form;
use super::test_edit::{Fx, condition, create, detail_text, fields, fx};
use super::tests_edit_load::{leg, raw_transaction};
const POSTED: &str = "2026-10-27T12:00:00Z";
async fn rejected(fx: &Fx, name: &str, payload: &str) -> anyhow::Result<(String, String, String)> {
let err = submit_form(fx.user, name, payload)
.await
.err()
.ok_or_else(|| anyhow::anyhow!("submit was accepted"))?;
let condition = condition(&err);
let detail = detail_text(&condition);
Ok((condition.code, condition.message, detail))
}
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn unknown_form_is_an_args_error(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = fx(&pool).await?;
let (code, message, _) = rejected(&fx, "nope", "()").await?;
assert_eq!(code, "args");
assert_eq!(message, "unknown form 'nope'");
Ok(())
async fn malformed_payload_is_a_whole_form_error(pool: PgPool) -> anyhow::Result<()> {
for name in ["transaction-create", "transaction-edit"] {
let (code, message, detail) = rejected(&fx, name, "(:date").await?;
assert_eq!(code, "form-invalid");
assert_eq!(message, "1 field invalid");
assert_eq!(fields(&detail), [""]);
async fn invalid_values_list_every_field(pool: PgPool) -> anyhow::Result<()> {
let (code, message, detail) = rejected(&fx, "transaction-create", "(:date \"x\")").await?;
assert_eq!(message, "2 fields invalid");
assert_eq!(fields(&detail), ["date", "splits"]);
async fn unknown_edit_id_is_invalid_at_id(pool: PgPool) -> anyhow::Result<()> {
let payload = format!(
"(:id \"{}\" :date \"2026-10-27\" :revision \"r\" :tags () :splits ({}) :split-tags ())",
Uuid::new_v4(),
fx.row("1", "")
);
let (code, _, detail) = rejected(&fx, "transaction-edit", &payload).await?;
assert_eq!(fields(&detail), ["id"]);
assert!(detail.contains("unknown transaction"), "{detail}");
async fn edit_of_a_refusable_transaction_is_checked_for_staleness_first(
pool: PgPool,
) -> anyhow::Result<()> {
let tx = raw_transaction(&fx, |tx| {
vec![
leg(&fx, tx, fx.cash, -100),
leg(&fx, tx, fx.food, 60),
leg(&fx, tx, fx.food, 40),
]
})
.await?;
"(:id \"{tx}\" :date \"2026-10-27\" :revision \"r\" :tags () :splits ({}) :split-tags ())",
assert_eq!(fields(&detail), ["revision"]);
async fn stale_revision_is_invalid_at_revision(pool: PgPool) -> anyhow::Result<()> {
let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
let stale = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
submit_form(
fx.user,
"transaction-edit",
&stale.replace(":note \"n\"", ":note \"m\""),
)
let (code, message, detail) = rejected(&fx, "transaction-edit", &stale).await?;
assert!(detail.contains("changed since it was loaded"), "{detail}");
/// The `:revision` text of a loaded payload.
fn revision_of(payload: &str) -> String {
payload
.split_once(":revision \"")
.and_then(|(_, tail)| tail.split_once('"'))
.map(|(revision, _)| revision.to_string())
.unwrap_or_default()
async fn reconciling_a_kept_split_makes_the_load_stale(pool: PgPool) -> anyhow::Result<()> {
let loaded = revision_of(&load_form(fx.user, "transaction-edit", &tx.to_string()).await?);
let reconciled: Vec<FinanceEntity> = User { id: fx.user }
.get_transaction_splits(tx)
.await?
.into_iter()
.map(|split| {
FinanceEntity::Split(Split {
reconcile_state: Some(true),
..split
.collect();
UpdateTransaction::new()
.user_id(fx.user)
.transaction_id(tx)
.splits(reconciled)
.run()
let stale = UpdateTransaction::new()
.note("m".to_string())
.expected_revision(loaded)
.await;
assert!(
matches!(&stale, Err(CmdError::Args(message)) if message == STALE_REVISION),
"{stale:?}"
let states: Vec<Option<bool>> =
sqlx::query_scalar("SELECT reconcile_state FROM splits WHERE tx_id = $1")
.bind(tx)
.fetch_all(&pool)
assert_eq!(states, [Some(true), Some(true)]);