Lines
97.61 %
Functions
100 %
Branches
//! A new split starts as a copy of the one above it, ready to be edited.
use std::time::Duration;
use anyhow::{Result, ensure};
use serde_json::json;
use super::harness::{Harness, with_harness};
use super::page::{
Pick, add_split, erase, login, open, pick, reference, reply_id, set_text, submit, wait_for,
};
use super::rows::{Split, fill_splits, named};
use super::store::{leg, legs, snapshot};
const FOCUSED_NAME: &str = "return document.activeElement.name;";
async fn first_split(h: &Harness) -> Result<()> {
pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
pick(
h,
"splits.0.from-commodity",
"JPY",
|l| l == "JPY",
0,
Pick::Mouse,
)
.await?;
"splits.0.to-commodity",
"EUR",
|l| l == "EUR",
Pick::Keyboard,
Ok(())
}
async fn copied_cells(h: &Harness) -> Result<()> {
login(h).await?;
open(h, "/transaction/new").await?;
first_split(h).await?;
add_split(h).await?;
for cell in ["from", "from-commodity", "to-commodity"] {
let before = reference(h, &format!("splits.0.{cell}")).await?;
let after = reference(h, &format!("splits.1.{cell}")).await?;
ensure!(
before == after && !after.0.is_empty(),
"{cell}: {before:?} became {after:?}"
);
let focused = h.driver.run(FOCUSED_NAME, vec![]).await?;
focused == json!("label:splits.1.to"),
"the focus is on {focused}"
wait_for(
&h.driver,
"const i = document.querySelector('[name=\"splits.1.to-amount\"]');\
return i && !i.disabled && i.offsetParent !== null;",
vec![],
"the copied commodities to offer the converted amount",
.await
.map(drop)
async fn emptiness_is_copied(h: &Harness) -> Result<()> {
"return document.querySelector('[name=\"label:splits.0.to-commodity\"]').value !== '';",
"the from commodity to fill the to commodity",
erase(h, "splits.0.to-commodity").await?;
let to_commodity = reference(h, "splits.1.to-commodity").await?;
to_commodity == (String::new(), String::new()),
"{to_commodity:?}"
async fn copies_follow_a_new_pick(h: &Harness) -> Result<()> {
"return document.querySelector('[name=\"label:splits.0.to-commodity\"]').value === 'JPY';",
"splits.1.from-commodity",
"return document.querySelector('[name=\"label:splits.1.to-commodity\"]').value === 'EUR'\
&& document.querySelector('[name=\"splits.1.to-amount\"]').disabled;",
"the copied to commodity to follow the new from commodity",
async fn copies_submit_and_edit(h: &Harness) -> Result<()> {
pick(h, "splits.0.to", "JR", named("JR"), 0, Pick::Mouse).await?;
set_text(h, "splits.0.value", "199").await?;
"splits.1.to",
"McDonalds",
named("McDonalds"),
set_text(h, "splits.1.value", "600").await?;
let reply = submit(h).await?;
ensure!(reply.contains("created with ID"), "{reply}");
let stored = legs(&snapshot(h, reply_id(&reply)?).await?);
let (suica, jpy) = (h.account("Suica"), h.commodity("JPY"));
for (to, value) in [("JR", "199"), ("McDonalds", "600")] {
let expected = [
leg(suica, jpy, &format!("-{value}/1")),
leg(h.account(to), jpy, &format!("{value}/1")),
];
expected.iter().all(|l| stored.contains(l)),
"{stored:#?} lacks {expected:?}"
async fn copies_stay_editable(h: &Harness) -> Result<()> {
pick(h, "splits.1.from", "JR", named("JR"), 0, Pick::Mouse).await?;
let (shown, id) = reference(h, "splits.1.from").await?;
shown == "JR" && id == h.account("JR").to_string(),
"the copy was not editable: {shown:?} {id:?}"
async fn copies_survive_a_pick_without_a_fill(h: &Harness) -> Result<()> {
tokio::time::sleep(Duration::from_secs(1)).await;
for (cell, expected) in [("from-commodity", "JPY"), ("to-commodity", "EUR")] {
let (shown, _) = reference(h, &format!("splits.1.{cell}")).await?;
ensure!(shown == expected, "{cell} was cleared to {shown:?}");
async fn commodities_are(h: &Harness, row: usize, shown: &str, what: &str) -> Result<()> {
let script = format!(
"return ['from-commodity', 'to-commodity'].every(c => \
document.querySelector(`[name=\"label:splits.{row}.${{c}}\"]`).value === '{shown}');"
wait_for(&h.driver, &script, vec![], what).await.map(drop)
async fn copies_survive_a_rerender(h: &Harness) -> Result<()> {
reply.starts_with("INVALID"),
"the submit was accepted: {reply}"
/// Suica and JR hold JPY only once this is stored; Wallet holds nothing.
async fn store_a_train_ride(h: &Harness) -> Result<()> {
fill_splits(h, &[Split::plain("Suica", "JR", "199")]).await?;
async fn a_moved_copy_is_an_ordinary_fill(h: &Harness) -> Result<()> {
store_a_train_ride(h).await?;
"splits.0.from",
"Wallet",
named("Wallet"),
commodities_are(h, 0, "EUR", "the from commodity to fill the to commodity").await?;
pick(h, "splits.1.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
commodities_are(h, 1, "JPY", "Suica's commodity to move the copies").await?;
"splits.1.from",
commodities_are(h, 1, "", "Wallet to clear Suica's fills").await
#[supp_macro::shared_runtime_test]
async fn a_new_split_copies_the_accounts_and_commodities_above() -> Result<()> {
with_harness(|h| Box::pin(copied_cells(h))).await
async fn an_empty_to_commodity_is_copied_empty() -> Result<()> {
with_harness(|h| Box::pin(emptiness_is_copied(h))).await
async fn a_copied_to_commodity_follows_a_new_from_commodity() -> Result<()> {
with_harness(|h| Box::pin(copies_follow_a_new_pick(h))).await
async fn copied_ids_are_submitted_with_two_splits() -> Result<()> {
with_harness(|h| Box::pin(copies_submit_and_edit(h))).await
async fn a_copied_cell_can_be_picked_again() -> Result<()> {
with_harness(|h| Box::pin(copies_stay_editable(h))).await
async fn a_copied_cell_is_kept_by_a_pick_without_a_fill() -> Result<()> {
with_harness(|h| Box::pin(copies_survive_a_pick_without_a_fill(h))).await
async fn a_copied_cell_is_kept_after_a_validation_rerender() -> Result<()> {
with_harness(|h| Box::pin(copies_survive_a_rerender(h))).await
async fn a_copy_a_fill_moved_is_cleared_by_a_pick_without_a_fill() -> Result<()> {
with_harness(|h| Box::pin(a_moved_copy_is_an_ordinary_fill(h))).await