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 <noreply@anthropic.com>
This commit is contained in:
parent
ecbcf44f86
commit
03c5f2538f
2 changed files with 5 additions and 66 deletions
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
@ -63,21 +63,6 @@ function reducer(state: BudgetState, action: BudgetAction): BudgetState {
|
|||
|
||||
const TYPE_ORDER: Record<string, number> = { 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<number, number> {
|
||||
const prevYearTotalMap = new Map<number, number>();
|
||||
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<number, number>();
|
||||
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) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue