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
+57
View File
@@ -3,7 +3,11 @@
import datetime
import pytest
from app.utils.holidays import (
UncachedYearError,
cached_year_bounds,
get_holiday_name,
get_red_days,
get_swedish_holidays,
@@ -255,3 +259,56 @@ class TestFallbackForUncachedYear:
d for d, name in get_swedish_holidays(2040).items() if "Påskdagen" in name
)
assert easter_sunday in red_days
# ---------------------------------------------------------------------------
# `strict=True` — defense in depth for callers that must never silently hit
# the fallback (currently `CalendarService`; see the calendar-endpoint bug
# where years past the cache silently degraded to this same fallback).
# ---------------------------------------------------------------------------
class TestStrictMode:
def test_strict_raises_for_a_year_past_the_cache(self) -> None:
bounds = cached_year_bounds()
assert bounds is not None
_, max_year = bounds
with pytest.raises(UncachedYearError):
get_swedish_holidays(max_year + 1, strict=True)
def test_strict_raises_for_a_year_before_the_cache(self) -> None:
bounds = cached_year_bounds()
assert bounds is not None
min_year, _ = bounds
with pytest.raises(UncachedYearError):
get_swedish_holidays(min_year - 1, strict=True)
def test_strict_succeeds_and_matches_non_strict_for_a_cached_year(self) -> None:
bounds = cached_year_bounds()
assert bounds is not None
min_year, _ = bounds
assert get_swedish_holidays(min_year, strict=True) == get_swedish_holidays(min_year)
def test_strict_still_raises_even_if_the_year_was_already_cached_non_strict(self) -> None:
"""A prior non-strict call for an uncached year must not poison the
per-year memo cache so a later strict call is fooled into thinking
it's fine."""
bounds = cached_year_bounds()
assert bounds is not None
_, max_year = bounds
uncached_year = max_year + 5
get_swedish_holidays(uncached_year) # warms the non-strict cache
with pytest.raises(UncachedYearError):
get_swedish_holidays(uncached_year, strict=True)
def test_get_red_days_forwards_strict(self) -> None:
bounds = cached_year_bounds()
assert bounds is not None
_, max_year = bounds
with pytest.raises(UncachedYearError):
get_red_days(max_year + 1, strict=True)