Fix calendar window overshooting the cached year range

Found by probing the running API; both suites were green and missed it.

CalendarService validated only start_year against the cache, never the window
end. GET /api/calendar/days?start_year=2035&years=4 returned 200 with
end_year=2038, pulling 2036-2038 from the live holidays-library fallback in
app/utils/holidays.py. That fallback does not know the de facto free days, so
Julafton, Nyarsafton and Midsommarafton were reported as ordinary working days
(18 red days per cached year vs 13 per uncached year). Julafton being charged
as a working day was fixed once before; it had reappeared past 2035.

- clamp end_year to max_year; the response now always reports what it returned
- add strict=True to get_swedish_holidays/get_red_days plus UncachedYearError,
  checked before the memo cache, so a strict caller fails loudly rather than
  silently degrading. Non-strict default behaviour is unchanged.
- RedDaysList derived endYear locally as startYear + years - 1, independent of
  the response, so a clamped window rendered phantom empty years. It now takes
  endYear from the backend.

Tests: clamping at and before max_year (service + router), Julafton/Nyarsafton/
Midsommarafton red for every cached year, red-day count consistent across the
served range, and strict-mode raise/pass-through.

Backend 404 -> 414 tests, frontend 704 -> 705.

Known, deliberately not fixed here: AllocationInputLoader builds its holiday map
the same non-strict way over the planning horizon, so it has the same exposure
once the horizon passes 2035. Clamping would silently truncate planning and
raising would break real plans, so it needs a product decision first.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
This commit is contained in:
2026-09-14 02:46:47 +02:00
parent 17dadb79cc
commit 28c632ca36
8 changed files with 295 additions and 8 deletions
@@ -118,6 +118,27 @@ describe('RedDaysList', () => {
expect(onNextWindow).toHaveBeenCalledTimes(1)
})
it('uses the actual endYear from the backend instead of startYear + years - 1 when the window was clamped', () => {
// Regression test: the backend clamps a window whose requested end
// exceeds the cached range (e.g. paging to the last page) and reports
// the ACTUAL end year via `endYear`. Without honoring it, this component
// would render extra year headers (2036, 2037) the backend never
// returned data for, showing a false "no red days" for years that are
// simply outside the cache rather than genuinely holiday-free.
render(
<RedDaysList
{...defaultProps}
startYear={2034}
endYear={2035}
minYear={2026}
maxYear={2035}
/>,
)
expect(screen.getByTestId('red-days-range')).toHaveTextContent('2034–2035')
expect(screen.queryByTestId('red-days-year-2036')).not.toBeInTheDocument()
expect(screen.queryByTestId('red-days-year-2037')).not.toBeInTheDocument()
})
it('shows a loading message and hides year groups while loading', () => {
render(<RedDaysList {...defaultProps} isLoading={true} />)
expect(screen.getByText(/Laddar röda dagar/)).toBeInTheDocument()
@@ -17,6 +17,17 @@ export interface RedDaysListProps {
days: CalendarDayEntry[]
startYear: number
years?: number
/**
* The window's ACTUAL end year, as reported by `GET /api/calendar/days`
* (`CalendarWindow.endYear`). The backend clamps its window to the cached
* range, so the last page can span fewer than `years` years — pass this
* through so the header/year-groups never claim years the backend didn't
* return (which would otherwise render as false "no red days" for years
* past the cache, e.g. once paging reaches `maxYear`). Falls back to the
* naive `startYear + years - 1` when omitted, e.g. before the first fetch
* resolves and there's no response to read it from yet.
*/
endYear?: number | undefined
minYear: number | null
maxYear: number | null
isLoading?: boolean
@@ -29,6 +40,7 @@ export function RedDaysList({
days,
startYear,
years = 4,
endYear: endYearProp,
minYear,
maxYear,
isLoading = false,
@@ -37,7 +49,7 @@ export function RedDaysList({
onNextWindow,
}: RedDaysListProps): ReactNode {
const { t } = useTranslation()
const endYear = startYear + years - 1
const endYear = endYearProp ?? startYear + years - 1
const byYear = useMemo(() => {
const map = new Map<number, CalendarDayEntry[]>()