Lines
95.31 %
Functions
68.75 %
Branches
100 %
//! The activity totals on the wire are the ones the server computed.
use nomiscript::{Reader, Value, list_to_vec};
use num_rational::Rational64;
use rpc::forms::Outcome;
use rpc::try_expr_to_value;
use scripting::runtime::EvalValue;
use server::command::{
ActivityData, ActivityGroupResult, ActivityPeriod, CommodityAmount, ReportMeta, ReportNode,
};
use uuid::Uuid;
fn amount(id: Uuid, symbol: &str, num: i64, den: i64) -> CommodityAmount {
CommodityAmount {
commodity_id: id,
commodity_symbol: symbol.to_string(),
amount: Rational64::new(num, den),
}
fn node(
name: &str,
depth: usize,
amounts: Vec<CommodityAmount>,
children: Vec<ReportNode>,
) -> ReportNode {
ReportNode {
account_id: Uuid::new_v4(),
account_name: name.to_string(),
account_path: name.to_string(),
depth,
account_type: None,
own_amounts: amounts.clone(),
amounts,
children,
fn activity() -> ActivityData {
let (usd, eur) = (Uuid::new_v4(), Uuid::new_v4());
let salary = node("Salary", 1, vec![amount(usd, "USD", -500, 1)], vec![]);
let bonus = node("Bonus", 1, vec![amount(eur, "EUR", -10, 3)], vec![]);
let income = ActivityGroupResult::new(
"Income".to_string(),
true,
vec![
node(
"Income",
0,
vec![amount(usd, "USD", -500, 1), amount(eur, "EUR", -10, 3)],
vec![salary, bonus],
),
node("Gifts", 0, vec![amount(usd, "USD", -25, 2)], vec![]),
],
)
.expect("income total");
let rent = node("Rent", 1, vec![amount(usd, "USD", 200, 1)], vec![]);
let expense = ActivityGroupResult::new(
"Expense".to_string(),
false,
vec![node(
"Expense",
vec![amount(usd, "USD", 200, 1), amount(eur, "EUR", 1, 3)],
vec![rent],
)],
.expect("expense total");
ActivityData {
meta: ReportMeta {
date_from: None,
date_to: None,
target_commodity_id: None,
},
periods: vec![
ActivityPeriod::new(Some("2026-Q1".to_string()), vec![income, expense]).expect("net"),
fn field(plist: &Value, key: &str) -> anyhow::Result<Value> {
let items = list_to_vec(plist).ok_or_else(|| anyhow::anyhow!("not a plist: {plist:?}"))?;
let at = items
.iter()
.position(|v| matches!(v, Value::Symbol(s) if s.eq_ignore_ascii_case(key)))
.ok_or_else(|| anyhow::anyhow!("missing {key}"))?;
items
.get(at + 1)
.cloned()
.ok_or_else(|| anyhow::anyhow!("no value for {key}"))
fn items(list: &Value) -> anyhow::Result<Vec<Value>> {
list_to_vec(list).ok_or_else(|| anyhow::anyhow!("not a list: {list:?}"))
fn text(value: &Value) -> anyhow::Result<String> {
match value {
Value::String(s) => Ok(s.clone()),
other => anyhow::bail!("not a string: {other:?}"),
fn shown(amounts: &Value) -> anyhow::Result<Vec<(String, Rational64)>> {
items(amounts)?
.map(|a| {
let symbol = text(&field(a, ":symbol")?)?;
let value = match field(a, ":amount")? {
Value::String(s) => s,
other => nomiscript::format_value(&other),
let ratio = match value.split_once('/') {
Some((n, d)) => Rational64::new(n.parse()?, d.parse()?),
None => Rational64::new(value.parse()?, 1),
Ok((symbol, ratio))
})
.collect()
fn pair(symbol: &str, num: i64, den: i64) -> (String, Rational64) {
(symbol.to_string(), Rational64::new(num, den))
#[test]
fn wire_totals_equal_the_server_view() -> anyhow::Result<()> {
let EvalValue::String(wire) = Outcome::Activity(activity()).to_value()? else {
anyhow::bail!("activity wire is not a string");
let program = Reader::parse(&wire)?;
let expr = program
.exprs
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("empty wire"))?;
let plist = try_expr_to_value(expr)?;
let periods = items(&field(&plist, ":periods")?)?;
let period = &periods[0];
let groups = items(&field(period, ":groups")?)?;
assert_eq!(
shown(&field(&groups[0], ":total")?)?,
[pair("USD", 1025, 2), pair("EUR", 10, 3)]
);
shown(&field(&groups[1], ":total")?)?,
[pair("USD", 200, 1), pair("EUR", 1, 3)]
shown(&field(period, ":net")?)?,
[pair("USD", 625, 2), pair("EUR", 3, 1)]
let roots = items(&field(&groups[0], ":roots")?)?;
shown(&field(&roots[0], ":amounts")?)?,
[pair("USD", 500, 1), pair("EUR", 10, 3)]
Ok(())