Lines
92.8 %
Functions
83.33 %
Branches
100 %
//! Merging two accounts from the account manage page: pick both, review the
//! labels and the splits that move, merge.
use anyhow::{Result, ensure};
use chrono::Utc;
use finance::split::Split;
use server::command::FinanceEntity;
use server::command::transaction::CreateTransaction;
use uuid::Uuid;
use super::driver::Driver;
use super::harness::{Harness, with_harness};
use super::page::{Pick, login, open, pick, reference, wait_for};
/// One split pair moving `amount` JPY from `from` to `to`.
async fn transfer(h: &Harness, from: Uuid, to: Uuid, amount: i64) -> Result<()> {
let tx_id = Uuid::new_v4();
let splits = [(from, -amount), (to, amount)]
.into_iter()
.map(|(account_id, value_num)| {
FinanceEntity::Split(Split {
id: Uuid::new_v4(),
tx_id,
account_id,
commodity_id: h.commodity("JPY"),
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
})
.collect();
let now = Utc::now();
CreateTransaction::new()
.user_id(h.tenant.id)
.splits(splits)
.id(tx_id)
.post_date(now)
.enter_date(now)
.run()
.await
.map_err(|e| anyhow::anyhow!("{e:?}"))?;
Ok(())
}
/// The path the page's link reading `text` points to.
async fn link_to(driver: &Driver, text: &str) -> Result<String> {
let href = driver
.run(
"const a = [...document.querySelectorAll('a')]\
.find(a => a.textContent.trim() === arguments[0]);\
return a ? a.getAttribute('href') : null;",
vec![serde_json::json!(text)],
)
.await?;
href.as_str()
.map(str::to_string)
.ok_or_else(|| anyhow::anyhow!("no link {text:?}"))
/// A label shown for the top-level `Cafe` accounts (not the child path).
fn top_cafe(label: &str) -> bool {
label.starts_with("Cafe") && !label.contains(':') && !label.contains("McDonalds")
async fn merge_twins(h: &Harness) -> Result<()> {
let (cafe, twin) = (h.account("Cafe"), h.account("Cafe2"));
transfer(h, h.account("Wallet"), twin, 600).await?;
transfer(h, h.account("Suica"), twin, 300).await?;
login(h).await?;
h.driver.goto(&h.url("/account/manage")).await?;
let merge_page = link_to(&h.driver, "Merge accounts").await?;
open(h, &merge_page).await?;
let into = pick(h, "into", "Caf", top_cafe, 0, Pick::Mouse).await?;
let from = pick(h, "from", "Caf", top_cafe, 1, Pick::Mouse).await?;
let (_, into_id) = reference(h, "into").await?;
let from_id = match into_id == cafe.to_string() {
true => twin,
false => cafe,
};
let button = h
.driver
.find("form.nomi-form button[type=\"submit\"]")
h.driver.click(&button).await?;
let summary = wait_for(
&h.driver,
"const s = document.querySelector('.merge-summary');\
return s ? s.textContent.replace(/\\s+/g, ' ').trim() : null;",
vec![],
"the merge review",
let summary = summary.as_str().unwrap_or_default();
ensure!(
summary.starts_with(&format!("Merge {from} into {into}:")),
"{summary}"
);
let moving = if from_id == twin {
"2 splits move"
} else {
"0 splits move"
ensure!(summary.contains(moving), "{summary}");
let result = wait_for(
"const r = document.querySelector('.merge-result');\
return r ? r.textContent.replace(/\\s+/g, ' ').trim() : null;",
"the merge result",
let result = result.as_str().unwrap_or_default();
result.contains(&format!("{from} into {into}"))
&& result.contains(&format!("{into} was kept")),
"{result}"
result.contains(moving.replace("move", "moved").as_str()),
let buttons = h
"return document.querySelectorAll('#nf-account-merge button').length;",
ensure!(buttons == serde_json::json!(0), "a button stays: {buttons}");
link_to(&h.driver, "Merge more accounts").await? == "/account/merge",
"no link to a fresh merge form"
link_to(&h.driver, "Accounts").await? == "/account/list",
"no link to the accounts list"
let mut conn = crate::common::tenant::tenant_connection(h.tenant.id).await;
let gone: i64 = sqlx::query_scalar("SELECT count(*) FROM accounts WHERE id = $1")
.bind(from_id)
.fetch_one(&mut *conn)
ensure!(gone == 0, "the merged account is still stored");
#[supp_macro::shared_runtime_test]
async fn picked_twins_are_reviewed_and_merged() -> Result<()> {
with_harness(|h| Box::pin(merge_twins(h))).await