Lines
96.51 %
Functions
100 %
Branches
//! Picking an entity: the visible control only ever shows a label, and the
//! submit stores the entity picked, by mouse, keyboard or exact typed label.
use anyhow::{Result, ensure};
use serde_json::json;
use super::checks::check_page;
use super::driver::TAB;
use super::harness::{Harness, with_harness};
use super::page::{
Pick, login, open, pick, reference, reply_id, set_text, submit, visible, wait_for,
};
use super::rows::named;
use super::store::{leg, legs, snapshot};
/// Whether `text` holds a UUID, dashed or not.
fn shows_an_id(text: &str) -> bool {
text.split(|c: char| !(c.is_ascii_hexdigit() || c == '-'))
.any(|word| uuid::Uuid::parse_str(word).is_ok() || word.replace('-', "").len() >= 32)
}
/// 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 mouse_pick_of_a_duplicate(h: &Harness) -> Result<()> {
login(h).await?;
open(h, "/transaction/new").await?;
pick(h, "splits.0.from", "Suica", named("Suica"), 0, Pick::Mouse).await?;
let label = pick(h, "splits.0.to", "Caf", top_cafe, 1, Pick::Mouse).await?;
let (shown, id) = reference(h, "splits.0.to").await?;
ensure!(
!shows_an_id(&shown),
"the To control shows an id: {shown:?}"
);
ensure!(shown == label, "shown {shown:?}, picked {label:?}");
let cafes = [h.account("Cafe"), h.account("Cafe2")];
cafes.iter().any(|cafe| cafe.to_string() == id),
"the To reference {id:?} is no Cafe"
pick(
h,
"splits.0.from-commodity",
"JPY",
|l| l == "JPY",
0,
Pick::Mouse,
)
.await?;
set_text(h, "splits.0.value", "600").await?;
check_page(h, "new-duplicate-picked", &[]).await?;
let reply = submit(h).await?;
ensure!(reply.contains("created with ID"), "{reply}");
let stored = legs(&snapshot(h, reply_id(&reply)?).await?);
let jpy = h.commodity("JPY");
let to = leg(id.parse()?, jpy, "600/1");
ensure!(stored.contains(&to), "{stored:#?} lacks {to}");
Ok(())
#[supp_macro::shared_runtime_test]
async fn a_mouse_picked_duplicate_shows_its_label_and_stores_its_id() -> Result<()> {
with_harness(|h| Box::pin(mouse_pick_of_a_duplicate(h))).await
async fn labels_are_names(h: &Harness) -> Result<()> {
let input = visible(h, "splits.0.to").await?;
h.driver.click(&input).await?;
h.driver.type_into(&input, "Caf").await?;
let labels = wait_for(
&h.driver,
"const l = [...document.querySelectorAll('.autocomplete-results .autocomplete-item')]\
.map(i => i.firstChild.textContent.trim()); return l.length >= 3 ? l : null;",
vec![],
"the Cafe suggestions",
let labels: Vec<String> = serde_json::from_value(labels)?;
let mut unique = labels.clone();
unique.sort();
unique.dedup();
ensure!(unique.len() == labels.len(), "duplicate labels: {labels:?}");
ensure!(labels.contains(&"McDonalds".to_string()), "{labels:?}");
labels
.iter()
.filter(|l| top_cafe(l))
.all(|l| l.starts_with("Cafe ‹")),
"{labels:?}"
h.driver.clear(&input).await?;
pick(h, "splits.0.to", "JR", |l| l == "JR", 0, Pick::Keyboard).await?;
shown == "JR" && id == h.account("JR").to_string(),
"{shown} {id}"
async fn picks_show_bare_names_and_duplicates_disambiguated() -> Result<()> {
with_harness(|h| Box::pin(labels_are_names(h))).await
async fn typed_text(h: &Harness) -> Result<()> {
let from = visible(h, "splits.0.from").await?;
h.driver.click(&from).await?;
h.driver.type_into(&from, &format!("Suica{TAB}")).await?;
let suica = h.account("Suica").to_string();
wait_for(
"return document.querySelector('input[type=hidden][name=\"splits.0.from\"]').value \
=== arguments[0];",
vec![json!(suica)],
"the typed label Suica to pick its account",
let to = visible(h, "splits.0.to").await?;
h.driver.click(&to).await?;
h.driver.type_into(&to, &format!("Caf{TAB}")).await?;
set_text(h, "splits.0.value", "5").await?;
reply.contains("pick an entry from the list"),
"unpicked text was not refused: {reply}"
let typed = h
.driver
.run(
"return document.querySelector('input[name=\"label:splits.0.to\"]').value;",
ensure!(typed == json!("Caf"), "the typed text is lost: {typed}");
check_page(h, "new-unpicked-refused", &[422]).await?;
pick(h, "splits.0.to", "JR", |l| l == "JR", 0, Pick::Mouse).await?;
let jr = leg(h.account("JR"), h.commodity("JPY"), "5/1");
ensure!(stored.contains(&jr), "{stored:#?}");
async fn typed_text_resolves_only_as_an_exact_label() -> Result<()> {
with_harness(|h| Box::pin(typed_text(h))).await
async fn field_errors(h: &Harness) -> Result<Vec<String>> {
let found = h
"return [...document.querySelectorAll('.field-error')].map(e => e.textContent.trim());",
Ok(serde_json::from_value(found)?)
async fn success_clears_field_errors(h: &Harness) -> Result<()> {
h.driver.type_into(&to, &format!("Cafeteria{TAB}")).await?;
set_text(h, "splits.0.value", "7").await?;
ensure!(reply.contains("pick an entry from the list"), "{reply}");
let shown = field_errors(h).await?;
ensure!(shown.len() == 1, "expected one field error: {shown:?}");
ensure!(shown.len() == 1, "errors accumulated: {shown:?}");
let stale = field_errors(h).await?;
stale.is_empty(),
"stale field errors after success: {stale:?}"
async fn a_successful_submit_leaves_no_stale_field_errors() -> Result<()> {
with_harness(|h| Box::pin(success_clears_field_errors(h))).await