mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(tours): the last review findings of the city walks
A place closing at midnight is no longer read as open past it: the hours run on only where the next date opens at midnight (any window of the day counts, as written order is kept), and an impossible date such as 2026-02-31 reads as unknown instead of rolling into March. The hours source names the trip's places rather than "your own" entries, in all 27 locales, as the pill beside it does. The day a walk's arrivals are checked for shows its date on the pill, not only "Day N". The 12 hour clock is tested for a window to midnight. The along-the-tour term table is built once again, now that the one test that stubbed the places client without its real exports spreads them. New test seeding goes through the ORM factories.
This commit is contained in:
@@ -21,7 +21,11 @@ const SATURDAY = '2026-10-24';
|
||||
const SUNDAY = '2026-10-25';
|
||||
|
||||
const at = (clock: string, atLeast = false) => ({ arrival: minutesOf(clock) ?? Number.NaN, atLeast });
|
||||
const osm = (windows: Array<[number, number]>): StationHours => ({ source: 'osm', windows });
|
||||
const osm = (windows: Array<[number, number]>, continues = false): StationHours => ({
|
||||
source: 'osm',
|
||||
windows,
|
||||
continues,
|
||||
});
|
||||
|
||||
describe('minutesOf and clockOf', () => {
|
||||
it('reads a time as TREK stores it, with or without seconds or a leading zero', () => {
|
||||
@@ -92,6 +96,8 @@ describe('openWindows (OpenStreetMap)', () => {
|
||||
expect(openWindows(text, MONDAY)).toBeNull();
|
||||
}
|
||||
expect(openWindows(week, '19.10.2026')).toBeNull();
|
||||
expect(openWindows(week, '2026-02-31')).toBeNull();
|
||||
expect(openWindows(week, '2026-13-01')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -148,6 +154,7 @@ describe('stationHoursOn', () => {
|
||||
expect(stationHoursOn({ place, osm: 'Mo-Fr 09:00-18:00' }, MONDAY)).toEqual({
|
||||
source: 'place',
|
||||
windows: [[660, 1020]],
|
||||
continues: false,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -155,13 +162,42 @@ describe('stationHoursOn', () => {
|
||||
expect(stationHoursOn({ place, osm: 'Mo-Fr 09:00-18:00' }, TUESDAY)).toEqual({
|
||||
source: 'osm',
|
||||
windows: [[540, 1080]],
|
||||
continues: false,
|
||||
});
|
||||
expect(stationHoursOn({ osm: 'sunrise-sunset' }, TUESDAY)).toEqual({ source: 'osm', unread: 'sunrise-sunset' });
|
||||
});
|
||||
|
||||
it('is null where neither says anything', () => {
|
||||
it('is null where neither says anything, or for a date it cannot read', () => {
|
||||
expect(stationHoursOn({ place, osm: ' ' }, TUESDAY)).toBeNull();
|
||||
expect(stationHoursOn({}, MONDAY)).toBeNull();
|
||||
expect(stationHoursOn({ place, osm: 'Mo-Fr 09:00-18:00' }, '2026-02-31')).toBeNull();
|
||||
});
|
||||
|
||||
it('runs an OpenStreetMap last window on past midnight only where the next date opens at midnight', () => {
|
||||
const continuesOn = (text: string, date: string) => {
|
||||
const hours = stationHoursOn({ osm: text }, date);
|
||||
return hours && 'continues' in hours ? hours.continues : null;
|
||||
};
|
||||
expect(continuesOn('24/7', SUNDAY)).toBe(true);
|
||||
expect(continuesOn('Mo 18:00-24:00; Tu 00:00-02:00', MONDAY)).toBe(true);
|
||||
expect(continuesOn('Mo-Su 10:00-24:00', MONDAY)).toBe(false);
|
||||
expect(continuesOn('Mo-Su 10:00-18:00', MONDAY)).toBe(false);
|
||||
expect(continuesOn('Mo 18:00-24:00; Tu off', MONDAY)).toBe(false);
|
||||
});
|
||||
|
||||
it("runs a trip place's last window on past midnight only where the next date opens at midnight", () => {
|
||||
const week = (monday: object, tuesday: object) =>
|
||||
JSON.stringify([monday, tuesday, ...Array.from({ length: 5 }, () => ({ closed: true }))]);
|
||||
const continuesOn = (raw: string) => {
|
||||
const hours = stationHoursOn({ place: raw }, MONDAY);
|
||||
return hours && 'continues' in hours ? hours.continues : null;
|
||||
};
|
||||
expect(continuesOn(week({ closed: false, open: '18:00', close: '02:00' }, { closed: true }))).toBe(true);
|
||||
const allDay = { closed: false, open: '00:00', close: '00:00' };
|
||||
expect(continuesOn(week(allDay, allDay))).toBe(true);
|
||||
expect(continuesOn(week({ closed: false, open: '18:00', close: '00:00' }, { closed: true }))).toBe(false);
|
||||
expect(continuesOn(week({ closed: false, open: '18:00', close: '00:00' }, { closed: false }))).toBe(false);
|
||||
expect(continuesOn(week({ closed: true }, allDay))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -181,14 +217,27 @@ describe('stationFit', () => {
|
||||
it('is closed at an arrival outside the windows, with the windows of the day', () => {
|
||||
expect(stationFit(museum, at('09:00'), 30)).toEqual({ kind: 'closed', source: 'osm', windows: [[600, 1080]] });
|
||||
expect(stationFit(museum, at('18:00'), null)).toEqual({ kind: 'closed', source: 'osm', windows: [[600, 1080]] });
|
||||
expect(stationFit({ source: 'place', windows: [] }, at('12:00'), 30)).toEqual({
|
||||
expect(stationFit({ source: 'place', windows: [], continues: false }, at('12:00'), 30)).toEqual({
|
||||
kind: 'closedAllDay',
|
||||
source: 'place',
|
||||
});
|
||||
});
|
||||
|
||||
it('takes a window running to midnight as open past it', () => {
|
||||
expect(stationFit(osm([[600, 1440]]), at('23:20'), 90).kind).toBe('open');
|
||||
it('takes a window to midnight as open past it only where the hours run on into the next date', () => {
|
||||
expect(stationFit(osm([[600, 1440]], true), at('23:20'), 90).kind).toBe('open');
|
||||
expect(stationFit(osm([[600, 1440]]), at('23:30'), 60)).toEqual({
|
||||
kind: 'closes',
|
||||
source: 'osm',
|
||||
window: [600, 1440],
|
||||
});
|
||||
expect(stationFit(osm([[600, 1440]]), at('23:20'), 30).kind).toBe('open');
|
||||
expect(stationFit(osm([[600, 1440]]), at('23:30', true), 60).kind).toBe('closes');
|
||||
});
|
||||
|
||||
it('names the closing at midnight of OpenStreetMap hours ending at 24:00 for a stay past it', () => {
|
||||
const hours = stationHoursOn({ osm: 'Mo-Su 10:00-24:00' }, MONDAY);
|
||||
expect(stationFit(hours, at('23:30'), 60)).toEqual({ kind: 'closes', source: 'osm', window: [600, 1440] });
|
||||
expect(stationFit(stationHoursOn({ osm: '24/7' }, MONDAY), at('23:30'), 60).kind).toBe('open');
|
||||
});
|
||||
|
||||
it('says nothing for hours not known, and gives unread hours back as written', () => {
|
||||
|
||||
@@ -28,8 +28,14 @@ export type HoursWindow = readonly [number, number];
|
||||
/** Where a stop's hours come from: OpenStreetMap, or the trip place's own entry. */
|
||||
export type HoursSource = 'osm' | 'place';
|
||||
|
||||
/** A stop's hours on one date: its open windows (none when closed all day), or text that could not be read. */
|
||||
export type StationHours = { source: HoursSource; windows: readonly HoursWindow[] } | { source: 'osm'; unread: string };
|
||||
/**
|
||||
* A stop's hours on one date: its open windows (none when closed all day), or
|
||||
* text that could not be read. `continues` says the last window, ending at
|
||||
* midnight, runs on into the next date, as it does around the clock or for a
|
||||
* night past midnight; a place closing at midnight does not.
|
||||
*/
|
||||
export type StationHours =
|
||||
{ source: HoursSource; windows: readonly HoursWindow[]; continues: boolean } | { source: 'osm'; unread: string };
|
||||
|
||||
function clockMinutes(hours: number, minutes: number): number | null {
|
||||
return hours < 24 && minutes < 60 ? hours * 60 + minutes : null;
|
||||
@@ -58,8 +64,12 @@ export function clockOf(minutes: number): string {
|
||||
function weekdayOf(date: string): number | null {
|
||||
const match = ISO_DATE.exec(date);
|
||||
if (!match) return null;
|
||||
const day = new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3]))).getUTCDay();
|
||||
return Number.isNaN(day) ? null : (day + 6) % 7;
|
||||
const month = Number(match[2]) - 1;
|
||||
const dayOfMonth = Number(match[3]);
|
||||
const day = new Date(Date.UTC(Number(match[1]), month, dayOfMonth));
|
||||
// A date that does not exist, such as 2026-02-31, would roll over into the next month.
|
||||
if (day.getUTCMonth() !== month || day.getUTCDate() !== dayOfMonth) return null;
|
||||
return (day.getUTCDay() + 6) % 7;
|
||||
}
|
||||
|
||||
/** The weekdays of an OpenStreetMap day list such as `Mo-Fr,Su`; a range may wrap round the week (`Sa-Mo`). */
|
||||
@@ -106,8 +116,11 @@ function ruleOf(rule: string): { days: Set<number>; windows: HoursWindow[] } | n
|
||||
/** The windows OpenStreetMap's hours give a date, in minutes; null where they cannot be read. */
|
||||
export function openWindows(hours: string, date: string): HoursWindow[] | null {
|
||||
const weekday = weekdayOf(date);
|
||||
if (weekday === null) return null;
|
||||
const text = hours.trim();
|
||||
return weekday === null ? null : osmWindows(hours.trim(), weekday);
|
||||
}
|
||||
|
||||
/** The windows OpenStreetMap's hours give a weekday, Monday 0; null where they cannot be read. */
|
||||
function osmWindows(text: string, weekday: number): HoursWindow[] | null {
|
||||
if (text === '24/7') return [[0, DAY_MINUTES]];
|
||||
let windows: HoursWindow[] = [];
|
||||
for (const raw of text.split(';')) {
|
||||
@@ -147,7 +160,11 @@ function spillOf(day: PlaceHoursDay | undefined): HoursWindow | null {
|
||||
export function placeWindows(raw: string | null | undefined, date: string): HoursWindow[] | null {
|
||||
const week = parsePlaceHours(raw);
|
||||
const weekday = weekdayOf(date);
|
||||
if (!week || weekday === null) return null;
|
||||
return week && weekday !== null ? placeWeekWindows(week, weekday) : null;
|
||||
}
|
||||
|
||||
/** The windows of one weekday of a place's own week, Monday 0, with the night of the day before. */
|
||||
function placeWeekWindows(week: readonly PlaceHoursDay[], weekday: number): HoursWindow[] | null {
|
||||
const today = dayWindows(week[weekday]);
|
||||
if (today === null) return null;
|
||||
const spill = spillOf(week[(weekday + 6) % 7]);
|
||||
@@ -157,18 +174,35 @@ export function placeWindows(raw: string | null | undefined, date: string): Hour
|
||||
/**
|
||||
* A stop's hours on a date: the trip place it stands for first, as the
|
||||
* travellers entered them, else what OpenStreetMap had for it; null where
|
||||
* neither says anything.
|
||||
* neither says anything or the date cannot be read.
|
||||
*/
|
||||
export function stationHoursOn(
|
||||
hours: { place?: string | null; osm?: string | null },
|
||||
date: string,
|
||||
): StationHours | null {
|
||||
const own = placeWindows(hours.place, date);
|
||||
if (own) return { source: 'place', windows: own };
|
||||
const weekday = weekdayOf(date);
|
||||
if (weekday === null) return null;
|
||||
const tomorrow = (weekday + 1) % 7;
|
||||
const week = parsePlaceHours(hours.place);
|
||||
const own = week && placeWeekWindows(week, weekday);
|
||||
if (week && own) {
|
||||
return { source: 'place', windows: own, continues: runsOn(own, placeWeekWindows(week, tomorrow)) };
|
||||
}
|
||||
const text = hours.osm?.trim();
|
||||
if (!text) return null;
|
||||
const windows = openWindows(text, date);
|
||||
return windows ? { source: 'osm', windows } : { source: 'osm', unread: text };
|
||||
const windows = osmWindows(text, weekday);
|
||||
return windows
|
||||
? { source: 'osm', windows, continues: runsOn(windows, osmWindows(text, tomorrow)) }
|
||||
: { source: 'osm', unread: text };
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a window of the day ends at midnight and the next day opens at
|
||||
* midnight, so the hours run on past it. Every window is looked at, as the
|
||||
* hours text keeps the order it was written in.
|
||||
*/
|
||||
function runsOn(today: readonly HoursWindow[], tomorrow: readonly HoursWindow[] | null): boolean {
|
||||
return today.some(([, to]) => to === DAY_MINUTES) && (tomorrow?.some(([from]) => from === 0) ?? false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,10 +236,10 @@ function fitInWindow(
|
||||
source: HoursSource,
|
||||
window: HoursWindow,
|
||||
leave: number | null,
|
||||
later: { atLeast: boolean; windowAfter: boolean },
|
||||
later: { atLeast: boolean; windowAfter: boolean; continues: boolean },
|
||||
): StationFit {
|
||||
// A window running to midnight is taken as open past it.
|
||||
const closes = leave !== null && leave > window[1] && window[1] !== DAY_MINUTES;
|
||||
// A window to midnight is open past it only where the hours run on into the next date.
|
||||
const closes = leave !== null && leave > window[1] && !(window[1] === DAY_MINUTES && later.continues);
|
||||
// An arrival that may come later can still miss the closing, or reach the next window instead.
|
||||
if (later.atLeast)
|
||||
return closes && !later.windowAfter ? { kind: 'closes', source, window } : { kind: 'uncertain', source };
|
||||
@@ -221,12 +255,12 @@ export function stationFit(hours: StationHours | null, at: WalkArrival, stay: nu
|
||||
if (at.arrival >= DAY_MINUTES) return { kind: 'nextDay' };
|
||||
if (!hours) return { kind: 'unknown' };
|
||||
if ('unread' in hours) return { kind: 'unread', text: hours.unread };
|
||||
const { source, windows } = hours;
|
||||
const { source, windows, continues } = hours;
|
||||
if (windows.length === 0) return { kind: 'closedAllDay', source };
|
||||
const leave = stay === null ? null : at.arrival + stay;
|
||||
const window = windows.find(([from, to]) => at.arrival >= from && at.arrival < to);
|
||||
const windowAfter = windows.some(([from]) => from > at.arrival);
|
||||
if (window) return fitInWindow(source, window, leave, { atLeast: at.atLeast, windowAfter });
|
||||
if (window) return fitInWindow(source, window, leave, { atLeast: at.atLeast, windowAfter, continues });
|
||||
return at.atLeast && windowAfter ? { kind: 'uncertain', source } : { kind: 'closed', source, windows };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user