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:
@@ -77,6 +77,115 @@ class TestYearWindowPagination:
|
||||
years_seen = {d.date.year for d in window.days}
|
||||
assert len(years_seen) == MAX_WINDOW_YEARS
|
||||
|
||||
async def test_window_reaching_max_year_is_clamped_not_rejected(
|
||||
self, calendar_service: CalendarService
|
||||
) -> None:
|
||||
"""§ bug: `GET /days?start_year=<max_year>&years=4` used to happily
|
||||
report `end_year = max_year + 3` and silently serve the extra years
|
||||
from the degraded `holidays`-library fallback. The window must be
|
||||
clamped to the cache instead, and `end_year` must report the ACTUAL
|
||||
range served — never a range that wasn't returned.
|
||||
"""
|
||||
bounds = cached_year_bounds()
|
||||
assert bounds is not None
|
||||
_, max_year = bounds
|
||||
|
||||
window = await calendar_service.get_calendar_window(
|
||||
workspace_id=uuid4(), start_year=max_year, years=MAX_WINDOW_YEARS
|
||||
)
|
||||
|
||||
assert window.start_year == max_year
|
||||
assert window.end_year == max_year, "must clamp end_year to the cache, not overshoot it"
|
||||
years_seen = {d.date.year for d in window.days}
|
||||
assert years_seen == {max_year}, "no day may come from a year beyond the cache"
|
||||
|
||||
async def test_a_middle_page_that_would_overshoot_is_also_clamped(
|
||||
self, calendar_service: CalendarService
|
||||
) -> None:
|
||||
"""Same clamp, exercised one page before the very last year (the
|
||||
`start_year=2034, years=4` case from real paging: 2034+3=2037 would
|
||||
overshoot the 2026-2035 cache by two years)."""
|
||||
bounds = cached_year_bounds()
|
||||
assert bounds is not None
|
||||
_, max_year = bounds
|
||||
start_year = max_year - 1
|
||||
|
||||
window = await calendar_service.get_calendar_window(
|
||||
workspace_id=uuid4(), start_year=start_year, years=MAX_WINDOW_YEARS
|
||||
)
|
||||
|
||||
assert window.end_year == max_year
|
||||
years_seen = {d.date.year for d in window.days}
|
||||
assert years_seen == {start_year, max_year}
|
||||
assert max(years_seen) <= max_year
|
||||
|
||||
|
||||
class TestNoDegradedDataAcrossTheServedRange:
|
||||
"""§4.2/§6: the fallback `holidays` library only knows formal röda dagar
|
||||
and misses de facto days (Julafton, Nyårsafton, Midsommarafton). This is
|
||||
exactly the regression that shipped: years past the cache silently lost
|
||||
those days. Assert they're present for EVERY year the endpoint can ever
|
||||
serve, and that no year is silently poorer than its neighbours.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
async def _all_cached_years_red_days(
|
||||
calendar_service: CalendarService,
|
||||
) -> dict[int, dict[date, str]]:
|
||||
bounds = cached_year_bounds()
|
||||
assert bounds is not None
|
||||
min_year, max_year = bounds
|
||||
|
||||
by_year: dict[int, dict[date, str]] = {}
|
||||
start = min_year
|
||||
while start <= max_year:
|
||||
window = await calendar_service.get_calendar_window(
|
||||
workspace_id=uuid4(), start_year=start
|
||||
)
|
||||
for day in window.days:
|
||||
if day.day_type == "red_day":
|
||||
by_year.setdefault(day.date.year, {})[day.date] = day.name or ""
|
||||
start = window.end_year + 1
|
||||
return by_year
|
||||
|
||||
async def test_julafton_nyarsafton_and_midsommarafton_are_red_days_every_year(
|
||||
self, calendar_service: CalendarService
|
||||
) -> None:
|
||||
by_year = await self._all_cached_years_red_days(calendar_service)
|
||||
bounds = cached_year_bounds()
|
||||
assert bounds is not None
|
||||
min_year, max_year = bounds
|
||||
assert set(by_year) == set(range(min_year, max_year + 1)), (
|
||||
"every cached year must be reachable via pagination"
|
||||
)
|
||||
|
||||
for year, red_days in by_year.items():
|
||||
names_by_date = {d: name.lower() for d, name in red_days.items()}
|
||||
|
||||
julafton = [d for d, n in names_by_date.items() if d.month == 12 and d.day == 24]
|
||||
assert julafton and "julafton" in names_by_date[julafton[0]], (
|
||||
f"{year}: Julafton (12-24) must be a red day"
|
||||
)
|
||||
|
||||
nyarsafton = [d for d, n in names_by_date.items() if d.month == 12 and d.day == 31]
|
||||
assert nyarsafton and "nyårsafton" in names_by_date[nyarsafton[0]], (
|
||||
f"{year}: Nyårsafton (12-31) must be a red day"
|
||||
)
|
||||
|
||||
midsommarafton = [d for d, n in names_by_date.items() if "midsommarafton" in n]
|
||||
assert midsommarafton, f"{year}: Midsommarafton must be a red day"
|
||||
|
||||
async def test_red_day_count_is_consistent_across_every_served_year(
|
||||
self, calendar_service: CalendarService
|
||||
) -> None:
|
||||
by_year = await self._all_cached_years_red_days(calendar_service)
|
||||
counts = {year: len(red_days) for year, red_days in by_year.items()}
|
||||
# No year may be silently poorer than its neighbours (the fallback's
|
||||
# signature: 18 real red days in-cache vs. 13 once it kicks in).
|
||||
assert len(set(counts.values())) == 1, (
|
||||
f"red-day counts differ across years, one may be degraded: {counts}"
|
||||
)
|
||||
|
||||
|
||||
class TestOverrideMerging:
|
||||
async def test_worked_override_flips_a_statutory_red_day_to_not_free(
|
||||
|
||||
Reference in New Issue
Block a user