1
//! A split carrying two values of one tag name reaches a script through
2
//! `get-split-tags`, while `get-split-tag` stays the lowest value.
3

            
4
#![cfg(feature = "db")]
5

            
6
use rpc::{ScriptCtx, Session};
7
use server::db::install_test_pool;
8
use server::user::User;
9
use sqlx::PgPool;
10
use supp_macro::local_db_sqlx_test;
11
use uuid::Uuid;
12

            
13
2
async fn setup() {}
14

            
15
1
async fn insert_test_user(pool: &PgPool, id: Uuid) -> anyhow::Result<()> {
16
1
    sqlx::query(
17
1
        "INSERT INTO users (
18
1
            id, user_name, email, photo, verified, user_password,
19
1
            user_role, database_name, created_at
20
1
        ) VALUES (
21
1
            $1, 'split-values-user', 'split-values@example.com', 'default.png',
22
1
            FALSE, 'irrelevant', 'user', 'split-values', NOW()
23
1
        )",
24
1
    )
25
1
    .bind(id)
26
1
    .execute(pool)
27
1
    .await?;
28
1
    Ok(())
29
1
}
30

            
31
4
fn extract_value_uuid(response: &str) -> Option<String> {
32
4
    let needle = ":value \"";
33
4
    let start = response.find(needle)? + needle.len();
34
4
    let end = start + response[start..].find('"')?;
35
4
    Some(response[start..end].to_string())
36
4
}
37

            
38
#[local_db_sqlx_test]
39
async fn two_values_of_one_name_reach_a_script(pool: PgPool) -> anyhow::Result<()> {
40
    setup().await;
41
    let user_id = Uuid::new_v4();
42
    insert_test_user(&pool, user_id).await?;
43
    let mut session = Session::new(ScriptCtx::new(user_id)).expect("Session::new");
44

            
45
    let usd = extract_value_uuid(
46
        &session
47
            .handle_form("(:id 1 :form (create-commodity \"USD\" \"Dollar\"))")
48
            .await,
49
    )
50
    .expect("commodity id");
51
    let cash = extract_value_uuid(
52
        &session
53
            .handle_form("(:id 2 :form (create-account \"Cash\" \"\"))")
54
            .await,
55
    )
56
    .expect("cash id");
57
    let food = extract_value_uuid(
58
        &session
59
            .handle_form("(:id 3 :form (create-account \"Food\" \"\"))")
60
            .await,
61
    )
62
    .expect("food id");
63
    let tx = extract_value_uuid(
64
        &session
65
            .handle_form(&format!(
66
                "(:id 4 :form (create-transaction \"(:post-date \\\"2026-02-01T00:00:00Z\\\" \
67
                 :note \\\"n\\\" :splits ((:account-id \\\"{cash}\\\" :commodity-id \\\"{usd}\\\" :value -5) \
68
                 (:account-id \\\"{food}\\\" :commodity-id \\\"{usd}\\\" :value 5)))\"))"
69
            ))
70
            .await,
71
    )
72
    .expect("tx id");
73
    let split =
74
        sqlx::query_scalar::<_, Uuid>("SELECT id FROM splits WHERE tx_id = $1 AND account_id = $2")
75
            .bind(Uuid::parse_str(&tx)?)
76
            .bind(Uuid::parse_str(&food)?)
77
            .fetch_one(&pool)
78
            .await?;
79

            
80
    let user = User { id: user_id };
81
    for value in ["b", "a"] {
82
        user.create_split_tag(split, "dup".to_string(), value.to_string(), None)
83
            .await?;
84
    }
85

            
86
    let all = session
87
        .handle_form(&format!(
88
            "(:id 5 :form (get-split-tags \"{split}\" \"dup\"))"
89
        ))
90
        .await;
91
    assert!(!all.contains(":error"), "{all}");
92
    assert!(all.contains(r#":value ("a" "b")"#), "{all}");
93

            
94
    let first = session
95
        .handle_form(&format!(
96
            "(:id 6 :form (get-split-tag \"{split}\" \"dup\"))"
97
        ))
98
        .await;
99
    assert!(first.contains(":value \"a\""), "{first}");
100

            
101
    let none = session
102
        .handle_form(&format!(
103
            "(:id 7 :form (length (get-split-tags \"{split}\" \"missing\")))"
104
        ))
105
        .await;
106
    assert!(none.contains(":value 0"), "{none}");
107
}