🧪 Test timezone helpers and local collections

This commit is contained in:
2026-11-29 20:16:45 +00:00
parent 29f3cb9926
commit 1e5110f253
3 changed files with 402 additions and 1 deletions
+58 -1
View File
@@ -1,5 +1,15 @@
import { describe, it, expect } from 'vitest';
import { dayCapabilities, weekDayKeys, isWorkday, composeDeltaMs, decomposeDeltaMs } from './utils';
import {
dayCapabilities,
weekDayKeys,
isWorkday,
composeDeltaMs,
decomposeDeltaMs,
dayKeyForMs,
defaultDayForWeek,
endOfDayMs,
newId
} from './utils';
describe('dayCapabilities', () => {
const today = '2026-04-30';
@@ -114,3 +124,50 @@ describe('decomposeDeltaMs', () => {
expect(composeDeltaMs(sign, hours, minutes)).toBe(original);
});
});
describe('dayKeyForMs', () => {
it('UTC keeps the calendar day of the instant', () => {
expect(dayKeyForMs(Date.parse('2026-06-15T22:30:00Z'), 'UTC')).toBe('2026-06-15');
expect(dayKeyForMs(Date.parse('2026-06-15T23:59:59.999Z'), 'UTC')).toBe('2026-06-15');
});
it('Europe/Berlin is already the next day', () => {
expect(dayKeyForMs(Date.parse('2026-06-15T22:30:00Z'), 'Europe/Berlin')).toBe('2026-06-16');
});
it('Pacific/Auckland is already the next day', () => {
expect(dayKeyForMs(Date.parse('2026-06-15T12:30:00Z'), 'Pacific/Auckland')).toBe('2026-06-16');
});
});
describe('endOfDayMs', () => {
it('2026-06-16 in Europe/Berlin ends at 21:59:59.999Z', () => {
expect(endOfDayMs('2026-06-16', 'Europe/Berlin')).toBe(Date.parse('2026-06-16T21:59:59.999Z'));
});
it('DST day 2026-10-25 in Europe/Berlin ends at 22:59:59.999Z', () => {
expect(endOfDayMs('2026-10-25', 'Europe/Berlin')).toBe(Date.parse('2026-10-25T22:59:59.999Z'));
});
it('UTC ends at 23:59:59.999Z', () => {
expect(endOfDayMs('2026-06-16', 'UTC')).toBe(Date.parse('2026-06-16T23:59:59.999Z'));
});
});
describe('defaultDayForWeek', () => {
it('picks the given today when it is in the week, else Monday', () => {
expect(defaultDayForWeek('2026-W18', '2026-04-30')).toBe('2026-04-30');
expect(defaultDayForWeek('2026-W18', '2026-05-10')).toBe('2026-04-27');
});
});
describe('newId', () => {
it('is 15 chars [a-z0-9]', () => {
for (let i = 0; i < 100; i++) expect(newId()).toMatch(/^[a-z0-9]{15}$/);
});
it('is unique over 1000 draws', () => {
const ids = new Set(Array.from({ length: 1000 }, () => newId()));
expect(ids.size).toBe(1000);
});
});