Lines
97.03 %
Functions
85 %
Branches
100 %
//! The category report's descriptor form: change the sort and the tag,
//! generate, and see the table follow while the form keeps its values.
use anyhow::{Result, anyhow, ensure};
use chrono::Utc;
use finance::split::Split;
use serde_json::json;
use server::command::FinanceEntity;
use server::command::account::SetAccountTag;
use server::command::split::SetSplitTag;
use server::command::transaction::CreateTransaction;
use uuid::Uuid;
use super::harness::{Harness, with_harness};
use super::page::{choose, login, open, set_text, wait_for};
const FIRST_CELLS: &str = ".report-data table.report-table tbody tr td:first-child";
/// An expense of `amount` into `Cafe`, its leg tagged with each of `tags`.
async fn spend(h: &Harness, amount: i64, tags: &[(&str, &str)]) -> Result<()> {
let tx_id = Uuid::new_v4();
let leg = |account: &str, value_num| Split {
id: Uuid::new_v4(),
tx_id,
account_id: h.account(account),
commodity_id: h.commodity("JPY"),
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
};
let (from, to) = (leg("Wallet", -amount), leg("Cafe", amount));
let to_id = to.id;
let now = Utc::now();
CreateTransaction::new()
.user_id(h.tenant.id)
.splits([from, to].into_iter().map(FinanceEntity::Split).collect())
.id(tx_id)
.post_date(now)
.enter_date(now)
.run()
.await
.map_err(|e| anyhow!("{e:?}"))?;
for (name, value) in tags {
SetSplitTag::new()
.split_id(to_id)
.tag_name((*name).to_string())
.tag_value((*value).to_string())
}
Ok(())
async fn seed(h: &Harness) -> Result<()> {
SetAccountTag::new()
.account_id(h.account("Cafe"))
.tag_name("type".to_string())
.tag_value("expense".to_string())
spend(h, 600, &[("category", "Rent"), ("kind", "Zed")]).await?;
spend(h, 300, &[("category", "Fun"), ("kind", "Abe")]).await
async fn generate(h: &Harness) -> Result<()> {
let button = h
.driver
.find("form.nomi-form button[type=\"submit\"]")
.await?;
h.driver.click(&button).await
async fn names(h: &Harness) -> Result<serde_json::Value> {
h.driver
.run(
"return [...document.querySelectorAll(arguments[0])].map(c => c.textContent.trim());",
vec![json!(FIRST_CELLS)],
)
async fn wait_names(h: &Harness, want: &serde_json::Value, what: &str) -> Result<()> {
wait_for(
&h.driver,
"const got = [...document.querySelectorAll(arguments[0])].map(c => c.textContent.trim());\
return JSON.stringify(got) === JSON.stringify(arguments[1]) ? true : null;",
vec![json!(FIRST_CELLS), want.clone()],
what,
.map(drop)
async fn kept(h: &Harness) -> Result<serde_json::Value> {
"return [document.querySelector('select[name=\"sort\"]').value,\
document.querySelector('input[name=\"tag\"]').value,\
document.querySelector('.report-data table.report-table th').textContent.trim()];",
vec![],
async fn change_and_generate(h: &Harness) -> Result<()> {
seed(h).await?;
login(h).await?;
open(h, "/report/category-breakdown").await?;
wait_names(h, &json!(["Rent", "Fun"]), "the default order").await?;
choose(h, "sort", "name_asc").await?;
generate(h).await?;
wait_names(h, &json!(["Fun", "Rent"]), "the name order").await?;
set_text(h, "tag", "kind").await?;
wait_names(h, &json!(["Abe", "Zed"]), "the other tag's rows").await?;
let after = kept(h).await?;
ensure!(after == json!(["name_asc", "kind", "kind"]), "kept {after}");
ensure!(names(h).await? == json!(["Abe", "Zed"]), "rows");
#[supp_macro::shared_runtime_test]
async fn changing_the_sort_and_tag_regenerates_the_category_table() -> Result<()> {
with_harness(|h| Box::pin(change_and_generate(h))).await