1
//! A transaction the form cannot keep is refused on submit even when the
2
//! submitted revision is the current one.
3

            
4
use server::logical::stored_revision;
5
use server::user::User;
6
use sqlx::PgPool;
7
use uuid::Uuid;
8

            
9
use super::test_edit::{Fx, condition, create, fx};
10
use super::test_seed::submit_edit;
11
use super::tests_edit_load::{leg, raw_transaction};
12

            
13
const POSTED: &str = "2026-10-27T12:00:00Z";
14

            
15
3
async fn current_revision(user: Uuid, tx: Uuid) -> anyhow::Result<String> {
16
3
    let snapshot = User { id: user }
17
3
        .transaction_snapshot(tx)
18
3
        .await?
19
        .ok_or_else(|| anyhow::anyhow!("no transaction"))?;
20
3
    Ok(stored_revision(
21
3
        snapshot.transaction.post_date,
22
3
        snapshot.tags,
23
3
        &snapshot.splits,
24
3
        &snapshot.split_tags,
25
3
        &snapshot.prices,
26
3
    ))
27
3
}
28

            
29
3
async fn submit_current(fx: &Fx, tx: Uuid) -> wasmtime::Result<String> {
30
3
    let revision = current_revision(fx.user, tx)
31
3
        .await
32
        .map_err(|err| wasmtime::Error::msg(err.to_string()))?;
33
3
    let payload = format!(
34
3
        "(:id \"{tx}\" :date \"2026-10-27\" :revision \"{revision}\" :tags () :splits ({}) \
35
         :split-tags ())",
36
3
        fx.row("1", "")
37
    );
38
3
    submit_edit(fx.user, &payload).await
39
3
}
40

            
41
2
async fn expect_args_refusal(fx: &Fx, tx: Uuid, needle: &str) -> anyhow::Result<()> {
42
2
    let err = submit_current(fx, tx).await.expect_err("must be refused");
43
2
    let condition = condition(&err);
44
2
    assert_eq!(condition.code, "args");
45
2
    assert!(condition.message.contains(needle), "{}", condition.message);
46
2
    Ok(())
47
2
}
48

            
49
#[sqlx::test(migrator = "server::db::MIGRATOR")]
50
async fn reconciled_split_is_refused_at_the_current_revision(pool: PgPool) -> anyhow::Result<()> {
51
    let _guard = server::db::install_test_pool(pool.clone());
52
    let fx = fx(&pool).await?;
53
    let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
54
    sqlx::query("UPDATE splits SET reconcile_state = TRUE")
55
        .execute(&pool)
56
        .await?;
57

            
58
    expect_args_refusal(&fx, tx, "cannot keep").await?;
59
    let states: Vec<Option<bool>> =
60
        sqlx::query_scalar("SELECT reconcile_state FROM splits WHERE tx_id = $1")
61
            .bind(tx)
62
            .fetch_all(&pool)
63
            .await?;
64
    assert_eq!(states, [Some(true), Some(true)]);
65
    Ok(())
66
}
67

            
68
#[sqlx::test(migrator = "server::db::MIGRATOR")]
69
async fn ambiguous_pairing_is_refused_at_the_current_revision(pool: PgPool) -> anyhow::Result<()> {
70
    let _guard = server::db::install_test_pool(pool.clone());
71
    let fx = fx(&pool).await?;
72
1
    let tx = raw_transaction(&fx, |tx| {
73
1
        vec![
74
1
            leg(&fx, tx, fx.cash, -100),
75
1
            leg(&fx, tx, fx.food, 60),
76
1
            leg(&fx, tx, fx.food, 40),
77
        ]
78
1
    })
79
    .await?;
80

            
81
    expect_args_refusal(&fx, tx, "paired").await?;
82
    let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM splits WHERE tx_id = $1")
83
        .bind(tx)
84
        .fetch_one(&pool)
85
        .await?;
86
    assert_eq!(count, 3);
87
    Ok(())
88
}
89

            
90
#[sqlx::test(migrator = "server::db::MIGRATOR")]
91
async fn keepable_transaction_submits_at_the_current_revision(pool: PgPool) -> anyhow::Result<()> {
92
    let _guard = server::db::install_test_pool(pool.clone());
93
    let fx = fx(&pool).await?;
94
    let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
95
    submit_current(&fx, tx).await?;
96
    Ok(())
97
}