From 03c5f2538ff2dd55392213a013ac58b60586fe93 Mon Sep 17 00:00:00 2001 From: medic-bot Date: Wed, 11 Mar 2026 13:02:47 -0400 Subject: [PATCH] refactor: inline buildPrevYearTotalMap and remove disproportionate tests (#39) The 3-line helper was exported solely for testing. Inlining it removes the export-for-test pattern and eliminates 50 lines of tests that were disproportionate for a trivial filter-and-set loop. Co-Authored-By: Claude Opus 4.6 --- src/hooks/useBudget.test.ts | 50 ------------------------------------- src/hooks/useBudget.ts | 21 ++++------------ 2 files changed, 5 insertions(+), 66 deletions(-) delete mode 100644 src/hooks/useBudget.test.ts diff --git a/src/hooks/useBudget.test.ts b/src/hooks/useBudget.test.ts deleted file mode 100644 index 220b684..0000000 --- a/src/hooks/useBudget.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { describe, it, expect } from "vitest"; -import { buildPrevYearTotalMap } from "./useBudget"; - -/** - * Unit tests for the previous-year actuals normalization logic used in useBudget. - * - * Transaction amounts in the database use signed values (expenses are negative, - * income is positive). The buildPrevYearTotalMap function preserves these signs - * as-is, because the budget display layer does NOT apply a sign multiplier to - * previous year actuals (unlike planned budget amounts). - */ - -describe("buildPrevYearTotalMap", () => { - it("should preserve negative sign for expense actuals", () => { - const actuals = [{ category_id: 1, actual: -500 }]; // expense: negative in DB - const map = buildPrevYearTotalMap(actuals); - expect(map.get(1)).toBe(-500); - }); - - it("should preserve positive sign for income actuals", () => { - const actuals = [{ category_id: 2, actual: 3000 }]; // income: positive in DB - const map = buildPrevYearTotalMap(actuals); - expect(map.get(2)).toBe(3000); - }); - - it("should skip null category_id entries", () => { - const actuals = [{ category_id: null, actual: -100 }]; - const map = buildPrevYearTotalMap(actuals); - expect(map.size).toBe(0); - }); - - it("should handle zero actuals", () => { - const actuals = [{ category_id: 3, actual: 0 }]; - const map = buildPrevYearTotalMap(actuals); - expect(map.get(3)).toBe(0); - }); - - it("should handle multiple categories", () => { - const actuals = [ - { category_id: 1, actual: -200 }, - { category_id: 2, actual: 1500 }, - { category_id: 3, actual: -75.5 }, - ]; - const map = buildPrevYearTotalMap(actuals); - expect(map.size).toBe(3); - expect(map.get(1)).toBe(-200); - expect(map.get(2)).toBe(1500); - expect(map.get(3)).toBe(-75.5); - }); -}); diff --git a/src/hooks/useBudget.ts b/src/hooks/useBudget.ts index 2f41eab..bdd0038 100644 --- a/src/hooks/useBudget.ts +++ b/src/hooks/useBudget.ts @@ -63,21 +63,6 @@ function reducer(state: BudgetState, action: BudgetAction): BudgetState { const TYPE_ORDER: Record = { expense: 0, income: 1, transfer: 2 }; -/** - * Build a map of category_id -> annual actual total from raw actuals. - * Transaction amounts are already signed (expenses negative, income positive), - * so they are stored as-is without normalization. - */ -export function buildPrevYearTotalMap( - actuals: Array<{ category_id: number | null; actual: number }> -): Map { - const prevYearTotalMap = new Map(); - for (const a of actuals) { - if (a.category_id != null) prevYearTotalMap.set(a.category_id, a.actual); - } - return prevYearTotalMap; -} - export function useBudget() { const [state, dispatch] = useReducer(reducer, undefined, initialState); const fetchIdRef = useRef(0); @@ -105,7 +90,11 @@ export function useBudget() { } // Build a map for previous year actuals: categoryId -> annual actual total - const prevYearTotalMap = buildPrevYearTotalMap(prevYearActuals); + // Amounts are already signed (expenses negative, income positive) — stored as-is. + const prevYearTotalMap = new Map(); + for (const a of prevYearActuals) { + if (a.category_id != null) prevYearTotalMap.set(a.category_id, a.actual); + } // Helper: build months array from entryMap const buildMonths = (catId: number) => {