integration.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #![allow(missing_docs)]
  2. use std::sync::Arc;
  3. use kuatia::ledger::Ledger;
  4. use kuatia::mem_store::InMemoryStore;
  5. use kuatia_core::*;
  6. use std::collections::BTreeMap;
  7. fn usd() -> AssetId {
  8. AssetId::new(1)
  9. }
  10. fn eur() -> AssetId {
  11. AssetId::new(2)
  12. }
  13. fn account(id: i64) -> AccountId {
  14. AccountId::new(id)
  15. }
  16. fn external() -> AccountId {
  17. AccountId::new(99)
  18. }
  19. fn make_account(id: i64, policy: AccountPolicy) -> Account {
  20. Account {
  21. id: AccountId::new(id),
  22. version: 1,
  23. policy,
  24. flags: AccountFlags::empty(),
  25. book: BookId(0),
  26. user_data: UserData::default(),
  27. metadata: BTreeMap::new(),
  28. }
  29. }
  30. async fn setup_ledger() -> Arc<Ledger> {
  31. let store = InMemoryStore::new();
  32. let ledger = Arc::new(Ledger::new(store));
  33. ledger
  34. .store()
  35. .create_account(make_account(1, AccountPolicy::NoOverdraft))
  36. .await
  37. .unwrap();
  38. ledger
  39. .store()
  40. .create_account(make_account(2, AccountPolicy::NoOverdraft))
  41. .await
  42. .unwrap();
  43. ledger
  44. .store()
  45. .create_account(make_account(3, AccountPolicy::NoOverdraft))
  46. .await
  47. .unwrap();
  48. ledger
  49. .store()
  50. .create_account(make_account(99, AccountPolicy::ExternalAccount))
  51. .await
  52. .unwrap();
  53. ledger
  54. }
  55. /// Helper: deposit via commit()
  56. async fn deposit(
  57. ledger: &Arc<Ledger>,
  58. to: AccountId,
  59. asset: AssetId,
  60. amount: Cent,
  61. ext: AccountId,
  62. ) -> Receipt {
  63. let transfer = TransferBuilder::new()
  64. .deposit(to, asset, amount, ext)
  65. .unwrap()
  66. .build();
  67. ledger.commit(transfer).await.unwrap()
  68. }
  69. /// Helper: pay via commit()
  70. async fn pay(
  71. ledger: &Arc<Ledger>,
  72. from: AccountId,
  73. to: AccountId,
  74. asset: AssetId,
  75. amount: Cent,
  76. ) -> Receipt {
  77. let transfer = TransferBuilder::new().pay(from, to, asset, amount).build();
  78. ledger.commit(transfer).await.unwrap()
  79. }
  80. /// Helper: withdraw via commit()
  81. async fn withdraw(
  82. ledger: &Arc<Ledger>,
  83. from: AccountId,
  84. asset: AssetId,
  85. amount: Cent,
  86. ext: AccountId,
  87. ) -> Receipt {
  88. let transfer = TransferBuilder::new()
  89. .withdraw(from, asset, amount, ext)
  90. .build();
  91. ledger.commit(transfer).await.unwrap()
  92. }
  93. // ---------------------------------------------------------------------------
  94. // §4.1 Deposit
  95. // ---------------------------------------------------------------------------
  96. #[tokio::test]
  97. async fn deposit_creates_balanced_postings() {
  98. let ledger = setup_ledger().await;
  99. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  100. assert_eq!(
  101. ledger.balance(&account(1), &usd()).await.unwrap(),
  102. Cent::from(100)
  103. );
  104. assert_eq!(
  105. ledger.balance(&external(), &usd()).await.unwrap(),
  106. Cent::from(-100)
  107. );
  108. }
  109. // ---------------------------------------------------------------------------
  110. // §4.2 Internal transfer with change
  111. // ---------------------------------------------------------------------------
  112. #[tokio::test]
  113. async fn pay_with_change() {
  114. let ledger = setup_ledger().await;
  115. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  116. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  117. assert_eq!(
  118. ledger.balance(&account(1), &usd()).await.unwrap(),
  119. Cent::from(50)
  120. );
  121. assert_eq!(
  122. ledger.balance(&account(2), &usd()).await.unwrap(),
  123. Cent::from(50)
  124. );
  125. assert_eq!(
  126. ledger.balance(&external(), &usd()).await.unwrap(),
  127. Cent::from(-100)
  128. );
  129. }
  130. // ---------------------------------------------------------------------------
  131. // §4.3 Multi-hop
  132. // ---------------------------------------------------------------------------
  133. #[tokio::test]
  134. async fn multi_hop_transfer() {
  135. let ledger = setup_ledger().await;
  136. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  137. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  138. pay(&ledger, account(2), account(3), usd(), Cent::from(20)).await;
  139. assert_eq!(
  140. ledger.balance(&account(1), &usd()).await.unwrap(),
  141. Cent::from(50)
  142. );
  143. assert_eq!(
  144. ledger.balance(&account(2), &usd()).await.unwrap(),
  145. Cent::from(30)
  146. );
  147. assert_eq!(
  148. ledger.balance(&account(3), &usd()).await.unwrap(),
  149. Cent::from(20)
  150. );
  151. assert_eq!(
  152. ledger.balance(&external(), &usd()).await.unwrap(),
  153. Cent::from(-100)
  154. );
  155. }
  156. // ---------------------------------------------------------------------------
  157. // §4.5 Withdrawal
  158. // ---------------------------------------------------------------------------
  159. #[tokio::test]
  160. async fn withdrawal_reduces_external_liability() {
  161. let ledger = setup_ledger().await;
  162. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  163. withdraw(&ledger, account(1), usd(), Cent::from(50), external()).await;
  164. assert_eq!(
  165. ledger.balance(&account(1), &usd()).await.unwrap(),
  166. Cent::from(50)
  167. );
  168. assert_eq!(
  169. ledger.balance(&external(), &usd()).await.unwrap(),
  170. Cent::from(-50)
  171. );
  172. }
  173. // ---------------------------------------------------------------------------
  174. // Full round-trip: deposit -> pay -> withdraw -> verify total = 0
  175. // ---------------------------------------------------------------------------
  176. #[tokio::test]
  177. async fn full_round_trip() {
  178. let ledger = setup_ledger().await;
  179. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  180. pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  181. withdraw(&ledger, account(2), usd(), Cent::from(60), external()).await;
  182. withdraw(&ledger, account(1), usd(), Cent::from(40), external()).await;
  183. assert_eq!(
  184. ledger.balance(&account(1), &usd()).await.unwrap(),
  185. Cent::ZERO
  186. );
  187. assert_eq!(
  188. ledger.balance(&account(2), &usd()).await.unwrap(),
  189. Cent::ZERO
  190. );
  191. assert_eq!(
  192. ledger.balance(&external(), &usd()).await.unwrap(),
  193. Cent::ZERO
  194. );
  195. }
  196. // ---------------------------------------------------------------------------
  197. // Idempotency -- committing same envelope twice returns same receipt
  198. // ---------------------------------------------------------------------------
  199. #[tokio::test]
  200. async fn idempotent_commit() {
  201. let ledger = setup_ledger().await;
  202. let envelope = EnvelopeBuilder::new()
  203. .creates(vec![
  204. NewPosting {
  205. owner: account(1),
  206. asset: usd(),
  207. value: Cent::from(100),
  208. payer: None,
  209. },
  210. NewPosting {
  211. owner: external(),
  212. asset: usd(),
  213. value: Cent::from(-100),
  214. payer: None,
  215. },
  216. ])
  217. .build();
  218. let r1 = ledger.commit_atomic(envelope.clone()).await.unwrap();
  219. let r2 = ledger.commit_atomic(envelope).await.unwrap();
  220. assert_eq!(r1.transfer_id, r2.transfer_id);
  221. // Balance should only be 100, not 200 (second commit was a no-op)
  222. assert_eq!(
  223. ledger.balance(&account(1), &usd()).await.unwrap(),
  224. Cent::from(100)
  225. );
  226. }
  227. // ---------------------------------------------------------------------------
  228. // Overdraft prevention
  229. // ---------------------------------------------------------------------------
  230. #[tokio::test]
  231. async fn overdraft_rejected() {
  232. let ledger = setup_ledger().await;
  233. deposit(&ledger, account(1), usd(), Cent::from(50), external()).await;
  234. let transfer = TransferBuilder::new()
  235. .pay(account(1), account(2), usd(), Cent::from(100))
  236. .build();
  237. let result = ledger.commit(transfer).await;
  238. assert!(result.is_err());
  239. // Balance unchanged
  240. assert_eq!(
  241. ledger.balance(&account(1), &usd()).await.unwrap(),
  242. Cent::from(50)
  243. );
  244. }
  245. // ---------------------------------------------------------------------------
  246. // Reverse: forward compensating transfer
  247. // ---------------------------------------------------------------------------
  248. #[tokio::test]
  249. async fn reverse_restores_balances() {
  250. let ledger = setup_ledger().await;
  251. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  252. let pay_receipt = pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  253. assert_eq!(
  254. ledger.balance(&account(1), &usd()).await.unwrap(),
  255. Cent::from(40)
  256. );
  257. assert_eq!(
  258. ledger.balance(&account(2), &usd()).await.unwrap(),
  259. Cent::from(60)
  260. );
  261. // Reverse the payment
  262. ledger.reverse(&pay_receipt.transfer_id).await.unwrap();
  263. assert_eq!(
  264. ledger.balance(&account(1), &usd()).await.unwrap(),
  265. Cent::from(100)
  266. );
  267. assert_eq!(
  268. ledger.balance(&account(2), &usd()).await.unwrap(),
  269. Cent::ZERO
  270. );
  271. }
  272. // ---------------------------------------------------------------------------
  273. // Frozen account blocks transfers
  274. // ---------------------------------------------------------------------------
  275. #[tokio::test]
  276. async fn frozen_account_rejected() {
  277. let store = InMemoryStore::new();
  278. let ledger = Arc::new(Ledger::new(store));
  279. let mut frozen = make_account(1, AccountPolicy::NoOverdraft);
  280. frozen.flags = AccountFlags::FROZEN;
  281. ledger.store().create_account(frozen).await.unwrap();
  282. ledger
  283. .store()
  284. .create_account(make_account(99, AccountPolicy::ExternalAccount))
  285. .await
  286. .unwrap();
  287. let transfer = TransferBuilder::new()
  288. .deposit(account(1), usd(), Cent::from(100), external())
  289. .unwrap()
  290. .build();
  291. let result = ledger.commit(transfer).await;
  292. assert!(result.is_err());
  293. }
  294. // ---------------------------------------------------------------------------
  295. // Multi-asset: each asset conserves independently
  296. // ---------------------------------------------------------------------------
  297. #[tokio::test]
  298. async fn multi_asset_independent_balances() {
  299. let ledger = setup_ledger().await;
  300. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  301. deposit(&ledger, account(1), eur(), Cent::from(200), external()).await;
  302. assert_eq!(
  303. ledger.balance(&account(1), &usd()).await.unwrap(),
  304. Cent::from(100)
  305. );
  306. assert_eq!(
  307. ledger.balance(&account(1), &eur()).await.unwrap(),
  308. Cent::from(200)
  309. );
  310. pay(&ledger, account(1), account(2), usd(), Cent::from(30)).await;
  311. assert_eq!(
  312. ledger.balance(&account(1), &usd()).await.unwrap(),
  313. Cent::from(70)
  314. );
  315. assert_eq!(
  316. ledger.balance(&account(1), &eur()).await.unwrap(),
  317. Cent::from(200)
  318. );
  319. assert_eq!(
  320. ledger.balance(&account(2), &usd()).await.unwrap(),
  321. Cent::from(30)
  322. );
  323. }
  324. // ---------------------------------------------------------------------------
  325. // §4.4 FX trade via market account
  326. // ---------------------------------------------------------------------------
  327. #[tokio::test]
  328. async fn fx_trade_via_market_account() {
  329. let store = InMemoryStore::new();
  330. let ledger = Arc::new(Ledger::new(store));
  331. // Setup accounts
  332. for (id, policy) in [
  333. (1, AccountPolicy::NoOverdraft),
  334. (50, AccountPolicy::SystemAccount), // FX market account
  335. (99, AccountPolicy::ExternalAccount),
  336. ] {
  337. ledger
  338. .store()
  339. .create_account(make_account(id, policy))
  340. .await
  341. .unwrap();
  342. }
  343. // Seed: account1 has 100 USD, fx has 92 EUR
  344. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  345. deposit(&ledger, account(50), eur(), Cent::from(92), external()).await;
  346. // FX trade: account1 sells 100 USD, buys 92 EUR
  347. // Build the atomic envelope manually since it spans two assets
  348. let a1_usd_postings = ledger
  349. .store()
  350. .get_postings_by_account(&account(1), Some(&usd()), Some(PostingStatus::Active))
  351. .await
  352. .unwrap();
  353. let fx_eur_postings = ledger
  354. .store()
  355. .get_postings_by_account(&account(50), Some(&eur()), Some(PostingStatus::Active))
  356. .await
  357. .unwrap();
  358. let envelope = EnvelopeBuilder::new()
  359. .consumes(vec![a1_usd_postings[0].id, fx_eur_postings[0].id])
  360. .creates(vec![
  361. NewPosting {
  362. owner: account(50),
  363. asset: usd(),
  364. value: Cent::from(100),
  365. payer: Some(account(1)),
  366. },
  367. NewPosting {
  368. owner: account(1),
  369. asset: eur(),
  370. value: Cent::from(92),
  371. payer: Some(account(50)),
  372. },
  373. ])
  374. .build();
  375. ledger.commit_atomic(envelope).await.unwrap();
  376. // Verify
  377. assert_eq!(
  378. ledger.balance(&account(1), &usd()).await.unwrap(),
  379. Cent::ZERO
  380. );
  381. assert_eq!(
  382. ledger.balance(&account(1), &eur()).await.unwrap(),
  383. Cent::from(92)
  384. );
  385. assert_eq!(
  386. ledger.balance(&account(50), &usd()).await.unwrap(),
  387. Cent::from(100)
  388. );
  389. assert_eq!(
  390. ledger.balance(&account(50), &eur()).await.unwrap(),
  391. Cent::ZERO
  392. );
  393. }
  394. // ---------------------------------------------------------------------------
  395. // Account lifecycle: freeze / unfreeze / close
  396. // ---------------------------------------------------------------------------
  397. #[tokio::test]
  398. async fn freeze_blocks_transfers() {
  399. let ledger = setup_ledger().await;
  400. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  401. ledger.freeze(&account(1)).await.unwrap();
  402. // Paying from a frozen account should fail
  403. let transfer = TransferBuilder::new()
  404. .pay(account(1), account(2), usd(), Cent::from(50))
  405. .build();
  406. let result = ledger.commit(transfer).await;
  407. assert!(result.is_err());
  408. // Balance unchanged
  409. assert_eq!(
  410. ledger.balance(&account(1), &usd()).await.unwrap(),
  411. Cent::from(100)
  412. );
  413. }
  414. #[tokio::test]
  415. async fn unfreeze_re_enables_transfers() {
  416. let ledger = setup_ledger().await;
  417. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  418. ledger.freeze(&account(1)).await.unwrap();
  419. ledger.unfreeze(&account(1)).await.unwrap();
  420. // Should work again
  421. pay(&ledger, account(1), account(2), usd(), Cent::from(50)).await;
  422. assert_eq!(
  423. ledger.balance(&account(1), &usd()).await.unwrap(),
  424. Cent::from(50)
  425. );
  426. }
  427. #[tokio::test]
  428. async fn close_account_with_zero_balance() {
  429. let ledger = setup_ledger().await;
  430. // Account 3 has never transacted -- zero balance, no postings
  431. ledger.close(&account(3)).await.unwrap();
  432. // Closed account rejects deposits
  433. let transfer = TransferBuilder::new()
  434. .deposit(account(3), usd(), Cent::from(100), external())
  435. .unwrap()
  436. .build();
  437. let result = ledger.commit(transfer).await;
  438. assert!(result.is_err());
  439. }
  440. #[tokio::test]
  441. async fn close_account_with_balance_rejected() {
  442. let ledger = setup_ledger().await;
  443. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  444. // Should fail -- account still has active postings
  445. let result = ledger.close(&account(1)).await;
  446. assert!(result.is_err());
  447. // Balance unchanged
  448. assert_eq!(
  449. ledger.balance(&account(1), &usd()).await.unwrap(),
  450. Cent::from(100)
  451. );
  452. }
  453. #[tokio::test]
  454. async fn freeze_closed_account_rejected() {
  455. let ledger = setup_ledger().await;
  456. ledger.close(&account(3)).await.unwrap();
  457. let result = ledger.freeze(&account(3)).await;
  458. assert!(result.is_err());
  459. }
  460. // ---------------------------------------------------------------------------
  461. // Query layer: history, postings, list_accounts, get_account
  462. // ---------------------------------------------------------------------------
  463. #[tokio::test]
  464. async fn history_returns_transfers_for_account() {
  465. let ledger = setup_ledger().await;
  466. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  467. pay(&ledger, account(1), account(2), usd(), Cent::from(40)).await;
  468. deposit(&ledger, account(2), usd(), Cent::from(50), external()).await;
  469. let h1 = ledger.history(&account(1)).await.unwrap();
  470. // account(1) was in the deposit and the pay
  471. assert_eq!(h1.len(), 2);
  472. let h2 = ledger.history(&account(2)).await.unwrap();
  473. // account(2) was in the pay and a second deposit
  474. assert_eq!(h2.len(), 2);
  475. }
  476. #[tokio::test]
  477. async fn postings_returns_all_postings() {
  478. let ledger = setup_ledger().await;
  479. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  480. pay(&ledger, account(1), account(2), usd(), Cent::from(60)).await;
  481. let posts = ledger.postings(&account(1)).await.unwrap();
  482. // Original 100 posting (now consumed) + 40 change posting (active)
  483. assert_eq!(posts.len(), 2);
  484. let active: Vec<_> = posts.iter().filter(|p| p.is_active()).collect();
  485. assert_eq!(active.len(), 1);
  486. assert_eq!(active[0].value, Cent::from(40));
  487. }
  488. #[tokio::test]
  489. async fn list_accounts_returns_all() {
  490. let ledger = setup_ledger().await;
  491. let accounts = ledger.list_accounts().await.unwrap();
  492. // setup_ledger creates accounts 1, 2, 3, 99
  493. assert_eq!(accounts.len(), 4);
  494. }
  495. #[tokio::test]
  496. async fn get_account_by_id() {
  497. let ledger = setup_ledger().await;
  498. let acc = ledger.get_account(&account(1)).await.unwrap();
  499. assert_eq!(acc.id, account(1));
  500. assert_eq!(acc.policy, AccountPolicy::NoOverdraft);
  501. }
  502. #[tokio::test]
  503. async fn get_account_not_found() {
  504. let ledger = setup_ledger().await;
  505. let result = ledger.get_account(&account(999)).await;
  506. assert!(result.is_err());
  507. }
  508. // ---------------------------------------------------------------------------
  509. // Append-only accounts: version history, version conflict, account_versions
  510. // ---------------------------------------------------------------------------
  511. #[tokio::test]
  512. async fn account_history_tracks_versions() {
  513. let ledger = setup_ledger().await;
  514. // Version 1: created
  515. let history = ledger.account_history(&account(1)).await.unwrap();
  516. assert_eq!(history.len(), 1);
  517. assert_eq!(history[0].version, 1);
  518. // Version 2: frozen
  519. ledger.freeze(&account(1)).await.unwrap();
  520. let history = ledger.account_history(&account(1)).await.unwrap();
  521. assert_eq!(history.len(), 2);
  522. assert_eq!(history[1].version, 2);
  523. assert!(history[1].is_frozen());
  524. // Version 3: unfrozen
  525. ledger.unfreeze(&account(1)).await.unwrap();
  526. let history = ledger.account_history(&account(1)).await.unwrap();
  527. assert_eq!(history.len(), 3);
  528. assert_eq!(history[2].version, 3);
  529. assert!(!history[2].is_frozen());
  530. }
  531. #[tokio::test]
  532. async fn store_never_compacts() {
  533. let ledger = setup_ledger().await;
  534. // Freeze and unfreeze multiple times
  535. for _ in 0..5 {
  536. ledger.freeze(&account(1)).await.unwrap();
  537. ledger.unfreeze(&account(1)).await.unwrap();
  538. }
  539. // All 11 versions preserved (1 creation + 10 mutations)
  540. let history = ledger.account_history(&account(1)).await.unwrap();
  541. assert_eq!(history.len(), 11);
  542. // Versions are monotonically increasing
  543. for (i, acc) in history.iter().enumerate() {
  544. assert_eq!(acc.version, (i + 1) as u64);
  545. }
  546. }
  547. #[tokio::test]
  548. async fn transfer_records_account_snapshots() {
  549. let ledger = setup_ledger().await;
  550. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  551. // The envelope should have account_snapshots populated by the resolve step
  552. let transfers = ledger.history(&account(1)).await.unwrap();
  553. assert_eq!(transfers.len(), 1);
  554. assert!(!transfers[0].envelope.account_snapshots().is_empty());
  555. }
  556. #[tokio::test]
  557. async fn stale_snapshot_rejected() {
  558. let ledger = setup_ledger().await;
  559. // Get current snapshot for account(1)
  560. let acc1 = ledger.get_account(&account(1)).await.unwrap();
  561. let stale_snapshot = kuatia_core::account_snapshot_id(&acc1);
  562. // Freeze account(1) -- changes its snapshot hash
  563. ledger.freeze(&account(1)).await.unwrap();
  564. // Build an envelope with the stale snapshot
  565. let envelope = EnvelopeBuilder::new()
  566. .creates(vec![
  567. NewPosting {
  568. owner: account(1),
  569. asset: usd(),
  570. value: Cent::from(100),
  571. payer: None,
  572. },
  573. NewPosting {
  574. owner: external(),
  575. asset: usd(),
  576. value: Cent::from(-100),
  577. payer: None,
  578. },
  579. ])
  580. .account_snapshots(vec![stale_snapshot])
  581. .build();
  582. let result = ledger.commit_atomic(envelope).await;
  583. assert!(result.is_err());
  584. }
  585. #[tokio::test]
  586. async fn account_hash_deterministic() {
  587. let acc = make_account(42, AccountPolicy::NoOverdraft);
  588. let h1 = kuatia_core::account_hash(&acc);
  589. let h2 = kuatia_core::account_hash(&acc);
  590. assert_eq!(h1, h2);
  591. }
  592. #[tokio::test]
  593. async fn account_hash_changes_with_version() {
  594. let mut acc = make_account(42, AccountPolicy::NoOverdraft);
  595. let h1 = kuatia_core::account_hash(&acc);
  596. acc.version = 2;
  597. acc.flags |= AccountFlags::FROZEN;
  598. let h2 = kuatia_core::account_hash(&acc);
  599. assert_ne!(h1, h2);
  600. }
  601. // ---------------------------------------------------------------------------
  602. // Overdraft via negative postings
  603. // ---------------------------------------------------------------------------
  604. #[tokio::test]
  605. async fn capped_overdraft_creates_negative_posting() {
  606. let store = InMemoryStore::new();
  607. let ledger = Arc::new(Ledger::new(store));
  608. for (id, policy) in [
  609. (10, AccountPolicy::CappedOverdraft { floor: Cent::from(-200) }),
  610. (2, AccountPolicy::NoOverdraft),
  611. (99, AccountPolicy::ExternalAccount),
  612. ] {
  613. ledger.store().create_account(make_account(id, policy)).await.unwrap();
  614. }
  615. // Fund account 10 with 50, then pay 100 — overdraft covers the 50 shortfall.
  616. deposit(&ledger, account(10), usd(), Cent::from(50), external()).await;
  617. pay(&ledger, account(10), account(2), usd(), Cent::from(100)).await;
  618. assert_eq!(ledger.balance(&account(10), &usd()).await.unwrap(), Cent::from(-50));
  619. assert_eq!(ledger.balance(&account(2), &usd()).await.unwrap(), Cent::from(100));
  620. // A negative posting now backs the overdraft.
  621. let postings = ledger
  622. .store()
  623. .get_postings_by_account(&account(10), Some(&usd()), Some(PostingStatus::Active))
  624. .await
  625. .unwrap();
  626. assert!(postings.iter().any(|p| p.value == Cent::from(-50)));
  627. }
  628. #[tokio::test]
  629. async fn capped_overdraft_respects_floor() {
  630. let store = InMemoryStore::new();
  631. let ledger = Arc::new(Ledger::new(store));
  632. for (id, policy) in [
  633. (10, AccountPolicy::CappedOverdraft { floor: Cent::from(-80) }),
  634. (2, AccountPolicy::NoOverdraft),
  635. (99, AccountPolicy::ExternalAccount),
  636. ] {
  637. ledger.store().create_account(make_account(id, policy)).await.unwrap();
  638. }
  639. // Paying 100 from an empty account would project to -100, below the -80 floor.
  640. let transfer = TransferBuilder::new()
  641. .pay(account(10), account(2), usd(), Cent::from(100))
  642. .build();
  643. assert!(ledger.commit(transfer).await.is_err());
  644. assert_eq!(ledger.balance(&account(10), &usd()).await.unwrap(), Cent::ZERO);
  645. }
  646. #[tokio::test]
  647. async fn uncapped_overdraft_allows_arbitrary_negative() {
  648. let store = InMemoryStore::new();
  649. let ledger = Arc::new(Ledger::new(store));
  650. for (id, policy) in [
  651. (10, AccountPolicy::UncappedOverdraft),
  652. (2, AccountPolicy::NoOverdraft),
  653. (99, AccountPolicy::ExternalAccount),
  654. ] {
  655. ledger.store().create_account(make_account(id, policy)).await.unwrap();
  656. }
  657. pay(&ledger, account(10), account(2), usd(), Cent::from(1_000_000)).await;
  658. assert_eq!(
  659. ledger.balance(&account(10), &usd()).await.unwrap(),
  660. Cent::from(-1_000_000)
  661. );
  662. }
  663. // ---------------------------------------------------------------------------
  664. // Book policy enforcement
  665. // ---------------------------------------------------------------------------
  666. #[tokio::test]
  667. async fn book_policy_rejects_disallowed_asset() {
  668. let ledger = setup_ledger().await;
  669. // Book 5 permits only EUR.
  670. let book = BookBuilder::new("eur-only")
  671. .id(BookId::new(5))
  672. .allow_asset(eur())
  673. .build();
  674. ledger.store().create_book(book).await.unwrap();
  675. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  676. // Paying USD under a EUR-only book is rejected, balance unchanged.
  677. let transfer = TransferBuilder::new()
  678. .book(BookId::new(5))
  679. .pay(account(1), account(2), usd(), Cent::from(50))
  680. .build();
  681. assert!(ledger.commit(transfer).await.is_err());
  682. assert_eq!(ledger.balance(&account(1), &usd()).await.unwrap(), Cent::from(100));
  683. }
  684. #[tokio::test]
  685. async fn transfer_in_missing_named_book_is_rejected() {
  686. let ledger = setup_ledger().await;
  687. deposit(&ledger, account(1), usd(), Cent::from(100), external()).await;
  688. let transfer = TransferBuilder::new()
  689. .book(BookId::new(404))
  690. .pay(account(1), account(2), usd(), Cent::from(50))
  691. .build();
  692. assert!(ledger.commit(transfer).await.is_err());
  693. assert_eq!(ledger.balance(&account(1), &usd()).await.unwrap(), Cent::from(100));
  694. }
  695. // ---------------------------------------------------------------------------
  696. // Content-addressed determinism
  697. // ---------------------------------------------------------------------------
  698. #[tokio::test]
  699. async fn identical_transfers_share_envelope_id() {
  700. // Two independently-built default-book transfers must hash identically.
  701. let a = TransferBuilder::new()
  702. .pay(account(1), account(2), usd(), Cent::from(10))
  703. .build();
  704. let b = TransferBuilder::new()
  705. .pay(account(1), account(2), usd(), Cent::from(10))
  706. .build();
  707. assert_eq!(a.book, b.book, "default book must be deterministic");
  708. assert_eq!(a.book, DEFAULT_BOOK);
  709. }