Skip to content

Commit 8dd3edf

Browse files
committed
test(scenarios): walk the working-day calendar once in the run-date oracle
The rebase oracle stepped through the calendar a day at a time for every date of every card on every run date, so the 2028 run dates (hundreds of working days from the cards' today) took 5-7 s on CI against vitest's 5 s default. The same day-by-day walk now runs once over 1960-2040 into a numbered table that walkShift, walkCount, onOrBefore and onOrAfter read, and leaves the implementation's arithmetic out of it as before. The stepping walkers stay, and a new case checks the table against them across the epoch week, a year end and a leap day. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ADHkmRWUrXgua6hAwuKweu
1 parent 63db724 commit 8dd3edf

1 file changed

Lines changed: 69 additions & 9 deletions

File tree

‎packages/qa/scenarios/test/runner/run-date.test.ts‎

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,24 @@ const RUN_DATES = ['2026-11-15', '2026-11-30', '2026-12-31', '2027-01-01', '2027
4545

4646
const ISO = /^\d{4}-\d{2}-\d{2}$/;
4747

48-
// ── an independent working-day reading: one calendar day at a time, skipping Saturday and Sunday ──
48+
// ── an independent working-day reading: the calendar walked one day at a time, skipping Saturday and Sunday ──
4949
const weekday = (d: string) => new Date(`${d.slice(0, 10)}T00:00:00Z`).getUTCDay();
5050
const isWorking = (d: string) => weekday(d) !== 0 && weekday(d) !== 6;
51-
/** The working day on or before `d`. */
52-
const onOrBefore = (d: string): string => (isWorking(d) ? d : onOrBefore(shiftDate(d, -1)));
53-
/** The working day on or after `d`. */
54-
const onOrAfter = (d: string): string => (isWorking(d) ? d : onOrAfter(shiftDate(d, 1)));
55-
/** Working days from `from` to `to`, both working days, counted by walking the calendar. */
56-
function walkCount(from: string, to: string): number {
51+
52+
// The walk, step by step: the reading itself, kept to check the calendar table below against.
53+
/** The working day on or before `d`, stepping back a day at a time. */
54+
const stepOnOrBefore = (d: string): string => (isWorking(d) ? d : stepOnOrBefore(shiftDate(d, -1)));
55+
/** The working day on or after `d`, stepping on a day at a time. */
56+
const stepOnOrAfter = (d: string): string => (isWorking(d) ? d : stepOnOrAfter(shiftDate(d, 1)));
57+
/** Working days from `from` to `to`, counted by stepping through the calendar. */
58+
function stepCount(from: string, to: string): number {
5759
let n = 0;
5860
for (let d = from; d < to; d = shiftDate(d, 1)) if (isWorking(shiftDate(d, 1))) n++;
5961
for (let d = from; d > to; d = shiftDate(d, -1)) if (isWorking(d)) n--;
6062
return n;
6163
}
62-
/** `d`, a working day, `n` working days on, by walking the calendar. */
63-
function walkShift(d: string, n: number): string {
64+
/** `d` moved `n` working days, by stepping through the calendar. */
65+
function stepShift(d: string, n: number): string {
6466
let t = d.slice(0, 10);
6567
for (let k = n; k !== 0; ) {
6668
t = shiftDate(t, Math.sign(k));
@@ -69,6 +71,45 @@ function walkShift(d: string, n: number): string {
6971
return t + d.slice(10);
7072
}
7173

74+
// The same walk taken once: every calendar day from CALENDAR[0] to CALENDAR[1], stepped a day at a
75+
// time, numbering the working days in order. Every card date of every run date falls well inside it.
76+
const CALENDAR = ['1960-01-01', '2040-12-31'] as const;
77+
let table: { days: string[]; atOrBefore: Map<string, number> } | undefined;
78+
function calendar() {
79+
if (table) return table;
80+
const days: string[] = [];
81+
const atOrBefore = new Map<string, number>();
82+
for (let d: string = CALENDAR[0]; d <= CALENDAR[1]; d = shiftDate(d, 1)) {
83+
if (isWorking(d)) days.push(d);
84+
atOrBefore.set(d, days.length - 1);
85+
}
86+
return (table = { days, atOrBefore });
87+
}
88+
/** The number of the working day on or before `d`. */
89+
function numberOf(d: string): number {
90+
const i = calendar().atOrBefore.get(d.slice(0, 10));
91+
if (i === undefined || i < 0) throw new Error(`${d} is outside the walked calendar ${CALENDAR.join('..')}`);
92+
return i;
93+
}
94+
/** Working day number `i`. */
95+
function dayOf(i: number): string {
96+
const d = calendar().days[i];
97+
if (d === undefined) throw new Error(`working day ${i} is outside the walked calendar ${CALENDAR.join('..')}`);
98+
return d;
99+
}
100+
/** The working day on or before `d`. */
101+
const onOrBefore = (d: string): string => dayOf(numberOf(d)) + d.slice(10);
102+
/** The working day on or after `d`. */
103+
const onOrAfter = (d: string): string => (isWorking(d) ? d : dayOf(numberOf(d) + 1) + d.slice(10));
104+
/** Working days from `from` to `to`, read off the walked calendar. */
105+
const walkCount = (from: string, to: string): number => numberOf(to) - numberOf(from);
106+
/** `d` moved `n` working days, read off the walked calendar: a weekend `d` counts from the next working day back, the last one on. */
107+
function walkShift(d: string, n: number): string {
108+
if (n === 0) return d;
109+
const i = numberOf(d);
110+
return dayOf((isWorking(d) || n > 0 ? i : i + 1) + n) + d.slice(10);
111+
}
112+
72113
/**
73114
* How the rebase must move `value`, a date of card `s` run on `runDate`, read independently of the
74115
* implementation: whole days, or on a `workingDays` card `asOf` onto `runDate` and every other date by
@@ -156,6 +197,25 @@ describe('whole working days', () => {
156197
for (const d of RUN_DATES) expect(workingDaysBetween('2026-09-30', d), d).toBe(walkCount('2026-09-30', onOrBefore(d)));
157198
expect([onWorkingDay('2026-11-13'), onWorkingDay('2026-11-14'), onWorkingDay('2026-11-15T09:00:00Z')]).toEqual([true, false, false]);
158199
});
200+
201+
it('the calendar walked once reads the same as stepping through it, across the epoch week, a year end and a leap day', () => {
202+
const around = (d: string) => Array.from({ length: 15 }, (_, k) => shiftDate(d, k - 7));
203+
const days = ['1970-01-01', '2026-12-31', '2028-02-29'].flatMap(around);
204+
days.push('2026-12-31T08:30:00Z', '2028-02-26T09:00:00Z');
205+
for (const d of days) {
206+
expect([onOrBefore(d), onOrAfter(d)], d).toEqual([stepOnOrBefore(d), stepOnOrAfter(d)]);
207+
for (const n of [-6, -1, 0, 1, 6]) expect(walkShift(d, n), `${d} + ${n}`).toBe(stepShift(d, n));
208+
}
209+
for (const [d, n] of [['1969-12-31', 7], ['2026-09-30', 380], ['2027-01-01', -380], ['2028-02-26', -260], ['2028-02-29', 260]] as const) {
210+
expect(walkShift(d, n), `${d} + ${n}`).toBe(stepShift(d, n));
211+
}
212+
for (const [from, to] of [['1969-12-27', '1970-01-05'], ['2026-09-30', '2028-03-31'], ['2028-03-04', '2026-12-26'], ['2028-02-26', '2028-03-04']] as const) {
213+
expect(walkCount(from, to), `${from} → ${to}`).toBe(stepCount(from, to));
214+
expect(walkCount(to, from), `${to} → ${from}`).toBe(stepCount(to, from));
215+
}
216+
expect(() => walkShift('1959-12-31', 1)).toThrow(/outside the walked calendar/);
217+
expect(() => walkShift('2040-12-31', 1)).toThrow(/outside the walked calendar/);
218+
});
159219
});
160220

161221
describe('the dates an ask writes in its text move with the card', () => {

0 commit comments

Comments
 (0)