1
//! A create or rename that gives a notice shows it in the status line.
2

            
3
use cli_core::transaction::submit_call;
4
use forms::{ACCOUNT_CREATE, ACCOUNT_EDIT, FormValues};
5
use rpc::{ScriptCtx, Session};
6
use server::command::account_name::duplicate_name_notice;
7
use sqlx::PgPool;
8
use sqlx::types::Uuid;
9

            
10
use crate::app::App;
11
use crate::modal::Modal;
12
use crate::route::{Route, RouteCtx};
13
use crate::view::ViewId;
14

            
15
5
async fn reply(session: &mut Session, form: &str) -> String {
16
5
    session.handle_form(&format!("(:id 1 :form {form})")).await
17
5
}
18

            
19
3
fn create_account(name: &str) -> String {
20
3
    let mut values = FormValues::default();
21
3
    values.set("name", name);
22
3
    submit_call(&ACCOUNT_CREATE, &values)
23
3
}
24

            
25
3
fn mutation(app: &App) -> Route {
26
3
    Route {
27
3
        target: Some(app.window_of(ViewId::Accounts)),
28
3
        ctx: RouteCtx::Mutation {
29
3
            refresh: ViewId::Accounts,
30
3
        },
31
3
    }
32
3
}
33

            
34
1
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
35
1
    let user_id = Uuid::new_v4();
36
1
    sqlx::query(
37
1
        "INSERT INTO users \
38
1
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
39
1
         VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
40
1
    )
41
1
    .bind(user_id)
42
1
    .bind(format!("u{user_id}"))
43
1
    .bind(format!("{user_id}@test.com"))
44
1
    .execute(pool)
45
1
    .await?;
46
1
    Ok(user_id)
47
1
}
48

            
49
/// The `account-edit` submit of the loaded account with its name set to `Cafe`.
50
1
async fn rename_account(session: &mut Session, id: &str) -> anyhow::Result<String> {
51
1
    let loaded = reply(session, &format!("(load-form \"account-edit\" \"{id}\")")).await;
52
1
    let mut app = crate::test_support::app();
53
1
    app.form_options_seq.advance();
54
1
    app.deliver_account_edit_reply(Some(app.window_of(crate::view::ViewId::Accounts)), &loaded);
55
1
    let Some(Modal::Form(mut form)) = app.overlays.pop() else {
56
        anyhow::bail!("the edit form is not open: {}", app.status);
57
    };
58
1
    let name = form
59
1
        .fields
60
1
        .iter_mut()
61
1
        .find(|field| field.key == Some("name"))
62
        .ok_or_else(|| anyhow::anyhow!("no name field"))?;
63
1
    name.widget.set_text("Cafe");
64
1
    Ok(submit_call(&ACCOUNT_EDIT, &form.values()))
65
1
}
66

            
67
#[sqlx::test(migrator = "server::db::MIGRATOR")]
68
async fn a_twin_create_and_rename_show_the_notice(pool: PgPool) -> anyhow::Result<()> {
69
    let _guard = server::db::install_test_pool(pool.clone());
70
    let user = seed_user(&pool).await?;
71
    let mut session = Session::new(ScriptCtx::new(user))?;
72
    let mut app = crate::test_support::app();
73
    reply(&mut session, &create_account("Cafe")).await;
74

            
75
    let twin = reply(&mut session, &create_account("Cafe")).await;
76
    app.deliver_reply(mutation(&app), &twin);
77

            
78
    assert_eq!(app.status, duplicate_name_notice("Cafe"));
79
    let bar = reply(&mut session, &create_account("Bar")).await;
80
    app.status.clear();
81
    app.deliver_reply(mutation(&app), &bar);
82
    assert!(app.status.is_empty(), "{}", app.status);
83
    let id = rpc::parse_response(&bar)
84
        .ok()
85
1
        .and_then(|r| match r.payload {
86
1
            rpc::ResponsePayload::Value(nomiscript::Value::String(id)) => Some(id),
87
            _ => None,
88
1
        })
89
        .unwrap_or_default();
90
    let form = rename_account(&mut session, &id).await?;
91
    let renamed = reply(&mut session, &form).await;
92
    app.deliver_reply(mutation(&app), &renamed);
93
    assert_eq!(app.status, duplicate_name_notice("Cafe"));
94
    Ok(())
95
}