|
| 1 | +/** |
| 2 | + * Zone-aware instant construction — issue #355. |
| 3 | + * |
| 4 | + * The wizard collects a date, a time, and an IANA timezone. Those three must |
| 5 | + * combine into one instant. Every existing site does `new Date("YYYY-MM-DDTHH:mm")`, |
| 6 | + * which JavaScript reads in the BROWSER's zone, so the declared timezone is |
| 7 | + * ignored and the same inputs produce different runs by operator location. |
| 8 | + * |
| 9 | + * No date library exists in this package, deliberately — these use Intl, the |
| 10 | + * same mechanism dateHelpers.ts already relies on. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect } from 'vitest'; |
| 14 | +import { resolveZonedInstant } from '../zonedInstant.js'; |
| 15 | + |
| 16 | +describe('resolveZonedInstant', () => { |
| 17 | + it('resolves a summer mountain-daylight time to the right instant', () => { |
| 18 | + // 2026-08-04 12:00 MDT (UTC-6) === 18:00Z. Vita's ignition. |
| 19 | + const instant = resolveZonedInstant('2026-08-04', '12:00', 'America/Edmonton'); |
| 20 | + expect(instant.toISOString()).toBe('2026-08-04T18:00:00.000Z'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('resolves a winter mountain-standard time, offset and all', () => { |
| 24 | + // 2026-01-04 12:00 MST (UTC-7) === 19:00Z. The offset differs from summer, |
| 25 | + // which a fixed offset would get wrong half the year. |
| 26 | + const instant = resolveZonedInstant('2026-01-04', '12:00', 'America/Edmonton'); |
| 27 | + expect(instant.toISOString()).toBe('2026-01-04T19:00:00.000Z'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('handles a half-hour offset zone', () => { |
| 31 | + // Newfoundland daylight time is UTC-2:30. |
| 32 | + const instant = resolveZonedInstant('2026-08-04', '12:00', 'America/St_Johns'); |
| 33 | + expect(instant.toISOString()).toBe('2026-08-04T14:30:00.000Z'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('is the identity for UTC', () => { |
| 37 | + const instant = resolveZonedInstant('2026-08-04', '12:00', 'UTC'); |
| 38 | + expect(instant.toISOString()).toBe('2026-08-04T12:00:00.000Z'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('resolves an eastern-hemisphere zone', () => { |
| 42 | + // Tokyo is UTC+9 year round. |
| 43 | + const instant = resolveZonedInstant('2026-08-04', '12:00', 'Asia/Tokyo'); |
| 44 | + expect(instant.toISOString()).toBe('2026-08-04T03:00:00.000Z'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('produces different instants for the same wall clock in different zones', () => { |
| 48 | + // The whole point of #355: an Ontario duty officer and an NWT one entering |
| 49 | + // identical wizard values must get the fire's zone, not their own. |
| 50 | + const edmonton = resolveZonedInstant('2026-08-04', '12:00', 'America/Edmonton'); |
| 51 | + const toronto = resolveZonedInstant('2026-08-04', '12:00', 'America/Toronto'); |
| 52 | + |
| 53 | + expect(edmonton.getTime()).not.toBe(toronto.getTime()); |
| 54 | + expect(edmonton.getTime() - toronto.getTime()).toBe(2 * 60 * 60 * 1000); |
| 55 | + }); |
| 56 | + |
| 57 | + it('crosses a DST boundary correctly on the day the clocks go forward', () => { |
| 58 | + // 2026-03-08 is the US/Canada spring-forward date. 03:00 local is MDT. |
| 59 | + const instant = resolveZonedInstant('2026-03-08', '03:00', 'America/Edmonton'); |
| 60 | + expect(instant.toISOString()).toBe('2026-03-08T09:00:00.000Z'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('resolves a time on the day the clocks go back', () => { |
| 64 | + // 2026-11-01, fall-back. 12:00 local is MST (UTC-7) by midday. |
| 65 | + const instant = resolveZonedInstant('2026-11-01', '12:00', 'America/Edmonton'); |
| 66 | + expect(instant.toISOString()).toBe('2026-11-01T19:00:00.000Z'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('accepts a time with seconds', () => { |
| 70 | + const instant = resolveZonedInstant('2026-08-04', '12:30:45', 'UTC'); |
| 71 | + expect(instant.toISOString()).toBe('2026-08-04T12:30:45.000Z'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('defaults a missing time to midnight local', () => { |
| 75 | + const instant = resolveZonedInstant('2026-08-04', '', 'America/Edmonton'); |
| 76 | + expect(instant.toISOString()).toBe('2026-08-04T06:00:00.000Z'); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('fail-fast', () => { |
| 80 | + it('throws on an unknown timezone rather than silently using the browser zone', () => { |
| 81 | + expect(() => resolveZonedInstant('2026-08-04', '12:00', 'Mars/Olympus_Mons')).toThrow(/timezone/i); |
| 82 | + }); |
| 83 | + |
| 84 | + it('throws on a malformed date', () => { |
| 85 | + expect(() => resolveZonedInstant('not-a-date', '12:00', 'UTC')).toThrow(/date/i); |
| 86 | + }); |
| 87 | + |
| 88 | + it('throws on a malformed time', () => { |
| 89 | + expect(() => resolveZonedInstant('2026-08-04', '25:99', 'UTC')).toThrow(/time/i); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments