Files
kakeibo/frontend/src/lib/debt.test.ts
T

96 lines
2.8 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, expect, it } from 'vitest'
import {
debtSeries,
debtTotal,
installmentLoad,
monthEnd,
monthIndex,
monthName,
remainingAfter,
} from './debt'
// Kaynak belgedeki 8 taksit (paidCount Kasım 2026 itibarıyla → ilk ay = Kasım − paid + 1)
const nov = monthIndex('2026-11')!
const plans = (
[
[271_761, 7, 5],
[33_324, 9, 5],
[11_945, 7, 5],
[12_717, 7, 4],
[17_067, 7, 3],
[7_428, 7, 3],
[18_202, 7, 2],
[58_738, 3, 2],
] as const
).map(([monthly_amount, total_count, paid]) => ({
monthly_amount,
total_count,
first_month: monthName(nov - paid + 1),
}))
describe('debt (backend ile aynı, kabul rakamları)', () => {
it('totals', () => {
expect(
debtTotal({
kmh: 0,
positive_balance: 236_311,
card_balance: 6_671_590,
remaining_installments: 1_417_769,
}),
).toBe(7_853_048)
expect(
debtTotal({
kmh: 3_926_444,
positive_balance: 0,
card_balance: 2_994_854,
remaining_installments: 986_587,
}),
).toBe(7_907_885)
})
it('installment loads and remaining', () => {
const loads = ['2026-11', '2026-12', '2027-01', '2027-02', '2027-03', '2027-04'].map((m) =>
installmentLoad(plans, monthIndex(m)!),
)
expect(loads).toEqual([431_182, 431_182, 372_444, 88_738, 76_021, 18_202])
expect(remainingAfter(plans, monthIndex('2026-10')!)).toBe(1_417_769)
expect(remainingAfter(plans, nov)).toBe(986_587)
})
it('months', () => {
expect(monthName(monthIndex('2026-12')! + 1)).toBe('2027-01')
expect(monthIndex('2026-13')).toBeNull()
expect(monthEnd('2026-11')).toBe('2026-11-30')
expect(monthEnd('2028-02')).toBe('2028-02-29')
})
it('chart series: actual + target from the first snapshot', () => {
const s = debtSeries(
[
{ date: '2026-11-13', total: 7_907_885 },
{ date: '2026-10-31', total: 7_853_048 },
],
[{ month: '2026-11', closing_target: 6_699_500 }],
)
expect(s.map((p) => [p.date, p.actual, p.target])).toEqual([
['2026-10-31', 7_853_048, 7_853_048],
['2026-11-13', 7_907_885, undefined],
['2026-11-30', undefined, 6_699_500],
])
})
})
describe('plan integration', () => {
it('finds the installments expense item by name', async () => {
const { findInstallmentItem } = await import('./debt')
const items = [
{ kind: 'income', name: 'Taksitler' },
{ kind: 'expense', name: 'Kira' },
{ kind: 'expense', name: ' TAKSİTLER ' },
]
expect(findInstallmentItem(items)?.name).toBe(' TAKSİTLER ')
expect(findInstallmentItem([{ kind: 'expense', name: 'Taksit' }])).toBeTruthy()
expect(findInstallmentItem([{ kind: 'expense', name: 'Taksitli alışveriş' }])).toBeUndefined()
})
})