fix(folio): extract balanceMatchesAddedSum predicate as testable helper

Move the ledger-balance-vs-added-rows predicate into a pure helper module
so the property's logic is unit-testable in isolation. Marks the sanderling
example as an ES module so cross-package ESM imports resolve under node.
This commit is contained in:
pj committed 2026-05-31 15:49:45 +05:30
1 parent e9c6066ba4
commit 35fce05b65
2 files changed
+23

No files matched your search

+1
View File
@@ -1,6 +1,7 @@
{
"name": "folio",
"private": true,
"type": "module",
"description": "Minimal sanderling integration example.",
"devDependencies": {
"@sanderling/spec": "file:../../../pkg/spec"
+22
View File
@@ -0,0 +1,22 @@
export interface LedgerRow {
key: string;
signed: number;
}
// When new ledger rows appear, the balance delta must equal the SUM of the
// new rows' signed amounts. Single-row sums and multi-row sums BOTH must
// match the delta; a double-submit landing two rows whose total is twice
// the balance change trips the sum check.
export function balanceMatchesAddedSum(
previousRows: readonly LedgerRow[],
currentRows: readonly LedgerRow[],
previousBalance: number,
currentBalance: number,
): boolean {
const previousKeys = new Set(previousRows.map(row => row.key));
const added = currentRows.filter(row => !previousKeys.has(row.key));
if (added.length === 0) return true;
const delta = currentBalance - previousBalance;
const addedSum = added.reduce((sum, row) => sum + row.signed, 0);
return addedSum === delta;
}