Lines
95 %
Functions
94.12 %
Branches
100 %
//! `?from_account` leaves the commodity cells exactly as a pick of that account does.
use anyhow::{Result, anyhow, ensure};
use serde_json::{Value, json};
use server::command::{CmdResult, FinanceEntity, account::CreateAccount};
use uuid::Uuid;
use super::harness::{Harness, with_harness};
use super::page::{Pick, login, open, pick, reference, reply_id, set_text, submit, wait_for};
use super::rows::named;
const CELLS: [&str; 2] = ["splits.0.from-commodity", "splits.0.to-commodity"];
async fn account(h: &Harness, name: &str) -> Result<Uuid> {
let created = CreateAccount::new()
.name(name.to_string())
.user_id(h.tenant.id)
.run()
.await
.map_err(|e| anyhow!("{e:?}"))?;
match created {
Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
other => Err(anyhow!("expected an account, got {other:?}")),
}
async fn pick_account(h: &Harness, address: &str, name: &'static str) -> Result<()> {
pick(h, address, name, named(name), 0, Pick::Mouse)
.map(drop)
/// Makes `name` hold only `commodity`, by one posted transaction into `sink`.
async fn hold(h: &Harness, name: &'static str, commodity: &'static str) -> Result<()> {
open(h, "/transaction/new").await?;
pick_account(h, "splits.0.from", name).await?;
pick_account(h, "splits.0.to", "PrefillSink").await?;
pick(h, CELLS[0], commodity, |l| l == commodity, 0, Pick::Mouse).await?;
set_text(h, "splits.0.value", "1").await?;
let reply = submit(h).await?;
ensure!(reply.contains("created with ID"), "{reply}");
reply_id(&reply).map(drop)
/// What each commodity cell shows, carries and is marked as.
async fn cells(h: &Harness) -> Result<Vec<(String, String, bool)>> {
let mut state = Vec::new();
for cell in CELLS {
let (shown, id) = reference(h, cell).await?;
let filled = h
.driver
.run(
"return document.querySelector(`input[data-source][name=\"label:${arguments[0]}\"]`)\
.hasAttribute('data-filled');",
vec![json!(cell)],
)
.await?;
state.push((shown, id, filled == true));
Ok(state)
async fn settled(h: &Harness, shown: &str) -> Result<Value> {
wait_for(
&h.driver,
"const c = document.querySelector('[name=\"label:splits.0.to-commodity\"]');\
return c && c.value === arguments[0];",
vec![json!(shown)],
"the commodity fill",
async fn prefilled_like_a_pick(h: &Harness) -> Result<()> {
let sink = account(h, "PrefillSink").await?;
let (yen, euro) = (
account(h, "PrefillYen").await?,
account(h, "PrefillEuro").await?,
);
ensure!(sink != yen && yen != euro);
login(h).await?;
hold(h, "PrefillYen", "JPY").await?;
hold(h, "PrefillEuro", "EUR").await?;
pick_account(h, "splits.0.from", "PrefillYen").await?;
settled(h, "JPY").await?;
let picked = cells(h).await?;
ensure!(picked[0].0 == "JPY" && picked[0].2, "{picked:?}");
open(h, &format!("/transaction/new?from_account={yen}")).await?;
let prefilled = cells(h).await?;
ensure!(
prefilled == picked,
"prefilled {prefilled:?}, picked {picked:?}"
pick_account(h, "splits.0.from", "PrefillEuro").await?;
settled(h, "EUR").await?;
let moved = cells(h).await?;
moved[0].0 == "EUR" && moved[1].0 == "EUR" && moved.iter().all(|c| c.2),
"{moved:?}"
ensure!(moved[0].1 == h.commodity("EUR").to_string(), "{moved:?}");
Ok(())
#[supp_macro::shared_runtime_test]
async fn a_prefilled_from_account_fills_the_commodities_like_a_pick() -> Result<()> {
with_harness(|h| Box::pin(prefilled_like_a_pick(h))).await