Skip to content

Add property tests for the billing fee engine's decimal money arithmetic in services/billing/feeEngine.ts#656

Open
barnabasolutayo-lgtm wants to merge 1 commit into
CredenceOrg:mainfrom
barnabasolutayo-lgtm:test/fee-engine-property-tests
Open

Add property tests for the billing fee engine's decimal money arithmetic in services/billing/feeEngine.ts#656
barnabasolutayo-lgtm wants to merge 1 commit into
CredenceOrg:mainfrom
barnabasolutayo-lgtm:test/fee-engine-property-tests

Conversation

@barnabasolutayo-lgtm

Copy link
Copy Markdown

##closes #630

Description

This PR introduces robust, deterministic property-based testing using fast-check to assert the algebraic invariants of the billing fee engine (src/services/billing/feeEngine.ts). By replacing simple example-based testing with randomized inputs spanning edge cases, we ensure mathematical correctness and prevent IEEE-754 precision drift or carry bugs in our financial logic.

Invariants Asserted

  1. applyFees Invariant (fee + net == gross): Asserts that baseAmount.amount + feeAmount.amount === totalAmount.amount across all inputs.
  2. addMoneyStrings Algebraic Properties (tested via public proxy calculateFee and decimalMath helpers):
    • Commutativity: add(a, b) == add(b, a)
    • Associativity: add(add(a, b), c) == add(a, add(b, c))
  3. Monotonicity: Asserts that calculateFee is monotonic non-decreasing in the amount for any fixed rate.
  4. Parsing & Error Handling:
    • Asserts that parseNonNegativeDecimal rejects negative numbers, NaN values, and malformed structures.
    • Asserts that valid decimal strings successfully round-trip under clean base-100 calculations.
  5. No Loss of Precision: Asserts that if the exact intermediate product base * rate / 100 is exactly representable within the target currency's configured scale, the returned fee matches it exactly.

Verification Results

Automated Test Runs

Tests were verified using vitest:

npx vitest run src/services/billing/feeEngine.property.test.ts src/services/billing/feeEngine.test.ts


Verifies fee+net==gross, add commutativity/associativity, and monotonicity across generated decimal inputs in services/billing/feeEngine.ts.

…rithmetic

Verifies fee+net==gross, add commutativity/associativity, and monotonicity across generated decimal inputs in services/billing/feeEngine.ts.
@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@barnabasolutayo-lgtm Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add property tests for the billing fee engine's decimal money arithmetic in services/billing/feeEngine.ts

1 participant