1
//! The success results of a mutation, `transaction-query`, `config-set` and
2
//! `convert`, pinned on the wire (plan 0016 M3r0, VF2).
3

            
4
use forms::escape_str;
5
use sqlx::PgPool;
6
use uuid::Uuid;
7

            
8
use super::test_seed::{seed_account, seed_commodity, seed_price, seed_user};
9
use crate::ctx::ScriptCtx;
10
use crate::session::Session;
11

            
12
const MUTATION: &str = include_str!("../../../../forms/tests/fixtures/result-mutation.sexp");
13
const QUERY: &str = include_str!("../../../../forms/tests/fixtures/result-query.sexp");
14
const CONFIG: &str = include_str!("../../../../forms/tests/fixtures/result-config.sexp");
15
const CONVERT: &str = include_str!("../../../../forms/tests/fixtures/result-convert.sexp");
16

            
17
/// Each UUID becomes `<uuid-N>`, numbered by first appearance across `wires`.
18
1
fn normalized(wires: &[String]) -> Vec<String> {
19
1
    let mut seen: Vec<String> = Vec::new();
20
1
    wires
21
1
        .iter()
22
6
        .map(|wire| {
23
6
            let mut out = String::new();
24
6
            let mut rest = wire.as_str();
25
538
            while !rest.is_empty() {
26
532
                match rest.get(..36).filter(|head| Uuid::parse_str(head).is_ok()) {
27
                    Some(id) => {
28
5
                        let n = seen.iter().position(|s| s == id).unwrap_or_else(|| {
29
2
                            seen.push(id.to_string());
30
2
                            seen.len() - 1
31
2
                        });
32
5
                        out.push_str(&format!("<uuid-{}>", n + 1));
33
5
                        rest = &rest[36..];
34
                    }
35
                    None => {
36
527
                        let next = rest.chars().next().map_or(1, char::len_utf8);
37
527
                        out.push_str(&rest[..next]);
38
527
                        rest = &rest[next..];
39
                    }
40
                }
41
            }
42
6
            out
43
6
        })
44
        .collect()
45
1
}
46

            
47
6
async fn wire(session: &mut Session, form: &str) -> String {
48
6
    session.handle_form(&format!("(:id 1 :form {form})")).await
49
6
}
50

            
51
6
fn submit(name: &str, payload: &str) -> String {
52
6
    format!("(submit-form {} {})", escape_str(name), escape_str(payload))
53
6
}
54

            
55
#[sqlx::test(migrator = "server::db::MIGRATOR")]
56
async fn success_results_match_the_pinned_wire(pool: PgPool) -> anyhow::Result<()> {
57
    let _guard = server::db::install_test_pool(pool.clone());
58
    let user = seed_user(&pool).await?;
59
    let usd = seed_commodity(user, "USD").await?;
60
    let eur = seed_commodity(user, "EUR").await?;
61
    let cash = seed_account(user, "Cash").await?;
62
    let food = seed_account(user, "Food").await?;
63
    seed_price(&pool, usd, eur, (9, 10)).await?;
64
    let mut session = Session::new(ScriptCtx::new(user))?;
65
    let payload = format!(
66
        "(:date \"2026-10-27\" :note \"lunch \\\"out\\\"\nsecond line\" :splits ((:from \"{cash}\" :to \"{food}\" :from-commodity \"{usd}\" :value \"1/3\")))"
67
    );
68
    let second = payload.replace("2026-10-27", "2026-10-26");
69
2
    let page = |limit: &str| format!("(:account \"{cash}\" :limit \"{limit}\")");
70

            
71
    let wires = [
72
        wire(&mut session, &submit("transaction-create", &payload)).await,
73
        wire(&mut session, &submit("transaction-create", &second)).await,
74
        wire(&mut session, &submit("transaction-query", &page("1"))).await,
75
        wire(&mut session, &submit("transaction-query", &page("10"))).await,
76
        wire(
77
            &mut session,
78
            &submit("config-set", "(:key \"report-currency\" :value \"USD\")"),
79
        )
80
        .await,
81
        wire(
82
            &mut session,
83
            &submit(
84
                "convert",
85
                &format!("(:amount \"1\" :from \"{usd}\" :to \"{eur}\")"),
86
            ),
87
        )
88
        .await,
89
    ];
90

            
91
    let pinned: Vec<&str> = [MUTATION, QUERY, CONFIG, CONVERT]
92
        .iter()
93
4
        .flat_map(|fixture| fixture.lines())
94
        .collect();
95
    assert_eq!(normalized(&wires), pinned, "{wires:#?}");
96
    Ok(())
97
}