| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #![allow(missing_docs)]
- use std::collections::BTreeMap;
- use std::sync::Arc;
- use kuatia::error::LedgerError;
- use kuatia::ledger::Ledger;
- use kuatia::mem_store::InMemoryStore;
- use kuatia::workflow::run_transfers;
- use kuatia_core::*;
- /// An amount of `asset` in minor units. An amount compares by asset id, and the
- /// tests register these same ids, so building the row here is equivalent to
- /// reading it back from the registry.
- fn money<V>(asset: AssetId, value: V) -> Amount
- where
- Amount: From<(V, Arc<Asset>)>,
- {
- Amount::from((value, Arc::new(Asset::new(asset, "TEST", "$", 2))))
- }
- fn usd() -> AssetId {
- AssetId::new(1)
- }
- fn account(id: i64) -> AccountId {
- AccountId::new(id)
- }
- fn external() -> AccountId {
- AccountId::new(99)
- }
- fn make_account(id: i64, flags: AccountFlags) -> Account {
- Account {
- id: AccountId::new(id),
- version: 1,
- flags,
- book: BookId(0),
- metadata: BTreeMap::new(),
- }
- }
- async fn setup_ledger() -> Arc<Ledger> {
- let store = InMemoryStore::new();
- let ledger = Arc::new(Ledger::new(store));
- for (id, policy) in [
- (1, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
- (2, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
- (3, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
- (99, AccountFlags::empty()),
- ] {
- ledger
- .store()
- .create_account(make_account(id, policy))
- .await
- .unwrap();
- }
- ledger
- .create_asset(Asset::new(usd(), "USD", "$", 2))
- .await
- .unwrap();
- ledger
- }
- fn deposit(to: AccountId, amount: i64) -> Transfer {
- TransferBuilder::new()
- .deposit(to, money(usd(), amount), external())
- .build()
- .unwrap()
- }
- fn pay(from: AccountId, to: AccountId, amount: i64) -> Transfer {
- TransferBuilder::new()
- .pay(from, to, money(usd(), amount))
- .build()
- .unwrap()
- }
- /// A two-movement workflow (deposit then pay) commits both legs in order.
- #[tokio::test]
- async fn workflow_happy_path() {
- let ledger = setup_ledger().await;
- let receipts = run_transfers(
- &ledger,
- &[deposit(account(1), 100), pay(account(1), account(2), 60)],
- )
- .await
- .unwrap();
- assert_eq!(receipts.len(), 2);
- assert_eq!(
- ledger
- .balance(&account(1), &usd())
- .await
- .unwrap()
- .minor_units(),
- 40
- );
- assert_eq!(
- ledger
- .balance(&account(2), &usd())
- .await
- .unwrap()
- .minor_units(),
- 60
- );
- assert_eq!(
- ledger
- .balance(&external(), &usd())
- .await
- .unwrap()
- .minor_units(),
- -100
- );
- }
- /// The second movement overspends and fails; the first (deposit) is reversed
- /// LIFO, so the net effect is zero. The typed `InsufficientFunds` reaches the
- /// caller.
- #[tokio::test]
- async fn workflow_compensation_on_failure() {
- let ledger = setup_ledger().await;
- let err = run_transfers(
- &ledger,
- &[deposit(account(1), 50), pay(account(1), account(2), 100)],
- )
- .await
- .unwrap_err();
- assert!(
- matches!(err, LedgerError::Selection(InsufficientFunds { .. })),
- "expected typed InsufficientFunds, got {err:?}"
- );
- // The deposit was compensated (reversed); the net effect is zero.
- assert_eq!(
- ledger
- .balance(&account(1), &usd())
- .await
- .unwrap()
- .minor_units(),
- 0
- );
- assert_eq!(
- ledger
- .balance(&external(), &usd())
- .await
- .unwrap()
- .minor_units(),
- 0
- );
- }
- /// A three-movement workflow commits all legs and settles the chain.
- #[tokio::test]
- async fn workflow_three_steps_happy() {
- let ledger = setup_ledger().await;
- let receipts = run_transfers(
- &ledger,
- &[
- deposit(account(1), 100),
- pay(account(1), account(2), 60),
- pay(account(2), account(3), 30),
- ],
- )
- .await
- .unwrap();
- assert_eq!(receipts.len(), 3);
- assert_eq!(
- ledger
- .balance(&account(1), &usd())
- .await
- .unwrap()
- .minor_units(),
- 40
- );
- assert_eq!(
- ledger
- .balance(&account(2), &usd())
- .await
- .unwrap()
- .minor_units(),
- 30
- );
- assert_eq!(
- ledger
- .balance(&account(3), &usd())
- .await
- .unwrap()
- .minor_units(),
- 30
- );
- }
|