1
use forms::FormValues;
2
use rpc::{ScriptCtx, Session};
3
use server::command::account::CreateAccount;
4
use server::command::{CmdResult, FinanceEntity};
5
use sqlx::PgPool;
6
use uuid::Uuid;
7

            
8
use super::merge;
9
use crate::CommandError;
10
use crate::transaction::eval_text;
11

            
12
3
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
13
3
    let user_id = Uuid::new_v4();
14
3
    sqlx::query(
15
3
        "INSERT INTO users \
16
3
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
17
3
         VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
18
3
    )
19
3
    .bind(user_id)
20
3
    .bind(format!("u{user_id}"))
21
3
    .bind(format!("{user_id}@test.com"))
22
3
    .execute(pool)
23
3
    .await?;
24
3
    Ok(user_id)
25
3
}
26

            
27
7
async fn account(user_id: Uuid, name: &str) -> anyhow::Result<Uuid> {
28
7
    match CreateAccount::new()
29
7
        .name(name.to_string())
30
7
        .user_id(user_id)
31
7
        .run()
32
7
        .await?
33
    {
34
7
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
35
        other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
36
    }
37
7
}
38

            
39
6
fn picks(into: &str, from: &str) -> FormValues {
40
6
    let mut values = FormValues::default();
41
6
    values.set("into", into);
42
6
    values.set("from", from);
43
6
    values
44
6
}
45

            
46
#[sqlx::test(migrator = "server::db::MIGRATOR")]
47
async fn merge_picks_two_and_keeps_into(pool: PgPool) -> anyhow::Result<()> {
48
    let _guard = server::db::install_test_pool(pool.clone());
49
    let user = seed_user(&pool).await?;
50
    let into = account(user, "Cafe").await?;
51
    let from = account(user, "Cafe").await?;
52
    let mut session = Session::new(ScriptCtx::new(user))?;
53

            
54
    let kept = merge(&mut session, &picks(&into.to_string(), &from.to_string())).await?;
55

            
56
    assert_eq!(kept, into.to_string());
57
    let again = merge(&mut session, &picks(&into.to_string(), &from.to_string())).await;
58
    assert!(
59
        matches!(&again, Err(CommandError::Argument(m)) if m.contains("--from")),
60
        "the merged account is gone: {again:?}"
61
    );
62
    Ok(())
63
}
64

            
65
3
async fn listed(session: &mut Session) -> anyhow::Result<String> {
66
3
    Ok(eval_text(session, "(form-options \"accounts\")").await?)
67
3
}
68

            
69
#[sqlx::test(migrator = "server::db::MIGRATOR")]
70
async fn merge_maps_an_alias_and_passes_a_uuid(pool: PgPool) -> anyhow::Result<()> {
71
    let _guard = server::db::install_test_pool(pool.clone());
72
    let user = seed_user(&pool).await?;
73
    let into = account(user, "Cafe").await?;
74
    account(user, "Coffee").await?;
75
    let mut session = Session::new(ScriptCtx::new(user))?;
76
    let kept = merge(&mut session, &picks(&into.to_string(), "Coffee")).await?;
77
    assert_eq!(kept, into.to_string());
78
    assert!(!listed(&mut session).await?.contains("Coffee"));
79
    Ok(())
80
}
81

            
82
#[sqlx::test(migrator = "server::db::MIGRATOR")]
83
async fn ambiguous_or_unknown_merge_pick_fails_before_merging(pool: PgPool) -> anyhow::Result<()> {
84
    let _guard = server::db::install_test_pool(pool.clone());
85
    let user = seed_user(&pool).await?;
86
    account(user, "Twin").await?;
87
    account(user, "Twin").await?;
88
    account(user, "Solo").await?;
89
    let mut session = Session::new(ScriptCtx::new(user))?;
90
    let before = listed(&mut session).await?;
91
    for (values, flag) in [
92
        (picks("Solo", "Twin"), "--from"),
93
        (picks("Twin", "Solo"), "--into"),
94
        (picks("Solo", "Nowhere"), "--from"),
95
    ] {
96
        let failed = merge(&mut session, &values).await;
97
        assert!(
98
            matches!(&failed, Err(CommandError::Argument(m)) if m.contains(flag)),
99
            "{flag}: {failed:?}"
100
        );
101
    }
102
    assert_eq!(listed(&mut session).await?, before);
103
    Ok(())
104
}
105

            
106
#[test]
107
1
fn the_preview_call_carries_the_merge_payload() {
108
1
    let mut values = FormValues::default();
109
1
    values.set("into", "a");
110
1
    values.set("from", "b");
111
1
    assert_eq!(
112
1
        super::merge_preview_call(&values),
113
1
        r#"(merge-preview "(:into \"a\" :from \"b\")")"#
114
    );
115
1
}
116

            
117
#[test]
118
1
fn the_preview_splits_are_read_from_the_reply_plist() -> anyhow::Result<()> {
119
    use crate::render::{WireValue, parse_wire};
120
1
    let WireValue::Value(plist) = parse_wire(r#"(:id 1 :value (:into "x" :from "y" :splits 4))"#)?;
121
1
    assert_eq!(super::merge_preview_splits(&plist), Some(4));
122
1
    assert_eq!(super::merge_preview_splits(&nomiscript::Value::Nil), None);
123
1
    Ok(())
124
1
}