| 1 | // datepicker.test.mjs — the date picker's arithmetic, and the chip it |
| 2 | // inserts (build note 83). The picker itself, its keys and axe are in |
| 3 | // internal/mapatest/datepicker_browser_test.go; L3 over the corpus is in |
| 4 | // fidelity.test.mjs. |
| 5 | |
| 6 | import { test } from 'node:test' |
| 7 | import assert from 'node:assert/strict' |
| 8 | import { TextSelection } from 'prosemirror-state' |
| 9 | import { undo } from 'prosemirror-history' |
| 10 | import { editor, schema } from './testkit.mjs' |
| 11 | import { serialise } from './serialize.mjs' |
| 12 | import { mount, prepareSave } from './fidelity.mjs' |
| 13 | import { insertDate, blockCommands, dateInsertion, today } from './commands.mjs' |
| 14 | import { refusals } from './pagelink.mjs' |
| 15 | import { blocks } from './slash.mjs' |
| 16 | import { addDays, addMonths, monthGrid, monthTitle, moveFor } from './datepicker.mjs' |
| 17 | import { useDates } from './dates.mjs' |
| 18 | import { readFileSync } from 'node:fs' |
| 19 | |
| 20 | // The island's dates for a locale (locale.Client), from dates.json. |
| 21 | const dates = JSON.parse(readFileSync(new URL('../internal/mapa/locale/dates.json', import.meta.url), 'utf8')) |
| 22 | const datesFor = code => ({ code, ...dates.locales[code] }) |
| 23 | useDates(datesFor('en-IE')) |
| 24 | |
| 25 | test('days and weeks move across months and years', () => { |
| 26 | assert.equal(addDays('2026-11-16', 4), '2026-11-20') |
| 27 | assert.equal(addDays('2026-11-30', 1), '2026-12-01') |
| 28 | assert.equal(addDays('2026-12-31', 1), '2027-01-01') |
| 29 | assert.equal(addDays('2028-02-28', 1), '2028-02-29') |
| 30 | assert.equal(addDays('2026-03-01', -1), '2026-02-28') |
| 31 | assert.equal(addDays('9999-12-31', 1), null) |
| 32 | }) |
| 33 | |
| 34 | test('a month keeps the day, or takes the month\'s last', () => { |
| 35 | assert.equal(addMonths('2026-11-16', 1), '2026-12-16') |
| 36 | assert.equal(addMonths('2026-01-31', 1), '2026-02-28') |
| 37 | assert.equal(addMonths('2028-01-31', 1), '2028-02-29') |
| 38 | assert.equal(addMonths('2026-03-31', -1), '2026-02-28') |
| 39 | assert.equal(addMonths('2026-12-05', 1), '2027-01-05') |
| 40 | assert.equal(addMonths('2026-01-05', -1), '2025-12-05') |
| 41 | assert.equal(addMonths('2028-02-29', 12), '2029-02-28') |
| 42 | }) |
| 43 | |
| 44 | test('the grid is six weeks from Monday, as the month calendar draws it', () => { |
| 45 | const november = monthGrid('2026-11-20') |
| 46 | assert.equal(november.length, 42) |
| 47 | assert.equal(november[0], '2026-10-26') |
| 48 | assert.equal(november[6], '2026-11-01') |
| 49 | assert.equal(november[41], '2026-12-06') |
| 50 | // A month starting on a Monday starts the grid. |
| 51 | assert.equal(monthGrid('2027-02-10')[0], '2027-02-01') |
| 52 | assert.equal(monthTitle('2026-11-20'), 'November 2026') |
| 53 | }) |
| 54 | |
| 55 | // build note 85: the week starts on the reader's locale's first day, |
| 56 | // and the month is named in its language. |
| 57 | test('the grid starts the week where the locale does', () => { |
| 58 | useDates(datesFor('en-US')) |
| 59 | try { |
| 60 | const november = monthGrid('2026-11-20') |
| 61 | assert.equal(november[0], '2026-11-01') |
| 62 | assert.equal(november[41], '2026-12-12') |
| 63 | assert.equal(monthGrid('2027-02-10')[0], '2027-01-31') |
| 64 | useDates(datesFor('fr')) |
| 65 | assert.equal(monthGrid('2026-11-20')[0], '2026-10-26') |
| 66 | assert.equal(monthTitle('2026-11-20'), 'novembre 2026') |
| 67 | } finally { |
| 68 | useDates(datesFor('en-IE')) |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | test('the keys move a day, a week, a month and a year', () => { |
| 73 | const from = '2026-11-16' |
| 74 | assert.equal(moveFor('ArrowRight', from), '2026-11-17') |
| 75 | assert.equal(moveFor('ArrowLeft', from), '2026-11-15') |
| 76 | assert.equal(moveFor('ArrowDown', from), '2026-11-23') |
| 77 | assert.equal(moveFor('ArrowUp', from), '2026-11-09') |
| 78 | assert.equal(moveFor('PageDown', from), '2026-12-16') |
| 79 | assert.equal(moveFor('PageUp', from), '2026-10-16') |
| 80 | assert.equal(moveFor('PageDown', from, true), '2027-11-16') |
| 81 | assert.equal(moveFor('Home', from), null) |
| 82 | assert.equal(moveFor('a', from), null) |
| 83 | }) |
| 84 | |
| 85 | // put places the caret after text's first occurrence and inserts date. |
| 86 | function put (body, after, date) { |
| 87 | const { view, fidelity } = editor(body) |
| 88 | let at = null |
| 89 | view.state.doc.descendants((node, pos) => { |
| 90 | if (at !== null || !node.isTextblock) return at === null |
| 91 | const index = node.textContent.indexOf(after) |
| 92 | if (index >= 0) at = pos + 1 + index + after.length |
| 93 | return false |
| 94 | }) |
| 95 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, at))) |
| 96 | assert.equal(view.run(insertDate(date)), true) |
| 97 | const saved = prepareSave(view, fidelity).body |
| 98 | const again = mount(schema, saved) |
| 99 | assert.equal(serialise(again.doc, again.fidelity), saved, 'the saved body reads back as written') |
| 100 | return { view, fidelity, saved } |
| 101 | } |
| 102 | |
| 103 | test('the picked day is the chip [[ makes for a date', () => { |
| 104 | assert.equal(put('Call Ana on ', 'on ', '2026-11-20').saved, 'Call Ana on [[2026-11-20]]') |
| 105 | assert.equal(put('# Plan\n\n- one\n- two', 'one', '2026-11-20').saved, |
| 106 | '# Plan\n\n- one[[2026-11-20]]\n- two') |
| 107 | const { view, fidelity } = put('Met.\n\nSee soon ^ab', 'See ', '2027-01-04') |
| 108 | assert.equal(serialise(view.state.doc, fidelity), 'Met.\n\nSee [[2027-01-04]] soon ^ab') |
| 109 | view.run(undo) |
| 110 | assert.equal(serialise(view.state.doc, fidelity), 'Met.\n\nSee soon ^ab') |
| 111 | }) |
| 112 | |
| 113 | test('Insert → Today\'s date still puts today in at once', () => { |
| 114 | const { view, fidelity } = editor('Today: ') |
| 115 | view.dispatch(view.state.tr.setSelection(TextSelection.atEnd(view.state.doc))) |
| 116 | view.run(blockCommands(schema)['insert-date']) |
| 117 | assert.match(prepareSave(view, fidelity).body, /^Today: \[\[\d{4}-\d{2}-\d{2}\]\]$/u) |
| 118 | }) |
| 119 | |
| 120 | test('/ offers Date, the picker, and Today\'s date beside it', () => { |
| 121 | const rows = blocks.map(([label, , name]) => `${label}=${name}`) |
| 122 | assert.ok(rows.includes('Date=insert-date-pick'), rows.join()) |
| 123 | assert.ok(rows.includes('Today’s date=insert-date'), rows.join()) |
| 124 | assert.ok(rows.indexOf('Date=insert-date-pick') < rows.indexOf('Today’s date=insert-date')) |
| 125 | }) |
| 126 | |
| 127 | // Review M1: right after a ! the chip would be an embed, and the ! would |
| 128 | // go from the reading. A space goes first; where even that would not |
| 129 | // read back, the chip is refused, as a letter there would not read back |
| 130 | // either. |
| 131 | test('after a ! the chip goes in after a space, never as an embed', () => { |
| 132 | assert.equal(put('Look!', '!', '2026-11-20').saved, 'Look! [[2026-11-20]]') |
| 133 | assert.equal(put('x! then', 'x!', '2026-11-20').saved, 'x! [[2026-11-20]] then') |
| 134 | const { view } = editor('Look!') |
| 135 | view.dispatch(view.state.tr.setSelection(TextSelection.atEnd(view.state.doc))) |
| 136 | assert.equal(dateInsertion(view.state, '2026-11-20').spaced, true) |
| 137 | }) |
| 138 | |
| 139 | test('where the Markdown would read the chip differently, it is refused', () => { |
| 140 | const { view, fidelity } = editor('`` foo ` bar ``') |
| 141 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, 2))) |
| 142 | assert.deepEqual(dateInsertion(view.state, '2026-11-20'), { refuse: refusals.date }) |
| 143 | assert.equal(view.run(insertDate('2026-11-20')), false) |
| 144 | assert.equal(prepareSave(view, fidelity).body, '`` foo ` bar ``') |
| 145 | // In code, or with no text block, there is no chip and no refusal. |
| 146 | const code = editor('```\ncode\n```').view |
| 147 | code.dispatch(code.state.tr.setSelection(TextSelection.create(code.state.doc, 2))) |
| 148 | assert.equal(dateInsertion(code.state, '2026-11-20'), null) |
| 149 | }) |
| 150 | |
| 151 | // Review M3: over a selection the chip goes at its end, and the words stay. |
| 152 | test('over a selection the chip goes after it and the words stay', () => { |
| 153 | const { view, fidelity } = editor('Call Ana today') |
| 154 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, 1, 9))) |
| 155 | assert.equal(view.run(insertDate('2026-11-20')), true) |
| 156 | assert.equal(prepareSave(view, fidelity).body, 'Call Ana[[2026-11-20]] today') |
| 157 | assert.equal(view.state.selection.empty, true) |
| 158 | const { view: all, fidelity: allFidelity } = editor('One.\n\nTwo.') |
| 159 | all.dispatch(all.state.tr.setSelection(TextSelection.create(all.state.doc, 1, all.state.doc.content.size - 1))) |
| 160 | all.run(blockCommands(schema)['insert-date']) |
| 161 | assert.match(prepareSave(all, allFidelity).body, /^One\.\n\nTwo\.\[\[\d{4}-\d{2}-\d{2}\]\]$/u) |
| 162 | }) |
| 163 | |
| 164 | // Review L2: today is the viewer's zone's (<body data-zone>), not the |
| 165 | // browser's, and is read each time. |
| 166 | test('today is the day in the viewer\'s zone', () => { |
| 167 | const instant = new Date(Date.UTC(2026, 10, 16, 23, 30)) |
| 168 | assert.equal(today(instant, 'Europe/Dublin'), '2026-11-16') |
| 169 | assert.equal(today(instant, 'Asia/Tokyo'), '2026-11-17') |
| 170 | assert.equal(today(instant, 'America/Los_Angeles'), '2026-11-16') |
| 171 | assert.equal(today(new Date(Date.UTC(2026, 10, 17, 9)), 'America/Los_Angeles'), '2026-11-17') |
| 172 | assert.match(today(instant, 'Not/AZone'), /^\d{4}-\d{2}-\d{2}$/u) |
| 173 | }) |
| 174 | |