Files
accounted/extensions/general/enable-banking/lib/__tests__/date-suggestions.test.ts
T
Jakob WennbergandClaude Fable 5 11400b4dec fix(banking): base sync date suggestions on booked coverage and the real fiscal period (#956)
* fix(enable-banking): base sync date suggestions on booked coverage and actual fiscal period

The "start after your bookkeeping" suggestion in the account picker now
uses the latest posted verifikat date (journal_entries, status posted)
instead of sie_imports.fiscal_year_end: the fiscal period end can lie
months past the last actually booked transaction, so the old suggestion
made users skip every unbooked transaction in between. Companies with
no posted entries get no suggestion instead of a misleading one.

The suggested start date (day after the last posted verifikat) is
clamped to today (UTC): the PATCH handler rejects non-past
initial_lookback_from_date values, so a company whose latest verifikat
is dated today would otherwise be suggested tomorrow and get a 400 when
saving.

"Sedan raekenskapsaarets boerjan" now resolves from the fiscal_periods
row containing today, falling back to the recurring
fiscal_year_start_month setting only when no period row exists, so an
extended or shortened first fiscal year (e.g. 2025-10-01 to 2026-12-31)
resolves to its real start date instead of the recurring-year date.

Date logic extracted to lib/date-suggestions.ts with regression tests
covering both issue scenarios.

Fixes #917

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(banking): keep the fiscal-year date masked when the settings fetch fails (CodeRabbit)

A failed company_settings or fiscal_periods query silently fell back to
the calendar-year default, the exact misleading suggestion issue #917
removes. On error the date now stays masked and the request-side
fallback remains the recurring-setting derivation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 21:20:24 +02:00

78 lines
3.0 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { resolveBookedCoverage, resolveFiscalYearStart } from '../date-suggestions'
describe('resolveBookedCoverage', () => {
it('suggests the day after the last posted verifikat date', () => {
expect(resolveBookedCoverage('2026-05-14')).toEqual({
lastBookedDate: '2026-05-14',
suggestedStartDate: '2026-05-15',
})
})
it('rolls over month and year boundaries', () => {
// Pin "today" past every case so the clamp does not kick in.
const today = new Date('2030-01-01T12:00:00Z')
expect(resolveBookedCoverage('2026-01-31', today)?.suggestedStartDate).toBe('2026-02-01')
expect(resolveBookedCoverage('2026-12-31', today)?.suggestedStartDate).toBe('2027-01-01')
// Leap year: 2028-02-28 is not the last day of February.
expect(resolveBookedCoverage('2028-02-28', today)?.suggestedStartDate).toBe('2028-02-29')
})
it('clamps to today when the last posted verifikat is dated today (backend rejects non-past dates)', () => {
// Day after 2026-07-09 would be 2026-07-10 (tomorrow), which the PATCH
// handler rejects with 400; the suggestion must stay clickable.
const today = new Date('2026-07-09T12:00:00Z')
expect(resolveBookedCoverage('2026-07-09', today)).toEqual({
lastBookedDate: '2026-07-09',
suggestedStartDate: '2026-07-09',
})
})
it('clamps to today when the last posted verifikat is dated in the future', () => {
const today = new Date('2026-07-09T12:00:00Z')
expect(resolveBookedCoverage('2026-08-15', today)).toEqual({
lastBookedDate: '2026-08-15',
suggestedStartDate: '2026-07-09',
})
})
it('returns null when the company has no posted entries (issue #917: never fall back to fiscal_year_end)', () => {
expect(resolveBookedCoverage(null)).toBeNull()
expect(resolveBookedCoverage(undefined)).toBeNull()
expect(resolveBookedCoverage('')).toBeNull()
})
})
describe('resolveFiscalYearStart', () => {
const calendarYearSettings = {
fiscal_year_start_month: 1,
entity_type: 'aktiebolag' as const,
}
it('prefers the actual fiscal period row over the recurring start month (issue #917: extended first year)', () => {
// Company with an extended first fiscal year 2025-10-01 to 2026-12-31 that
// later runs calendar years: the recurring setting would wrongly resolve
// to 2026-01-01.
expect(
resolveFiscalYearStart('2025-10-01', calendarYearSettings, new Date('2026-07-09')),
).toBe('2025-10-01')
})
it('falls back to the recurring fiscal_year_start_month when no period row exists', () => {
expect(
resolveFiscalYearStart(null, calendarYearSettings, new Date('2026-07-09')),
).toBe('2026-01-01')
expect(
resolveFiscalYearStart(
undefined,
{ fiscal_year_start_month: 7, entity_type: 'aktiebolag' },
new Date('2026-05-01'),
),
).toBe('2025-07-01')
})
it('falls back to calendar year when settings are missing too', () => {
expect(resolveFiscalYearStart(null, null, new Date('2026-07-09'))).toBe('2026-01-01')
})
})