Simpl-Resultat/src/utils/splitOffset.test.ts
le king fu e130c346c5
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 2m30s
test: pin the amount condition of isOffsetChild under a parent of zero (#454)
The mutation pass left one mutation green: without `child.amount !== 0`,
every existing case still failed on the sign test. Only under a parent
of zero does a child of zero pass it (Math.sign(0) === -Math.sign(0),
since 0 === -0); the test now covers that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 22:14:49 -04:00

66 lines
2.7 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 { isOffsetChild } from "./splitOffset";
import type { SplitChild } from "../shared/types";
// The reproduction of #454: −120 split as −50 to category B (the entry, with
// the parent's description, as the split window writes an entry left without
// one) and the offset line of +50, written in the parent's category A.
const A = 3;
const B = 4;
const C = 9;
const PARENT = { description: "EPICERIE METRO", amount: -120 };
/** A child as `getSplitChildren` returns it. */
const child = (amount: number, categoryId: number | null, description = PARENT.description): SplitChild => ({
id: 1,
category_id: categoryId,
category_name: null,
category_color: null,
amount,
description,
});
describe("isOffsetChild (#454)", () => {
it("recognizes the offset line: the parent's description and the opposite sign", () => {
expect(isOffsetChild(PARENT, child(50, A))).toBe(true);
});
// Decision D2: an offset line left in a former category still follows.
it("recognizes it whatever its category", () => {
for (const categoryId of [A, B, C, null]) {
expect(isOffsetChild(PARENT, child(50, categoryId)), `category ${categoryId}`).toBe(true);
}
});
it("refuses an entry, which has the parent's sign, even with the parent's description and category", () => {
expect(isOffsetChild(PARENT, child(-50, B))).toBe(false);
expect(isOffsetChild(PARENT, child(-50, A))).toBe(false);
});
it("refuses a child of the opposite sign that carries another description", () => {
expect(isOffsetChild(PARENT, child(50, A, "REMBOURSEMENT"))).toBe(false);
expect(isOffsetChild(PARENT, child(50, A, "epicerie metro"))).toBe(false);
});
it("refuses an amount of zero", () => {
expect(isOffsetChild(PARENT, child(0, A))).toBe(false);
expect(isOffsetChild(PARENT, child(-0, A))).toBe(false);
// Under a parent of zero, a child of zero passes the sign test
// (Math.sign(0) === -Math.sign(0), since 0 === -0): only the amount refuses it.
const zero = { description: PARENT.description, amount: 0 };
expect(isOffsetChild(zero, child(0, A))).toBe(false);
expect(isOffsetChild(zero, child(-0, A))).toBe(false);
});
it("recognizes the offset line of a split income: a positive parent, a negative offset", () => {
const income = { description: "SALAIRE", amount: 2000 };
expect(isOffsetChild(income, child(-500, A, "SALAIRE"))).toBe(true);
expect(isOffsetChild(income, child(500, B, "SALAIRE"))).toBe(false);
});
it("finds no offset line under a parent of zero", () => {
const zero = { description: "EPICERIE METRO", amount: 0 };
expect(isOffsetChild(zero, child(50, A))).toBe(false);
expect(isOffsetChild(zero, child(-50, A))).toBe(false);
});
});