workflow.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #![allow(missing_docs)]
  2. use std::collections::BTreeMap;
  3. use std::sync::Arc;
  4. use kuatia::error::LedgerError;
  5. use kuatia::ledger::Ledger;
  6. use kuatia::mem_store::InMemoryStore;
  7. use kuatia::workflow::run_transfers;
  8. use kuatia_core::*;
  9. /// An amount of `asset` in minor units. An amount compares by asset id, and the
  10. /// tests register these same ids, so building the row here is equivalent to
  11. /// reading it back from the registry.
  12. fn money<V>(asset: AssetId, value: V) -> Amount
  13. where
  14. Amount: From<(V, Arc<Asset>)>,
  15. {
  16. Amount::from((value, Arc::new(Asset::new(asset, "TEST", "$", 2))))
  17. }
  18. fn usd() -> AssetId {
  19. AssetId::new(1)
  20. }
  21. fn account(id: i64) -> AccountId {
  22. AccountId::new(id)
  23. }
  24. fn external() -> AccountId {
  25. AccountId::new(99)
  26. }
  27. fn make_account(id: i64, flags: AccountFlags) -> Account {
  28. Account {
  29. id: AccountId::new(id),
  30. version: 1,
  31. flags,
  32. book: BookId(0),
  33. metadata: BTreeMap::new(),
  34. }
  35. }
  36. async fn setup_ledger() -> Arc<Ledger> {
  37. let store = InMemoryStore::new();
  38. let ledger = Arc::new(Ledger::new(store));
  39. for (id, policy) in [
  40. (1, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  41. (2, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  42. (3, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  43. (99, AccountFlags::empty()),
  44. ] {
  45. ledger
  46. .store()
  47. .create_account(make_account(id, policy))
  48. .await
  49. .unwrap();
  50. }
  51. ledger
  52. .create_asset(Asset::new(usd(), "USD", "$", 2))
  53. .await
  54. .unwrap();
  55. ledger
  56. }
  57. fn deposit(to: AccountId, amount: i64) -> Transfer {
  58. TransferBuilder::new()
  59. .deposit(to, money(usd(), amount), external())
  60. .build()
  61. .unwrap()
  62. }
  63. fn pay(from: AccountId, to: AccountId, amount: i64) -> Transfer {
  64. TransferBuilder::new()
  65. .pay(from, to, money(usd(), amount))
  66. .build()
  67. .unwrap()
  68. }
  69. /// A two-movement workflow (deposit then pay) commits both legs in order.
  70. #[tokio::test]
  71. async fn workflow_happy_path() {
  72. let ledger = setup_ledger().await;
  73. let receipts = run_transfers(
  74. &ledger,
  75. &[deposit(account(1), 100), pay(account(1), account(2), 60)],
  76. )
  77. .await
  78. .unwrap();
  79. assert_eq!(receipts.len(), 2);
  80. assert_eq!(
  81. ledger
  82. .balance(&account(1), &usd())
  83. .await
  84. .unwrap()
  85. .minor_units(),
  86. 40
  87. );
  88. assert_eq!(
  89. ledger
  90. .balance(&account(2), &usd())
  91. .await
  92. .unwrap()
  93. .minor_units(),
  94. 60
  95. );
  96. assert_eq!(
  97. ledger
  98. .balance(&external(), &usd())
  99. .await
  100. .unwrap()
  101. .minor_units(),
  102. -100
  103. );
  104. }
  105. /// The second movement overspends and fails; the first (deposit) is reversed
  106. /// LIFO, so the net effect is zero. The typed `InsufficientFunds` reaches the
  107. /// caller.
  108. #[tokio::test]
  109. async fn workflow_compensation_on_failure() {
  110. let ledger = setup_ledger().await;
  111. let err = run_transfers(
  112. &ledger,
  113. &[deposit(account(1), 50), pay(account(1), account(2), 100)],
  114. )
  115. .await
  116. .unwrap_err();
  117. assert!(
  118. matches!(err, LedgerError::Selection(InsufficientFunds { .. })),
  119. "expected typed InsufficientFunds, got {err:?}"
  120. );
  121. // The deposit was compensated (reversed); the net effect is zero.
  122. assert_eq!(
  123. ledger
  124. .balance(&account(1), &usd())
  125. .await
  126. .unwrap()
  127. .minor_units(),
  128. 0
  129. );
  130. assert_eq!(
  131. ledger
  132. .balance(&external(), &usd())
  133. .await
  134. .unwrap()
  135. .minor_units(),
  136. 0
  137. );
  138. }
  139. /// A three-movement workflow commits all legs and settles the chain.
  140. #[tokio::test]
  141. async fn workflow_three_steps_happy() {
  142. let ledger = setup_ledger().await;
  143. let receipts = run_transfers(
  144. &ledger,
  145. &[
  146. deposit(account(1), 100),
  147. pay(account(1), account(2), 60),
  148. pay(account(2), account(3), 30),
  149. ],
  150. )
  151. .await
  152. .unwrap();
  153. assert_eq!(receipts.len(), 3);
  154. assert_eq!(
  155. ledger
  156. .balance(&account(1), &usd())
  157. .await
  158. .unwrap()
  159. .minor_units(),
  160. 40
  161. );
  162. assert_eq!(
  163. ledger
  164. .balance(&account(2), &usd())
  165. .await
  166. .unwrap()
  167. .minor_units(),
  168. 30
  169. );
  170. assert_eq!(
  171. ledger
  172. .balance(&account(3), &usd())
  173. .await
  174. .unwrap()
  175. .minor_units(),
  176. 30
  177. );
  178. }