import {describe, it, beforeEach, expect} from 'vitest'; import {DateTimeFormatTimeZone, Instant} from '@/'; import type {TimeZone} from '@clarify/zone-data'; import importer from '@clarify/zone-data/importer'; import {getPossibleMilliseconds} from '@/utilities.js'; describe('TimeZone', () => { describe('Precalculated', () => { let timeZone!: TimeZone; beforeEach(async () => { timeZone = await importer['Europe/Paris'](); }); describe('#getPossibleMilliseconds', () => { it('finds the next transition (at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000, 1729990800000]); }); it('finds the next transition (not at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000 + 3456).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987203456, 1729990803456]); }); it('finds the next transition (outside of range)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000 - 1).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000 - 1]); }); it('finds the previous transition (at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729990800000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000, 1729990800000]); }); it('finds the previous transition (outside of edge)', async () => { let now = Instant.fromEpochMilliseconds(1729994400000 + 1).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729994400000 + 1]); }); it('finds the previous transition (not at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729990800000 - 3456).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729990800000 - 3456, 1729994400000 - 3456]); }); }); }); describe('DateFormatter', () => { let timeZone!: DateTimeFormatTimeZone; beforeEach(async () => { timeZone = new DateTimeFormatTimeZone('Europe/Berlin'); }); describe('#getPossibleMilliseconds', () => { it('finds the next transition (at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000, 1729990800000]); }); it('finds the next transition (not at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000 + 3456).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987203456, 1729990803456]); }); it('finds the next transition (outside of range)', async () => { let now = Instant.fromEpochMilliseconds(1729987200000 - 1).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000 - 1]); }); it('finds the previous transition (at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729990800000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729987200000, 1729990800000]); }); it('finds the previous transition (outside of edge)', async () => { let now = Instant.fromEpochMilliseconds(1729994400000 + 1).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729994400000 + 1]); }); it('finds the previous transition (not at edge)', async () => { let now = Instant.fromEpochMilliseconds(1729990800000 - 3456).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1729990800000 - 3456, 1729994400000 - 3456]); }); it('finds the next transition (jump forward)', async () => { let now = Instant.fromEpochMilliseconds(1743292800000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1743292800000]); }); it('finds the previous transition (jump forward)', async () => { let now = Instant.fromEpochMilliseconds(1743296400000).toZonedDateTimeISO(timeZone); let timestamps = getPossibleMilliseconds(now.epochMilliseconds, timeZone); expect(timestamps).toEqual([1743296400000]); }); }); }); });