| 1 | // typeddate.test.mjs — the date picker's typed dates answer what ⌘K's |
| 2 | // do: every case in internal/mapa/testdata/typed_dates.json, which Go's |
| 3 | // TestTypedDatesMatchTheSharedCases reads too (build note 83). |
| 4 | |
| 5 | import { test } from 'node:test' |
| 6 | import assert from 'node:assert/strict' |
| 7 | import { readFileSync } from 'node:fs' |
| 8 | import { dirname, join } from 'node:path' |
| 9 | import { fileURLToPath } from 'node:url' |
| 10 | import { typedDate } from './typeddate.mjs' |
| 11 | import { order } from './dates.mjs' |
| 12 | |
| 13 | const here = dirname(fileURLToPath(import.meta.url)) |
| 14 | const shared = JSON.parse(readFileSync( |
| 15 | join(here, '..', 'internal', 'mapa', 'testdata', 'typed_dates.json'), 'utf8')) |
| 16 | const dates = JSON.parse(readFileSync( |
| 17 | join(here, '..', 'internal', 'mapa', 'locale', 'dates.json'), 'utf8')) |
| 18 | |
| 19 | // A case naming a locale is read in its order; one naming none, in |
| 20 | // every offered locale's (build note 85). |
| 21 | test('the port answers every shared case as typeddate.go does', () => { |
| 22 | assert.ok(shared.cases.length >= 100, `only ${shared.cases.length} cases`) |
| 23 | const wrong = [] |
| 24 | for (const { today, query, want, locale, strict } of shared.cases) { |
| 25 | for (const code of locale ? [locale] : dates.offered) { |
| 26 | const reading = dates.locales[code] |
| 27 | const got = typedDate(query, today, order(reading), strict ? reading.separator : '') |
| 28 | if (got !== want) wrong.push({ code, today, query, want, got }) |
| 29 | } |
| 30 | } |
| 31 | assert.deepEqual(wrong, []) |
| 32 | }) |
| 33 | |
| 34 | test('numbers follow the locale: 12/11 is November in en-IE, December in en-US', () => { |
| 35 | assert.equal(typedDate('12/11', '2026-11-14', order(dates.locales['en-IE'])), '2026-11-12') |
| 36 | assert.equal(typedDate('12/11', '2026-11-14', order(dates.locales['en-US'])), '2026-12-11') |
| 37 | assert.equal(typedDate('12.11.', '2026-11-14', order(dates.locales.de)), '2026-11-12') |
| 38 | }) |
| 39 | |