Files
faunosteo/packages/pdf/tests/r4.dates-oracles.test.ts
T

96 lines
2.6 KiB
TypeScript

/**
* R4.1 freeze O5 (plan 2026-10-17 §B.5.1): `formatDate` (render/dates.ts)
* as the TypeScript engine prints it with Node's ICU, for fr and en, over a
* grid of `YYYY-MM-DD` inputs (years 0000 to 9999 with the `Date.UTC`
* 0-99 → 1900-1999 mapping, months 00 to 13, days 00 to 32, so rolled-over
* dates come back unchanged) and strings the pattern refuses.
* `MAKE_R4_ORACLES=1` writes `crates/core/tests/oracles/r4/dates.json`.
*/
import { describe, it } from 'vitest'
import { formatDate } from '../src/render/dates'
import { environment, freezeJson } from './helpers/r4-oracle-io'
const YEARS = [
'0000',
'0001',
'0050',
'0099',
'0100',
'0999',
'1000',
'1900',
'1904',
'1999',
'2000',
'2024',
'2026',
'9999',
]
const pad2 = (n: number): string => String(n).padStart(2, '0')
const MONTHS = Array.from({ length: 14 }, (_, i) => pad2(i))
const DAYS = Array.from({ length: 33 }, (_, i) => pad2(i))
/** Inputs `^\d{4}-\d{2}-\d{2}$` refuses (printed unchanged), plus near misses it accepts. */
const OTHER_INPUTS = [
'2026-1-5',
'26-01-05',
'',
'2026-01-05T00:00',
' 2026-01-05',
'2026-01-05 ',
'2026 -01-05',
'\u00a02026-01-05',
'2026-01-05',
'+2026-01-05',
'-2026-01-05',
'+002026-01-05',
'2026/01/05',
'2026-01-05\n',
'\n2026-01-05',
'20260105',
'2026-001-05',
'abcd-ef-gh',
'١٢٣٤-٠١-٠٥',
'2026-01-05Z',
'10000-01-05',
'0000-00-00',
]
function buildGrid() {
const results: Record<string, { fr: string[]; en: string[] }> = {}
for (const year of YEARS) {
for (const month of MONTHS) {
const inputs = DAYS.map((day) => `${year}-${month}-${day}`)
results[`${year}-${month}`] = {
fr: inputs.map((input) => formatDate(input, 'fr')),
en: inputs.map((input) => formatDate(input, 'en')),
}
}
}
return results
}
function buildDates() {
return {
comment: [
'formatDate(input, locale) (packages/pdf/src/render/dates.ts), Intl.DateTimeFormat fr-FR and',
'en-US, day numeric, month long, year numeric, UTC. grid.results["YYYY-MM"].fr[d] is the',
'output for input "YYYY-MM-" + days[d] (d = 0..32); an output equal to its input means the',
'date was refused (rolled over or out of range). others: [input, fr, en].',
],
environment: environment(),
grid: { years: YEARS, months: MONTHS, days: DAYS, results: buildGrid() },
others: OTHER_INPUTS.map((input) => [
input,
formatDate(input, 'fr'),
formatDate(input, 'en'),
]),
}
}
describe('R4 date oracles (TypeScript freeze)', () => {
it('matches the frozen formatDate outputs', () => {
freezeJson('dates.json', buildDates())
})
})