// datepicker.test.mjs — the date picker's arithmetic, and the chip it // inserts (build note 83). The picker itself, its keys and axe are in // internal/mapatest/datepicker_browser_test.go; L3 over the corpus is in // fidelity.test.mjs. import { test } from 'node:test' import assert from 'node:assert/strict' import { TextSelection } from 'prosemirror-state' import { undo } from 'prosemirror-history' import { editor, schema } from './testkit.mjs' import { serialise } from './serialize.mjs' import { mount, prepareSave } from './fidelity.mjs' import { insertDate, blockCommands, dateInsertion, today } from './commands.mjs' import { refusals } from './pagelink.mjs' import { blocks } from './slash.mjs' import { addDays, addMonths, monthGrid, monthTitle, moveFor } from './datepicker.mjs' import { useDates } from './dates.mjs' import { readFileSync } from 'node:fs' // The island's dates for a locale (locale.Client), from dates.json. const dates = JSON.parse(readFileSync(new URL('../internal/mapa/locale/dates.json', import.meta.url), 'utf8')) const datesFor = code => ({ code, ...dates.locales[code] }) useDates(datesFor('en-IE')) test('days and weeks move across months and years', () => { assert.equal(addDays('2026-11-16', 4), '2026-11-20') assert.equal(addDays('2026-11-30', 1), '2026-12-01') assert.equal(addDays('2026-12-31', 1), '2027-01-01') assert.equal(addDays('2028-02-28', 1), '2028-02-29') assert.equal(addDays('2026-03-01', -1), '2026-02-28') assert.equal(addDays('9999-12-31', 1), null) }) test('a month keeps the day, or takes the month\'s last', () => { assert.equal(addMonths('2026-11-16', 1), '2026-12-16') assert.equal(addMonths('2026-01-31', 1), '2026-02-28') assert.equal(addMonths('2028-01-31', 1), '2028-02-29') assert.equal(addMonths('2026-03-31', -1), '2026-02-28') assert.equal(addMonths('2026-12-05', 1), '2027-01-05') assert.equal(addMonths('2026-01-05', -1), '2025-12-05') assert.equal(addMonths('2028-02-29', 12), '2029-02-28') }) test('the grid is six weeks from Monday, as the month calendar draws it', () => { const november = monthGrid('2026-11-20') assert.equal(november.length, 42) assert.equal(november[0], '2026-10-26') assert.equal(november[6], '2026-11-01') assert.equal(november[41], '2026-12-06') // A month starting on a Monday starts the grid. assert.equal(monthGrid('2027-02-10')[0], '2027-02-01') assert.equal(monthTitle('2026-11-20'), 'November 2026') }) // build note 85: the week starts on the reader's locale's first day, // and the month is named in its language. test('the grid starts the week where the locale does', () => { useDates(datesFor('en-US')) try { const november = monthGrid('2026-11-20') assert.equal(november[0], '2026-11-01') assert.equal(november[41], '2026-12-12') assert.equal(monthGrid('2027-02-10')[0], '2027-01-31') useDates(datesFor('fr')) assert.equal(monthGrid('2026-11-20')[0], '2026-10-26') assert.equal(monthTitle('2026-11-20'), 'novembre 2026') } finally { useDates(datesFor('en-IE')) } }) test('the keys move a day, a week, a month and a year', () => { const from = '2026-11-16' assert.equal(moveFor('ArrowRight', from), '2026-11-17') assert.equal(moveFor('ArrowLeft', from), '2026-11-15') assert.equal(moveFor('ArrowDown', from), '2026-11-23') assert.equal(moveFor('ArrowUp', from), '2026-11-09') assert.equal(moveFor('PageDown', from), '2026-12-16') assert.equal(moveFor('PageUp', from), '2026-10-16') assert.equal(moveFor('PageDown', from, true), '2027-11-16') assert.equal(moveFor('Home', from), null) assert.equal(moveFor('a', from), null) }) // put places the caret after text's first occurrence and inserts date. function put (body, after, date) { const { view, fidelity } = editor(body) let at = null view.state.doc.descendants((node, pos) => { if (at !== null || !node.isTextblock) return at === null const index = node.textContent.indexOf(after) if (index >= 0) at = pos + 1 + index + after.length return false }) view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, at))) assert.equal(view.run(insertDate(date)), true) const saved = prepareSave(view, fidelity).body const again = mount(schema, saved) assert.equal(serialise(again.doc, again.fidelity), saved, 'the saved body reads back as written') return { view, fidelity, saved } } test('the picked day is the chip [[ makes for a date', () => { assert.equal(put('Call Ana on ', 'on ', '2026-11-20').saved, 'Call Ana on [[2026-11-20]]') assert.equal(put('# Plan\n\n- one\n- two', 'one', '2026-11-20').saved, '# Plan\n\n- one[[2026-11-20]]\n- two') const { view, fidelity } = put('Met.\n\nSee soon ^ab', 'See ', '2027-01-04') assert.equal(serialise(view.state.doc, fidelity), 'Met.\n\nSee [[2027-01-04]] soon ^ab') view.run(undo) assert.equal(serialise(view.state.doc, fidelity), 'Met.\n\nSee soon ^ab') }) test('Insert → Today\'s date still puts today in at once', () => { const { view, fidelity } = editor('Today: ') view.dispatch(view.state.tr.setSelection(TextSelection.atEnd(view.state.doc))) view.run(blockCommands(schema)['insert-date']) assert.match(prepareSave(view, fidelity).body, /^Today: \[\[\d{4}-\d{2}-\d{2}\]\]$/u) }) test('/ offers Date, the picker, and Today\'s date beside it', () => { const rows = blocks.map(([label, , name]) => `${label}=${name}`) assert.ok(rows.includes('Date=insert-date-pick'), rows.join()) assert.ok(rows.includes('Today’s date=insert-date'), rows.join()) assert.ok(rows.indexOf('Date=insert-date-pick') < rows.indexOf('Today’s date=insert-date')) }) // Review M1: right after a ! the chip would be an embed, and the ! would // go from the reading. A space goes first; where even that would not // read back, the chip is refused, as a letter there would not read back // either. test('after a ! the chip goes in after a space, never as an embed', () => { assert.equal(put('Look!', '!', '2026-11-20').saved, 'Look! [[2026-11-20]]') assert.equal(put('x! then', 'x!', '2026-11-20').saved, 'x! [[2026-11-20]] then') const { view } = editor('Look!') view.dispatch(view.state.tr.setSelection(TextSelection.atEnd(view.state.doc))) assert.equal(dateInsertion(view.state, '2026-11-20').spaced, true) }) test('where the Markdown would read the chip differently, it is refused', () => { const { view, fidelity } = editor('`` foo ` bar ``') view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, 2))) assert.deepEqual(dateInsertion(view.state, '2026-11-20'), { refuse: refusals.date }) assert.equal(view.run(insertDate('2026-11-20')), false) assert.equal(prepareSave(view, fidelity).body, '`` foo ` bar ``') // In code, or with no text block, there is no chip and no refusal. const code = editor('```\ncode\n```').view code.dispatch(code.state.tr.setSelection(TextSelection.create(code.state.doc, 2))) assert.equal(dateInsertion(code.state, '2026-11-20'), null) }) // Review M3: over a selection the chip goes at its end, and the words stay. test('over a selection the chip goes after it and the words stay', () => { const { view, fidelity } = editor('Call Ana today') view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, 1, 9))) assert.equal(view.run(insertDate('2026-11-20')), true) assert.equal(prepareSave(view, fidelity).body, 'Call Ana[[2026-11-20]] today') assert.equal(view.state.selection.empty, true) const { view: all, fidelity: allFidelity } = editor('One.\n\nTwo.') all.dispatch(all.state.tr.setSelection(TextSelection.create(all.state.doc, 1, all.state.doc.content.size - 1))) all.run(blockCommands(schema)['insert-date']) assert.match(prepareSave(all, allFidelity).body, /^One\.\n\nTwo\.\[\[\d{4}-\d{2}-\d{2}\]\]$/u) }) // Review L2: today is the viewer's zone's (), not the // browser's, and is read each time. test('today is the day in the viewer\'s zone', () => { const instant = new Date(Date.UTC(2026, 10, 16, 23, 30)) assert.equal(today(instant, 'Europe/Dublin'), '2026-11-16') assert.equal(today(instant, 'Asia/Tokyo'), '2026-11-17') assert.equal(today(instant, 'America/Los_Angeles'), '2026-11-16') assert.equal(today(new Date(Date.UTC(2026, 10, 17, 9)), 'America/Los_Angeles'), '2026-11-17') assert.match(today(instant, 'Not/AZone'), /^\d{4}-\d{2}-\d{2}$/u) })