Tofu/tests/dates/listWeekDates.test.js

41 lines
1.6 KiB
JavaScript

// A fixed time zone with daylight saving makes the "no time drift" case deterministic.
// node --test runs each test file in its own process, so this affects nothing else.
process.env.TZ = 'Europe/Paris'
import test from 'node:test'
import assert from 'node:assert/strict'
import { listWeekDates } from '#tofu/tools/dates/listWeekDates.js'
test('returns seven dates from Monday to Sunday', () => {
const dates = listWeekDates(new Date(2026, 8, 21))
assert.equal(dates.length, 7)
assert.deepEqual(dates.map((date) => date.getDate()), [21, 22, 23, 24, 25, 26, 27])
})
test('crosses the month and year boundary', () => {
const dates = listWeekDates(new Date(2025, 11, 29))
assert.deepEqual(dates.map((date) => date.getDate()), [29, 30, 31, 1, 2, 3, 4])
assert.equal(dates[6].getFullYear(), 2026)
assert.equal(dates[6].getMonth(), 0)
})
test('keeps every date at the same local time of day across a daylight saving change', () => {
// In Europe/Paris the clocks move backward on Sunday 2026-10-25.
const dates = listWeekDates(new Date(2026, 9, 19))
assert.deepEqual(dates.map((date) => date.getHours()), [0, 0, 0, 0, 0, 0, 0])
assert.equal(dates[6].getDate(), 25)
})
test('returns distinct instances, never the received date', () => {
const weekStart = new Date(2026, 8, 21)
const dates = listWeekDates(weekStart)
assert.notEqual(dates[0], weekStart)
assert.equal(new Set(dates).size, 7)
})
test('does not mutate the received date', () => {
const weekStart = new Date(2026, 8, 21, 6, 5)
const before = weekStart.getTime()
listWeekDates(weekStart)
assert.equal(weekStart.getTime(), before)
})