Ledger reconciliation: reports and regression tests
Project milestone
Produce a reconciliation report containing differences by identity and the two totals. The signed difference is actual total minus expected total, but equal totals alone do not prove the ledgers agree.
Run the examples
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {reconcile}=require('./ledger.cjs');const r=reconcile([{id:'A',cents:100}],[{id:'B',cents:100}]);console.log(JSON.stringify([r.difference,r.missing,r.extra]));Expected console output:
[0,["A"],["B"]]Matching totals do not erase mismatching record IDs.
In projects/ledger-reconcile/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {reconcile}=require('./ledger.cjs');const r=reconcile([{id:'A',cents:100}],[{id:'A',cents:80}]);console.log(JSON.stringify([r.changed,r.difference]));Expected console output:
[["A"],-20]The same ID has a changed amount, and actual minus expected is negative.
Implementation and contract
reconcile() computes both totals with checkedAdd and returns missing, extra, changed, expectedTotal, actualTotal and difference. The returned arrays allow a reviewer to identify discrepancies even if totals cancel. All computation is in memory and side-effect free for the documented plain-record input. Run the regression suite before adapting this model to any persistent financial system.
Common mistakes
Do not treat a zero difference as proof of record equality or a report as authorization to alter real transactions.
Acceptance task
Add a test with equal totals but one missing and one extra ID. Verify that the report flags both.
Project source, setup and tests: projects/ledger-reconcile/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ledger reconciliation: reports and regression tests — Knowledge Check
Assess implementation decisions and project behavior.
What is the signed difference formula?
Why report discrepancies when totals match?
What does this lab not authorize?
In the Ledger reconciliation: reports and regression tests lab, what does this independent example log? const {reconcile}=require('./ledger.cjs'); const r=reconcile([{id:'A',cents:100}],[{id:'B',cents:100}]); console.log(JSON.stringify([r.difference,r.missing,r.extra]));
In the Ledger reconciliation: reports and regression tests lab, what does this independent example log? const {reconcile}=require('./ledger.cjs'); const r=reconcile([{id:'A',cents:100}],[{id:'A',cents:80}]); console.log(JSON.stringify([r.changed,r.difference]));