Tofu/tests/dates/getWeekStart.test.js

53 lines
1.9 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 { getWeekStart } from '#tofu/tools/dates/getWeekStart.js'
test('returns the same day for a Monday', () => {
const weekStart = getWeekStart(new Date(2026, 8, 21, 14, 30))
assert.equal(weekStart.getFullYear(), 2026)
assert.equal(weekStart.getMonth(), 8)
assert.equal(weekStart.getDate(), 21)
})
test('walks back to the Monday of the same week', () => {
const weekStart = getWeekStart(new Date(2026, 8, 24))
assert.equal(weekStart.getDate(), 21)
})
test('puts a Sunday in the week that started six days earlier', () => {
const weekStart = getWeekStart(new Date(2026, 8, 27))
assert.equal(weekStart.getMonth(), 8)
assert.equal(weekStart.getDate(), 21)
})
test('crosses the month boundary backwards', () => {
const weekStart = getWeekStart(new Date(2026, 9, 2))
assert.equal(weekStart.getMonth(), 8)
assert.equal(weekStart.getDate(), 28)
})
test('sets the time to local midnight', () => {
const weekStart = getWeekStart(new Date(2026, 8, 23, 23, 59, 59, 999))
assert.equal(weekStart.getHours(), 0)
assert.equal(weekStart.getMinutes(), 0)
assert.equal(weekStart.getSeconds(), 0)
assert.equal(weekStart.getMilliseconds(), 0)
})
test('stays at local midnight across a daylight saving change', () => {
// In Europe/Paris the clocks move forward on Sunday 2026-03-29.
const weekStart = getWeekStart(new Date(2026, 2, 29, 12))
assert.equal(weekStart.getDate(), 23)
assert.equal(weekStart.getHours(), 0)
})
test('does not mutate the received date', () => {
const date = new Date(2026, 8, 24, 11, 15)
const before = date.getTime()
getWeekStart(date)
assert.equal(date.getTime(), before)
})