Simpl-Resultat/src/utils/incomeStatement.test.ts
le king fu 96dfd4f1f8 feat: one sign convention for the reports, in a pure module (#392)
src/utils/incomeStatement.ts holds the rule every report will follow
(ADR 0026): the SQL section of a row (SECTION_SQL on CATEGORY_JOIN_SQL, a row
without an existing category being uncategorized), and the pure functions
that turn signed category balances into what the screens show: orient (an
expense shows its spending, the opposite of its balance), incomeStatement and
statementOf (income, expenses, uncategorized, transfers, result before
transfers, net), addStatements, savingsRate ((income - expenses) / income)
and toCents (no float remainder, no negative zero).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 22:32:11 -05:00

132 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, it, expect } from "vitest";
import {
SECTION_ORDER,
SECTION_SQL,
addStatements,
incomeStatement,
orient,
savingsRate,
statementOf,
toCents,
toSection,
} from "./incomeStatement";
describe("toSection", () => {
it("keeps the three category types and makes anything else uncategorized", () => {
expect(toSection("income")).toBe("income");
expect(toSection("expense")).toBe("expense");
expect(toSection("transfer")).toBe("transfer");
expect(toSection("uncategorized")).toBe("uncategorized");
expect(toSection(null)).toBe("uncategorized");
expect(toSection(undefined)).toBe("uncategorized");
});
it("reads the section the SQL fragment gives, the uncategorized one included", () => {
expect(SECTION_SQL).toBe("CASE WHEN c.id IS NULL THEN 'uncategorized' ELSE c.type END");
});
});
describe("toCents", () => {
it("drops the floating remainder of a refund that cancels two purchases, without a negative zero", () => {
const remainder = 10.1 + 20.2 - 30.3;
expect(remainder).not.toBe(0);
expect(Object.is(toCents(remainder), 0)).toBe(true);
expect(Object.is(toCents(-0), 0)).toBe(true);
});
it("keeps the cents", () => {
expect(toCents(12.34)).toBe(12.34);
expect(toCents(-0.1 - 0.2)).toBe(-0.3);
});
});
describe("orient", () => {
it("shows an expense category's spending: the opposite of its balance", () => {
expect(orient("expense", -80)).toBe(80);
// Refunds above purchases: a negative expense (D1).
expect(orient("expense", 50)).toBe(-50);
});
it("shows every other section's balance as it is", () => {
expect(orient("income", 1000)).toBe(1000);
expect(orient("transfer", -300)).toBe(-300);
expect(orient("uncategorized", 40)).toBe(40);
});
it("never shows a negative zero", () => {
expect(Object.is(orient("expense", 0), 0)).toBe(true);
});
});
describe("incomeStatement", () => {
it("adds the signed balances by section, a split and a refund included (reproduction of #392)", () => {
// Groceries: −100, its +30 offset line and a +20 refund; −30 split to Pharmacy.
const r = incomeStatement([
{ section: "income", balance: 1000 },
{ section: "expense", balance: -100 + 30 + 20 },
{ section: "expense", balance: -30 },
{ section: "transfer", balance: -500 + 500 },
]);
expect(r).toEqual({
income: 1000,
expenses: 80,
uncategorized: 0,
transfers: 0,
resultBeforeTransfers: 920,
net: 920,
});
});
it("keeps the uncategorized line and a one-leg transfer apart, both in the net (edge cases of #392)", () => {
const r = incomeStatement([
{ section: "income", balance: 1000 },
{ section: "expense", balance: -80 },
{ section: "expense", balance: 50 },
{ section: "uncategorized", balance: 40 },
{ section: "transfer", balance: -300 },
]);
expect(r).toEqual({
income: 1000,
expenses: 30,
uncategorized: 40,
transfers: -300,
resultBeforeTransfers: 1010,
net: 710,
});
});
it("is empty without rows, and reads a null balance as zero", () => {
expect(incomeStatement([])).toEqual(statementOf(0, 0, 0, 0));
expect(incomeStatement([{ section: "income", balance: null }]).income).toBe(0);
});
});
describe("addStatements", () => {
it("adds two periods, the result lines recomputed", () => {
const sum = addStatements(statementOf(1000, 80, 0, 0), statementOf(500, 30, 40, -300));
expect(sum).toEqual(statementOf(1500, 110, 40, -300));
// 1500 − 110 + 40 − 300
expect(sum.net).toBe(1130);
});
});
describe("savingsRate", () => {
it("is (income − expenses) ÷ income, without transfers nor uncategorized amounts (D6)", () => {
expect(savingsRate(1000, 80)).toBe(92);
expect(savingsRate(1000, 30)).toBe(97);
});
it("is null when income is not positive", () => {
expect(savingsRate(0, 80)).toBeNull();
expect(savingsRate(-10, 80)).toBeNull();
});
});
describe("SECTION_ORDER", () => {
it("reads income, expenses, the uncategorized line, then transfers", () => {
const order = Object.entries(SECTION_ORDER)
.sort((a, b) => a[1] - b[1])
.map(([section]) => section);
expect(order).toEqual(["income", "expense", "uncategorized", "transfer"]);
});
});