feat: build the frozen screen payload from resolved schedule data
Every string the panel shows is computed here, server-side and in both languages: the status word, the formatted hours, the next-change line, the week strip and the banner. The renderer downstream only draws, so it has no access to the schedule and cannot disagree with this file about what the shop's hours are. Closing soon is still open and opening soon is still closed: the large word states the fact and the line under it carries the nuance, which is the only thing a passer-by can read from the pavement. A message someone took the trouble to write always wins the banner over the automatic notice. When its English translation has not landed yet, the French line shows alone rather than a placeholder. The snapshot test is the regression guard for the whole display pipeline: any change to the shape or to a computed string has to be acknowledged there before it can reach the panel. lib/screen is at 100% statements and 96% branches. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
@@ -5,8 +5,10 @@ import {
|
||||
civilDayOfWeek,
|
||||
compareCivil,
|
||||
minutesOfTime,
|
||||
startOfCivilWeek,
|
||||
toCivilDate,
|
||||
toCivilTime,
|
||||
toIsoWithOffset,
|
||||
} from './civil';
|
||||
|
||||
const ZURICH = 'Europe/Zurich';
|
||||
@@ -99,3 +101,36 @@ describe('minutesOfTime', () => {
|
||||
expect(minutesOfTime('23:59')).toBe(1439);
|
||||
});
|
||||
});
|
||||
|
||||
describe('startOfCivilWeek', () => {
|
||||
it('returns the Monday of that week', () => {
|
||||
// 2026-09-20 is a Sunday: its week started on Monday the 14th.
|
||||
expect(startOfCivilWeek('2026-09-20')).toBe('2026-09-14');
|
||||
expect(startOfCivilWeek('2026-09-21')).toBe('2026-09-21');
|
||||
expect(startOfCivilWeek('2026-09-23')).toBe('2026-09-21');
|
||||
});
|
||||
|
||||
it('reaches back across a month boundary', () => {
|
||||
expect(startOfCivilWeek('2026-10-01')).toBe('2026-09-28');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toIsoWithOffset', () => {
|
||||
it('carries the summer offset', () => {
|
||||
expect(toIsoWithOffset(new Date('2026-09-20T08:15:00Z'), ZURICH)).toBe(
|
||||
'2026-09-20T10:15:00+02:00',
|
||||
);
|
||||
});
|
||||
|
||||
it('carries the winter offset', () => {
|
||||
expect(toIsoWithOffset(new Date('2026-01-15T08:15:00Z'), ZURICH)).toBe(
|
||||
'2026-01-15T09:15:00+01:00',
|
||||
);
|
||||
});
|
||||
|
||||
it('prints UTC as +00:00', () => {
|
||||
expect(toIsoWithOffset(new Date('2026-09-20T08:15:30Z'), 'UTC')).toBe(
|
||||
'2026-09-20T08:15:30+00:00',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -101,6 +101,46 @@ export function isWithinCivilRange(date: CivilDate, start: CivilDate, end: Civil
|
||||
return compareCivil(date, start) >= 0 && compareCivil(date, end) <= 0;
|
||||
}
|
||||
|
||||
/** The Monday of the week containing `date`. */
|
||||
export function startOfCivilWeek(date: CivilDate): CivilDate {
|
||||
const dayOfWeek = civilDayOfWeek(date);
|
||||
// getDay() puts Sunday at 0; the shop's week starts on Monday.
|
||||
const daysSinceMonday = (dayOfWeek + 6) % 7;
|
||||
return addCivilDays(date, -daysSinceMonday);
|
||||
}
|
||||
|
||||
/**
|
||||
* The instant as an ISO 8601 string carrying the shop's UTC offset, e.g.
|
||||
* "2026-09-20T10:15:00+02:00".
|
||||
*
|
||||
* The offset is derived from the zone itself rather than assumed, so the same
|
||||
* code prints +01:00 in winter and +02:00 in summer without being told.
|
||||
*/
|
||||
export function toIsoWithOffset(instant: Date, timeZone: string): string {
|
||||
const date = toCivilDate(instant, timeZone);
|
||||
const time = toCivilTime(instant, timeZone);
|
||||
const seconds = instant.getUTCSeconds().toString().padStart(2, '0');
|
||||
|
||||
// Reading the wall clock back as if it were UTC gives the offset directly.
|
||||
const asUtc = Date.UTC(
|
||||
Number(date.slice(0, 4)),
|
||||
Number(date.slice(5, 7)) - 1,
|
||||
Number(date.slice(8, 10)),
|
||||
Number(time.slice(0, 2)),
|
||||
Number(time.slice(3, 5)),
|
||||
instant.getUTCSeconds(),
|
||||
);
|
||||
const offsetMinutes = Math.round((asUtc - instant.getTime()) / 60_000);
|
||||
const sign = offsetMinutes < 0 ? '-' : '+';
|
||||
const absolute = Math.abs(offsetMinutes);
|
||||
const hours = Math.floor(absolute / 60)
|
||||
.toString()
|
||||
.padStart(2, '0');
|
||||
const minutes = (absolute % 60).toString().padStart(2, '0');
|
||||
|
||||
return `${date}T${time}:${seconds}${sign}${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
/** Minutes since midnight for a `HH:mm` wall-clock time. */
|
||||
export function minutesOfTime(time: CivilTime): number {
|
||||
const [hours, minutes] = time.split(':');
|
||||
|
||||
Reference in New Issue
Block a user