Lines
100 %
Functions
Branches
//! Filling split rows the way a person does, and what they must store.
use anyhow::Result;
use super::harness::Harness;
use serde_json::json;
use super::page::{Pick, add_split, add_split_tag, choose, pick, set_text, wait_for};
use super::store::leg;
/// A split tag on one leg of its row.
#[derive(Clone, Copy)]
pub struct LegTag {
pub name: &'static str,
pub value: &'static str,
pub side: &'static str,
}
/// One split row, by seeded account and commodity names.
pub struct Split {
pub from: &'static str,
pub to: &'static str,
pub commodity: &'static str,
pub to_commodity: Option<&'static str>,
pub to_amount: Option<&'static str>,
pub tag: Option<LegTag>,
impl Split {
pub const fn plain(from: &'static str, to: &'static str, value: &'static str) -> Self {
Self {
from,
to,
commodity: "JPY",
to_commodity: None,
value,
to_amount: None,
tag: None,
pub const fn tagged(self, side: &'static str) -> Self {
tag: Some(LegTag {
name: "category",
value: "e2e",
side,
}),
..self
/// The two legs it stores, as `store::legs` lists them.
pub fn legs(&self, h: &Harness) -> [String; 2] {
let to_commodity = self.to_commodity.unwrap_or(self.commodity);
let received = self.to_amount.unwrap_or(self.value);
[
leg(
h.account(self.from),
h.commodity(self.commodity),
&format!("-{}/1", self.value),
),
h.account(self.to),
h.commodity(to_commodity),
&format!("{received}/1"),
]
/// The split tag it stores, as `store::leg_tags` lists it.
pub fn leg_tag(&self, h: &Harness) -> Option<String> {
let tag = self.tag?;
let [from, to] = self.legs(h);
let leg = if tag.side == "from" { from } else { to };
let mut words = leg.split(' ');
let account = words.next().unwrap_or_default();
let value = words.nth(1).unwrap_or_default();
Some(format!("{account} {value} {} {}", tag.name, tag.value))
/// A label of the account `name`: the name, a path ending in it, or the
/// name with an id suffix.
pub fn named(name: &'static str) -> impl Fn(&str) -> bool {
move |label: &str| {
label == name
|| label.ends_with(&format!(":{name}"))
|| label.starts_with(&format!("{name} ‹"))
/// Fills split row `row` (adding it first unless it is the first), and its
/// tag as split tag `tag_row`.
pub async fn fill_split(h: &Harness, row: usize, tag_row: usize, split: &Split) -> Result<()> {
if row > 0 {
add_split(h).await?;
let at = |cell: &str| format!("splits.{row}.{cell}");
pick(
h,
&at("from"),
split.from,
named(split.from),
0,
Pick::Mouse,
)
.await?;
pick(h, &at("to"), split.to, named(split.to), 0, Pick::Mouse).await?;
let commodity = split.commodity;
&at("from-commodity"),
commodity,
|l| l == commodity,
if let Some(to_commodity) = split.to_commodity {
&at("to-commodity"),
to_commodity,
|l| l == to_commodity,
Pick::Keyboard,
set_text(h, &at("value"), split.value).await?;
if let Some(received) = split.to_amount {
wait_for(
&h.driver,
"const i = document.querySelector(`[name=\"${arguments[0]}\"]`);\
return i && !i.disabled && i.offsetParent !== null;",
vec![json!(at("to-amount"))],
"the converted amount to be offered",
set_text(h, &at("to-amount"), received).await?;
if let Some(tag) = split.tag {
add_split_tag(h, row).await?;
let cell = |name: &str| format!("split-tags.{tag_row}.{name}");
set_text(h, &cell("name"), tag.name).await?;
set_text(h, &cell("value"), tag.value).await?;
choose(h, &cell("side"), tag.side).await?;
Ok(())
/// Fills every row of `splits` in order.
pub async fn fill_splits(h: &Harness, splits: &[Split]) -> Result<()> {
let mut tag_row = 0;
for (row, split) in splits.iter().enumerate() {
fill_split(h, row, tag_row, split).await?;
tag_row += usize::from(split.tag.is_some());