import { windowStart, WINDOW_MS } from '../../src/intake/capacity'; const NOW = new Date('2026-09-01T12:00:00.000Z'); const DAY_AGO = new Date(NOW.getTime() - WINDOW_MS); describe('windowStart', () => { it('is 24 hours ago when there has been no reset', () => { expect(windowStart(NOW, '').toISOString()).toBe(DAY_AGO.toISOString()); }); // A reset inside the window is the point of it: it forgives what came before // without deleting anything. it('is the reset when the reset is more recent', () => { const reset = '2026-09-01T09:00:00.000Z'; expect(windowStart(NOW, reset).toISOString()).toBe(reset); }); // An old reset must not widen the window beyond 24 hours, which would make // the ceiling stricter over time rather than rolling. it('ignores a reset older than the window', () => { expect(windowStart(NOW, '2026-08-01T00:00:00.000Z').toISOString()).toBe(DAY_AGO.toISOString()); }); // A malformed stored value must not produce an Invalid Date, which compares // false against everything and would silently disable the ceiling. it('falls back to 24 hours ago for an unparseable reset', () => { expect(windowStart(NOW, 'not-a-date').toISOString()).toBe(DAY_AGO.toISOString()); }); it('falls back for a reset in the future', () => { expect(windowStart(NOW, '2027-01-01T00:00:00.000Z').toISOString()).toBe(DAY_AGO.toISOString()); }); it('tolerates whitespace around an empty setting', () => { expect(windowStart(NOW, ' ').toISOString()).toBe(DAY_AGO.toISOString()); }); });