Lines
94.74 %
Functions
100 %
Branches
//! Account merge: pick two accounts, confirm naming both, merged.
use crate::tabs::fetch::Fetch;
use cli_core::account::merge_preview_call;
use cli_core::transaction::submit_call;
use forms::{ACCOUNT_MERGE, FormValues};
use nomiscript::Value;
use rpc::{ResponsePayload, ScriptCtx, Session};
use server::command::account::CreateAccount;
use server::command::{CmdResult, FinanceEntity};
use sqlx::PgPool;
use sqlx::types::Uuid;
use crate::event::{Intent, apply};
use crate::form::Form;
use crate::modal::{ConfirmAction, Modal};
use crate::widgets::Widget;
async fn reply(session: &mut Session, form: &str) -> String {
session.handle_form(&format!("(:id 1 :form {form})")).await
}
pub(super) async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
let user_id = Uuid::new_v4();
sqlx::query(
"INSERT INTO users \
(id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
)
.bind(user_id)
.bind(format!("u{user_id}"))
.bind(format!("{user_id}@test.com"))
.execute(pool)
.await?;
Ok(user_id)
async fn account(user_id: Uuid, name: &str) -> anyhow::Result<Uuid> {
match CreateAccount::new()
.name(name.to_string())
.user_id(user_id)
.run()
.await?
{
Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
fn pick(form: &mut Form, key: &str, text: &str) {
let select = form
.fields
.iter_mut()
.find(|field| field.key == Some(key))
.map(|field| &mut field.widget);
if let Some(Widget::Select(select)) = select {
text.chars().for_each(|c| select.filter_push(c));
select.confirm();
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn picked_accounts_are_confirmed_by_label_and_merged(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let into = account(user, "Coffee").await?;
let from = account(user, "Cafe").await?;
let mut session = Session::new(ScriptCtx::new(user))?;
let mut app = crate::test_support::app();
app.attach_echo();
app.accounts_mut().state = Fetch::Loaded(Vec::new());
app.open_account_merge(FormValues::default());
let options = reply(&mut session, r#"(form-options "accounts")"#).await;
app.route_reply(1, &options);
if let Some(Modal::Form(form)) = app.overlays.top_mut() {
pick(form, "into", "Coffee");
pick(form, "from", "Cafe");
let Some(Modal::Form(open)) = app.overlays.top() else {
anyhow::bail!("the form is gone");
};
let picked = open.values();
apply(&mut app, Intent::Submit);
let preview = reply(&mut session, &merge_preview_call(&picked)).await;
app.route_reply(2, &preview);
let Some(Modal::Confirm {
prompt,
action: ConfirmAction::MergeAccounts(form),
..
}) = app.overlays.top()
else {
anyhow::bail!("no merge confirmation: {:?}", app.overlays.top());
assert!(
prompt.starts_with("Merge account Cafe into Coffee: 0 splits move"),
"{prompt}"
);
let values = form.values();
assert_eq!(values.text("into"), Some(into.to_string().as_str()));
assert_eq!(values.text("from"), Some(from.to_string().as_str()));
let wire = reply(&mut session, &submit_call(&ACCOUNT_MERGE, &values)).await;
let merged = rpc::parse_response(&wire).map(|response| response.payload);
matches!(&merged, Ok(ResponsePayload::Value(Value::String(id))) if *id == into.to_string()),
"{wire}"
apply(&mut app, Intent::ConfirmYes);
app.pending_form.is_some(),
"the confirmed form awaits its reply"
Ok(())