Files
F-r-ldraledigplannerare/frontend/tests/fixtures/plan.ts
T
jacobh2 014503e89b Spend vacation on the days worth having, and name the ones about to expire
The planner knew exactly how many semesterdagar a parent was about to
forfeit and never placed a single day to prevent it. It also had no
weekend-wrap logic at all: REASON_VACATION_WRAP_WEEKEND was stamped on
every tier-3 interior vacation day, including a lone Tuesday in February.

Two changes.

Spend better. A workday adjacent to a free stretch, or in the middle of a
gap of three or fewer fenced by free days, now takes vacation --
_vacation_bridge_reason, consulted for every parent type. This departs
from spec 6.1's "wrapping suppressed for hourly": the FP-preservation
argument is salary-type independent, only the pay argument was hourly-moot.
In a both-parents window, both now take vacation on such a date rather
than one paying with FP. After funding, reallocate_vacation_by_value swaps
vacation onto higher-ranked dates 1-for-1 with FP days, so which dates hold
vacation improves while how many never drops -- greedy first, re-timed
after, never withheld. An invariant test asserts the count can only rise.

Name the losses. A new pass proposes concrete dates for vacation that would
otherwise expire, current year only, ranked red-day bridge then weekend
bridge then interior, later dates first. They ride on
PlanProposal.rescue_days, kept off `days` so nothing downstream commits
them, and surface on the Dashboard with accept-one, accept-all (one
batched /api/plan/apply, not N) and dismiss. Dismissal reuses the
tip_dismissal table, keyed on (parent, year) identity rather than the
group's dates -- those are recomputed from the whole plan's residual on
every propose, so date-hashing made dismissal self-defeating. Dismiss does
not bump the plan version; it changes no data and must not trigger a write.

Measured on the seeded demo: 12 forfeiting days for one parent now yield 12
proposed dates, all Fridays or Mondays. Total vacation placed is unchanged
at 203 days, confirming the reallocation only re-times.

Three defects here were found by driving a browser, while 2273 unit tests
passed -- all three lived in the seam between layers that unit tests mock
away. Adds API-level tests over the real request sequence. Rate limiting is
now off in tests (one file consumed the whole registration budget) with the
suite's first real 429 assertion.

PROPOSE_VS_SYNC_BUG.md documents a pre-existing, unfixed issue found along
the way: Dashboard figures swing on the first sync with no user action.

Backend 946 tests, frontend 1341, coverage above all gates.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZaX6xbuMEMXuyKA1fq2Mm
2026-09-21 07:00:18 +02:00

271 lines
6.7 KiB
TypeScript

/**
* Shared test fixtures — the planning engine (`POST /api/plan/propose`,
* `POST /api/plan/apply`) and its nested shapes.
*/
import type {
CostBreakdown,
DayCollision,
EvictionCandidate,
EvictionPreview,
PlanAge4Projection,
PlanApplyResponse,
PlanChange,
PlanProposal,
PlanSyncResponse,
PlanWarning,
PoolBalance,
ProposedDay,
ProposedDayApplyItem,
Rejection,
ResourceShortage,
RescueDay,
ShortfallRemedy,
SurplusAdvice,
VacationBalance,
WindowOutcome,
} from '@/types'
export function makeCostBreakdown(overrides: Partial<CostBreakdown> = {}): CostBreakdown {
return { free: 0, vacation: 0, sjukpenning: 0, lagsta: 0, ...overrides }
}
export function makeShortfallRemedy(overrides: Partial<ShortfallRemedy> = {}): ShortfallRemedy {
return {
kind: 'dubbeldag',
messageKey: 'planRemedies.example',
data: {},
daysFreed: 0,
...overrides,
}
}
export function makeProposedDay(overrides: Partial<ProposedDay> = {}): ProposedDay {
return {
date: '2026-06-15',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
dayType: 'sjukpenning',
isDubbeldag: false,
wishId: 'lw-1',
reason: 'Sommar',
...overrides,
}
}
/** The `/api/plan/apply` request-side counterpart of `ProposedDay` — no
* `wishId`/`reason`, and `dayType` additionally allows `'free'`. */
export function makeProposedDayApplyItem(
overrides: Partial<ProposedDayApplyItem> = {},
): ProposedDayApplyItem {
return {
date: '2026-06-15',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
dayType: 'sjukpenning',
...overrides,
}
}
export function makeWindowOutcome(overrides: Partial<WindowOutcome> = {}): WindowOutcome {
return {
wishId: 'lw-1',
wishName: 'Sommar',
wishNameMessageKey: null,
year: 2026,
startDate: '2026-06-15',
endDate: '2026-07-26',
weeksWished: 6,
weeksFunded: 6,
shortfallDays: 0,
strategy: 'parent_swap',
messageKey: 'planOutcomes.windowExplanation',
data: {},
costBreakdown: makeCostBreakdown({ free: 12, vacation: 19, sjukpenning: 11 }),
remedies: [],
shiftGainDays: 0,
...overrides,
}
}
export function makePlanWarning(overrides: Partial<PlanWarning> = {}): PlanWarning {
return {
kind: 'days_lost_at_age_4',
messageKey: 'planWarnings.days_lost_at_age_4',
data: {},
...overrides,
}
}
export function makePoolBalance(overrides: Partial<PoolBalance> = {}): PoolBalance {
return {
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
sjukpenningLeft: 310,
lagstaLeft: 90,
dubbeldagarLeft: 55,
...overrides,
}
}
export function makeVacationBalance(overrides: Partial<VacationBalance> = {}): VacationBalance {
return {
parentPersonalDataUuid: 'pd-a',
year: 2026,
totalDays: 25,
savedDaysIn: 0,
alreadyUsed: 0,
isAssumed: false,
...overrides,
}
}
export function makeSurplusAdvice(overrides: Partial<SurplusAdvice> = {}): SurplusAdvice {
return {
kind: 'wish_more_leave',
messageKey: 'planSurplus.wish_more_leave',
daysInvolved: 0,
data: {},
...overrides,
}
}
export function makePlanAge4Projection(overrides: Partial<PlanAge4Projection> = {}): PlanAge4Projection {
return {
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
fourthBirthday: '2029-06-01',
projectedLeftAtB4: 0,
savableAfterB4: 96,
expiringAtB4: 0,
...overrides,
}
}
/** §vacation-rescue — one suggest-only concrete date (see `RescueDay` in
* src/types/entities.ts). Defaults to a date within `makePlanProposal`'s
* implicit current-year assumption for tests that don't care about the
* exact date. */
export function makeRescueDay(overrides: Partial<RescueDay> = {}): RescueDay {
return {
date: '2026-12-28',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
year: 2026,
reason: 'vacation_rescue',
...overrides,
}
}
export function makePlanProposal(overrides: Partial<PlanProposal> = {}): PlanProposal {
return {
days: [makeProposedDay()],
outcomes: [makeWindowOutcome()],
warnings: [],
poolBalances: [],
vacationBalances: [],
surplusAdvice: [],
age4Projections: [],
rescueDays: [],
...overrides,
}
}
export function makeDayCollision(overrides: Partial<DayCollision> = {}): DayCollision {
return {
date: '2026-06-15',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
existingDayType: 'vacation',
proposedDayType: 'sjukpenning',
...overrides,
}
}
export function makeRejection(overrides: Partial<Rejection> = {}): Rejection {
return {
messageKey: 'planApply.rejection.insufficientBalance',
data: {},
...overrides,
}
}
export function makePlanApplyResponse(overrides: Partial<PlanApplyResponse> = {}): PlanApplyResponse {
return {
accepted: true,
createdCount: 0,
skippedIdenticalCount: 0,
skippedFreeCount: 0,
replacedCount: 0,
collisions: [],
rejections: [],
createdDays: [],
...overrides,
}
}
/** `POST /api/plan/sync` — one (child, parent, date) slot's delta. See
* `PlanChange`'s docstring (src/types/entities.ts) for the `kind` vocabulary. */
export function makePlanChange(overrides: Partial<PlanChange> = {}): PlanChange {
return {
kind: 'added',
date: '2028-06-20',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
oldDayType: null,
newDayType: 'vacation',
oldIsDubbeldag: null,
newIsDubbeldag: false,
oldWishId: null,
newWishId: 'lw-1',
...overrides,
}
}
export function makeEvictionCandidate(overrides: Partial<EvictionCandidate> = {}): EvictionCandidate {
return {
date: '2028-06-20',
parentPersonalDataUuid: 'pd-a',
childId: 'child-1',
dayType: 'vacation',
isDubbeldag: false,
wishId: 'lw-1',
wishName: 'Sommar',
...overrides,
}
}
export function makeResourceShortage(overrides: Partial<ResourceShortage> = {}): ResourceShortage {
return {
resourceKind: 'vacation',
parentPersonalDataUuid: 'pd-a',
tier: null,
childId: null,
year: 2028,
daysEvicted: 1,
...overrides,
}
}
/** Always present on `PlanSyncResponse`. Defaults to the common (nothing
* evicted) case — pass `{ wouldEvict: true, candidates: [...], shortages:
* [...] }` to build the "nothing was written, ask the user" case. */
export function makeEvictionPreview(overrides: Partial<EvictionPreview> = {}): EvictionPreview {
return {
wouldEvict: false,
candidates: [],
shortages: [],
...overrides,
}
}
export function makePlanSyncResponse(overrides: Partial<PlanSyncResponse> = {}): PlanSyncResponse {
return {
proposal: makePlanProposal(),
applyResult: makePlanApplyResponse(),
changes: [],
evictionPreview: makeEvictionPreview(),
...overrides,
}
}