Files
Rosea/packages/engine/test/localDay.test.ts
T
mnovak 49c8def037 [repo] Workspace: Scaffold Phase 0 with schema, engine, content, ui and the PWA shell
pnpm + turbo workspace with the commands from CLAUDE.md §3. The contracts
from §6 as JSON Schemas with generated types; localDay as the one place a
day is computed; the forbidden-word list shared by lint:copy and the
library validator, with 13 negative fixtures; a Svelte 5 PWA shell built
under /Rosea/ with a meta CSP. E2E and offline suites run against the
built bundle; the offline one closes the server rather than emulating.

Analysis: .agents/features/phase0-workspace.md
2026-11-01 00:27:33 +01:00

89 lines
3.7 KiB
TypeScript

import fc from 'fast-check';
import { describe, expect, it } from 'vitest';
import { 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)));
});
});