DEV Community

Cover image for 15 Automated Tests That Verify Vault Ledger Maths Can't Be Tricked by Edge Cases
DEVANSHU PATIL
DEVANSHU PATIL

Posted on

15 Automated Tests That Verify Vault Ledger Maths Can't Be Tricked by Edge Cases

15 Automated Tests That Verify Vault Ledger Maths Can't Be Tricked by Edge Cases

When software engineers build an isolated security sandbox, our natural instinct is to test the locks:

  • Did the biometric prompt fire?
  • Did the 5-minute auto-lock timer trigger?
  • Did the Haversine formula reject coordinates outside the geofence?

These security checks are vital. But once the door opens, the software must do its real job: accounting.

If a user deposits Rs. 20,000 in cash into their secret vault, swipes Rs. 5,000 on an emergency vault credit card, locks Rs. 50,000 in an emergency Fixed Deposit, and transfers Rs. 10,000 from vault checking to pay down the card, the math inside that vault must be unshakeable.

Because FinLedger's Secret Vault lives in a completely isolated SQLite schema to prevent data leaks, it cannot rely on the public ledger's queries. It runs on its own dedicated mathematical kernel: VaultLedgerEngine.kt.

To ensure that secret balances are just as mathematically sound as public ledgers, FinLedger maintains a suite of 15 automated domain test suites in VaultLedgerEngineTest.kt.

Here is a walkthrough of the automated test harness that guarantees mathematical truth inside our isolated vault.

The 15 Test Suites at a Glance

flowchart TD
    subgraph VaultTestSuite["VaultLedgerEngineTest.kt (15 Test Suites)"]
        direction TB

        S1["Suites 1 - 3: Liquid Asset Aggregation
        Cash, Bank, and UPI posting rules;
        Opening balance carried-forward verification."]

        S2["Suites 4 - 6: Revolving Liabilities
        Credit card swipe liability expansion;
        Credit limit utilization; Interest accrual math."]

        S3["Suites 7 - 9: Illiquid Term Assets
        Fixed deposit principal isolation;
        Spendable liquidity vs total net worth separation."]

        S4["Suites 10 - 12: Internal Vault Transfers
        Cross-account debt settlement;
        Zero phantom income; Double-entry parity."]

        S5["Suites 13 - 15: Cross-Ledger Bridges & Precision
        Public-to-vault transfer parity;
        Floating-point half-paisa boundary assertions."]

        S1 --> S2 --> S3 --> S4 --> S5
    end

Let's dissect three of the most complex mathematical scenarios verified by this test harness:

Scenario 1: Credit Card Swipes vs Available Liquidity (Suite 5)

Suppose you have the following accounts configured inside your Secret Vault:

  • Emergency Cash: Rs. 15,000.00
  • Emergency Checking Account: Rs. 35,000.00
  • Emergency Credit Card: Limit Rs. 1,00,000.00 (Current Outstanding: Rs. 0.00)

Now you swipe the emergency credit card for an unexpected Rs. 10,000 car repair.

What should the vault report?

In VaultLedgerEngineTest.kt:testCreditCardLiabilityPosting, the test suite asserts against three distinct metrics:

  1. Liquid Spendable Reserves: Remained exactly Rs. 50,000.00 (15k cash + 35k bank). Swiping a credit card did not consume a single rupee of liquid cash!
  2. Vault Liabilities: Increased from Rs. 0.00 to Rs. 10,000.00.
  3. Vault Net Worth: Evaluates to: $$\text{Net Worth} = \text{Total Assets} - \text{Total Liabilities}$$ $$\text{Net Worth} = 50,000.00 - 10,000.00 = \text{Rs. 40,000.00}$$

The test proves that your net worth correctly decreased by Rs. 10,000 to reflect the new debt obligation, while your liquid cash was correctly preserved.

Scenario 2: Internal Debt Settlement Transfers (Suite 11)

Next, you transfer Rs. 10,000 from your vault checking account to pay off the vault credit card bill.

In VaultLedgerEngineTest.kt:testInternalVaultTransferSettlement, the test asserts the double-entry transfer legs:

  • Vault Checking Balance: Decreases by Rs. 10,000 (35,000 -> 25,000).
  • Vault Credit Card Outstanding: Decreases by Rs. 10,000 (10,000 -> 0.00).
  • Vault Operational Expenses: Asserts Rs. 0.00! Paying a credit card bill is a transfer between owned accounts; it must not be logged as a living expense.
  • Vault Net Worth: Asserts Rs. 40,000.00 (UNMODIFIED!). Paying off debt simply converts liquid cash into debt clearance without altering overall net worth.

Scenario 3: Floating-Point Parity Across 10,000 Cycles (Suite 15)

In binary IEEE-754 floating-point math, recurring fractional additions across multi-currency ledgers accumulate micro-precision drift:
100.10 + 200.20 + 300.30 can result in 600.6000000000001.

Suite 15 runs a high-throughput stress simulation:

  • Simulates 10,000 randomized transactions, transfers, and settlements across all seven vault account types.
  • Asserts that at the end of 10,000 operations, the sum of all individual account balances matches the computed vault net worth to within 0.001 rupees (less than a tenth of a paisa).

Zero floating-point leakage. Zero balance drift.

Key Takeaway

A feature designed for emergencies must be the most reliable code in your entire repository.

When users access their secret vault during an urgent crisis, they cannot afford software glitches, missing transactions, or incorrect balances. By subjecting VaultLedgerEngine.kt to 15 rigorous, automated test suites verifying multi-asset liquidity, liability tracking, and cross-account transfers, FinLedger guarantees that your emergency reserves remain mathematically unshakeable.


FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.

Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The Scenario 2 assertion is the one I'd steal for any finance app, Devanshu — net worth UNMODIFIED at Rs. 40,000 after settling a bill is exactly the invariant most apps get wrong, because they book the payment as an expense and the user's net worth quietly drops twice. Testing it at the engine level instead of the UI is the right call; UI tests would never catch a double-entry leg being wrong.

One edge case I'd want in the suite: a partial settlement — paying Rs. 4,000 of a Rs. 10,000 bill — where the remaining liability, the checking balance and the net worth all have to move by exactly the same amount in opposite directions. That's where rounding usually shows up first. Did the 10,000-cycle stress run include partial settlements, or only full ones?