workflow.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. fn usd() -> AssetId {
  10. AssetId::new(1)
  11. }
  12. fn account(id: i64) -> AccountId {
  13. AccountId::new(id)
  14. }
  15. fn external() -> AccountId {
  16. AccountId::new(99)
  17. }
  18. fn make_account(id: i64, flags: AccountFlags) -> Account {
  19. Account {
  20. id: AccountId::new(id),
  21. version: 1,
  22. flags,
  23. book: BookId(0),
  24. metadata: BTreeMap::new(),
  25. }
  26. }
  27. async fn setup_ledger() -> Arc<Ledger> {
  28. let store = InMemoryStore::new();
  29. let ledger = Arc::new(Ledger::new(store));
  30. for (id, policy) in [
  31. (1, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  32. (2, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  33. (3, AccountFlags::DEBIT_MUST_NOT_EXCEED_CREDIT),
  34. (99, AccountFlags::empty()),
  35. ] {
  36. ledger
  37. .store()
  38. .create_account(make_account(id, policy))
  39. .await
  40. .unwrap();
  41. }
  42. ledger
  43. }
  44. fn deposit(to: AccountId, amount: Cent) -> Transfer {
  45. TransferBuilder::new()
  46. .deposit(to, usd(), amount, external())
  47. .unwrap()
  48. .build()
  49. }
  50. fn pay(from: AccountId, to: AccountId, amount: Cent) -> Transfer {
  51. TransferBuilder::new().pay(from, to, usd(), amount).build()
  52. }
  53. /// A two-movement workflow (deposit then pay) commits both legs in order.
  54. #[tokio::test]
  55. async fn workflow_happy_path() {
  56. let ledger = setup_ledger().await;
  57. let receipts = run_transfers(
  58. &ledger,
  59. &[
  60. deposit(account(1), Cent::from(100)),
  61. pay(account(1), account(2), Cent::from(60)),
  62. ],
  63. )
  64. .await
  65. .unwrap();
  66. assert_eq!(receipts.len(), 2);
  67. assert_eq!(
  68. ledger.balance(&account(1), &usd()).await.unwrap(),
  69. Cent::from(40)
  70. );
  71. assert_eq!(
  72. ledger.balance(&account(2), &usd()).await.unwrap(),
  73. Cent::from(60)
  74. );
  75. assert_eq!(
  76. ledger.balance(&external(), &usd()).await.unwrap(),
  77. Cent::from(-100)
  78. );
  79. }
  80. /// The second movement overspends and fails; the first (deposit) is reversed
  81. /// LIFO, so the net effect is zero. The typed `InsufficientFunds` reaches the
  82. /// caller.
  83. #[tokio::test]
  84. async fn workflow_compensation_on_failure() {
  85. let ledger = setup_ledger().await;
  86. let err = run_transfers(
  87. &ledger,
  88. &[
  89. deposit(account(1), Cent::from(50)),
  90. pay(account(1), account(2), Cent::from(100)),
  91. ],
  92. )
  93. .await
  94. .unwrap_err();
  95. assert!(
  96. matches!(err, LedgerError::Selection(InsufficientFunds { .. })),
  97. "expected typed InsufficientFunds, got {err:?}"
  98. );
  99. // The deposit was compensated (reversed); the net effect is zero.
  100. assert_eq!(
  101. ledger.balance(&account(1), &usd()).await.unwrap(),
  102. Cent::ZERO
  103. );
  104. assert_eq!(
  105. ledger.balance(&external(), &usd()).await.unwrap(),
  106. Cent::ZERO
  107. );
  108. }
  109. /// A three-movement workflow commits all legs and settles the chain.
  110. #[tokio::test]
  111. async fn workflow_three_steps_happy() {
  112. let ledger = setup_ledger().await;
  113. let receipts = run_transfers(
  114. &ledger,
  115. &[
  116. deposit(account(1), Cent::from(100)),
  117. pay(account(1), account(2), Cent::from(60)),
  118. pay(account(2), account(3), Cent::from(30)),
  119. ],
  120. )
  121. .await
  122. .unwrap();
  123. assert_eq!(receipts.len(), 3);
  124. assert_eq!(
  125. ledger.balance(&account(1), &usd()).await.unwrap(),
  126. Cent::from(40)
  127. );
  128. assert_eq!(
  129. ledger.balance(&account(2), &usd()).await.unwrap(),
  130. Cent::from(30)
  131. );
  132. assert_eq!(
  133. ledger.balance(&account(3), &usd()).await.unwrap(),
  134. Cent::from(30)
  135. );
  136. }