Files
Rosea/packages/engine/test/localDay.test.ts

113 lines
4.7 KiB
TypeScript

import fc from 'fast-check';
import { describe, expect, it } from 'vitest';
import { addDays, isLocalDay, localDay } from '../src/time/localDay.js';
const at = (iso: string): number => Date.parse(iso);
describe('localDay — golden cases', () => {
it.each([
// Prague, 2026 spring-forward day (29 March, 02:00 CET -> 03:00 CEST).
['2026-03-28T23:30:00Z', 'Europe/Prague', '2026-03-29'], // 00:30 CET, first half-hour of the short day
['2026-03-29T21:59:00Z', 'Europe/Prague', '2026-03-29'], // 23:59 CEST, last minute of it
['2026-03-29T22:00:00Z', 'Europe/Prague', '2026-03-30'],
// Prague, 2026 fall-back day (25 October, 03:00 CEST -> 02:00 CET), 25 hours long.
['2026-10-24T22:00:00Z', 'Europe/Prague', '2026-10-25'],
['2026-10-25T22:59:00Z', 'Europe/Prague', '2026-10-25'], // 23:59 CET
['2026-10-25T23:00:00Z', 'Europe/Prague', '2026-10-26'],
// Southern hemisphere: Sydney is ahead of UTC, so its morning is UTC's yesterday.
['2026-11-01T21:00:00Z', 'Australia/Sydney', '2026-11-02'],
// Reykjavík has no DST and sits on UTC: the one zone where UTC days are right.
['2026-06-21T23:59:00Z', 'Atlantic/Reykjavik', '2026-06-21'],
// The edges of the clock face.
['2026-01-01T10:00:00Z', 'Pacific/Kiritimati', '2026-01-02'], // UTC+14
['2026-01-01T10:00:00Z', 'Etc/GMT+12', '2025-12-31'] // UTC-12
])('%s in %s is %s', (iso, zone, expected) => {
expect(localDay(at(iso), zone)).toBe(expected);
});
it('Monday evening in Anchorage is Monday, though UTC already says Tuesday', () => {
// 2026-10-26 is a Monday. 20:00 AKDT = 04:00 UTC on Tuesday.
const mondayEvening = at('2026-10-27T04:00:00Z');
expect(localDay(mondayEvening, 'America/Anchorage')).toBe('2026-10-26');
expect(new Date(mondayEvening).toISOString().slice(0, 10)).toBe('2026-10-27'); // the trap, measured
});
it('just after midnight on Tuesday in Prague is Tuesday, though UTC still says Monday', () => {
const tuesdayNight = at('2026-10-26T23:30:00Z'); // 00:30 CET on Tuesday 27th
expect(localDay(tuesdayNight, 'Europe/Prague')).toBe('2026-10-27');
});
});
describe('localDay — refusals', () => {
it('throws on an unknown zone instead of falling back to UTC', () => {
expect(() => localDay(0, 'Europe/Atlantis')).toThrow(RangeError);
});
it('throws on a non-finite time', () => {
expect(() => localDay(Number.NaN, 'Europe/Prague')).toThrow(RangeError);
expect(() => localDay(Number.POSITIVE_INFINITY, 'Europe/Prague')).toThrow(RangeError);
});
});
describe('localDay — properties', () => {
const ZONES = [
'Europe/Prague',
'Atlantic/Reykjavik',
'Europe/Oslo', // Tromsø's zone
'America/Guayaquil', // Quito's zone
'Australia/Sydney',
'America/Anchorage',
'Pacific/Kiritimati',
'Etc/GMT+12'
] as const;
// 2000-01-01 .. 2100-01-01: every year a player could plausibly be in.
const instant = fc.integer({ min: Date.UTC(2000, 0, 1), max: Date.UTC(2100, 0, 1) });
const zone = fc.constantFrom(...ZONES);
const HOUR = 3_600_000;
it('always returns a well-formed day', () => {
fc.assert(fc.property(instant, zone, (t, z) => isLocalDay(localDay(t, z))));
});
it('never goes backwards in time, across DST changes included', () => {
fc.assert(
fc.property(instant, fc.integer({ min: 0, max: 72 * HOUR }), zone, (t, dt, z) => {
return localDay(t, z) <= localDay(t + dt, z);
})
);
});
it('always advances within 26 hours (no local day is longer than 25)', () => {
fc.assert(fc.property(instant, zone, (t, z) => localDay(t, z) < localDay(t + 26 * HOUR, z)));
});
it('is deterministic: the same arguments give the same day', () => {
fc.assert(fc.property(instant, zone, (t, z) => localDay(t, z) === localDay(t, z)));
});
});
describe('addDays — a calendar step, no zone', () => {
it.each([
['2026-03-28', 1, '2026-03-29'], // across a DST change: still one day
['2026-12-31', 1, '2027-01-01'],
['2028-02-28', 1, '2028-02-29'], // leap year
['2026-03-01', -1, '2026-02-28'],
['2026-06-21', 0, '2026-06-21']
])('%s + %s = %s', (day, n, expected) => {
expect(addDays(day, n)).toBe(expected);
});
it('refuses a day that is not a LocalDay, and a part of a day', () => {
expect(() => addDays('2026-6-1', 1)).toThrow(/not a LocalDay/);
expect(() => addDays('2026-06-01', 0.5)).toThrow(/whole number/);
});
it('stepping forward and back again returns the same day', () => {
fc.assert(fc.property(fc.integer({ min: 0, max: 3000 }), fc.integer({ min: -400, max: 400 }), (k, n) => {
const day = addDays('2025-01-01', k);
return addDays(addDays(day, n), -n) === day && isLocalDay(addDays(day, n));
}));
});
});