fix: start the quick controls from the selected day's own hours

"Opens later at" and "closes earlier at" were prefilled with 14:00 and
16:00 — arbitrary values, wrong on most days, which left the preview
showing hours nobody had asked for until both fields were corrected.
They now start from the day's real opening and closing, so any
adjustment is relative to what that day actually is.

A day with a lunch break is bounded by its first opening and its last
closing, which is what these two actions act on.

Changing the date changes the hours the times are relative to, so a
value typed for Tuesday must not survive into Thursday. The fields live
in a component keyed on the date and React resets them by remounting —
the React compiler rightly refuses an effect that writes state back, and
it is the clearer arrangement anyway.

The end-to-end test picks a trading day rather than assuming today is
one. The first version guarded the assertions behind a visibility check,
which would have passed in silence on exactly the days the controls
matter least.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-22 23:24:28 +02:00
co-authored by Claude Opus 5
parent 7312a121df
commit 743a3778cc
4 changed files with 168 additions and 57 deletions
+37 -1
View File
@@ -1,6 +1,13 @@
import { describe, expect, it } from 'vitest';
import { checkRange, earlyClosing, expandRange, lateOpening, MAX_RANGE_DAYS } from './exceptions';
import {
checkRange,
earlyClosing,
expandRange,
lateOpening,
MAX_RANGE_DAYS,
usualBounds,
} from './exceptions';
import type { Slot } from './types';
const FULL: Slot[] = [{ open: '10:00', close: '18:30' }];
@@ -130,3 +137,32 @@ describe('checkRange', () => {
expect(checkRange('2026-01-01', '2030-01-01')).toBe('TOO_LONG');
});
});
describe('usualBounds', () => {
it('takes the times of a single period', () => {
expect(usualBounds([{ open: '11:00', close: '18:00' }])).toEqual({
open: '11:00',
close: '18:00',
});
});
it('spans a day with a lunch break from first opening to last closing', () => {
// "Opens later" and "closes earlier" act on the day as a whole.
expect(usualBounds(SPLIT)).toEqual({ open: '10:00', close: '18:30' });
});
it('falls back to usable times on a closed day', () => {
// The controls are hidden there, but the inputs must still hold a value.
expect(usualBounds([])).toEqual({ open: '10:00', close: '18:00' });
});
it('handles three periods', () => {
expect(
usualBounds([
{ open: '09:30', close: '12:00' },
{ open: '13:30', close: '16:00' },
{ open: '17:00', close: '19:30' },
]),
).toEqual({ open: '09:30', close: '19:30' });
});
});
+21
View File
@@ -40,6 +40,27 @@ export function earlyClosing(slots: readonly Slot[], until: string): Slot[] {
.map((slot) => (minutesOfTime(slot.close) > closesAt ? { ...slot, close: until } : { ...slot }));
}
/**
* The day's own opening and closing times, for the quick-change controls.
*
* Prefilling them with the day's real hours anchors the adjustment in what
* that day actually is: pushing the opening back starts from when it opens,
* not from an arbitrary 14:00 that happens to be wrong on most days. The
* preview beside the control then shows the consequence immediately.
*
* A day with a lunch break is bounded by its first opening and its last
* closing, which is what "opens later" and "closes earlier" act on.
*/
export function usualBounds(slots: readonly Slot[]): { open: string; close: string } {
const first = slots[0];
const last = slots[slots.length - 1];
if (!first || !last) {
// The controls are hidden on a closed day; these keep the inputs valid.
return { open: '10:00', close: '18:00' };
}
return { open: first.open, close: last.close };
}
/**
* Every date in `[start, end]`, inclusive.
*