Tofu/tests/planning/createEmptyWeekPlanning.test.js

54 lines
1.9 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { createEmptyWeekPlanning } from '#tofu/tools/planning/createEmptyWeekPlanning.js'
function buildWeekDates() {
// Monday 2026-09-21 to Sunday 2026-09-27, local time.
return [21, 22, 23, 24, 25, 26, 27].map((day) => new Date(2026, 8, day))
}
test('builds one empty day per date, keyed by local date', () => {
const planning = createEmptyWeekPlanning('2026-W39', buildWeekDates())
assert.equal(planning.weekId, '2026-W39')
assert.equal(planning.days.length, 7)
assert.deepEqual(planning.days[0], { date: '2026-09-21', dish: '', ingredients: '' })
assert.deepEqual(planning.days[6], { date: '2026-09-27', dish: '', ingredients: '' })
})
test('keeps the dates in the order they were given', () => {
const planning = createEmptyWeekPlanning('2026-W39', buildWeekDates())
const keys = planning.days.map((day) => day.date)
assert.deepEqual(keys, [
'2026-09-21',
'2026-09-22',
'2026-09-23',
'2026-09-24',
'2026-09-25',
'2026-09-26',
'2026-09-27',
])
})
test('uses the local day even late in the evening', () => {
// 23:30 local would roll over to the next day through toISOString.
const planning = createEmptyWeekPlanning('2026-W39', [new Date(2026, 8, 21, 23, 30)])
assert.equal(planning.days[0].date, '2026-09-21')
})
test('an empty list of dates gives an empty week', () => {
const planning = createEmptyWeekPlanning('2026-W39', [])
assert.deepEqual(planning, { weekId: '2026-W39', days: [] })
})
test('does not mutate the received dates', () => {
const dates = buildWeekDates()
const times = dates.map((date) => date.getTime())
createEmptyWeekPlanning('2026-W39', dates)
assert.deepEqual(dates.map((date) => date.getTime()), times)
})
test('each day is a distinct object', () => {
const planning = createEmptyWeekPlanning('2026-W39', buildWeekDates())
planning.days[0].dish = 'Gnocchis à la sauge'
assert.equal(planning.days[1].dish, '')
})