// dates.test.mjs — the editor writes every date as the server does // (level-up build note 85): dates.mjs's formatWith over dates.json // equals Go's internal/mapa/locale on every case in // internal/mapa/locale/testdata/date_labels.json, in every offered // locale. make golden rewrites the cases. import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { dayLabel, firstDay, formatWith, monthTitle, order, useDates, weekHeads } from './dates.mjs' const here = dirname(fileURLToPath(import.meta.url)) const dir = join(here, '..', 'internal', 'mapa', 'locale') const data = JSON.parse(readFileSync(join(dir, 'dates.json'), 'utf8')) const cases = JSON.parse(readFileSync(join(dir, 'testdata', 'date_labels.json'), 'utf8')) // dataFor is the island's dates for a locale (locale.Client). export function dataFor (code) { return { code, ...data.locales[code] } } test('every offered locale has cases', () => { for (const code of data.offered) { assert.ok(cases.some(each => each.locale === code), code) } }) test('dates.mjs writes every date Go writes, byte for byte', () => { const wrong = [] for (const { locale, kind, at, want } of cases) { const [date, time] = at.split('T') const [year, month, day] = date.split('-').map(Number) const [hour, minute] = time.split(':').map(Number) const weekday = new Date(Date.UTC(year, month - 1, day)).getUTCDay() const got = formatWith(data.locales[locale].patterns[kind], { year, month, day, weekday, hour, minute }) if (got !== want) wrong.push(`${locale} ${kind} ${at}: ${JSON.stringify(got)}, Go ${JSON.stringify(want)}`) } assert.deepEqual(wrong, []) }) test('a chip, the month and the week, in three locales', () => { useDates(dataFor('en-US')) assert.equal(dayLabel('2026-11-20', 2026), 'Friday, November 20') assert.equal(dayLabel('2025-01-01', 2026), 'Wednesday, January 1, 2025') assert.equal(monthTitle('2026-11-20'), 'November 2026') assert.equal(firstDay(), 0) assert.equal(order(), 'mdy') assert.equal(weekHeads().map(head => head.letter).join(''), 'SMTWTFS') useDates(dataFor('de')) assert.equal(dayLabel('2026-11-20', 2026), 'Freitag, 20. November') assert.equal(monthTitle('2026-03-02'), 'März 2026') assert.equal(weekHeads()[0].name, 'Montag') useDates(dataFor('en-GB')) assert.equal(dayLabel('2026-11-20', 2026), 'Friday 20 November') assert.equal(firstDay(), 1) useDates(null) assert.equal(dayLabel('2026-11-20', 2026), '2026-11-20') }) // dates.json is CLDR's, not a hand edit (review round 8, L2): with the // ICU it was read from, regenerating it gives the committed bytes. A // Node carrying another CLDR cannot say, and skips loudly. test('dates.json is what ICU gives, byte for byte', async t => { const { generate, encode } = await import('./gen-dates.mjs') if (process.versions.cldr !== data.source.cldr || process.versions.icu !== data.source.icu) { t.skip(`this Node links CLDR ${process.versions.cldr} (ICU ${process.versions.icu}); dates.json came from CLDR ${data.source.cldr} (ICU ${data.source.icu}): not checked`) return } assert.equal(encode(generate()), readFileSync(join(dir, 'dates.json'), 'utf8')) })