1
use super::*;
2
use crate::{AccountCmd, Cli, Command as Top};
3
use clap::Parser;
4
use rpc::ScriptCtx;
5
use server::command::account::CreateAccount;
6
use server::command::{CmdResult, FinanceEntity};
7
use sqlx::PgPool;
8
use uuid::Uuid;
9

            
10
4
fn parse(args: &[&str]) -> anyhow::Result<MergeArgs> {
11
4
    let user = Uuid::nil().to_string();
12
4
    let argv = ["nomisync", "--userid", user.as_str(), "account", "merge"]
13
4
        .into_iter()
14
4
        .chain(args.iter().copied());
15
4
    match Cli::try_parse_from(argv)?.cmd {
16
4
        Top::Account(AccountCmd::Merge(args)) => Ok(args),
17
        other => anyhow::bail!("expected account merge, got {other:?}"),
18
    }
19
4
}
20

            
21
#[test]
22
1
fn flags_come_from_the_descriptor() -> anyhow::Result<()> {
23
1
    let args = parse(&["--into", "Cafe", "--from", "Coffee", "--yes"])?;
24
1
    assert_eq!(args.values.text("into"), Some("Cafe"));
25
1
    assert_eq!(args.values.text("from"), Some("Coffee"));
26
1
    assert!(args.yes);
27
1
    assert!(!parse(&["--into", "Cafe"])?.yes);
28
1
    Ok(())
29
1
}
30

            
31
#[test]
32
1
fn the_question_names_both_accounts() {
33
1
    let mut values = FormValues::default();
34
1
    values.set("into", "Cafe");
35
1
    values.set("from", "Coffee");
36
1
    assert!(question(&values).starts_with("Merge account Coffee into Cafe?"));
37
1
}
38

            
39
#[tokio::test]
40
1
async fn a_declined_merge_sends_nothing() -> anyhow::Result<()> {
41
1
    let mut session = Session::new(ScriptCtx::new(Uuid::nil()))?;
42
1
    let args = parse(&["--into", "Cafe", "--from", "Coffee"])?;
43
1
    let merged = merge_confirmed(&mut session, &args, true, "n\n".as_bytes()).await?;
44
1
    assert_eq!(merged, None);
45
2
    Ok(())
46
1
}
47

            
48
1
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
49
1
    let user_id = Uuid::new_v4();
50
1
    sqlx::query(
51
1
        "INSERT INTO users \
52
1
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
53
1
         VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
54
1
    )
55
1
    .bind(user_id)
56
1
    .bind(format!("u{user_id}"))
57
1
    .bind(format!("{user_id}@test.com"))
58
1
    .execute(pool)
59
1
    .await?;
60
1
    Ok(user_id)
61
1
}
62

            
63
2
async fn account(user_id: Uuid, name: &str) -> anyhow::Result<Uuid> {
64
2
    match CreateAccount::new()
65
2
        .name(name.to_string())
66
2
        .user_id(user_id)
67
2
        .run()
68
2
        .await?
69
    {
70
2
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
71
        other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
72
    }
73
2
}
74

            
75
#[sqlx::test(migrator = "server::db::MIGRATOR")]
76
async fn a_confirmed_merge_keeps_into(pool: PgPool) -> anyhow::Result<()> {
77
    let _guard = server::db::install_test_pool(pool.clone());
78
    let user = seed_user(&pool).await?;
79
    let into = account(user, "Cafe").await?.to_string();
80
    let from = account(user, "Cafe").await?.to_string();
81
    let mut session = Session::new(ScriptCtx::new(user))?;
82
    let args = parse(&["--into", &into, "--from", &from])?;
83

            
84
    let merged = merge_confirmed(&mut session, &args, true, "y\n".as_bytes()).await?;
85

            
86
    assert_eq!(merged, Some(into));
87
    Ok(())
88
}