| 1 | // dates.test.mjs — the editor writes every date as the server does |
| 2 | // (level-up build note 85): dates.mjs's formatWith over dates.json |
| 3 | // equals Go's internal/mapa/locale on every case in |
| 4 | // internal/mapa/locale/testdata/date_labels.json, in every offered |
| 5 | // locale. make golden rewrites the cases. |
| 6 | |
| 7 | import { test } from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import { readFileSync } from 'node:fs' |
| 10 | import { dirname, join } from 'node:path' |
| 11 | import { fileURLToPath } from 'node:url' |
| 12 | import { dayLabel, firstDay, formatWith, monthTitle, order, useDates, weekHeads } from './dates.mjs' |
| 13 | |
| 14 | const here = dirname(fileURLToPath(import.meta.url)) |
| 15 | const dir = join(here, '..', 'internal', 'mapa', 'locale') |
| 16 | const data = JSON.parse(readFileSync(join(dir, 'dates.json'), 'utf8')) |
| 17 | const cases = JSON.parse(readFileSync(join(dir, 'testdata', 'date_labels.json'), 'utf8')) |
| 18 | |
| 19 | // dataFor is the island's dates for a locale (locale.Client). |
| 20 | export function dataFor (code) { |
| 21 | return { code, ...data.locales[code] } |
| 22 | } |
| 23 | |
| 24 | test('every offered locale has cases', () => { |
| 25 | for (const code of data.offered) { |
| 26 | assert.ok(cases.some(each => each.locale === code), code) |
| 27 | } |
| 28 | }) |
| 29 | |
| 30 | test('dates.mjs writes every date Go writes, byte for byte', () => { |
| 31 | const wrong = [] |
| 32 | for (const { locale, kind, at, want } of cases) { |
| 33 | const [date, time] = at.split('T') |
| 34 | const [year, month, day] = date.split('-').map(Number) |
| 35 | const [hour, minute] = time.split(':').map(Number) |
| 36 | const weekday = new Date(Date.UTC(year, month - 1, day)).getUTCDay() |
| 37 | const got = formatWith(data.locales[locale].patterns[kind], |
| 38 | { year, month, day, weekday, hour, minute }) |
| 39 | if (got !== want) wrong.push(`${locale} ${kind} ${at}: ${JSON.stringify(got)}, Go ${JSON.stringify(want)}`) |
| 40 | } |
| 41 | assert.deepEqual(wrong, []) |
| 42 | }) |
| 43 | |
| 44 | test('a chip, the month and the week, in three locales', () => { |
| 45 | useDates(dataFor('en-US')) |
| 46 | assert.equal(dayLabel('2026-11-20', 2026), 'Friday, November 20') |
| 47 | assert.equal(dayLabel('2025-01-01', 2026), 'Wednesday, January 1, 2025') |
| 48 | assert.equal(monthTitle('2026-11-20'), 'November 2026') |
| 49 | assert.equal(firstDay(), 0) |
| 50 | assert.equal(order(), 'mdy') |
| 51 | assert.equal(weekHeads().map(head => head.letter).join(''), 'SMTWTFS') |
| 52 | useDates(dataFor('de')) |
| 53 | assert.equal(dayLabel('2026-11-20', 2026), 'Freitag, 20. November') |
| 54 | assert.equal(monthTitle('2026-03-02'), 'März 2026') |
| 55 | assert.equal(weekHeads()[0].name, 'Montag') |
| 56 | useDates(dataFor('en-GB')) |
| 57 | assert.equal(dayLabel('2026-11-20', 2026), 'Friday 20 November') |
| 58 | assert.equal(firstDay(), 1) |
| 59 | useDates(null) |
| 60 | assert.equal(dayLabel('2026-11-20', 2026), '2026-11-20') |
| 61 | }) |
| 62 | |
| 63 | // dates.json is CLDR's, not a hand edit (review round 8, L2): with the |
| 64 | // ICU it was read from, regenerating it gives the committed bytes. A |
| 65 | // Node carrying another CLDR cannot say, and skips loudly. |
| 66 | test('dates.json is what ICU gives, byte for byte', async t => { |
| 67 | const { generate, encode } = await import('./gen-dates.mjs') |
| 68 | if (process.versions.cldr !== data.source.cldr || process.versions.icu !== data.source.icu) { |
| 69 | 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`) |
| 70 | return |
| 71 | } |
| 72 | assert.equal(encode(generate()), readFileSync(join(dir, 'dates.json'), 'utf8')) |
| 73 | }) |
| 74 | |