feat(domain): loan amortization schedule
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1c7b1a53e4
commit
9fb4aab887
2 files changed
+228
No files matched your search
@@ -0,0 +1,95 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSchedule, loanSummary, monthlyPayment, remainingAt, type LoanInput } from "@/domain/loan";
|
||||
|
||||
const base: LoanInput = {
|
||||
principal: 2000000,
|
||||
annualRatePct: 4,
|
||||
durationMonths: 48,
|
||||
startDate: "2026-01-05",
|
||||
insuranceMonthly: 1500,
|
||||
paymentOverride: null,
|
||||
referenceDate: null,
|
||||
referenceRemainingCapital: null,
|
||||
};
|
||||
|
||||
describe("monthlyPayment", () => {
|
||||
it("uses the French constant-payment formula", () => {
|
||||
expect(monthlyPayment(2000000, 4, 48)).toBe(45158);
|
||||
});
|
||||
|
||||
it("divides evenly at zero rate", () => {
|
||||
expect(monthlyPayment(1200000, 0, 12)).toBe(100000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildSchedule", () => {
|
||||
const rows = buildSchedule(base);
|
||||
|
||||
it("produces one row per month with interest and principal", () => {
|
||||
expect(rows).toHaveLength(48);
|
||||
expect(rows[0]).toMatchObject({
|
||||
n: 1,
|
||||
date: "2026-01-05",
|
||||
payment: 45158,
|
||||
interest: 6667,
|
||||
principal: 38491,
|
||||
insurance: 1500,
|
||||
remaining: 1961509,
|
||||
beforeTracking: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("lets the last payment absorb the rounding residue", () => {
|
||||
const last = rows[rows.length - 1];
|
||||
expect(last.remaining).toBe(0);
|
||||
expect(last.principal).toBe(rows[rows.length - 2].remaining);
|
||||
expect(last.date).toBe("2029-12-05");
|
||||
expect(rows.reduce((t, r) => t + r.principal, 0)).toBe(2000000);
|
||||
});
|
||||
|
||||
it("clamps day 31 to the end of shorter months", () => {
|
||||
const r = buildSchedule({ ...base, startDate: "2026-01-31" });
|
||||
expect(r[1].date).toBe("2026-02-28");
|
||||
expect(r[2].date).toBe("2026-03-31");
|
||||
});
|
||||
|
||||
it("restarts from the bank's remaining capital and real payment in reference mode", () => {
|
||||
const r = buildSchedule({
|
||||
...base,
|
||||
referenceDate: "2026-06-10",
|
||||
referenceRemainingCapital: 1800000,
|
||||
paymentOverride: 46000,
|
||||
});
|
||||
const before = r.filter((x) => x.beforeTracking);
|
||||
expect(before.map((x) => x.date)).toEqual([
|
||||
"2026-01-05", "2026-02-05", "2026-03-05", "2026-04-05", "2026-05-05", "2026-06-05",
|
||||
]);
|
||||
const first = r.find((x) => !x.beforeTracking)!;
|
||||
expect(first).toMatchObject({ date: "2026-07-05", payment: 46000, interest: 6000, principal: 40000, remaining: 1760000 });
|
||||
const last = r[r.length - 1];
|
||||
expect(last.remaining).toBe(0);
|
||||
expect(last.payment).toBeLessThanOrEqual(46000);
|
||||
});
|
||||
|
||||
it("refuses a payment that does not cover the interest", () => {
|
||||
expect(() => buildSchedule({ ...base, referenceDate: "2026-06-10", referenceRemainingCapital: 1800000, paymentOverride: 5000 })).toThrow(
|
||||
"Mensualité insuffisante pour couvrir les intérêts",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remainingAt / loanSummary", () => {
|
||||
const rows = buildSchedule(base);
|
||||
|
||||
it("returns the capital after the last payment on or before the date", () => {
|
||||
expect(remainingAt(rows, "2026-01-04")).toBe(2000000);
|
||||
expect(remainingAt(rows, "2026-01-05")).toBe(1961509);
|
||||
});
|
||||
|
||||
it("summarises the loan at a date", () => {
|
||||
const s = loanSummary(rows, "2026-01-05");
|
||||
expect(s).toMatchObject({ payment: 45158, insurance: 1500, endDate: "2029-12-05", remaining: 1961509, interestPaid: 6667 });
|
||||
const totalInterest = rows.reduce((t, r) => t + r.interest, 0);
|
||||
expect(s.interestPaid + s.interestLeft).toBe(totalInterest);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user